import modules ;
import option ;
import os ;
import path ;
import project ;
import build-system ;
import version ;
#Shell with trailing line removed http://lists.boost.org/boost-build/2007/08/17051.php
rule trim-nl ( str extras * ) {
return [ MATCH "([^
]*)" : $(str) ] $(extras) ;
}
rule _shell ( cmd : extras * ) {
return [ trim-nl [ SHELL $(cmd) : $(extras) ] ] ;
}
rule shell_or_fail ( cmd ) {
local ret = [ SHELL $(cmd) : exit-status ] ;
if $(ret[2]) != 0 {
exit $(cmd) failed : 1 ;
}
}
cxxflags = [ os.environ "CXXFLAGS" ] ;
cflags = [ os.environ "CFLAGS" ] ;
ldflags = [ os.environ "LDFLAGS" ] ;
#Run g++ with empty main and these arguments to see if it passes.
rule test_flags ( flags * : main ? ) {
flags = $(cxxflags) $(ldflags) $(flags) ;
if ! $(main) {
main = "int main() {}" ;
}
local cmd = "bash -c \"g++ "$(flags:J=" ")" -x c++ - <<<'$(main)' -o $(TOP)/dummy >/dev/null 2>/dev/null && rm $(TOP)/dummy 2>/dev/null\"" ;
local ret = [ SHELL $(cmd) : exit-status ] ;
if --debug-configuration in [ modules.peek : ARGV ] {
echo $(cmd) ;
echo $(ret) ;
}
if $(ret[2]) = 0 {
return true ;
} else {
return ;
}
}
rule test_header ( name ) {
return [ test_flags "-include $(name)" ] ;
}
requirements = ;
FORCE-STATIC = [ option.get "static" : : "yes" ] ;
if $(FORCE-STATIC) {
requirements += static static ;
}
rule test_library ( name ) {
if $(FORCE-STATIC) {
return [ test_flags "-Wl,-Bstatic -l$(name) -Wl,-Bdynamic" ] ;
} else {
return [ test_flags "-l$(name)" ] ;
}
}
{
local cleaning = [ option.get "clean" : : yes ] ;
cleaning ?= [ option.get "clean-all" : no : yes ] ;
if "clean" in [ modules.peek : ARGV ] {
cleaning = yes ;
}
constant CLEANING : $(cleaning) ;
}
shared-command-line = ;
local argv = [ modules.peek : ARGV ] ;
while $(argv) {
if $(argv[1]) = "link=shared" {
shared-command-line = shared ;
}
argv = $(argv[2-]) ;
}
#Determine if a library can be compiled statically.
rule auto-shared ( name : additional * ) {
additional ?= "" ;
if $(shared-command-line) = "shared" {
return "shared" ;
} else {
if [ test_flags $(additional)" -Wl,-Bstatic -l"$(name)" -Wl,-Bdynamic" ] {
return ;
} else {
if $(FORCE-STATIC) {
echo "Could not statically link against lib $(name). Your build will probably fail." ;
return ;
} else {
return "shared" ;
}
}
}
}
# MacPorts' default location is /opt/local -- use this if no path is given.
with-macports = [ option.get "with-macports" : : "/opt/local" ] ;
if $(with-macports) {
using darwin ;
ECHO "Using --with-macports=$(with-macports), implying use of darwin GCC" ;
L-boost-search = -L$(with-macports)/lib ;
boost-search = $(with-macports)/lib ;
I-boost-include = -I$(with-macports)/include ;
boost-include = $(with-macports)/include ;
requirements += $(boost-include) ;
} else {
with-boost = [ option.get "with-boost" ] ;
with-boost ?= [ os.environ "BOOST_ROOT" ] ;
if $(with-boost) {
L-boost-search = -L$(with-boost)/lib" "-L$(with-boost)/lib64 ;
boost-search = $(with-boost)/lib $(with-boost)/lib64 ;
I-boost-include = -I$(with-boost)/include ;
boost-include = $(with-boost)/include ;
requirements += $(boost-include) ;
} else {
L-boost-search = "" ;
boost-search = ;
I-boost-include = "" ;
boost-include = ;
}
}
#Convenience rule for boost libraries. Defines library boost_$(name).
rule boost-lib ( name macro : deps * ) {
flags = $(L-boost-search)" -lboost_"$(name)"$(boost-lib-version)" ;
local main ;
if $(name) = "unit_test_framework" {
main = "BOOST_AUTO_TEST_CASE(foo) {}" ;
flags += " -DBOOST_TEST_MODULE=CompileTest $(I-boost-include) -include boost/test/unit_test.hpp" ;
}
if $(boost-auto-shared) = "shared" {
flags += " -DBOOST_$(macro)" ;
} else {
flags = " -Wl,-Bstatic $(flags) -Wl,-Bdynamic " ;
}
if [ test_flags $(flags) : $(main) ] {
lib inner_boost_$(name) : : single $(boost-search) boost_$(name)$(boost-lib-version) : static : $(deps) ;
lib inner_boost_$(name) : : multi $(boost-search) boost_$(name)$(boost-lib-version) : static : $(deps) ;
} else {
lib inner_boost_$(name) : : $(boost-search) boost_$(name)$(boost-lib-version) : : $(deps) ;
}
if $(boost-auto-shared) = "shared" {
alias boost_$(name) : inner_boost_$(name) : shared ;
requirements += BOOST_$(macro) ;
} else {
alias boost_$(name) : inner_boost_$(name) : static ;
}
}
#Argument is e.g. 103600
rule boost ( min-version ) {
local cmd = "bash -c \"g++ "$(I-boost-include)" -dM -x c++ -E /dev/null -include boost/version.hpp 2>/dev/null |grep '#define BOOST_'\"" ;
local boost-shell = [ SHELL "$(cmd)" : exit-status ] ;
if $(boost-shell[2]) != 0 && $(CLEANING) = no {
echo Failed to run "$(cmd)" ;
exit Boost does not seem to be installed or g++ is confused. : 1 ;
}
constant BOOST-VERSION : [ MATCH "#define BOOST_VERSION ([0-9]*)" : $(boost-shell[1]) ] ;
if $(BOOST-VERSION) < $(min-version) && $(CLEANING) = no {
exit You have Boost $(BOOST-VERSION). This package requires Boost at least $(min-version) (and preferably newer). : 1 ;
}
# If matching version tags exist, use them.
boost-lib-version = [ MATCH "#define BOOST_LIB_VERSION \"([^\"]*)\"" : $(boost-shell[1]) ] ;
if [ test_flags $(L-boost-search)" -lboost_program_options-"$(boost-lib-version) ] {
boost-lib-version = "-"$(boost-lib-version) ;
} else {
boost-lib-version = "" ;
}
#Are we linking static binaries against shared boost?
boost-auto-shared = [ auto-shared "boost_program_options"$(boost-lib-version) : $(L-boost-search) ] ;
#See tools/build/v2/contrib/boost.jam in a boost distribution for a table of macros to define.
boost-lib system SYSTEM_DYN_LINK ;
boost-lib thread THREAD_DYN_DLL : boost_system ;
boost-lib program_options PROGRAM_OPTIONS_DYN_LINK ;
boost-lib unit_test_framework TEST_DYN_LINK ;
boost-lib iostreams IOSTREAMS_DYN_LINK ;
boost-lib filesystem FILE_SYSTEM_DYN_LINK ;
# if $(BOOST-VERSION) >= 104800 {
# boost-lib chrono CHRONO_DYN_LINK ;
# boost-lib timer TIMER_DYN_LINK : boost_chrono ;
# }
}
#Link normally to a library, but sometimes static isn't installed so fall back to dynamic.
rule external-lib ( name : search-path * : deps * ) {
lib $(name) : : [ auto-shared $(name) : "-L"$(search-path) ] $(search-path)