VirtualBox

source: kBuild/trunk/src/kmk/variable.c@ 1439

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

CONFIG_WITH_EVALPLUS: evalctx, evalval, evalvalctx, evalcall and evalcall2. (all untested)

  • Property svn:eol-style set to native
File size: 62.3 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#ifndef KMK
976 char buf[200];
977#else
978 char buf[1024];
979 const char *envvar;
980#endif
981
982 sprintf (buf, "%u", makelevel);
983 (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
984
985 sprintf (buf, "%s%s%s",
986 version_string,
987 (remote_description == 0 || remote_description[0] == '\0')
988 ? "" : "-",
989 (remote_description == 0 || remote_description[0] == '\0')
990 ? "" : remote_description);
991 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
992
993#ifdef KMK
994 /* Define KMK_VERSION to indicate kMk. */
995 (void) define_variable ("KMK_VERSION", 11, buf, o_default, 0);
996
997 /* Define KBUILD_VERSION* */
998 sprintf (buf, "%d", KBUILD_VERSION_MAJOR);
999 define_variable ("KBUILD_VERSION_MAJOR", sizeof ("KBUILD_VERSION_MAJOR") - 1,
1000 buf, o_default, 0);
1001 sprintf (buf, "%d", KBUILD_VERSION_MINOR);
1002 define_variable ("KBUILD_VERSION_MINOR", sizeof("KBUILD_VERSION_MINOR") - 1,
1003 buf, o_default, 0);
1004 sprintf (buf, "%d", KBUILD_VERSION_PATCH);
1005 define_variable ("KBUILD_VERSION_PATCH", sizeof ("KBUILD_VERSION_PATCH") - 1,
1006 buf, o_default, 0);
1007
1008 sprintf (buf, "%d.%d.%d", KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR, KBUILD_VERSION_PATCH);
1009 define_variable ("KBUILD_VERSION", sizeof ("KBUILD_VERSION") - 1,
1010 buf, o_default, 0);
1011
1012 /* The build platform defaults. */
1013 envvar = getenv ("BUILD_PLATFORM");
1014 if (!envvar)
1015 define_variable ("BUILD_PLATFORM", sizeof ("BUILD_PLATFORM") - 1,
1016 BUILD_PLATFORM, o_default, 0);
1017 envvar = getenv ("BUILD_PLATFORM_ARCH");
1018 if (!envvar)
1019 define_variable ("BUILD_PLATFORM_ARCH", sizeof ("BUILD_PLATFORM_ARCH") - 1,
1020 BUILD_PLATFORM_ARCH, o_default, 0);
1021 envvar = getenv ("BUILD_PLATFORM_CPU");
1022 if (!envvar)
1023 define_variable ("BUILD_PLATFORM_CPU", sizeof ("BUILD_PLATFORM_CPU") - 1,
1024 BUILD_PLATFORM_CPU, o_default, 0);
1025
1026 /* The kBuild locations. */
1027 define_variable ("PATH_KBUILD", sizeof ("PATH_KBUILD") - 1,
1028 get_path_kbuild (), o_default, 0);
1029 define_variable ("PATH_KBUILD_BIN", sizeof ("PATH_KBUILD_BIN") - 1,
1030 get_path_kbuild_bin (), o_default, 0);
1031
1032 /* Define KMK_FEATURES to indicate various working KMK features. */
1033# if defined (CONFIG_WITH_RSORT) \
1034 && defined (CONFIG_WITH_ABSPATHEX) \
1035 && defined (CONFIG_WITH_TOUPPER_TOLOWER) \
1036 && defined (CONFIG_WITH_VALUE_LENGTH) && defined (CONFIG_WITH_COMPARE) \
1037 && defined (CONFIG_WITH_STACK) \
1038 && defined (CONFIG_WITH_MATH) \
1039 && defined (CONFIG_WITH_XARGS) \
1040 && defined (CONFIG_WITH_EXPLICIT_MULTITARGET) \
1041 && defined (CONFIG_WITH_PREPEND_ASSIGNMENT) \
1042 && defined (CONFIG_WITH_SET_CONDITIONALS) \
1043 && defined (CONFIG_WITH_DATE) \
1044 && defined (CONFIG_WITH_FILE_SIZE) \
1045 && defined (CONFIG_WITH_WHICH) \
1046 && defined (CONFIG_WITH_EVALPLUS) \
1047 && defined (CONFIG_WITH_MAKE_STATS) \
1048 && defined (KMK_HELPERS)
1049 (void) define_variable ("KMK_FEATURES", 12,
1050 "append-dash-n abspath"
1051 " rsort"
1052 " abspathex"
1053 " toupper tolower"
1054 " comp-vars comp-cmds"
1055 " stack"
1056 " math-int"
1057 " xargs"
1058 " explicit-multitarget"
1059 " prepend-assignment"
1060 " set-conditionals"
1061 " date"
1062 " file-size"
1063 " which"
1064 " evalctx evalval evalvalctx evalcall evalcall2"
1065 " make-stats"
1066 " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one "
1067 , o_default, 0);
1068# else /* MSC can't deal with strings mixed with #if/#endif, thus the slow way. */
1069# error "All features should be enabled by default!"
1070 strcpy (buf, "append-dash-n abspath");
1071# if defined (CONFIG_WITH_RSORT)
1072 strcat (buf, " rsort");
1073# endif
1074# if defined (CONFIG_WITH_ABSPATHEX)
1075 strcat (buf, " abspathex");
1076# endif
1077# if defined (CONFIG_WITH_TOUPPER_TOLOWER)
1078 strcat (buf, " toupper tolower");
1079# endif
1080# if defined (CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
1081 strcat (buf, " comp-vars comp-cmds");
1082# endif
1083# if defined (CONFIG_WITH_STACK)
1084 strcat (buf, " stack");
1085# endif
1086# if defined (CONFIG_WITH_MATH)
1087 strcat (buf, " math-int");
1088# endif
1089# if defined (CONFIG_WITH_XARGS)
1090 strcat (buf, " xargs");
1091# endif
1092# if defined (CONFIG_WITH_EXPLICIT_MULTITARGET)
1093 strcat (buf, " explicit-multitarget");
1094# endif
1095# if defined (CONFIG_WITH_PREPEND_ASSIGNMENT)
1096 strcat (buf, " prepend-assignment");
1097# endif
1098# if defined (CONFIG_WITH_SET_CONDITIONALS)
1099 strcat (buf, " set-conditionals");
1100# endif
1101# if defined (CONFIG_WITH_DATE)
1102 strcat (buf, " date");
1103# endif
1104# if defined (CONFIG_WITH_FILE_SIZE)
1105 strcat (buf, " file-size");
1106# endif
1107# if defined (CONFIG_WITH_WHICH)
1108 strcat (buf, " which");
1109# endif
1110# if defined (CONFIG_WITH_EVALPLUS)
1111 strcat (buf, " evalctx evalval evalvalctx evalcall evalcall2");
1112# endif
1113# if defined (CONFIG_WITH_MAKE_STATS)
1114 strcat (buf, " make-stats");
1115# endif
1116# if defined (KMK_HELPERS)
1117 strcat (buf, " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one");
1118# endif
1119 (void) define_variable ("KMK_FEATURES", 12, buf, o_default, 0);
1120# endif
1121
1122#endif /* KMK */
1123
1124#ifdef CONFIG_WITH_KMK_BUILTIN
1125 /* The supported kMk Builtin commands. */
1126 (void) define_variable ("KMK_BUILTIN", 11, "append cat cp cmp echo install kDepIDB ln md5sum mkdir mv printf rm rmdir test", o_default, 0);
1127#endif
1128
1129#ifdef __MSDOS__
1130 /* Allow to specify a special shell just for Make,
1131 and use $COMSPEC as the default $SHELL when appropriate. */
1132 {
1133 static char shell_str[] = "SHELL";
1134 const int shlen = sizeof (shell_str) - 1;
1135 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
1136 struct variable *comp = lookup_variable ("COMSPEC", 7);
1137
1138 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
1139 if (mshp)
1140 (void) define_variable (shell_str, shlen,
1141 mshp->value, o_env_override, 0);
1142 else if (comp)
1143 {
1144 /* $COMSPEC shouldn't override $SHELL. */
1145 struct variable *shp = lookup_variable (shell_str, shlen);
1146
1147 if (!shp)
1148 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
1149 }
1150 }
1151#elif defined(__EMX__)
1152 {
1153 static char shell_str[] = "SHELL";
1154 const int shlen = sizeof (shell_str) - 1;
1155 struct variable *shell = lookup_variable (shell_str, shlen);
1156 struct variable *replace = lookup_variable ("MAKESHELL", 9);
1157
1158 /* if $MAKESHELL is defined in the environment assume o_env_override */
1159 if (replace && *replace->value && replace->origin == o_env)
1160 replace->origin = o_env_override;
1161
1162 /* if $MAKESHELL is not defined use $SHELL but only if the variable
1163 did not come from the environment */
1164 if (!replace || !*replace->value)
1165 if (shell && *shell->value && (shell->origin == o_env
1166 || shell->origin == o_env_override))
1167 {
1168 /* overwrite whatever we got from the environment */
1169 free(shell->value);
1170 shell->value = xstrdup (default_shell);
1171 shell->origin = o_default;
1172 }
1173
1174 /* Some people do not like cmd to be used as the default
1175 if $SHELL is not defined in the Makefile.
1176 With -DNO_CMD_DEFAULT you can turn off this behaviour */
1177# ifndef NO_CMD_DEFAULT
1178 /* otherwise use $COMSPEC */
1179 if (!replace || !*replace->value)
1180 replace = lookup_variable ("COMSPEC", 7);
1181
1182 /* otherwise use $OS2_SHELL */
1183 if (!replace || !*replace->value)
1184 replace = lookup_variable ("OS2_SHELL", 9);
1185# else
1186# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
1187# endif
1188
1189 if (replace && *replace->value)
1190 /* overwrite $SHELL */
1191 (void) define_variable (shell_str, shlen, replace->value,
1192 replace->origin, 0);
1193 else
1194 /* provide a definition if there is none */
1195 (void) define_variable (shell_str, shlen, default_shell,
1196 o_default, 0);
1197 }
1198
1199#endif
1200
1201 /* This won't override any definition, but it will provide one if there
1202 isn't one there. */
1203 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
1204
1205 /* On MSDOS we do use SHELL from environment, since it isn't a standard
1206 environment variable on MSDOS, so whoever sets it, does that on purpose.
1207 On OS/2 we do not use SHELL from environment but we have already handled
1208 that problem above. */
1209#if !defined(__MSDOS__) && !defined(__EMX__)
1210 /* Don't let SHELL come from the environment. */
1211 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
1212 {
1213 free (v->value);
1214 v->origin = o_file;
1215 v->value = xstrdup (default_shell);
1216#ifdef CONFIG_WITH_VALUE_LENGTH
1217 v->value_length = strlen (v->value);
1218 v->value_alloc_len = v->value_length + 1;
1219#endif
1220 }
1221#endif
1222
1223 /* Make sure MAKEFILES gets exported if it is set. */
1224 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
1225 v->export = v_ifset;
1226
1227 /* Define the magic D and F variables in terms of
1228 the automatic variables they are variations of. */
1229
1230#ifdef VMS
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 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
1237 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
1238#else
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 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
1245 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
1246#endif
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 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
1253 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
1254}
1255
1256
1257int export_all_variables;
1258
1259/* Create a new environment for FILE's commands.
1260 If FILE is nil, this is for the `shell' function.
1261 The child's MAKELEVEL variable is incremented. */
1262
1263char **
1264target_environment (struct file *file)
1265{
1266 struct variable_set_list *set_list;
1267 register struct variable_set_list *s;
1268 struct hash_table table;
1269 struct variable **v_slot;
1270 struct variable **v_end;
1271 struct variable makelevel_key;
1272 char **result_0;
1273 char **result;
1274
1275 if (file == 0)
1276 set_list = current_variable_set_list;
1277 else
1278 set_list = file->variables;
1279
1280 hash_init (&table, VARIABLE_BUCKETS,
1281 variable_hash_1, variable_hash_2, variable_hash_cmp);
1282
1283 /* Run through all the variable sets in the list,
1284 accumulating variables in TABLE. */
1285 for (s = set_list; s != 0; s = s->next)
1286 {
1287 struct variable_set *set = s->set;
1288 v_slot = (struct variable **) set->table.ht_vec;
1289 v_end = v_slot + set->table.ht_size;
1290 for ( ; v_slot < v_end; v_slot++)
1291 if (! HASH_VACANT (*v_slot))
1292 {
1293 struct variable **new_slot;
1294 struct variable *v = *v_slot;
1295
1296 /* If this is a per-target variable and it hasn't been touched
1297 already then look up the global version and take its export
1298 value. */
1299 if (v->per_target && v->export == v_default)
1300 {
1301 struct variable *gv;
1302
1303 gv = lookup_variable_in_set (v->name, strlen(v->name),
1304 &global_variable_set);
1305 if (gv)
1306 v->export = gv->export;
1307 }
1308
1309 switch (v->export)
1310 {
1311 case v_default:
1312 if (v->origin == o_default || v->origin == o_automatic)
1313 /* Only export default variables by explicit request. */
1314 continue;
1315
1316 /* The variable doesn't have a name that can be exported. */
1317 if (! v->exportable)
1318 continue;
1319
1320 if (! export_all_variables
1321 && v->origin != o_command
1322 && v->origin != o_env && v->origin != o_env_override)
1323 continue;
1324 break;
1325
1326 case v_export:
1327 break;
1328
1329 case v_noexport:
1330 /* If this is the SHELL variable and it's not exported, then
1331 add the value from our original environment. */
1332 if (streq (v->name, "SHELL"))
1333 {
1334 extern struct variable shell_var;
1335 v = &shell_var;
1336 break;
1337 }
1338 continue;
1339
1340 case v_ifset:
1341 if (v->origin == o_default)
1342 continue;
1343 break;
1344 }
1345
1346 new_slot = (struct variable **) hash_find_slot (&table, v);
1347 if (HASH_VACANT (*new_slot))
1348 hash_insert_at (&table, v, new_slot);
1349 }
1350 }
1351
1352 makelevel_key.name = MAKELEVEL_NAME;
1353 makelevel_key.length = MAKELEVEL_LENGTH;
1354#ifdef VARIABLE_HASH /* bird */
1355 makelevel_key.hash1 = variable_hash_1i (MAKELEVEL_NAME, MAKELEVEL_LENGTH);
1356 makelevel_key.hash2 = 0;
1357#endif
1358 hash_delete (&table, &makelevel_key);
1359
1360 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
1361
1362 v_slot = (struct variable **) table.ht_vec;
1363 v_end = v_slot + table.ht_size;
1364 for ( ; v_slot < v_end; v_slot++)
1365 if (! HASH_VACANT (*v_slot))
1366 {
1367 struct variable *v = *v_slot;
1368
1369 /* If V is recursively expanded and didn't come from the environment,
1370 expand its value. If it came from the environment, it should
1371 go back into the environment unchanged. */
1372 if (v->recursive
1373 && v->origin != o_env && v->origin != o_env_override)
1374 {
1375 char *value = recursively_expand_for_file (v, file);
1376#ifdef WINDOWS32
1377 if (strcmp(v->name, "Path") == 0 ||
1378 strcmp(v->name, "PATH") == 0)
1379 convert_Path_to_windows32(value, ';');
1380#endif
1381 *result++ = xstrdup (concat (v->name, "=", value));
1382 free (value);
1383 }
1384 else
1385 {
1386#ifdef WINDOWS32
1387 if (strcmp(v->name, "Path") == 0 ||
1388 strcmp(v->name, "PATH") == 0)
1389 convert_Path_to_windows32(v->value, ';');
1390#endif
1391 *result++ = xstrdup (concat (v->name, "=", v->value));
1392 }
1393 }
1394
1395 *result = xmalloc (100);
1396 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
1397 *++result = 0;
1398
1399 hash_free (&table, 0);
1400
1401 return result_0;
1402}
1403
1404
1405#ifdef CONFIG_WITH_VALUE_LENGTH
1406static struct variable *
1407do_variable_definition_append (const struct floc *flocp, struct variable *v, const char *value,
1408 enum variable_origin origin, int append)
1409{
1410 if (env_overrides && origin == o_env)
1411 origin = o_env_override;
1412
1413 if (env_overrides && v->origin == o_env)
1414 /* V came from in the environment. Since it was defined
1415 before the switches were parsed, it wasn't affected by -e. */
1416 v->origin = o_env_override;
1417
1418 /* A variable of this name is already defined.
1419 If the old definition is from a stronger source
1420 than this one, don't redefine it. */
1421 if ((int) origin < (int) v->origin)
1422 return v;
1423 v->origin = origin;
1424
1425 /* location */
1426 if (flocp != 0)
1427 v->fileinfo = *flocp;
1428
1429 /* The juicy bits, append the specified value to the variable
1430 This is a heavily exercised code path in kBuild. */
1431 if (v->recursive)
1432 {
1433 /* The previous definition of the variable was recursive.
1434 The new value is the unexpanded old and new values. */
1435 unsigned int value_len = strlen (value);
1436 unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
1437 int done_1st_prepend_copy = 0;
1438
1439 /* adjust the size. */
1440 if ((unsigned)v->value_alloc_len <= new_value_len + 1)
1441 {
1442 v->value_alloc_len *= 2;
1443 if (v->value_alloc_len < new_value_len + 1)
1444 v->value_alloc_len = (new_value_len + 1 + value_len + 0x7f) + ~0x7fU;
1445 if (append || !v->value_length)
1446 v->value = xrealloc (v->value, v->value_alloc_len);
1447 else
1448 {
1449 /* avoid the extra memcpy the xrealloc may have to do */
1450 char *new_buf = xmalloc (v->value_alloc_len);
1451 memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
1452 done_1st_prepend_copy = 1;
1453 free (v->value);
1454 v->value = new_buf;
1455 }
1456 }
1457
1458 /* insert the new bits */
1459 if (v->value_length != 0)
1460 {
1461 if (append)
1462 {
1463 v->value[v->value_length] = ' ';
1464 memcpy (&v->value[v->value_length + 1], value, value_len + 1);
1465 }
1466 else
1467 {
1468 if (!done_1st_prepend_copy)
1469 memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
1470 v->value[value_len] = ' ';
1471 memcpy (v->value, value, value_len);
1472 }
1473 }
1474 else
1475 memcpy (v->value, value, value_len + 1);
1476 v->value_length = new_value_len;
1477 }
1478 else
1479 {
1480 /* The previous definition of the variable was simple.
1481 The new value comes from the old value, which was expanded
1482 when it was set; and from the expanded new value. */
1483 append_expanded_string_to_variable(v, value);
1484 }
1485
1486 /* update the variable */
1487 return v;
1488}
1489#endif /* CONFIG_WITH_VALUE_LENGTH */
1490
1491
1492/* Given a variable, a value, and a flavor, define the variable.
1493 See the try_variable_definition() function for details on the parameters. */
1494
1495struct variable *
1496do_variable_definition (const struct floc *flocp, const char *varname,
1497 const char *value, enum variable_origin origin,
1498 enum variable_flavor flavor, int target_var)
1499{
1500 const char *p;
1501 char *alloc_value = NULL;
1502 struct variable *v;
1503 int append = 0;
1504 int conditional = 0;
1505 const size_t varname_len = strlen (varname); /* bird */
1506#ifdef CONFIG_WITH_VALUE_LENGTH
1507 unsigned int value_len = ~0U;
1508#endif
1509
1510 /* Calculate the variable's new value in VALUE. */
1511
1512 switch (flavor)
1513 {
1514 default:
1515 case f_bogus:
1516 /* Should not be possible. */
1517 abort ();
1518 case f_simple:
1519 /* A simple variable definition "var := value". Expand the value.
1520 We have to allocate memory since otherwise it'll clobber the
1521 variable buffer, and we may still need that if we're looking at a
1522 target-specific variable. */
1523 p = alloc_value = allocated_variable_expand (value);
1524 break;
1525 case f_conditional:
1526 /* A conditional variable definition "var ?= value".
1527 The value is set IFF the variable is not defined yet. */
1528 v = lookup_variable (varname, varname_len);
1529 if (v)
1530 return v;
1531
1532 conditional = 1;
1533 flavor = f_recursive;
1534 /* FALLTHROUGH */
1535 case f_recursive:
1536 /* A recursive variable definition "var = value".
1537 The value is used verbatim. */
1538 p = value;
1539 break;
1540#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1541 case f_append:
1542 case f_prepend:
1543 {
1544 const enum variable_flavor org_flavor = flavor;
1545#else
1546 case f_append:
1547 {
1548#endif
1549
1550#ifdef CONFIG_WITH_LOCAL_VARIABLES
1551 /* If we have += but we're in a target or local variable context,
1552 we want to append only with other variables in the context of
1553 this target. */
1554 if (target_var || origin == o_local)
1555#else
1556 /* If we have += but we're in a target variable context, we want to
1557 append only with other variables in the context of this target. */
1558 if (target_var)
1559#endif
1560 {
1561 append = 1;
1562 v = lookup_variable_in_set (varname, varname_len,
1563 current_variable_set_list->set);
1564
1565 /* Don't append from the global set if a previous non-appending
1566 target-specific variable definition exists. */
1567 if (v && !v->append)
1568 append = 0;
1569 }
1570 else
1571 v = lookup_variable (varname, varname_len);
1572
1573 if (v == 0)
1574 {
1575 /* There was no old value.
1576 This becomes a normal recursive definition. */
1577 p = value;
1578 flavor = f_recursive;
1579 }
1580 else
1581 {
1582#ifdef CONFIG_WITH_VALUE_LENGTH
1583 v->append = append;
1584# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1585 return do_variable_definition_append (flocp, v, value, origin, org_flavor == f_append);
1586# else
1587 return do_variable_definition_append (flocp, v, value, origin, 1);
1588# endif
1589#else /* !CONFIG_WITH_VALUE_LENGTH */
1590
1591 /* Paste the old and new values together in VALUE. */
1592
1593 unsigned int oldlen, vallen;
1594 const char *val;
1595 char *tp;
1596
1597 val = value;
1598 if (v->recursive)
1599 /* The previous definition of the variable was recursive.
1600 The new value is the unexpanded old and new values. */
1601 flavor = f_recursive;
1602 else
1603 /* The previous definition of the variable was simple.
1604 The new value comes from the old value, which was expanded
1605 when it was set; and from the expanded new value. Allocate
1606 memory for the expansion as we may still need the rest of the
1607 buffer if we're looking at a target-specific variable. */
1608 val = alloc_value = allocated_variable_expand (val);
1609
1610 oldlen = strlen (v->value);
1611 vallen = strlen (val);
1612 tp = alloca (oldlen + 1 + vallen + 1);
1613# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1614 if (org_flavor == f_prepend)
1615 {
1616 memcpy (tp, val, vallen);
1617 tp[oldlen] = ' ';
1618 memcpy (&tp[oldlen + 1], v->value, oldlen + 1);
1619 }
1620 else
1621# endif /* CONFIG_WITH_PREPEND_ASSIGNMENT */
1622 {
1623 memcpy (tp, v->value, oldlen);
1624 tp[oldlen] = ' ';
1625 memcpy (&tp[oldlen + 1], val, vallen + 1);
1626 }
1627 p = tp;
1628#endif /* !CONFIG_WITH_VALUE_LENGTH */
1629 }
1630 }
1631 }
1632
1633#ifdef __MSDOS__
1634 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1635 non-Unix systems don't conform to this default configuration (in
1636 fact, most of them don't even have `/bin'). On the other hand,
1637 $SHELL in the environment, if set, points to the real pathname of
1638 the shell.
1639 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1640 the Makefile override $SHELL from the environment. But first, we
1641 look for the basename of the shell in the directory where SHELL=
1642 points, and along the $PATH; if it is found in any of these places,
1643 we define $SHELL to be the actual pathname of the shell. Thus, if
1644 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1645 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1646 defining SHELL to be "d:/unix/bash.exe". */
1647 if ((origin == o_file || origin == o_override)
1648 && strcmp (varname, "SHELL") == 0)
1649 {
1650 PATH_VAR (shellpath);
1651 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1652
1653 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1654 if (__dosexec_find_on_path (p, NULL, shellpath))
1655 {
1656 char *tp;
1657
1658 for (tp = shellpath; *tp; tp++)
1659 if (*tp == '\\')
1660 *tp = '/';
1661
1662 v = define_variable_loc (varname, varname_len,
1663 shellpath, origin, flavor == f_recursive,
1664 flocp);
1665 }
1666 else
1667 {
1668 const char *shellbase, *bslash;
1669 struct variable *pathv = lookup_variable ("PATH", 4);
1670 char *path_string;
1671 char *fake_env[2];
1672 size_t pathlen = 0;
1673
1674 shellbase = strrchr (p, '/');
1675 bslash = strrchr (p, '\\');
1676 if (!shellbase || bslash > shellbase)
1677 shellbase = bslash;
1678 if (!shellbase && p[1] == ':')
1679 shellbase = p + 1;
1680 if (shellbase)
1681 shellbase++;
1682 else
1683 shellbase = p;
1684
1685 /* Search for the basename of the shell (with standard
1686 executable extensions) along the $PATH. */
1687 if (pathv)
1688 pathlen = strlen (pathv->value);
1689 path_string = xmalloc (5 + pathlen + 2 + 1);
1690 /* On MSDOS, current directory is considered as part of $PATH. */
1691 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1692 fake_env[0] = path_string;
1693 fake_env[1] = 0;
1694 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1695 {
1696 char *tp;
1697
1698 for (tp = shellpath; *tp; tp++)
1699 if (*tp == '\\')
1700 *tp = '/';
1701
1702 v = define_variable_loc (varname, varname_len,
1703 shellpath, origin,
1704 flavor == f_recursive, flocp);
1705 }
1706 else
1707 v = lookup_variable (varname, varname_len);
1708
1709 free (path_string);
1710 }
1711 }
1712 else
1713#endif /* __MSDOS__ */
1714#ifdef WINDOWS32
1715 if ( varname_len == sizeof("SHELL") - 1 /* bird */
1716 && (origin == o_file || origin == o_override || origin == o_command)
1717 && streq (varname, "SHELL"))
1718 {
1719 extern char *default_shell;
1720
1721 /* Call shell locator function. If it returns TRUE, then
1722 set no_default_sh_exe to indicate sh was found and
1723 set new value for SHELL variable. */
1724
1725 if (find_and_set_default_shell (p))
1726 {
1727 v = define_variable_in_set (varname, varname_len, default_shell,
1728# ifdef CONFIG_WITH_VALUE_LENGTH
1729 ~0U, 1 /* duplicate_value */,
1730# endif
1731 origin, flavor == f_recursive,
1732 (target_var
1733 ? current_variable_set_list->set
1734 : NULL),
1735 flocp);
1736 no_default_sh_exe = 0;
1737 }
1738 else
1739 v = lookup_variable (varname, varname_len);
1740 }
1741 else
1742#endif
1743
1744 /* If we are defining variables inside an $(eval ...), we might have a
1745 different variable context pushed, not the global context (maybe we're
1746 inside a $(call ...) or something. Since this function is only ever
1747 invoked in places where we want to define globally visible variables,
1748 make sure we define this variable in the global set. */
1749
1750 v = define_variable_in_set (varname, varname_len, p,
1751#ifdef CONFIG_WITH_VALUE_LENGTH
1752 value_len, !alloc_value,
1753#endif
1754 origin, flavor == f_recursive,
1755#ifdef CONFIG_WITH_LOCAL_VARIABLES
1756 (target_var || origin == o_local
1757#else
1758 (target_var
1759#endif
1760 ? current_variable_set_list->set : NULL),
1761 flocp);
1762 v->append = append;
1763 v->conditional = conditional;
1764
1765#ifndef CONFIG_WITH_VALUE_LENGTH
1766 if (alloc_value)
1767 free (alloc_value);
1768#endif
1769
1770 return v;
1771}
1772
1773
1774/* Try to interpret LINE (a null-terminated string) as a variable definition.
1775
1776 ORIGIN may be o_file, o_override, o_env, o_env_override,
1777 or o_command specifying that the variable definition comes
1778 from a makefile, an override directive, the environment with
1779 or without the -e switch, or the command line.
1780
1781 See the comments for parse_variable_definition().
1782
1783 If LINE was recognized as a variable definition, a pointer to its `struct
1784 variable' is returned. If LINE is not a variable definition, NULL is
1785 returned. */
1786
1787struct variable *
1788parse_variable_definition (struct variable *v, char *line)
1789{
1790 register int c;
1791 register char *p = line;
1792 register char *beg;
1793 register char *end;
1794 enum variable_flavor flavor = f_bogus;
1795 char *name;
1796
1797 while (1)
1798 {
1799 c = *p++;
1800 if (c == '\0' || c == '#')
1801 return 0;
1802 if (c == '=')
1803 {
1804 end = p - 1;
1805 flavor = f_recursive;
1806 break;
1807 }
1808 else if (c == ':')
1809 if (*p == '=')
1810 {
1811 end = p++ - 1;
1812 flavor = f_simple;
1813 break;
1814 }
1815 else
1816 /* A colon other than := is a rule line, not a variable defn. */
1817 return 0;
1818 else if (c == '+' && *p == '=')
1819 {
1820 end = p++ - 1;
1821 flavor = f_append;
1822 break;
1823 }
1824#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1825 else if (c == '<' && *p == '=')
1826 {
1827 end = p++ - 1;
1828 flavor = f_prepend;
1829 break;
1830 }
1831#endif
1832 else if (c == '?' && *p == '=')
1833 {
1834 end = p++ - 1;
1835 flavor = f_conditional;
1836 break;
1837 }
1838 else if (c == '$')
1839 {
1840 /* This might begin a variable expansion reference. Make sure we
1841 don't misrecognize chars inside the reference as =, := or +=. */
1842 char closeparen;
1843 int count;
1844 c = *p++;
1845 if (c == '(')
1846 closeparen = ')';
1847 else if (c == '{')
1848 closeparen = '}';
1849 else
1850 continue; /* Nope. */
1851
1852 /* P now points past the opening paren or brace.
1853 Count parens or braces until it is matched. */
1854 count = 0;
1855 for (; *p != '\0'; ++p)
1856 {
1857 if (*p == c)
1858 ++count;
1859 else if (*p == closeparen && --count < 0)
1860 {
1861 ++p;
1862 break;
1863 }
1864 }
1865 }
1866 }
1867 v->flavor = flavor;
1868
1869 beg = next_token (line);
1870 while (end > beg && isblank ((unsigned char)end[-1]))
1871 --end;
1872 p = next_token (p);
1873 v->value = p;
1874#ifdef CONFIG_WITH_VALUE_LENGTH
1875 v->value_length = v->value_alloc_len = -1;
1876#endif
1877
1878 /* Expand the name, so "$(foo)bar = baz" works. */
1879 name = alloca (end - beg + 1);
1880 memcpy (name, beg, end - beg);
1881 name[end - beg] = '\0';
1882 v->name = allocated_variable_expand (name);
1883
1884 if (v->name[0] == '\0')
1885 fatal (&v->fileinfo, _("empty variable name"));
1886
1887 return v;
1888}
1889
1890
1891/* Try to interpret LINE (a null-terminated string) as a variable definition.
1892
1893 ORIGIN may be o_file, o_override, o_env, o_env_override, o_local,
1894 or o_command specifying that the variable definition comes
1895 from a makefile, an override directive, the environment with
1896 or without the -e switch, or the command line.
1897
1898 See the comments for parse_variable_definition().
1899
1900 If LINE was recognized as a variable definition, a pointer to its `struct
1901 variable' is returned. If LINE is not a variable definition, NULL is
1902 returned. */
1903
1904struct variable *
1905try_variable_definition (const struct floc *flocp, char *line,
1906 enum variable_origin origin, int target_var)
1907{
1908 struct variable v;
1909 struct variable *vp;
1910
1911 if (flocp != 0)
1912 v.fileinfo = *flocp;
1913 else
1914 v.fileinfo.filenm = 0;
1915
1916 if (!parse_variable_definition (&v, line))
1917 return 0;
1918
1919 vp = do_variable_definition (flocp, v.name, v.value,
1920 origin, v.flavor, target_var);
1921
1922 free (v.name);
1923
1924 return vp;
1925}
1926
1927
1928/* Print information for variable V, prefixing it with PREFIX. */
1929
1930static void
1931print_variable (const void *item, void *arg)
1932{
1933 const struct variable *v = item;
1934 const char *prefix = arg;
1935 const char *origin;
1936
1937 switch (v->origin)
1938 {
1939 case o_default:
1940 origin = _("default");
1941 break;
1942 case o_env:
1943 origin = _("environment");
1944 break;
1945 case o_file:
1946 origin = _("makefile");
1947 break;
1948 case o_env_override:
1949 origin = _("environment under -e");
1950 break;
1951 case o_command:
1952 origin = _("command line");
1953 break;
1954 case o_override:
1955 origin = _("`override' directive");
1956 break;
1957 case o_automatic:
1958 origin = _("automatic");
1959 break;
1960#ifdef CONFIG_WITH_LOCAL_VARIABLES
1961 case o_local:
1962 origin = _("`local' directive");
1963 break;
1964#endif
1965 case o_invalid:
1966 default:
1967 abort ();
1968 }
1969 fputs ("# ", stdout);
1970 fputs (origin, stdout);
1971 if (v->fileinfo.filenm)
1972 printf (_(" (from `%s', line %lu)"),
1973 v->fileinfo.filenm, v->fileinfo.lineno);
1974 putchar ('\n');
1975 fputs (prefix, stdout);
1976
1977 /* Is this a `define'? */
1978 if (v->recursive && strchr (v->value, '\n') != 0)
1979 printf ("define %s\n%s\nendef\n", v->name, v->value);
1980 else
1981 {
1982 register char *p;
1983
1984 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1985
1986 /* Check if the value is just whitespace. */
1987 p = next_token (v->value);
1988 if (p != v->value && *p == '\0')
1989 /* All whitespace. */
1990 printf ("$(subst ,,%s)", v->value);
1991 else if (v->recursive)
1992 fputs (v->value, stdout);
1993 else
1994 /* Double up dollar signs. */
1995 for (p = v->value; *p != '\0'; ++p)
1996 {
1997 if (*p == '$')
1998 putchar ('$');
1999 putchar (*p);
2000 }
2001 putchar ('\n');
2002 }
2003}
2004
2005
2006/* Print all the variables in SET. PREFIX is printed before
2007 the actual variable definitions (everything else is comments). */
2008
2009void
2010print_variable_set (struct variable_set *set, char *prefix)
2011{
2012 hash_map_arg (&set->table, print_variable, prefix);
2013
2014 fputs (_("# variable set hash-table stats:\n"), stdout);
2015 fputs ("# ", stdout);
2016 hash_print_stats (&set->table, stdout);
2017 putc ('\n', stdout);
2018}
2019
2020/* Print the data base of variables. */
2021
2022void
2023print_variable_data_base (void)
2024{
2025 puts (_("\n# Variables\n"));
2026
2027 print_variable_set (&global_variable_set, "");
2028
2029 puts (_("\n# Pattern-specific Variable Values"));
2030
2031 {
2032 struct pattern_var *p;
2033 int rules = 0;
2034
2035 for (p = pattern_vars; p != 0; p = p->next)
2036 {
2037 ++rules;
2038 printf ("\n%s :\n", p->target);
2039 print_variable (&p->variable, "# ");
2040 }
2041
2042 if (rules == 0)
2043 puts (_("\n# No pattern-specific variable values."));
2044 else
2045 printf (_("\n# %u pattern-specific variable values"), rules);
2046 }
2047}
2048
2049
2050/* Print all the local variables of FILE. */
2051
2052void
2053print_file_variables (const struct file *file)
2054{
2055 if (file->variables != 0)
2056 print_variable_set (file->variables->set, "# ");
2057}
2058
2059#ifdef WINDOWS32
2060void
2061sync_Path_environment (void)
2062{
2063 char *path = allocated_variable_expand ("$(PATH)");
2064 static char *environ_path = NULL;
2065
2066 if (!path)
2067 return;
2068
2069 /*
2070 * If done this before, don't leak memory unnecessarily.
2071 * Free the previous entry before allocating new one.
2072 */
2073 if (environ_path)
2074 free (environ_path);
2075
2076 /*
2077 * Create something WINDOWS32 world can grok
2078 */
2079 convert_Path_to_windows32 (path, ';');
2080 environ_path = xstrdup (concat ("PATH", "=", path));
2081 putenv (environ_path);
2082 free (path);
2083}
2084#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette