VirtualBox

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

Last change on this file since 2548 was 2548, checked in by bird, 13 years ago

kmk: hacking on a new kmk/kBuild language extension.

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