VirtualBox

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

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

Windows build fix.

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