VirtualBox

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

Last change on this file since 1968 was 1968, checked in by bird, 16 years ago

kmk: main.c, variable.c: shell_var must have a valid name length or we'll get duplicates in target_environment(). fixed strcache issue with it as well.

  • Property svn:eol-style set to native
File size: 73.0 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 assert (strcache2_is_cached (&variable_strcache, v->name));
1536 new_slot = (struct variable **) hash_find_slot_strcached (&table, v);
1537#endif /* CONFIG_WITH_STRCACHE2 */
1538 if (HASH_VACANT (*new_slot))
1539 hash_insert_at (&table, v, new_slot);
1540 }
1541 }
1542
1543#ifndef CONFIG_WITH_STRCACHE2
1544 makelevel_key.name = MAKELEVEL_NAME;
1545 makelevel_key.length = MAKELEVEL_LENGTH;
1546 hash_delete (&table, &makelevel_key);
1547#else /* CONFIG_WITH_STRCACHE2 */
1548 /* lookup the name in the string case, if it's not there it won't
1549 be in any of the sets either. */
1550 cached_name = strcache2_lookup (&variable_strcache,
1551 MAKELEVEL_NAME, MAKELEVEL_LENGTH);
1552 if (cached_name)
1553 {
1554 makelevel_key.name = cached_name;
1555 makelevel_key.length = MAKELEVEL_LENGTH;
1556 hash_delete_strcached (&table, &makelevel_key);
1557 }
1558#endif /* CONFIG_WITH_STRCACHE2 */
1559
1560 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
1561
1562 v_slot = (struct variable **) table.ht_vec;
1563 v_end = v_slot + table.ht_size;
1564 for ( ; v_slot < v_end; v_slot++)
1565 if (! HASH_VACANT (*v_slot))
1566 {
1567 struct variable *v = *v_slot;
1568
1569 /* If V is recursively expanded and didn't come from the environment,
1570 expand its value. If it came from the environment, it should
1571 go back into the environment unchanged. */
1572 if (v->recursive
1573 && v->origin != o_env && v->origin != o_env_override)
1574 {
1575#ifndef CONFIG_WITH_VALUE_LENGTH
1576 char *value = recursively_expand_for_file (v, file);
1577#else
1578 char *value = recursively_expand_for_file (v, file, NULL);
1579#endif
1580#ifdef WINDOWS32
1581 if (strcmp(v->name, "Path") == 0 ||
1582 strcmp(v->name, "PATH") == 0)
1583 convert_Path_to_windows32(value, ';');
1584#endif
1585 *result++ = xstrdup (concat (v->name, "=", value));
1586 free (value);
1587 }
1588 else
1589 {
1590#ifdef WINDOWS32
1591 if (strcmp(v->name, "Path") == 0 ||
1592 strcmp(v->name, "PATH") == 0)
1593 convert_Path_to_windows32(v->value, ';');
1594#endif
1595 *result++ = xstrdup (concat (v->name, "=", v->value));
1596 }
1597 }
1598
1599 *result = xmalloc (100);
1600 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
1601 *++result = 0;
1602
1603 hash_free (&table, 0);
1604
1605 return result_0;
1606}
1607
1608
1609#ifdef CONFIG_WITH_VALUE_LENGTH
1610/* Worker function for do_variable_definition_append() and
1611 append_expanded_string_to_variable().
1612 The APPEND argument indicates whether it's an append or prepend operation. */
1613void append_string_to_variable (struct variable *v, const char *value, unsigned int value_len, int append)
1614{
1615 /* The previous definition of the variable was recursive.
1616 The new value is the unexpanded old and new values. */
1617 unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
1618 int done_1st_prepend_copy = 0;
1619
1620 /* Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
1621 if (!value_len)
1622 return;
1623
1624 /* adjust the size. */
1625 if ((unsigned)v->value_alloc_len <= new_value_len + 1)
1626 {
1627 v->value_alloc_len *= 2;
1628 if ((unsigned)v->value_alloc_len < new_value_len + 1)
1629 v->value_alloc_len = (new_value_len + 1 + value_len + 0x7f) + ~0x7fU;
1630# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1631 if ((append || !v->value_length) && !v->rdonly_val)
1632# else
1633 if (append || !v->value_length)
1634# endif
1635 v->value = xrealloc (v->value, v->value_alloc_len);
1636 else
1637 {
1638 /* avoid the extra memcpy the xrealloc may have to do */
1639 char *new_buf = xmalloc (v->value_alloc_len);
1640 memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
1641 done_1st_prepend_copy = 1;
1642# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1643 if (v->rdonly_val)
1644 v->rdonly_val = 0;
1645 else
1646# endif
1647 free (v->value);
1648 v->value = new_buf;
1649 }
1650 }
1651
1652 /* insert the new bits */
1653 if (v->value_length != 0)
1654 {
1655 if (append)
1656 {
1657 v->value[v->value_length] = ' ';
1658 memcpy (&v->value[v->value_length + 1], value, value_len + 1);
1659 }
1660 else
1661 {
1662 if (!done_1st_prepend_copy)
1663 memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
1664 v->value[value_len] = ' ';
1665 memcpy (v->value, value, value_len);
1666 }
1667 }
1668 else
1669 memcpy (v->value, value, value_len + 1);
1670 v->value_length = new_value_len;
1671}
1672
1673static struct variable *
1674do_variable_definition_append (const struct floc *flocp, struct variable *v,
1675 const char *value, unsigned int value_len,
1676 int simple_value, enum variable_origin origin,
1677 int append)
1678{
1679 if (env_overrides && origin == o_env)
1680 origin = o_env_override;
1681
1682 if (env_overrides && v->origin == o_env)
1683 /* V came from in the environment. Since it was defined
1684 before the switches were parsed, it wasn't affected by -e. */
1685 v->origin = o_env_override;
1686
1687 /* A variable of this name is already defined.
1688 If the old definition is from a stronger source
1689 than this one, don't redefine it. */
1690 if ((int) origin < (int) v->origin)
1691 return v;
1692 v->origin = origin;
1693
1694 /* location */
1695 if (flocp != 0)
1696 v->fileinfo = *flocp;
1697
1698 /* The juicy bits, append the specified value to the variable
1699 This is a heavily exercised code path in kBuild. */
1700 if (value_len == ~0U)
1701 value_len = strlen (value);
1702 if (v->recursive || simple_value)
1703 append_string_to_variable (v, value, value_len, append);
1704 else
1705 /* The previous definition of the variable was simple.
1706 The new value comes from the old value, which was expanded
1707 when it was set; and from the expanded new value. */
1708 append_expanded_string_to_variable (v, value, value_len, append);
1709
1710 /* update the variable */
1711 return v;
1712}
1713#endif /* CONFIG_WITH_VALUE_LENGTH */
1714
1715
1716/* Given a variable, a value, and a flavor, define the variable.
1717 See the try_variable_definition() function for details on the parameters. */
1718
1719struct variable *
1720#ifndef CONFIG_WITH_VALUE_LENGTH
1721do_variable_definition (const struct floc *flocp, const char *varname,
1722 const char *value, enum variable_origin origin,
1723 enum variable_flavor flavor, int target_var)
1724#else /* CONFIG_WITH_VALUE_LENGTH */
1725do_variable_definition_2 (const struct floc *flocp,
1726 const char *varname, const char *value,
1727 unsigned int value_len, int simple_value,
1728 char *free_value,
1729 enum variable_origin origin,
1730 enum variable_flavor flavor,
1731 int target_var)
1732#endif /* CONFIG_WITH_VALUE_LENGTH */
1733{
1734 const char *p;
1735 char *alloc_value = NULL;
1736 struct variable *v;
1737 int append = 0;
1738 int conditional = 0;
1739 const size_t varname_len = strlen (varname); /* bird */
1740#ifdef CONFIG_WITH_VALUE_LENGTH
1741 assert (value_len == ~0U || value_len == strlen (value));
1742#endif
1743
1744 /* Calculate the variable's new value in VALUE. */
1745
1746 switch (flavor)
1747 {
1748 default:
1749 case f_bogus:
1750 /* Should not be possible. */
1751 abort ();
1752 case f_simple:
1753 /* A simple variable definition "var := value". Expand the value.
1754 We have to allocate memory since otherwise it'll clobber the
1755 variable buffer, and we may still need that if we're looking at a
1756 target-specific variable. */
1757#ifndef CONFIG_WITH_VALUE_LENGTH
1758 p = alloc_value = allocated_variable_expand (value);
1759#else /* CONFIG_WITH_VALUE_LENGTH */
1760 if (!simple_value)
1761 p = alloc_value = allocated_variable_expand_2 (value, value_len, &value_len);
1762 else
1763 {
1764 if (value_len == ~0U)
1765 value_len = strlen (value);
1766 if (!free_value)
1767 p = alloc_value = savestring (value, value_len);
1768 else
1769 {
1770 assert (value == free_value);
1771 p = alloc_value = free_value;
1772 free_value = 0;
1773 }
1774 }
1775#endif /* CONFIG_WITH_VALUE_LENGTH */
1776 break;
1777 case f_conditional:
1778 /* A conditional variable definition "var ?= value".
1779 The value is set IFF the variable is not defined yet. */
1780 v = lookup_variable (varname, varname_len);
1781 if (v)
1782 return v;
1783
1784 conditional = 1;
1785 flavor = f_recursive;
1786 /* FALLTHROUGH */
1787 case f_recursive:
1788 /* A recursive variable definition "var = value".
1789 The value is used verbatim. */
1790 p = value;
1791 break;
1792#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1793 case f_append:
1794 case f_prepend:
1795 {
1796 const enum variable_flavor org_flavor = flavor;
1797#else
1798 case f_append:
1799 {
1800#endif
1801
1802#ifdef CONFIG_WITH_LOCAL_VARIABLES
1803 /* If we have += but we're in a target or local variable context,
1804 we want to append only with other variables in the context of
1805 this target. */
1806 if (target_var || origin == o_local)
1807#else
1808 /* If we have += but we're in a target variable context, we want to
1809 append only with other variables in the context of this target. */
1810 if (target_var)
1811#endif
1812 {
1813 append = 1;
1814 v = lookup_variable_in_set (varname, varname_len,
1815 current_variable_set_list->set);
1816
1817 /* Don't append from the global set if a previous non-appending
1818 target-specific variable definition exists. */
1819 if (v && !v->append)
1820 append = 0;
1821 }
1822 else
1823 v = lookup_variable (varname, varname_len);
1824
1825 if (v == 0)
1826 {
1827 /* There was no old value.
1828 This becomes a normal recursive definition. */
1829 p = value;
1830 flavor = f_recursive;
1831 }
1832 else
1833 {
1834#ifdef CONFIG_WITH_VALUE_LENGTH
1835 v->append = append;
1836 v = do_variable_definition_append (flocp, v, value, value_len,
1837 simple_value, origin,
1838# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1839 org_flavor == f_append);
1840# else
1841 1);
1842# endif
1843 if (free_value)
1844 free (free_value);
1845 return v;
1846#else /* !CONFIG_WITH_VALUE_LENGTH */
1847
1848 /* Paste the old and new values together in VALUE. */
1849
1850 unsigned int oldlen, vallen;
1851 const char *val;
1852 char *tp;
1853
1854 val = value;
1855 if (v->recursive)
1856 /* The previous definition of the variable was recursive.
1857 The new value is the unexpanded old and new values. */
1858 flavor = f_recursive;
1859 else
1860 /* The previous definition of the variable was simple.
1861 The new value comes from the old value, which was expanded
1862 when it was set; and from the expanded new value. Allocate
1863 memory for the expansion as we may still need the rest of the
1864 buffer if we're looking at a target-specific variable. */
1865 val = alloc_value = allocated_variable_expand (val);
1866
1867 oldlen = strlen (v->value);
1868 vallen = strlen (val);
1869 tp = alloca (oldlen + 1 + vallen + 1);
1870# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1871 if (org_flavor == f_prepend)
1872 {
1873 memcpy (tp, val, vallen);
1874 tp[oldlen] = ' ';
1875 memcpy (&tp[oldlen + 1], v->value, oldlen + 1);
1876 }
1877 else
1878# endif /* CONFIG_WITH_PREPEND_ASSIGNMENT */
1879 {
1880 memcpy (tp, v->value, oldlen);
1881 tp[oldlen] = ' ';
1882 memcpy (&tp[oldlen + 1], val, vallen + 1);
1883 }
1884 p = tp;
1885#endif /* !CONFIG_WITH_VALUE_LENGTH */
1886 }
1887 }
1888 }
1889
1890#ifdef __MSDOS__
1891 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1892 non-Unix systems don't conform to this default configuration (in
1893 fact, most of them don't even have `/bin'). On the other hand,
1894 $SHELL in the environment, if set, points to the real pathname of
1895 the shell.
1896 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1897 the Makefile override $SHELL from the environment. But first, we
1898 look for the basename of the shell in the directory where SHELL=
1899 points, and along the $PATH; if it is found in any of these places,
1900 we define $SHELL to be the actual pathname of the shell. Thus, if
1901 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1902 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1903 defining SHELL to be "d:/unix/bash.exe". */
1904 if ((origin == o_file || origin == o_override)
1905 && strcmp (varname, "SHELL") == 0)
1906 {
1907 PATH_VAR (shellpath);
1908 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1909
1910 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1911 if (__dosexec_find_on_path (p, NULL, shellpath))
1912 {
1913 char *tp;
1914
1915 for (tp = shellpath; *tp; tp++)
1916 if (*tp == '\\')
1917 *tp = '/';
1918
1919 v = define_variable_loc (varname, varname_len,
1920 shellpath, origin, flavor == f_recursive,
1921 flocp);
1922 }
1923 else
1924 {
1925 const char *shellbase, *bslash;
1926 struct variable *pathv = lookup_variable ("PATH", 4);
1927 char *path_string;
1928 char *fake_env[2];
1929 size_t pathlen = 0;
1930
1931 shellbase = strrchr (p, '/');
1932 bslash = strrchr (p, '\\');
1933 if (!shellbase || bslash > shellbase)
1934 shellbase = bslash;
1935 if (!shellbase && p[1] == ':')
1936 shellbase = p + 1;
1937 if (shellbase)
1938 shellbase++;
1939 else
1940 shellbase = p;
1941
1942 /* Search for the basename of the shell (with standard
1943 executable extensions) along the $PATH. */
1944 if (pathv)
1945 pathlen = strlen (pathv->value);
1946 path_string = xmalloc (5 + pathlen + 2 + 1);
1947 /* On MSDOS, current directory is considered as part of $PATH. */
1948 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1949 fake_env[0] = path_string;
1950 fake_env[1] = 0;
1951 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1952 {
1953 char *tp;
1954
1955 for (tp = shellpath; *tp; tp++)
1956 if (*tp == '\\')
1957 *tp = '/';
1958
1959 v = define_variable_loc (varname, varname_len,
1960 shellpath, origin,
1961 flavor == f_recursive, flocp);
1962 }
1963 else
1964 v = lookup_variable (varname, varname_len);
1965
1966 free (path_string);
1967 }
1968 }
1969 else
1970#endif /* __MSDOS__ */
1971#ifdef WINDOWS32
1972 if ( varname_len == sizeof("SHELL") - 1 /* bird */
1973 && (origin == o_file || origin == o_override || origin == o_command)
1974 && streq (varname, "SHELL"))
1975 {
1976 extern char *default_shell;
1977
1978 /* Call shell locator function. If it returns TRUE, then
1979 set no_default_sh_exe to indicate sh was found and
1980 set new value for SHELL variable. */
1981
1982 if (find_and_set_default_shell (p))
1983 {
1984 v = define_variable_in_set (varname, varname_len, default_shell,
1985# ifdef CONFIG_WITH_VALUE_LENGTH
1986 ~0U, 1 /* duplicate_value */,
1987# endif
1988 origin, flavor == f_recursive,
1989 (target_var
1990 ? current_variable_set_list->set
1991 : NULL),
1992 flocp);
1993 no_default_sh_exe = 0;
1994 }
1995 else
1996 v = lookup_variable (varname, varname_len);
1997 }
1998 else
1999#endif
2000
2001 /* If we are defining variables inside an $(eval ...), we might have a
2002 different variable context pushed, not the global context (maybe we're
2003 inside a $(call ...) or something. Since this function is only ever
2004 invoked in places where we want to define globally visible variables,
2005 make sure we define this variable in the global set. */
2006
2007 v = define_variable_in_set (varname, varname_len, p,
2008#ifdef CONFIG_WITH_VALUE_LENGTH
2009 value_len, !alloc_value,
2010#endif
2011 origin, flavor == f_recursive,
2012#ifdef CONFIG_WITH_LOCAL_VARIABLES
2013 (target_var || origin == o_local
2014#else
2015 (target_var
2016#endif
2017 ? current_variable_set_list->set : NULL),
2018 flocp);
2019 v->append = append;
2020 v->conditional = conditional;
2021
2022#ifndef CONFIG_WITH_VALUE_LENGTH
2023 if (alloc_value)
2024 free (alloc_value);
2025#else
2026 if (free_value)
2027 free (free_value);
2028#endif
2029
2030 return v;
2031}
2032
2033
2034/* Try to interpret LINE (a null-terminated string) as a variable definition.
2035
2036 ORIGIN may be o_file, o_override, o_env, o_env_override,
2037 or o_command specifying that the variable definition comes
2038 from a makefile, an override directive, the environment with
2039 or without the -e switch, or the command line.
2040
2041 See the comments for parse_variable_definition().
2042
2043 If LINE was recognized as a variable definition, a pointer to its `struct
2044 variable' is returned. If LINE is not a variable definition, NULL is
2045 returned. */
2046
2047struct variable *
2048#ifndef CONFIG_WITH_VALUE_LENGTH
2049parse_variable_definition (struct variable *v, char *line)
2050#else
2051parse_variable_definition (struct variable *v, char *line, char *eos)
2052#endif
2053{
2054 register int c;
2055 register char *p = line;
2056 register char *beg;
2057 register char *end;
2058 enum variable_flavor flavor = f_bogus;
2059#ifndef CONFIG_WITH_VALUE_LENGTH
2060 char *name;
2061#endif
2062
2063 while (1)
2064 {
2065 c = *p++;
2066 if (c == '\0' || c == '#')
2067 return 0;
2068 if (c == '=')
2069 {
2070 end = p - 1;
2071 flavor = f_recursive;
2072 break;
2073 }
2074 else if (c == ':')
2075 if (*p == '=')
2076 {
2077 end = p++ - 1;
2078 flavor = f_simple;
2079 break;
2080 }
2081 else
2082 /* A colon other than := is a rule line, not a variable defn. */
2083 return 0;
2084 else if (c == '+' && *p == '=')
2085 {
2086 end = p++ - 1;
2087 flavor = f_append;
2088 break;
2089 }
2090#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2091 else if (c == '<' && *p == '=')
2092 {
2093 end = p++ - 1;
2094 flavor = f_prepend;
2095 break;
2096 }
2097#endif
2098 else if (c == '?' && *p == '=')
2099 {
2100 end = p++ - 1;
2101 flavor = f_conditional;
2102 break;
2103 }
2104 else if (c == '$')
2105 {
2106 /* This might begin a variable expansion reference. Make sure we
2107 don't misrecognize chars inside the reference as =, := or +=. */
2108 char closeparen;
2109 int count;
2110 c = *p++;
2111 if (c == '(')
2112 closeparen = ')';
2113 else if (c == '{')
2114 closeparen = '}';
2115 else
2116 continue; /* Nope. */
2117
2118 /* P now points past the opening paren or brace.
2119 Count parens or braces until it is matched. */
2120 count = 0;
2121 for (; *p != '\0'; ++p)
2122 {
2123 if (*p == c)
2124 ++count;
2125 else if (*p == closeparen && --count < 0)
2126 {
2127 ++p;
2128 break;
2129 }
2130 }
2131 }
2132 }
2133 v->flavor = flavor;
2134
2135 beg = next_token (line);
2136 while (end > beg && isblank ((unsigned char)end[-1]))
2137 --end;
2138 p = next_token (p);
2139 v->value = p;
2140#ifdef CONFIG_WITH_VALUE_LENGTH
2141 v->value_alloc_len = -1;
2142 v->value_length = eos != NULL ? eos - p : -1;
2143 assert (eos == NULL || strchr (p, '\0') == eos);
2144# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2145 v->rdonly_val = 0;
2146# endif
2147#endif
2148
2149 /* Expand the name, so "$(foo)bar = baz" works. */
2150#ifndef CONFIG_WITH_VALUE_LENGTH
2151 name = alloca (end - beg + 1);
2152 memcpy (name, beg, end - beg);
2153 name[end - beg] = '\0';
2154 v->name = allocated_variable_expand (name);
2155#else /* CONFIG_WITH_VALUE_LENGTH */
2156 v->name = allocated_variable_expand_2 (beg, end - beg, NULL);
2157#endif /* CONFIG_WITH_VALUE_LENGTH */
2158
2159 if (v->name[0] == '\0')
2160 fatal (&v->fileinfo, _("empty variable name"));
2161
2162 return v;
2163}
2164
2165
2166/* Try to interpret LINE (a null-terminated string) as a variable definition.
2167
2168 ORIGIN may be o_file, o_override, o_env, o_env_override, o_local,
2169 or o_command specifying that the variable definition comes
2170 from a makefile, an override directive, the environment with
2171 or without the -e switch, or the command line.
2172
2173 See the comments for parse_variable_definition().
2174
2175 If LINE was recognized as a variable definition, a pointer to its `struct
2176 variable' is returned. If LINE is not a variable definition, NULL is
2177 returned. */
2178
2179struct variable *
2180#ifndef CONFIG_WITH_VALUE_LENGTH
2181try_variable_definition (const struct floc *flocp, char *line,
2182 enum variable_origin origin, int target_var)
2183#else
2184try_variable_definition (const struct floc *flocp, char *line, char *eos,
2185 enum variable_origin origin, int target_var)
2186#endif
2187{
2188 struct variable v;
2189 struct variable *vp;
2190
2191 if (flocp != 0)
2192 v.fileinfo = *flocp;
2193 else
2194 v.fileinfo.filenm = 0;
2195
2196#ifndef CONFIG_WITH_VALUE_LENGTH
2197 if (!parse_variable_definition (&v, line))
2198 return 0;
2199
2200 vp = do_variable_definition (flocp, v.name, v.value,
2201 origin, v.flavor, target_var);
2202#else
2203 if (!parse_variable_definition (&v, line, eos))
2204 return 0;
2205
2206 vp = do_variable_definition_2 (flocp, v.name, v.value,
2207 v.value_length != -1 ? (unsigned int)v.value_length : ~0U, /* FIXME */
2208 0, NULL, origin, v.flavor, target_var);
2209#endif
2210
2211#ifndef CONFIG_WITH_STRCACHE2
2212 free (v.name);
2213#else
2214 free ((char *)v.name);
2215#endif
2216
2217 return vp;
2218}
2219
2220
2221/* Print information for variable V, prefixing it with PREFIX. */
2222
2223static void
2224print_variable (const void *item, void *arg)
2225{
2226 const struct variable *v = item;
2227 const char *prefix = arg;
2228 const char *origin;
2229
2230 switch (v->origin)
2231 {
2232 case o_default:
2233 origin = _("default");
2234 break;
2235 case o_env:
2236 origin = _("environment");
2237 break;
2238 case o_file:
2239 origin = _("makefile");
2240 break;
2241 case o_env_override:
2242 origin = _("environment under -e");
2243 break;
2244 case o_command:
2245 origin = _("command line");
2246 break;
2247 case o_override:
2248 origin = _("`override' directive");
2249 break;
2250 case o_automatic:
2251 origin = _("automatic");
2252 break;
2253#ifdef CONFIG_WITH_LOCAL_VARIABLES
2254 case o_local:
2255 origin = _("`local' directive");
2256 break;
2257#endif
2258 case o_invalid:
2259 default:
2260 abort ();
2261 }
2262 fputs ("# ", stdout);
2263 fputs (origin, stdout);
2264 if (v->fileinfo.filenm)
2265 printf (_(" (from `%s', line %lu)"),
2266 v->fileinfo.filenm, v->fileinfo.lineno);
2267 putchar ('\n');
2268 fputs (prefix, stdout);
2269
2270 /* Is this a `define'? */
2271 if (v->recursive && strchr (v->value, '\n') != 0)
2272 printf ("define %s\n%s\nendef\n", v->name, v->value);
2273 else
2274 {
2275 register char *p;
2276
2277 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
2278
2279 /* Check if the value is just whitespace. */
2280 p = next_token (v->value);
2281 if (p != v->value && *p == '\0')
2282 /* All whitespace. */
2283 printf ("$(subst ,,%s)", v->value);
2284 else if (v->recursive)
2285 fputs (v->value, stdout);
2286 else
2287 /* Double up dollar signs. */
2288 for (p = v->value; *p != '\0'; ++p)
2289 {
2290 if (*p == '$')
2291 putchar ('$');
2292 putchar (*p);
2293 }
2294 putchar ('\n');
2295 }
2296}
2297
2298
2299/* Print all the variables in SET. PREFIX is printed before
2300 the actual variable definitions (everything else is comments). */
2301
2302void
2303print_variable_set (struct variable_set *set, char *prefix)
2304{
2305 hash_map_arg (&set->table, print_variable, prefix);
2306
2307 fputs (_("# variable set hash-table stats:\n"), stdout);
2308 fputs ("# ", stdout);
2309 hash_print_stats (&set->table, stdout);
2310 putc ('\n', stdout);
2311}
2312
2313/* Print the data base of variables. */
2314
2315void
2316print_variable_data_base (void)
2317{
2318 puts (_("\n# Variables\n"));
2319
2320 print_variable_set (&global_variable_set, "");
2321
2322 puts (_("\n# Pattern-specific Variable Values"));
2323
2324 {
2325 struct pattern_var *p;
2326 int rules = 0;
2327
2328 for (p = pattern_vars; p != 0; p = p->next)
2329 {
2330 ++rules;
2331 printf ("\n%s :\n", p->target);
2332 print_variable (&p->variable, "# ");
2333 }
2334
2335 if (rules == 0)
2336 puts (_("\n# No pattern-specific variable values."));
2337 else
2338 printf (_("\n# %u pattern-specific variable values"), rules);
2339 }
2340
2341#ifdef CONFIG_WITH_STRCACHE2
2342 strcache2_print_stats (&variable_strcache, "# ");
2343#endif
2344}
2345
2346#ifdef CONFIG_WITH_PRINT_STATS_SWITCH
2347void
2348print_variable_stats (void)
2349{
2350 fputs (_("\n# Global variable hash-table stats:\n# "), stdout);
2351 hash_print_stats (&global_variable_set.table, stdout);
2352 fputs ("\n", stdout);
2353}
2354#endif
2355
2356/* Print all the local variables of FILE. */
2357
2358void
2359print_file_variables (const struct file *file)
2360{
2361 if (file->variables != 0)
2362 print_variable_set (file->variables->set, "# ");
2363}
2364
2365#ifdef WINDOWS32
2366void
2367sync_Path_environment (void)
2368{
2369 char *path = allocated_variable_expand ("$(PATH)");
2370 static char *environ_path = NULL;
2371
2372 if (!path)
2373 return;
2374
2375 /*
2376 * If done this before, don't leak memory unnecessarily.
2377 * Free the previous entry before allocating new one.
2378 */
2379 if (environ_path)
2380 free (environ_path);
2381
2382 /*
2383 * Create something WINDOWS32 world can grok
2384 */
2385 convert_Path_to_windows32 (path, ';');
2386 environ_path = xstrdup (concat ("PATH", "=", path));
2387 putenv (environ_path);
2388 free (path);
2389}
2390#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