VirtualBox

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

Last change on this file since 2532 was 2532, checked in by bird, 14 years ago

kmk: Implemented the where function. Fixes #108.

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

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