VirtualBox

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

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

kmk: added $(firstdefined ) and $(lastdefined ).

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