ConfigRules revision 8c54af56
1# ConfigRules
2#
3# Contains rules providing the config variable feature. It allows to set the
4# values for certain variables for subdirectories in a central place. That is
5# one can, for instance, specify in a file like UserBuildConfig for which
6# directories to enable debugging, warnings, set special defines, compiler
7# flags and the like without needing to edit the Jamfiles for the respective
8# dirs.
9
10rule ConfigObject
11{
12	# ConfigObject <dir> [ : <varName> ] ;
13	#
14	# Private rule. Returns the dummy object on which the config variables are
15	# set for a given subdir.
16	#
17	# <dir>: Parameters as passed to the SubDir rule, i.e. the name of the
18	#        TOP variable and the subdir tokens.
19	#
20	local config = $(2:E=__config__) ;
21	local grist = [ FGrist root $(1) ] ;
22	return $(config:G=$(grist)) ;
23}
24
25rule SetConfigVar
26{
27	# SetConfigVar <var> : <dir> : <value> [ : <scope> ] ;
28	#
29	# Sets a config variable for a specified directory to the given value.
30	#
31	# <var>: The name of the variable to be set.
32	# <dir>: Parameters as passed to the SubDir rule, i.e. the name of the
33	#        TOP variable and the subdir tokens.
34	# <value>: The value to which the variable shall be set.
35	# <scope>: Either "global" or "local". The former implies that the variable
36	#          value shall also be used for subdirectories (recursively), if
37	#          for them the variable has not been set. The latter has the same
38	#          effect regarding subdirs as if the variable for the directory
39	#          is not set. Defaults to "global".
40	#
41	local var = $(1[1]) ;
42	local config = [ ConfigObject $(2) ] ;
43	local scope = $(4:E=global) ;
44
45	$(var) on $(config) = $(3) ;
46	__set_$(var) on $(config) = $(scope) ;
47
48	if $(scope) = global {
49		$(var) on [ ConfigObject $(2) : __inherited_config__ ] = $(3) ;
50	}
51
52	if ! [ on $(config) return $(__configured) ] {
53		__configured on $(config) = true ;
54		__dir_tokens on $(config) = $(2) ;
55
56		HAIKU_EXISTING_SUBDIR_CONFIGS += $(config) ;
57	}
58}
59
60rule AppendToConfigVar
61{
62	# AppendToConfigVar <var> : <dir> : <value> [ : <scope> ] ;
63	#
64	# Appends a value to a config variable for a specified directory. Shortcut
65	# for
66	#   SetConfigVar <var> : <dir> : [ ConfigVar <var> : <dir> ] <value
67	#                : <scope> ;
68	#
69	# <var>: The name of the variable to be set.
70	# <dir>: Parameters as passed to the SubDir rule, i.e. the name of the
71	#        TOP variable and the subdir tokens.
72	# <value>: The value which to append to the variables current value.
73	# <scope>: Either "global" or "local". The former implies that the variable
74	#          value shall also be used for subdirectories (recursively), if
75	#          for them the variable has not been set. The latter has the same
76	#          effect regarding subdirs as if the variable for the directory
77	#          is not set. Defaults to "global".
78	#
79	SetConfigVar $(1) : $(2) : [ ConfigVar $(1) : $(2) ] $(3) : $(4) ;
80}
81
82rule ConfigVar
83{
84	# ConfigVar <var> : <dir> [ : <scope> ] ;
85	#
86	# Returns the value of a configuration variable for a given subdir.
87	# If the variable is not set for the subdir, the rule is invoked
88	# recursively for the parent directory with the scope "global". When
89	# the root is reached without yielding a value, the value of the global
90	# variable <var> is returned.
91	#
92	# <var>: The name of the variable whose value shall be returned.
93	# <dir>: Parameters as passed to the SubDir rule, i.e. the name of the
94	#        TOP variable and the subdir tokens.
95	# <scope>: If not given any scope passed to SetConfigVar for the given
96	#          directory will be accepted, otherwise it must match the scope
97	#          passed to SetConfigVar.
98	#
99	local var = $(1[1]) ;
100	local dir = $(2) ;
101	local config = [ ConfigObject $(dir) ] ;
102	local scope = $(3) ;
103	local varScope = [ on $(config) return $(__set_$(var)) ] ;
104	if ( ! $(scope) && $(varScope) )
105		|| ( $(scope) && $(scope) = $(varScope) )
106		|| ! $(dir) {
107		on $(config) return $($(var)) ;
108	} else {
109		dir = [ FReverse $(dir) ] ;
110		return [ ConfigVar $(var) : [ FReverse $(dir[2-]) ] : global ] ;
111	}
112}
113
114rule PrepareSubDirConfigVariables
115{
116	local dirTokens = $(1) ;
117	local config = [ ConfigObject $(dirTokens) ] ;
118
119	if ! [ on $(config) return $(__prepared) ] {
120		# prepare config for parent dir
121		local parentDir = [ FReverse $(dirTokens) ] ;
122		parentDir = [ FReverse $(parentDir[2-]) ] ;
123		PrepareSubDirConfigVariables $(parentDir) ;
124
125		# set values for all config variables for the config and the inherited
126		# config for this directory
127		local inheritedConfig = [ ConfigObject $(dirTokens)
128			: __inherited_config__ ] ;
129
130		on [ ConfigObject $(parentDir) : __inherited_config__ ] {
131			for var in $(AUTO_SET_UP_CONFIG_VARIABLES) {
132				$(var) on $(config) ?= $($(var)) ;
133				$(var) on $(inheritedConfig) ?= $($(var)) ;
134			}
135		}
136
137		HAIKU_INHERITED_SUBDIR_CONFIG on $(config) = $(inheritedConfig) ;
138
139		__prepared on $(config) = true ;
140	}
141}
142
143rule PrepareConfigVariables
144{
145	# initialize variables on the root config and the root inherited config
146	# objects to the global values
147	local rootConfig = [ ConfigObject ] ; # the root config object
148	local inheritedRootConfig = [ ConfigObject : __inherited_config__ ] ;
149
150	for var in $(AUTO_SET_UP_CONFIG_VARIABLES) {
151		$(var) on $(rootConfig) = $($(var)) ;
152		$(var) on $(inheritedRootConfig) = $($(var)) ;
153	}
154	__prepared on $(rootConfig) = true ;
155
156	HAIKU_INHERITED_SUBDIR_CONFIG = $(rootConfig) ;
157
158	local config ;
159	for config in $(HAIKU_EXISTING_SUBDIR_CONFIGS) {
160		PrepareSubDirConfigVariables [ on $(config) return $(__dir_tokens) ] ;
161	}
162}
163
164# Some config variables that should be set up automatically for subdirs.
165AUTO_SET_UP_CONFIG_VARIABLES +=
166	CCFLAGS C++FLAGS DEBUG DEFINES HDRS JAMFILE LINKFLAGS OPTIM OPTIMIZE
167	SYSHDRS WARNINGS
168;
169