VirtualBox

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

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

Merged in current GNU Make code (CVS from 2008-10-28). Ref #55.

  • Property svn:eol-style set to native
File size: 74.4 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, 2007 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 3 of the License, or (at your option) any later
10version.
11
12GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License along with
17this program. If not, see <http://www.gnu.org/licenses/>. */
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 *
390lookup_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) ? lookup_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) ? lookup_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) ? lookup_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) ? lookup_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) ? lookup_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) ? lookup_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) ? lookup_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 ? lookup_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#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
923 if (!v->rdonly_val)
924#endif
925 free (v->value);
926}
927
928void
929free_variable_set (struct variable_set_list *list)
930{
931 hash_map (&list->set->table, free_variable_name_and_value);
932#ifndef CONFIG_WITH_ALLOC_CACHES
933 hash_free (&list->set->table, 1);
934 free (list->set);
935 free (list);
936#else
937 hash_free_cached (&list->set->table, 1, &variable_cache);
938 alloccache_free (&variable_set_cache, list->set);
939 alloccache_free (&variable_set_list_cache, list);
940#endif
941}
942
943/* Create a new variable set and push it on the current setlist.
944 If we're pushing a global scope (that is, the current scope is the global
945 scope) then we need to "push" it the other way: file variable sets point
946 directly to the global_setlist so we need to replace that with the new one.
947 */
948
949struct variable_set_list *
950push_new_variable_scope (void)
951{
952 current_variable_set_list = create_new_variable_set();
953 if (current_variable_set_list->next == &global_setlist)
954 {
955 /* It was the global, so instead of new -> &global we want to replace
956 &global with the new one and have &global -> new, with current still
957 pointing to &global */
958 struct variable_set *set = current_variable_set_list->set;
959 current_variable_set_list->set = global_setlist.set;
960 global_setlist.set = set;
961 current_variable_set_list->next = global_setlist.next;
962 global_setlist.next = current_variable_set_list;
963 current_variable_set_list = &global_setlist;
964 }
965 return (current_variable_set_list);
966}
967
968void
969pop_variable_scope (void)
970{
971 struct variable_set_list *setlist;
972 struct variable_set *set;
973
974 /* Can't call this if there's no scope to pop! */
975 assert(current_variable_set_list->next != NULL);
976
977 if (current_variable_set_list != &global_setlist)
978 {
979 /* We're not pointing to the global setlist, so pop this one. */
980 setlist = current_variable_set_list;
981 set = setlist->set;
982 current_variable_set_list = setlist->next;
983 }
984 else
985 {
986 /* This set is the one in the global_setlist, but there is another global
987 set beyond that. We want to copy that set to global_setlist, then
988 delete what used to be in global_setlist. */
989 setlist = global_setlist.next;
990 set = global_setlist.set;
991 global_setlist.set = setlist->set;
992 global_setlist.next = setlist->next;
993 }
994
995 /* Free the one we no longer need. */
996#ifndef CONFIG_WITH_ALLOC_CACHES
997 free (setlist);
998 hash_map (&set->table, free_variable_name_and_value);
999 hash_free (&set->table, 1);
1000 free (set);
1001#else
1002 alloccache_free (&variable_set_list_cache, setlist);
1003 hash_map (&set->table, free_variable_name_and_value);
1004 hash_free_cached (&set->table, 1, &variable_cache);
1005 alloccache_free (&variable_set_cache, set);
1006#endif
1007}
1008
1009
1010/* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
1011
1012static void
1013merge_variable_sets (struct variable_set *to_set,
1014 struct variable_set *from_set)
1015{
1016 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
1017 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
1018
1019 for ( ; from_var_slot < from_var_end; from_var_slot++)
1020 if (! HASH_VACANT (*from_var_slot))
1021 {
1022 struct variable *from_var = *from_var_slot;
1023 struct variable **to_var_slot
1024#ifndef CONFIG_WITH_STRCACHE2
1025 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
1026#else /* CONFIG_WITH_STRCACHE2 */
1027 = (struct variable **) hash_find_slot_strcached (&to_set->table,
1028 *from_var_slot);
1029#endif /* CONFIG_WITH_STRCACHE2 */
1030 if (HASH_VACANT (*to_var_slot))
1031 hash_insert_at (&to_set->table, from_var, to_var_slot);
1032 else
1033 {
1034 /* GKM FIXME: delete in from_set->table */
1035 free (from_var->value);
1036 free (from_var);
1037 }
1038 }
1039}
1040
1041/* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
1042
1043void
1044merge_variable_set_lists (struct variable_set_list **setlist0,
1045 struct variable_set_list *setlist1)
1046{
1047 struct variable_set_list *to = *setlist0;
1048 struct variable_set_list *last0 = 0;
1049
1050 /* If there's nothing to merge, stop now. */
1051 if (!setlist1)
1052 return;
1053
1054 /* This loop relies on the fact that all setlists terminate with the global
1055 setlist (before NULL). If that's not true, arguably we SHOULD die. */
1056 if (to)
1057 while (setlist1 != &global_setlist && to != &global_setlist)
1058 {
1059 struct variable_set_list *from = setlist1;
1060 setlist1 = setlist1->next;
1061
1062 merge_variable_sets (to->set, from->set);
1063
1064 last0 = to;
1065 to = to->next;
1066 }
1067
1068 if (setlist1 != &global_setlist)
1069 {
1070 if (last0 == 0)
1071 *setlist0 = setlist1;
1072 else
1073 last0->next = setlist1;
1074 }
1075}
1076
1077
1078/* Define the automatic variables, and record the addresses
1079 of their structures so we can change their values quickly. */
1080
1081void
1082define_automatic_variables (void)
1083{
1084#if defined(WINDOWS32) || defined(__EMX__)
1085 extern char* default_shell;
1086#else
1087 extern char default_shell[];
1088#endif
1089 register struct variable *v;
1090#ifndef KMK
1091 char buf[200];
1092#else
1093 char buf[1024];
1094 const char *val;
1095 struct variable *envvar1;
1096 struct variable *envvar2;
1097#endif
1098
1099 sprintf (buf, "%u", makelevel);
1100 (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
1101
1102 sprintf (buf, "%s%s%s",
1103 version_string,
1104 (remote_description == 0 || remote_description[0] == '\0')
1105 ? "" : "-",
1106 (remote_description == 0 || remote_description[0] == '\0')
1107 ? "" : remote_description);
1108#ifndef KMK
1109 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
1110#else /* KMK */
1111
1112 /* Define KMK_VERSION to indicate kMk. */
1113 (void) define_variable ("KMK_VERSION", 11, buf, o_default, 0);
1114
1115 /* Define KBUILD_VERSION* */
1116 sprintf (buf, "%d", KBUILD_VERSION_MAJOR);
1117 define_variable ("KBUILD_VERSION_MAJOR", sizeof ("KBUILD_VERSION_MAJOR") - 1,
1118 buf, o_default, 0);
1119 sprintf (buf, "%d", KBUILD_VERSION_MINOR);
1120 define_variable ("KBUILD_VERSION_MINOR", sizeof("KBUILD_VERSION_MINOR") - 1,
1121 buf, o_default, 0);
1122 sprintf (buf, "%d", KBUILD_VERSION_PATCH);
1123 define_variable ("KBUILD_VERSION_PATCH", sizeof ("KBUILD_VERSION_PATCH") - 1,
1124 buf, o_default, 0);
1125 sprintf (buf, "%d", KBUILD_SVN_REV);
1126 define_variable ("KBUILD_KMK_REVISION", sizeof ("KBUILD_KMK_REVISION") - 1,
1127 buf, o_default, 0);
1128
1129 sprintf (buf, "%d.%d.%d-r%d", KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR,
1130 KBUILD_VERSION_PATCH, KBUILD_SVN_REV);
1131 define_variable ("KBUILD_VERSION", sizeof ("KBUILD_VERSION") - 1,
1132 buf, o_default, 0);
1133
1134 /* The host defaults. The BUILD_* stuff will be replaced by KBUILD_* soon. */
1135 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST"));
1136 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM"));
1137 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST;
1138 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1139 error (NULL, _("KBUILD_HOST and BUILD_PLATFORM differs, using KBUILD_HOST=%s."), val);
1140 if (!envvar1)
1141 define_variable ("KBUILD_HOST", sizeof ("KBUILD_HOST") - 1,
1142 val, o_default, 0);
1143 if (!envvar2)
1144 define_variable ("BUILD_PLATFORM", sizeof ("BUILD_PLATFORM") - 1,
1145 val, o_default, 0);
1146
1147 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_ARCH"));
1148 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_ARCH"));
1149 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_ARCH;
1150 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1151 error (NULL, _("KBUILD_HOST_ARCH and BUILD_PLATFORM_ARCH differs, using KBUILD_HOST_ARCH=%s."), val);
1152 if (!envvar1)
1153 define_variable ("KBUILD_HOST_ARCH", sizeof ("KBUILD_HOST_ARCH") - 1,
1154 val, o_default, 0);
1155 if (!envvar2)
1156 define_variable ("BUILD_PLATFORM_ARCH", sizeof ("BUILD_PLATFORM_ARCH") - 1,
1157 val, o_default, 0);
1158
1159 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_CPU"));
1160 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_CPU"));
1161 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_CPU;
1162 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1163 error (NULL, _("KBUILD_HOST_CPU and BUILD_PLATFORM_CPU differs, using KBUILD_HOST_CPU=%s."), val);
1164 if (!envvar1)
1165 define_variable ("KBUILD_HOST_CPU", sizeof ("KBUILD_HOST_CPU") - 1,
1166 val, o_default, 0);
1167 if (!envvar2)
1168 define_variable ("BUILD_PLATFORM_CPU", sizeof ("BUILD_PLATFORM_CPU") - 1,
1169 val, o_default, 0);
1170
1171 /* The kBuild locations. */
1172 define_variable ("KBUILD_PATH", sizeof ("KBUILD_PATH") - 1,
1173 get_kbuild_path (), o_default, 0);
1174 define_variable ("KBUILD_BIN_PATH", sizeof ("KBUILD_BIN_PATH") - 1,
1175 get_kbuild_bin_path (), o_default, 0);
1176
1177 define_variable ("PATH_KBUILD", sizeof ("PATH_KBUILD") - 1,
1178 get_kbuild_path (), o_default, 0);
1179 define_variable ("PATH_KBUILD_BIN", sizeof ("PATH_KBUILD_BIN") - 1,
1180 get_kbuild_bin_path (), o_default, 0);
1181
1182 /* Define KMK_FEATURES to indicate various working KMK features. */
1183# if defined (CONFIG_WITH_RSORT) \
1184 && defined (CONFIG_WITH_ABSPATHEX) \
1185 && defined (CONFIG_WITH_TOUPPER_TOLOWER) \
1186 && defined (CONFIG_WITH_DEFINED) \
1187 && defined (CONFIG_WITH_VALUE_LENGTH) && defined (CONFIG_WITH_COMPARE) \
1188 && defined (CONFIG_WITH_STACK) \
1189 && defined (CONFIG_WITH_MATH) \
1190 && defined (CONFIG_WITH_XARGS) \
1191 && defined (CONFIG_WITH_EXPLICIT_MULTITARGET) \
1192 && defined (CONFIG_WITH_PREPEND_ASSIGNMENT) \
1193 && defined (CONFIG_WITH_SET_CONDITIONALS) \
1194 && defined (CONFIG_WITH_DATE) \
1195 && defined (CONFIG_WITH_FILE_SIZE) \
1196 && defined (CONFIG_WITH_WHICH) \
1197 && defined (CONFIG_WITH_EVALPLUS) \
1198 && (defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)) \
1199 && defined (CONFIG_WITH_COMMANDS_FUNC) \
1200 && defined (KMK_HELPERS)
1201 (void) define_variable ("KMK_FEATURES", 12,
1202 "append-dash-n abspath includedep-queue"
1203 " rsort"
1204 " abspathex"
1205 " toupper tolower"
1206 " defined"
1207 " comp-vars comp-cmds comp-cmds-ex"
1208 " stack"
1209 " math-int"
1210 " xargs"
1211 " explicit-multitarget"
1212 " prepend-assignment"
1213 " set-conditionals"
1214 " date"
1215 " file-size"
1216 " expr if-expr"
1217 " which"
1218 " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var"
1219 " make-stats"
1220 " commands"
1221 " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one "
1222 , o_default, 0);
1223# else /* MSC can't deal with strings mixed with #if/#endif, thus the slow way. */
1224# error "All features should be enabled by default!"
1225 strcpy (buf, "append-dash-n abspath includedep-queue");
1226# if defined (CONFIG_WITH_RSORT)
1227 strcat (buf, " rsort");
1228# endif
1229# if defined (CONFIG_WITH_ABSPATHEX)
1230 strcat (buf, " abspathex");
1231# endif
1232# if defined (CONFIG_WITH_TOUPPER_TOLOWER)
1233 strcat (buf, " toupper tolower");
1234# endif
1235# if defined (CONFIG_WITH_DEFINED)
1236 strcat (buf, " defined");
1237# endif
1238# if defined (CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
1239 strcat (buf, " comp-vars comp-cmds comp-cmds-ex");
1240# endif
1241# if defined (CONFIG_WITH_STACK)
1242 strcat (buf, " stack");
1243# endif
1244# if defined (CONFIG_WITH_MATH)
1245 strcat (buf, " math-int");
1246# endif
1247# if defined (CONFIG_WITH_XARGS)
1248 strcat (buf, " xargs");
1249# endif
1250# if defined (CONFIG_WITH_EXPLICIT_MULTITARGET)
1251 strcat (buf, " explicit-multitarget");
1252# endif
1253# if defined (CONFIG_WITH_PREPEND_ASSIGNMENT)
1254 strcat (buf, " prepend-assignment");
1255# endif
1256# if defined (CONFIG_WITH_SET_CONDITIONALS)
1257 strcat (buf, " set-conditionals");
1258# endif
1259# if defined (CONFIG_WITH_DATE)
1260 strcat (buf, " date");
1261# endif
1262# if defined (CONFIG_WITH_FILE_SIZE)
1263 strcat (buf, " file-size");
1264# endif
1265# if defined (CONFIG_WITH_IF_CONDITIONALS)
1266 strcat (buf, " expr if-expr");
1267# endif
1268# if defined (CONFIG_WITH_WHICH)
1269 strcat (buf, " which");
1270# endif
1271# if defined (CONFIG_WITH_EVALPLUS)
1272 strcat (buf, " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var");
1273# endif
1274# if defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)
1275 strcat (buf, " make-stats");
1276# endif
1277# if defined (CONFIG_WITH_COMMANDS_FUNC)
1278 strcat (buf, " commands");
1279# endif
1280# if defined (KMK_HELPERS)
1281 strcat (buf, " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one");
1282# endif
1283 (void) define_variable ("KMK_FEATURES", 12, buf, o_default, 0);
1284# endif
1285
1286#endif /* KMK */
1287
1288#ifdef CONFIG_WITH_KMK_BUILTIN
1289 /* The supported kMk Builtin commands. */
1290 (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);
1291#endif
1292
1293#ifdef __MSDOS__
1294 /* Allow to specify a special shell just for Make,
1295 and use $COMSPEC as the default $SHELL when appropriate. */
1296 {
1297 static char shell_str[] = "SHELL";
1298 const int shlen = sizeof (shell_str) - 1;
1299 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
1300 struct variable *comp = lookup_variable ("COMSPEC", 7);
1301
1302 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
1303 if (mshp)
1304 (void) define_variable (shell_str, shlen,
1305 mshp->value, o_env_override, 0);
1306 else if (comp)
1307 {
1308 /* $COMSPEC shouldn't override $SHELL. */
1309 struct variable *shp = lookup_variable (shell_str, shlen);
1310
1311 if (!shp)
1312 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
1313 }
1314 }
1315#elif defined(__EMX__)
1316 {
1317 static char shell_str[] = "SHELL";
1318 const int shlen = sizeof (shell_str) - 1;
1319 struct variable *shell = lookup_variable (shell_str, shlen);
1320 struct variable *replace = lookup_variable ("MAKESHELL", 9);
1321
1322 /* if $MAKESHELL is defined in the environment assume o_env_override */
1323 if (replace && *replace->value && replace->origin == o_env)
1324 replace->origin = o_env_override;
1325
1326 /* if $MAKESHELL is not defined use $SHELL but only if the variable
1327 did not come from the environment */
1328 if (!replace || !*replace->value)
1329 if (shell && *shell->value && (shell->origin == o_env
1330 || shell->origin == o_env_override))
1331 {
1332 /* overwrite whatever we got from the environment */
1333 free(shell->value);
1334 shell->value = xstrdup (default_shell);
1335 shell->origin = o_default;
1336 }
1337
1338 /* Some people do not like cmd to be used as the default
1339 if $SHELL is not defined in the Makefile.
1340 With -DNO_CMD_DEFAULT you can turn off this behaviour */
1341# ifndef NO_CMD_DEFAULT
1342 /* otherwise use $COMSPEC */
1343 if (!replace || !*replace->value)
1344 replace = lookup_variable ("COMSPEC", 7);
1345
1346 /* otherwise use $OS2_SHELL */
1347 if (!replace || !*replace->value)
1348 replace = lookup_variable ("OS2_SHELL", 9);
1349# else
1350# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
1351# endif
1352
1353 if (replace && *replace->value)
1354 /* overwrite $SHELL */
1355 (void) define_variable (shell_str, shlen, replace->value,
1356 replace->origin, 0);
1357 else
1358 /* provide a definition if there is none */
1359 (void) define_variable (shell_str, shlen, default_shell,
1360 o_default, 0);
1361 }
1362
1363#endif
1364
1365 /* This won't override any definition, but it will provide one if there
1366 isn't one there. */
1367 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
1368#ifdef __MSDOS__
1369 v->export = v_export; /* Export always SHELL. */
1370#endif
1371
1372 /* On MSDOS we do use SHELL from environment, since it isn't a standard
1373 environment variable on MSDOS, so whoever sets it, does that on purpose.
1374 On OS/2 we do not use SHELL from environment but we have already handled
1375 that problem above. */
1376#if !defined(__MSDOS__) && !defined(__EMX__)
1377 /* Don't let SHELL come from the environment. */
1378 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
1379 {
1380#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1381 if (v->rdonly_val)
1382 v->rdonly_val = 0;
1383 else
1384#endif
1385 free (v->value);
1386 v->origin = o_file;
1387 v->value = xstrdup (default_shell);
1388#ifdef CONFIG_WITH_VALUE_LENGTH
1389 v->value_length = strlen (v->value);
1390 v->value_alloc_len = v->value_length + 1;
1391#endif
1392 }
1393#endif
1394
1395 /* Make sure MAKEFILES gets exported if it is set. */
1396 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
1397 v->export = v_ifset;
1398
1399 /* Define the magic D and F variables in terms of
1400 the automatic variables they are variations of. */
1401
1402#ifdef VMS
1403 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
1404 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
1405 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
1406 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
1407 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
1408 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
1409 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
1410#else
1411 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
1412 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
1413 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
1414 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
1415 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
1416 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
1417 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
1418#endif
1419 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
1420 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
1421 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
1422 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
1423 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
1424 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
1425 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
1426#ifdef CONFIG_WITH_LAZY_DEPS_VARS
1427 define_variable ("^", 1, "$(deps $@)", o_automatic, 1);
1428 define_variable ("+", 1, "$(deps-all $@)", o_automatic, 1);
1429 define_variable ("?", 1, "$(deps-newer $@)", o_automatic, 1);
1430 define_variable ("|", 1, "$(deps-oo $@)", o_automatic, 1);
1431#endif /* CONFIG_WITH_LAZY_DEPS_VARS */
1432}
1433
1434
1435int export_all_variables;
1436
1437/* Create a new environment for FILE's commands.
1438 If FILE is nil, this is for the `shell' function.
1439 The child's MAKELEVEL variable is incremented. */
1440
1441char **
1442target_environment (struct file *file)
1443{
1444 struct variable_set_list *set_list;
1445 register struct variable_set_list *s;
1446 struct hash_table table;
1447 struct variable **v_slot;
1448 struct variable **v_end;
1449 struct variable makelevel_key;
1450 char **result_0;
1451 char **result;
1452#ifdef CONFIG_WITH_STRCACHE2
1453 const char *cached_name;
1454#endif
1455
1456 if (file == 0)
1457 set_list = current_variable_set_list;
1458 else
1459 set_list = file->variables;
1460
1461#ifndef CONFIG_WITH_STRCACHE2
1462 hash_init (&table, VARIABLE_BUCKETS,
1463 variable_hash_1, variable_hash_2, variable_hash_cmp);
1464#else /* CONFIG_WITH_STRCACHE2 */
1465 hash_init_strcached (&table, VARIABLE_BUCKETS,
1466 &variable_strcache, offsetof (struct variable, name));
1467#endif /* CONFIG_WITH_STRCACHE2 */
1468
1469 /* Run through all the variable sets in the list,
1470 accumulating variables in TABLE. */
1471 for (s = set_list; s != 0; s = s->next)
1472 {
1473 struct variable_set *set = s->set;
1474 v_slot = (struct variable **) set->table.ht_vec;
1475 v_end = v_slot + set->table.ht_size;
1476 for ( ; v_slot < v_end; v_slot++)
1477 if (! HASH_VACANT (*v_slot))
1478 {
1479 struct variable **new_slot;
1480 struct variable *v = *v_slot;
1481
1482 /* If this is a per-target variable and it hasn't been touched
1483 already then look up the global version and take its export
1484 value. */
1485 if (v->per_target && v->export == v_default)
1486 {
1487 struct variable *gv;
1488
1489#ifndef CONFIG_WITH_VALUE_LENGTH
1490 gv = lookup_variable_in_set (v->name, strlen(v->name),
1491 &global_variable_set);
1492#else
1493 assert ((int)strlen(v->name) == v->length);
1494 gv = lookup_variable_in_set (v->name, v->length,
1495 &global_variable_set);
1496#endif
1497 if (gv)
1498 v->export = gv->export;
1499 }
1500
1501 switch (v->export)
1502 {
1503 case v_default:
1504 if (v->origin == o_default || v->origin == o_automatic)
1505 /* Only export default variables by explicit request. */
1506 continue;
1507
1508 /* The variable doesn't have a name that can be exported. */
1509 if (! v->exportable)
1510 continue;
1511
1512 if (! export_all_variables
1513 && v->origin != o_command
1514 && v->origin != o_env && v->origin != o_env_override)
1515 continue;
1516 break;
1517
1518 case v_export:
1519 break;
1520
1521 case v_noexport:
1522 {
1523 /* If this is the SHELL variable and it's not exported,
1524 then add the value from our original environment, if
1525 the original environment defined a value for SHELL. */
1526 extern struct variable shell_var;
1527 if (streq (v->name, "SHELL") && shell_var.value)
1528 {
1529 v = &shell_var;
1530 break;
1531 }
1532 continue;
1533 }
1534
1535 case v_ifset:
1536 if (v->origin == o_default)
1537 continue;
1538 break;
1539 }
1540
1541#ifndef CONFIG_WITH_STRCACHE2
1542 new_slot = (struct variable **) hash_find_slot (&table, v);
1543#else /* CONFIG_WITH_STRCACHE2 */
1544 assert (strcache2_is_cached (&variable_strcache, v->name));
1545 new_slot = (struct variable **) hash_find_slot_strcached (&table, v);
1546#endif /* CONFIG_WITH_STRCACHE2 */
1547 if (HASH_VACANT (*new_slot))
1548 hash_insert_at (&table, v, new_slot);
1549 }
1550 }
1551
1552#ifndef CONFIG_WITH_STRCACHE2
1553 makelevel_key.name = MAKELEVEL_NAME;
1554 makelevel_key.length = MAKELEVEL_LENGTH;
1555 hash_delete (&table, &makelevel_key);
1556#else /* CONFIG_WITH_STRCACHE2 */
1557 /* lookup the name in the string case, if it's not there it won't
1558 be in any of the sets either. */
1559 cached_name = strcache2_lookup (&variable_strcache,
1560 MAKELEVEL_NAME, MAKELEVEL_LENGTH);
1561 if (cached_name)
1562 {
1563 makelevel_key.name = cached_name;
1564 makelevel_key.length = MAKELEVEL_LENGTH;
1565 hash_delete_strcached (&table, &makelevel_key);
1566 }
1567#endif /* CONFIG_WITH_STRCACHE2 */
1568
1569 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
1570
1571 v_slot = (struct variable **) table.ht_vec;
1572 v_end = v_slot + table.ht_size;
1573 for ( ; v_slot < v_end; v_slot++)
1574 if (! HASH_VACANT (*v_slot))
1575 {
1576 struct variable *v = *v_slot;
1577
1578 /* If V is recursively expanded and didn't come from the environment,
1579 expand its value. If it came from the environment, it should
1580 go back into the environment unchanged. */
1581 if (v->recursive
1582 && v->origin != o_env && v->origin != o_env_override)
1583 {
1584#ifndef CONFIG_WITH_VALUE_LENGTH
1585 char *value = recursively_expand_for_file (v, file);
1586#else
1587 char *value = recursively_expand_for_file (v, file, NULL);
1588#endif
1589#ifdef WINDOWS32
1590 if (strcmp(v->name, "Path") == 0 ||
1591 strcmp(v->name, "PATH") == 0)
1592 convert_Path_to_windows32(value, ';');
1593#endif
1594 *result++ = xstrdup (concat (v->name, "=", value));
1595 free (value);
1596 }
1597 else
1598 {
1599#ifdef WINDOWS32
1600 if (strcmp(v->name, "Path") == 0 ||
1601 strcmp(v->name, "PATH") == 0)
1602 convert_Path_to_windows32(v->value, ';');
1603#endif
1604 *result++ = xstrdup (concat (v->name, "=", v->value));
1605 }
1606 }
1607
1608 *result = xmalloc (100);
1609 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
1610 *++result = 0;
1611
1612 hash_free (&table, 0);
1613
1614 return result_0;
1615}
1616
1617
1618#ifdef CONFIG_WITH_VALUE_LENGTH
1619/* Worker function for do_variable_definition_append() and
1620 append_expanded_string_to_variable().
1621 The APPEND argument indicates whether it's an append or prepend operation. */
1622void append_string_to_variable (struct variable *v, const char *value, unsigned int value_len, int append)
1623{
1624 /* The previous definition of the variable was recursive.
1625 The new value is the unexpanded old and new values. */
1626 unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
1627 int done_1st_prepend_copy = 0;
1628
1629 /* Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
1630 if (!value_len)
1631 return;
1632
1633 /* adjust the size. */
1634 if ((unsigned)v->value_alloc_len <= new_value_len + 1)
1635 {
1636 v->value_alloc_len *= 2;
1637 if ((unsigned)v->value_alloc_len < new_value_len + 1)
1638 v->value_alloc_len = (new_value_len + 1 + value_len + 0x7f) + ~0x7fU;
1639# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1640 if ((append || !v->value_length) && !v->rdonly_val)
1641# else
1642 if (append || !v->value_length)
1643# endif
1644 v->value = xrealloc (v->value, v->value_alloc_len);
1645 else
1646 {
1647 /* avoid the extra memcpy the xrealloc may have to do */
1648 char *new_buf = xmalloc (v->value_alloc_len);
1649 memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
1650 done_1st_prepend_copy = 1;
1651# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1652 if (v->rdonly_val)
1653 v->rdonly_val = 0;
1654 else
1655# endif
1656 free (v->value);
1657 v->value = new_buf;
1658 }
1659 }
1660
1661 /* insert the new bits */
1662 if (v->value_length != 0)
1663 {
1664 if (append)
1665 {
1666 v->value[v->value_length] = ' ';
1667 memcpy (&v->value[v->value_length + 1], value, value_len + 1);
1668 }
1669 else
1670 {
1671 if (!done_1st_prepend_copy)
1672 memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
1673 v->value[value_len] = ' ';
1674 memcpy (v->value, value, value_len);
1675 }
1676 }
1677 else
1678 memcpy (v->value, value, value_len + 1);
1679 v->value_length = new_value_len;
1680}
1681
1682static struct variable *
1683do_variable_definition_append (const struct floc *flocp, struct variable *v,
1684 const char *value, unsigned int value_len,
1685 int simple_value, enum variable_origin origin,
1686 int append)
1687{
1688 if (env_overrides && origin == o_env)
1689 origin = o_env_override;
1690
1691 if (env_overrides && v->origin == o_env)
1692 /* V came from in the environment. Since it was defined
1693 before the switches were parsed, it wasn't affected by -e. */
1694 v->origin = o_env_override;
1695
1696 /* A variable of this name is already defined.
1697 If the old definition is from a stronger source
1698 than this one, don't redefine it. */
1699 if ((int) origin < (int) v->origin)
1700 return v;
1701 v->origin = origin;
1702
1703 /* location */
1704 if (flocp != 0)
1705 v->fileinfo = *flocp;
1706
1707 /* The juicy bits, append the specified value to the variable
1708 This is a heavily exercised code path in kBuild. */
1709 if (value_len == ~0U)
1710 value_len = strlen (value);
1711 if (v->recursive || simple_value)
1712 append_string_to_variable (v, value, value_len, append);
1713 else
1714 /* The previous definition of the variable was simple.
1715 The new value comes from the old value, which was expanded
1716 when it was set; and from the expanded new value. */
1717 append_expanded_string_to_variable (v, value, value_len, append);
1718
1719 /* update the variable */
1720 return v;
1721}
1722#endif /* CONFIG_WITH_VALUE_LENGTH */
1723
1724
1725static struct variable *
1726set_special_var (struct variable *var)
1727{
1728 if (streq (var->name, RECIPEPREFIX_NAME))
1729 {
1730 /* The user is resetting the command introduction prefix. This has to
1731 happen immediately, so that subsequent rules are interpreted
1732 properly. */
1733 cmd_prefix = var->value[0]=='\0' ? RECIPEPREFIX_DEFAULT : var->value[0];
1734 }
1735
1736 return var;
1737}
1738
1739
1740/* Given a variable, a value, and a flavor, define the variable.
1741 See the try_variable_definition() function for details on the parameters. */
1742
1743struct variable *
1744#ifndef CONFIG_WITH_VALUE_LENGTH
1745do_variable_definition (const struct floc *flocp, const char *varname,
1746 const char *value, enum variable_origin origin,
1747 enum variable_flavor flavor, int target_var)
1748#else /* CONFIG_WITH_VALUE_LENGTH */
1749do_variable_definition_2 (const struct floc *flocp,
1750 const char *varname, const char *value,
1751 unsigned int value_len, int simple_value,
1752 char *free_value,
1753 enum variable_origin origin,
1754 enum variable_flavor flavor,
1755 int target_var)
1756#endif /* CONFIG_WITH_VALUE_LENGTH */
1757{
1758 const char *p;
1759 char *alloc_value = NULL;
1760 struct variable *v;
1761 int append = 0;
1762 int conditional = 0;
1763 const size_t varname_len = strlen (varname); /* bird */
1764#ifdef CONFIG_WITH_VALUE_LENGTH
1765 assert (value_len == ~0U || value_len == strlen (value));
1766#endif
1767
1768 /* Calculate the variable's new value in VALUE. */
1769
1770 switch (flavor)
1771 {
1772 default:
1773 case f_bogus:
1774 /* Should not be possible. */
1775 abort ();
1776 case f_simple:
1777 /* A simple variable definition "var := value". Expand the value.
1778 We have to allocate memory since otherwise it'll clobber the
1779 variable buffer, and we may still need that if we're looking at a
1780 target-specific variable. */
1781#ifndef CONFIG_WITH_VALUE_LENGTH
1782 p = alloc_value = allocated_variable_expand (value);
1783#else /* CONFIG_WITH_VALUE_LENGTH */
1784 if (!simple_value)
1785 p = alloc_value = allocated_variable_expand_2 (value, value_len, &value_len);
1786 else
1787 {
1788 if (value_len == ~0U)
1789 value_len = strlen (value);
1790 if (!free_value)
1791 p = alloc_value = savestring (value, value_len);
1792 else
1793 {
1794 assert (value == free_value);
1795 p = alloc_value = free_value;
1796 free_value = 0;
1797 }
1798 }
1799#endif /* CONFIG_WITH_VALUE_LENGTH */
1800 break;
1801 case f_conditional:
1802 /* A conditional variable definition "var ?= value".
1803 The value is set IFF the variable is not defined yet. */
1804 v = lookup_variable (varname, varname_len);
1805 if (v)
1806#ifndef CONFIG_WITH_VALUE_LENGTH
1807 return v->special ? set_special_var (v) : v;
1808#else /* CONFIG_WITH_VALUE_LENGTH */
1809 {
1810 if (free_value)
1811 free (free_value);
1812 return v->special ? set_special_var (v) : v;
1813 }
1814#endif /* CONFIG_WITH_VALUE_LENGTH */
1815
1816 conditional = 1;
1817 flavor = f_recursive;
1818 /* FALLTHROUGH */
1819 case f_recursive:
1820 /* A recursive variable definition "var = value".
1821 The value is used verbatim. */
1822 p = value;
1823 break;
1824#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1825 case f_append:
1826 case f_prepend:
1827 {
1828 const enum variable_flavor org_flavor = flavor;
1829#else
1830 case f_append:
1831 {
1832#endif
1833
1834#ifdef CONFIG_WITH_LOCAL_VARIABLES
1835 /* If we have += but we're in a target or local variable context,
1836 we want to append only with other variables in the context of
1837 this target. */
1838 if (target_var || origin == o_local)
1839#else
1840 /* If we have += but we're in a target variable context, we want to
1841 append only with other variables in the context of this target. */
1842 if (target_var)
1843#endif
1844 {
1845 append = 1;
1846 v = lookup_variable_in_set (varname, varname_len,
1847 current_variable_set_list->set);
1848
1849 /* Don't append from the global set if a previous non-appending
1850 target-specific variable definition exists. */
1851 if (v && !v->append)
1852 append = 0;
1853 }
1854 else
1855 v = lookup_variable (varname, varname_len);
1856
1857 if (v == 0)
1858 {
1859 /* There was no old value.
1860 This becomes a normal recursive definition. */
1861 p = value;
1862 flavor = f_recursive;
1863 }
1864 else
1865 {
1866#ifdef CONFIG_WITH_VALUE_LENGTH
1867 v->append = append;
1868 v = do_variable_definition_append (flocp, v, value, value_len,
1869 simple_value, origin,
1870# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1871 org_flavor == f_append);
1872# else
1873 1);
1874# endif
1875 if (free_value)
1876 free (free_value);
1877 return v;
1878#else /* !CONFIG_WITH_VALUE_LENGTH */
1879
1880 /* Paste the old and new values together in VALUE. */
1881
1882 unsigned int oldlen, vallen;
1883 const char *val;
1884 char *tp;
1885
1886 val = value;
1887 if (v->recursive)
1888 /* The previous definition of the variable was recursive.
1889 The new value is the unexpanded old and new values. */
1890 flavor = f_recursive;
1891 else
1892 /* The previous definition of the variable was simple.
1893 The new value comes from the old value, which was expanded
1894 when it was set; and from the expanded new value. Allocate
1895 memory for the expansion as we may still need the rest of the
1896 buffer if we're looking at a target-specific variable. */
1897 val = alloc_value = allocated_variable_expand (val);
1898
1899 oldlen = strlen (v->value);
1900 vallen = strlen (val);
1901 tp = alloca (oldlen + 1 + vallen + 1);
1902# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1903 if (org_flavor == f_prepend)
1904 {
1905 memcpy (tp, val, vallen);
1906 tp[oldlen] = ' ';
1907 memcpy (&tp[oldlen + 1], v->value, oldlen + 1);
1908 }
1909 else
1910# endif /* CONFIG_WITH_PREPEND_ASSIGNMENT */
1911 {
1912 memcpy (tp, v->value, oldlen);
1913 tp[oldlen] = ' ';
1914 memcpy (&tp[oldlen + 1], val, vallen + 1);
1915 }
1916 p = tp;
1917#endif /* !CONFIG_WITH_VALUE_LENGTH */
1918 }
1919 }
1920 }
1921
1922#ifdef __MSDOS__
1923 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1924 non-Unix systems don't conform to this default configuration (in
1925 fact, most of them don't even have `/bin'). On the other hand,
1926 $SHELL in the environment, if set, points to the real pathname of
1927 the shell.
1928 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1929 the Makefile override $SHELL from the environment. But first, we
1930 look for the basename of the shell in the directory where SHELL=
1931 points, and along the $PATH; if it is found in any of these places,
1932 we define $SHELL to be the actual pathname of the shell. Thus, if
1933 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1934 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1935 defining SHELL to be "d:/unix/bash.exe". */
1936 if ((origin == o_file || origin == o_override)
1937 && strcmp (varname, "SHELL") == 0)
1938 {
1939 PATH_VAR (shellpath);
1940 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1941
1942 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1943 if (__dosexec_find_on_path (p, NULL, shellpath))
1944 {
1945 char *tp;
1946
1947 for (tp = shellpath; *tp; tp++)
1948 if (*tp == '\\')
1949 *tp = '/';
1950
1951 v = define_variable_loc (varname, varname_len,
1952 shellpath, origin, flavor == f_recursive,
1953 flocp);
1954 }
1955 else
1956 {
1957 const char *shellbase, *bslash;
1958 struct variable *pathv = lookup_variable ("PATH", 4);
1959 char *path_string;
1960 char *fake_env[2];
1961 size_t pathlen = 0;
1962
1963 shellbase = strrchr (p, '/');
1964 bslash = strrchr (p, '\\');
1965 if (!shellbase || bslash > shellbase)
1966 shellbase = bslash;
1967 if (!shellbase && p[1] == ':')
1968 shellbase = p + 1;
1969 if (shellbase)
1970 shellbase++;
1971 else
1972 shellbase = p;
1973
1974 /* Search for the basename of the shell (with standard
1975 executable extensions) along the $PATH. */
1976 if (pathv)
1977 pathlen = strlen (pathv->value);
1978 path_string = xmalloc (5 + pathlen + 2 + 1);
1979 /* On MSDOS, current directory is considered as part of $PATH. */
1980 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1981 fake_env[0] = path_string;
1982 fake_env[1] = 0;
1983 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1984 {
1985 char *tp;
1986
1987 for (tp = shellpath; *tp; tp++)
1988 if (*tp == '\\')
1989 *tp = '/';
1990
1991 v = define_variable_loc (varname, varname_len,
1992 shellpath, origin,
1993 flavor == f_recursive, flocp);
1994 }
1995 else
1996 v = lookup_variable (varname, varname_len);
1997
1998 free (path_string);
1999 }
2000 }
2001 else
2002#endif /* __MSDOS__ */
2003#ifdef WINDOWS32
2004 if ( varname_len == sizeof("SHELL") - 1 /* bird */
2005 && (origin == o_file || origin == o_override || origin == o_command)
2006 && streq (varname, "SHELL"))
2007 {
2008 extern char *default_shell;
2009
2010 /* Call shell locator function. If it returns TRUE, then
2011 set no_default_sh_exe to indicate sh was found and
2012 set new value for SHELL variable. */
2013
2014 if (find_and_set_default_shell (p))
2015 {
2016 v = define_variable_in_set (varname, varname_len, default_shell,
2017# ifdef CONFIG_WITH_VALUE_LENGTH
2018 ~0U, 1 /* duplicate_value */,
2019# endif
2020 origin, flavor == f_recursive,
2021 (target_var
2022 ? current_variable_set_list->set
2023 : NULL),
2024 flocp);
2025 no_default_sh_exe = 0;
2026 }
2027 else
2028 {
2029 if (alloc_value)
2030 free (alloc_value);
2031
2032 alloc_value = allocated_variable_expand (p);
2033 if (find_and_set_default_shell (alloc_value))
2034 {
2035 v = define_variable_in_set (varname, varname_len, p,
2036 origin, flavor == f_recursive,
2037 (target_var
2038 ? current_variable_set_list->set
2039 : NULL),
2040 flocp);
2041 no_default_sh_exe = 0;
2042 }
2043 else
2044 v = lookup_variable (varname, varname_len);
2045 }
2046 }
2047 else
2048#endif
2049
2050 /* If we are defining variables inside an $(eval ...), we might have a
2051 different variable context pushed, not the global context (maybe we're
2052 inside a $(call ...) or something. Since this function is only ever
2053 invoked in places where we want to define globally visible variables,
2054 make sure we define this variable in the global set. */
2055
2056 v = define_variable_in_set (varname, varname_len, p,
2057#ifdef CONFIG_WITH_VALUE_LENGTH
2058 value_len, !alloc_value,
2059#endif
2060 origin, flavor == f_recursive,
2061#ifdef CONFIG_WITH_LOCAL_VARIABLES
2062 (target_var || origin == o_local
2063#else
2064 (target_var
2065#endif
2066 ? current_variable_set_list->set : NULL),
2067 flocp);
2068 v->append = append;
2069 v->conditional = conditional;
2070
2071#ifndef CONFIG_WITH_VALUE_LENGTH
2072 if (alloc_value)
2073 free (alloc_value);
2074#else
2075 if (free_value)
2076 free (free_value);
2077#endif
2078
2079 return v->special ? set_special_var (v) : v;
2080}
2081
2082
2083/* Try to interpret LINE (a null-terminated string) as a variable definition.
2084
2085 ORIGIN may be o_file, o_override, o_env, o_env_override,
2086 or o_command specifying that the variable definition comes
2087 from a makefile, an override directive, the environment with
2088 or without the -e switch, or the command line.
2089
2090 See the comments for parse_variable_definition().
2091
2092 If LINE was recognized as a variable definition, a pointer to its `struct
2093 variable' is returned. If LINE is not a variable definition, NULL is
2094 returned. */
2095
2096struct variable *
2097#ifndef CONFIG_WITH_VALUE_LENGTH
2098parse_variable_definition (struct variable *v, char *line)
2099#else
2100parse_variable_definition (struct variable *v, char *line, char *eos)
2101#endif
2102{
2103 register int c;
2104 register char *p = line;
2105 register char *beg;
2106 register char *end;
2107 enum variable_flavor flavor = f_bogus;
2108#ifndef CONFIG_WITH_VALUE_LENGTH
2109 char *name;
2110#endif
2111
2112 while (1)
2113 {
2114 c = *p++;
2115 if (c == '\0' || c == '#')
2116 return 0;
2117 if (c == '=')
2118 {
2119 end = p - 1;
2120 flavor = f_recursive;
2121 break;
2122 }
2123 else if (c == ':')
2124 if (*p == '=')
2125 {
2126 end = p++ - 1;
2127 flavor = f_simple;
2128 break;
2129 }
2130 else
2131 /* A colon other than := is a rule line, not a variable defn. */
2132 return 0;
2133 else if (c == '+' && *p == '=')
2134 {
2135 end = p++ - 1;
2136 flavor = f_append;
2137 break;
2138 }
2139#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2140 else if (c == '<' && *p == '=')
2141 {
2142 end = p++ - 1;
2143 flavor = f_prepend;
2144 break;
2145 }
2146#endif
2147 else if (c == '?' && *p == '=')
2148 {
2149 end = p++ - 1;
2150 flavor = f_conditional;
2151 break;
2152 }
2153 else if (c == '$')
2154 {
2155 /* This might begin a variable expansion reference. Make sure we
2156 don't misrecognize chars inside the reference as =, := or +=. */
2157 char closeparen;
2158 int count;
2159 c = *p++;
2160 if (c == '(')
2161 closeparen = ')';
2162 else if (c == '{')
2163 closeparen = '}';
2164 else
2165 continue; /* Nope. */
2166
2167 /* P now points past the opening paren or brace.
2168 Count parens or braces until it is matched. */
2169 count = 0;
2170 for (; *p != '\0'; ++p)
2171 {
2172 if (*p == c)
2173 ++count;
2174 else if (*p == closeparen && --count < 0)
2175 {
2176 ++p;
2177 break;
2178 }
2179 }
2180 }
2181 }
2182 v->flavor = flavor;
2183
2184 beg = next_token (line);
2185 while (end > beg && isblank ((unsigned char)end[-1]))
2186 --end;
2187 p = next_token (p);
2188 v->value = p;
2189#ifdef CONFIG_WITH_VALUE_LENGTH
2190 v->value_alloc_len = -1;
2191 v->value_length = eos != NULL ? eos - p : -1;
2192 assert (eos == NULL || strchr (p, '\0') == eos);
2193# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2194 v->rdonly_val = 0;
2195# endif
2196#endif
2197
2198 /* Expand the name, so "$(foo)bar = baz" works. */
2199#ifndef CONFIG_WITH_VALUE_LENGTH
2200 name = alloca (end - beg + 1);
2201 memcpy (name, beg, end - beg);
2202 name[end - beg] = '\0';
2203 v->name = allocated_variable_expand (name);
2204#else /* CONFIG_WITH_VALUE_LENGTH */
2205 v->name = allocated_variable_expand_2 (beg, end - beg, NULL);
2206#endif /* CONFIG_WITH_VALUE_LENGTH */
2207
2208 if (v->name[0] == '\0')
2209 fatal (&v->fileinfo, _("empty variable name"));
2210
2211 return v;
2212}
2213
2214
2215/* Try to interpret LINE (a null-terminated string) as a variable definition.
2216
2217 ORIGIN may be o_file, o_override, o_env, o_env_override, o_local,
2218 or o_command specifying that the variable definition comes
2219 from a makefile, an override directive, the environment with
2220 or without the -e switch, or the command line.
2221
2222 See the comments for parse_variable_definition().
2223
2224 If LINE was recognized as a variable definition, a pointer to its `struct
2225 variable' is returned. If LINE is not a variable definition, NULL is
2226 returned. */
2227
2228struct variable *
2229#ifndef CONFIG_WITH_VALUE_LENGTH
2230try_variable_definition (const struct floc *flocp, char *line,
2231 enum variable_origin origin, int target_var)
2232#else
2233try_variable_definition (const struct floc *flocp, char *line, char *eos,
2234 enum variable_origin origin, int target_var)
2235#endif
2236{
2237 struct variable v;
2238 struct variable *vp;
2239
2240 if (flocp != 0)
2241 v.fileinfo = *flocp;
2242 else
2243 v.fileinfo.filenm = 0;
2244
2245#ifndef CONFIG_WITH_VALUE_LENGTH
2246 if (!parse_variable_definition (&v, line))
2247 return 0;
2248
2249 vp = do_variable_definition (flocp, v.name, v.value,
2250 origin, v.flavor, target_var);
2251#else
2252 if (!parse_variable_definition (&v, line, eos))
2253 return 0;
2254
2255 vp = do_variable_definition_2 (flocp, v.name, v.value,
2256 v.value_length != -1 ? (unsigned int)v.value_length : ~0U, /* FIXME */
2257 0, NULL, origin, v.flavor, target_var);
2258#endif
2259
2260#ifndef CONFIG_WITH_STRCACHE2
2261 free (v.name);
2262#else
2263 free ((char *)v.name);
2264#endif
2265
2266 return vp;
2267}
2268
2269
2270/* Print information for variable V, prefixing it with PREFIX. */
2271
2272static void
2273print_variable (const void *item, void *arg)
2274{
2275 const struct variable *v = item;
2276 const char *prefix = arg;
2277 const char *origin;
2278
2279 switch (v->origin)
2280 {
2281 case o_default:
2282 origin = _("default");
2283 break;
2284 case o_env:
2285 origin = _("environment");
2286 break;
2287 case o_file:
2288 origin = _("makefile");
2289 break;
2290 case o_env_override:
2291 origin = _("environment under -e");
2292 break;
2293 case o_command:
2294 origin = _("command line");
2295 break;
2296 case o_override:
2297 origin = _("`override' directive");
2298 break;
2299 case o_automatic:
2300 origin = _("automatic");
2301 break;
2302#ifdef CONFIG_WITH_LOCAL_VARIABLES
2303 case o_local:
2304 origin = _("`local' directive");
2305 break;
2306#endif
2307 case o_invalid:
2308 default:
2309 abort ();
2310 }
2311 fputs ("# ", stdout);
2312 fputs (origin, stdout);
2313 if (v->fileinfo.filenm)
2314 printf (_(" (from `%s', line %lu)"),
2315 v->fileinfo.filenm, v->fileinfo.lineno);
2316 putchar ('\n');
2317 fputs (prefix, stdout);
2318
2319 /* Is this a `define'? */
2320 if (v->recursive && strchr (v->value, '\n') != 0)
2321 printf ("define %s\n%s\nendef\n", v->name, v->value);
2322 else
2323 {
2324 register char *p;
2325
2326 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
2327
2328 /* Check if the value is just whitespace. */
2329 p = next_token (v->value);
2330 if (p != v->value && *p == '\0')
2331 /* All whitespace. */
2332 printf ("$(subst ,,%s)", v->value);
2333 else if (v->recursive)
2334 fputs (v->value, stdout);
2335 else
2336 /* Double up dollar signs. */
2337 for (p = v->value; *p != '\0'; ++p)
2338 {
2339 if (*p == '$')
2340 putchar ('$');
2341 putchar (*p);
2342 }
2343 putchar ('\n');
2344 }
2345}
2346
2347
2348/* Print all the variables in SET. PREFIX is printed before
2349 the actual variable definitions (everything else is comments). */
2350
2351void
2352print_variable_set (struct variable_set *set, char *prefix)
2353{
2354 hash_map_arg (&set->table, print_variable, prefix);
2355
2356 fputs (_("# variable set hash-table stats:\n"), stdout);
2357 fputs ("# ", stdout);
2358 hash_print_stats (&set->table, stdout);
2359 putc ('\n', stdout);
2360}
2361
2362/* Print the data base of variables. */
2363
2364void
2365print_variable_data_base (void)
2366{
2367 puts (_("\n# Variables\n"));
2368
2369 print_variable_set (&global_variable_set, "");
2370
2371 puts (_("\n# Pattern-specific Variable Values"));
2372
2373 {
2374 struct pattern_var *p;
2375 int rules = 0;
2376
2377 for (p = pattern_vars; p != 0; p = p->next)
2378 {
2379 ++rules;
2380 printf ("\n%s :\n", p->target);
2381 print_variable (&p->variable, "# ");
2382 }
2383
2384 if (rules == 0)
2385 puts (_("\n# No pattern-specific variable values."));
2386 else
2387 printf (_("\n# %u pattern-specific variable values"), rules);
2388 }
2389
2390#ifdef CONFIG_WITH_STRCACHE2
2391 strcache2_print_stats (&variable_strcache, "# ");
2392#endif
2393}
2394
2395#ifdef CONFIG_WITH_PRINT_STATS_SWITCH
2396void
2397print_variable_stats (void)
2398{
2399 fputs (_("\n# Global variable hash-table stats:\n# "), stdout);
2400 hash_print_stats (&global_variable_set.table, stdout);
2401 fputs ("\n", stdout);
2402}
2403#endif
2404
2405/* Print all the local variables of FILE. */
2406
2407void
2408print_file_variables (const struct file *file)
2409{
2410 if (file->variables != 0)
2411 print_variable_set (file->variables->set, "# ");
2412}
2413
2414#ifdef WINDOWS32
2415void
2416sync_Path_environment (void)
2417{
2418 char *path = allocated_variable_expand ("$(PATH)");
2419 static char *environ_path = NULL;
2420
2421 if (!path)
2422 return;
2423
2424 /*
2425 * If done this before, don't leak memory unnecessarily.
2426 * Free the previous entry before allocating new one.
2427 */
2428 if (environ_path)
2429 free (environ_path);
2430
2431 /*
2432 * Create something WINDOWS32 world can grok
2433 */
2434 convert_Path_to_windows32 (path, ';');
2435 environ_path = xstrdup (concat ("PATH", "=", path));
2436 putenv (environ_path);
2437 free (path);
2438}
2439#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