source: trunk/src/kmk/variable.c@ 1447

Last change on this file since 1447 was 1444, checked in by bird, 17 years ago

Increased SMALL_SCOPE_VARIABLE_BUCKETS to avoid massive collitions when using the new 'local' keyword.

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