1 | /* Definitions for using variables in GNU Make.
|
---|
2 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
|
---|
3 | 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
|
---|
4 | Foundation, Inc.
|
---|
5 | This file is part of GNU Make.
|
---|
6 |
|
---|
7 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
8 | terms of the GNU General Public License as published by the Free Software
|
---|
9 | Foundation; either version 2, or (at your option) any later version.
|
---|
10 |
|
---|
11 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
13 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License along with
|
---|
16 | GNU Make; see the file COPYING. If not, write to the Free Software
|
---|
17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
|
---|
18 |
|
---|
19 | #include "hash.h"
|
---|
20 |
|
---|
21 | /* Codes in a variable definition saying where the definition came from.
|
---|
22 | Increasing numeric values signify less-overridable definitions. */
|
---|
23 | enum variable_origin
|
---|
24 | {
|
---|
25 | o_default, /* Variable from the default set. */
|
---|
26 | o_env, /* Variable from environment. */
|
---|
27 | o_file, /* Variable given in a makefile. */
|
---|
28 | o_env_override, /* Variable from environment, if -e. */
|
---|
29 | o_command, /* Variable given by user. */
|
---|
30 | o_override, /* Variable from an `override' directive. */
|
---|
31 | #ifdef CONFIG_WITH_LOCAL_VARIABLES
|
---|
32 | o_local, /* Variable from an 'local' directive. */
|
---|
33 | #endif
|
---|
34 | o_automatic, /* Automatic variable -- cannot be set. */
|
---|
35 | o_invalid /* Core dump time. */
|
---|
36 | };
|
---|
37 |
|
---|
38 | enum variable_flavor
|
---|
39 | {
|
---|
40 | f_bogus, /* Bogus (error) */
|
---|
41 | f_simple, /* Simple definition (:=) */
|
---|
42 | f_recursive, /* Recursive definition (=) */
|
---|
43 | f_append, /* Appending definition (+=) */
|
---|
44 | #ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
|
---|
45 | f_prepend, /* Prepending definition (>=) */
|
---|
46 | #endif
|
---|
47 | f_conditional /* Conditional definition (?=) */
|
---|
48 | };
|
---|
49 |
|
---|
50 | /* Structure that represents one variable definition.
|
---|
51 | Each bucket of the hash table is a chain of these,
|
---|
52 | chained through `next'. */
|
---|
53 |
|
---|
54 | #define EXP_COUNT_BITS 15 /* This gets all the bitfields into 32 bits */
|
---|
55 | #define EXP_COUNT_MAX ((1<<EXP_COUNT_BITS)-1)
|
---|
56 |
|
---|
57 | struct variable
|
---|
58 | {
|
---|
59 | #ifndef CONFIG_WITH_STRCACHE2
|
---|
60 | char *name; /* Variable name. */
|
---|
61 | #else
|
---|
62 | const char *name; /* Variable name. */
|
---|
63 | #endif
|
---|
64 | int length; /* strlen (name) */
|
---|
65 | #ifdef VARIABLE_HASH /* bird */
|
---|
66 | int hash1; /* the primary hash */
|
---|
67 | int hash2; /* the secondary hash */
|
---|
68 | #endif
|
---|
69 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
70 | int value_length; /* The length of the value, usually unused. */
|
---|
71 | int value_alloc_len; /* The amount of memory we've actually allocated. */
|
---|
72 | /* FIXME: make lengths unsigned! */
|
---|
73 | #endif
|
---|
74 | char *value; /* Variable value. */
|
---|
75 | struct floc fileinfo; /* Where the variable was defined. */
|
---|
76 | unsigned int recursive:1; /* Gets recursively re-evaluated. */
|
---|
77 | unsigned int append:1; /* Nonzero if an appending target-specific
|
---|
78 | variable. */
|
---|
79 | unsigned int conditional:1; /* Nonzero if set with a ?=. */
|
---|
80 | unsigned int per_target:1; /* Nonzero if a target-specific variable. */
|
---|
81 | unsigned int special:1; /* Nonzero if this is a special variable. */
|
---|
82 | unsigned int exportable:1; /* Nonzero if the variable _could_ be
|
---|
83 | exported. */
|
---|
84 | unsigned int expanding:1; /* Nonzero if currently being expanded. */
|
---|
85 | unsigned int exp_count:EXP_COUNT_BITS;
|
---|
86 | /* If >1, allow this many self-referential
|
---|
87 | expansions. */
|
---|
88 | enum variable_flavor
|
---|
89 | flavor ENUM_BITFIELD (3); /* Variable flavor. */
|
---|
90 | enum variable_origin
|
---|
91 | #ifdef CONFIG_WITH_LOCAL_VARIABLES
|
---|
92 | origin ENUM_BITFIELD (4); /* Variable origin. */
|
---|
93 | #else
|
---|
94 | origin ENUM_BITFIELD (3); /* Variable origin. */
|
---|
95 | #endif
|
---|
96 | enum variable_export
|
---|
97 | {
|
---|
98 | v_export, /* Export this variable. */
|
---|
99 | v_noexport, /* Don't export this variable. */
|
---|
100 | v_ifset, /* Export it if it has a non-default value. */
|
---|
101 | v_default /* Decide in target_environment. */
|
---|
102 | } export ENUM_BITFIELD (2);
|
---|
103 | };
|
---|
104 |
|
---|
105 | /* Structure that represents a variable set. */
|
---|
106 |
|
---|
107 | struct variable_set
|
---|
108 | {
|
---|
109 | struct hash_table table; /* Hash table of variables. */
|
---|
110 | };
|
---|
111 |
|
---|
112 | /* Structure that represents a list of variable sets. */
|
---|
113 |
|
---|
114 | struct variable_set_list
|
---|
115 | {
|
---|
116 | struct variable_set_list *next; /* Link in the chain. */
|
---|
117 | struct variable_set *set; /* Variable set. */
|
---|
118 | };
|
---|
119 |
|
---|
120 | /* Structure used for pattern-specific variables. */
|
---|
121 |
|
---|
122 | struct pattern_var
|
---|
123 | {
|
---|
124 | struct pattern_var *next;
|
---|
125 | const char *suffix;
|
---|
126 | const char *target;
|
---|
127 | unsigned int len;
|
---|
128 | struct variable variable;
|
---|
129 | };
|
---|
130 |
|
---|
131 | extern char *variable_buffer;
|
---|
132 | extern struct variable_set_list *current_variable_set_list;
|
---|
133 | #ifdef KMK
|
---|
134 | extern unsigned int variable_buffer_length;
|
---|
135 | #define VARIABLE_BUFFER_ZONE 5
|
---|
136 | #endif
|
---|
137 |
|
---|
138 | /* expand.c */
|
---|
139 | #ifndef KMK
|
---|
140 | char *variable_buffer_output (char *ptr, const char *string, unsigned int length);
|
---|
141 | #else /* KMK */
|
---|
142 | /* Subroutine of variable_expand and friends:
|
---|
143 | The text to add is LENGTH chars starting at STRING to the variable_buffer.
|
---|
144 | The text is added to the buffer at PTR, and the updated pointer into
|
---|
145 | the buffer is returned as the value. Thus, the value returned by
|
---|
146 | each call to variable_buffer_output should be the first argument to
|
---|
147 | the following call. */
|
---|
148 |
|
---|
149 | __inline static char *
|
---|
150 | variable_buffer_output (char *ptr, const char *string, unsigned int length)
|
---|
151 | {
|
---|
152 | register unsigned int newlen = length + (ptr - variable_buffer);
|
---|
153 |
|
---|
154 | if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
|
---|
155 | {
|
---|
156 | unsigned int offset = ptr - variable_buffer;
|
---|
157 | variable_buffer_length = variable_buffer_length <= 1024
|
---|
158 | ? 2048 : variable_buffer_length * 4;
|
---|
159 | if (variable_buffer_length < newlen + 100)
|
---|
160 | variable_buffer_length = (newlen + 100 + 1023) & ~1023U;
|
---|
161 | variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
|
---|
162 | ptr = variable_buffer + offset;
|
---|
163 | }
|
---|
164 |
|
---|
165 | # ifndef _MSC_VER
|
---|
166 | switch (length)
|
---|
167 | {
|
---|
168 | case 4: ptr[3] = string[3];
|
---|
169 | case 3: ptr[2] = string[2];
|
---|
170 | case 2: ptr[1] = string[1];
|
---|
171 | case 1: ptr[0] = string[0];
|
---|
172 | case 0:
|
---|
173 | break;
|
---|
174 | default:
|
---|
175 | memcpy (ptr, string, length);
|
---|
176 | break;
|
---|
177 | }
|
---|
178 | # else
|
---|
179 | memcpy (ptr, string, length);
|
---|
180 | # endif
|
---|
181 | return ptr + length;
|
---|
182 | }
|
---|
183 |
|
---|
184 | #endif /* KMK */
|
---|
185 | char *variable_expand (const char *line);
|
---|
186 | char *variable_expand_for_file (const char *line, struct file *file);
|
---|
187 | #ifdef CONFIG_WITH_COMMANDS_FUNC
|
---|
188 | char *variable_expand_for_file_2 (char *o, const char *line, unsigned int lenght,
|
---|
189 | struct file *file, unsigned int *value_lenp);
|
---|
190 | #endif
|
---|
191 | char *allocated_variable_expand_for_file (const char *line, struct file *file);
|
---|
192 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
193 | #define allocated_variable_expand(line) \
|
---|
194 | allocated_variable_expand_for_file (line, (struct file *) 0)
|
---|
195 | #else /* CONFIG_WITH_VALUE_LENGTH */
|
---|
196 | # define allocated_variable_expand(line) \
|
---|
197 | allocated_variable_expand_2 (line, -1, NULL)
|
---|
198 | char *allocated_variable_expand_2(const char *line, unsigned int length, unsigned int *value_lenp);
|
---|
199 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
200 | char *expand_argument (const char *str, const char *end);
|
---|
201 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
202 | char *variable_expand_string (char *line, const char *string, long length);
|
---|
203 | #else /* CONFIG_WITH_VALUE_LENGTH */
|
---|
204 | char *variable_expand_string_2 (char *line, const char *string, long length, char **eol);
|
---|
205 | __inline static char *
|
---|
206 | variable_expand_string (char *line, const char *string, long length)
|
---|
207 | {
|
---|
208 | char *ignored;
|
---|
209 | return variable_expand_string_2 (line, string, length, &ignored);
|
---|
210 | }
|
---|
211 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
212 | void install_variable_buffer (char **bufp, unsigned int *lenp);
|
---|
213 | void restore_variable_buffer (char *buf, unsigned int len);
|
---|
214 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
215 | void append_expanded_string_to_variable (struct variable *v, const char *value,
|
---|
216 | unsigned int value_len, int append);
|
---|
217 | #endif
|
---|
218 |
|
---|
219 | /* function.c */
|
---|
220 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
221 | int handle_function (char **op, const char **stringp);
|
---|
222 | #else
|
---|
223 | int handle_function (char **op, const char **stringp, const char *eol);
|
---|
224 | #endif
|
---|
225 | int pattern_matches (const char *pattern, const char *percent, const char *str);
|
---|
226 | char *subst_expand (char *o, const char *text, const char *subst,
|
---|
227 | const char *replace, unsigned int slen, unsigned int rlen,
|
---|
228 | int by_word);
|
---|
229 | char *patsubst_expand_pat (char *o, const char *text, const char *pattern,
|
---|
230 | const char *replace, const char *pattern_percent,
|
---|
231 | const char *replace_percent);
|
---|
232 | char *patsubst_expand (char *o, const char *text, char *pattern, char *replace);
|
---|
233 | #ifdef CONFIG_WITH_COMMANDS_FUNC
|
---|
234 | char *func_commands (char *o, char **argv, const char *funcname);
|
---|
235 | #endif
|
---|
236 |
|
---|
237 | /* expand.c */
|
---|
238 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
239 | char *recursively_expand_for_file (struct variable *v, struct file *file);
|
---|
240 | #define recursively_expand(v) recursively_expand_for_file (v, NULL)
|
---|
241 | #else
|
---|
242 | char *recursively_expand_for_file (struct variable *v, struct file *file,
|
---|
243 | unsigned int *value_lenp);
|
---|
244 | #define recursively_expand(v) recursively_expand_for_file (v, NULL, NULL)
|
---|
245 | #endif
|
---|
246 |
|
---|
247 | /* variable.c */
|
---|
248 | struct variable_set_list *create_new_variable_set (void);
|
---|
249 | void free_variable_set (struct variable_set_list *);
|
---|
250 | struct variable_set_list *push_new_variable_scope (void);
|
---|
251 | void pop_variable_scope (void);
|
---|
252 | void define_automatic_variables (void);
|
---|
253 | void initialize_file_variables (struct file *file, int reading);
|
---|
254 | void print_file_variables (const struct file *file);
|
---|
255 | void print_variable_set (struct variable_set *set, char *prefix);
|
---|
256 | void merge_variable_set_lists (struct variable_set_list **to_list,
|
---|
257 | struct variable_set_list *from_list);
|
---|
258 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
259 | struct variable *do_variable_definition (const struct floc *flocp,
|
---|
260 | const char *name, const char *value,
|
---|
261 | enum variable_origin origin,
|
---|
262 | enum variable_flavor flavor,
|
---|
263 | int target_var);
|
---|
264 | struct variable *parse_variable_definition (struct variable *v, char *line);
|
---|
265 | struct variable *try_variable_definition (const struct floc *flocp, char *line,
|
---|
266 | enum variable_origin origin,
|
---|
267 | int target_var);
|
---|
268 | #else /* CONFIG_WITH_VALUE_LENGTH */
|
---|
269 | # define do_variable_definition(flocp, varname, value, origin, flavor, target_var) \
|
---|
270 | do_variable_definition_2 ((flocp), (varname), (value), ~0U, 0, NULL, \
|
---|
271 | (origin), (flavor), (target_var))
|
---|
272 |
|
---|
273 | struct variable *do_variable_definition_2 (const struct floc *flocp,
|
---|
274 | const char *varname,
|
---|
275 | const char *value,
|
---|
276 | unsigned int value_len,
|
---|
277 | int simple_value, char *free_value,
|
---|
278 | enum variable_origin origin,
|
---|
279 | enum variable_flavor flavor,
|
---|
280 | int target_var);
|
---|
281 | struct variable *parse_variable_definition (struct variable *v, char *line,
|
---|
282 | char *eos);
|
---|
283 | struct variable *try_variable_definition (const struct floc *flocp, char *line,
|
---|
284 | char *eos,
|
---|
285 | enum variable_origin origin,
|
---|
286 | int target_var);
|
---|
287 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
288 | void init_hash_global_variable_set (void);
|
---|
289 | void hash_init_function_table (void);
|
---|
290 | struct variable *lookup_variable (const char *name, unsigned int length);
|
---|
291 | struct variable *lookup_variable_in_set (const char *name, unsigned int length,
|
---|
292 | const struct variable_set *set);
|
---|
293 |
|
---|
294 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
295 | void append_string_to_variable (struct variable *v, const char *value,
|
---|
296 | unsigned int value_len, int append);
|
---|
297 |
|
---|
298 | struct variable *define_variable_in_set (const char *name, unsigned int length,
|
---|
299 | const char *value,
|
---|
300 | unsigned int value_length,
|
---|
301 | int duplicate_value,
|
---|
302 | enum variable_origin origin,
|
---|
303 | int recursive,
|
---|
304 | struct variable_set *set,
|
---|
305 | const struct floc *flocp);
|
---|
306 |
|
---|
307 | /* Define a variable in the current variable set. */
|
---|
308 |
|
---|
309 | #define define_variable(n,l,v,o,r) \
|
---|
310 | define_variable_in_set((n),(l),(v),~0U,1,(o),(r),\
|
---|
311 | current_variable_set_list->set,NILF)
|
---|
312 |
|
---|
313 | #define define_variable_vl(n,l,v,vl,dv,o,r) \
|
---|
314 | define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),\
|
---|
315 | current_variable_set_list->set,NILF)
|
---|
316 |
|
---|
317 | /* Define a variable with a location in the current variable set. */
|
---|
318 |
|
---|
319 | #define define_variable_loc(n,l,v,o,r,f) \
|
---|
320 | define_variable_in_set((n),(l),(v),~0U,1,(o),(r),\
|
---|
321 | current_variable_set_list->set,(f))
|
---|
322 |
|
---|
323 | /* Define a variable with a location in the global variable set. */
|
---|
324 |
|
---|
325 | #define define_variable_global(n,l,v,o,r,f) \
|
---|
326 | define_variable_in_set((n),(l),(v),~0U,1,(o),(r),NULL,(f))
|
---|
327 |
|
---|
328 | #define define_variable_vl_global(n,l,v,vl,dv,o,r,f) \
|
---|
329 | define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),NULL,(f))
|
---|
330 |
|
---|
331 | /* Define a variable in FILE's variable set. */
|
---|
332 |
|
---|
333 | #define define_variable_for_file(n,l,v,o,r,f) \
|
---|
334 | define_variable_in_set((n),(l),(v),~0U,1,(o),(r),(f)->variables->set,NILF)
|
---|
335 |
|
---|
336 | #else /* !CONFIG_WITH_VALUE_LENGTH */
|
---|
337 |
|
---|
338 | struct variable *define_variable_in_set (const char *name, unsigned int length,
|
---|
339 | const char *value,
|
---|
340 | enum variable_origin origin,
|
---|
341 | int recursive,
|
---|
342 | struct variable_set *set,
|
---|
343 | const struct floc *flocp);
|
---|
344 |
|
---|
345 | /* Define a variable in the current variable set. */
|
---|
346 |
|
---|
347 | #define define_variable(n,l,v,o,r) \
|
---|
348 | define_variable_in_set((n),(l),(v),(o),(r),\
|
---|
349 | current_variable_set_list->set,NILF) /* force merge conflict */
|
---|
350 |
|
---|
351 | /* Define a variable with a location in the current variable set. */
|
---|
352 |
|
---|
353 | #define define_variable_loc(n,l,v,o,r,f) \
|
---|
354 | define_variable_in_set((n),(l),(v),(o),(r),\
|
---|
355 | current_variable_set_list->set,(f)) /* force merge conflict */
|
---|
356 |
|
---|
357 | /* Define a variable with a location in the global variable set. */
|
---|
358 |
|
---|
359 | #define define_variable_global(n,l,v,o,r,f) \
|
---|
360 | define_variable_in_set((n),(l),(v),(o),(r),NULL,(f)) /* force merge conflict */
|
---|
361 |
|
---|
362 | /* Define a variable in FILE's variable set. */
|
---|
363 |
|
---|
364 | #define define_variable_for_file(n,l,v,o,r,f) \
|
---|
365 | define_variable_in_set((n),(l),(v),(o),(r),(f)->variables->set,NILF) /* force merge conflict */
|
---|
366 |
|
---|
367 | #endif /* !CONFIG_WITH_VALUE_LENGTH */
|
---|
368 |
|
---|
369 | /* Warn that NAME is an undefined variable. */
|
---|
370 |
|
---|
371 | #define warn_undefined(n,l) do{\
|
---|
372 | if (warn_undefined_variables_flag) \
|
---|
373 | error (reading_file, \
|
---|
374 | _("warning: undefined variable `%.*s'"), \
|
---|
375 | (int)(l), (n)); \
|
---|
376 | }while(0)
|
---|
377 |
|
---|
378 | char **target_environment (struct file *file);
|
---|
379 |
|
---|
380 | struct pattern_var *create_pattern_var (const char *target,
|
---|
381 | const char *suffix);
|
---|
382 |
|
---|
383 | extern int export_all_variables;
|
---|
384 |
|
---|
385 | #ifdef KMK
|
---|
386 | # define MAKELEVEL_NAME "KMK_LEVEL"
|
---|
387 | #else
|
---|
388 | #define MAKELEVEL_NAME "MAKELEVEL"
|
---|
389 | #endif
|
---|
390 | #define MAKELEVEL_LENGTH (sizeof (MAKELEVEL_NAME) - 1)
|
---|