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) ]
37 # Remove properties that are overridden by requirements
38 for local p in $(properties)
40 if [ MATCH (:) : $(p:G=) ] || ! $(p:G) in $(unset)
46 return [ sequence.unique $(result) $(requirements) ] ;
50 # Removes all conditional properties whose conditions are not met. For those
51 # with met conditions, removes the condition. Properties in conditions are
52 # looked up in 'context'.
54 rule evaluate-conditionals-in-context ( properties * : context * )
58 for local p in $(properties)
60 if [ MATCH (:<) : $(p) ]
62 conditionals += $(p) ;
70 local result = $(base) ;
71 for local p in $(conditionals)
73 # Separate condition and property.
74 local s = [ MATCH ^(.*):(<.*) : $(p) ] ;
75 # Split condition into individual properties.
76 local condition = [ regex.split $(s[1]) "," ] ;
78 if ! [ MATCH ^(!).* : $(condition:G=) ]
80 # Only positive checks
81 if $(condition) in $(context)
88 # Have negative checks
92 local c = $(condition[1]) ;
93 local m = [ MATCH ^!(.*) : $(c) ] ;
96 local p = $(m:G=$(c:G)) ;
105 if ! $(c) in $(context)
111 condition = $(condition[2-]) ;
123 rule expand-subfeatures-in-conditions ( properties * )
126 for local p in $(properties)
128 local s = [ MATCH ^(.*):(<.*) : $(p) ] ;
135 local condition = $(s[1]) ;
136 local value = $(s[2]) ;
137 # Condition might include several elements.
138 condition = [ regex.split $(condition) "," ] ;
140 for local c in $(condition)
142 # It is common for a condition to include a toolset or
143 # subfeatures that have not been defined. In that case we want
144 # the condition to simply 'never be satisfied' and validation
145 # would only produce a spurious error so we prevent it by
146 # passing 'true' as the second parameter.
147 e += [ feature.expand-subfeatures $(c) : true ] ;
149 if $(e) = $(condition)
152 # This is just an optimization and possibly a premature one at
154 # (todo) (12.07.2008.) (Jurko)
159 result += $(e:J=,):$(value) ;
167 # Helper for as-path, below. Orders properties with the implicit ones first, and
168 # within the two sections in alphabetical order of feature name.
170 local rule path-order ( x y )
172 if $(y:G) && ! $(x:G)
176 else if $(x:G) && ! $(y:G)
184 x = [ feature.expand-subfeatures $(x) ] ;
185 y = [ feature.expand-subfeatures $(y) ] ;
196 local rule abbreviate-dashed ( string )
199 for local part in [ regex.split $(string) - ]
201 r += [ string.abbreviate $(part) ] ;
207 local rule identity ( string )
213 if --abbreviate-paths in [ modules.peek : ARGV ]
215 .abbrev = abbreviate-dashed ;
223 # Returns a path representing the given expanded property set.
225 rule as-path ( properties * )
227 local entry = .result.$(properties:J=-) ;
232 properties = [ feature.minimize $(properties) ] ;
234 # Sort according to path-order.
235 properties = [ sequence.insertion-sort $(properties) : path-order ] ;
238 for local p in $(properties)
240 if ! hidden in [ feature.attributes $(p:G) ]
244 local f = [ utility.ungrist $(p:G) ] ;
247 components += [ $(.abbrev) $(p) ] ;
251 $(entry) = $(components:J=/) ;
258 # Exit with error if property is not valid.
260 local rule validate1 ( property )
265 local feature = $(property:G) ;
266 local value = $(property:G=) ;
268 if ! [ feature.valid $(feature) ]
270 # Ungrist for better error messages.
271 feature = [ utility.ungrist $(property:G) ] ;
272 msg = "unknown feature '$(feature)'" ;
274 else if $(value) && ! free in [ feature.attributes $(feature) ]
276 feature.validate-value-string $(feature) $(value) ;
278 else if ! ( $(value) || ( optional in [ feature.attributes $(feature) ] ) )
280 # Ungrist for better error messages.
281 feature = [ utility.ungrist $(property:G) ] ;
282 msg = "No value specified for feature '$(feature)'" ;
287 local feature = [ feature.implied-feature $(property) ] ;
288 feature.validate-value-string $(feature) $(property) ;
293 errors.error "Invalid property "'$(property:J=" ")'": "$(msg:J=" "). ;
298 rule validate ( properties * )
300 for local p in $(properties)
307 rule validate-property-sets ( property-sets * )
309 for local s in $(property-sets)
311 validate [ feature.split $(s) ] ;
316 # Expands any implicit property values in the given property 'specification' so
317 # they explicitly state their feature.
319 rule make ( specification * )
322 for local e in $(specification)
328 else if [ feature.is-implicit-value $(e) ]
330 local feature = [ feature.implied-feature $(e) ] ;
331 result += $(feature)$(e) ;
336 errors.error "'$(e)' is not a valid property specification" ;
343 # Returns a property set containing all the elements in 'properties' that do not
344 # have their attributes listed in 'attributes'.
346 rule remove ( attributes + : properties * )
349 for local e in $(properties)
351 if ! [ set.intersection $(attributes) : [ feature.attributes $(e:G) ] ]
360 # Returns a property set containing all the elements in 'properties' that have
361 # their attributes listed in 'attributes'.
363 rule take ( attributes + : properties * )
366 for local e in $(properties)
368 if [ set.intersection $(attributes) : [ feature.attributes $(e:G) ] ]
377 # Selects properties corresponding to any of the given features.
379 rule select ( features * : properties * )
383 # Add any missing angle brackets.
385 features = $(empty:G=$(features)) ;
387 for local p in $(properties)
389 if $(p:G) in $(features)
398 # Returns a modified version of properties with all values of the given feature
399 # replaced by the given value. If 'value' is empty the feature will be removed.
401 rule change ( properties * : feature value ? )
404 for local p in $(properties)
406 if $(p:G) = $(feature)
408 result += $(value:G=$(feature)) ;
419 # If 'property' is a conditional property, returns the condition and the
420 # property. E.g. <variant>debug,<toolset>gcc:<inlining>full will become
421 # <variant>debug,<toolset>gcc <inlining>full. Otherwise, returns an empty
424 rule split-conditional ( property )
426 return [ MATCH "^(.+):(<.+)" : $(property) ] ;
430 rule translate-path-value ( value : path )
433 for local v in [ regex.split $(value) "&&" ]
435 t += [ path.root [ path.make $(v) ] $(path) ] ;
437 return $(t:TJ="&&") ;
440 rule translate-dependency-value ( value : project-id : project-location )
442 local split-target = [ regex.match ^(.*)//(.*) : $(value) ] ;
445 local rooted = [ path.root [ path.make $(split-target[1]) ]
446 [ path.root $(project-location) [ path.pwd ] ] ] ;
447 return $(rooted)//$(split-target[2]) ;
449 else if [ path.is-rooted $(value) ]
455 return $(project-id)//$(value) ;
459 rule translate-indirect-value ( rulename : context-module )
461 if [ MATCH "^([^%]*)%([^%]+)$" : $(rulename) ]
463 # Rule is already in the 'indirect-rule' format.
464 return @$(rulename) ;
469 if ! [ MATCH "([.])" : $(rulename) ]
471 # This is an unqualified rule name. The user might want to
472 # set flags on this rule name and toolset.flag
473 # auto-qualifies it. Need to do the same here so flag
474 # setting works. We can arrange for toolset.flag to *not*
475 # auto-qualify the argument but then two rules defined in
476 # two Jamfiles would conflict.
477 rulename = $(context-module).$(rulename) ;
479 v = [ indirect.make $(rulename) : $(context-module) ] ;
485 # Equivalent to a calling all of:
488 # translate-dependency
489 # expand-subfeatures-in-conditions
492 rule translate ( properties * : project-id : project-location : context-module )
495 for local p in $(properties)
497 local split = [ split-conditional $(p) ] ;
498 local condition property ;
502 condition = $(split[1]) ;
503 property = $(split[2]) ;
506 for local c in [ regex.split $(condition) "," ]
508 e += [ feature.expand-subfeatures $(c) : true ] ;
511 condition = $(e:J=,): ;
518 local feature = $(property:G) ;
521 if [ feature.is-implicit-value $(property) ]
523 feature = [ feature.implied-feature $(property) ] ;
524 result += $(condition:E=)$(feature)$(property) ;
529 errors.error "'$(property)' is not a valid property specification" ;
532 local attributes = [ feature.attributes $(feature) ] ;
534 # Only free features should be translated
535 if free in $(attributes)
537 if path in $(attributes)
539 value = [ translate-path-value $(property:G=) : $(project-location) ] ;
540 result += $(condition:E=)$(feature)$(value) ;
542 else if dependency in $(attributes)
544 value = [ translate-dependency-value $(property:G=) : $(project-id) : $(project-location) ] ;
545 result += $(condition:E=)$(feature)$(value) ;
549 local m = [ MATCH ^@(.+) : $(property:G=) ] ;
552 value = [ translate-indirect-value $(m) : $(context-module) ] ;
553 result += $(condition:E=)$(feature)$(value) ;
557 result += $(condition:E=)$(property) ;
563 result += $(condition:E=)$(property) ;
570 # Interpret all path properties in 'properties' as relative to 'path'. The
571 # property values are assumed to be in system-specific form, and will be
572 # translated into normalized form.
574 rule translate-paths ( properties * : path )
577 for local p in $(properties)
579 local split = [ split-conditional $(p) ] ;
580 local condition = "" ;
583 condition = $(split[1]): ;
587 if path in [ feature.attributes $(p:G) ]
589 local values = [ regex.split $(p:TG=) "&&" ] ;
591 for local v in $(values)
593 t += [ path.root [ path.make $(v) ] $(path) ] ;
596 result += $(condition)$(t:TG=$(p:G)) ;
600 result += $(condition)$(p) ;
607 # Assumes that all feature values that start with '@' are names of rules, used
608 # in 'context-module'. Such rules can be either local to the module or global.
609 # Converts such values into 'indirect-rule' format (see indirect.jam), so they
610 # can be called from other modules. Does nothing for such values that are
611 # already in the 'indirect-rule' format.
613 rule translate-indirect ( specification * : context-module )
616 for local p in $(specification)
618 local m = [ MATCH ^@(.+) : $(p:G=) ] ;
622 if [ MATCH "^([^%]*)%([^%]+)$" : $(m) ]
624 # Rule is already in the 'indirect-rule' format.
629 if ! [ MATCH "([.])" : $(m) ]
631 # This is an unqualified rule name. The user might want to
632 # set flags on this rule name and toolset.flag
633 # auto-qualifies it. Need to do the same here so flag
634 # setting works. We can arrange for toolset.flag to *not*
635 # auto-qualify the argument but then two rules defined in
636 # two Jamfiles would conflict.
637 m = $(context-module).$(m) ;
639 v = [ indirect.make $(m) : $(context-module) ] ;
643 result += $(v:G=$(p:G)) ;
654 # Binds all dependency properties in a list relative to the given project.
655 # Targets with absolute paths will be left unchanged and targets which have a
656 # project specified will have the path to the project interpreted relative to
657 # the specified location.
659 rule translate-dependencies ( specification * : project-id : location )
662 for local p in $(specification)
664 local split = [ split-conditional $(p) ] ;
665 local condition = "" ;
668 condition = $(split[1]): ;
671 if dependency in [ feature.attributes $(p:G) ]
673 local split-target = [ regex.match ^(.*)//(.*) : $(p:G=) ] ;
676 local rooted = [ path.root [ path.make $(split-target[1]) ]
677 [ path.root $(location) [ path.pwd ] ] ] ;
678 result += $(condition)$(p:G)$(rooted)//$(split-target[2]) ;
680 else if [ path.is-rooted $(p:G=) ]
682 result += $(condition)$(p) ;
686 result += $(condition)$(p:G)$(project-id)//$(p:G=) ;
691 result += $(condition)$(p) ;
698 # Class maintaining a property set -> string mapping.
710 # Associate 'value' with 'properties'.
712 rule insert ( properties * : value )
714 self.all-flags += self.$(self.next-flag) ;
715 self.$(self.next-flag) = $(value) $(properties) ;
717 self.next-flag = [ numbers.increment $(self.next-flag) ] ;
720 # Returns the value associated with 'properties' or any subset of it. If
721 # more than one subset has a value assigned to it, returns the value for the
722 # longest subset, if it is unique.
724 rule find ( property-set )
726 # First find all matches.
729 for local i in $(self.all-flags)
731 local list = $($(i)) ;
732 if [ $(property-set).contains-raw $(list[2-]) ]
734 matches += $(list[1]) ;
735 match-ranks += [ sequence.length $(list) ] ;
738 local best = [ sequence.select-highest-ranked $(matches)
742 import errors : error : errors.error ;
743 errors.error "Ambiguous key $(properties:J= :E=)" ;
748 # Returns the value associated with 'properties'. If 'value' parameter is
749 # given, replaces the found value.
751 rule find-replace ( properties * : value ? )
753 # First find all matches.
756 for local i in $(self.all-flags)
758 if $($(i)[2-]) in $(properties)
761 match-ranks += [ sequence.length $($(i)) ] ;
764 local best = [ sequence.select-highest-ranked $(matches)
768 import errors : error : errors.error ;
769 errors.error "Ambiguous key $(properties:J= :E=)" ;
771 local original = $($(best)[1]) ;
774 $(best) = $(value) $($(best)[2-]) ;
784 import "class" : new ;
785 import errors : try catch ;
788 # Local rules must be explicitly re-imported.
789 import property : path-order abbreviate-dashed ;
791 feature.prepare-test property-test-temp ;
793 feature.feature toolset : gcc : implicit symmetric ;
794 feature.subfeature toolset gcc : version : 2.95.2 2.95.3 2.95.4 3.0 3.0.1
796 feature.feature define : : free ;
797 feature.feature runtime-link : dynamic static : symmetric link-incompatible ;
798 feature.feature optimization : on off ;
799 feature.feature variant : debug release : implicit composite symmetric ;
800 feature.feature rtti : on off : link-incompatible ;
802 feature.compose <variant>debug : <define>_DEBUG <optimization>off ;
803 feature.compose <variant>release : <define>NDEBUG <optimization>on ;
805 validate <toolset>gcc <toolset>gcc-3.0.1 : $(test-space) ;
807 assert.true path-order $(test-space) debug <define>foo ;
808 assert.false path-order $(test-space) <define>foo debug ;
809 assert.true path-order $(test-space) gcc debug ;
810 assert.false path-order $(test-space) debug gcc ;
811 assert.true path-order $(test-space) <optimization>on <rtti>on ;
812 assert.false path-order $(test-space) <rtti>on <optimization>on ;
814 assert.result-set-equal <toolset>gcc <rtti>off <define>FOO
815 : refine <toolset>gcc <rtti>off
819 assert.result-set-equal <toolset>gcc <optimization>on
820 : refine <toolset>gcc <optimization>off
824 assert.result-set-equal <toolset>gcc <rtti>off
825 : refine <toolset>gcc : <rtti>off : $(test-space) ;
827 assert.result-set-equal <toolset>gcc <rtti>off <rtti>off:<define>FOO
828 : refine <toolset>gcc : <rtti>off <rtti>off:<define>FOO
831 assert.result-set-equal <toolset>gcc:<define>foo <toolset>gcc:<define>bar
832 : refine <toolset>gcc:<define>foo : <toolset>gcc:<define>bar
835 assert.result <define>MY_RELEASE
836 : evaluate-conditionals-in-context
837 <variant>release,<rtti>off:<define>MY_RELEASE
838 : <toolset>gcc <variant>release <rtti>off ;
841 : as-path <optimization>off <variant>debug
844 assert.result gcc/debug/rtti-off
845 : as-path <toolset>gcc <optimization>off <rtti>off <variant>debug
848 assert.result optmz-off : abbreviate-dashed optimization-off ;
849 assert.result rntm-lnk-sttc : abbreviate-dashed runtime-link-static ;
852 validate <feature>value : $(test-space) ;
853 catch "Invalid property '<feature>value': unknown feature 'feature'." ;
856 validate <rtti>default : $(test-space) ;
857 catch \"default\" is not a known value of feature <rtti> ;
859 validate <define>WHATEVER : $(test-space) ;
862 validate <rtti> : $(test-space) ;
863 catch "Invalid property '<rtti>': No value specified for feature 'rtti'." ;
866 validate value : $(test-space) ;
867 catch \"value\" is not an implicit feature value ;
869 assert.result-set-equal <rtti>on
870 : remove free implicit : <toolset>gcc <define>foo <rtti>on : $(test-space) ;
872 assert.result-set-equal <include>a
873 : select include : <include>a <toolset>gcc ;
875 assert.result-set-equal <include>a
876 : select include bar : <include>a <toolset>gcc ;
878 assert.result-set-equal <include>a <toolset>gcc
879 : select include <bar> <toolset> : <include>a <toolset>gcc ;
881 assert.result-set-equal <toolset>kylix <include>a
882 : change <toolset>gcc <include>a : <toolset> kylix ;
884 pm = [ new property-map ] ;
885 $(pm).insert <toolset>gcc : o ;
886 $(pm).insert <toolset>gcc <os>NT : obj ;
887 $(pm).insert <toolset>gcc <os>CYGWIN : obj ;
889 assert.equal o : [ $(pm).find-replace <toolset>gcc ] ;
891 assert.equal obj : [ $(pm).find-replace <toolset>gcc <os>NT ] ;
894 $(pm).find-replace <toolset>gcc <os>NT <os>CYGWIN ;
895 catch "Ambiguous key <toolset>gcc <os>NT <os>CYGWIN" ;
897 # Test ordinary properties.
898 assert.result : split-conditional <toolset>gcc ;
900 # Test properties with ":".
901 assert.result : split-conditional <define>FOO=A::B ;
903 # Test conditional feature.
904 assert.result-set-equal <toolset>gcc,<toolset-gcc:version>3.0 <define>FOO
905 : split-conditional <toolset>gcc,<toolset-gcc:version>3.0:<define>FOO ;
907 feature.finish-test property-test-temp ;