VirtualBox

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

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

variable.c: Fixed missed CONFIG_WITH_VALUE_LENGTH case in handle_special_var (.VARIABLES).

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

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