kmk: Added a new function $(defined var) which check whether var is defined and retur...
[kbuild-mirror.git] / src / kmk / variable.c
blobacddb128d39ea651e244bf20d14f0a84de05d5ab
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.
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.
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.
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. */
19 #include "make.h"
21 #include <assert.h>
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 #ifdef KMK
34 # include "kbuild.h"
35 #endif
37 /* Chain of all pattern-specific variables. */
39 static struct pattern_var *pattern_vars;
41 /* Pointer to last struct in the chain, so we can add onto the end. */
43 static struct pattern_var *last_pattern_var;
45 /* Create a new pattern-specific variable struct. */
47 struct pattern_var *
48 create_pattern_var (const char *target, const char *suffix)
50 register struct pattern_var *p = xmalloc (sizeof (struct pattern_var));
52 if (last_pattern_var != 0)
53 last_pattern_var->next = p;
54 else
55 pattern_vars = p;
56 last_pattern_var = p;
57 p->next = 0;
59 p->target = target;
60 p->len = strlen (target);
61 p->suffix = suffix + 1;
63 return p;
66 /* Look up a target in the pattern-specific variable list. */
68 static struct pattern_var *
69 lookup_pattern_var (struct pattern_var *start, const char *target)
71 struct pattern_var *p;
72 unsigned int targlen = strlen(target);
74 for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
76 const char *stem;
77 unsigned int stemlen;
79 if (p->len > targlen)
80 /* It can't possibly match. */
81 continue;
83 /* From the lengths of the filename and the pattern parts,
84 find the stem: the part of the filename that matches the %. */
85 stem = target + (p->suffix - p->target - 1);
86 stemlen = targlen - p->len + 1;
88 /* Compare the text in the pattern before the stem, if any. */
89 if (stem > target && !strneq (p->target, target, stem - target))
90 continue;
92 /* Compare the text in the pattern after the stem, if any.
93 We could test simply using streq, but this way we compare the
94 first two characters immediately. This saves time in the very
95 common case where the first character matches because it is a
96 period. */
97 if (*p->suffix == stem[stemlen]
98 && (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
99 break;
102 return p;
105 /* Hash table of all global variable definitions. */
107 #if defined(VARIABLE_HASH) || defined(CONFIG_WITH_OPTIMIZATION_HACKS)
108 # ifdef _MSC_VER
109 # define inline _inline
110 typedef signed int int32_t;
111 typedef signed short int int16_t;
112 # endif
113 static inline unsigned long variable_hash_2i(register const char *var, register int length)
115 # define UPDATE_HASH(ch) hash = (ch) + (hash << 6) + (hash << 16) - hash
116 # ifndef CONFIG_WITH_OPTIMIZATION_HACKS
117 # if 1
118 register const unsigned char *uvar = (const unsigned char *)var;
119 register unsigned long hash = 0;
120 while (length-- > 0)
121 UPDATE_HASH(*uvar++);
122 return hash;
123 # else
124 return_STRING_N_HASH_2 (var, length);
125 # endif
126 # else /* CONFIG_WITH_OPTIMIZATION_HACKS */
127 register unsigned long hash = 0;
128 register const unsigned char *uvar = (const unsigned char *)var;
129 register const unsigned char *uvar_end = uvar + length;
130 switch (length)
132 default:
133 case 32: //UPDATE_HASH(uvar_end[-16]);
134 case 31: UPDATE_HASH(uvar_end[-15]);
135 case 30: //UPDATE_HASH(uvar_end[-14]);
136 case 29: UPDATE_HASH(uvar_end[-13]);
137 case 28: //UPDATE_HASH(uvar_end[-12]);
138 case 27: UPDATE_HASH(uvar_end[-11]);
139 case 26: //UPDATE_HASH(uvar_end[-10]);
140 case 25: UPDATE_HASH(uvar_end[-9]);
141 case 24: //UPDATE_HASH(uvar[15]);
142 case 23: UPDATE_HASH(uvar[14]);
143 case 22: //UPDATE_HASH(uvar[13]);
144 case 21: UPDATE_HASH(uvar[12]);
145 case 20: //UPDATE_HASH(uvar[11]);
146 case 19: UPDATE_HASH(uvar[10]);
147 case 18: //UPDATE_HASH(uvar[9]);
148 case 17: UPDATE_HASH(uvar[8]);
149 case 16: //UPDATE_HASH(uvar_end[-8]);
150 case 15: UPDATE_HASH(uvar_end[-7]);
151 case 14: //UPDATE_HASH(uvar_end[-6]);
152 case 13: UPDATE_HASH(uvar_end[-5]);
153 case 12: //UPDATE_HASH(uvar_end[-4]);
154 case 11: UPDATE_HASH(uvar_end[-3]);
155 case 10: //UPDATE_HASH(uvar_end[-2]);
156 case 9: UPDATE_HASH(uvar_end[-1]);
157 case 8: //UPDATE_HASH(uvar[7]);
158 case 7: UPDATE_HASH(uvar[6]);
159 case 6: //UPDATE_HASH(uvar[5]);
160 case 5: UPDATE_HASH(uvar[4]);
161 case 4: //UPDATE_HASH(uvar[3]);
162 case 3: UPDATE_HASH(uvar[2]);
163 case 2: //UPDATE_HASH(uvar[1]);
164 case 1: UPDATE_HASH(uvar[0]);
165 case 0:
166 return hash;
168 # endif /* CONFIG_WITH_OPTIMIZATION_HACKS*/
169 # undef UPDATE_HASH
172 static inline unsigned long variable_hash_1i(register const char *var, register int length)
174 # define UPDATE_HASH(ch) hash = ((hash << 5) + hash) + (ch)
175 # ifndef CONFIG_WITH_OPTIMIZATION_HACKS
176 # if 1
177 register const unsigned char *uvar = (const unsigned char *)var;
178 register unsigned long hash = 5381;
179 while (length-- > 0)
180 UPDATE_HASH(*uvar++);
181 return hash;
182 # else
183 return_STRING_N_HASH_1 (var, length);
184 # endif
185 # else /* CONFIG_WITH_OPTIMIZATION_HACKS */
186 register const unsigned char *uvar = (const unsigned char *)var;
187 register const unsigned char *uvar_end = (const unsigned char *)var + length;
188 register unsigned long hash = ((5381 << 5) + 5381) + *uvar;
189 switch (length)
191 default:
192 #if 0 /* seems to be a waste of time. */
193 case 97: UPDATE_HASH(uvar_end[-77]);
194 case 96: //UPDATE_HASH(uvar_end[-76]);
195 case 95: //UPDATE_HASH(uvar_end[-75]);
196 case 94: //UPDATE_HASH(uvar_end[-74]);
197 case 93: UPDATE_HASH(uvar_end[-73]);
198 case 92: //UPDATE_HASH(uvar_end[-72]);
199 case 91: //UPDATE_HASH(uvar_end[-71]);
200 case 90: //UPDATE_HASH(uvar_end[-70]);
201 case 89: UPDATE_HASH(uvar_end[-69]);
202 case 88: //UPDATE_HASH(uvar_end[-68]);
203 case 87: //UPDATE_HASH(uvar_end[-67]);
204 case 86: //UPDATE_HASH(uvar_end[-66]);
205 case 85: UPDATE_HASH(uvar_end[-65]);
206 case 84: //UPDATE_HASH(uvar_end[-64]);
207 case 83: //UPDATE_HASH(uvar_end[-63]);
208 case 82: //UPDATE_HASH(uvar_end[-62]);
209 case 81: UPDATE_HASH(uvar_end[-61]);
210 case 80: //UPDATE_HASH(uvar_end[-60]);
211 case 79: //UPDATE_HASH(uvar_end[-59]);
212 case 78: //UPDATE_HASH(uvar_end[-58]);
213 case 77: UPDATE_HASH(uvar_end[-57]);
214 case 76: //UPDATE_HASH(uvar_end[-56]);
215 case 75: //UPDATE_HASH(uvar_end[-55]);
216 case 74: //UPDATE_HASH(uvar_end[-54]);
217 case 73: UPDATE_HASH(uvar_end[-53]);
218 case 72: //UPDATE_HASH(uvar_end[-52]);
219 case 71: //UPDATE_HASH(uvar_end[-51]);
220 case 70: //UPDATE_HASH(uvar_end[-50]);
221 case 69: UPDATE_HASH(uvar_end[-49]);
222 case 68: //UPDATE_HASH(uvar_end[-48]);
223 case 67: //UPDATE_HASH(uvar_end[-47]);
224 case 66: //UPDATE_HASH(uvar_end[-46]);
225 case 65: UPDATE_HASH(uvar_end[-49]);
226 case 64: //UPDATE_HASH(uvar_end[-48]);
227 case 63: //UPDATE_HASH(uvar_end[-47]);
228 case 62: //UPDATE_HASH(uvar_end[-46]);
229 case 61: UPDATE_HASH(uvar_end[-45]);
230 case 60: //UPDATE_HASH(uvar_end[-44]);
231 case 59: //UPDATE_HASH(uvar_end[-43]);
232 case 58: //UPDATE_HASH(uvar_end[-42]);
233 case 57: UPDATE_HASH(uvar_end[-41]);
234 case 56: //UPDATE_HASH(uvar_end[-40]);
235 case 55: //UPDATE_HASH(uvar_end[-39]);
236 case 54: //UPDATE_HASH(uvar_end[-38]);
237 case 53: UPDATE_HASH(uvar_end[-37]);
238 case 52: //UPDATE_HASH(uvar_end[-36]);
239 case 51: UPDATE_HASH(uvar_end[-35]);
240 case 50: //UPDATE_HASH(uvar_end[-34]);
241 case 49: UPDATE_HASH(uvar_end[-33]);
242 #endif
243 case 48: //UPDATE_HASH(uvar_end[-32]);
244 case 47: UPDATE_HASH(uvar_end[-31]);
245 case 46: //UPDATE_HASH(uvar_end[-30]);
246 case 45: UPDATE_HASH(uvar_end[-29]);
247 case 44: //UPDATE_HASH(uvar_end[-28]);
248 case 43: UPDATE_HASH(uvar_end[-27]);
249 case 42: //UPDATE_HASH(uvar_end[-26]);
250 case 41: UPDATE_HASH(uvar_end[-25]);
251 case 40: //UPDATE_HASH(uvar_end[-24]);
252 case 39: UPDATE_HASH(uvar_end[-23]);
253 case 38: //UPDATE_HASH(uvar_end[-22]);
254 case 37: UPDATE_HASH(uvar_end[-21]);
255 case 36: //UPDATE_HASH(uvar_end[-20]);
256 case 35: UPDATE_HASH(uvar_end[-19]);
257 case 34: //UPDATE_HASH(uvar_end[-18]);
258 case 33: UPDATE_HASH(uvar_end[-17]);
260 case 32: UPDATE_HASH(uvar_end[-16]);
261 case 31: UPDATE_HASH(uvar_end[-15]);
262 case 30: UPDATE_HASH(uvar_end[-14]);
263 case 29: UPDATE_HASH(uvar_end[-13]);
264 case 28: UPDATE_HASH(uvar[15]);
265 case 27: UPDATE_HASH(uvar[14]);
266 case 26: UPDATE_HASH(uvar[13]);
267 case 25: UPDATE_HASH(uvar[12]);
269 case 24: UPDATE_HASH(uvar_end[-12]);
270 case 23: UPDATE_HASH(uvar_end[-11]);
271 case 22: UPDATE_HASH(uvar_end[-10]);
272 case 21: UPDATE_HASH(uvar_end[-9]);
273 case 20: UPDATE_HASH(uvar[7]);
274 case 19: UPDATE_HASH(uvar[6]);
275 case 18: UPDATE_HASH(uvar[5]);
276 case 17: UPDATE_HASH(uvar[4]);
278 case 16: UPDATE_HASH(uvar_end[-8]);
279 case 15: UPDATE_HASH(uvar_end[-7]);
280 case 14: UPDATE_HASH(uvar_end[-6]);
281 case 13: UPDATE_HASH(uvar_end[-5]);
282 case 12: UPDATE_HASH(uvar[11]);
283 case 11: UPDATE_HASH(uvar[10]);
284 case 10: UPDATE_HASH(uvar[9]);
285 case 9: UPDATE_HASH(uvar[8]);
287 case 8: UPDATE_HASH(uvar_end[-4]);
288 case 7: UPDATE_HASH(uvar_end[-3]);
289 case 6: UPDATE_HASH(uvar_end[-2]);
290 case 5: UPDATE_HASH(uvar_end[-1]);
291 case 4: UPDATE_HASH(uvar[3]);
292 case 3: UPDATE_HASH(uvar[2]);
293 case 2: UPDATE_HASH(uvar[1]);
294 case 1: return hash;
295 case 0: return 5381; /* shouldn't happen */
297 # endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
298 # undef UPDATE_HASH
300 #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
302 static unsigned long
303 variable_hash_1 (const void *keyv)
305 struct variable const *key = (struct variable const *) keyv;
306 #ifdef VARIABLE_HASH /* bird */
307 # ifdef VARIABLE_HASH_STRICT
308 if (key->hash1 != variable_hash_1i (key->name, key->length))
309 __asm__("int3");
310 if (key->hash2 && key->hash2 != variable_hash_2i (key->name, key->length))
311 __asm__("int3");
312 # endif
313 return key->hash1;
314 #else
315 # ifdef CONFIG_WITH_OPTIMIZATION_HACKS
316 return variable_hash_1i (key->name, key->length);
317 # else
318 return_STRING_N_HASH_1 (key->name, key->length);
319 # endif
320 #endif
323 static unsigned long
324 variable_hash_2 (const void *keyv)
326 #ifdef VARIABLE_HASH /* bird */
327 struct variable *key = (struct variable *) keyv;
328 if (!key->hash2)
329 key->hash2 = variable_hash_2i (key->name, key->length);
330 return key->hash2;
331 #else
332 struct variable const *key = (struct variable const *) keyv;
333 # ifdef CONFIG_WITH_OPTIMIZATION_HACKS
334 return variable_hash_2i (key->name, key->length);
335 # else
336 return_STRING_N_HASH_2 (key->name, key->length);
337 # endif
338 #endif
341 static int
342 variable_hash_cmp (const void *xv, const void *yv)
344 struct variable const *x = (struct variable const *) xv;
345 struct variable const *y = (struct variable const *) yv;
346 int result = x->length - y->length;
347 if (result)
348 return result;
349 #ifdef VARIABLE_HASH
350 #ifdef VARIABLE_HASH_STRICT /* bird */
351 if (x->hash1 != variable_hash_1i (x->name, x->length))
352 __asm__("int3");
353 if (x->hash2 && x->hash2 != variable_hash_2i (x->name, x->length))
354 __asm__("int3");
355 if (y->hash1 != variable_hash_1i (y->name, y->length))
356 __asm__("int3");
357 if (y->hash2 && y->hash2 != variable_hash_2i (y->name, y->length))
358 __asm__("int3");
359 #endif
360 /* hash 1 */
361 result = x->hash1 - y->hash1;
362 if (result)
363 return result;
364 #endif
365 #ifdef CONFIG_WITH_OPTIMIZATION_HACKS /* bird: speed */
367 const char *xs = x->name;
368 const char *ys = y->name;
369 switch (x->length)
371 case 8:
372 result = *(int32_t*)(xs + 4) - *(int32_t*)(ys + 4);
373 if (result)
374 return result;
375 return *(int32_t*)xs - *(int32_t*)ys;
376 case 7:
377 result = xs[6] - ys[6];
378 if (result)
379 return result;
380 case 6:
381 result = *(int32_t*)xs - *(int32_t*)ys;
382 if (result)
383 return result;
384 return *(int16_t*)(xs + 4) - *(int16_t*)(ys + 4);
385 case 5:
386 result = xs[4] - ys[4];
387 if (result)
388 return result;
389 case 4:
390 return *(int32_t*)xs - *(int32_t*)ys;
391 case 3:
392 result = xs[2] - ys[2];
393 if (result)
394 return result;
395 case 2:
396 return *(int16_t*)xs - *(int16_t*)ys;
397 case 1:
398 return *xs - *ys;
399 case 0:
400 return 0;
403 #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
404 #ifdef VARIABLE_HASH
405 /* hash 2 */
406 if (!x->hash2)
407 ((struct variable *)x)->hash2 = variable_hash_2i (x->name, x->length);
408 if (!y->hash2)
409 ((struct variable *)y)->hash2 = variable_hash_2i (y->name, y->length);
410 result = x->hash2 - y->hash2;
411 if (result)
412 return result;
413 #endif
414 #ifdef CONFIG_WITH_OPTIMIZATION_HACKS
415 return memcmp (x->name, y->name, x->length);
416 #else
417 return_STRING_N_COMPARE (x->name, y->name, x->length);
418 #endif
421 #ifndef VARIABLE_BUCKETS
422 # ifdef KMK /* Move to Makefile.kmk? */
423 # define VARIABLE_BUCKETS 16384
424 # else /*!KMK*/
425 #define VARIABLE_BUCKETS 523
426 # endif /*!KMK*/
427 #endif
428 #ifndef PERFILE_VARIABLE_BUCKETS
429 # ifdef KMK /* Move to Makefile.kmk? */
430 # define PERFILE_VARIABLE_BUCKETS 64
431 # else
432 #define PERFILE_VARIABLE_BUCKETS 23
433 # endif
434 #endif
435 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
436 # ifdef KMK /* Move to Makefile.kmk? */
437 # define SMALL_SCOPE_VARIABLE_BUCKETS 32
438 # else
439 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
440 # endif
441 #endif
443 static struct variable_set global_variable_set;
444 static struct variable_set_list global_setlist
445 = { 0, &global_variable_set };
446 struct variable_set_list *current_variable_set_list = &global_setlist;
448 /* Implement variables. */
450 void
451 init_hash_global_variable_set (void)
453 hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
454 variable_hash_1, variable_hash_2, variable_hash_cmp);
457 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
458 LENGTH is the length of NAME, which does not need to be null-terminated.
459 ORIGIN specifies the origin of the variable (makefile, command line
460 or environment).
461 If RECURSIVE is nonzero a flag is set in the variable saying
462 that it should be recursively re-expanded. */
464 #ifdef CONFIG_WITH_VALUE_LENGTH
465 struct variable *
466 define_variable_in_set (const char *name, unsigned int length,
467 const char *value, unsigned int value_length, int duplicate_value,
468 enum variable_origin origin, int recursive,
469 struct variable_set *set, const struct floc *flocp)
470 #else
471 struct variable *
472 define_variable_in_set (const char *name, unsigned int length,
473 const char *value, enum variable_origin origin,
474 int recursive, struct variable_set *set,
475 const struct floc *flocp)
476 #endif
478 struct variable *v;
479 struct variable **var_slot;
480 struct variable var_key;
482 if (set == NULL)
483 set = &global_variable_set;
485 var_key.name = (char *) name;
486 var_key.length = length;
487 #ifdef VARIABLE_HASH /* bird */
488 var_key.hash1 = variable_hash_1i (name, length);
489 var_key.hash2 = 0;
490 #endif
491 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
493 if (env_overrides && origin == o_env)
494 origin = o_env_override;
496 v = *var_slot;
497 if (! HASH_VACANT (v))
499 if (env_overrides && v->origin == o_env)
500 /* V came from in the environment. Since it was defined
501 before the switches were parsed, it wasn't affected by -e. */
502 v->origin = o_env_override;
504 /* A variable of this name is already defined.
505 If the old definition is from a stronger source
506 than this one, don't redefine it. */
507 if ((int) origin >= (int) v->origin)
509 #ifdef CONFIG_WITH_VALUE_LENGTH
510 if (value_length == ~0U)
511 value_length = strlen (value);
512 else
513 assert (value_length == strlen (value));
514 if (!duplicate_value)
516 if (v->value != 0)
517 free (v->value);
518 v->value = (char *)value;
519 v->value_alloc_len = value_length + 1;
521 else
523 if ((unsigned int)v->value_alloc_len <= value_length)
525 free (v->value);
526 v->value_alloc_len = (value_length + 0x40) & ~0x3f;
527 v->value = xmalloc (v->value_alloc_len);
529 memcpy (v->value, value, value_length + 1);
531 v->value_length = value_length;
532 #else
533 if (v->value != 0)
534 free (v->value);
535 v->value = xstrdup (value);
536 #endif
537 if (flocp != 0)
538 v->fileinfo = *flocp;
539 else
540 v->fileinfo.filenm = 0;
541 v->origin = origin;
542 v->recursive = recursive;
544 return v;
547 /* Create a new variable definition and add it to the hash table. */
549 v = xmalloc (sizeof (struct variable));
550 v->name = savestring (name, length);
551 v->length = length;
552 #ifdef VARIABLE_HASH /* bird */
553 v->hash1 = variable_hash_1i (name, length);
554 v->hash2 = 0;
555 #endif
556 hash_insert_at (&set->table, v, var_slot);
557 #ifdef CONFIG_WITH_VALUE_LENGTH
558 if (value_length == ~0U)
559 value_length = strlen (value);
560 else
561 assert (value_length == strlen (value));
562 v->value_length = value_length;
563 if (!duplicate_value)
565 v->value_alloc_len = value_length + 1;
566 v->value = (char *)value;
568 else
570 v->value_alloc_len = (value_length + 32) & ~31;
571 v->value = xmalloc (v->value_alloc_len);
572 memcpy (v->value, value, value_length + 1);
574 #else
575 v->value = xstrdup (value);
576 #endif
577 if (flocp != 0)
578 v->fileinfo = *flocp;
579 else
580 v->fileinfo.filenm = 0;
581 v->origin = origin;
582 v->recursive = recursive;
583 v->special = 0;
584 v->expanding = 0;
585 v->exp_count = 0;
586 v->per_target = 0;
587 v->append = 0;
588 v->export = v_default;
590 v->exportable = 1;
591 if (*name != '_' && (*name < 'A' || *name > 'Z')
592 && (*name < 'a' || *name > 'z'))
593 v->exportable = 0;
594 else
596 for (++name; *name != '\0'; ++name)
597 if (*name != '_' && (*name < 'a' || *name > 'z')
598 && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
599 break;
601 if (*name != '\0')
602 v->exportable = 0;
605 return v;
608 /* If the variable passed in is "special", handle its special nature.
609 Currently there are two such variables, both used for introspection:
610 .VARIABLES expands to a list of all the variables defined in this instance
611 of make.
612 .TARGETS expands to a list of all the targets defined in this
613 instance of make.
614 Returns the variable reference passed in. */
616 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
618 static struct variable *
619 handle_special_var (struct variable *var)
621 static unsigned long last_var_count = 0;
624 /* This one actually turns out to be very hard, due to the way the parser
625 records targets. The way it works is that target information is collected
626 internally until make knows the target is completely specified. It unitl
627 it sees that some new construct (a new target or variable) is defined that
628 it knows the previous one is done. In short, this means that if you do
629 this:
631 all:
633 TARGS := $(.TARGETS)
635 then $(TARGS) won't contain "all", because it's not until after the
636 variable is created that the previous target is completed.
638 Changing this would be a major pain. I think a less complex way to do it
639 would be to pre-define the target files as soon as the first line is
640 parsed, then come back and do the rest of the definition as now. That
641 would allow $(.TARGETS) to be correct without a major change to the way
642 the parser works.
644 if (streq (var->name, ".TARGETS"))
645 var->value = build_target_list (var->value);
646 else
649 if (streq (var->name, ".VARIABLES")
650 && global_variable_set.table.ht_fill != last_var_count)
652 unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
653 unsigned long len;
654 char *p;
655 struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
656 struct variable **end = &vp[global_variable_set.table.ht_size];
658 /* Make sure we have at least MAX bytes in the allocated buffer. */
659 var->value = xrealloc (var->value, max);
661 /* Walk through the hash of variables, constructing a list of names. */
662 p = var->value;
663 len = 0;
664 for (; vp < end; ++vp)
665 if (!HASH_VACANT (*vp))
667 struct variable *v = *vp;
668 int l = v->length;
670 len += l + 1;
671 if (len > max)
673 unsigned long off = p - var->value;
675 max += EXPANSION_INCREMENT (l + 1);
676 var->value = xrealloc (var->value, max);
677 p = &var->value[off];
680 memcpy (p, v->name, l);
681 p += l;
682 *(p++) = ' ';
684 *(p-1) = '\0';
686 /* Remember how many variables are in our current count. Since we never
687 remove variables from the list, this is a reliable way to know whether
688 the list is up to date or needs to be recomputed. */
690 last_var_count = global_variable_set.table.ht_fill;
693 return var;
697 /* Lookup a variable whose name is a string starting at NAME
698 and with LENGTH chars. NAME need not be null-terminated.
699 Returns address of the `struct variable' containing all info
700 on the variable, or nil if no such variable is defined. */
702 struct variable *
703 lookup_variable (const char *name, unsigned int length)
705 const struct variable_set_list *setlist;
706 struct variable var_key;
708 var_key.name = (char *) name;
709 var_key.length = length;
710 #ifdef VARIABLE_HASH /* bird */
711 var_key.hash1 = variable_hash_1i (name, length);
712 var_key.hash2 = 0;
713 #endif
715 for (setlist = current_variable_set_list;
716 setlist != 0; setlist = setlist->next)
718 #ifdef VARIABLE_HASH /* bird: speed */
719 struct hash_table *ht = &setlist->set->table;
720 unsigned int hash_1 = var_key.hash1;
721 struct variable *v;
723 ht->ht_lookups++;
724 for (;;)
726 hash_1 &= (ht->ht_size - 1);
727 v = (struct variable *)ht->ht_vec[hash_1];
729 if (v == 0)
730 break;
731 if ((void *)v != hash_deleted_item)
733 if (variable_hash_cmp(&var_key, v) == 0)
735 # ifdef VARIABLE_HASH_STRICT /* bird */
736 struct variable *v2 = (struct variable *) hash_find_item ((struct hash_table *) &setlist->set->table, &var_key);
737 assert(v2 == v);
738 # endif
739 return v->special ? handle_special_var (v) : v;
741 ht->ht_collisions++;
743 if (!var_key.hash2)
744 var_key.hash2 = variable_hash_2i(name, length);
745 hash_1 += (var_key.hash2 | 1);
748 #else /* !VARIABLE_HASH */
749 const struct variable_set *set = setlist->set;
750 struct variable *v;
752 v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
753 if (v)
754 return v->special ? handle_special_var (v) : v;
755 #endif /* !VARIABLE_HASH */
758 #ifdef VMS
759 /* since we don't read envp[] on startup, try to get the
760 variable via getenv() here. */
762 char *vname = alloca (length + 1);
763 char *value;
764 strncpy (vname, name, length);
765 vname[length] = 0;
766 value = getenv (vname);
767 if (value != 0)
769 char *sptr;
770 int scnt;
772 sptr = value;
773 scnt = 0;
775 while ((sptr = strchr (sptr, '$')))
777 scnt++;
778 sptr++;
781 if (scnt > 0)
783 char *nvalue;
784 char *nptr;
786 nvalue = alloca (strlen (value) + scnt + 1);
787 sptr = value;
788 nptr = nvalue;
790 while (*sptr)
792 if (*sptr == '$')
794 *nptr++ = '$';
795 *nptr++ = '$';
797 else
799 *nptr++ = *sptr;
801 sptr++;
804 *nptr = '\0';
805 return define_variable (vname, length, nvalue, o_env, 1);
809 return define_variable (vname, length, value, o_env, 1);
812 #endif /* VMS */
814 return 0;
817 /* Lookup a variable whose name is a string starting at NAME
818 and with LENGTH chars in set SET. NAME need not be null-terminated.
819 Returns address of the `struct variable' containing all info
820 on the variable, or nil if no such variable is defined. */
822 struct variable *
823 lookup_variable_in_set (const char *name, unsigned int length,
824 const struct variable_set *set)
826 struct variable var_key;
828 var_key.name = (char *) name;
829 var_key.length = length;
830 #ifdef VARIABLE_HASH /* bird */
831 var_key.hash1 = variable_hash_1i (name, length);
832 var_key.hash2 = 0;
833 #endif
835 return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
838 /* Initialize FILE's variable set list. If FILE already has a variable set
839 list, the topmost variable set is left intact, but the the rest of the
840 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
841 rule, then we will use the "root" double-colon target's variable set as the
842 parent of FILE's variable set.
844 If we're READING a makefile, don't do the pattern variable search now,
845 since the pattern variable might not have been defined yet. */
847 void
848 initialize_file_variables (struct file *file, int reading)
850 struct variable_set_list *l = file->variables;
852 if (l == 0)
854 l = (struct variable_set_list *)
855 xmalloc (sizeof (struct variable_set_list));
856 l->set = xmalloc (sizeof (struct variable_set));
857 hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
858 variable_hash_1, variable_hash_2, variable_hash_cmp);
859 file->variables = l;
862 /* If this is a double-colon, then our "parent" is the "root" target for
863 this double-colon rule. Since that rule has the same name, parent,
864 etc. we can just use its variables as the "next" for ours. */
866 if (file->double_colon && file->double_colon != file)
868 initialize_file_variables (file->double_colon, reading);
869 l->next = file->double_colon->variables;
870 return;
873 if (file->parent == 0)
874 l->next = &global_setlist;
875 else
877 initialize_file_variables (file->parent, reading);
878 l->next = file->parent->variables;
881 /* If we're not reading makefiles and we haven't looked yet, see if
882 we can find pattern variables for this target. */
884 if (!reading && !file->pat_searched)
886 struct pattern_var *p;
888 p = lookup_pattern_var (0, file->name);
889 if (p != 0)
891 struct variable_set_list *global = current_variable_set_list;
893 /* We found at least one. Set up a new variable set to accumulate
894 all the pattern variables that match this target. */
896 file->pat_variables = create_new_variable_set ();
897 current_variable_set_list = file->pat_variables;
901 /* We found one, so insert it into the set. */
903 struct variable *v;
905 if (p->variable.flavor == f_simple)
907 v = define_variable_loc (
908 p->variable.name, strlen (p->variable.name),
909 p->variable.value, p->variable.origin,
910 0, &p->variable.fileinfo);
912 v->flavor = f_simple;
914 else
916 v = do_variable_definition (
917 &p->variable.fileinfo, p->variable.name,
918 p->variable.value, p->variable.origin,
919 p->variable.flavor, 1);
922 /* Also mark it as a per-target and copy export status. */
923 v->per_target = p->variable.per_target;
924 v->export = p->variable.export;
926 while ((p = lookup_pattern_var (p, file->name)) != 0);
928 current_variable_set_list = global;
930 file->pat_searched = 1;
933 /* If we have a pattern variable match, set it up. */
935 if (file->pat_variables != 0)
937 file->pat_variables->next = l->next;
938 l->next = file->pat_variables;
942 /* Pop the top set off the current variable set list,
943 and free all its storage. */
945 struct variable_set_list *
946 create_new_variable_set (void)
948 register struct variable_set_list *setlist;
949 register struct variable_set *set;
951 set = xmalloc (sizeof (struct variable_set));
952 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
953 variable_hash_1, variable_hash_2, variable_hash_cmp);
955 setlist = (struct variable_set_list *)
956 xmalloc (sizeof (struct variable_set_list));
957 setlist->set = set;
958 setlist->next = current_variable_set_list;
960 return setlist;
963 static void
964 free_variable_name_and_value (const void *item)
966 struct variable *v = (struct variable *) item;
967 free (v->name);
968 free (v->value);
971 void
972 free_variable_set (struct variable_set_list *list)
974 hash_map (&list->set->table, free_variable_name_and_value);
975 hash_free (&list->set->table, 1);
976 free (list->set);
977 free (list);
980 /* Create a new variable set and push it on the current setlist.
981 If we're pushing a global scope (that is, the current scope is the global
982 scope) then we need to "push" it the other way: file variable sets point
983 directly to the global_setlist so we need to replace that with the new one.
986 struct variable_set_list *
987 push_new_variable_scope (void)
989 current_variable_set_list = create_new_variable_set();
990 if (current_variable_set_list->next == &global_setlist)
992 /* It was the global, so instead of new -> &global we want to replace
993 &global with the new one and have &global -> new, with current still
994 pointing to &global */
995 struct variable_set *set = current_variable_set_list->set;
996 current_variable_set_list->set = global_setlist.set;
997 global_setlist.set = set;
998 current_variable_set_list->next = global_setlist.next;
999 global_setlist.next = current_variable_set_list;
1000 current_variable_set_list = &global_setlist;
1002 return (current_variable_set_list);
1005 void
1006 pop_variable_scope (void)
1008 struct variable_set_list *setlist;
1009 struct variable_set *set;
1011 /* Can't call this if there's no scope to pop! */
1012 assert(current_variable_set_list->next != NULL);
1014 if (current_variable_set_list != &global_setlist)
1016 /* We're not pointing to the global setlist, so pop this one. */
1017 setlist = current_variable_set_list;
1018 set = setlist->set;
1019 current_variable_set_list = setlist->next;
1021 else
1023 /* This set is the one in the global_setlist, but there is another global
1024 set beyond that. We want to copy that set to global_setlist, then
1025 delete what used to be in global_setlist. */
1026 setlist = global_setlist.next;
1027 set = global_setlist.set;
1028 global_setlist.set = setlist->set;
1029 global_setlist.next = setlist->next;
1032 /* Free the one we no longer need. */
1033 free (setlist);
1034 hash_map (&set->table, free_variable_name_and_value);
1035 hash_free (&set->table, 1);
1036 free (set);
1039 /* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
1041 static void
1042 merge_variable_sets (struct variable_set *to_set,
1043 struct variable_set *from_set)
1045 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
1046 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
1048 for ( ; from_var_slot < from_var_end; from_var_slot++)
1049 if (! HASH_VACANT (*from_var_slot))
1051 struct variable *from_var = *from_var_slot;
1052 struct variable **to_var_slot
1053 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
1054 if (HASH_VACANT (*to_var_slot))
1055 hash_insert_at (&to_set->table, from_var, to_var_slot);
1056 else
1058 /* GKM FIXME: delete in from_set->table */
1059 free (from_var->value);
1060 free (from_var);
1065 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
1067 void
1068 merge_variable_set_lists (struct variable_set_list **setlist0,
1069 struct variable_set_list *setlist1)
1071 struct variable_set_list *to = *setlist0;
1072 struct variable_set_list *last0 = 0;
1074 /* If there's nothing to merge, stop now. */
1075 if (!setlist1)
1076 return;
1078 /* This loop relies on the fact that all setlists terminate with the global
1079 setlist (before NULL). If that's not true, arguably we SHOULD die. */
1080 if (to)
1081 while (setlist1 != &global_setlist && to != &global_setlist)
1083 struct variable_set_list *from = setlist1;
1084 setlist1 = setlist1->next;
1086 merge_variable_sets (to->set, from->set);
1088 last0 = to;
1089 to = to->next;
1092 if (setlist1 != &global_setlist)
1094 if (last0 == 0)
1095 *setlist0 = setlist1;
1096 else
1097 last0->next = setlist1;
1101 /* Define the automatic variables, and record the addresses
1102 of their structures so we can change their values quickly. */
1104 void
1105 define_automatic_variables (void)
1107 #if defined(WINDOWS32) || defined(__EMX__)
1108 extern char* default_shell;
1109 #else
1110 extern char default_shell[];
1111 #endif
1112 register struct variable *v;
1113 #ifndef KMK
1114 char buf[200];
1115 #else
1116 char buf[1024];
1117 const char *val;
1118 struct variable *envvar1;
1119 struct variable *envvar2;
1120 #endif
1122 sprintf (buf, "%u", makelevel);
1123 (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
1125 sprintf (buf, "%s%s%s",
1126 version_string,
1127 (remote_description == 0 || remote_description[0] == '\0')
1128 ? "" : "-",
1129 (remote_description == 0 || remote_description[0] == '\0')
1130 ? "" : remote_description);
1131 #ifndef KMK
1132 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
1133 #else /* KMK */
1135 /* Define KMK_VERSION to indicate kMk. */
1136 (void) define_variable ("KMK_VERSION", 11, buf, o_default, 0);
1138 /* Define KBUILD_VERSION* */
1139 sprintf (buf, "%d", KBUILD_VERSION_MAJOR);
1140 define_variable ("KBUILD_VERSION_MAJOR", sizeof ("KBUILD_VERSION_MAJOR") - 1,
1141 buf, o_default, 0);
1142 sprintf (buf, "%d", KBUILD_VERSION_MINOR);
1143 define_variable ("KBUILD_VERSION_MINOR", sizeof("KBUILD_VERSION_MINOR") - 1,
1144 buf, o_default, 0);
1145 sprintf (buf, "%d", KBUILD_VERSION_PATCH);
1146 define_variable ("KBUILD_VERSION_PATCH", sizeof ("KBUILD_VERSION_PATCH") - 1,
1147 buf, o_default, 0);
1148 sprintf (buf, "%d", KBUILD_SVN_REV);
1149 define_variable ("KBUILD_KMK_REVISION", sizeof ("KBUILD_KMK_REVISION") - 1,
1150 buf, o_default, 0);
1152 sprintf (buf, "%d.%d.%d-r%d", KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR,
1153 KBUILD_VERSION_PATCH, KBUILD_SVN_REV);
1154 define_variable ("KBUILD_VERSION", sizeof ("KBUILD_VERSION") - 1,
1155 buf, o_default, 0);
1157 /* The host defaults. The BUILD_* stuff will be replaced by KBUILD_* soon. */
1158 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST"));
1159 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM"));
1160 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST;
1161 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1162 error (NULL, _("KBUILD_HOST and BUILD_PLATFORM differs, using KBUILD_HOST=%s."), val);
1163 if (!envvar1)
1164 define_variable ("KBUILD_HOST", sizeof ("KBUILD_HOST") - 1,
1165 val, o_default, 0);
1166 if (!envvar2)
1167 define_variable ("BUILD_PLATFORM", sizeof ("BUILD_PLATFORM") - 1,
1168 val, o_default, 0);
1170 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_ARCH"));
1171 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_ARCH"));
1172 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_ARCH;
1173 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1174 error (NULL, _("KBUILD_HOST_ARCH and BUILD_PLATFORM_ARCH differs, using KBUILD_HOST_ARCH=%s."), val);
1175 if (!envvar1)
1176 define_variable ("KBUILD_HOST_ARCH", sizeof ("KBUILD_HOST_ARCH") - 1,
1177 val, o_default, 0);
1178 if (!envvar2)
1179 define_variable ("BUILD_PLATFORM_ARCH", sizeof ("BUILD_PLATFORM_ARCH") - 1,
1180 val, o_default, 0);
1182 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_CPU"));
1183 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_CPU"));
1184 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_CPU;
1185 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1186 error (NULL, _("KBUILD_HOST_CPU and BUILD_PLATFORM_CPU differs, using KBUILD_HOST_CPU=%s."), val);
1187 if (!envvar1)
1188 define_variable ("KBUILD_HOST_CPU", sizeof ("KBUILD_HOST_CPU") - 1,
1189 val, o_default, 0);
1190 if (!envvar2)
1191 define_variable ("BUILD_PLATFORM_CPU", sizeof ("BUILD_PLATFORM_CPU") - 1,
1192 val, o_default, 0);
1194 /* The kBuild locations. */
1195 define_variable ("KBUILD_PATH", sizeof ("KBUILD_PATH") - 1,
1196 get_kbuild_path (), o_default, 0);
1197 define_variable ("KBUILD_BIN_PATH", sizeof ("KBUILD_BIN_PATH") - 1,
1198 get_kbuild_bin_path (), o_default, 0);
1200 define_variable ("PATH_KBUILD", sizeof ("PATH_KBUILD") - 1,
1201 get_kbuild_path (), o_default, 0);
1202 define_variable ("PATH_KBUILD_BIN", sizeof ("PATH_KBUILD_BIN") - 1,
1203 get_kbuild_bin_path (), o_default, 0);
1205 /* Define KMK_FEATURES to indicate various working KMK features. */
1206 # if defined (CONFIG_WITH_RSORT) \
1207 && defined (CONFIG_WITH_ABSPATHEX) \
1208 && defined (CONFIG_WITH_TOUPPER_TOLOWER) \
1209 && defined (CONFIG_WITH_DEFINED) \
1210 && defined (CONFIG_WITH_VALUE_LENGTH) && defined (CONFIG_WITH_COMPARE) \
1211 && defined (CONFIG_WITH_STACK) \
1212 && defined (CONFIG_WITH_MATH) \
1213 && defined (CONFIG_WITH_XARGS) \
1214 && defined (CONFIG_WITH_EXPLICIT_MULTITARGET) \
1215 && defined (CONFIG_WITH_PREPEND_ASSIGNMENT) \
1216 && defined (CONFIG_WITH_SET_CONDITIONALS) \
1217 && defined (CONFIG_WITH_DATE) \
1218 && defined (CONFIG_WITH_FILE_SIZE) \
1219 && defined (CONFIG_WITH_WHICH) \
1220 && defined (CONFIG_WITH_EVALPLUS) \
1221 && defined (CONFIG_WITH_MAKE_STATS) \
1222 && defined (CONFIG_WITH_COMMANDS_FUNC) \
1223 && defined (KMK_HELPERS)
1224 (void) define_variable ("KMK_FEATURES", 12,
1225 "append-dash-n abspath"
1226 " rsort"
1227 " abspathex"
1228 " toupper tolower"
1229 " defined"
1230 " comp-vars comp-cmds comp-cmds-ex"
1231 " stack"
1232 " math-int"
1233 " xargs"
1234 " explicit-multitarget"
1235 " prepend-assignment"
1236 " set-conditionals"
1237 " date"
1238 " file-size"
1239 " which"
1240 " evalctx evalval evalvalctx evalcall evalcall2"
1241 " make-stats"
1242 " commands"
1243 " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one "
1244 , o_default, 0);
1245 # else /* MSC can't deal with strings mixed with #if/#endif, thus the slow way. */
1246 # error "All features should be enabled by default!"
1247 strcpy (buf, "append-dash-n abspath");
1248 # if defined (CONFIG_WITH_RSORT)
1249 strcat (buf, " rsort");
1250 # endif
1251 # if defined (CONFIG_WITH_ABSPATHEX)
1252 strcat (buf, " abspathex");
1253 # endif
1254 # if defined (CONFIG_WITH_TOUPPER_TOLOWER)
1255 strcat (buf, " toupper tolower");
1256 # endif
1257 # if defined (CONFIG_WITH_DEFINED)
1258 strcat (buf, " defined");
1259 # endif
1260 # if defined (CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
1261 strcat (buf, " comp-vars comp-cmds comp-cmds-ex");
1262 # endif
1263 # if defined (CONFIG_WITH_STACK)
1264 strcat (buf, " stack");
1265 # endif
1266 # if defined (CONFIG_WITH_MATH)
1267 strcat (buf, " math-int");
1268 # endif
1269 # if defined (CONFIG_WITH_XARGS)
1270 strcat (buf, " xargs");
1271 # endif
1272 # if defined (CONFIG_WITH_EXPLICIT_MULTITARGET)
1273 strcat (buf, " explicit-multitarget");
1274 # endif
1275 # if defined (CONFIG_WITH_PREPEND_ASSIGNMENT)
1276 strcat (buf, " prepend-assignment");
1277 # endif
1278 # if defined (CONFIG_WITH_SET_CONDITIONALS)
1279 strcat (buf, " set-conditionals");
1280 # endif
1281 # if defined (CONFIG_WITH_DATE)
1282 strcat (buf, " date");
1283 # endif
1284 # if defined (CONFIG_WITH_FILE_SIZE)
1285 strcat (buf, " file-size");
1286 # endif
1287 # if defined (CONFIG_WITH_WHICH)
1288 strcat (buf, " which");
1289 # endif
1290 # if defined (CONFIG_WITH_EVALPLUS)
1291 strcat (buf, " evalctx evalval evalvalctx evalcall evalcall2");
1292 # endif
1293 # if defined (CONFIG_WITH_MAKE_STATS)
1294 strcat (buf, " make-stats");
1295 # endif
1296 # if defined (CONFIG_WITH_COMMANDS_FUNC)
1297 strcat (buf, " commands");
1298 # endif
1299 # if defined (KMK_HELPERS)
1300 strcat (buf, " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one");
1301 # endif
1302 (void) define_variable ("KMK_FEATURES", 12, buf, o_default, 0);
1303 # endif
1305 #endif /* KMK */
1307 #ifdef CONFIG_WITH_KMK_BUILTIN
1308 /* The supported kMk Builtin commands. */
1309 (void) define_variable ("KMK_BUILTIN", 11, "append cat chmod cp cmp echo expr install kDepIDB ln md5sum mkdir mv printf rm rmdir test", o_default, 0);
1310 #endif
1312 #ifdef __MSDOS__
1313 /* Allow to specify a special shell just for Make,
1314 and use $COMSPEC as the default $SHELL when appropriate. */
1316 static char shell_str[] = "SHELL";
1317 const int shlen = sizeof (shell_str) - 1;
1318 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
1319 struct variable *comp = lookup_variable ("COMSPEC", 7);
1321 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
1322 if (mshp)
1323 (void) define_variable (shell_str, shlen,
1324 mshp->value, o_env_override, 0);
1325 else if (comp)
1327 /* $COMSPEC shouldn't override $SHELL. */
1328 struct variable *shp = lookup_variable (shell_str, shlen);
1330 if (!shp)
1331 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
1334 #elif defined(__EMX__)
1336 static char shell_str[] = "SHELL";
1337 const int shlen = sizeof (shell_str) - 1;
1338 struct variable *shell = lookup_variable (shell_str, shlen);
1339 struct variable *replace = lookup_variable ("MAKESHELL", 9);
1341 /* if $MAKESHELL is defined in the environment assume o_env_override */
1342 if (replace && *replace->value && replace->origin == o_env)
1343 replace->origin = o_env_override;
1345 /* if $MAKESHELL is not defined use $SHELL but only if the variable
1346 did not come from the environment */
1347 if (!replace || !*replace->value)
1348 if (shell && *shell->value && (shell->origin == o_env
1349 || shell->origin == o_env_override))
1351 /* overwrite whatever we got from the environment */
1352 free(shell->value);
1353 shell->value = xstrdup (default_shell);
1354 shell->origin = o_default;
1357 /* Some people do not like cmd to be used as the default
1358 if $SHELL is not defined in the Makefile.
1359 With -DNO_CMD_DEFAULT you can turn off this behaviour */
1360 # ifndef NO_CMD_DEFAULT
1361 /* otherwise use $COMSPEC */
1362 if (!replace || !*replace->value)
1363 replace = lookup_variable ("COMSPEC", 7);
1365 /* otherwise use $OS2_SHELL */
1366 if (!replace || !*replace->value)
1367 replace = lookup_variable ("OS2_SHELL", 9);
1368 # else
1369 # warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
1370 # endif
1372 if (replace && *replace->value)
1373 /* overwrite $SHELL */
1374 (void) define_variable (shell_str, shlen, replace->value,
1375 replace->origin, 0);
1376 else
1377 /* provide a definition if there is none */
1378 (void) define_variable (shell_str, shlen, default_shell,
1379 o_default, 0);
1382 #endif
1384 /* This won't override any definition, but it will provide one if there
1385 isn't one there. */
1386 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
1388 /* On MSDOS we do use SHELL from environment, since it isn't a standard
1389 environment variable on MSDOS, so whoever sets it, does that on purpose.
1390 On OS/2 we do not use SHELL from environment but we have already handled
1391 that problem above. */
1392 #if !defined(__MSDOS__) && !defined(__EMX__)
1393 /* Don't let SHELL come from the environment. */
1394 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
1396 free (v->value);
1397 v->origin = o_file;
1398 v->value = xstrdup (default_shell);
1399 #ifdef CONFIG_WITH_VALUE_LENGTH
1400 v->value_length = strlen (v->value);
1401 v->value_alloc_len = v->value_length + 1;
1402 #endif
1404 #endif
1406 /* Make sure MAKEFILES gets exported if it is set. */
1407 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
1408 v->export = v_ifset;
1410 /* Define the magic D and F variables in terms of
1411 the automatic variables they are variations of. */
1413 #ifdef VMS
1414 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
1415 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
1416 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
1417 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
1418 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
1419 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
1420 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
1421 #else
1422 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
1423 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
1424 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
1425 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
1426 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
1427 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
1428 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
1429 #endif
1430 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
1431 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
1432 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
1433 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
1434 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
1435 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
1436 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
1439 int export_all_variables;
1441 /* Create a new environment for FILE's commands.
1442 If FILE is nil, this is for the `shell' function.
1443 The child's MAKELEVEL variable is incremented. */
1445 char **
1446 target_environment (struct file *file)
1448 struct variable_set_list *set_list;
1449 register struct variable_set_list *s;
1450 struct hash_table table;
1451 struct variable **v_slot;
1452 struct variable **v_end;
1453 struct variable makelevel_key;
1454 char **result_0;
1455 char **result;
1457 if (file == 0)
1458 set_list = current_variable_set_list;
1459 else
1460 set_list = file->variables;
1462 hash_init (&table, VARIABLE_BUCKETS,
1463 variable_hash_1, variable_hash_2, variable_hash_cmp);
1465 /* Run through all the variable sets in the list,
1466 accumulating variables in TABLE. */
1467 for (s = set_list; s != 0; s = s->next)
1469 struct variable_set *set = s->set;
1470 v_slot = (struct variable **) set->table.ht_vec;
1471 v_end = v_slot + set->table.ht_size;
1472 for ( ; v_slot < v_end; v_slot++)
1473 if (! HASH_VACANT (*v_slot))
1475 struct variable **new_slot;
1476 struct variable *v = *v_slot;
1478 /* If this is a per-target variable and it hasn't been touched
1479 already then look up the global version and take its export
1480 value. */
1481 if (v->per_target && v->export == v_default)
1483 struct variable *gv;
1485 gv = lookup_variable_in_set (v->name, strlen(v->name),
1486 &global_variable_set);
1487 if (gv)
1488 v->export = gv->export;
1491 switch (v->export)
1493 case v_default:
1494 if (v->origin == o_default || v->origin == o_automatic)
1495 /* Only export default variables by explicit request. */
1496 continue;
1498 /* The variable doesn't have a name that can be exported. */
1499 if (! v->exportable)
1500 continue;
1502 if (! export_all_variables
1503 && v->origin != o_command
1504 && v->origin != o_env && v->origin != o_env_override)
1505 continue;
1506 break;
1508 case v_export:
1509 break;
1511 case v_noexport:
1512 /* If this is the SHELL variable and it's not exported, then
1513 add the value from our original environment. */
1514 if (streq (v->name, "SHELL"))
1516 extern struct variable shell_var;
1517 v = &shell_var;
1518 break;
1520 continue;
1522 case v_ifset:
1523 if (v->origin == o_default)
1524 continue;
1525 break;
1528 new_slot = (struct variable **) hash_find_slot (&table, v);
1529 if (HASH_VACANT (*new_slot))
1530 hash_insert_at (&table, v, new_slot);
1534 makelevel_key.name = MAKELEVEL_NAME;
1535 makelevel_key.length = MAKELEVEL_LENGTH;
1536 #ifdef VARIABLE_HASH /* bird */
1537 makelevel_key.hash1 = variable_hash_1i (MAKELEVEL_NAME, MAKELEVEL_LENGTH);
1538 makelevel_key.hash2 = 0;
1539 #endif
1540 hash_delete (&table, &makelevel_key);
1542 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
1544 v_slot = (struct variable **) table.ht_vec;
1545 v_end = v_slot + table.ht_size;
1546 for ( ; v_slot < v_end; v_slot++)
1547 if (! HASH_VACANT (*v_slot))
1549 struct variable *v = *v_slot;
1551 /* If V is recursively expanded and didn't come from the environment,
1552 expand its value. If it came from the environment, it should
1553 go back into the environment unchanged. */
1554 if (v->recursive
1555 && v->origin != o_env && v->origin != o_env_override)
1557 char *value = recursively_expand_for_file (v, file);
1558 #ifdef WINDOWS32
1559 if (strcmp(v->name, "Path") == 0 ||
1560 strcmp(v->name, "PATH") == 0)
1561 convert_Path_to_windows32(value, ';');
1562 #endif
1563 *result++ = xstrdup (concat (v->name, "=", value));
1564 free (value);
1566 else
1568 #ifdef WINDOWS32
1569 if (strcmp(v->name, "Path") == 0 ||
1570 strcmp(v->name, "PATH") == 0)
1571 convert_Path_to_windows32(v->value, ';');
1572 #endif
1573 *result++ = xstrdup (concat (v->name, "=", v->value));
1577 *result = xmalloc (100);
1578 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
1579 *++result = 0;
1581 hash_free (&table, 0);
1583 return result_0;
1586 #ifdef CONFIG_WITH_VALUE_LENGTH
1587 /* Worker function for do_variable_definition_append() and
1588 append_expanded_string_to_variable().
1589 The APPEND argument indicates whether it's an append or prepend operation. */
1590 void append_string_to_variable (struct variable *v, const char *value, unsigned int value_len, int append)
1592 /* The previous definition of the variable was recursive.
1593 The new value is the unexpanded old and new values. */
1594 unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
1595 int done_1st_prepend_copy = 0;
1597 /* Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
1598 if (!value_len)
1599 return;
1601 /* adjust the size. */
1602 if ((unsigned)v->value_alloc_len <= new_value_len + 1)
1604 v->value_alloc_len *= 2;
1605 if (v->value_alloc_len < new_value_len + 1)
1606 v->value_alloc_len = (new_value_len + 1 + value_len + 0x7f) + ~0x7fU;
1607 if (append || !v->value_length)
1608 v->value = xrealloc (v->value, v->value_alloc_len);
1609 else
1611 /* avoid the extra memcpy the xrealloc may have to do */
1612 char *new_buf = xmalloc (v->value_alloc_len);
1613 memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
1614 done_1st_prepend_copy = 1;
1615 free (v->value);
1616 v->value = new_buf;
1620 /* insert the new bits */
1621 if (v->value_length != 0)
1623 if (append)
1625 v->value[v->value_length] = ' ';
1626 memcpy (&v->value[v->value_length + 1], value, value_len + 1);
1628 else
1630 if (!done_1st_prepend_copy)
1631 memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
1632 v->value[value_len] = ' ';
1633 memcpy (v->value, value, value_len);
1636 else
1637 memcpy (v->value, value, value_len + 1);
1638 v->value_length = new_value_len;
1641 static struct variable *
1642 do_variable_definition_append (const struct floc *flocp, struct variable *v, const char *value,
1643 enum variable_origin origin, int append)
1645 if (env_overrides && origin == o_env)
1646 origin = o_env_override;
1648 if (env_overrides && v->origin == o_env)
1649 /* V came from in the environment. Since it was defined
1650 before the switches were parsed, it wasn't affected by -e. */
1651 v->origin = o_env_override;
1653 /* A variable of this name is already defined.
1654 If the old definition is from a stronger source
1655 than this one, don't redefine it. */
1656 if ((int) origin < (int) v->origin)
1657 return v;
1658 v->origin = origin;
1660 /* location */
1661 if (flocp != 0)
1662 v->fileinfo = *flocp;
1664 /* The juicy bits, append the specified value to the variable
1665 This is a heavily exercised code path in kBuild. */
1666 if (v->recursive)
1667 append_string_to_variable (v, value, strlen (value), append);
1668 else
1669 /* The previous definition of the variable was simple.
1670 The new value comes from the old value, which was expanded
1671 when it was set; and from the expanded new value. */
1672 append_expanded_string_to_variable (v, value, append);
1674 /* update the variable */
1675 return v;
1677 #endif /* CONFIG_WITH_VALUE_LENGTH */
1679 /* Given a variable, a value, and a flavor, define the variable.
1680 See the try_variable_definition() function for details on the parameters. */
1682 struct variable *
1683 do_variable_definition (const struct floc *flocp, const char *varname,
1684 const char *value, enum variable_origin origin,
1685 enum variable_flavor flavor, int target_var)
1687 const char *p;
1688 char *alloc_value = NULL;
1689 struct variable *v;
1690 int append = 0;
1691 int conditional = 0;
1692 const size_t varname_len = strlen (varname); /* bird */
1693 #ifdef CONFIG_WITH_VALUE_LENGTH
1694 unsigned int value_len = ~0U;
1695 #endif
1697 /* Calculate the variable's new value in VALUE. */
1699 switch (flavor)
1701 default:
1702 case f_bogus:
1703 /* Should not be possible. */
1704 abort ();
1705 case f_simple:
1706 /* A simple variable definition "var := value". Expand the value.
1707 We have to allocate memory since otherwise it'll clobber the
1708 variable buffer, and we may still need that if we're looking at a
1709 target-specific variable. */
1710 p = alloc_value = allocated_variable_expand (value);
1711 break;
1712 case f_conditional:
1713 /* A conditional variable definition "var ?= value".
1714 The value is set IFF the variable is not defined yet. */
1715 v = lookup_variable (varname, varname_len);
1716 if (v)
1717 return v;
1719 conditional = 1;
1720 flavor = f_recursive;
1721 /* FALLTHROUGH */
1722 case f_recursive:
1723 /* A recursive variable definition "var = value".
1724 The value is used verbatim. */
1725 p = value;
1726 break;
1727 #ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1728 case f_append:
1729 case f_prepend:
1731 const enum variable_flavor org_flavor = flavor;
1732 #else
1733 case f_append:
1735 #endif
1737 #ifdef CONFIG_WITH_LOCAL_VARIABLES
1738 /* If we have += but we're in a target or local variable context,
1739 we want to append only with other variables in the context of
1740 this target. */
1741 if (target_var || origin == o_local)
1742 #else
1743 /* If we have += but we're in a target variable context, we want to
1744 append only with other variables in the context of this target. */
1745 if (target_var)
1746 #endif
1748 append = 1;
1749 v = lookup_variable_in_set (varname, varname_len,
1750 current_variable_set_list->set);
1752 /* Don't append from the global set if a previous non-appending
1753 target-specific variable definition exists. */
1754 if (v && !v->append)
1755 append = 0;
1757 else
1758 v = lookup_variable (varname, varname_len);
1760 if (v == 0)
1762 /* There was no old value.
1763 This becomes a normal recursive definition. */
1764 p = value;
1765 flavor = f_recursive;
1767 else
1769 #ifdef CONFIG_WITH_VALUE_LENGTH
1770 v->append = append;
1771 # ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1772 return do_variable_definition_append (flocp, v, value, origin, org_flavor == f_append);
1773 # else
1774 return do_variable_definition_append (flocp, v, value, origin, 1);
1775 # endif
1776 #else /* !CONFIG_WITH_VALUE_LENGTH */
1778 /* Paste the old and new values together in VALUE. */
1780 unsigned int oldlen, vallen;
1781 const char *val;
1782 char *tp;
1784 val = value;
1785 if (v->recursive)
1786 /* The previous definition of the variable was recursive.
1787 The new value is the unexpanded old and new values. */
1788 flavor = f_recursive;
1789 else
1790 /* The previous definition of the variable was simple.
1791 The new value comes from the old value, which was expanded
1792 when it was set; and from the expanded new value. Allocate
1793 memory for the expansion as we may still need the rest of the
1794 buffer if we're looking at a target-specific variable. */
1795 val = alloc_value = allocated_variable_expand (val);
1797 oldlen = strlen (v->value);
1798 vallen = strlen (val);
1799 tp = alloca (oldlen + 1 + vallen + 1);
1800 # ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1801 if (org_flavor == f_prepend)
1803 memcpy (tp, val, vallen);
1804 tp[oldlen] = ' ';
1805 memcpy (&tp[oldlen + 1], v->value, oldlen + 1);
1807 else
1808 # endif /* CONFIG_WITH_PREPEND_ASSIGNMENT */
1810 memcpy (tp, v->value, oldlen);
1811 tp[oldlen] = ' ';
1812 memcpy (&tp[oldlen + 1], val, vallen + 1);
1814 p = tp;
1815 #endif /* !CONFIG_WITH_VALUE_LENGTH */
1820 #ifdef __MSDOS__
1821 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1822 non-Unix systems don't conform to this default configuration (in
1823 fact, most of them don't even have `/bin'). On the other hand,
1824 $SHELL in the environment, if set, points to the real pathname of
1825 the shell.
1826 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1827 the Makefile override $SHELL from the environment. But first, we
1828 look for the basename of the shell in the directory where SHELL=
1829 points, and along the $PATH; if it is found in any of these places,
1830 we define $SHELL to be the actual pathname of the shell. Thus, if
1831 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1832 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1833 defining SHELL to be "d:/unix/bash.exe". */
1834 if ((origin == o_file || origin == o_override)
1835 && strcmp (varname, "SHELL") == 0)
1837 PATH_VAR (shellpath);
1838 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1840 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1841 if (__dosexec_find_on_path (p, NULL, shellpath))
1843 char *tp;
1845 for (tp = shellpath; *tp; tp++)
1846 if (*tp == '\\')
1847 *tp = '/';
1849 v = define_variable_loc (varname, varname_len,
1850 shellpath, origin, flavor == f_recursive,
1851 flocp);
1853 else
1855 const char *shellbase, *bslash;
1856 struct variable *pathv = lookup_variable ("PATH", 4);
1857 char *path_string;
1858 char *fake_env[2];
1859 size_t pathlen = 0;
1861 shellbase = strrchr (p, '/');
1862 bslash = strrchr (p, '\\');
1863 if (!shellbase || bslash > shellbase)
1864 shellbase = bslash;
1865 if (!shellbase && p[1] == ':')
1866 shellbase = p + 1;
1867 if (shellbase)
1868 shellbase++;
1869 else
1870 shellbase = p;
1872 /* Search for the basename of the shell (with standard
1873 executable extensions) along the $PATH. */
1874 if (pathv)
1875 pathlen = strlen (pathv->value);
1876 path_string = xmalloc (5 + pathlen + 2 + 1);
1877 /* On MSDOS, current directory is considered as part of $PATH. */
1878 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1879 fake_env[0] = path_string;
1880 fake_env[1] = 0;
1881 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1883 char *tp;
1885 for (tp = shellpath; *tp; tp++)
1886 if (*tp == '\\')
1887 *tp = '/';
1889 v = define_variable_loc (varname, varname_len,
1890 shellpath, origin,
1891 flavor == f_recursive, flocp);
1893 else
1894 v = lookup_variable (varname, varname_len);
1896 free (path_string);
1899 else
1900 #endif /* __MSDOS__ */
1901 #ifdef WINDOWS32
1902 if ( varname_len == sizeof("SHELL") - 1 /* bird */
1903 && (origin == o_file || origin == o_override || origin == o_command)
1904 && streq (varname, "SHELL"))
1906 extern char *default_shell;
1908 /* Call shell locator function. If it returns TRUE, then
1909 set no_default_sh_exe to indicate sh was found and
1910 set new value for SHELL variable. */
1912 if (find_and_set_default_shell (p))
1914 v = define_variable_in_set (varname, varname_len, default_shell,
1915 # ifdef CONFIG_WITH_VALUE_LENGTH
1916 ~0U, 1 /* duplicate_value */,
1917 # endif
1918 origin, flavor == f_recursive,
1919 (target_var
1920 ? current_variable_set_list->set
1921 : NULL),
1922 flocp);
1923 no_default_sh_exe = 0;
1925 else
1926 v = lookup_variable (varname, varname_len);
1928 else
1929 #endif
1931 /* If we are defining variables inside an $(eval ...), we might have a
1932 different variable context pushed, not the global context (maybe we're
1933 inside a $(call ...) or something. Since this function is only ever
1934 invoked in places where we want to define globally visible variables,
1935 make sure we define this variable in the global set. */
1937 v = define_variable_in_set (varname, varname_len, p,
1938 #ifdef CONFIG_WITH_VALUE_LENGTH
1939 value_len, !alloc_value,
1940 #endif
1941 origin, flavor == f_recursive,
1942 #ifdef CONFIG_WITH_LOCAL_VARIABLES
1943 (target_var || origin == o_local
1944 #else
1945 (target_var
1946 #endif
1947 ? current_variable_set_list->set : NULL),
1948 flocp);
1949 v->append = append;
1950 v->conditional = conditional;
1952 #ifndef CONFIG_WITH_VALUE_LENGTH
1953 if (alloc_value)
1954 free (alloc_value);
1955 #endif
1957 return v;
1960 /* Try to interpret LINE (a null-terminated string) as a variable definition.
1962 ORIGIN may be o_file, o_override, o_env, o_env_override,
1963 or o_command specifying that the variable definition comes
1964 from a makefile, an override directive, the environment with
1965 or without the -e switch, or the command line.
1967 See the comments for parse_variable_definition().
1969 If LINE was recognized as a variable definition, a pointer to its `struct
1970 variable' is returned. If LINE is not a variable definition, NULL is
1971 returned. */
1973 struct variable *
1974 parse_variable_definition (struct variable *v, char *line)
1976 register int c;
1977 register char *p = line;
1978 register char *beg;
1979 register char *end;
1980 enum variable_flavor flavor = f_bogus;
1981 char *name;
1983 while (1)
1985 c = *p++;
1986 if (c == '\0' || c == '#')
1987 return 0;
1988 if (c == '=')
1990 end = p - 1;
1991 flavor = f_recursive;
1992 break;
1994 else if (c == ':')
1995 if (*p == '=')
1997 end = p++ - 1;
1998 flavor = f_simple;
1999 break;
2001 else
2002 /* A colon other than := is a rule line, not a variable defn. */
2003 return 0;
2004 else if (c == '+' && *p == '=')
2006 end = p++ - 1;
2007 flavor = f_append;
2008 break;
2010 #ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2011 else if (c == '<' && *p == '=')
2013 end = p++ - 1;
2014 flavor = f_prepend;
2015 break;
2017 #endif
2018 else if (c == '?' && *p == '=')
2020 end = p++ - 1;
2021 flavor = f_conditional;
2022 break;
2024 else if (c == '$')
2026 /* This might begin a variable expansion reference. Make sure we
2027 don't misrecognize chars inside the reference as =, := or +=. */
2028 char closeparen;
2029 int count;
2030 c = *p++;
2031 if (c == '(')
2032 closeparen = ')';
2033 else if (c == '{')
2034 closeparen = '}';
2035 else
2036 continue; /* Nope. */
2038 /* P now points past the opening paren or brace.
2039 Count parens or braces until it is matched. */
2040 count = 0;
2041 for (; *p != '\0'; ++p)
2043 if (*p == c)
2044 ++count;
2045 else if (*p == closeparen && --count < 0)
2047 ++p;
2048 break;
2053 v->flavor = flavor;
2055 beg = next_token (line);
2056 while (end > beg && isblank ((unsigned char)end[-1]))
2057 --end;
2058 p = next_token (p);
2059 v->value = p;
2060 #ifdef CONFIG_WITH_VALUE_LENGTH
2061 v->value_length = v->value_alloc_len = -1;
2062 #endif
2064 /* Expand the name, so "$(foo)bar = baz" works. */
2065 name = alloca (end - beg + 1);
2066 memcpy (name, beg, end - beg);
2067 name[end - beg] = '\0';
2068 v->name = allocated_variable_expand (name);
2070 if (v->name[0] == '\0')
2071 fatal (&v->fileinfo, _("empty variable name"));
2073 return v;
2076 /* Try to interpret LINE (a null-terminated string) as a variable definition.
2078 ORIGIN may be o_file, o_override, o_env, o_env_override, o_local,
2079 or o_command specifying that the variable definition comes
2080 from a makefile, an override directive, the environment with
2081 or without the -e switch, or the command line.
2083 See the comments for parse_variable_definition().
2085 If LINE was recognized as a variable definition, a pointer to its `struct
2086 variable' is returned. If LINE is not a variable definition, NULL is
2087 returned. */
2089 struct variable *
2090 try_variable_definition (const struct floc *flocp, char *line,
2091 enum variable_origin origin, int target_var)
2093 struct variable v;
2094 struct variable *vp;
2096 if (flocp != 0)
2097 v.fileinfo = *flocp;
2098 else
2099 v.fileinfo.filenm = 0;
2101 if (!parse_variable_definition (&v, line))
2102 return 0;
2104 vp = do_variable_definition (flocp, v.name, v.value,
2105 origin, v.flavor, target_var);
2107 free (v.name);
2109 return vp;
2112 /* Print information for variable V, prefixing it with PREFIX. */
2114 static void
2115 print_variable (const void *item, void *arg)
2117 const struct variable *v = item;
2118 const char *prefix = arg;
2119 const char *origin;
2121 switch (v->origin)
2123 case o_default:
2124 origin = _("default");
2125 break;
2126 case o_env:
2127 origin = _("environment");
2128 break;
2129 case o_file:
2130 origin = _("makefile");
2131 break;
2132 case o_env_override:
2133 origin = _("environment under -e");
2134 break;
2135 case o_command:
2136 origin = _("command line");
2137 break;
2138 case o_override:
2139 origin = _("`override' directive");
2140 break;
2141 case o_automatic:
2142 origin = _("automatic");
2143 break;
2144 #ifdef CONFIG_WITH_LOCAL_VARIABLES
2145 case o_local:
2146 origin = _("`local' directive");
2147 break;
2148 #endif
2149 case o_invalid:
2150 default:
2151 abort ();
2153 fputs ("# ", stdout);
2154 fputs (origin, stdout);
2155 if (v->fileinfo.filenm)
2156 printf (_(" (from `%s', line %lu)"),
2157 v->fileinfo.filenm, v->fileinfo.lineno);
2158 putchar ('\n');
2159 fputs (prefix, stdout);
2161 /* Is this a `define'? */
2162 if (v->recursive && strchr (v->value, '\n') != 0)
2163 printf ("define %s\n%s\nendef\n", v->name, v->value);
2164 else
2166 register char *p;
2168 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
2170 /* Check if the value is just whitespace. */
2171 p = next_token (v->value);
2172 if (p != v->value && *p == '\0')
2173 /* All whitespace. */
2174 printf ("$(subst ,,%s)", v->value);
2175 else if (v->recursive)
2176 fputs (v->value, stdout);
2177 else
2178 /* Double up dollar signs. */
2179 for (p = v->value; *p != '\0'; ++p)
2181 if (*p == '$')
2182 putchar ('$');
2183 putchar (*p);
2185 putchar ('\n');
2190 /* Print all the variables in SET. PREFIX is printed before
2191 the actual variable definitions (everything else is comments). */
2193 void
2194 print_variable_set (struct variable_set *set, char *prefix)
2196 hash_map_arg (&set->table, print_variable, prefix);
2198 fputs (_("# variable set hash-table stats:\n"), stdout);
2199 fputs ("# ", stdout);
2200 hash_print_stats (&set->table, stdout);
2201 putc ('\n', stdout);
2204 /* Print the data base of variables. */
2206 void
2207 print_variable_data_base (void)
2209 puts (_("\n# Variables\n"));
2211 print_variable_set (&global_variable_set, "");
2213 puts (_("\n# Pattern-specific Variable Values"));
2216 struct pattern_var *p;
2217 int rules = 0;
2219 for (p = pattern_vars; p != 0; p = p->next)
2221 ++rules;
2222 printf ("\n%s :\n", p->target);
2223 print_variable (&p->variable, "# ");
2226 if (rules == 0)
2227 puts (_("\n# No pattern-specific variable values."));
2228 else
2229 printf (_("\n# %u pattern-specific variable values"), rules);
2234 /* Print all the local variables of FILE. */
2236 void
2237 print_file_variables (const struct file *file)
2239 if (file->variables != 0)
2240 print_variable_set (file->variables->set, "# ");
2243 #ifdef WINDOWS32
2244 void
2245 sync_Path_environment (void)
2247 char *path = allocated_variable_expand ("$(PATH)");
2248 static char *environ_path = NULL;
2250 if (!path)
2251 return;
2254 * If done this before, don't leak memory unnecessarily.
2255 * Free the previous entry before allocating new one.
2257 if (environ_path)
2258 free (environ_path);
2261 * Create something WINDOWS32 world can grok
2263 convert_Path_to_windows32 (path, ';');
2264 environ_path = xstrdup (concat ("PATH", "=", path));
2265 putenv (environ_path);
2266 free (path);
2268 #endif