1 | /* Internals of variables for 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 "make.h"
|
---|
20 |
|
---|
21 | #include <assert.h>
|
---|
22 |
|
---|
23 | #include "dep.h"
|
---|
24 | #include "filedef.h"
|
---|
25 | #include "job.h"
|
---|
26 | #include "commands.h"
|
---|
27 | #include "variable.h"
|
---|
28 | #include "rule.h"
|
---|
29 | #ifdef WINDOWS32
|
---|
30 | #include "pathstuff.h"
|
---|
31 | #endif
|
---|
32 | #include "hash.h"
|
---|
33 |
|
---|
34 | /* Chain of all pattern-specific variables. */
|
---|
35 |
|
---|
36 | static struct pattern_var *pattern_vars;
|
---|
37 |
|
---|
38 | /* Pointer to last struct in the chain, so we can add onto the end. */
|
---|
39 |
|
---|
40 | static struct pattern_var *last_pattern_var;
|
---|
41 |
|
---|
42 | /* Create a new pattern-specific variable struct. */
|
---|
43 |
|
---|
44 | struct pattern_var *
|
---|
45 | create_pattern_var (char *target, char *suffix)
|
---|
46 | {
|
---|
47 | register struct pattern_var *p
|
---|
48 | = (struct pattern_var *) xmalloc (sizeof (struct pattern_var));
|
---|
49 |
|
---|
50 | if (last_pattern_var != 0)
|
---|
51 | last_pattern_var->next = p;
|
---|
52 | else
|
---|
53 | pattern_vars = p;
|
---|
54 | last_pattern_var = p;
|
---|
55 | p->next = 0;
|
---|
56 |
|
---|
57 | p->target = target;
|
---|
58 | p->len = strlen (target);
|
---|
59 | p->suffix = suffix + 1;
|
---|
60 |
|
---|
61 | return p;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /* Look up a target in the pattern-specific variable list. */
|
---|
65 |
|
---|
66 | static struct pattern_var *
|
---|
67 | lookup_pattern_var (struct pattern_var *start, char *target)
|
---|
68 | {
|
---|
69 | struct pattern_var *p;
|
---|
70 | unsigned int targlen = strlen(target);
|
---|
71 |
|
---|
72 | for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
|
---|
73 | {
|
---|
74 | char *stem;
|
---|
75 | unsigned int stemlen;
|
---|
76 |
|
---|
77 | if (p->len > targlen)
|
---|
78 | /* It can't possibly match. */
|
---|
79 | continue;
|
---|
80 |
|
---|
81 | /* From the lengths of the filename and the pattern parts,
|
---|
82 | find the stem: the part of the filename that matches the %. */
|
---|
83 | stem = target + (p->suffix - p->target - 1);
|
---|
84 | stemlen = targlen - p->len + 1;
|
---|
85 |
|
---|
86 | /* Compare the text in the pattern before the stem, if any. */
|
---|
87 | if (stem > target && !strneq (p->target, target, stem - target))
|
---|
88 | continue;
|
---|
89 |
|
---|
90 | /* Compare the text in the pattern after the stem, if any.
|
---|
91 | We could test simply using streq, but this way we compare the
|
---|
92 | first two characters immediately. This saves time in the very
|
---|
93 | common case where the first character matches because it is a
|
---|
94 | period. */
|
---|
95 | if (*p->suffix == stem[stemlen]
|
---|
96 | && (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
|
---|
97 | break;
|
---|
98 | }
|
---|
99 |
|
---|
100 | return p;
|
---|
101 | }
|
---|
102 | |
---|
103 |
|
---|
104 | /* Hash table of all global variable definitions. */
|
---|
105 |
|
---|
106 | #ifdef CONFIG_WITH_OPTIMIZATION_HACKS
|
---|
107 | # ifdef _MSC_VER
|
---|
108 | # define inline _inline
|
---|
109 | typedef signed int int32_t;
|
---|
110 | typedef signed short int int16_t;
|
---|
111 | # endif
|
---|
112 | static inline unsigned long variable_hash_2i(register const unsigned char *var, register int length)
|
---|
113 | {
|
---|
114 | register unsigned int hash = 0;
|
---|
115 | var += length;
|
---|
116 | switch (length)
|
---|
117 | {
|
---|
118 | default:
|
---|
119 | case 16: hash = *--var + (hash << 6) + (hash << 16) - hash;
|
---|
120 | case 15: hash = *--var + (hash << 6) + (hash << 16) - hash;
|
---|
121 | case 14: hash = *--var + (hash << 6) + (hash << 16) - hash;
|
---|
122 | case 13: hash = *--var + (hash << 6) + (hash << 16) - hash;
|
---|
123 | case 12: hash = *--var + (hash << 6) + (hash << 16) - hash;
|
---|
124 | case 11: hash = *--var + (hash << 6) + (hash << 16) - hash;
|
---|
125 | case 10: hash = *--var + (hash << 6) + (hash << 16) - hash;
|
---|
126 | case 9: hash = *--var + (hash << 6) + (hash << 16) - hash;
|
---|
127 | case 8: hash = *--var + (hash << 6) + (hash << 16) - hash;
|
---|
128 | case 7: hash = *--var + (hash << 6) + (hash << 16) - hash;
|
---|
129 | case 6: hash = *--var + (hash << 6) + (hash << 16) - hash;
|
---|
130 | case 5: hash = *--var + (hash << 6) + (hash << 16) - hash;
|
---|
131 | case 4: hash = *--var + (hash << 6) + (hash << 16) - hash;
|
---|
132 | case 3: hash = *--var + (hash << 6) + (hash << 16) - hash;
|
---|
133 | case 2: hash = *--var + (hash << 6) + (hash << 16) - hash;
|
---|
134 | case 1: hash = *--var + (hash << 6) + (hash << 16) - hash;
|
---|
135 | case 0:
|
---|
136 | break;
|
---|
137 | }
|
---|
138 | return hash;
|
---|
139 | }
|
---|
140 |
|
---|
141 | static inline unsigned long variable_hash_1i(register const unsigned char *var, register int length)
|
---|
142 | {
|
---|
143 | register unsigned int hash = ((5381 << 5) + 5381) + *var;
|
---|
144 | switch (length)
|
---|
145 | {
|
---|
146 | default:
|
---|
147 | case 8: hash = ((hash << 5) + hash) + var[7];
|
---|
148 | case 7: hash = ((hash << 5) + hash) + var[6];
|
---|
149 | case 6: hash = ((hash << 5) + hash) + var[5];
|
---|
150 | case 5: hash = ((hash << 5) + hash) + var[4];
|
---|
151 | case 4: hash = ((hash << 5) + hash) + var[3];
|
---|
152 | case 3: hash = ((hash << 5) + hash) + var[2];
|
---|
153 | case 2: hash = ((hash << 5) + hash) + var[1];
|
---|
154 | case 1: return hash;
|
---|
155 | case 0: return 5381; /* shouldn't happen */
|
---|
156 | }
|
---|
157 | }
|
---|
158 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
159 |
|
---|
160 | static unsigned long
|
---|
161 | variable_hash_1 (const void *keyv)
|
---|
162 | {
|
---|
163 | struct variable const *key = (struct variable const *) keyv;
|
---|
164 | #ifdef VARIABLE_HASH /* bird */
|
---|
165 | # ifdef VARIABLE_HASH_STRICT
|
---|
166 | if (key->hash1 != variable_hash_1i (key->name, key->length))
|
---|
167 | __asm__("int3");
|
---|
168 | if (key->hash2 && key->hash2 != variable_hash_2i (key->name, key->length))
|
---|
169 | __asm__("int3");
|
---|
170 | # endif
|
---|
171 | return key->hash1;
|
---|
172 | #else
|
---|
173 | # ifdef CONFIG_WITH_OPTIMIZATION_HACKS
|
---|
174 | return variable_hash_1i (key->name, key->length);
|
---|
175 | # else
|
---|
176 | return_STRING_N_HASH_1 (key->name, key->length);
|
---|
177 | # endif
|
---|
178 | #endif
|
---|
179 | }
|
---|
180 |
|
---|
181 | static unsigned long
|
---|
182 | variable_hash_2 (const void *keyv)
|
---|
183 | {
|
---|
184 | #ifdef VARIABLE_HASH /* bird */
|
---|
185 | struct variable *key = (struct variable *) keyv;
|
---|
186 | if (!key->hash2)
|
---|
187 | key->hash2 = variable_hash_2i (key->name, key->length);
|
---|
188 | return key->hash2;
|
---|
189 | #else
|
---|
190 | struct variable const *key = (struct variable const *) keyv;
|
---|
191 | # ifdef CONFIG_WITH_OPTIMIZATION_HACKS
|
---|
192 | return variable_hash_2i (key->name, key->length);
|
---|
193 | # else
|
---|
194 | return_STRING_N_HASH_2 (key->name, key->length);
|
---|
195 | # endif
|
---|
196 | #endif
|
---|
197 | }
|
---|
198 |
|
---|
199 | static int
|
---|
200 | variable_hash_cmp (const void *xv, const void *yv)
|
---|
201 | {
|
---|
202 | struct variable const *x = (struct variable const *) xv;
|
---|
203 | struct variable const *y = (struct variable const *) yv;
|
---|
204 | int result = x->length - y->length;
|
---|
205 | if (result)
|
---|
206 | return result;
|
---|
207 | #ifdef VARIABLE_HASH_STRICT /* bird */
|
---|
208 | if (x->hash1 != variable_hash_1i (x->name, x->length))
|
---|
209 | __asm__("int3");
|
---|
210 | if (x->hash2 && x->hash2 != variable_hash_2i (x->name, x->length))
|
---|
211 | __asm__("int3");
|
---|
212 | if (y->hash1 != variable_hash_1i (y->name, y->length))
|
---|
213 | __asm__("int3");
|
---|
214 | if (y->hash2 && y->hash2 != variable_hash_2i (y->name, y->length))
|
---|
215 | __asm__("int3");
|
---|
216 | #endif
|
---|
217 | #ifdef VARIABLE_HASH
|
---|
218 | /* hash 1 */
|
---|
219 | result = x->hash1 - y->hash1;
|
---|
220 | if (result)
|
---|
221 | return result;
|
---|
222 | #endif
|
---|
223 | #ifdef CONFIG_WITH_OPTIMIZATION_HACKS /* bird: speed */
|
---|
224 | {
|
---|
225 | const char *xs = x->name;
|
---|
226 | const char *ys = y->name;
|
---|
227 | switch (x->length)
|
---|
228 | {
|
---|
229 | case 8:
|
---|
230 | result = *(int32_t*)(xs + 4) - *(int32_t*)(ys + 4);
|
---|
231 | if (result)
|
---|
232 | return result;
|
---|
233 | return *(int32_t*)xs - *(int32_t*)ys;
|
---|
234 | case 7:
|
---|
235 | result = xs[6] - ys[6];
|
---|
236 | if (result)
|
---|
237 | return result;
|
---|
238 | case 6:
|
---|
239 | result = *(int32_t*)xs - *(int32_t*)ys;
|
---|
240 | if (result)
|
---|
241 | return result;
|
---|
242 | return *(int16_t*)(xs + 4) - *(int16_t*)(ys + 4);
|
---|
243 | case 5:
|
---|
244 | result = xs[4] - ys[4];
|
---|
245 | if (result)
|
---|
246 | return result;
|
---|
247 | case 4:
|
---|
248 | return *(int32_t*)xs - *(int32_t*)ys;
|
---|
249 | case 3:
|
---|
250 | result = xs[2] - ys[2];
|
---|
251 | if (result)
|
---|
252 | return result;
|
---|
253 | case 2:
|
---|
254 | return *(int16_t*)xs - *(int16_t*)ys;
|
---|
255 | case 1:
|
---|
256 | return *xs - *ys;
|
---|
257 | case 0:
|
---|
258 | return 0;
|
---|
259 | }
|
---|
260 | }
|
---|
261 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
262 | #ifdef VARIABLE_HASH
|
---|
263 | /* hash 2 */
|
---|
264 | if (!x->hash2)
|
---|
265 | ((struct variable *)x)->hash2 = variable_hash_2i (x->name, x->length);
|
---|
266 | if (!y->hash2)
|
---|
267 | ((struct variable *)y)->hash2 = variable_hash_2i (y->name, y->length);
|
---|
268 | result = x->hash2 - y->hash2;
|
---|
269 | if (result)
|
---|
270 | return result;
|
---|
271 | #endif
|
---|
272 | #ifdef CONFIG_WITH_OPTIMIZATION_HACKS
|
---|
273 | return memcmp (x->name, y->name, x->length);
|
---|
274 | #else
|
---|
275 | return_STRING_N_COMPARE (x->name, y->name, x->length);
|
---|
276 | #endif
|
---|
277 | }
|
---|
278 |
|
---|
279 | #ifndef VARIABLE_BUCKETS
|
---|
280 | #define VARIABLE_BUCKETS 523
|
---|
281 | #endif
|
---|
282 | #ifndef PERFILE_VARIABLE_BUCKETS
|
---|
283 | #define PERFILE_VARIABLE_BUCKETS 23
|
---|
284 | #endif
|
---|
285 | #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
|
---|
286 | #define SMALL_SCOPE_VARIABLE_BUCKETS 13
|
---|
287 | #endif
|
---|
288 |
|
---|
289 | static struct variable_set global_variable_set;
|
---|
290 | static struct variable_set_list global_setlist
|
---|
291 | = { 0, &global_variable_set };
|
---|
292 | struct variable_set_list *current_variable_set_list = &global_setlist;
|
---|
293 | |
---|
294 |
|
---|
295 | /* Implement variables. */
|
---|
296 |
|
---|
297 | void
|
---|
298 | init_hash_global_variable_set (void)
|
---|
299 | {
|
---|
300 | hash_init (&global_variable_set.table,
|
---|
301 | #ifdef KMK /* FIMXE: just redefine the bucket size! */
|
---|
302 | 16384,
|
---|
303 | #else
|
---|
304 | VARIABLE_BUCKETS,
|
---|
305 | #endif
|
---|
306 | variable_hash_1, variable_hash_2, variable_hash_cmp);
|
---|
307 | }
|
---|
308 |
|
---|
309 | /* Define variable named NAME with value VALUE in SET. VALUE is copied.
|
---|
310 | LENGTH is the length of NAME, which does not need to be null-terminated.
|
---|
311 | ORIGIN specifies the origin of the variable (makefile, command line
|
---|
312 | or environment).
|
---|
313 | If RECURSIVE is nonzero a flag is set in the variable saying
|
---|
314 | that it should be recursively re-expanded. */
|
---|
315 |
|
---|
316 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
317 | struct variable *
|
---|
318 | define_variable_in_set (const char *name, unsigned int length,
|
---|
319 | char *value, unsigned int value_length, int duplicate_value,
|
---|
320 | enum variable_origin origin, int recursive,
|
---|
321 | struct variable_set *set, const struct floc *flocp)
|
---|
322 | #else
|
---|
323 | struct variable *
|
---|
324 | define_variable_in_set (const char *name, unsigned int length,
|
---|
325 | char *value, enum variable_origin origin,
|
---|
326 | int recursive, struct variable_set *set,
|
---|
327 | const struct floc *flocp)
|
---|
328 | #endif
|
---|
329 | {
|
---|
330 | struct variable *v;
|
---|
331 | struct variable **var_slot;
|
---|
332 | struct variable var_key;
|
---|
333 |
|
---|
334 | if (set == NULL)
|
---|
335 | set = &global_variable_set;
|
---|
336 |
|
---|
337 | var_key.name = (char *) name;
|
---|
338 | var_key.length = length;
|
---|
339 | #ifdef VARIABLE_HASH /* bird */
|
---|
340 | var_key.hash1 = variable_hash_1i (name, length);
|
---|
341 | var_key.hash2 = 0;
|
---|
342 | #endif
|
---|
343 | var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
|
---|
344 |
|
---|
345 | if (env_overrides && origin == o_env)
|
---|
346 | origin = o_env_override;
|
---|
347 |
|
---|
348 | v = *var_slot;
|
---|
349 | if (! HASH_VACANT (v))
|
---|
350 | {
|
---|
351 | if (env_overrides && v->origin == o_env)
|
---|
352 | /* V came from in the environment. Since it was defined
|
---|
353 | before the switches were parsed, it wasn't affected by -e. */
|
---|
354 | v->origin = o_env_override;
|
---|
355 |
|
---|
356 | /* A variable of this name is already defined.
|
---|
357 | If the old definition is from a stronger source
|
---|
358 | than this one, don't redefine it. */
|
---|
359 | if ((int) origin >= (int) v->origin)
|
---|
360 | {
|
---|
361 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
362 | if (value_length == ~0U)
|
---|
363 | value_length = strlen (value);
|
---|
364 | else
|
---|
365 | assert (value_length == strlen (value));
|
---|
366 | if (!duplicate_value)
|
---|
367 | {
|
---|
368 | if (v->value != 0)
|
---|
369 | free (v->value);
|
---|
370 | v->value = value;
|
---|
371 | v->value_alloc_len = value_length + 1;
|
---|
372 | }
|
---|
373 | else
|
---|
374 | {
|
---|
375 | if ((unsigned int)v->value_alloc_len <= value_length)
|
---|
376 | {
|
---|
377 | free (v->value);
|
---|
378 | v->value_alloc_len = (value_length + 0x40) & ~0x3f;
|
---|
379 | v->value = xmalloc (v->value_alloc_len);
|
---|
380 | }
|
---|
381 | memcpy (v->value, value, value_length + 1);
|
---|
382 | }
|
---|
383 | v->value_length = value_length;
|
---|
384 | #else
|
---|
385 | if (v->value != 0)
|
---|
386 | free (v->value);
|
---|
387 | v->value = xstrdup (value);
|
---|
388 | #endif
|
---|
389 | if (flocp != 0)
|
---|
390 | v->fileinfo = *flocp;
|
---|
391 | else
|
---|
392 | v->fileinfo.filenm = 0;
|
---|
393 | v->origin = origin;
|
---|
394 | v->recursive = recursive;
|
---|
395 | }
|
---|
396 | return v;
|
---|
397 | }
|
---|
398 |
|
---|
399 | /* Create a new variable definition and add it to the hash table. */
|
---|
400 |
|
---|
401 | v = (struct variable *) xmalloc (sizeof (struct variable));
|
---|
402 | v->name = savestring (name, length);
|
---|
403 | v->length = length;
|
---|
404 | #ifdef VARIABLE_HASH /* bird */
|
---|
405 | v->hash1 = variable_hash_1i (name, length);
|
---|
406 | v->hash2 = 0;
|
---|
407 | #endif
|
---|
408 | hash_insert_at (&set->table, v, var_slot);
|
---|
409 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
410 | if (value_length == ~0U)
|
---|
411 | value_length = strlen (value);
|
---|
412 | else
|
---|
413 | assert (value_length == strlen (value));
|
---|
414 | v->value_length = value_length;
|
---|
415 | if (!duplicate_value)
|
---|
416 | {
|
---|
417 | v->value_alloc_len = value_length + 1;
|
---|
418 | v->value = value;
|
---|
419 | }
|
---|
420 | else
|
---|
421 | {
|
---|
422 | v->value_alloc_len = (value_length + 32) & ~31;
|
---|
423 | v->value = xmalloc (v->value_alloc_len);
|
---|
424 | memcpy (v->value, value, value_length + 1);
|
---|
425 | }
|
---|
426 | #else
|
---|
427 | v->value = xstrdup (value);
|
---|
428 | #endif
|
---|
429 | if (flocp != 0)
|
---|
430 | v->fileinfo = *flocp;
|
---|
431 | else
|
---|
432 | v->fileinfo.filenm = 0;
|
---|
433 | v->origin = origin;
|
---|
434 | v->recursive = recursive;
|
---|
435 | v->special = 0;
|
---|
436 | v->expanding = 0;
|
---|
437 | v->exp_count = 0;
|
---|
438 | v->per_target = 0;
|
---|
439 | v->append = 0;
|
---|
440 | v->export = v_default;
|
---|
441 |
|
---|
442 | v->exportable = 1;
|
---|
443 | if (*name != '_' && (*name < 'A' || *name > 'Z')
|
---|
444 | && (*name < 'a' || *name > 'z'))
|
---|
445 | v->exportable = 0;
|
---|
446 | else
|
---|
447 | {
|
---|
448 | for (++name; *name != '\0'; ++name)
|
---|
449 | if (*name != '_' && (*name < 'a' || *name > 'z')
|
---|
450 | && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
|
---|
451 | break;
|
---|
452 |
|
---|
453 | if (*name != '\0')
|
---|
454 | v->exportable = 0;
|
---|
455 | }
|
---|
456 |
|
---|
457 | return v;
|
---|
458 | }
|
---|
459 | |
---|
460 |
|
---|
461 | /* If the variable passed in is "special", handle its special nature.
|
---|
462 | Currently there are two such variables, both used for introspection:
|
---|
463 | .VARIABLES expands to a list of all the variables defined in this instance
|
---|
464 | of make.
|
---|
465 | .TARGETS expands to a list of all the targets defined in this
|
---|
466 | instance of make.
|
---|
467 | Returns the variable reference passed in. */
|
---|
468 |
|
---|
469 | #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
|
---|
470 |
|
---|
471 | static struct variable *
|
---|
472 | handle_special_var (struct variable *var)
|
---|
473 | {
|
---|
474 | static unsigned long last_var_count = 0;
|
---|
475 |
|
---|
476 |
|
---|
477 | /* This one actually turns out to be very hard, due to the way the parser
|
---|
478 | records targets. The way it works is that target information is collected
|
---|
479 | internally until make knows the target is completely specified. It unitl
|
---|
480 | it sees that some new construct (a new target or variable) is defined that
|
---|
481 | it knows the previous one is done. In short, this means that if you do
|
---|
482 | this:
|
---|
483 |
|
---|
484 | all:
|
---|
485 |
|
---|
486 | TARGS := $(.TARGETS)
|
---|
487 |
|
---|
488 | then $(TARGS) won't contain "all", because it's not until after the
|
---|
489 | variable is created that the previous target is completed.
|
---|
490 |
|
---|
491 | Changing this would be a major pain. I think a less complex way to do it
|
---|
492 | would be to pre-define the target files as soon as the first line is
|
---|
493 | parsed, then come back and do the rest of the definition as now. That
|
---|
494 | would allow $(.TARGETS) to be correct without a major change to the way
|
---|
495 | the parser works.
|
---|
496 |
|
---|
497 | if (streq (var->name, ".TARGETS"))
|
---|
498 | var->value = build_target_list (var->value);
|
---|
499 | else
|
---|
500 | */
|
---|
501 |
|
---|
502 | if (streq (var->name, ".VARIABLES")
|
---|
503 | && global_variable_set.table.ht_fill != last_var_count)
|
---|
504 | {
|
---|
505 | unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
|
---|
506 | unsigned long len;
|
---|
507 | char *p;
|
---|
508 | struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
|
---|
509 | struct variable **end = &vp[global_variable_set.table.ht_size];
|
---|
510 |
|
---|
511 | /* Make sure we have at least MAX bytes in the allocated buffer. */
|
---|
512 | var->value = xrealloc (var->value, max);
|
---|
513 |
|
---|
514 | /* Walk through the hash of variables, constructing a list of names. */
|
---|
515 | p = var->value;
|
---|
516 | len = 0;
|
---|
517 | for (; vp < end; ++vp)
|
---|
518 | if (!HASH_VACANT (*vp))
|
---|
519 | {
|
---|
520 | struct variable *v = *vp;
|
---|
521 | int l = v->length;
|
---|
522 |
|
---|
523 | len += l + 1;
|
---|
524 | if (len > max)
|
---|
525 | {
|
---|
526 | unsigned long off = p - var->value;
|
---|
527 |
|
---|
528 | max += EXPANSION_INCREMENT (l + 1);
|
---|
529 | var->value = xrealloc (var->value, max);
|
---|
530 | p = &var->value[off];
|
---|
531 | }
|
---|
532 |
|
---|
533 | bcopy (v->name, p, l);
|
---|
534 | p += l;
|
---|
535 | *(p++) = ' ';
|
---|
536 | }
|
---|
537 | *(p-1) = '\0';
|
---|
538 |
|
---|
539 | /* Remember how many variables are in our current count. Since we never
|
---|
540 | remove variables from the list, this is a reliable way to know whether
|
---|
541 | the list is up to date or needs to be recomputed. */
|
---|
542 |
|
---|
543 | last_var_count = global_variable_set.table.ht_fill;
|
---|
544 | }
|
---|
545 |
|
---|
546 | return var;
|
---|
547 | }
|
---|
548 |
|
---|
549 | |
---|
550 |
|
---|
551 | /* Lookup a variable whose name is a string starting at NAME
|
---|
552 | and with LENGTH chars. NAME need not be null-terminated.
|
---|
553 | Returns address of the `struct variable' containing all info
|
---|
554 | on the variable, or nil if no such variable is defined. */
|
---|
555 |
|
---|
556 | struct variable *
|
---|
557 | lookup_variable (const char *name, unsigned int length)
|
---|
558 | {
|
---|
559 | const struct variable_set_list *setlist;
|
---|
560 | struct variable var_key;
|
---|
561 |
|
---|
562 | var_key.name = (char *) name;
|
---|
563 | var_key.length = length;
|
---|
564 | #ifdef VARIABLE_HASH /* bird */
|
---|
565 | var_key.hash1 = variable_hash_1i (name, length);
|
---|
566 | var_key.hash2 = 0;
|
---|
567 | #endif
|
---|
568 |
|
---|
569 | for (setlist = current_variable_set_list;
|
---|
570 | setlist != 0; setlist = setlist->next)
|
---|
571 | {
|
---|
572 | #ifdef VARIABLE_HASH /* bird: speed */
|
---|
573 | struct hash_table *ht = &setlist->set->table;
|
---|
574 | unsigned int hash_1 = var_key.hash1;
|
---|
575 | struct variable *v;
|
---|
576 |
|
---|
577 | ht->ht_lookups++;
|
---|
578 | for (;;)
|
---|
579 | {
|
---|
580 | hash_1 &= (ht->ht_size - 1);
|
---|
581 | v = (struct variable *)ht->ht_vec[hash_1];
|
---|
582 |
|
---|
583 | if (v == 0)
|
---|
584 | break;
|
---|
585 | if ((void *)v != hash_deleted_item)
|
---|
586 | {
|
---|
587 | if (variable_hash_cmp(&var_key, v) == 0)
|
---|
588 | {
|
---|
589 | # ifdef VARIABLE_HASH_STRICT /* bird */
|
---|
590 | struct variable *v2 = (struct variable *) hash_find_item ((struct hash_table *) &setlist->set->table, &var_key);
|
---|
591 | assert(v2 == v);
|
---|
592 | # endif
|
---|
593 | return v->special ? handle_special_var (v) : v;
|
---|
594 | }
|
---|
595 | ht->ht_collisions++;
|
---|
596 | }
|
---|
597 | if (!var_key.hash2)
|
---|
598 | var_key.hash2 = variable_hash_2i(name, length);
|
---|
599 | hash_1 += (var_key.hash2 | 1);
|
---|
600 | }
|
---|
601 |
|
---|
602 | #else /* !VARIABLE_HASH */
|
---|
603 | const struct variable_set *set = setlist->set;
|
---|
604 | struct variable *v;
|
---|
605 |
|
---|
606 | v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
|
---|
607 | if (v)
|
---|
608 | return v->special ? handle_special_var (v) : v;
|
---|
609 | #endif /* !VARIABLE_HASH */
|
---|
610 | }
|
---|
611 |
|
---|
612 | #ifdef VMS
|
---|
613 | /* since we don't read envp[] on startup, try to get the
|
---|
614 | variable via getenv() here. */
|
---|
615 | {
|
---|
616 | char *vname = alloca (length + 1);
|
---|
617 | char *value;
|
---|
618 | strncpy (vname, name, length);
|
---|
619 | vname[length] = 0;
|
---|
620 | value = getenv (vname);
|
---|
621 | if (value != 0)
|
---|
622 | {
|
---|
623 | char *sptr;
|
---|
624 | int scnt;
|
---|
625 |
|
---|
626 | sptr = value;
|
---|
627 | scnt = 0;
|
---|
628 |
|
---|
629 | while ((sptr = strchr (sptr, '$')))
|
---|
630 | {
|
---|
631 | scnt++;
|
---|
632 | sptr++;
|
---|
633 | }
|
---|
634 |
|
---|
635 | if (scnt > 0)
|
---|
636 | {
|
---|
637 | char *nvalue;
|
---|
638 | char *nptr;
|
---|
639 |
|
---|
640 | nvalue = alloca (strlen (value) + scnt + 1);
|
---|
641 | sptr = value;
|
---|
642 | nptr = nvalue;
|
---|
643 |
|
---|
644 | while (*sptr)
|
---|
645 | {
|
---|
646 | if (*sptr == '$')
|
---|
647 | {
|
---|
648 | *nptr++ = '$';
|
---|
649 | *nptr++ = '$';
|
---|
650 | }
|
---|
651 | else
|
---|
652 | {
|
---|
653 | *nptr++ = *sptr;
|
---|
654 | }
|
---|
655 | sptr++;
|
---|
656 | }
|
---|
657 |
|
---|
658 | *nptr = '\0';
|
---|
659 | return define_variable (vname, length, nvalue, o_env, 1);
|
---|
660 |
|
---|
661 | }
|
---|
662 |
|
---|
663 | return define_variable (vname, length, value, o_env, 1);
|
---|
664 | }
|
---|
665 | }
|
---|
666 | #endif /* VMS */
|
---|
667 |
|
---|
668 | return 0;
|
---|
669 | }
|
---|
670 | |
---|
671 |
|
---|
672 | /* Lookup a variable whose name is a string starting at NAME
|
---|
673 | and with LENGTH chars in set SET. NAME need not be null-terminated.
|
---|
674 | Returns address of the `struct variable' containing all info
|
---|
675 | on the variable, or nil if no such variable is defined. */
|
---|
676 |
|
---|
677 | struct variable *
|
---|
678 | lookup_variable_in_set (const char *name, unsigned int length,
|
---|
679 | const struct variable_set *set)
|
---|
680 | {
|
---|
681 | struct variable var_key;
|
---|
682 |
|
---|
683 | var_key.name = (char *) name;
|
---|
684 | var_key.length = length;
|
---|
685 | #ifdef VARIABLE_HASH /* bird */
|
---|
686 | var_key.hash1 = variable_hash_1i (name, length);
|
---|
687 | var_key.hash2 = 0;
|
---|
688 | #endif
|
---|
689 |
|
---|
690 | return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
|
---|
691 | }
|
---|
692 | |
---|
693 |
|
---|
694 | /* Initialize FILE's variable set list. If FILE already has a variable set
|
---|
695 | list, the topmost variable set is left intact, but the the rest of the
|
---|
696 | chain is replaced with FILE->parent's setlist. If FILE is a double-colon
|
---|
697 | rule, then we will use the "root" double-colon target's variable set as the
|
---|
698 | parent of FILE's variable set.
|
---|
699 |
|
---|
700 | If we're READing a makefile, don't do the pattern variable search now,
|
---|
701 | since the pattern variable might not have been defined yet. */
|
---|
702 |
|
---|
703 | void
|
---|
704 | initialize_file_variables (struct file *file, int reading)
|
---|
705 | {
|
---|
706 | struct variable_set_list *l = file->variables;
|
---|
707 |
|
---|
708 | if (l == 0)
|
---|
709 | {
|
---|
710 | l = (struct variable_set_list *)
|
---|
711 | xmalloc (sizeof (struct variable_set_list));
|
---|
712 | l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
|
---|
713 | hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
|
---|
714 | variable_hash_1, variable_hash_2, variable_hash_cmp);
|
---|
715 | file->variables = l;
|
---|
716 | }
|
---|
717 |
|
---|
718 | /* If this is a double-colon, then our "parent" is the "root" target for
|
---|
719 | this double-colon rule. Since that rule has the same name, parent,
|
---|
720 | etc. we can just use its variables as the "next" for ours. */
|
---|
721 |
|
---|
722 | if (file->double_colon && file->double_colon != file)
|
---|
723 | {
|
---|
724 | initialize_file_variables (file->double_colon, reading);
|
---|
725 | l->next = file->double_colon->variables;
|
---|
726 | return;
|
---|
727 | }
|
---|
728 |
|
---|
729 | if (file->parent == 0)
|
---|
730 | l->next = &global_setlist;
|
---|
731 | else
|
---|
732 | {
|
---|
733 | initialize_file_variables (file->parent, reading);
|
---|
734 | l->next = file->parent->variables;
|
---|
735 | }
|
---|
736 |
|
---|
737 | /* If we're not reading makefiles and we haven't looked yet, see if
|
---|
738 | we can find pattern variables for this target. */
|
---|
739 |
|
---|
740 | if (!reading && !file->pat_searched)
|
---|
741 | {
|
---|
742 | struct pattern_var *p;
|
---|
743 |
|
---|
744 | p = lookup_pattern_var (0, file->name);
|
---|
745 | if (p != 0)
|
---|
746 | {
|
---|
747 | struct variable_set_list *global = current_variable_set_list;
|
---|
748 |
|
---|
749 | /* We found at least one. Set up a new variable set to accumulate
|
---|
750 | all the pattern variables that match this target. */
|
---|
751 |
|
---|
752 | file->pat_variables = create_new_variable_set ();
|
---|
753 | current_variable_set_list = file->pat_variables;
|
---|
754 |
|
---|
755 | do
|
---|
756 | {
|
---|
757 | /* We found one, so insert it into the set. */
|
---|
758 |
|
---|
759 | struct variable *v;
|
---|
760 |
|
---|
761 | if (p->variable.flavor == f_simple)
|
---|
762 | {
|
---|
763 | v = define_variable_loc (
|
---|
764 | p->variable.name, strlen (p->variable.name),
|
---|
765 | p->variable.value, p->variable.origin,
|
---|
766 | 0, &p->variable.fileinfo);
|
---|
767 |
|
---|
768 | v->flavor = f_simple;
|
---|
769 | }
|
---|
770 | else
|
---|
771 | {
|
---|
772 | v = do_variable_definition (
|
---|
773 | &p->variable.fileinfo, p->variable.name,
|
---|
774 | p->variable.value, p->variable.origin,
|
---|
775 | p->variable.flavor, 1);
|
---|
776 | }
|
---|
777 |
|
---|
778 | /* Also mark it as a per-target and copy export status. */
|
---|
779 | v->per_target = p->variable.per_target;
|
---|
780 | v->export = p->variable.export;
|
---|
781 | }
|
---|
782 | while ((p = lookup_pattern_var (p, file->name)) != 0);
|
---|
783 |
|
---|
784 | current_variable_set_list = global;
|
---|
785 | }
|
---|
786 | file->pat_searched = 1;
|
---|
787 | }
|
---|
788 |
|
---|
789 | /* If we have a pattern variable match, set it up. */
|
---|
790 |
|
---|
791 | if (file->pat_variables != 0)
|
---|
792 | {
|
---|
793 | file->pat_variables->next = l->next;
|
---|
794 | l->next = file->pat_variables;
|
---|
795 | }
|
---|
796 | }
|
---|
797 | |
---|
798 |
|
---|
799 | /* Pop the top set off the current variable set list,
|
---|
800 | and free all its storage. */
|
---|
801 |
|
---|
802 | struct variable_set_list *
|
---|
803 | create_new_variable_set (void)
|
---|
804 | {
|
---|
805 | register struct variable_set_list *setlist;
|
---|
806 | register struct variable_set *set;
|
---|
807 |
|
---|
808 | set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
|
---|
809 | hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
|
---|
810 | variable_hash_1, variable_hash_2, variable_hash_cmp);
|
---|
811 |
|
---|
812 | setlist = (struct variable_set_list *)
|
---|
813 | xmalloc (sizeof (struct variable_set_list));
|
---|
814 | setlist->set = set;
|
---|
815 | setlist->next = current_variable_set_list;
|
---|
816 |
|
---|
817 | return setlist;
|
---|
818 | }
|
---|
819 |
|
---|
820 | static void
|
---|
821 | free_variable_name_and_value (const void *item)
|
---|
822 | {
|
---|
823 | struct variable *v = (struct variable *) item;
|
---|
824 | free (v->name);
|
---|
825 | free (v->value);
|
---|
826 | }
|
---|
827 |
|
---|
828 | void
|
---|
829 | free_variable_set (struct variable_set_list *list)
|
---|
830 | {
|
---|
831 | hash_map (&list->set->table, free_variable_name_and_value);
|
---|
832 | hash_free (&list->set->table, 1);
|
---|
833 | free ((char *) list->set);
|
---|
834 | free ((char *) list);
|
---|
835 | }
|
---|
836 |
|
---|
837 | /* Create a new variable set and push it on the current setlist.
|
---|
838 | If we're pushing a global scope (that is, the current scope is the global
|
---|
839 | scope) then we need to "push" it the other way: file variable sets point
|
---|
840 | directly to the global_setlist so we need to replace that with the new one.
|
---|
841 | */
|
---|
842 |
|
---|
843 | struct variable_set_list *
|
---|
844 | push_new_variable_scope (void)
|
---|
845 | {
|
---|
846 | current_variable_set_list = create_new_variable_set();
|
---|
847 | if (current_variable_set_list->next == &global_setlist)
|
---|
848 | {
|
---|
849 | /* It was the global, so instead of new -> &global we want to replace
|
---|
850 | &global with the new one and have &global -> new, with current still
|
---|
851 | pointing to &global */
|
---|
852 | struct variable_set *set = current_variable_set_list->set;
|
---|
853 | current_variable_set_list->set = global_setlist.set;
|
---|
854 | global_setlist.set = set;
|
---|
855 | current_variable_set_list->next = global_setlist.next;
|
---|
856 | global_setlist.next = current_variable_set_list;
|
---|
857 | current_variable_set_list = &global_setlist;
|
---|
858 | }
|
---|
859 | return (current_variable_set_list);
|
---|
860 | }
|
---|
861 |
|
---|
862 | void
|
---|
863 | pop_variable_scope (void)
|
---|
864 | {
|
---|
865 | struct variable_set_list *setlist;
|
---|
866 | struct variable_set *set;
|
---|
867 |
|
---|
868 | /* Can't call this if there's no scope to pop! */
|
---|
869 | assert(current_variable_set_list->next != NULL);
|
---|
870 |
|
---|
871 | if (current_variable_set_list != &global_setlist)
|
---|
872 | {
|
---|
873 | /* We're not pointing to the global setlist, so pop this one. */
|
---|
874 | setlist = current_variable_set_list;
|
---|
875 | set = setlist->set;
|
---|
876 | current_variable_set_list = setlist->next;
|
---|
877 | }
|
---|
878 | else
|
---|
879 | {
|
---|
880 | /* This set is the one in the global_setlist, but there is another global
|
---|
881 | set beyond that. We want to copy that set to global_setlist, then
|
---|
882 | delete what used to be in global_setlist. */
|
---|
883 | setlist = global_setlist.next;
|
---|
884 | set = global_setlist.set;
|
---|
885 | global_setlist.set = setlist->set;
|
---|
886 | global_setlist.next = setlist->next;
|
---|
887 | }
|
---|
888 |
|
---|
889 | /* Free the one we no longer need. */
|
---|
890 | free ((char *) setlist);
|
---|
891 | hash_map (&set->table, free_variable_name_and_value);
|
---|
892 | hash_free (&set->table, 1);
|
---|
893 | free ((char *) set);
|
---|
894 | }
|
---|
895 | |
---|
896 |
|
---|
897 | /* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
|
---|
898 |
|
---|
899 | static void
|
---|
900 | merge_variable_sets (struct variable_set *to_set,
|
---|
901 | struct variable_set *from_set)
|
---|
902 | {
|
---|
903 | struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
|
---|
904 | struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
|
---|
905 |
|
---|
906 | for ( ; from_var_slot < from_var_end; from_var_slot++)
|
---|
907 | if (! HASH_VACANT (*from_var_slot))
|
---|
908 | {
|
---|
909 | struct variable *from_var = *from_var_slot;
|
---|
910 | struct variable **to_var_slot
|
---|
911 | = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
|
---|
912 | if (HASH_VACANT (*to_var_slot))
|
---|
913 | hash_insert_at (&to_set->table, from_var, to_var_slot);
|
---|
914 | else
|
---|
915 | {
|
---|
916 | /* GKM FIXME: delete in from_set->table */
|
---|
917 | free (from_var->value);
|
---|
918 | free (from_var);
|
---|
919 | }
|
---|
920 | }
|
---|
921 | }
|
---|
922 |
|
---|
923 | /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
|
---|
924 |
|
---|
925 | void
|
---|
926 | merge_variable_set_lists (struct variable_set_list **setlist0,
|
---|
927 | struct variable_set_list *setlist1)
|
---|
928 | {
|
---|
929 | struct variable_set_list *to = *setlist0;
|
---|
930 | struct variable_set_list *last0 = 0;
|
---|
931 |
|
---|
932 | /* If there's nothing to merge, stop now. */
|
---|
933 | if (!setlist1)
|
---|
934 | return;
|
---|
935 |
|
---|
936 | /* This loop relies on the fact that all setlists terminate with the global
|
---|
937 | setlist (before NULL). If that's not true, arguably we SHOULD die. */
|
---|
938 | if (to)
|
---|
939 | while (setlist1 != &global_setlist && to != &global_setlist)
|
---|
940 | {
|
---|
941 | struct variable_set_list *from = setlist1;
|
---|
942 | setlist1 = setlist1->next;
|
---|
943 |
|
---|
944 | merge_variable_sets (to->set, from->set);
|
---|
945 |
|
---|
946 | last0 = to;
|
---|
947 | to = to->next;
|
---|
948 | }
|
---|
949 |
|
---|
950 | if (setlist1 != &global_setlist)
|
---|
951 | {
|
---|
952 | if (last0 == 0)
|
---|
953 | *setlist0 = setlist1;
|
---|
954 | else
|
---|
955 | last0->next = setlist1;
|
---|
956 | }
|
---|
957 | }
|
---|
958 | |
---|
959 |
|
---|
960 | /* Define the automatic variables, and record the addresses
|
---|
961 | of their structures so we can change their values quickly. */
|
---|
962 |
|
---|
963 | void
|
---|
964 | define_automatic_variables (void)
|
---|
965 | {
|
---|
966 | #if defined(WINDOWS32) || defined(__EMX__)
|
---|
967 | extern char* default_shell;
|
---|
968 | #else
|
---|
969 | extern char default_shell[];
|
---|
970 | #endif
|
---|
971 | register struct variable *v;
|
---|
972 | char buf[200];
|
---|
973 | #ifdef KMK
|
---|
974 | const char *envvar;
|
---|
975 | #endif
|
---|
976 |
|
---|
977 | sprintf (buf, "%u", makelevel);
|
---|
978 | (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
|
---|
979 |
|
---|
980 | sprintf (buf, "%s%s%s",
|
---|
981 | version_string,
|
---|
982 | (remote_description == 0 || remote_description[0] == '\0')
|
---|
983 | ? "" : "-",
|
---|
984 | (remote_description == 0 || remote_description[0] == '\0')
|
---|
985 | ? "" : remote_description);
|
---|
986 | (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
|
---|
987 |
|
---|
988 | #ifdef KMK
|
---|
989 | /* Define KMK_VERSION to indicate kMk. */
|
---|
990 | (void) define_variable ("KMK_VERSION", 11, buf, o_default, 0);
|
---|
991 |
|
---|
992 | /* Define KBUILD_VERSION* */
|
---|
993 | sprintf (buf, "%d", KBUILD_VERSION_MAJOR);
|
---|
994 | (void) define_variable ("KBUILD_VERSION_MAJOR", sizeof("KBUILD_VERSION_MAJOR") - 1,
|
---|
995 | buf, o_default, 0);
|
---|
996 | sprintf (buf, "%d", KBUILD_VERSION_MINOR);
|
---|
997 | (void) define_variable ("KBUILD_VERSION_MINOR", sizeof("KBUILD_VERSION_MINOR") - 1,
|
---|
998 | buf, o_default, 0);
|
---|
999 | sprintf (buf, "%d", KBUILD_VERSION_PATCH);
|
---|
1000 | (void) define_variable ("KBUILD_VERSION_PATCH", sizeof("KBUILD_VERSION_PATCH") - 1,
|
---|
1001 | buf, o_default, 0);
|
---|
1002 |
|
---|
1003 | sprintf (buf, "%d.%d.%d", KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR, KBUILD_VERSION_PATCH);
|
---|
1004 | (void) define_variable ("KBUILD_VERSION", sizeof("KBUILD_VERSION") - 1,
|
---|
1005 | buf, o_default, 0);
|
---|
1006 |
|
---|
1007 | /* The build platform defaults. */
|
---|
1008 | envvar = getenv ("BUILD_PLATFORM");
|
---|
1009 | if (!envvar)
|
---|
1010 | (void) define_variable ("BUILD_PLATFORM", sizeof("BUILD_PLATFORM") - 1,
|
---|
1011 | BUILD_PLATFORM, o_default, 0);
|
---|
1012 | envvar = getenv ("BUILD_PLATFORM_ARCH");
|
---|
1013 | if (!envvar)
|
---|
1014 | (void) define_variable ("BUILD_PLATFORM_ARCH", sizeof("BUILD_PLATFORM_ARCH") - 1,
|
---|
1015 | BUILD_PLATFORM_ARCH, o_default, 0);
|
---|
1016 | envvar = getenv ("BUILD_PLATFORM_CPU");
|
---|
1017 | if (!envvar)
|
---|
1018 | (void) define_variable ("BUILD_PLATFORM_CPU", sizeof("BUILD_PLATFORM_CPU") - 1,
|
---|
1019 | BUILD_PLATFORM_CPU, o_default, 0);
|
---|
1020 |
|
---|
1021 | # ifdef PATH_KBUILD
|
---|
1022 | /* define the installed. */
|
---|
1023 | envvar = getenv("PATH_KBUILD");
|
---|
1024 | if (!envvar)
|
---|
1025 | {
|
---|
1026 | (void) define_variable ("PATH_KBUILD", sizeof("PATH_KBUILD") - 1,
|
---|
1027 | PATH_KBUILD, o_default, 0);
|
---|
1028 | envvar = getenv("PATH_KBUILD_BIN");
|
---|
1029 | if (!envvar)
|
---|
1030 | (void) define_variable ("PATH_KBUILD_BIN", sizeof("PATH_KBUILD_BIN") - 1,
|
---|
1031 | PATH_KBUILD_BIN, o_default, 0);
|
---|
1032 | }
|
---|
1033 | # endif
|
---|
1034 |
|
---|
1035 |
|
---|
1036 | /* Define KMK_FEATURES to indicate various working KMK features. */
|
---|
1037 | # if defined(CONFIG_WITH_TOUPPER_TOLOWER) \
|
---|
1038 | && defined(CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE) \
|
---|
1039 | && defined(KMK_HELPERS)
|
---|
1040 | (void) define_variable ("KMK_FEATURES", 12,
|
---|
1041 | "abspath"
|
---|
1042 | " toupper tolower"
|
---|
1043 | " comp-vars"
|
---|
1044 | " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one"
|
---|
1045 | , o_default, 0);
|
---|
1046 | # else /* MSC can't deal with strings mixed with #if/#endif, thus the slow way. */
|
---|
1047 | strcpy(buf, "abspath");
|
---|
1048 | # if defined(CONFIG_WITH_TOUPPER_TOLOWER)
|
---|
1049 | strcat(buf, " toupper tolower");
|
---|
1050 | # endif
|
---|
1051 | # if defined(CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
|
---|
1052 | strcat(buf, " comp-vars");
|
---|
1053 | # endif
|
---|
1054 | # ifdef KMK_HELPERS
|
---|
1055 | strcat(buf, " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one");
|
---|
1056 | # endif
|
---|
1057 | (void) define_variable ("KMK_FEATURES", 12, buf, o_default, 0);
|
---|
1058 | # endif
|
---|
1059 |
|
---|
1060 | #endif /* KMK */
|
---|
1061 |
|
---|
1062 | #ifdef CONFIG_WITH_KMK_BUILTIN
|
---|
1063 | /* The supported kMk Builtin commands. */
|
---|
1064 | (void) define_variable ("KMK_BUILTIN", 11, "append cp cat echo install ln mkdir rm rmdir", o_default, 0);
|
---|
1065 | #endif
|
---|
1066 |
|
---|
1067 | #ifdef __MSDOS__
|
---|
1068 | /* Allow to specify a special shell just for Make,
|
---|
1069 | and use $COMSPEC as the default $SHELL when appropriate. */
|
---|
1070 | {
|
---|
1071 | static char shell_str[] = "SHELL";
|
---|
1072 | const int shlen = sizeof (shell_str) - 1;
|
---|
1073 | struct variable *mshp = lookup_variable ("MAKESHELL", 9);
|
---|
1074 | struct variable *comp = lookup_variable ("COMSPEC", 7);
|
---|
1075 |
|
---|
1076 | /* Make $MAKESHELL override $SHELL even if -e is in effect. */
|
---|
1077 | if (mshp)
|
---|
1078 | (void) define_variable (shell_str, shlen,
|
---|
1079 | mshp->value, o_env_override, 0);
|
---|
1080 | else if (comp)
|
---|
1081 | {
|
---|
1082 | /* $COMSPEC shouldn't override $SHELL. */
|
---|
1083 | struct variable *shp = lookup_variable (shell_str, shlen);
|
---|
1084 |
|
---|
1085 | if (!shp)
|
---|
1086 | (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
|
---|
1087 | }
|
---|
1088 | }
|
---|
1089 | #elif defined(__EMX__)
|
---|
1090 | {
|
---|
1091 | static char shell_str[] = "SHELL";
|
---|
1092 | const int shlen = sizeof (shell_str) - 1;
|
---|
1093 | struct variable *shell = lookup_variable (shell_str, shlen);
|
---|
1094 | struct variable *replace = lookup_variable ("MAKESHELL", 9);
|
---|
1095 |
|
---|
1096 | /* if $MAKESHELL is defined in the environment assume o_env_override */
|
---|
1097 | if (replace && *replace->value && replace->origin == o_env)
|
---|
1098 | replace->origin = o_env_override;
|
---|
1099 |
|
---|
1100 | /* if $MAKESHELL is not defined use $SHELL but only if the variable
|
---|
1101 | did not come from the environment */
|
---|
1102 | if (!replace || !*replace->value)
|
---|
1103 | if (shell && *shell->value && (shell->origin == o_env
|
---|
1104 | || shell->origin == o_env_override))
|
---|
1105 | {
|
---|
1106 | /* overwrite whatever we got from the environment */
|
---|
1107 | free(shell->value);
|
---|
1108 | shell->value = xstrdup (default_shell);
|
---|
1109 | shell->origin = o_default;
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | /* Some people do not like cmd to be used as the default
|
---|
1113 | if $SHELL is not defined in the Makefile.
|
---|
1114 | With -DNO_CMD_DEFAULT you can turn off this behaviour */
|
---|
1115 | # ifndef NO_CMD_DEFAULT
|
---|
1116 | /* otherwise use $COMSPEC */
|
---|
1117 | if (!replace || !*replace->value)
|
---|
1118 | replace = lookup_variable ("COMSPEC", 7);
|
---|
1119 |
|
---|
1120 | /* otherwise use $OS2_SHELL */
|
---|
1121 | if (!replace || !*replace->value)
|
---|
1122 | replace = lookup_variable ("OS2_SHELL", 9);
|
---|
1123 | # else
|
---|
1124 | # warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
|
---|
1125 | # endif
|
---|
1126 |
|
---|
1127 | if (replace && *replace->value)
|
---|
1128 | /* overwrite $SHELL */
|
---|
1129 | (void) define_variable (shell_str, shlen, replace->value,
|
---|
1130 | replace->origin, 0);
|
---|
1131 | else
|
---|
1132 | /* provide a definition if there is none */
|
---|
1133 | (void) define_variable (shell_str, shlen, default_shell,
|
---|
1134 | o_default, 0);
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | #endif
|
---|
1138 |
|
---|
1139 | /* This won't override any definition, but it will provide one if there
|
---|
1140 | isn't one there. */
|
---|
1141 | v = define_variable ("SHELL", 5, default_shell, o_default, 0);
|
---|
1142 |
|
---|
1143 | /* On MSDOS we do use SHELL from environment, since it isn't a standard
|
---|
1144 | environment variable on MSDOS, so whoever sets it, does that on purpose.
|
---|
1145 | On OS/2 we do not use SHELL from environment but we have already handled
|
---|
1146 | that problem above. */
|
---|
1147 | #if !defined(__MSDOS__) && !defined(__EMX__)
|
---|
1148 | /* Don't let SHELL come from the environment. */
|
---|
1149 | if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
|
---|
1150 | {
|
---|
1151 | free (v->value);
|
---|
1152 | v->origin = o_file;
|
---|
1153 | v->value = xstrdup (default_shell);
|
---|
1154 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
1155 | v->value_length = strlen (v->value);
|
---|
1156 | v->value_alloc_len = v->value_length + 1;
|
---|
1157 | #endif
|
---|
1158 | }
|
---|
1159 | #endif
|
---|
1160 |
|
---|
1161 | /* Make sure MAKEFILES gets exported if it is set. */
|
---|
1162 | v = define_variable ("MAKEFILES", 9, "", o_default, 0);
|
---|
1163 | v->export = v_ifset;
|
---|
1164 |
|
---|
1165 | /* Define the magic D and F variables in terms of
|
---|
1166 | the automatic variables they are variations of. */
|
---|
1167 |
|
---|
1168 | #ifdef VMS
|
---|
1169 | define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
|
---|
1170 | define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
|
---|
1171 | define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
|
---|
1172 | define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
|
---|
1173 | define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
|
---|
1174 | define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
|
---|
1175 | define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
|
---|
1176 | #else
|
---|
1177 | define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
|
---|
1178 | define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
|
---|
1179 | define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
|
---|
1180 | define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
|
---|
1181 | define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
|
---|
1182 | define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
|
---|
1183 | define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
|
---|
1184 | #endif
|
---|
1185 | define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
|
---|
1186 | define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
|
---|
1187 | define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
|
---|
1188 | define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
|
---|
1189 | define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
|
---|
1190 | define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
|
---|
1191 | define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
|
---|
1192 | }
|
---|
1193 | |
---|
1194 |
|
---|
1195 | int export_all_variables;
|
---|
1196 |
|
---|
1197 | /* Create a new environment for FILE's commands.
|
---|
1198 | If FILE is nil, this is for the `shell' function.
|
---|
1199 | The child's MAKELEVEL variable is incremented. */
|
---|
1200 |
|
---|
1201 | char **
|
---|
1202 | target_environment (struct file *file)
|
---|
1203 | {
|
---|
1204 | struct variable_set_list *set_list;
|
---|
1205 | register struct variable_set_list *s;
|
---|
1206 | struct hash_table table;
|
---|
1207 | struct variable **v_slot;
|
---|
1208 | struct variable **v_end;
|
---|
1209 | struct variable makelevel_key;
|
---|
1210 | char **result_0;
|
---|
1211 | char **result;
|
---|
1212 |
|
---|
1213 | if (file == 0)
|
---|
1214 | set_list = current_variable_set_list;
|
---|
1215 | else
|
---|
1216 | set_list = file->variables;
|
---|
1217 |
|
---|
1218 | hash_init (&table, VARIABLE_BUCKETS,
|
---|
1219 | variable_hash_1, variable_hash_2, variable_hash_cmp);
|
---|
1220 |
|
---|
1221 | /* Run through all the variable sets in the list,
|
---|
1222 | accumulating variables in TABLE. */
|
---|
1223 | for (s = set_list; s != 0; s = s->next)
|
---|
1224 | {
|
---|
1225 | struct variable_set *set = s->set;
|
---|
1226 | v_slot = (struct variable **) set->table.ht_vec;
|
---|
1227 | v_end = v_slot + set->table.ht_size;
|
---|
1228 | for ( ; v_slot < v_end; v_slot++)
|
---|
1229 | if (! HASH_VACANT (*v_slot))
|
---|
1230 | {
|
---|
1231 | struct variable **new_slot;
|
---|
1232 | struct variable *v = *v_slot;
|
---|
1233 |
|
---|
1234 | /* If this is a per-target variable and it hasn't been touched
|
---|
1235 | already then look up the global version and take its export
|
---|
1236 | value. */
|
---|
1237 | if (v->per_target && v->export == v_default)
|
---|
1238 | {
|
---|
1239 | struct variable *gv;
|
---|
1240 |
|
---|
1241 | gv = lookup_variable_in_set (v->name, strlen(v->name),
|
---|
1242 | &global_variable_set);
|
---|
1243 | if (gv)
|
---|
1244 | v->export = gv->export;
|
---|
1245 | }
|
---|
1246 |
|
---|
1247 | switch (v->export)
|
---|
1248 | {
|
---|
1249 | case v_default:
|
---|
1250 | if (v->origin == o_default || v->origin == o_automatic)
|
---|
1251 | /* Only export default variables by explicit request. */
|
---|
1252 | continue;
|
---|
1253 |
|
---|
1254 | /* The variable doesn't have a name that can be exported. */
|
---|
1255 | if (! v->exportable)
|
---|
1256 | continue;
|
---|
1257 |
|
---|
1258 | if (! export_all_variables
|
---|
1259 | && v->origin != o_command
|
---|
1260 | && v->origin != o_env && v->origin != o_env_override)
|
---|
1261 | continue;
|
---|
1262 | break;
|
---|
1263 |
|
---|
1264 | case v_export:
|
---|
1265 | break;
|
---|
1266 |
|
---|
1267 | case v_noexport:
|
---|
1268 | /* If this is the SHELL variable and it's not exported, then
|
---|
1269 | add the value from our original environment. */
|
---|
1270 | if (streq (v->name, "SHELL"))
|
---|
1271 | {
|
---|
1272 | extern struct variable shell_var;
|
---|
1273 | v = &shell_var;
|
---|
1274 | break;
|
---|
1275 | }
|
---|
1276 | continue;
|
---|
1277 |
|
---|
1278 | case v_ifset:
|
---|
1279 | if (v->origin == o_default)
|
---|
1280 | continue;
|
---|
1281 | break;
|
---|
1282 | }
|
---|
1283 |
|
---|
1284 | new_slot = (struct variable **) hash_find_slot (&table, v);
|
---|
1285 | if (HASH_VACANT (*new_slot))
|
---|
1286 | hash_insert_at (&table, v, new_slot);
|
---|
1287 | }
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | makelevel_key.name = MAKELEVEL_NAME;
|
---|
1291 | makelevel_key.length = MAKELEVEL_LENGTH;
|
---|
1292 | #ifdef VARIABLE_HASH /* bird */
|
---|
1293 | makelevel_key.hash1 = variable_hash_1i (MAKELEVEL_NAME, MAKELEVEL_LENGTH);
|
---|
1294 | makelevel_key.hash2 = 0;
|
---|
1295 | #endif
|
---|
1296 | hash_delete (&table, &makelevel_key);
|
---|
1297 |
|
---|
1298 | result = result_0 = (char **) xmalloc ((table.ht_fill + 2) * sizeof (char *));
|
---|
1299 |
|
---|
1300 | v_slot = (struct variable **) table.ht_vec;
|
---|
1301 | v_end = v_slot + table.ht_size;
|
---|
1302 | for ( ; v_slot < v_end; v_slot++)
|
---|
1303 | if (! HASH_VACANT (*v_slot))
|
---|
1304 | {
|
---|
1305 | struct variable *v = *v_slot;
|
---|
1306 |
|
---|
1307 | /* If V is recursively expanded and didn't come from the environment,
|
---|
1308 | expand its value. If it came from the environment, it should
|
---|
1309 | go back into the environment unchanged. */
|
---|
1310 | if (v->recursive
|
---|
1311 | && v->origin != o_env && v->origin != o_env_override)
|
---|
1312 | {
|
---|
1313 | char *value = recursively_expand_for_file (v, file);
|
---|
1314 | #ifdef WINDOWS32
|
---|
1315 | if (strcmp(v->name, "Path") == 0 ||
|
---|
1316 | strcmp(v->name, "PATH") == 0)
|
---|
1317 | convert_Path_to_windows32(value, ';');
|
---|
1318 | #endif
|
---|
1319 | *result++ = concat (v->name, "=", value);
|
---|
1320 | free (value);
|
---|
1321 | }
|
---|
1322 | else
|
---|
1323 | {
|
---|
1324 | #ifdef WINDOWS32
|
---|
1325 | if (strcmp(v->name, "Path") == 0 ||
|
---|
1326 | strcmp(v->name, "PATH") == 0)
|
---|
1327 | convert_Path_to_windows32(v->value, ';');
|
---|
1328 | #endif
|
---|
1329 | *result++ = concat (v->name, "=", v->value);
|
---|
1330 | }
|
---|
1331 | }
|
---|
1332 |
|
---|
1333 | *result = (char *) xmalloc (100);
|
---|
1334 | (void) sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
|
---|
1335 | *++result = 0;
|
---|
1336 |
|
---|
1337 | hash_free (&table, 0);
|
---|
1338 |
|
---|
1339 | return result_0;
|
---|
1340 | }
|
---|
1341 | |
---|
1342 |
|
---|
1343 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
1344 | static struct variable *
|
---|
1345 | do_variable_definition_append (const struct floc *flocp, struct variable *v, char *value,
|
---|
1346 | enum variable_origin origin)
|
---|
1347 | {
|
---|
1348 | if (env_overrides && origin == o_env)
|
---|
1349 | origin = o_env_override;
|
---|
1350 |
|
---|
1351 | if (env_overrides && v->origin == o_env)
|
---|
1352 | /* V came from in the environment. Since it was defined
|
---|
1353 | before the switches were parsed, it wasn't affected by -e. */
|
---|
1354 | v->origin = o_env_override;
|
---|
1355 |
|
---|
1356 | /* A variable of this name is already defined.
|
---|
1357 | If the old definition is from a stronger source
|
---|
1358 | than this one, don't redefine it. */
|
---|
1359 | if ((int) origin < (int) v->origin)
|
---|
1360 | return v;
|
---|
1361 | v->origin = origin;
|
---|
1362 |
|
---|
1363 | /* location */
|
---|
1364 | if (flocp != 0)
|
---|
1365 | v->fileinfo = *flocp;
|
---|
1366 |
|
---|
1367 | /* The juicy bits, append the specified value to the variable
|
---|
1368 | This is a heavily exercied code path in kBuild. */
|
---|
1369 | if (v->recursive)
|
---|
1370 | {
|
---|
1371 | /* The previous definition of the variable was recursive.
|
---|
1372 | The new value is the unexpanded old and new values. */
|
---|
1373 | unsigned int value_len = strlen (value);
|
---|
1374 | unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
|
---|
1375 |
|
---|
1376 | /* adjust the size. */
|
---|
1377 | if ((unsigned)v->value_alloc_len <= new_value_len)
|
---|
1378 | {
|
---|
1379 | v->value_alloc_len = (new_value_len + 0x80) + 0x7f;
|
---|
1380 | v->value = xrealloc (v->value, v->value_alloc_len);
|
---|
1381 | }
|
---|
1382 |
|
---|
1383 | /* insert the new bits */
|
---|
1384 | if (v->value_length != 0)
|
---|
1385 | {
|
---|
1386 | v->value[v->value_length] = ' ';
|
---|
1387 | memcpy (&v->value[v->value_length + 1], value, value_len + 1);
|
---|
1388 | }
|
---|
1389 | else
|
---|
1390 | memcpy (v->value, value, value_len + 1);
|
---|
1391 | v->value_length = new_value_len;
|
---|
1392 | }
|
---|
1393 | else
|
---|
1394 | {
|
---|
1395 | /* The previous definition of the variable was simple.
|
---|
1396 | The new value comes from the old value, which was expanded
|
---|
1397 | when it was set; and from the expanded new value. */
|
---|
1398 | append_expanded_string_to_variable(v, value);
|
---|
1399 | }
|
---|
1400 |
|
---|
1401 | /* update the variable */
|
---|
1402 | return v;
|
---|
1403 | }
|
---|
1404 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
1405 | |
---|
1406 |
|
---|
1407 | /* Given a variable, a value, and a flavor, define the variable.
|
---|
1408 | See the try_variable_definition() function for details on the parameters. */
|
---|
1409 |
|
---|
1410 | struct variable *
|
---|
1411 | do_variable_definition (const struct floc *flocp, const char *varname,
|
---|
1412 | char *value, enum variable_origin origin,
|
---|
1413 | enum variable_flavor flavor, int target_var)
|
---|
1414 | {
|
---|
1415 | char *p, *alloc_value = NULL;
|
---|
1416 | struct variable *v;
|
---|
1417 | int append = 0;
|
---|
1418 | int conditional = 0;
|
---|
1419 | const size_t varname_len = strlen (varname); /* bird */
|
---|
1420 | # ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
1421 | unsigned int value_len = ~0U;
|
---|
1422 | # endif
|
---|
1423 |
|
---|
1424 | /* Calculate the variable's new value in VALUE. */
|
---|
1425 |
|
---|
1426 | switch (flavor)
|
---|
1427 | {
|
---|
1428 | default:
|
---|
1429 | case f_bogus:
|
---|
1430 | /* Should not be possible. */
|
---|
1431 | abort ();
|
---|
1432 | case f_simple:
|
---|
1433 | /* A simple variable definition "var := value". Expand the value.
|
---|
1434 | We have to allocate memory since otherwise it'll clobber the
|
---|
1435 | variable buffer, and we may still need that if we're looking at a
|
---|
1436 | target-specific variable. */
|
---|
1437 | p = alloc_value = allocated_variable_expand (value);
|
---|
1438 | break;
|
---|
1439 | case f_conditional:
|
---|
1440 | /* A conditional variable definition "var ?= value".
|
---|
1441 | The value is set IFF the variable is not defined yet. */
|
---|
1442 | v = lookup_variable (varname, varname_len);
|
---|
1443 | if (v)
|
---|
1444 | return v;
|
---|
1445 |
|
---|
1446 | conditional = 1;
|
---|
1447 | flavor = f_recursive;
|
---|
1448 | /* FALLTHROUGH */
|
---|
1449 | case f_recursive:
|
---|
1450 | /* A recursive variable definition "var = value".
|
---|
1451 | The value is used verbatim. */
|
---|
1452 | p = value;
|
---|
1453 | break;
|
---|
1454 | case f_append:
|
---|
1455 | {
|
---|
1456 | /* If we have += but we're in a target variable context, we want to
|
---|
1457 | append only with other variables in the context of this target. */
|
---|
1458 | if (target_var)
|
---|
1459 | {
|
---|
1460 | append = 1;
|
---|
1461 | v = lookup_variable_in_set (varname, varname_len,
|
---|
1462 | current_variable_set_list->set);
|
---|
1463 |
|
---|
1464 | /* Don't append from the global set if a previous non-appending
|
---|
1465 | target-specific variable definition exists. */
|
---|
1466 | if (v && !v->append)
|
---|
1467 | append = 0;
|
---|
1468 | }
|
---|
1469 | else
|
---|
1470 | v = lookup_variable (varname, varname_len);
|
---|
1471 |
|
---|
1472 | if (v == 0)
|
---|
1473 | {
|
---|
1474 | /* There was no old value.
|
---|
1475 | This becomes a normal recursive definition. */
|
---|
1476 | p = value;
|
---|
1477 | flavor = f_recursive;
|
---|
1478 | }
|
---|
1479 | else
|
---|
1480 | {
|
---|
1481 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
1482 | v->append = append;
|
---|
1483 | return do_variable_definition_append (flocp, v, value, origin);
|
---|
1484 | #else /* !CONFIG_WITH_VALUE_LENGTH */
|
---|
1485 |
|
---|
1486 | /* Paste the old and new values together in VALUE. */
|
---|
1487 |
|
---|
1488 | unsigned int oldlen, vallen;
|
---|
1489 | char *val;
|
---|
1490 |
|
---|
1491 | val = value;
|
---|
1492 | if (v->recursive)
|
---|
1493 | /* The previous definition of the variable was recursive.
|
---|
1494 | The new value is the unexpanded old and new values. */
|
---|
1495 | flavor = f_recursive;
|
---|
1496 | else
|
---|
1497 | /* The previous definition of the variable was simple.
|
---|
1498 | The new value comes from the old value, which was expanded
|
---|
1499 | when it was set; and from the expanded new value. Allocate
|
---|
1500 | memory for the expansion as we may still need the rest of the
|
---|
1501 | buffer if we're looking at a target-specific variable. */
|
---|
1502 | val = alloc_value = allocated_variable_expand (val);
|
---|
1503 |
|
---|
1504 | oldlen = strlen (v->value);
|
---|
1505 | vallen = strlen (val);
|
---|
1506 | p = (char *) alloca (oldlen + 1 + vallen + 1);
|
---|
1507 | bcopy (v->value, p, oldlen);
|
---|
1508 | p[oldlen] = ' ';
|
---|
1509 | bcopy (val, &p[oldlen + 1], vallen + 1);
|
---|
1510 | #endif /* !CONFIG_WITH_VALUE_LENGTH */
|
---|
1511 | }
|
---|
1512 | }
|
---|
1513 | }
|
---|
1514 |
|
---|
1515 | #ifdef __MSDOS__
|
---|
1516 | /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
|
---|
1517 | non-Unix systems don't conform to this default configuration (in
|
---|
1518 | fact, most of them don't even have `/bin'). On the other hand,
|
---|
1519 | $SHELL in the environment, if set, points to the real pathname of
|
---|
1520 | the shell.
|
---|
1521 | Therefore, we generally won't let lines like "SHELL=/bin/sh" from
|
---|
1522 | the Makefile override $SHELL from the environment. But first, we
|
---|
1523 | look for the basename of the shell in the directory where SHELL=
|
---|
1524 | points, and along the $PATH; if it is found in any of these places,
|
---|
1525 | we define $SHELL to be the actual pathname of the shell. Thus, if
|
---|
1526 | you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
|
---|
1527 | your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
|
---|
1528 | defining SHELL to be "d:/unix/bash.exe". */
|
---|
1529 | if ((origin == o_file || origin == o_override)
|
---|
1530 | && strcmp (varname, "SHELL") == 0)
|
---|
1531 | {
|
---|
1532 | PATH_VAR (shellpath);
|
---|
1533 | extern char * __dosexec_find_on_path (const char *, char *[], char *);
|
---|
1534 |
|
---|
1535 | /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
|
---|
1536 | if (__dosexec_find_on_path (p, (char **)0, shellpath))
|
---|
1537 | {
|
---|
1538 | char *p;
|
---|
1539 |
|
---|
1540 | for (p = shellpath; *p; p++)
|
---|
1541 | {
|
---|
1542 | if (*p == '\\')
|
---|
1543 | *p = '/';
|
---|
1544 | }
|
---|
1545 | v = define_variable_loc (varname, varname_len,
|
---|
1546 | shellpath, origin, flavor == f_recursive,
|
---|
1547 | flocp);
|
---|
1548 | }
|
---|
1549 | else
|
---|
1550 | {
|
---|
1551 | char *shellbase, *bslash;
|
---|
1552 | struct variable *pathv = lookup_variable ("PATH", 4);
|
---|
1553 | char *path_string;
|
---|
1554 | char *fake_env[2];
|
---|
1555 | size_t pathlen = 0;
|
---|
1556 |
|
---|
1557 | shellbase = strrchr (p, '/');
|
---|
1558 | bslash = strrchr (p, '\\');
|
---|
1559 | if (!shellbase || bslash > shellbase)
|
---|
1560 | shellbase = bslash;
|
---|
1561 | if (!shellbase && p[1] == ':')
|
---|
1562 | shellbase = p + 1;
|
---|
1563 | if (shellbase)
|
---|
1564 | shellbase++;
|
---|
1565 | else
|
---|
1566 | shellbase = p;
|
---|
1567 |
|
---|
1568 | /* Search for the basename of the shell (with standard
|
---|
1569 | executable extensions) along the $PATH. */
|
---|
1570 | if (pathv)
|
---|
1571 | pathlen = strlen (pathv->value);
|
---|
1572 | path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
|
---|
1573 | /* On MSDOS, current directory is considered as part of $PATH. */
|
---|
1574 | sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
|
---|
1575 | fake_env[0] = path_string;
|
---|
1576 | fake_env[1] = (char *)0;
|
---|
1577 | if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
|
---|
1578 | {
|
---|
1579 | char *p;
|
---|
1580 |
|
---|
1581 | for (p = shellpath; *p; p++)
|
---|
1582 | {
|
---|
1583 | if (*p == '\\')
|
---|
1584 | *p = '/';
|
---|
1585 | }
|
---|
1586 | v = define_variable_loc (varname, varname_len,
|
---|
1587 | shellpath, origin,
|
---|
1588 | flavor == f_recursive, flocp);
|
---|
1589 | }
|
---|
1590 | else
|
---|
1591 | v = lookup_variable (varname, varname_len);
|
---|
1592 |
|
---|
1593 | free (path_string);
|
---|
1594 | }
|
---|
1595 | }
|
---|
1596 | else
|
---|
1597 | #endif /* __MSDOS__ */
|
---|
1598 | #ifdef WINDOWS32
|
---|
1599 | if ( varname_len == sizeof("SHELL") - 1 /* bird */
|
---|
1600 | && (origin == o_file || origin == o_override || origin == o_command)
|
---|
1601 | && streq (varname, "SHELL"))
|
---|
1602 | {
|
---|
1603 | extern char *default_shell;
|
---|
1604 |
|
---|
1605 | /* Call shell locator function. If it returns TRUE, then
|
---|
1606 | set no_default_sh_exe to indicate sh was found and
|
---|
1607 | set new value for SHELL variable. */
|
---|
1608 |
|
---|
1609 | if (find_and_set_default_shell (p))
|
---|
1610 | {
|
---|
1611 | v = define_variable_in_set (varname, varname_len, default_shell,
|
---|
1612 | # ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
1613 | ~0U, 1 /* duplicate_value */,
|
---|
1614 | # endif
|
---|
1615 | origin, flavor == f_recursive,
|
---|
1616 | (target_var
|
---|
1617 | ? current_variable_set_list->set
|
---|
1618 | : NULL),
|
---|
1619 | flocp);
|
---|
1620 | no_default_sh_exe = 0;
|
---|
1621 | }
|
---|
1622 | else
|
---|
1623 | v = lookup_variable (varname, varname_len);
|
---|
1624 | # ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
1625 | if (alloc_value)
|
---|
1626 | free (alloc_value);
|
---|
1627 | # endif
|
---|
1628 | }
|
---|
1629 | else
|
---|
1630 | #endif
|
---|
1631 |
|
---|
1632 | /* If we are defining variables inside an $(eval ...), we might have a
|
---|
1633 | different variable context pushed, not the global context (maybe we're
|
---|
1634 | inside a $(call ...) or something. Since this function is only ever
|
---|
1635 | invoked in places where we want to define globally visible variables,
|
---|
1636 | make sure we define this variable in the global set. */
|
---|
1637 |
|
---|
1638 | v = define_variable_in_set (varname, varname_len, p,
|
---|
1639 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
1640 | value_len, !alloc_value,
|
---|
1641 | #endif
|
---|
1642 | origin, flavor == f_recursive,
|
---|
1643 | (target_var
|
---|
1644 | ? current_variable_set_list->set : NULL),
|
---|
1645 | flocp);
|
---|
1646 | v->append = append;
|
---|
1647 | v->conditional = conditional;
|
---|
1648 |
|
---|
1649 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
1650 | if (alloc_value)
|
---|
1651 | free (alloc_value);
|
---|
1652 | #endif
|
---|
1653 |
|
---|
1654 | return v;
|
---|
1655 | }
|
---|
1656 | |
---|
1657 |
|
---|
1658 | /* Try to interpret LINE (a null-terminated string) as a variable definition.
|
---|
1659 |
|
---|
1660 | ORIGIN may be o_file, o_override, o_env, o_env_override,
|
---|
1661 | or o_command specifying that the variable definition comes
|
---|
1662 | from a makefile, an override directive, the environment with
|
---|
1663 | or without the -e switch, or the command line.
|
---|
1664 |
|
---|
1665 | See the comments for parse_variable_definition().
|
---|
1666 |
|
---|
1667 | If LINE was recognized as a variable definition, a pointer to its `struct
|
---|
1668 | variable' is returned. If LINE is not a variable definition, NULL is
|
---|
1669 | returned. */
|
---|
1670 |
|
---|
1671 | struct variable *
|
---|
1672 | parse_variable_definition (struct variable *v, char *line)
|
---|
1673 | {
|
---|
1674 | register int c;
|
---|
1675 | register char *p = line;
|
---|
1676 | register char *beg;
|
---|
1677 | register char *end;
|
---|
1678 | enum variable_flavor flavor = f_bogus;
|
---|
1679 | char *name;
|
---|
1680 |
|
---|
1681 | while (1)
|
---|
1682 | {
|
---|
1683 | c = *p++;
|
---|
1684 | if (c == '\0' || c == '#')
|
---|
1685 | return 0;
|
---|
1686 | if (c == '=')
|
---|
1687 | {
|
---|
1688 | end = p - 1;
|
---|
1689 | flavor = f_recursive;
|
---|
1690 | break;
|
---|
1691 | }
|
---|
1692 | else if (c == ':')
|
---|
1693 | if (*p == '=')
|
---|
1694 | {
|
---|
1695 | end = p++ - 1;
|
---|
1696 | flavor = f_simple;
|
---|
1697 | break;
|
---|
1698 | }
|
---|
1699 | else
|
---|
1700 | /* A colon other than := is a rule line, not a variable defn. */
|
---|
1701 | return 0;
|
---|
1702 | else if (c == '+' && *p == '=')
|
---|
1703 | {
|
---|
1704 | end = p++ - 1;
|
---|
1705 | flavor = f_append;
|
---|
1706 | break;
|
---|
1707 | }
|
---|
1708 | else if (c == '?' && *p == '=')
|
---|
1709 | {
|
---|
1710 | end = p++ - 1;
|
---|
1711 | flavor = f_conditional;
|
---|
1712 | break;
|
---|
1713 | }
|
---|
1714 | else if (c == '$')
|
---|
1715 | {
|
---|
1716 | /* This might begin a variable expansion reference. Make sure we
|
---|
1717 | don't misrecognize chars inside the reference as =, := or +=. */
|
---|
1718 | char closeparen;
|
---|
1719 | int count;
|
---|
1720 | c = *p++;
|
---|
1721 | if (c == '(')
|
---|
1722 | closeparen = ')';
|
---|
1723 | else if (c == '{')
|
---|
1724 | closeparen = '}';
|
---|
1725 | else
|
---|
1726 | continue; /* Nope. */
|
---|
1727 |
|
---|
1728 | /* P now points past the opening paren or brace.
|
---|
1729 | Count parens or braces until it is matched. */
|
---|
1730 | count = 0;
|
---|
1731 | for (; *p != '\0'; ++p)
|
---|
1732 | {
|
---|
1733 | if (*p == c)
|
---|
1734 | ++count;
|
---|
1735 | else if (*p == closeparen && --count < 0)
|
---|
1736 | {
|
---|
1737 | ++p;
|
---|
1738 | break;
|
---|
1739 | }
|
---|
1740 | }
|
---|
1741 | }
|
---|
1742 | }
|
---|
1743 | v->flavor = flavor;
|
---|
1744 |
|
---|
1745 | beg = next_token (line);
|
---|
1746 | while (end > beg && isblank ((unsigned char)end[-1]))
|
---|
1747 | --end;
|
---|
1748 | p = next_token (p);
|
---|
1749 | v->value = p;
|
---|
1750 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
1751 | v->value_length = v->value_alloc_len = -1;
|
---|
1752 | #endif
|
---|
1753 |
|
---|
1754 | /* Expand the name, so "$(foo)bar = baz" works. */
|
---|
1755 | name = (char *) alloca (end - beg + 1);
|
---|
1756 | bcopy (beg, name, end - beg);
|
---|
1757 | name[end - beg] = '\0';
|
---|
1758 | v->name = allocated_variable_expand (name);
|
---|
1759 |
|
---|
1760 | if (v->name[0] == '\0')
|
---|
1761 | fatal (&v->fileinfo, _("empty variable name"));
|
---|
1762 |
|
---|
1763 | return v;
|
---|
1764 | }
|
---|
1765 | |
---|
1766 |
|
---|
1767 | /* Try to interpret LINE (a null-terminated string) as a variable definition.
|
---|
1768 |
|
---|
1769 | ORIGIN may be o_file, o_override, o_env, o_env_override,
|
---|
1770 | or o_command specifying that the variable definition comes
|
---|
1771 | from a makefile, an override directive, the environment with
|
---|
1772 | or without the -e switch, or the command line.
|
---|
1773 |
|
---|
1774 | See the comments for parse_variable_definition().
|
---|
1775 |
|
---|
1776 | If LINE was recognized as a variable definition, a pointer to its `struct
|
---|
1777 | variable' is returned. If LINE is not a variable definition, NULL is
|
---|
1778 | returned. */
|
---|
1779 |
|
---|
1780 | struct variable *
|
---|
1781 | try_variable_definition (const struct floc *flocp, char *line,
|
---|
1782 | enum variable_origin origin, int target_var)
|
---|
1783 | {
|
---|
1784 | struct variable v;
|
---|
1785 | struct variable *vp;
|
---|
1786 |
|
---|
1787 | if (flocp != 0)
|
---|
1788 | v.fileinfo = *flocp;
|
---|
1789 | else
|
---|
1790 | v.fileinfo.filenm = 0;
|
---|
1791 |
|
---|
1792 | if (!parse_variable_definition (&v, line))
|
---|
1793 | return 0;
|
---|
1794 |
|
---|
1795 | vp = do_variable_definition (flocp, v.name, v.value,
|
---|
1796 | origin, v.flavor, target_var);
|
---|
1797 |
|
---|
1798 | free (v.name);
|
---|
1799 |
|
---|
1800 | return vp;
|
---|
1801 | }
|
---|
1802 | |
---|
1803 |
|
---|
1804 | /* Print information for variable V, prefixing it with PREFIX. */
|
---|
1805 |
|
---|
1806 | static void
|
---|
1807 | print_variable (const void *item, void *arg)
|
---|
1808 | {
|
---|
1809 | const struct variable *v = (struct variable *) item;
|
---|
1810 | const char *prefix = (char *) arg;
|
---|
1811 | const char *origin;
|
---|
1812 |
|
---|
1813 | switch (v->origin)
|
---|
1814 | {
|
---|
1815 | case o_default:
|
---|
1816 | origin = _("default");
|
---|
1817 | break;
|
---|
1818 | case o_env:
|
---|
1819 | origin = _("environment");
|
---|
1820 | break;
|
---|
1821 | case o_file:
|
---|
1822 | origin = _("makefile");
|
---|
1823 | break;
|
---|
1824 | case o_env_override:
|
---|
1825 | origin = _("environment under -e");
|
---|
1826 | break;
|
---|
1827 | case o_command:
|
---|
1828 | origin = _("command line");
|
---|
1829 | break;
|
---|
1830 | case o_override:
|
---|
1831 | origin = _("`override' directive");
|
---|
1832 | break;
|
---|
1833 | case o_automatic:
|
---|
1834 | origin = _("automatic");
|
---|
1835 | break;
|
---|
1836 | case o_invalid:
|
---|
1837 | default:
|
---|
1838 | abort ();
|
---|
1839 | }
|
---|
1840 | fputs ("# ", stdout);
|
---|
1841 | fputs (origin, stdout);
|
---|
1842 | if (v->fileinfo.filenm)
|
---|
1843 | printf (_(" (from `%s', line %lu)"),
|
---|
1844 | v->fileinfo.filenm, v->fileinfo.lineno);
|
---|
1845 | putchar ('\n');
|
---|
1846 | fputs (prefix, stdout);
|
---|
1847 |
|
---|
1848 | /* Is this a `define'? */
|
---|
1849 | if (v->recursive && strchr (v->value, '\n') != 0)
|
---|
1850 | printf ("define %s\n%s\nendef\n", v->name, v->value);
|
---|
1851 | else
|
---|
1852 | {
|
---|
1853 | register char *p;
|
---|
1854 |
|
---|
1855 | printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
|
---|
1856 |
|
---|
1857 | /* Check if the value is just whitespace. */
|
---|
1858 | p = next_token (v->value);
|
---|
1859 | if (p != v->value && *p == '\0')
|
---|
1860 | /* All whitespace. */
|
---|
1861 | printf ("$(subst ,,%s)", v->value);
|
---|
1862 | else if (v->recursive)
|
---|
1863 | fputs (v->value, stdout);
|
---|
1864 | else
|
---|
1865 | /* Double up dollar signs. */
|
---|
1866 | for (p = v->value; *p != '\0'; ++p)
|
---|
1867 | {
|
---|
1868 | if (*p == '$')
|
---|
1869 | putchar ('$');
|
---|
1870 | putchar (*p);
|
---|
1871 | }
|
---|
1872 | putchar ('\n');
|
---|
1873 | }
|
---|
1874 | }
|
---|
1875 |
|
---|
1876 |
|
---|
1877 | /* Print all the variables in SET. PREFIX is printed before
|
---|
1878 | the actual variable definitions (everything else is comments). */
|
---|
1879 |
|
---|
1880 | void
|
---|
1881 | print_variable_set (struct variable_set *set, char *prefix)
|
---|
1882 | {
|
---|
1883 | hash_map_arg (&set->table, print_variable, prefix);
|
---|
1884 |
|
---|
1885 | fputs (_("# variable set hash-table stats:\n"), stdout);
|
---|
1886 | fputs ("# ", stdout);
|
---|
1887 | hash_print_stats (&set->table, stdout);
|
---|
1888 | putc ('\n', stdout);
|
---|
1889 | }
|
---|
1890 |
|
---|
1891 | /* Print the data base of variables. */
|
---|
1892 |
|
---|
1893 | void
|
---|
1894 | print_variable_data_base (void)
|
---|
1895 | {
|
---|
1896 | puts (_("\n# Variables\n"));
|
---|
1897 |
|
---|
1898 | print_variable_set (&global_variable_set, "");
|
---|
1899 |
|
---|
1900 | puts (_("\n# Pattern-specific Variable Values"));
|
---|
1901 |
|
---|
1902 | {
|
---|
1903 | struct pattern_var *p;
|
---|
1904 | int rules = 0;
|
---|
1905 |
|
---|
1906 | for (p = pattern_vars; p != 0; p = p->next)
|
---|
1907 | {
|
---|
1908 | ++rules;
|
---|
1909 | printf ("\n%s :\n", p->target);
|
---|
1910 | print_variable (&p->variable, "# ");
|
---|
1911 | }
|
---|
1912 |
|
---|
1913 | if (rules == 0)
|
---|
1914 | puts (_("\n# No pattern-specific variable values."));
|
---|
1915 | else
|
---|
1916 | printf (_("\n# %u pattern-specific variable values"), rules);
|
---|
1917 | }
|
---|
1918 | }
|
---|
1919 |
|
---|
1920 |
|
---|
1921 | /* Print all the local variables of FILE. */
|
---|
1922 |
|
---|
1923 | void
|
---|
1924 | print_file_variables (struct file *file)
|
---|
1925 | {
|
---|
1926 | if (file->variables != 0)
|
---|
1927 | print_variable_set (file->variables->set, "# ");
|
---|
1928 | }
|
---|
1929 |
|
---|
1930 | #ifdef WINDOWS32
|
---|
1931 | void
|
---|
1932 | sync_Path_environment (void)
|
---|
1933 | {
|
---|
1934 | char *path = allocated_variable_expand ("$(PATH)");
|
---|
1935 | static char *environ_path = NULL;
|
---|
1936 |
|
---|
1937 | if (!path)
|
---|
1938 | return;
|
---|
1939 |
|
---|
1940 | /*
|
---|
1941 | * If done this before, don't leak memory unnecessarily.
|
---|
1942 | * Free the previous entry before allocating new one.
|
---|
1943 | */
|
---|
1944 | if (environ_path)
|
---|
1945 | free (environ_path);
|
---|
1946 |
|
---|
1947 | /*
|
---|
1948 | * Create something WINDOWS32 world can grok
|
---|
1949 | */
|
---|
1950 | convert_Path_to_windows32 (path, ';');
|
---|
1951 | environ_path = concat ("PATH", "=", path);
|
---|
1952 | putenv (environ_path);
|
---|
1953 | free (path);
|
---|
1954 | }
|
---|
1955 | #endif
|
---|