VirtualBox

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

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

kmk,footer.kmk: New helper function 'kb-exp-tmpl', 10x faster than the gnu make script.

  • Property svn:eol-style set to native
File size: 77.1 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_PREPEND_ASSIGNMENT) \
1199 && defined (CONFIG_WITH_SET_CONDITIONALS) \
1200 && defined (CONFIG_WITH_DATE) \
1201 && defined (CONFIG_WITH_FILE_SIZE) \
1202 && defined (CONFIG_WITH_WHICH) \
1203 && defined (CONFIG_WITH_EVALPLUS) \
1204 && (defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)) \
1205 && defined (CONFIG_WITH_COMMANDS_FUNC) \
1206 && defined (KMK_HELPERS)
1207 (void) define_variable ("KMK_FEATURES", 12,
1208 "append-dash-n abspath includedep-queue"
1209 " rsort"
1210 " abspathex"
1211 " toupper tolower"
1212 " defined"
1213 " comp-vars comp-cmds comp-cmds-ex"
1214 " stack"
1215 " math-int"
1216 " xargs"
1217 " explicit-multitarget"
1218 " prepend-assignment"
1219 " set-conditionals"
1220 " date"
1221 " file-size"
1222 " expr if-expr"
1223 " which"
1224 " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var"
1225 " make-stats"
1226 " commands"
1227 " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one kb-exp-tmpl "
1228 , o_default, 0);
1229# else /* MSC can't deal with strings mixed with #if/#endif, thus the slow way. */
1230# error "All features should be enabled by default!"
1231 strcpy (buf, "append-dash-n abspath includedep-queue");
1232# if defined (CONFIG_WITH_RSORT)
1233 strcat (buf, " rsort");
1234# endif
1235# if defined (CONFIG_WITH_ABSPATHEX)
1236 strcat (buf, " abspathex");
1237# endif
1238# if defined (CONFIG_WITH_TOUPPER_TOLOWER)
1239 strcat (buf, " toupper tolower");
1240# endif
1241# if defined (CONFIG_WITH_DEFINED)
1242 strcat (buf, " defined");
1243# endif
1244# if defined (CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
1245 strcat (buf, " comp-vars comp-cmds comp-cmds-ex");
1246# endif
1247# if defined (CONFIG_WITH_STACK)
1248 strcat (buf, " stack");
1249# endif
1250# if defined (CONFIG_WITH_MATH)
1251 strcat (buf, " math-int");
1252# endif
1253# if defined (CONFIG_WITH_XARGS)
1254 strcat (buf, " xargs");
1255# endif
1256# if defined (CONFIG_WITH_EXPLICIT_MULTITARGET)
1257 strcat (buf, " explicit-multitarget");
1258# endif
1259# if defined (CONFIG_WITH_PREPEND_ASSIGNMENT)
1260 strcat (buf, " prepend-assignment");
1261# endif
1262# if defined (CONFIG_WITH_SET_CONDITIONALS)
1263 strcat (buf, " set-conditionals");
1264# endif
1265# if defined (CONFIG_WITH_DATE)
1266 strcat (buf, " date");
1267# endif
1268# if defined (CONFIG_WITH_FILE_SIZE)
1269 strcat (buf, " file-size");
1270# endif
1271# if defined (CONFIG_WITH_IF_CONDITIONALS)
1272 strcat (buf, " expr if-expr");
1273# endif
1274# if defined (CONFIG_WITH_WHICH)
1275 strcat (buf, " which");
1276# endif
1277# if defined (CONFIG_WITH_EVALPLUS)
1278 strcat (buf, " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var");
1279# endif
1280# if defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)
1281 strcat (buf, " make-stats");
1282# endif
1283# if defined (CONFIG_WITH_COMMANDS_FUNC)
1284 strcat (buf, " commands");
1285# endif
1286# if defined (KMK_HELPERS)
1287 strcat (buf, " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one kb-exp-tmpl");
1288# endif
1289 (void) define_variable ("KMK_FEATURES", 12, buf, o_default, 0);
1290# endif
1291
1292#endif /* KMK */
1293
1294#ifdef CONFIG_WITH_KMK_BUILTIN
1295 /* The supported kMk Builtin commands. */
1296 (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);
1297#endif
1298
1299#ifdef __MSDOS__
1300 /* Allow to specify a special shell just for Make,
1301 and use $COMSPEC as the default $SHELL when appropriate. */
1302 {
1303 static char shell_str[] = "SHELL";
1304 const int shlen = sizeof (shell_str) - 1;
1305 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
1306 struct variable *comp = lookup_variable ("COMSPEC", 7);
1307
1308 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
1309 if (mshp)
1310 (void) define_variable (shell_str, shlen,
1311 mshp->value, o_env_override, 0);
1312 else if (comp)
1313 {
1314 /* $COMSPEC shouldn't override $SHELL. */
1315 struct variable *shp = lookup_variable (shell_str, shlen);
1316
1317 if (!shp)
1318 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
1319 }
1320 }
1321#elif defined(__EMX__)
1322 {
1323 static char shell_str[] = "SHELL";
1324 const int shlen = sizeof (shell_str) - 1;
1325 struct variable *shell = lookup_variable (shell_str, shlen);
1326 struct variable *replace = lookup_variable ("MAKESHELL", 9);
1327
1328 /* if $MAKESHELL is defined in the environment assume o_env_override */
1329 if (replace && *replace->value && replace->origin == o_env)
1330 replace->origin = o_env_override;
1331
1332 /* if $MAKESHELL is not defined use $SHELL but only if the variable
1333 did not come from the environment */
1334 if (!replace || !*replace->value)
1335 if (shell && *shell->value && (shell->origin == o_env
1336 || shell->origin == o_env_override))
1337 {
1338 /* overwrite whatever we got from the environment */
1339 free(shell->value);
1340 shell->value = xstrdup (default_shell);
1341 shell->origin = o_default;
1342 }
1343
1344 /* Some people do not like cmd to be used as the default
1345 if $SHELL is not defined in the Makefile.
1346 With -DNO_CMD_DEFAULT you can turn off this behaviour */
1347# ifndef NO_CMD_DEFAULT
1348 /* otherwise use $COMSPEC */
1349 if (!replace || !*replace->value)
1350 replace = lookup_variable ("COMSPEC", 7);
1351
1352 /* otherwise use $OS2_SHELL */
1353 if (!replace || !*replace->value)
1354 replace = lookup_variable ("OS2_SHELL", 9);
1355# else
1356# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
1357# endif
1358
1359 if (replace && *replace->value)
1360 /* overwrite $SHELL */
1361 (void) define_variable (shell_str, shlen, replace->value,
1362 replace->origin, 0);
1363 else
1364 /* provide a definition if there is none */
1365 (void) define_variable (shell_str, shlen, default_shell,
1366 o_default, 0);
1367 }
1368
1369#endif
1370
1371 /* This won't override any definition, but it will provide one if there
1372 isn't one there. */
1373 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
1374#ifdef __MSDOS__
1375 v->export = v_export; /* Export always SHELL. */
1376#endif
1377
1378 /* On MSDOS we do use SHELL from environment, since it isn't a standard
1379 environment variable on MSDOS, so whoever sets it, does that on purpose.
1380 On OS/2 we do not use SHELL from environment but we have already handled
1381 that problem above. */
1382#if !defined(__MSDOS__) && !defined(__EMX__)
1383 /* Don't let SHELL come from the environment. */
1384 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
1385 {
1386# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1387 if (v->rdonly_val)
1388 v->rdonly_val = 0;
1389 else
1390# endif
1391 free (v->value);
1392 v->origin = o_file;
1393 v->value = xstrdup (default_shell);
1394# ifdef CONFIG_WITH_VALUE_LENGTH
1395 v->value_length = strlen (v->value);
1396 v->value_alloc_len = v->value_length + 1;
1397# endif
1398 }
1399#endif
1400
1401 /* Make sure MAKEFILES gets exported if it is set. */
1402 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
1403 v->export = v_ifset;
1404
1405 /* Define the magic D and F variables in terms of
1406 the automatic variables they are variations of. */
1407
1408#ifdef VMS
1409 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
1410 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
1411 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
1412 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
1413 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
1414 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
1415 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
1416#else
1417 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
1418 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
1419 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
1420 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
1421 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
1422 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
1423 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
1424#endif
1425 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
1426 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
1427 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
1428 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
1429 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
1430 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
1431 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
1432#ifdef CONFIG_WITH_LAZY_DEPS_VARS
1433 define_variable ("^", 1, "$(deps $@)", o_automatic, 1);
1434 define_variable ("+", 1, "$(deps-all $@)", o_automatic, 1);
1435 define_variable ("?", 1, "$(deps-newer $@)", o_automatic, 1);
1436 define_variable ("|", 1, "$(deps-oo $@)", o_automatic, 1);
1437#endif /* CONFIG_WITH_LAZY_DEPS_VARS */
1438}
1439
1440
1441int export_all_variables;
1442
1443/* Create a new environment for FILE's commands.
1444 If FILE is nil, this is for the `shell' function.
1445 The child's MAKELEVEL variable is incremented. */
1446
1447char **
1448target_environment (struct file *file)
1449{
1450 struct variable_set_list *set_list;
1451 register struct variable_set_list *s;
1452 struct hash_table table;
1453 struct variable **v_slot;
1454 struct variable **v_end;
1455 struct variable makelevel_key;
1456 char **result_0;
1457 char **result;
1458#ifdef CONFIG_WITH_STRCACHE2
1459 const char *cached_name;
1460#endif
1461
1462 if (file == 0)
1463 set_list = current_variable_set_list;
1464 else
1465 set_list = file->variables;
1466
1467#ifndef CONFIG_WITH_STRCACHE2
1468 hash_init (&table, VARIABLE_BUCKETS,
1469 variable_hash_1, variable_hash_2, variable_hash_cmp);
1470#else /* CONFIG_WITH_STRCACHE2 */
1471 hash_init_strcached (&table, VARIABLE_BUCKETS,
1472 &variable_strcache, offsetof (struct variable, name));
1473#endif /* CONFIG_WITH_STRCACHE2 */
1474
1475 /* Run through all the variable sets in the list,
1476 accumulating variables in TABLE. */
1477 for (s = set_list; s != 0; s = s->next)
1478 {
1479 struct variable_set *set = s->set;
1480 v_slot = (struct variable **) set->table.ht_vec;
1481 v_end = v_slot + set->table.ht_size;
1482 for ( ; v_slot < v_end; v_slot++)
1483 if (! HASH_VACANT (*v_slot))
1484 {
1485 struct variable **new_slot;
1486 struct variable *v = *v_slot;
1487
1488 /* If this is a per-target variable and it hasn't been touched
1489 already then look up the global version and take its export
1490 value. */
1491 if (v->per_target && v->export == v_default)
1492 {
1493 struct variable *gv;
1494
1495#ifndef CONFIG_WITH_VALUE_LENGTH
1496 gv = lookup_variable_in_set (v->name, strlen(v->name),
1497 &global_variable_set);
1498#else
1499 assert ((int)strlen(v->name) == v->length);
1500 gv = lookup_variable_in_set (v->name, v->length,
1501 &global_variable_set);
1502#endif
1503 if (gv)
1504 v->export = gv->export;
1505 }
1506
1507 switch (v->export)
1508 {
1509 case v_default:
1510 if (v->origin == o_default || v->origin == o_automatic)
1511 /* Only export default variables by explicit request. */
1512 continue;
1513
1514 /* The variable doesn't have a name that can be exported. */
1515 if (! v->exportable)
1516 continue;
1517
1518 if (! export_all_variables
1519 && v->origin != o_command
1520 && v->origin != o_env && v->origin != o_env_override)
1521 continue;
1522 break;
1523
1524 case v_export:
1525 break;
1526
1527 case v_noexport:
1528 {
1529 /* If this is the SHELL variable and it's not exported,
1530 then add the value from our original environment, if
1531 the original environment defined a value for SHELL. */
1532 extern struct variable shell_var;
1533 if (streq (v->name, "SHELL") && shell_var.value)
1534 {
1535 v = &shell_var;
1536 break;
1537 }
1538 continue;
1539 }
1540
1541 case v_ifset:
1542 if (v->origin == o_default)
1543 continue;
1544 break;
1545 }
1546
1547#ifndef CONFIG_WITH_STRCACHE2
1548 new_slot = (struct variable **) hash_find_slot (&table, v);
1549#else /* CONFIG_WITH_STRCACHE2 */
1550 assert (strcache2_is_cached (&variable_strcache, v->name));
1551 new_slot = (struct variable **) hash_find_slot_strcached (&table, v);
1552#endif /* CONFIG_WITH_STRCACHE2 */
1553 if (HASH_VACANT (*new_slot))
1554 hash_insert_at (&table, v, new_slot);
1555 }
1556 }
1557
1558#ifndef CONFIG_WITH_STRCACHE2
1559 makelevel_key.name = MAKELEVEL_NAME;
1560 makelevel_key.length = MAKELEVEL_LENGTH;
1561 hash_delete (&table, &makelevel_key);
1562#else /* CONFIG_WITH_STRCACHE2 */
1563 /* lookup the name in the string case, if it's not there it won't
1564 be in any of the sets either. */
1565 cached_name = strcache2_lookup (&variable_strcache,
1566 MAKELEVEL_NAME, MAKELEVEL_LENGTH);
1567 if (cached_name)
1568 {
1569 makelevel_key.name = cached_name;
1570 makelevel_key.length = MAKELEVEL_LENGTH;
1571 hash_delete_strcached (&table, &makelevel_key);
1572 }
1573#endif /* CONFIG_WITH_STRCACHE2 */
1574
1575 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
1576
1577 v_slot = (struct variable **) table.ht_vec;
1578 v_end = v_slot + table.ht_size;
1579 for ( ; v_slot < v_end; v_slot++)
1580 if (! HASH_VACANT (*v_slot))
1581 {
1582 struct variable *v = *v_slot;
1583
1584 /* If V is recursively expanded and didn't come from the environment,
1585 expand its value. If it came from the environment, it should
1586 go back into the environment unchanged. */
1587 if (v->recursive
1588 && v->origin != o_env && v->origin != o_env_override)
1589 {
1590#ifndef CONFIG_WITH_VALUE_LENGTH
1591 char *value = recursively_expand_for_file (v, file);
1592#else
1593 char *value = recursively_expand_for_file (v, file, NULL);
1594#endif
1595#ifdef WINDOWS32
1596 if (strcmp(v->name, "Path") == 0 ||
1597 strcmp(v->name, "PATH") == 0)
1598 convert_Path_to_windows32(value, ';');
1599#endif
1600 *result++ = xstrdup (concat (v->name, "=", value));
1601 free (value);
1602 }
1603 else
1604 {
1605#ifdef WINDOWS32
1606 if (strcmp(v->name, "Path") == 0 ||
1607 strcmp(v->name, "PATH") == 0)
1608 convert_Path_to_windows32(v->value, ';');
1609#endif
1610 *result++ = xstrdup (concat (v->name, "=", v->value));
1611 }
1612 }
1613
1614 *result = xmalloc (100);
1615 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
1616 *++result = 0;
1617
1618 hash_free (&table, 0);
1619
1620 return result_0;
1621}
1622
1623
1624#ifdef CONFIG_WITH_VALUE_LENGTH
1625/* Worker function for do_variable_definition_append() and
1626 append_expanded_string_to_variable().
1627 The APPEND argument indicates whether it's an append or prepend operation. */
1628void append_string_to_variable (struct variable *v, const char *value, unsigned int value_len, int append)
1629{
1630 /* The previous definition of the variable was recursive.
1631 The new value is the unexpanded old and new values. */
1632 unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
1633 int done_1st_prepend_copy = 0;
1634
1635 /* Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
1636 if (!value_len)
1637 return;
1638
1639 /* adjust the size. */
1640 if (v->value_alloc_len <= new_value_len + 1)
1641 {
1642 if (v->value_alloc_len < 256)
1643 v->value_alloc_len = 256;
1644 else
1645 v->value_alloc_len *= 2;
1646 if (v->value_alloc_len < new_value_len + 1)
1647 v->value_alloc_len = VAR_ALIGN_VALUE_ALLOC (new_value_len + 1 + value_len /*future*/ );
1648# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1649 if ((append || !v->value_length) && !v->rdonly_val)
1650# else
1651 if (append || !v->value_length)
1652# endif
1653 v->value = xrealloc (v->value, v->value_alloc_len);
1654 else
1655 {
1656 /* avoid the extra memcpy the xrealloc may have to do */
1657 char *new_buf = xmalloc (v->value_alloc_len);
1658 memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
1659 done_1st_prepend_copy = 1;
1660# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1661 if (v->rdonly_val)
1662 v->rdonly_val = 0;
1663 else
1664# endif
1665 free (v->value);
1666 v->value = new_buf;
1667 }
1668 MAKE_STATS_2(v->reallocs++);
1669 }
1670
1671 /* insert the new bits */
1672 if (v->value_length != 0)
1673 {
1674 if (append)
1675 {
1676 v->value[v->value_length] = ' ';
1677 memcpy (&v->value[v->value_length + 1], value, value_len + 1);
1678 }
1679 else
1680 {
1681 if (!done_1st_prepend_copy)
1682 memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
1683 v->value[value_len] = ' ';
1684 memcpy (v->value, value, value_len);
1685 }
1686 }
1687 else
1688 memcpy (v->value, value, value_len + 1);
1689 v->value_length = new_value_len;
1690}
1691
1692static struct variable *
1693do_variable_definition_append (const struct floc *flocp, struct variable *v,
1694 const char *value, unsigned int value_len,
1695 int simple_value, enum variable_origin origin,
1696 int append)
1697{
1698 if (env_overrides && origin == o_env)
1699 origin = o_env_override;
1700
1701 if (env_overrides && v->origin == o_env)
1702 /* V came from in the environment. Since it was defined
1703 before the switches were parsed, it wasn't affected by -e. */
1704 v->origin = o_env_override;
1705
1706 /* A variable of this name is already defined.
1707 If the old definition is from a stronger source
1708 than this one, don't redefine it. */
1709 if ((int) origin < (int) v->origin)
1710 return v;
1711 v->origin = origin;
1712
1713 /* location */
1714 if (flocp != 0)
1715 v->fileinfo = *flocp;
1716
1717 /* The juicy bits, append the specified value to the variable
1718 This is a heavily exercised code path in kBuild. */
1719 if (value_len == ~0U)
1720 value_len = strlen (value);
1721 if (v->recursive || simple_value)
1722 append_string_to_variable (v, value, value_len, append);
1723 else
1724 /* The previous definition of the variable was simple.
1725 The new value comes from the old value, which was expanded
1726 when it was set; and from the expanded new value. */
1727 append_expanded_string_to_variable (v, value, value_len, append);
1728
1729 /* update the variable */
1730 return v;
1731}
1732#endif /* CONFIG_WITH_VALUE_LENGTH */
1733
1734
1735static struct variable *
1736set_special_var (struct variable *var)
1737{
1738 if (streq (var->name, RECIPEPREFIX_NAME))
1739 {
1740 /* The user is resetting the command introduction prefix. This has to
1741 happen immediately, so that subsequent rules are interpreted
1742 properly. */
1743 cmd_prefix = var->value[0]=='\0' ? RECIPEPREFIX_DEFAULT : var->value[0];
1744 }
1745
1746 return var;
1747}
1748
1749
1750/* Given a variable, a value, and a flavor, define the variable.
1751 See the try_variable_definition() function for details on the parameters. */
1752
1753struct variable *
1754#ifndef CONFIG_WITH_VALUE_LENGTH
1755do_variable_definition (const struct floc *flocp, const char *varname,
1756 const char *value, enum variable_origin origin,
1757 enum variable_flavor flavor, int target_var)
1758#else /* CONFIG_WITH_VALUE_LENGTH */
1759do_variable_definition_2 (const struct floc *flocp,
1760 const char *varname, const char *value,
1761 unsigned int value_len, int simple_value,
1762 char *free_value,
1763 enum variable_origin origin,
1764 enum variable_flavor flavor,
1765 int target_var)
1766#endif /* CONFIG_WITH_VALUE_LENGTH */
1767{
1768 const char *p;
1769 char *alloc_value = NULL;
1770 struct variable *v;
1771 int append = 0;
1772 int conditional = 0;
1773 const size_t varname_len = strlen (varname); /* bird */
1774#ifdef CONFIG_WITH_VALUE_LENGTH
1775 assert (value_len == ~0U || value_len == strlen (value));
1776#endif
1777
1778 /* Calculate the variable's new value in VALUE. */
1779
1780 switch (flavor)
1781 {
1782 default:
1783 case f_bogus:
1784 /* Should not be possible. */
1785 abort ();
1786 case f_simple:
1787 /* A simple variable definition "var := value". Expand the value.
1788 We have to allocate memory since otherwise it'll clobber the
1789 variable buffer, and we may still need that if we're looking at a
1790 target-specific variable. */
1791#ifndef CONFIG_WITH_VALUE_LENGTH
1792 p = alloc_value = allocated_variable_expand (value);
1793#else /* CONFIG_WITH_VALUE_LENGTH */
1794 if (!simple_value)
1795 p = alloc_value = allocated_variable_expand_2 (value, value_len, &value_len);
1796 else
1797 {
1798 if (value_len == ~0U)
1799 value_len = strlen (value);
1800 if (!free_value)
1801 p = alloc_value = savestring (value, value_len);
1802 else
1803 {
1804 assert (value == free_value);
1805 p = alloc_value = free_value;
1806 free_value = 0;
1807 }
1808 }
1809#endif /* CONFIG_WITH_VALUE_LENGTH */
1810 break;
1811 case f_conditional:
1812 /* A conditional variable definition "var ?= value".
1813 The value is set IFF the variable is not defined yet. */
1814 v = lookup_variable (varname, varname_len);
1815 if (v)
1816#ifndef CONFIG_WITH_VALUE_LENGTH
1817 return v->special ? set_special_var (v) : v;
1818#else /* CONFIG_WITH_VALUE_LENGTH */
1819 {
1820 if (free_value)
1821 free (free_value);
1822 return v->special ? set_special_var (v) : v;
1823 }
1824#endif /* CONFIG_WITH_VALUE_LENGTH */
1825
1826 conditional = 1;
1827 flavor = f_recursive;
1828 /* FALLTHROUGH */
1829 case f_recursive:
1830 /* A recursive variable definition "var = value".
1831 The value is used verbatim. */
1832 p = value;
1833 break;
1834#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1835 case f_append:
1836 case f_prepend:
1837 {
1838 const enum variable_flavor org_flavor = flavor;
1839#else
1840 case f_append:
1841 {
1842#endif
1843
1844#ifdef CONFIG_WITH_LOCAL_VARIABLES
1845 /* If we have += but we're in a target or local variable context,
1846 we want to append only with other variables in the context of
1847 this target. */
1848 if (target_var || origin == o_local)
1849#else
1850 /* If we have += but we're in a target variable context, we want to
1851 append only with other variables in the context of this target. */
1852 if (target_var)
1853#endif
1854 {
1855 append = 1;
1856 v = lookup_variable_in_set (varname, varname_len,
1857 current_variable_set_list->set);
1858
1859 /* Don't append from the global set if a previous non-appending
1860 target-specific variable definition exists. */
1861 if (v && !v->append)
1862 append = 0;
1863 }
1864 else
1865 v = lookup_variable (varname, varname_len);
1866
1867 if (v == 0)
1868 {
1869 /* There was no old value.
1870 This becomes a normal recursive definition. */
1871 p = value;
1872 flavor = f_recursive;
1873 }
1874 else
1875 {
1876#ifdef CONFIG_WITH_VALUE_LENGTH
1877 v->append = append;
1878 v = do_variable_definition_append (flocp, v, value, value_len,
1879 simple_value, origin,
1880# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1881 org_flavor == f_append);
1882# else
1883 1);
1884# endif
1885 if (free_value)
1886 free (free_value);
1887 MAKE_STATS_2(v->changes++);
1888 return v;
1889#else /* !CONFIG_WITH_VALUE_LENGTH */
1890
1891 /* Paste the old and new values together in VALUE. */
1892
1893 unsigned int oldlen, vallen;
1894 const char *val;
1895 char *tp;
1896
1897 val = value;
1898 if (v->recursive)
1899 /* The previous definition of the variable was recursive.
1900 The new value is the unexpanded old and new values. */
1901 flavor = f_recursive;
1902 else
1903 /* The previous definition of the variable was simple.
1904 The new value comes from the old value, which was expanded
1905 when it was set; and from the expanded new value. Allocate
1906 memory for the expansion as we may still need the rest of the
1907 buffer if we're looking at a target-specific variable. */
1908 val = alloc_value = allocated_variable_expand (val);
1909
1910 oldlen = strlen (v->value);
1911 vallen = strlen (val);
1912 tp = alloca (oldlen + 1 + vallen + 1);
1913# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1914 if (org_flavor == f_prepend)
1915 {
1916 memcpy (tp, val, vallen);
1917 tp[oldlen] = ' ';
1918 memcpy (&tp[oldlen + 1], v->value, oldlen + 1);
1919 }
1920 else
1921# endif /* CONFIG_WITH_PREPEND_ASSIGNMENT */
1922 {
1923 memcpy (tp, v->value, oldlen);
1924 tp[oldlen] = ' ';
1925 memcpy (&tp[oldlen + 1], val, vallen + 1);
1926 }
1927 p = tp;
1928#endif /* !CONFIG_WITH_VALUE_LENGTH */
1929 }
1930 }
1931 }
1932
1933#ifdef __MSDOS__
1934 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1935 non-Unix systems don't conform to this default configuration (in
1936 fact, most of them don't even have `/bin'). On the other hand,
1937 $SHELL in the environment, if set, points to the real pathname of
1938 the shell.
1939 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1940 the Makefile override $SHELL from the environment. But first, we
1941 look for the basename of the shell in the directory where SHELL=
1942 points, and along the $PATH; if it is found in any of these places,
1943 we define $SHELL to be the actual pathname of the shell. Thus, if
1944 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1945 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1946 defining SHELL to be "d:/unix/bash.exe". */
1947 if ((origin == o_file || origin == o_override)
1948 && strcmp (varname, "SHELL") == 0)
1949 {
1950 PATH_VAR (shellpath);
1951 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1952
1953 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1954 if (__dosexec_find_on_path (p, NULL, shellpath))
1955 {
1956 char *tp;
1957
1958 for (tp = shellpath; *tp; tp++)
1959 if (*tp == '\\')
1960 *tp = '/';
1961
1962 v = define_variable_loc (varname, varname_len,
1963 shellpath, origin, flavor == f_recursive,
1964 flocp);
1965 }
1966 else
1967 {
1968 const char *shellbase, *bslash;
1969 struct variable *pathv = lookup_variable ("PATH", 4);
1970 char *path_string;
1971 char *fake_env[2];
1972 size_t pathlen = 0;
1973
1974 shellbase = strrchr (p, '/');
1975 bslash = strrchr (p, '\\');
1976 if (!shellbase || bslash > shellbase)
1977 shellbase = bslash;
1978 if (!shellbase && p[1] == ':')
1979 shellbase = p + 1;
1980 if (shellbase)
1981 shellbase++;
1982 else
1983 shellbase = p;
1984
1985 /* Search for the basename of the shell (with standard
1986 executable extensions) along the $PATH. */
1987 if (pathv)
1988 pathlen = strlen (pathv->value);
1989 path_string = xmalloc (5 + pathlen + 2 + 1);
1990 /* On MSDOS, current directory is considered as part of $PATH. */
1991 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1992 fake_env[0] = path_string;
1993 fake_env[1] = 0;
1994 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1995 {
1996 char *tp;
1997
1998 for (tp = shellpath; *tp; tp++)
1999 if (*tp == '\\')
2000 *tp = '/';
2001
2002 v = define_variable_loc (varname, varname_len,
2003 shellpath, origin,
2004 flavor == f_recursive, flocp);
2005 }
2006 else
2007 v = lookup_variable (varname, varname_len);
2008
2009 free (path_string);
2010 }
2011 }
2012 else
2013#endif /* __MSDOS__ */
2014#ifdef WINDOWS32
2015 if ( varname_len == sizeof("SHELL") - 1 /* bird */
2016 && (origin == o_file || origin == o_override || origin == o_command)
2017 && streq (varname, "SHELL"))
2018 {
2019 extern char *default_shell;
2020
2021 /* Call shell locator function. If it returns TRUE, then
2022 set no_default_sh_exe to indicate sh was found and
2023 set new value for SHELL variable. */
2024
2025 if (find_and_set_default_shell (p))
2026 {
2027 v = define_variable_in_set (varname, varname_len, default_shell,
2028# ifdef CONFIG_WITH_VALUE_LENGTH
2029 ~0U, 1 /* duplicate_value */,
2030# endif
2031 origin, flavor == f_recursive,
2032 (target_var
2033 ? current_variable_set_list->set
2034 : NULL),
2035 flocp);
2036 no_default_sh_exe = 0;
2037 }
2038 else
2039 {
2040 if (alloc_value)
2041 free (alloc_value);
2042
2043 alloc_value = allocated_variable_expand (p);
2044 if (find_and_set_default_shell (alloc_value))
2045 {
2046 v = define_variable_in_set (varname, varname_len, p,
2047 origin, flavor == f_recursive,
2048 (target_var
2049 ? current_variable_set_list->set
2050 : NULL),
2051 flocp);
2052 no_default_sh_exe = 0;
2053 }
2054 else
2055 v = lookup_variable (varname, varname_len);
2056 }
2057 }
2058 else
2059#endif
2060
2061 /* If we are defining variables inside an $(eval ...), we might have a
2062 different variable context pushed, not the global context (maybe we're
2063 inside a $(call ...) or something. Since this function is only ever
2064 invoked in places where we want to define globally visible variables,
2065 make sure we define this variable in the global set. */
2066
2067 v = define_variable_in_set (varname, varname_len, p,
2068#ifdef CONFIG_WITH_VALUE_LENGTH
2069 value_len, !alloc_value,
2070#endif
2071 origin, flavor == f_recursive,
2072#ifdef CONFIG_WITH_LOCAL_VARIABLES
2073 (target_var || origin == o_local
2074#else
2075 (target_var
2076#endif
2077 ? current_variable_set_list->set : NULL),
2078 flocp);
2079 v->append = append;
2080 v->conditional = conditional;
2081
2082#ifndef CONFIG_WITH_VALUE_LENGTH
2083 if (alloc_value)
2084 free (alloc_value);
2085#else
2086 if (free_value)
2087 free (free_value);
2088#endif
2089
2090 return v->special ? set_special_var (v) : v;
2091}
2092
2093
2094/* Try to interpret LINE (a null-terminated string) as a variable definition.
2095
2096 ORIGIN may be o_file, o_override, o_env, o_env_override,
2097 or o_command specifying that the variable definition comes
2098 from a makefile, an override directive, the environment with
2099 or without the -e switch, or the command line.
2100
2101 See the comments for parse_variable_definition().
2102
2103 If LINE was recognized as a variable definition, a pointer to its `struct
2104 variable' is returned. If LINE is not a variable definition, NULL is
2105 returned. */
2106
2107struct variable *
2108#ifndef CONFIG_WITH_VALUE_LENGTH
2109parse_variable_definition (struct variable *v, char *line)
2110#else
2111parse_variable_definition (struct variable *v, char *line, char *eos)
2112#endif
2113{
2114 register int c;
2115 register char *p = line;
2116 register char *beg;
2117 register char *end;
2118 enum variable_flavor flavor = f_bogus;
2119#ifndef CONFIG_WITH_VALUE_LENGTH
2120 char *name;
2121#endif
2122
2123 while (1)
2124 {
2125 c = *p++;
2126 if (c == '\0' || c == '#')
2127 return 0;
2128 if (c == '=')
2129 {
2130 end = p - 1;
2131 flavor = f_recursive;
2132 break;
2133 }
2134 else if (c == ':')
2135 if (*p == '=')
2136 {
2137 end = p++ - 1;
2138 flavor = f_simple;
2139 break;
2140 }
2141 else
2142 /* A colon other than := is a rule line, not a variable defn. */
2143 return 0;
2144 else if (c == '+' && *p == '=')
2145 {
2146 end = p++ - 1;
2147 flavor = f_append;
2148 break;
2149 }
2150#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2151 else if (c == '<' && *p == '=')
2152 {
2153 end = p++ - 1;
2154 flavor = f_prepend;
2155 break;
2156 }
2157#endif
2158 else if (c == '?' && *p == '=')
2159 {
2160 end = p++ - 1;
2161 flavor = f_conditional;
2162 break;
2163 }
2164 else if (c == '$')
2165 {
2166 /* This might begin a variable expansion reference. Make sure we
2167 don't misrecognize chars inside the reference as =, := or +=. */
2168 char closeparen;
2169 int count;
2170 c = *p++;
2171 if (c == '(')
2172 closeparen = ')';
2173 else if (c == '{')
2174 closeparen = '}';
2175 else
2176 continue; /* Nope. */
2177
2178 /* P now points past the opening paren or brace.
2179 Count parens or braces until it is matched. */
2180 count = 0;
2181 for (; *p != '\0'; ++p)
2182 {
2183 if (*p == c)
2184 ++count;
2185 else if (*p == closeparen && --count < 0)
2186 {
2187 ++p;
2188 break;
2189 }
2190 }
2191 }
2192 }
2193 v->flavor = flavor;
2194
2195 beg = next_token (line);
2196 while (end > beg && isblank ((unsigned char)end[-1]))
2197 --end;
2198 p = next_token (p);
2199 v->value = p;
2200#ifdef CONFIG_WITH_VALUE_LENGTH
2201 v->value_alloc_len = ~(unsigned int)0;
2202 v->value_length = eos != NULL ? eos - p : -1;
2203 assert (eos == NULL || strchr (p, '\0') == eos);
2204# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2205 v->rdonly_val = 0;
2206# endif
2207#endif
2208
2209 /* Expand the name, so "$(foo)bar = baz" works. */
2210#ifndef CONFIG_WITH_VALUE_LENGTH
2211 name = alloca (end - beg + 1);
2212 memcpy (name, beg, end - beg);
2213 name[end - beg] = '\0';
2214 v->name = allocated_variable_expand (name);
2215#else /* CONFIG_WITH_VALUE_LENGTH */
2216 v->name = allocated_variable_expand_2 (beg, end - beg, NULL);
2217#endif /* CONFIG_WITH_VALUE_LENGTH */
2218
2219 if (v->name[0] == '\0')
2220 fatal (&v->fileinfo, _("empty variable name"));
2221
2222 return v;
2223}
2224
2225
2226/* Try to interpret LINE (a null-terminated string) as a variable definition.
2227
2228 ORIGIN may be o_file, o_override, o_env, o_env_override, o_local,
2229 or o_command specifying that the variable definition comes
2230 from a makefile, an override directive, the environment with
2231 or without the -e switch, or the command line.
2232
2233 See the comments for parse_variable_definition().
2234
2235 If LINE was recognized as a variable definition, a pointer to its `struct
2236 variable' is returned. If LINE is not a variable definition, NULL is
2237 returned. */
2238
2239struct variable *
2240#ifndef CONFIG_WITH_VALUE_LENGTH
2241try_variable_definition (const struct floc *flocp, char *line,
2242 enum variable_origin origin, int target_var)
2243#else
2244try_variable_definition (const struct floc *flocp, char *line, char *eos,
2245 enum variable_origin origin, int target_var)
2246#endif
2247{
2248 struct variable v;
2249 struct variable *vp;
2250
2251 if (flocp != 0)
2252 v.fileinfo = *flocp;
2253 else
2254 v.fileinfo.filenm = 0;
2255
2256#ifndef CONFIG_WITH_VALUE_LENGTH
2257 if (!parse_variable_definition (&v, line))
2258 return 0;
2259
2260 vp = do_variable_definition (flocp, v.name, v.value,
2261 origin, v.flavor, target_var);
2262#else
2263 if (!parse_variable_definition (&v, line, eos))
2264 return 0;
2265
2266 vp = do_variable_definition_2 (flocp, v.name, v.value, v.value_length,
2267 0, NULL, origin, v.flavor, target_var);
2268#endif
2269
2270#ifndef CONFIG_WITH_STRCACHE2
2271 free (v.name);
2272#else
2273 free ((char *)v.name);
2274#endif
2275
2276 return vp;
2277}
2278
2279
2280#ifdef CONFIG_WITH_MAKE_STATS
2281static unsigned long var_stats_changes, var_stats_changed;
2282static unsigned long var_stats_reallocs, var_stats_realloced;
2283static unsigned long var_stats_val_len, var_stats_val_alloc_len;
2284static unsigned long var_stats_val_rdonly_len;
2285#endif
2286
2287/* Print information for variable V, prefixing it with PREFIX. */
2288
2289static void
2290print_variable (const void *item, void *arg)
2291{
2292 const struct variable *v = item;
2293 const char *prefix = arg;
2294 const char *origin;
2295
2296 switch (v->origin)
2297 {
2298 case o_default:
2299 origin = _("default");
2300 break;
2301 case o_env:
2302 origin = _("environment");
2303 break;
2304 case o_file:
2305 origin = _("makefile");
2306 break;
2307 case o_env_override:
2308 origin = _("environment under -e");
2309 break;
2310 case o_command:
2311 origin = _("command line");
2312 break;
2313 case o_override:
2314 origin = _("`override' directive");
2315 break;
2316 case o_automatic:
2317 origin = _("automatic");
2318 break;
2319#ifdef CONFIG_WITH_LOCAL_VARIABLES
2320 case o_local:
2321 origin = _("`local' directive");
2322 break;
2323#endif
2324 case o_invalid:
2325 default:
2326 abort ();
2327 }
2328 fputs ("# ", stdout);
2329 fputs (origin, stdout);
2330 if (v->fileinfo.filenm)
2331 printf (_(" (from `%s', line %lu)"),
2332 v->fileinfo.filenm, v->fileinfo.lineno);
2333#ifdef CONFIG_WITH_MAKE_STATS
2334 if (v->changes != 0)
2335 printf (_(", %u changes"), v->changes);
2336 var_stats_changes += v->changes;
2337 var_stats_changed += (v->changes != 0);
2338 if (v->reallocs != 0)
2339 printf (_(", %u reallocs"), v->reallocs);
2340 var_stats_reallocs += v->reallocs;
2341 var_stats_realloced += (v->reallocs != 0);
2342 var_stats_val_len += v->value_length;
2343 if (v->value_alloc_len)
2344 var_stats_val_alloc_len += v->value_alloc_len;
2345 else
2346 var_stats_val_rdonly_len += v->value_length;
2347 assert (v->value_length == strlen (v->value));
2348 /*assert (v->rdonly_val ? !v->value_alloc_len : v->value_alloc_len > v->value_length); - FIXME */
2349#endif /* CONFIG_WITH_MAKE_STATS */
2350 putchar ('\n');
2351 fputs (prefix, stdout);
2352
2353 /* Is this a `define'? */
2354 if (v->recursive && strchr (v->value, '\n') != 0)
2355 printf ("define %s\n%s\nendef\n", v->name, v->value);
2356 else
2357 {
2358 register char *p;
2359
2360 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
2361
2362 /* Check if the value is just whitespace. */
2363 p = next_token (v->value);
2364 if (p != v->value && *p == '\0')
2365 /* All whitespace. */
2366 printf ("$(subst ,,%s)", v->value);
2367 else if (v->recursive)
2368 fputs (v->value, stdout);
2369 else
2370 /* Double up dollar signs. */
2371 for (p = v->value; *p != '\0'; ++p)
2372 {
2373 if (*p == '$')
2374 putchar ('$');
2375 putchar (*p);
2376 }
2377 putchar ('\n');
2378 }
2379}
2380
2381
2382/* Print all the variables in SET. PREFIX is printed before
2383 the actual variable definitions (everything else is comments). */
2384
2385void
2386print_variable_set (struct variable_set *set, char *prefix)
2387{
2388#ifdef CONFIG_WITH_MAKE_STATS
2389 var_stats_changes = var_stats_changed = var_stats_reallocs
2390 = var_stats_realloced = var_stats_val_len = var_stats_val_alloc_len
2391 = var_stats_val_rdonly_len = 0;
2392
2393 hash_map_arg (&set->table, print_variable, prefix);
2394
2395 if (set->table.ht_fill)
2396 {
2397 unsigned long fragmentation;
2398
2399 fragmentation = var_stats_val_alloc_len - (var_stats_val_len - var_stats_val_rdonly_len);
2400 printf(_("# variable set value stats:\n\
2401# strings %7lu bytes, readonly %6lu bytes\n"),
2402 var_stats_val_len, var_stats_val_rdonly_len);
2403
2404 if (var_stats_val_alloc_len)
2405 printf(_("# allocated %7lu bytes, fragmentation %6lu bytes (%u%%)\n"),
2406 var_stats_val_alloc_len, fragmentation,
2407 (unsigned int)((100.0 * fragmentation) / var_stats_val_alloc_len));
2408
2409 if (var_stats_changed)
2410 printf(_("# changed %5lu (%2u%%), changes %6lu\n"),
2411 var_stats_changed,
2412 (unsigned int)((100.0 * var_stats_changed) / set->table.ht_fill),
2413 var_stats_changes);
2414
2415 if (var_stats_realloced)
2416 printf(_("# reallocated %5lu (%2u%%), reallocations %6lu\n"),
2417 var_stats_realloced,
2418 (unsigned int)((100.0 * var_stats_realloced) / set->table.ht_fill),
2419 var_stats_reallocs);
2420 }
2421#else
2422 hash_map_arg (&set->table, print_variable, prefix);
2423#endif
2424
2425 fputs (_("# variable set hash-table stats:\n"), stdout);
2426 fputs ("# ", stdout);
2427 hash_print_stats (&set->table, stdout);
2428 putc ('\n', stdout);
2429}
2430
2431/* Print the data base of variables. */
2432
2433void
2434print_variable_data_base (void)
2435{
2436 puts (_("\n# Variables\n"));
2437
2438 print_variable_set (&global_variable_set, "");
2439
2440 puts (_("\n# Pattern-specific Variable Values"));
2441
2442 {
2443 struct pattern_var *p;
2444 int rules = 0;
2445
2446 for (p = pattern_vars; p != 0; p = p->next)
2447 {
2448 ++rules;
2449 printf ("\n%s :\n", p->target);
2450 print_variable (&p->variable, "# ");
2451 }
2452
2453 if (rules == 0)
2454 puts (_("\n# No pattern-specific variable values."));
2455 else
2456 printf (_("\n# %u pattern-specific variable values"), rules);
2457 }
2458
2459#ifdef CONFIG_WITH_STRCACHE2
2460 strcache2_print_stats (&variable_strcache, "# ");
2461#endif
2462}
2463
2464#ifdef CONFIG_WITH_PRINT_STATS_SWITCH
2465void
2466print_variable_stats (void)
2467{
2468 fputs (_("\n# Global variable hash-table stats:\n# "), stdout);
2469 hash_print_stats (&global_variable_set.table, stdout);
2470 fputs ("\n", stdout);
2471}
2472#endif
2473
2474/* Print all the local variables of FILE. */
2475
2476void
2477print_file_variables (const struct file *file)
2478{
2479 if (file->variables != 0)
2480 print_variable_set (file->variables->set, "# ");
2481}
2482
2483#ifdef WINDOWS32
2484void
2485sync_Path_environment (void)
2486{
2487 char *path = allocated_variable_expand ("$(PATH)");
2488 static char *environ_path = NULL;
2489
2490 if (!path)
2491 return;
2492
2493 /*
2494 * If done this before, don't leak memory unnecessarily.
2495 * Free the previous entry before allocating new one.
2496 */
2497 if (environ_path)
2498 free (environ_path);
2499
2500 /*
2501 * Create something WINDOWS32 world can grok
2502 */
2503 convert_Path_to_windows32 (path, ';');
2504 environ_path = xstrdup (concat ("PATH", "=", path));
2505 putenv (environ_path);
2506 free (path);
2507}
2508#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