VirtualBox

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

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

don't free twice.

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