1 # Copyright 2001, 2002, 2003 Dave Abrahams
2 # Copyright 2006 Rene Rivera
3 # Copyright 2002, 2003, 2004, 2005, 2006 Vladimir Prus
4 # Copyright 2020 Nikita Kniazev
5 # Distributed under the Boost Software License, Version 1.0.
6 # (See accompanying file LICENSE.txt or copy at
7 # https://www.bfgroup.xyz/b2/LICENSE.txt)
19 # Refines 'properties' by overriding any non-free and non-conditional properties
20 # for which a different value is specified in 'requirements'. Returns the
21 # resulting list of properties.
23 rule refine ( properties * : requirements * )
28 # Collect all non-free features in requirements
29 for local r in $(requirements)
31 # Do not consider conditional requirements.
32 if ! [ MATCH "(:<)" : $(r:G=) ] && ! free in [ feature.attributes $(r:G) ]
34 if ! $(r) in $(properties)
36 # Kill subfeatures of properties that we're changing
37 local sub = [ modules.peek feature : $(r:G).subfeatures ] ;
40 # non-specific subfeatures are still valid
41 sub = [ MATCH "(.*:.*)" : $(sub) ] ;
42 local name = [ utility.ungrist $(r:G) ] ;
43 unset += <$(name)-$(sub)> ;
50 # Remove properties that are overridden by requirements
51 for local p in $(properties)
53 if [ MATCH "(:<)" : $(p:G=) ] || ! $(p:G) in $(unset)
59 return [ sequence.unique $(result) $(requirements) ] ;
63 # Removes all conditional properties whose conditions are not met. For those
64 # with met conditions, removes the condition. Properties in conditions are
65 # looked up in 'context'.
67 rule evaluate-conditionals-in-context ( properties * : context * )
69 # Import here to avoid cyclic dependency
77 for local p in $(properties)
79 if [ MATCH "(:<)" : $(p) ] && ! free in [ feature.attributes $(p:G) ]
81 conditionals += $(p) ;
83 else if $(p:G) = <conditional>
94 for local p in $(conditionals)
96 # Separate condition and property.
97 local s = [ MATCH "^(.*):(<.*)" : $(p) ] ;
98 # Split condition into individual properties.
99 local condition = [ regex.split $(s[1]) "," ] ;
100 # Evaluate condition.
101 if ! [ MATCH ^(!).* : $(condition:G=) ]
103 # Only positive checks
104 if $(condition) in $(context)
106 properties += $(s[2]) ;
111 # Have negative checks
113 for local c in $(condition)
115 local c = [ MATCH ^(!)?(.*) : $(c) ] ;
116 # It is XOR: $(c[1]) = "!" ^ $(c[2]) in $(context)
117 if $(c[1]) = "!" && $(c[2]) in $(context) || $(c[1]) != "!" && ! ( $(c[2]) in $(context) )
125 properties += $(s[2]) ;
129 for local i in [ MATCH "^@(.*)" : $(indirect:G=) ]
131 # If the rule was set in a project module, translate paths
132 # relative to that project's location.
133 local m = [ indirect.get-module $(i) ] ;
134 local p = [ project.target $(m) : allow-missing ] ;
135 local new = [ indirect.call $(i) $(context) ] ;
136 if $(p) && [ $(p).location ]
138 local location = [ $(p).location ] ;
139 local project-id = [ project.attribute $(m) id ] ;
140 project-id ?= [ path.root $(location) [ path.pwd ] ] ;
142 [ translate $(new) : $(project-id) : $(location) : $(m) ] ;
146 properties += $(new) ;
154 # Returns <relevant> properties indicating how the conditionals in
155 # properties affect feature relevance. If the optional argument cond
156 # is passed, it is treated as extra conditions for all properties.
158 rule evaluate-conditional-relevance ( properties * : cond * )
160 cond = [ sequence.transform utility.ungrist : $(cond:G) ] ;
162 for local p in $(properties)
164 # Separate condition and property.
165 local s = [ MATCH "^(.*):(<.*)" : $(p) ] ;
166 if ! $(s) || free in [ feature.attributes $(p:G) ]
168 local value = [ utility.ungrist $(p:G) ] ;
169 result += <relevant>$(value):<relevant>$(cond) ;
173 local condition = [ regex.split $(s[1]) "," ] ;
174 condition = [ MATCH "^!?(.*)" : $(condition) ] ;
175 condition = [ sequence.transform utility.ungrist : $(condition:G) ] $(cond) ;
176 local value = [ utility.ungrist $(s[2]:G) ] ;
177 result += <relevant>$(value):<relevant>$(condition) ;
180 return [ sequence.unique $(result) ] ;
184 rule expand-subfeatures-in-conditions ( properties * )
187 for local p in $(properties)
189 local s = [ MATCH "^(.*):(<.*)" : $(p) ] ;
196 local condition = $(s[1]) ;
197 local value = $(s[2]) ;
198 # Condition might include several elements.
199 condition = [ regex.split $(condition) "," ] ;
201 for local c in $(condition)
203 # It is common for a condition to include a toolset or
204 # subfeatures that have not been defined. In that case we want
205 # the condition to simply 'never be satisfied' and validation
206 # would only produce a spurious error so we prevent it by
207 # passing 'true' as the second parameter.
208 e += [ feature.expand-subfeatures $(c) : true ] ;
210 if $(e) = $(condition)
213 # This is just an optimization and possibly a premature one at
215 # (todo) (12.07.2008.) (Jurko)
220 result += "$(e:J=,):$(value)" ;
228 # Helper for as-path, below. Orders properties with the implicit ones first, and
229 # within the two sections in alphabetical order of feature name.
231 local rule path-order ( x y )
233 if $(y:G) && ! $(x:G)
237 else if $(x:G) && ! $(y:G)
245 x = [ feature.expand-subfeatures $(x) ] ;
246 y = [ feature.expand-subfeatures $(y) ] ;
257 local rule abbreviate-dashed ( string )
260 for local part in [ regex.split $(string) - ]
262 r += [ string.abbreviate $(part) ] ;
268 local rule identity ( string )
274 if --abbreviate-paths in [ modules.peek : ARGV ]
276 .abbrev = abbreviate-dashed ;
284 # Returns a path representing the given expanded property set.
286 rule as-path ( properties * )
288 local entry = .result.$(properties:J=-) ;
293 properties = [ feature.minimize $(properties) ] ;
295 # Sort according to path-order.
296 properties = [ sequence.insertion-sort $(properties) : path-order ] ;
299 for local p in $(properties)
301 if ! hidden in [ feature.attributes $(p:G) ]
305 local f = [ utility.ungrist $(p:G) ] ;
308 components += [ $(.abbrev) $(p) ] ;
312 $(entry) = $(components:J=/) ;
319 # Exit with error if property is not valid.
321 local rule validate1 ( property )
326 local feature = $(property:G) ;
327 local value = $(property:G=) ;
329 if ! [ feature.valid $(feature) ]
331 # Ungrist for better error messages.
332 feature = [ utility.ungrist $(property:G) ] ;
333 msg = "unknown feature '$(feature)'" ;
335 else if $(value) && ! free in [ feature.attributes $(feature) ]
337 feature.validate-value-string $(feature) $(value) ;
339 else if ! ( $(value) || ( optional in [ feature.attributes $(feature) ] ) )
341 # Ungrist for better error messages.
342 feature = [ utility.ungrist $(property:G) ] ;
343 msg = "No value specified for feature '$(feature)'" ;
348 local feature = [ feature.implied-feature $(property) ] ;
349 feature.validate-value-string $(feature) $(property) ;
354 errors.error "Invalid property "'$(property:J=" ")'": "$(msg:J=" "). ;
359 rule validate ( properties * )
361 for local p in $(properties)
368 rule validate-property-sets ( property-sets * )
370 for local s in $(property-sets)
372 validate [ feature.split $(s) ] ;
377 # Expands any implicit property values in the given property 'specification' so
378 # they explicitly state their feature.
380 rule make ( specification * )
383 for local e in $(specification)
389 else if [ feature.is-implicit-value $(e) ]
391 local feature = [ feature.implied-feature $(e) ] ;
392 result += $(feature)$(e) ;
397 errors.error "'$(e)' is not a valid property specification" ;
404 # Returns a property set containing all the elements in 'properties' that do not
405 # have their attributes listed in 'attributes'.
407 rule remove ( attributes + : properties * )
410 for local e in $(properties)
412 if ! [ set.intersection $(attributes) : [ feature.attributes $(e:G) ] ]
421 # Returns a property set containing all the elements in 'properties' that have
422 # their attributes listed in 'attributes'.
424 rule take ( attributes + : properties * )
427 for local e in $(properties)
429 if [ set.intersection $(attributes) : [ feature.attributes $(e:G) ] ]
438 # Selects properties corresponding to any of the given features.
440 rule select ( features * : properties * )
444 # Add any missing angle brackets.
446 features = $(empty:G=$(features)) ;
448 for local p in $(properties)
450 if $(p:G) in $(features)
459 # Returns a modified version of properties with all values of the given feature
460 # replaced by the given value. If 'value' is empty the feature will be removed.
462 rule change ( properties * : feature value ? )
465 for local p in $(properties)
467 if $(p:G) = $(feature)
469 result += $(value:G=$(feature)) ;
480 # If 'property' is a conditional property, returns the condition and the
481 # property. E.g. <variant>debug,<toolset>gcc:<inlining>full will become
482 # <variant>debug,<toolset>gcc <inlining>full. Otherwise, returns an empty
485 rule split-conditional ( property )
487 return [ MATCH "^(.+):(<.+)" : $(property) ] ;
491 rule translate-path-value ( value : path )
494 for local v in [ regex.split $(value) "&&" ]
496 t += [ path.root [ path.make $(v) ] $(path) ] ;
498 return $(t:TJ="&&") ;
501 rule translate-dependency-value ( value : project-id : project-location )
503 local split-target = [ regex.match ^(.*)//(.*) : $(value) ] ;
506 local rooted = [ path.root [ path.make $(split-target[1]) ]
507 [ path.root $(project-location) [ path.pwd ] ] ] ;
508 return $(rooted)//$(split-target[2]) ;
510 else if [ path.is-rooted $(value) ]
516 return $(project-id)//$(value) ;
520 rule translate-indirect-value ( rulename : context-module )
522 if [ MATCH "^([^%]*)%([^%]+)$" : $(rulename) ]
524 # Rule is already in the 'indirect-rule' format.
525 return @$(rulename) ;
530 if ! [ MATCH "([.])" : $(rulename) ]
532 # This is an unqualified rule name. The user might want to
533 # set flags on this rule name and toolset.flag
534 # auto-qualifies it. Need to do the same here so flag
535 # setting works. We can arrange for toolset.flag to *not*
536 # auto-qualify the argument but then two rules defined in
537 # two Jamfiles would conflict.
538 rulename = $(context-module).$(rulename) ;
540 v = [ indirect.make $(rulename) : $(context-module) ] ;
546 # Equivalent to a calling all of:
549 # translate-dependency
550 # expand-subfeatures-in-conditions
553 rule translate ( properties * : project-id : project-location : context-module )
555 local translate-path-rule = [ MATCH "^<translate-path>[@](.*)$" : "$(properties)" ] ;
557 for local p in $(properties)
559 local split = [ split-conditional $(p) ] ;
560 local condition property ;
564 condition = $(split[1]) ;
565 property = $(split[2]) ;
568 for local c in [ regex.split $(condition) "," ]
570 # strip negation for expansion and readd after
571 c = [ MATCH "^(!)?(.*)" : $(c) ] ;
572 local expanded = [ feature.expand-subfeatures $(c[2]) : true ] ;
573 e += $(c[1])$(expanded) ;
576 condition = "$(e:J=,):" ;
583 local feature = $(property:G) ;
586 if [ feature.is-implicit-value $(property) ]
588 feature = [ feature.implied-feature $(property) ] ;
589 result += $(condition:E=)$(feature)$(property) ;
594 errors.error "'$(property)' is not a valid property specification" ;
597 local attributes = [ feature.attributes $(feature) ] ;
599 # Only free features should be translated
600 if free in $(attributes)
602 if path in $(attributes)
604 if $(translate-path-rule)
606 value = [ $(translate-path-rule) $(feature) $(property:G=) : $(properties) : $(project-id) : $(project-location) ] ;
610 value = [ translate-path-value $(property:G=) : $(project-location) ] ;
612 result += $(condition:E=)$(feature)$(value) ;
614 else if dependency in $(attributes)
616 value = [ translate-dependency-value $(property:G=) : $(project-id) : $(project-location) ] ;
617 result += $(condition:E=)$(feature)$(value) ;
621 local m = [ MATCH ^@(.+) : $(property:G=) ] ;
624 value = [ translate-indirect-value $(m) : $(context-module) ] ;
625 result += $(condition:E=)$(feature)$(value) ;
629 result += $(condition:E=)$(property) ;
635 result += $(condition:E=)$(property) ;
642 # Interpret all path properties in 'properties' as relative to 'path'. The
643 # property values are assumed to be in system-specific form, and will be
644 # translated into normalized form.
646 rule translate-paths ( properties * : path )
649 for local p in $(properties)
651 local split = [ split-conditional $(p) ] ;
652 local condition = "" ;
655 condition = "$(split[1]):" ;
659 if path in [ feature.attributes $(p:G) ]
661 local values = [ regex.split $(p:TG=) "&&" ] ;
663 for local v in $(values)
665 t += [ path.root [ path.make $(v) ] $(path) ] ;
668 result += $(condition)$(t:TG=$(p:G)) ;
672 result += $(condition)$(p) ;
679 # Assumes that all feature values that start with '@' are names of rules, used
680 # in 'context-module'. Such rules can be either local to the module or global.
681 # Converts such values into 'indirect-rule' format (see indirect.jam), so they
682 # can be called from other modules. Does nothing for such values that are
683 # already in the 'indirect-rule' format.
685 rule translate-indirect ( specification * : context-module )
688 for local p in $(specification)
690 local m = [ MATCH ^@(.+) : $(p:G=) ] ;
694 if [ MATCH "^([^%]*)%([^%]+)$" : $(m) ]
696 # Rule is already in the 'indirect-rule' format.
701 if ! [ MATCH "([.])" : $(m) ]
703 # This is an unqualified rule name. The user might want to
704 # set flags on this rule name and toolset.flag
705 # auto-qualifies it. Need to do the same here so flag
706 # setting works. We can arrange for toolset.flag to *not*
707 # auto-qualify the argument but then two rules defined in
708 # two Jamfiles would conflict.
709 m = $(context-module).$(m) ;
711 v = [ indirect.make $(m) : $(context-module) ] ;
715 result += $(v:G=$(p:G)) ;
726 # Binds all dependency properties in a list relative to the given project.
727 # Targets with absolute paths will be left unchanged and targets which have a
728 # project specified will have the path to the project interpreted relative to
729 # the specified location.
731 rule translate-dependencies ( specification * : project-id : location )
734 for local p in $(specification)
736 local split = [ split-conditional $(p) ] ;
737 local condition = "" ;
740 condition = "$(split[1]):" ;
743 if dependency in [ feature.attributes $(p:G) ]
745 local split-target = [ regex.match ^(.*)//(.*) : $(p:G=) ] ;
748 local rooted = [ path.root [ path.make $(split-target[1]) ]
749 [ path.root $(location) [ path.pwd ] ] ] ;
750 result += $(condition)$(p:G)$(rooted)//$(split-target[2]) ;
752 else if [ path.is-rooted $(p:G=) ]
754 result += $(condition)$(p) ;
758 result += $(condition)$(p:G)$(project-id)//$(p:G=) ;
763 result += $(condition)$(p) ;
770 # Class maintaining a property set -> string mapping.
782 # Associate 'value' with 'properties'.
784 rule insert ( properties * : value )
786 self.all-flags += self.$(self.next-flag) ;
787 self.$(self.next-flag) = $(value) $(properties) ;
789 self.next-flag = [ numbers.increment $(self.next-flag) ] ;
792 # Returns the value associated with 'properties' or any subset of it. If
793 # more than one subset has a value assigned to it, returns the value for the
794 # longest subset, if it is unique.
796 rule find ( property-set )
798 # First find all matches.
801 for local i in $(self.all-flags)
803 local list = $($(i)) ;
804 if [ $(property-set).contains-raw $(list[2-]) ]
806 matches += $(list[1]) ;
807 match-ranks += [ sequence.length $(list) ] ;
810 local best = [ sequence.select-highest-ranked $(matches)
814 import errors : error : errors.error ;
815 properties = [ $(property-set).raw ] ;
816 errors.error "Ambiguous key $(properties:J= :E=)" ;
821 # Returns the value associated with 'properties'. If 'value' parameter is
822 # given, replaces the found value.
824 rule find-replace ( properties * : value ? )
826 # First find all matches.
829 for local i in $(self.all-flags)
831 if $($(i)[2-]) in $(properties)
834 match-ranks += [ sequence.length $($(i)) ] ;
837 local best = [ sequence.select-highest-ranked $(matches)
841 import errors : error : errors.error ;
842 errors.error "Ambiguous key $(properties:J= :E=)" ;
844 local original = $($(best)[1]) ;
847 $(best) = $(value) $($(best)[2-]) ;
857 import "class" : new ;
858 import errors : try catch ;
861 # Local rules must be explicitly re-imported.
862 import property : path-order abbreviate-dashed ;
864 feature.prepare-test property-test-temp ;
866 feature.feature toolset : gcc : implicit symmetric ;
867 feature.subfeature toolset gcc : version : 2.95.2 2.95.3 2.95.4 3.0 3.0.1
869 feature.feature define : : free ;
870 feature.feature runtime-link : dynamic static : symmetric link-incompatible ;
871 feature.feature optimization : on off ;
872 feature.feature variant : debug release : implicit composite symmetric ;
873 feature.feature rtti : on off : link-incompatible ;
875 feature.compose <variant>debug : <define>_DEBUG <optimization>off ;
876 feature.compose <variant>release : <define>NDEBUG <optimization>on ;
878 validate <toolset>gcc <toolset>gcc-3.0.1 : $(test-space) ;
880 assert.true path-order $(test-space) debug <define>foo ;
881 assert.false path-order $(test-space) <define>foo debug ;
882 assert.true path-order $(test-space) gcc debug ;
883 assert.false path-order $(test-space) debug gcc ;
884 assert.true path-order $(test-space) <optimization>on <rtti>on ;
885 assert.false path-order $(test-space) <rtti>on <optimization>on ;
887 assert.result-set-equal <toolset>gcc <rtti>off <define>FOO
888 : refine <toolset>gcc <rtti>off
892 assert.result-set-equal <toolset>gcc <optimization>on
893 : refine <toolset>gcc <optimization>off
897 assert.result-set-equal <toolset>gcc <rtti>off
898 : refine <toolset>gcc : <rtti>off : $(test-space) ;
900 assert.result-set-equal <toolset>gcc <rtti>off <rtti>off:<define>FOO
901 : refine <toolset>gcc : <rtti>off <rtti>off:<define>FOO
904 assert.result-set-equal <toolset>gcc:<define>foo <toolset>gcc:<define>bar
905 : refine <toolset>gcc:<define>foo : <toolset>gcc:<define>bar
909 : evaluate-conditionals-in-context
910 <variant>release,<rtti>off:<define>MY_RELEASE
911 : <toolset>gcc <variant>release <rtti>on ;
913 assert.result <define>MY_RELEASE
914 : evaluate-conditionals-in-context
915 <variant>release,<rtti>off:<define>MY_RELEASE
916 : <toolset>gcc <variant>release <rtti>off ;
918 assert.result <define>MY_RELEASE
919 : evaluate-conditionals-in-context
920 <variant>release,!<rtti>off:<define>MY_RELEASE
921 : <toolset>gcc <variant>release <rtti>on ;
924 : evaluate-conditionals-in-context
925 <variant>release,!<rtti>off:<define>MY_RELEASE
926 : <toolset>gcc <variant>release <rtti>off ;
929 : as-path <optimization>off <variant>debug
932 assert.result gcc/debug/rtti-off
933 : as-path <toolset>gcc <optimization>off <rtti>off <variant>debug
936 assert.result optmz-off : abbreviate-dashed optimization-off ;
937 assert.result rntm-lnk-sttc : abbreviate-dashed runtime-link-static ;
940 validate <feature>value : $(test-space) ;
941 catch "Invalid property '<feature>value': unknown feature 'feature'." ;
944 validate <rtti>default : $(test-space) ;
945 catch \"default\" is not a known value of feature <rtti> ;
947 validate <define>WHATEVER : $(test-space) ;
950 validate <rtti> : $(test-space) ;
951 catch "Invalid property '<rtti>': No value specified for feature 'rtti'." ;
954 validate value : $(test-space) ;
955 catch \"value\" is not an implicit feature value ;
957 assert.result-set-equal <rtti>on
958 : remove free implicit : <toolset>gcc <define>foo <rtti>on : $(test-space) ;
960 assert.result-set-equal <include>a
961 : select include : <include>a <toolset>gcc ;
963 assert.result-set-equal <include>a
964 : select include bar : <include>a <toolset>gcc ;
966 assert.result-set-equal <include>a <toolset>gcc
967 : select include <bar> <toolset> : <include>a <toolset>gcc ;
969 assert.result-set-equal <toolset>kylix <include>a
970 : change <toolset>gcc <include>a : <toolset> kylix ;
972 pm = [ new property-map ] ;
973 $(pm).insert <toolset>gcc : o ;
974 $(pm).insert <toolset>gcc <os>NT : obj ;
975 $(pm).insert <toolset>gcc <os>CYGWIN : obj ;
978 $(pm).find [ new property-set <toolset>gcc <os>NT <os>CYGWIN ] ;
979 catch "Ambiguous key <toolset>gcc <os>NT <os>CYGWIN" ;
981 assert.equal o : [ $(pm).find-replace <toolset>gcc ] ;
983 assert.equal obj : [ $(pm).find-replace <toolset>gcc <os>NT ] ;
986 $(pm).find-replace <toolset>gcc <os>NT <os>CYGWIN ;
987 catch "Ambiguous key <toolset>gcc <os>NT <os>CYGWIN" ;
989 # Test ordinary properties.
990 assert.result : split-conditional <toolset>gcc ;
992 # Test properties with ":".
993 assert.result : split-conditional <define>"FOO=A::B" ;
995 # Test conditional feature.
996 assert.result-set-equal <toolset>gcc,<toolset-gcc:version>3.0 <define>FOO
997 : split-conditional <toolset>gcc,<toolset-gcc:version>3.0:<define>FOO ;
999 # Test translate does not choke on negations in conditional
1000 assert.result <toolset>gcc,!<rtti>off:<define>HELLO
1001 : translate <toolset>gcc,!<rtti>off:<define>HELLO
1002 : project-id : project-location : context-module ;
1004 feature.finish-test property-test-temp ;