1 # Copyright Rene Rivera 2015
2 # Distributed under the Boost Software License, Version 1.0.
3 # (See accompanying file LICENSE_1_0.txt or copy at
4 # http://www.boost.org/LICENSE_1_0.txt)
6 # Defines rules that provide requirements based on checking
7 # conditions using Boost Predef definitions and version numbers.
16 import "class" : new ;
19 # Create a project for our targets.
20 project.extension predef check ;
22 # Feature to pass check expressions to check programs.
23 feature.feature predef-expression : : free ;
25 # Checks the expressions and when used evaluates to the true-properties
26 # if the expressions are all true. Otherwise evaluates to the
28 rule check ( expressions + : language ? : true-properties * : false-properties * )
30 # Default to C++ on the check context.
33 local project_target = [ project.target $(__name__) ] ;
34 project.push-current $(project_target) ;
37 for expression in $(expressions)
39 if $(expression:L) in "and" "or"
41 terms += $(expression:L) ;
45 # Create the check run if we don't have one yet.
46 local key = [ MD5 $(language)::$(expression) ] ;
47 if ! ( $(key) in $(_checks_) )
50 _message_(/check/predef//predef_check_cc_$(key)) = $(expression) ;
51 check_target $(language) $(key) : [ change_term_to_def $(expression) ] ;
54 terms += /check/predef//predef_check_cc_$(key) ;
57 local instance = [ new check-expression-evaluator
58 $(terms) : $(true-properties) : $(false-properties) ] ;
59 result = <conditional>@$(instance).check ;
64 # Checks the expressions and when used evaluates to <build>no
65 # if the expressions are all false. Otherwise evaluates to the
67 rule require ( expressions + : language ? )
69 return [ check $(expressions) : $(language) : : <build>no ] ;
72 #############################################################################
79 # Check targets. Each needs to be compiled for different languages
80 # even though they are all the same source code.
81 local rule check_target ( language key : requirements * )
83 # Need to use absolute paths because we don't know the
84 # context of the invocation which affects where the paths
87 = [ modules.binding $(__name__) ] ;
89 = $(predef_jam:D)/predef_check_cc_as_$(language).$(.$(language).ext) ;
91 = $(predef_jam:D)/../../include ;
92 obj predef_check_cc_$(key)
94 : <include>$(include_path) $(requirements) ;
95 explicit predef_check_cc_$(key) ;
96 return predef_check_cc_$(key) ;
99 local rule change_term_to_def ( term )
101 local parts = [ regex.split $(term) " " ] ;
104 local version_number = [ regex.split $(parts[3]) "[.]" ] ;
105 if ! $(version_number[3]) { version_number += "0" ; }
106 if ! $(version_number[2]) { version_number += "0" ; }
107 parts = $(parts[1-2]) BOOST_VERSION_NUMBER($(version_number:J=",")) ;
109 return <define>CHECK=\"$(parts:J=" ")\" ;
112 class check-expression-evaluator
116 rule __init__ ( expression + : true-properties * : false-properties * )
118 self.expression = $(expression) ;
119 self.true-properties = $(true-properties) ;
120 self.false-properties = $(false-properties) ;
123 rule check ( properties * )
126 local tokens = "and" "or" ;
127 # Go through the expression and: eval the target values,
128 # and normalize to a full expression.
129 for local term in $(self.expression)
131 if ! ( $(term:L) in $(tokens) )
133 # A value is a target reference that will evan to "true"
135 if $(to-eval[-1]:L) && ! ( $(to-eval[-1]:L) in $(tokens) )
137 # Default to "and" operation.
140 local message = [ modules.peek predef : _message_($(term)) ] ;
141 if [ configure.builds $(term) : $(properties) : $(message) ]
155 # Eval full the expression.
156 local eval-result = [ eval $(to-eval) ] ;
157 # And resolve true/false properties.
158 if $(eval-result) = "true"
160 return $(self.true-properties) ;
164 return $(self.false-properties) ;
171 if $(e[1]) && $(e[2]) && $(e[3])
175 if $(e[1]) = "true" && $(e[3]) = "true"
177 r = [ eval "true" $(e[4-]) ] ;
181 r = [ eval "false" $(e[4-]) ] ;
184 else if $(e[2]) = "or"
186 if $(e[1]) = "true" || $(e[3]) = "true"
188 r = [ eval "true" $(e[4-]) ] ;
192 r = [ eval "false" $(e[4-]) ] ;