VirtualBox

source: kBuild/trunk/src/gmakenew/variable.c@ 903

Last change on this file since 903 was 903, checked in by bird, 18 years ago

Merged with the 2007-05-23 CVS. Added rsort and fixed a couple of windows build issues.

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