1 # Copyright 2001, 2002, 2003 Dave Abrahams
2 # Copyright 2006 Rene Rivera
3 # Copyright 2002, 2003, 2004, 2005, 2006 Vladimir Prus
4 # Distributed under the Boost Software License, Version 1.0.
5 # (See accompanying file LICENSE_1_0.txt or copy at
6 # http://www.boost.org/LICENSE_1_0.txt)
18 # Refines 'properties' by overriding any non-free and non-conditional properties
19 # for which a different value is specified in 'requirements'. Returns the
20 # resulting list of properties.
22 rule refine ( properties * : requirements * )
27 # Collect all non-free features in requirements
28 for local r in $(requirements)
30 # Do not consider conditional requirements.
31 if ! [ MATCH "(:<)" : $(r:G=) ] && ! free in [ feature.attributes $(r:G) ]
33 if ! $(r) in $(properties)
35 # Kill subfeatures of properties that we're changing
36 local sub = [ modules.peek feature : $(r:G).subfeatures ] ;
39 # non-specific subfeatures are still valid
40 sub = [ MATCH "(.*:.*)" : $(sub) ] ;
41 local name = [ utility.ungrist $(r:G) ] ;
42 unset += <$(name)-$(sub)> ;
49 # Remove properties that are overridden by requirements
50 for local p in $(properties)
52 if [ MATCH "(:<)" : $(p:G=) ] || ! $(p:G) in $(unset)
58 return [ sequence.unique $(result) $(requirements) ] ;
62 # Removes all conditional properties whose conditions are not met. For those
63 # with met conditions, removes the condition. Properties in conditions are
64 # looked up in 'context'.
66 rule evaluate-conditionals-in-context ( properties * : context * )
71 for local p in $(properties)
73 if [ MATCH "(:<)" : $(p) ] && ! free in [ feature.attributes $(p:G) ]
75 conditionals += $(p) ;
77 else if $(p:G) = <conditional>
87 local result = $(base) ;
88 for local p in $(conditionals)
90 # Separate condition and property.
91 local s = [ MATCH "^(.*):(<.*)" : $(p) ] ;
92 # Split condition into individual properties.
93 local condition = [ regex.split $(s[1]) "," ] ;
95 if ! [ MATCH ^(!).* : $(condition:G=) ]
97 # Only positive checks
98 if $(condition) in $(context)
105 # Have negative checks
109 local c = $(condition[1]) ;
110 local m = [ MATCH ^!(.*) : $(c) ] ;
113 local p = $(m:G=$(c:G)) ;
114 if $(p) in $(context)
122 if ! $(c) in $(context)
128 condition = $(condition[2-]) ;
136 # Import here to avoid cyclic dependency
138 for local i in [ MATCH "^@(.*)" : $(indirect:G=) ]
140 # If the rule was set in a project module, translate paths
141 # relative to that project's location.
142 local m = [ indirect.get-module $(i) ] ;
143 local p = [ project.target $(m) : allow-missing ] ;
144 local new = [ indirect.call $(i) $(context) ] ;
145 if $(p) && [ $(p).location ]
147 local location = [ $(p).location ] ;
148 local project-id = [ project.attribute $(m) id ] ;
149 project-id ?= [ path.root $(location) [ path.pwd ] ] ;
151 [ translate $(new) : $(project-id) : $(location) : $(m) ] ;
162 # Returns <relevant> properties indicating how the conditionals in
163 # properties affect feature relevance. If the optional argument cond
164 # is passed, it is treated as extra conditions for all properties.
166 rule evaluate-conditional-relevance ( properties * : cond * )
168 cond = [ sequence.transform utility.ungrist : $(cond:G) ] ;
170 for local p in $(properties)
172 # Separate condition and property.
173 local s = [ MATCH "^(.*):(<.*)" : $(p) ] ;
174 if ! $(s) || free in [ feature.attributes $(p:G) ]
176 local value = [ utility.ungrist $(p:G) ] ;
177 result += <relevant>$(value):<relevant>$(cond) ;
181 local condition = [ regex.split $(s[1]) "," ] ;
182 condition = [ MATCH "^!?(.*)" : $(condition) ] ;
183 condition = [ sequence.transform utility.ungrist : $(condition:G) ] $(cond) ;
184 local value = [ utility.ungrist $(s[2]:G) ] ;
185 result += <relevant>$(value):<relevant>$(condition) ;
188 return [ sequence.unique $(result) ] ;
192 rule expand-subfeatures-in-conditions ( properties * )
195 for local p in $(properties)
197 local s = [ MATCH "^(.*):(<.*)" : $(p) ] ;
204 local condition = $(s[1]) ;
205 local value = $(s[2]) ;
206 # Condition might include several elements.
207 condition = [ regex.split $(condition) "," ] ;
209 for local c in $(condition)
211 # It is common for a condition to include a toolset or
212 # subfeatures that have not been defined. In that case we want
213 # the condition to simply 'never be satisfied' and validation
214 # would only produce a spurious error so we prevent it by
215 # passing 'true' as the second parameter.
216 e += [ feature.expand-subfeatures $(c) : true ] ;
218 if $(e) = $(condition)
221 # This is just an optimization and possibly a premature one at
223 # (todo) (12.07.2008.) (Jurko)
228 result += "$(e:J=,):$(value)" ;
236 # Helper for as-path, below. Orders properties with the implicit ones first, and
237 # within the two sections in alphabetical order of feature name.
239 local rule path-order ( x y )
241 if $(y:G) && ! $(x:G)
245 else if $(x:G) && ! $(y:G)
253 x = [ feature.expand-subfeatures $(x) ] ;
254 y = [ feature.expand-subfeatures $(y) ] ;
265 local rule abbreviate-dashed ( string )
268 for local part in [ regex.split $(string) - ]
270 r += [ string.abbreviate $(part) ] ;
276 local rule identity ( string )
282 if --abbreviate-paths in [ modules.peek : ARGV ]
284 .abbrev = abbreviate-dashed ;
292 # Returns a path representing the given expanded property set.
294 rule as-path ( properties * )
296 local entry = .result.$(properties:J=-) ;
301 properties = [ feature.minimize $(properties) ] ;
303 # Sort according to path-order.
304 properties = [ sequence.insertion-sort $(properties) : path-order ] ;
307 for local p in $(properties)
309 if ! hidden in [ feature.attributes $(p:G) ]
313 local f = [ utility.ungrist $(p:G) ] ;
316 components += [ $(.abbrev) $(p) ] ;
320 $(entry) = $(components:J=/) ;
327 # Exit with error if property is not valid.
329 local rule validate1 ( property )
334 local feature = $(property:G) ;
335 local value = $(property:G=) ;
337 if ! [ feature.valid $(feature) ]
339 # Ungrist for better error messages.
340 feature = [ utility.ungrist $(property:G) ] ;
341 msg = "unknown feature '$(feature)'" ;
343 else if $(value) && ! free in [ feature.attributes $(feature) ]
345 feature.validate-value-string $(feature) $(value) ;
347 else if ! ( $(value) || ( optional in [ feature.attributes $(feature) ] ) )
349 # Ungrist for better error messages.
350 feature = [ utility.ungrist $(property:G) ] ;
351 msg = "No value specified for feature '$(feature)'" ;
356 local feature = [ feature.implied-feature $(property) ] ;
357 feature.validate-value-string $(feature) $(property) ;
362 errors.error "Invalid property "'$(property:J=" ")'": "$(msg:J=" "). ;
367 rule validate ( properties * )
369 for local p in $(properties)
376 rule validate-property-sets ( property-sets * )
378 for local s in $(property-sets)
380 validate [ feature.split $(s) ] ;
385 # Expands any implicit property values in the given property 'specification' so
386 # they explicitly state their feature.
388 rule make ( specification * )
391 for local e in $(specification)
397 else if [ feature.is-implicit-value $(e) ]
399 local feature = [ feature.implied-feature $(e) ] ;
400 result += $(feature)$(e) ;
405 errors.error "'$(e)' is not a valid property specification" ;
412 # Returns a property set containing all the elements in 'properties' that do not
413 # have their attributes listed in 'attributes'.
415 rule remove ( attributes + : properties * )
418 for local e in $(properties)
420 if ! [ set.intersection $(attributes) : [ feature.attributes $(e:G) ] ]
429 # Returns a property set containing all the elements in 'properties' that have
430 # their attributes listed in 'attributes'.
432 rule take ( attributes + : properties * )
435 for local e in $(properties)
437 if [ set.intersection $(attributes) : [ feature.attributes $(e:G) ] ]
446 # Selects properties corresponding to any of the given features.
448 rule select ( features * : properties * )
452 # Add any missing angle brackets.
454 features = $(empty:G=$(features)) ;
456 for local p in $(properties)
458 if $(p:G) in $(features)
467 # Returns a modified version of properties with all values of the given feature
468 # replaced by the given value. If 'value' is empty the feature will be removed.
470 rule change ( properties * : feature value ? )
473 for local p in $(properties)
475 if $(p:G) = $(feature)
477 result += $(value:G=$(feature)) ;
488 # If 'property' is a conditional property, returns the condition and the
489 # property. E.g. <variant>debug,<toolset>gcc:<inlining>full will become
490 # <variant>debug,<toolset>gcc <inlining>full. Otherwise, returns an empty
493 rule split-conditional ( property )
495 return [ MATCH "^(.+):(<.+)" : $(property) ] ;
499 rule translate-path-value ( value : path )
502 for local v in [ regex.split $(value) "&&" ]
504 t += [ path.root [ path.make $(v) ] $(path) ] ;
506 return $(t:TJ="&&") ;
509 rule translate-dependency-value ( value : project-id : project-location )
511 local split-target = [ regex.match ^(.*)//(.*) : $(value) ] ;
514 local rooted = [ path.root [ path.make $(split-target[1]) ]
515 [ path.root $(project-location) [ path.pwd ] ] ] ;
516 return $(rooted)//$(split-target[2]) ;
518 else if [ path.is-rooted $(value) ]
524 return $(project-id)//$(value) ;
528 rule translate-indirect-value ( rulename : context-module )
530 if [ MATCH "^([^%]*)%([^%]+)$" : $(rulename) ]
532 # Rule is already in the 'indirect-rule' format.
533 return @$(rulename) ;
538 if ! [ MATCH "([.])" : $(rulename) ]
540 # This is an unqualified rule name. The user might want to
541 # set flags on this rule name and toolset.flag
542 # auto-qualifies it. Need to do the same here so flag
543 # setting works. We can arrange for toolset.flag to *not*
544 # auto-qualify the argument but then two rules defined in
545 # two Jamfiles would conflict.
546 rulename = $(context-module).$(rulename) ;
548 v = [ indirect.make $(rulename) : $(context-module) ] ;
554 # Equivalent to a calling all of:
557 # translate-dependency
558 # expand-subfeatures-in-conditions
561 rule translate ( properties * : project-id : project-location : context-module )
564 for local p in $(properties)
566 local split = [ split-conditional $(p) ] ;
567 local condition property ;
571 condition = $(split[1]) ;
572 property = $(split[2]) ;
575 for local c in [ regex.split $(condition) "," ]
577 e += [ feature.expand-subfeatures $(c) : true ] ;
580 condition = "$(e:J=,):" ;
587 local feature = $(property:G) ;
590 if [ feature.is-implicit-value $(property) ]
592 feature = [ feature.implied-feature $(property) ] ;
593 result += $(condition:E=)$(feature)$(property) ;
598 errors.error "'$(property)' is not a valid property specification" ;
601 local attributes = [ feature.attributes $(feature) ] ;
603 # Only free features should be translated
604 if free in $(attributes)
606 if path in $(attributes)
608 value = [ translate-path-value $(property:G=) : $(project-location) ] ;
609 result += $(condition:E=)$(feature)$(value) ;
611 else if dependency in $(attributes)
613 value = [ translate-dependency-value $(property:G=) : $(project-id) : $(project-location) ] ;
614 result += $(condition:E=)$(feature)$(value) ;
618 local m = [ MATCH ^@(.+) : $(property:G=) ] ;
621 value = [ translate-indirect-value $(m) : $(context-module) ] ;
622 result += $(condition:E=)$(feature)$(value) ;
626 result += $(condition:E=)$(property) ;
632 result += $(condition:E=)$(property) ;
639 # Interpret all path properties in 'properties' as relative to 'path'. The
640 # property values are assumed to be in system-specific form, and will be
641 # translated into normalized form.
643 rule translate-paths ( properties * : path )
646 for local p in $(properties)
648 local split = [ split-conditional $(p) ] ;
649 local condition = "" ;
652 condition = "$(split[1]):" ;
656 if path in [ feature.attributes $(p:G) ]
658 local values = [ regex.split $(p:TG=) "&&" ] ;
660 for local v in $(values)
662 t += [ path.root [ path.make $(v) ] $(path) ] ;
665 result += $(condition)$(t:TG=$(p:G)) ;
669 result += $(condition)$(p) ;
676 # Assumes that all feature values that start with '@' are names of rules, used
677 # in 'context-module'. Such rules can be either local to the module or global.
678 # Converts such values into 'indirect-rule' format (see indirect.jam), so they
679 # can be called from other modules. Does nothing for such values that are
680 # already in the 'indirect-rule' format.
682 rule translate-indirect ( specification * : context-module )
685 for local p in $(specification)
687 local m = [ MATCH ^@(.+) : $(p:G=) ] ;
691 if [ MATCH "^([^%]*)%([^%]+)$" : $(m) ]
693 # Rule is already in the 'indirect-rule' format.
698 if ! [ MATCH "([.])" : $(m) ]
700 # This is an unqualified rule name. The user might want to
701 # set flags on this rule name and toolset.flag
702 # auto-qualifies it. Need to do the same here so flag
703 # setting works. We can arrange for toolset.flag to *not*
704 # auto-qualify the argument but then two rules defined in
705 # two Jamfiles would conflict.
706 m = $(context-module).$(m) ;
708 v = [ indirect.make $(m) : $(context-module) ] ;
712 result += $(v:G=$(p:G)) ;
723 # Binds all dependency properties in a list relative to the given project.
724 # Targets with absolute paths will be left unchanged and targets which have a
725 # project specified will have the path to the project interpreted relative to
726 # the specified location.
728 rule translate-dependencies ( specification * : project-id : location )
731 for local p in $(specification)
733 local split = [ split-conditional $(p) ] ;
734 local condition = "" ;
737 condition = "$(split[1]):" ;
740 if dependency in [ feature.attributes $(p:G) ]
742 local split-target = [ regex.match ^(.*)//(.*) : $(p:G=) ] ;
745 local rooted = [ path.root [ path.make $(split-target[1]) ]
746 [ path.root $(location) [ path.pwd ] ] ] ;
747 result += $(condition)$(p:G)$(rooted)//$(split-target[2]) ;
749 else if [ path.is-rooted $(p:G=) ]
751 result += $(condition)$(p) ;
755 result += $(condition)$(p:G)$(project-id)//$(p:G=) ;
760 result += $(condition)$(p) ;
767 # Class maintaining a property set -> string mapping.
779 # Associate 'value' with 'properties'.
781 rule insert ( properties * : value )
783 self.all-flags += self.$(self.next-flag) ;
784 self.$(self.next-flag) = $(value) $(properties) ;
786 self.next-flag = [ numbers.increment $(self.next-flag) ] ;
789 # Returns the value associated with 'properties' or any subset of it. If
790 # more than one subset has a value assigned to it, returns the value for the
791 # longest subset, if it is unique.
793 rule find ( property-set )
795 # First find all matches.
798 for local i in $(self.all-flags)
800 local list = $($(i)) ;
801 if [ $(property-set).contains-raw $(list[2-]) ]
803 matches += $(list[1]) ;
804 match-ranks += [ sequence.length $(list) ] ;
807 local best = [ sequence.select-highest-ranked $(matches)
811 import errors : error : errors.error ;
812 errors.error "Ambiguous key $(properties:J= :E=)" ;
817 # Returns the value associated with 'properties'. If 'value' parameter is
818 # given, replaces the found value.
820 rule find-replace ( properties * : value ? )
822 # First find all matches.
825 for local i in $(self.all-flags)
827 if $($(i)[2-]) in $(properties)
830 match-ranks += [ sequence.length $($(i)) ] ;
833 local best = [ sequence.select-highest-ranked $(matches)
837 import errors : error : errors.error ;
838 errors.error "Ambiguous key $(properties:J= :E=)" ;
840 local original = $($(best)[1]) ;
843 $(best) = $(value) $($(best)[2-]) ;
853 import "class" : new ;
854 import errors : try catch ;
857 # Local rules must be explicitly re-imported.
858 import property : path-order abbreviate-dashed ;
860 feature.prepare-test property-test-temp ;
862 feature.feature toolset : gcc : implicit symmetric ;
863 feature.subfeature toolset gcc : version : 2.95.2 2.95.3 2.95.4 3.0 3.0.1
865 feature.feature define : : free ;
866 feature.feature runtime-link : dynamic static : symmetric link-incompatible ;
867 feature.feature optimization : on off ;
868 feature.feature variant : debug release : implicit composite symmetric ;
869 feature.feature rtti : on off : link-incompatible ;
871 feature.compose <variant>debug : <define>_DEBUG <optimization>off ;
872 feature.compose <variant>release : <define>NDEBUG <optimization>on ;
874 validate <toolset>gcc <toolset>gcc-3.0.1 : $(test-space) ;
876 assert.true path-order $(test-space) debug <define>foo ;
877 assert.false path-order $(test-space) <define>foo debug ;
878 assert.true path-order $(test-space) gcc debug ;
879 assert.false path-order $(test-space) debug gcc ;
880 assert.true path-order $(test-space) <optimization>on <rtti>on ;
881 assert.false path-order $(test-space) <rtti>on <optimization>on ;
883 assert.result-set-equal <toolset>gcc <rtti>off <define>FOO
884 : refine <toolset>gcc <rtti>off
888 assert.result-set-equal <toolset>gcc <optimization>on
889 : refine <toolset>gcc <optimization>off
893 assert.result-set-equal <toolset>gcc <rtti>off
894 : refine <toolset>gcc : <rtti>off : $(test-space) ;
896 assert.result-set-equal <toolset>gcc <rtti>off <rtti>off:<define>FOO
897 : refine <toolset>gcc : <rtti>off <rtti>off:<define>FOO
900 assert.result-set-equal <toolset>gcc:<define>foo <toolset>gcc:<define>bar
901 : refine <toolset>gcc:<define>foo : <toolset>gcc:<define>bar
904 assert.result <define>MY_RELEASE
905 : evaluate-conditionals-in-context
906 <variant>release,<rtti>off:<define>MY_RELEASE
907 : <toolset>gcc <variant>release <rtti>off ;
910 : as-path <optimization>off <variant>debug
913 assert.result gcc/debug/rtti-off
914 : as-path <toolset>gcc <optimization>off <rtti>off <variant>debug
917 assert.result optmz-off : abbreviate-dashed optimization-off ;
918 assert.result rntm-lnk-sttc : abbreviate-dashed runtime-link-static ;
921 validate <feature>value : $(test-space) ;
922 catch "Invalid property '<feature>value': unknown feature 'feature'." ;
925 validate <rtti>default : $(test-space) ;
926 catch \"default\" is not a known value of feature <rtti> ;
928 validate <define>WHATEVER : $(test-space) ;
931 validate <rtti> : $(test-space) ;
932 catch "Invalid property '<rtti>': No value specified for feature 'rtti'." ;
935 validate value : $(test-space) ;
936 catch \"value\" is not an implicit feature value ;
938 assert.result-set-equal <rtti>on
939 : remove free implicit : <toolset>gcc <define>foo <rtti>on : $(test-space) ;
941 assert.result-set-equal <include>a
942 : select include : <include>a <toolset>gcc ;
944 assert.result-set-equal <include>a
945 : select include bar : <include>a <toolset>gcc ;
947 assert.result-set-equal <include>a <toolset>gcc
948 : select include <bar> <toolset> : <include>a <toolset>gcc ;
950 assert.result-set-equal <toolset>kylix <include>a
951 : change <toolset>gcc <include>a : <toolset> kylix ;
953 pm = [ new property-map ] ;
954 $(pm).insert <toolset>gcc : o ;
955 $(pm).insert <toolset>gcc <os>NT : obj ;
956 $(pm).insert <toolset>gcc <os>CYGWIN : obj ;
958 assert.equal o : [ $(pm).find-replace <toolset>gcc ] ;
960 assert.equal obj : [ $(pm).find-replace <toolset>gcc <os>NT ] ;
963 $(pm).find-replace <toolset>gcc <os>NT <os>CYGWIN ;
964 catch "Ambiguous key <toolset>gcc <os>NT <os>CYGWIN" ;
966 # Test ordinary properties.
967 assert.result : split-conditional <toolset>gcc ;
969 # Test properties with ":".
970 assert.result : split-conditional <define>"FOO=A::B" ;
972 # Test conditional feature.
973 assert.result-set-equal <toolset>gcc,<toolset-gcc:version>3.0 <define>FOO
974 : split-conditional <toolset>gcc,<toolset-gcc:version>3.0:<define>FOO ;
976 feature.finish-test property-test-temp ;