VirtualBox

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

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

kmk: Merged in changes from GNU make 3.82. Previous GNU make base version was gnumake-2008-10-28-CVS.

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