# Copyright 2018 Peter Dimov
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at http://boost.org/LICENSE_1_0.txt)

require-b2 5.2 ;

import package ;
import path ;
import sequence ;
import set ;
import project ;
import regex ;

import-search /boost/boost_install ;
import boost-install ;
import boost-install-dirs ;

path-constant LIBS_ROOT : ../.. ;

# header-subdir

local header-subdir = [ boost-install-dirs.header-subdir ] ;
header-subdir ?= "" ;

# install-headers

# first, the 'modular' headers

local modular-headers
    = [ SORT [ MATCH .*libs/(.*)/include/boost : [ glob
        $(LIBS_ROOT)/*/include/boost
        $(LIBS_ROOT)/numeric/*/include/boost
        ] ] ] ;

local skip-headers ;

for local lib in $(modular-headers)
{
    local header-root = $(LIBS_ROOT)/$(lib)/include ;
    local header-boost = $(header-root)/boost ;

    local headers =
        [ path.glob-tree $(header-boost) : *.hpp *.ipp *.h *.inc ]
        [ path.glob-tree $(header-boost)/compatibility/cpp_c_headers : c* ] ;

    skip-headers += [ sequence.transform path.relative-to [ path.make $(header-boost) ] : $(headers) ] ;

    install install-$(lib)-headers
        : $(headers)
        : <location>(includedir)/$(header-subdir)
          <install-source-root>$(header-root)
          <install-no-version-symlinks>on
        : <install-package>Boost
    ;

    explicit install-$(lib)-headers ;
}

# then, the non-modular headers in boost/, minus the modular ones

local headers ;
local header-root ;

if $(BOOST_ROOT)
{
    header-root = [ path.make $(BOOST_ROOT) ] ;

    headers =
        [ path.glob-tree $(BOOST_ROOT)/boost : *.hpp *.ipp *.h *.inc ]
        [ path.glob-tree $(BOOST_ROOT)/boost/compatibility/cpp_c_headers : c* ] ;
}

headers = [ set.difference $(headers) : $(header-root)/$(skip-headers) ] ;

install install-boost-headers
    : $(headers)
    : <location>(includedir)/$(header-subdir)
      <install-source-root>$(header-root)
      <install-no-version-symlinks>on
    : <install-package>Boost
;

explicit install-boost-headers ;

# Boost version format: XYYYZZ

if ! [ modules.peek boostcpp : BOOST_VERSION ]
{
    local boost-config = [ project.search /boost/config ] ;
    ECHO "[INFO] boost-config:" $(boost-config) ;
    if $(boost-config)
    {
        local boost-version-num = [ regex.grep
            [ path.native [ path.join $(boost-config) include boost ] ]
            : version.hpp : "BOOST_VERSION ([0-9]+)" : 1 ] ;
        boost-version-num = [ MATCH "(.*)(...)(..)" : $(boost-version-num[2]) ] ;
        boost-version-num =
            [ CALC $(boost-version-num[1]) + 0 ]
            [ CALC $(boost-version-num[2]) + 0 ]
            [ CALC $(boost-version-num[3]) + 0 ] ;
        modules.poke boostcpp : BOOST_VERSION : $(boost-version-num:J=.) ;
    }
}

#

alias install-headers : install-$(modular-headers)-headers install-boost-headers ;
explicit install-headers ;

# install

alias boost_headers ;

boost-install.install-cmake-config boost_headers ;
explicit install-cmake-config ;

alias install : install-headers install-cmake-config ;
explicit install ;

# stage

boost-install.stage-cmake-config boost_headers ;
explicit stage-cmake-config ;

alias stage : stage-cmake-config ;
explicit stage ;
