VirtualBox

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

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

kmk: build fix.

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

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