VirtualBox

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

Last change on this file since 2717 was 2717, checked in by bird, 11 years ago

kmk: Hacking kBuild-define-*.

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