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_1_0.txt or copy at
7 # http://www.boost.org/LICENSE_1_0.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 * )
72 for local p in $(properties)
74 if [ MATCH "(:<)" : $(p) ] && ! free in [ feature.attributes $(p:G) ]
76 conditionals += $(p) ;
78 else if $(p:G) = <conditional>
88 local result = $(base) ;
89 for local p in $(conditionals)
91 # Separate condition and property.
92 local s = [ MATCH "^(.*):(<.*)" : $(p) ] ;
93 # Split condition into individual properties.
94 local condition = [ regex.split $(s[1]) "," ] ;
96 if ! [ MATCH ^(!).* : $(condition:G=) ]
98 # Only positive checks
99 if $(condition) in $(context)
106 # Have negative checks
108 for local c in $(condition)
110 local c = [ MATCH ^(!)?(.*) : $(c) ] ;
111 # It is XOR: $(c[1]) = "!" ^ $(c[2]) in $(context)
112 if $(c[1]) = "!" && $(c[2]) in $(context) || $(c[1]) != "!" && ! ( $(c[2]) in $(context) )
124 # Import here to avoid cyclic dependency
126 for local i in [ MATCH "^@(.*)" : $(indirect:G=) ]
128 # If the rule was set in a project module, translate paths
129 # relative to that project's location.
130 local m = [ indirect.get-module $(i) ] ;
131 local p = [ project.target $(m) : allow-missing ] ;
132 local new = [ indirect.call $(i) $(context) ] ;
133 if $(p) && [ $(p).location ]
135 local location = [ $(p).location ] ;
136 local project-id = [ project.attribute $(m) id ] ;
137 project-id ?= [ path.root $(location) [ path.pwd ] ] ;
139 [ translate $(new) : $(project-id) : $(location) : $(m) ] ;
150 # Returns <relevant> properties indicating how the conditionals in
151 # properties affect feature relevance. If the optional argument cond
152 # is passed, it is treated as extra conditions for all properties.
154 rule evaluate-conditional-relevance ( properties * : cond * )
156 cond = [ sequence.transform utility.ungrist : $(cond:G) ] ;
158 for local p in $(properties)
160 # Separate condition and property.
161 local s = [ MATCH "^(.*):(<.*)" : $(p) ] ;
162 if ! $(s) || free in [ feature.attributes $(p:G) ]
164 local value = [ utility.ungrist $(p:G) ] ;
165 result += <relevant>$(value):<relevant>$(cond) ;
169 local condition = [ regex.split $(s[1]) "," ] ;
170 condition = [ MATCH "^!?(.*)" : $(condition) ] ;
171 condition = [ sequence.transform utility.ungrist : $(condition:G) ] $(cond) ;
172 local value = [ utility.ungrist $(s[2]:G) ] ;
173 result += <relevant>$(value):<relevant>$(condition) ;
176 return [ sequence.unique $(result) ] ;
180 rule expand-subfeatures-in-conditions ( properties * )
183 for local p in $(properties)
185 local s = [ MATCH "^(.*):(<.*)" : $(p) ] ;
192 local condition = $(s[1]) ;
193 local value = $(s[2]) ;
194 # Condition might include several elements.
195 condition = [ regex.split $(condition) "," ] ;
197 for local c in $(condition)
199 # It is common for a condition to include a toolset or
200 # subfeatures that have not been defined. In that case we want
201 # the condition to simply 'never be satisfied' and validation
202 # would only produce a spurious error so we prevent it by
203 # passing 'true' as the second parameter.
204 e += [ feature.expand-subfeatures $(c) : true ] ;
206 if $(e) = $(condition)
209 # This is just an optimization and possibly a premature one at
211 # (todo) (12.07.2008.) (Jurko)
216 result += "$(e:J=,):$(value)" ;
224 # Helper for as-path, below. Orders properties with the implicit ones first, and
225 # within the two sections in alphabetical order of feature name.
227 local rule path-order ( x y )
229 if $(y:G) && ! $(x:G)
233 else if $(x:G) && ! $(y:G)
241 x = [ feature.expand-subfeatures $(x) ] ;
242 y = [ feature.expand-subfeatures $(y) ] ;
253 local rule abbreviate-dashed ( string )
256 for local part in [ regex.split $(string) - ]
258 r += [ string.abbreviate $(part) ] ;
264 local rule identity ( string )
270 if --abbreviate-paths in [ modules.peek : ARGV ]
272 .abbrev = abbreviate-dashed ;
280 # Returns a path representing the given expanded property set.
282 rule as-path ( properties * )
284 local entry = .result.$(properties:J=-) ;
289 properties = [ feature.minimize $(properties) ] ;
291 # Sort according to path-order.
292 properties = [ sequence.insertion-sort $(properties) : path-order ] ;
295 for local p in $(properties)
297 if ! hidden in [ feature.attributes $(p:G) ]
301 local f = [ utility.ungrist $(p:G) ] ;
304 components += [ $(.abbrev) $(p) ] ;
308 $(entry) = $(components:J=/) ;
315 # Exit with error if property is not valid.
317 local rule validate1 ( property )
322 local feature = $(property:G) ;
323 local value = $(property:G=) ;
325 if ! [ feature.valid $(feature) ]
327 # Ungrist for better error messages.
328 feature = [ utility.ungrist $(property:G) ] ;
329 msg = "unknown feature '$(feature)'" ;
331 else if $(value) && ! free in [ feature.attributes $(feature) ]
333 feature.validate-value-string $(feature) $(value) ;
335 else if ! ( $(value) || ( optional in [ feature.attributes $(feature) ] ) )
337 # Ungrist for better error messages.
338 feature = [ utility.ungrist $(property:G) ] ;
339 msg = "No value specified for feature '$(feature)'" ;
344 local feature = [ feature.implied-feature $(property) ] ;
345 feature.validate-value-string $(feature) $(property) ;
350 errors.error "Invalid property "'$(property:J=" ")'": "$(msg:J=" "). ;
355 rule validate ( properties * )
357 for local p in $(properties)
364 rule validate-property-sets ( property-sets * )
366 for local s in $(property-sets)
368 validate [ feature.split $(s) ] ;
373 # Expands any implicit property values in the given property 'specification' so
374 # they explicitly state their feature.
376 rule make ( specification * )
379 for local e in $(specification)
385 else if [ feature.is-implicit-value $(e) ]
387 local feature = [ feature.implied-feature $(e) ] ;
388 result += $(feature)$(e) ;
393 errors.error "'$(e)' is not a valid property specification" ;
400 # Returns a property set containing all the elements in 'properties' that do not
401 # have their attributes listed in 'attributes'.
403 rule remove ( attributes + : properties * )
406 for local e in $(properties)
408 if ! [ set.intersection $(attributes) : [ feature.attributes $(e:G) ] ]
417 # Returns a property set containing all the elements in 'properties' that have
418 # their attributes listed in 'attributes'.
420 rule take ( attributes + : properties * )
423 for local e in $(properties)
425 if [ set.intersection $(attributes) : [ feature.attributes $(e:G) ] ]
434 # Selects properties corresponding to any of the given features.
436 rule select ( features * : properties * )
440 # Add any missing angle brackets.
442 features = $(empty:G=$(features)) ;
444 for local p in $(properties)
446 if $(p:G) in $(features)
455 # Returns a modified version of properties with all values of the given feature
456 # replaced by the given value. If 'value' is empty the feature will be removed.
458 rule change ( properties * : feature value ? )
461 for local p in $(properties)
463 if $(p:G) = $(feature)
465 result += $(value:G=$(feature)) ;
476 # If 'property' is a conditional property, returns the condition and the
477 # property. E.g. <variant>debug,<toolset>gcc:<inlining>full will become
478 # <variant>debug,<toolset>gcc <inlining>full. Otherwise, returns an empty
481 rule split-conditional ( property )
483 return [ MATCH "^(.+):(<.+)" : $(property) ] ;
487 rule translate-path-value ( value : path )
490 for local v in [ regex.split $(value) "&&" ]
492 t += [ path.root [ path.make $(v) ] $(path) ] ;
494 return $(t:TJ="&&") ;
497 rule translate-dependency-value ( value : project-id : project-location )
499 local split-target = [ regex.match ^(.*)//(.*) : $(value) ] ;
502 local rooted = [ path.root [ path.make $(split-target[1]) ]
503 [ path.root $(project-location) [ path.pwd ] ] ] ;
504 return $(rooted)//$(split-target[2]) ;
506 else if [ path.is-rooted $(value) ]
512 return $(project-id)//$(value) ;
516 rule translate-indirect-value ( rulename : context-module )
518 if [ MATCH "^([^%]*)%([^%]+)$" : $(rulename) ]
520 # Rule is already in the 'indirect-rule' format.
521 return @$(rulename) ;
526 if ! [ MATCH "([.])" : $(rulename) ]
528 # This is an unqualified rule name. The user might want to
529 # set flags on this rule name and toolset.flag
530 # auto-qualifies it. Need to do the same here so flag
531 # setting works. We can arrange for toolset.flag to *not*
532 # auto-qualify the argument but then two rules defined in
533 # two Jamfiles would conflict.
534 rulename = $(context-module).$(rulename) ;
536 v = [ indirect.make $(rulename) : $(context-module) ] ;
542 # Equivalent to a calling all of:
545 # translate-dependency
546 # expand-subfeatures-in-conditions
549 rule translate ( properties * : project-id : project-location : context-module )
551 local translate-path-rule = [ MATCH "^<translate-path>[@](.*)$" : "$(properties)" ] ;
553 for local p in $(properties)
555 local split = [ split-conditional $(p) ] ;
556 local condition property ;
560 condition = $(split[1]) ;
561 property = $(split[2]) ;
564 for local c in [ regex.split $(condition) "," ]
566 # strip negation for expansion and readd after
567 c = [ MATCH "^(!)?(.*)" : $(c) ] ;
568 local expanded = [ feature.expand-subfeatures $(c[2]) : true ] ;
569 e += $(c[1])$(expanded) ;
572 condition = "$(e:J=,):" ;
579 local feature = $(property:G) ;
582 if [ feature.is-implicit-value $(property) ]
584 feature = [ feature.implied-feature $(property) ] ;
585 result += $(condition:E=)$(feature)$(property) ;
590 errors.error "'$(property)' is not a valid property specification" ;
593 local attributes = [ feature.attributes $(feature) ] ;
595 # Only free features should be translated
596 if free in $(attributes)
598 if path in $(attributes)
600 if $(translate-path-rule)
602 value = [ $(translate-path-rule) $(feature) $(property:G=) : $(properties) : $(project-id) : $(project-location) ] ;
606 value = [ translate-path-value $(property:G=) : $(project-location) ] ;
608 result += $(condition:E=)$(feature)$(value) ;
610 else if dependency in $(attributes)
612 value = [ translate-dependency-value $(property:G=) : $(project-id) : $(project-location) ] ;
613 result += $(condition:E=)$(feature)$(value) ;
617 local m = [ MATCH ^@(.+) : $(property:G=) ] ;
620 value = [ translate-indirect-value $(m) : $(context-module) ] ;
621 result += $(condition:E=)$(feature)$(value) ;
625 result += $(condition:E=)$(property) ;
631 result += $(condition:E=)$(property) ;
638 # Interpret all path properties in 'properties' as relative to 'path'. The
639 # property values are assumed to be in system-specific form, and will be
640 # translated into normalized form.
642 rule translate-paths ( properties * : path )
645 for local p in $(properties)
647 local split = [ split-conditional $(p) ] ;
648 local condition = "" ;
651 condition = "$(split[1]):" ;
655 if path in [ feature.attributes $(p:G) ]
657 local values = [ regex.split $(p:TG=) "&&" ] ;
659 for local v in $(values)
661 t += [ path.root [ path.make $(v) ] $(path) ] ;
664 result += $(condition)$(t:TG=$(p:G)) ;
668 result += $(condition)$(p) ;
675 # Assumes that all feature values that start with '@' are names of rules, used
676 # in 'context-module'. Such rules can be either local to the module or global.
677 # Converts such values into 'indirect-rule' format (see indirect.jam), so they
678 # can be called from other modules. Does nothing for such values that are
679 # already in the 'indirect-rule' format.
681 rule translate-indirect ( specification * : context-module )
684 for local p in $(specification)
686 local m = [ MATCH ^@(.+) : $(p:G=) ] ;
690 if [ MATCH "^([^%]*)%([^%]+)$" : $(m) ]
692 # Rule is already in the 'indirect-rule' format.
697 if ! [ MATCH "([.])" : $(m) ]
699 # This is an unqualified rule name. The user might want to
700 # set flags on this rule name and toolset.flag
701 # auto-qualifies it. Need to do the same here so flag
702 # setting works. We can arrange for toolset.flag to *not*
703 # auto-qualify the argument but then two rules defined in
704 # two Jamfiles would conflict.
705 m = $(context-module).$(m) ;
707 v = [ indirect.make $(m) : $(context-module) ] ;
711 result += $(v:G=$(p:G)) ;
722 # Binds all dependency properties in a list relative to the given project.
723 # Targets with absolute paths will be left unchanged and targets which have a
724 # project specified will have the path to the project interpreted relative to
725 # the specified location.
727 rule translate-dependencies ( specification * : project-id : location )
730 for local p in $(specification)
732 local split = [ split-conditional $(p) ] ;
733 local condition = "" ;
736 condition = "$(split[1]):" ;
739 if dependency in [ feature.attributes $(p:G) ]
741 local split-target = [ regex.match ^(.*)//(.*) : $(p:G=) ] ;
744 local rooted = [ path.root [ path.make $(split-target[1]) ]
745 [ path.root $(location) [ path.pwd ] ] ] ;
746 result += $(condition)$(p:G)$(rooted)//$(split-target[2]) ;
748 else if [ path.is-rooted $(p:G=) ]
750 result += $(condition)$(p) ;
754 result += $(condition)$(p:G)$(project-id)//$(p:G=) ;
759 result += $(condition)$(p) ;
766 # Class maintaining a property set -> string mapping.
778 # Associate 'value' with 'properties'.
780 rule insert ( properties * : value )
782 self.all-flags += self.$(self.next-flag) ;
783 self.$(self.next-flag) = $(value) $(properties) ;
785 self.next-flag = [ numbers.increment $(self.next-flag) ] ;
788 # Returns the value associated with 'properties' or any subset of it. If
789 # more than one subset has a value assigned to it, returns the value for the
790 # longest subset, if it is unique.
792 rule find ( property-set )
794 # First find all matches.
797 for local i in $(self.all-flags)
799 local list = $($(i)) ;
800 if [ $(property-set).contains-raw $(list[2-]) ]
802 matches += $(list[1]) ;
803 match-ranks += [ sequence.length $(list) ] ;
806 local best = [ sequence.select-highest-ranked $(matches)
810 import errors : error : errors.error ;
811 errors.error "Ambiguous key $(properties:J= :E=)" ;
816 # Returns the value associated with 'properties'. If 'value' parameter is
817 # given, replaces the found value.
819 rule find-replace ( properties * : value ? )
821 # First find all matches.
824 for local i in $(self.all-flags)
826 if $($(i)[2-]) in $(properties)
829 match-ranks += [ sequence.length $($(i)) ] ;
832 local best = [ sequence.select-highest-ranked $(matches)
836 import errors : error : errors.error ;
837 errors.error "Ambiguous key $(properties:J= :E=)" ;
839 local original = $($(best)[1]) ;
842 $(best) = $(value) $($(best)[2-]) ;
852 import "class" : new ;
853 import errors : try catch ;
856 # Local rules must be explicitly re-imported.
857 import property : path-order abbreviate-dashed ;
859 feature.prepare-test property-test-temp ;
861 feature.feature toolset : gcc : implicit symmetric ;
862 feature.subfeature toolset gcc : version : 2.95.2 2.95.3 2.95.4 3.0 3.0.1
864 feature.feature define : : free ;
865 feature.feature runtime-link : dynamic static : symmetric link-incompatible ;
866 feature.feature optimization : on off ;
867 feature.feature variant : debug release : implicit composite symmetric ;
868 feature.feature rtti : on off : link-incompatible ;
870 feature.compose <variant>debug : <define>_DEBUG <optimization>off ;
871 feature.compose <variant>release : <define>NDEBUG <optimization>on ;
873 validate <toolset>gcc <toolset>gcc-3.0.1 : $(test-space) ;
875 assert.true path-order $(test-space) debug <define>foo ;
876 assert.false path-order $(test-space) <define>foo debug ;
877 assert.true path-order $(test-space) gcc debug ;
878 assert.false path-order $(test-space) debug gcc ;
879 assert.true path-order $(test-space) <optimization>on <rtti>on ;
880 assert.false path-order $(test-space) <rtti>on <optimization>on ;
882 assert.result-set-equal <toolset>gcc <rtti>off <define>FOO
883 : refine <toolset>gcc <rtti>off
887 assert.result-set-equal <toolset>gcc <optimization>on
888 : refine <toolset>gcc <optimization>off
892 assert.result-set-equal <toolset>gcc <rtti>off
893 : refine <toolset>gcc : <rtti>off : $(test-space) ;
895 assert.result-set-equal <toolset>gcc <rtti>off <rtti>off:<define>FOO
896 : refine <toolset>gcc : <rtti>off <rtti>off:<define>FOO
899 assert.result-set-equal <toolset>gcc:<define>foo <toolset>gcc:<define>bar
900 : refine <toolset>gcc:<define>foo : <toolset>gcc:<define>bar
904 : evaluate-conditionals-in-context
905 <variant>release,<rtti>off:<define>MY_RELEASE
906 : <toolset>gcc <variant>release <rtti>on ;
908 assert.result <define>MY_RELEASE
909 : evaluate-conditionals-in-context
910 <variant>release,<rtti>off:<define>MY_RELEASE
911 : <toolset>gcc <variant>release <rtti>off ;
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>on ;
919 : evaluate-conditionals-in-context
920 <variant>release,!<rtti>off:<define>MY_RELEASE
921 : <toolset>gcc <variant>release <rtti>off ;
924 : as-path <optimization>off <variant>debug
927 assert.result gcc/debug/rtti-off
928 : as-path <toolset>gcc <optimization>off <rtti>off <variant>debug
931 assert.result optmz-off : abbreviate-dashed optimization-off ;
932 assert.result rntm-lnk-sttc : abbreviate-dashed runtime-link-static ;
935 validate <feature>value : $(test-space) ;
936 catch "Invalid property '<feature>value': unknown feature 'feature'." ;
939 validate <rtti>default : $(test-space) ;
940 catch \"default\" is not a known value of feature <rtti> ;
942 validate <define>WHATEVER : $(test-space) ;
945 validate <rtti> : $(test-space) ;
946 catch "Invalid property '<rtti>': No value specified for feature 'rtti'." ;
949 validate value : $(test-space) ;
950 catch \"value\" is not an implicit feature value ;
952 assert.result-set-equal <rtti>on
953 : remove free implicit : <toolset>gcc <define>foo <rtti>on : $(test-space) ;
955 assert.result-set-equal <include>a
956 : select include : <include>a <toolset>gcc ;
958 assert.result-set-equal <include>a
959 : select include bar : <include>a <toolset>gcc ;
961 assert.result-set-equal <include>a <toolset>gcc
962 : select include <bar> <toolset> : <include>a <toolset>gcc ;
964 assert.result-set-equal <toolset>kylix <include>a
965 : change <toolset>gcc <include>a : <toolset> kylix ;
967 pm = [ new property-map ] ;
968 $(pm).insert <toolset>gcc : o ;
969 $(pm).insert <toolset>gcc <os>NT : obj ;
970 $(pm).insert <toolset>gcc <os>CYGWIN : obj ;
972 assert.equal o : [ $(pm).find-replace <toolset>gcc ] ;
974 assert.equal obj : [ $(pm).find-replace <toolset>gcc <os>NT ] ;
977 $(pm).find-replace <toolset>gcc <os>NT <os>CYGWIN ;
978 catch "Ambiguous key <toolset>gcc <os>NT <os>CYGWIN" ;
980 # Test ordinary properties.
981 assert.result : split-conditional <toolset>gcc ;
983 # Test properties with ":".
984 assert.result : split-conditional <define>"FOO=A::B" ;
986 # Test conditional feature.
987 assert.result-set-equal <toolset>gcc,<toolset-gcc:version>3.0 <define>FOO
988 : split-conditional <toolset>gcc,<toolset-gcc:version>3.0:<define>FOO ;
990 # Test translate does not choke on negations in conditional
991 assert.result <toolset>gcc,!<rtti>off:<define>HELLO
992 : translate <toolset>gcc,!<rtti>off:<define>HELLO
993 : project-id : project-location : context-module ;
995 feature.finish-test property-test-temp ;