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

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

Added $(make-stats ) that provides access to memory and hash stats (if CONFIG_WITH_MAKE_STATS is defined).

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