VirtualBox

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

Last change on this file since 1897 was 1897, checked in by bird, 16 years ago

kmk: added strcache2_get_ptr_hash for more efficent hashing by string pool users; replacing hash1 with that and hash2 with hash1, thus avoiding unnecessary hash2 calculations.

  • Property svn:eol-style set to native
File size: 66.7 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 Free Software
4Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 2, or (at your option) any later version.
10
11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16GNU Make; see the file COPYING. If not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
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#endif
36
37/* Chain of all pattern-specific variables. */
38
39static struct pattern_var *pattern_vars;
40
41/* Pointer to last struct in the chain, so we can add onto the end. */
42
43static struct pattern_var *last_pattern_var;
44
45/* Create a new pattern-specific variable struct. */
46
47struct pattern_var *
48create_pattern_var (const char *target, const char *suffix)
49{
50 register struct pattern_var *p = xmalloc (sizeof (struct pattern_var));
51
52 if (last_pattern_var != 0)
53 last_pattern_var->next = p;
54 else
55 pattern_vars = p;
56 last_pattern_var = p;
57 p->next = 0;
58
59 p->target = target;
60 p->len = strlen (target);
61 p->suffix = suffix + 1;
62
63 return p;
64}
65
66/* Look up a target in the pattern-specific variable list. */
67
68static struct pattern_var *
69lookup_pattern_var (struct pattern_var *start, const char *target)
70{
71 struct pattern_var *p;
72 unsigned int targlen = strlen(target);
73
74 for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
75 {
76 const char *stem;
77 unsigned int stemlen;
78
79 if (p->len > targlen)
80 /* It can't possibly match. */
81 continue;
82
83 /* From the lengths of the filename and the pattern parts,
84 find the stem: the part of the filename that matches the %. */
85 stem = target + (p->suffix - p->target - 1);
86 stemlen = targlen - p->len + 1;
87
88 /* Compare the text in the pattern before the stem, if any. */
89 if (stem > target && !strneq (p->target, target, stem - target))
90 continue;
91
92 /* Compare the text in the pattern after the stem, if any.
93 We could test simply using streq, but this way we compare the
94 first two characters immediately. This saves time in the very
95 common case where the first character matches because it is a
96 period. */
97 if (*p->suffix == stem[stemlen]
98 && (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
99 break;
100 }
101
102 return p;
103}
104
105
106#ifdef CONFIG_WITH_STRCACHE2
107static struct strcache2 variable_strcache;
108#endif
109
110/* Hash table of all global variable definitions. */
111
112static unsigned long
113variable_hash_1 (const void *keyv)
114{
115 struct variable const *key = (struct variable const *) keyv;
116#ifndef CONFIG_WITH_STRCACHE2
117 return_STRING_N_HASH_1 (key->name, key->length);
118#else
119 /* all requests are served from the cache. */
120 return strcache2_get_ptr_hash (&variable_strcache, key->name);
121#endif
122}
123
124static unsigned long
125variable_hash_2 (const void *keyv)
126{
127 struct variable const *key = (struct variable const *) keyv;
128#ifndef CONFIG_WITH_STRCACHE2
129 return_STRING_N_HASH_2 (key->name, key->length);
130#else
131 /* all requests are served from the cache. */
132 return strcache2_get_hash2 (&variable_strcache, key->name);
133#endif
134}
135
136static int
137variable_hash_cmp (const void *xv, const void *yv)
138{
139 struct variable const *x = (struct variable const *) xv;
140 struct variable const *y = (struct variable const *) yv;
141#ifndef CONFIG_WITH_STRCACHE2
142 int result = x->length - y->length;
143 if (result)
144 return result;
145
146 return_STRING_N_COMPARE (x->name, y->name, x->length);
147#else /* CONFIG_WITH_STRCACHE2 */
148
149 /* everything is in the cache. */
150 return x->name == y->name ? 0 : -1;
151#endif /* CONFIG_WITH_STRCACHE2 */
152}
153
154#ifndef VARIABLE_BUCKETS
155# ifdef KMK /* Move to Makefile.kmk? (insanely high, but wtf, it gets the collitions down) */
156# define VARIABLE_BUCKETS 65535
157# else /*!KMK*/
158#define VARIABLE_BUCKETS 523
159# endif /*!KMK*/
160#endif
161#ifndef PERFILE_VARIABLE_BUCKETS
162# ifdef KMK /* Move to Makefile.kmk? */
163# define PERFILE_VARIABLE_BUCKETS 127
164# else
165#define PERFILE_VARIABLE_BUCKETS 23
166# endif
167#endif
168#ifndef SMALL_SCOPE_VARIABLE_BUCKETS
169# ifdef KMK /* Move to Makefile.kmk? */
170# define SMALL_SCOPE_VARIABLE_BUCKETS 63
171# else
172#define SMALL_SCOPE_VARIABLE_BUCKETS 13
173# endif
174#endif
175
176static struct variable_set global_variable_set;
177static struct variable_set_list global_setlist
178 = { 0, &global_variable_set };
179struct variable_set_list *current_variable_set_list = &global_setlist;
180
181
182/* Implement variables. */
183
184void
185init_hash_global_variable_set (void)
186{
187 hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
188 variable_hash_1, variable_hash_2, variable_hash_cmp);
189#ifdef CONFIG_WITH_STRCACHE2
190 strcache2_init (&variable_strcache, "variable", 65536, 0, 0, 0);
191#endif
192}
193
194/* Define variable named NAME with value VALUE in SET. VALUE is copied.
195 LENGTH is the length of NAME, which does not need to be null-terminated.
196 ORIGIN specifies the origin of the variable (makefile, command line
197 or environment).
198 If RECURSIVE is nonzero a flag is set in the variable saying
199 that it should be recursively re-expanded. */
200
201#ifdef CONFIG_WITH_VALUE_LENGTH
202struct variable *
203define_variable_in_set (const char *name, unsigned int length,
204 const char *value, unsigned int value_len,
205 int duplicate_value, enum variable_origin origin,
206 int recursive, struct variable_set *set,
207 const struct floc *flocp)
208#else
209struct variable *
210define_variable_in_set (const char *name, unsigned int length,
211 const char *value, enum variable_origin origin,
212 int recursive, struct variable_set *set,
213 const struct floc *flocp)
214#endif
215{
216 struct variable *v;
217 struct variable **var_slot;
218 struct variable var_key;
219
220 if (set == NULL)
221 set = &global_variable_set;
222
223#ifndef CONFIG_WITH_STRCACHE2
224 var_key.name = (char *) name;
225 var_key.length = length;
226 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
227
228 if (env_overrides && origin == o_env)
229 origin = o_env_override;
230
231 v = *var_slot;
232#else /* CONFIG_WITH_STRCACHE2 */
233 var_key.name = name = strcache2_add (&variable_strcache, name, length);
234 var_key.length = length;
235 if ( set != &global_variable_set
236 || !(v = strcache2_get_user_val (&variable_strcache, var_key.name)))
237 {
238 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
239 v = *var_slot;
240 }
241 else
242 assert (!v || (v->name == name && !HASH_VACANT (v)));
243#endif /* CONFIG_WITH_STRCACHE2 */
244 if (! HASH_VACANT (v))
245 {
246 if (env_overrides && v->origin == o_env)
247 /* V came from in the environment. Since it was defined
248 before the switches were parsed, it wasn't affected by -e. */
249 v->origin = o_env_override;
250
251 /* A variable of this name is already defined.
252 If the old definition is from a stronger source
253 than this one, don't redefine it. */
254 if ((int) origin >= (int) v->origin)
255 {
256#ifdef CONFIG_WITH_VALUE_LENGTH
257 if (value_len == ~0U)
258 value_len = strlen (value);
259 else
260 assert (value_len == strlen (value));
261 if (!duplicate_value)
262 {
263 if (v->value != 0)
264 free (v->value);
265 v->value = (char *)value;
266 v->value_alloc_len = value_len + 1;
267 }
268 else
269 {
270 if ((unsigned int)v->value_alloc_len <= value_len)
271 {
272 free (v->value);
273 v->value_alloc_len = (value_len + 0x40) & ~0x3f;
274 v->value = xmalloc (v->value_alloc_len);
275 }
276 memcpy (v->value, value, value_len + 1);
277 }
278 v->value_length = value_len;
279#else
280 if (v->value != 0)
281 free (v->value);
282 v->value = xstrdup (value);
283#endif
284 if (flocp != 0)
285 v->fileinfo = *flocp;
286 else
287 v->fileinfo.filenm = 0;
288 v->origin = origin;
289 v->recursive = recursive;
290 }
291 return v;
292 }
293
294 /* Create a new variable definition and add it to the hash table. */
295
296#ifndef CONFIG_WITH_ALLOC_CACHES
297 v = xmalloc (sizeof (struct variable));
298#else
299 v = alloccache_alloc (&variable_cache);
300#endif
301#ifndef CONFIG_WITH_STRCACHE2
302 v->name = savestring (name, length);
303#else
304 v->name = name; /* already cached. */
305#endif
306 v->length = length;
307 hash_insert_at (&set->table, v, var_slot);
308#ifdef CONFIG_WITH_VALUE_LENGTH
309 if (value_len == ~0U)
310 value_len = strlen (value);
311 else
312 assert (value_len == strlen (value));
313 v->value_length = value_len;
314 if (!duplicate_value)
315 {
316 v->value_alloc_len = value_len + 1;
317 v->value = (char *)value;
318 }
319 else
320 {
321 v->value_alloc_len = (value_len + 32) & ~31;
322 v->value = xmalloc (v->value_alloc_len);
323 memcpy (v->value, value, value_len + 1);
324 }
325#else
326 v->value = xstrdup (value);
327#endif
328 if (flocp != 0)
329 v->fileinfo = *flocp;
330 else
331 v->fileinfo.filenm = 0;
332 v->origin = origin;
333 v->recursive = recursive;
334 v->special = 0;
335 v->expanding = 0;
336 v->exp_count = 0;
337 v->per_target = 0;
338 v->append = 0;
339 v->export = v_default;
340
341 v->exportable = 1;
342 if (*name != '_' && (*name < 'A' || *name > 'Z')
343 && (*name < 'a' || *name > 'z'))
344 v->exportable = 0;
345 else
346 {
347 for (++name; *name != '\0'; ++name)
348 if (*name != '_' && (*name < 'a' || *name > 'z')
349 && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
350 break;
351
352 if (*name != '\0')
353 v->exportable = 0;
354 }
355
356#ifdef CONFIG_WITH_STRCACHE2
357 /* If it's the global set, remember the variable. */
358 if (set == &global_variable_set)
359 strcache2_set_user_val (&variable_strcache, v->name, v);
360#endif
361 return v;
362}
363
364
365/* If the variable passed in is "special", handle its special nature.
366 Currently there are two such variables, both used for introspection:
367 .VARIABLES expands to a list of all the variables defined in this instance
368 of make.
369 .TARGETS expands to a list of all the targets defined in this
370 instance of make.
371 Returns the variable reference passed in. */
372
373#define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
374
375static struct variable *
376handle_special_var (struct variable *var)
377{
378 static unsigned long last_var_count = 0;
379
380
381 /* This one actually turns out to be very hard, due to the way the parser
382 records targets. The way it works is that target information is collected
383 internally until make knows the target is completely specified. It unitl
384 it sees that some new construct (a new target or variable) is defined that
385 it knows the previous one is done. In short, this means that if you do
386 this:
387
388 all:
389
390 TARGS := $(.TARGETS)
391
392 then $(TARGS) won't contain "all", because it's not until after the
393 variable is created that the previous target is completed.
394
395 Changing this would be a major pain. I think a less complex way to do it
396 would be to pre-define the target files as soon as the first line is
397 parsed, then come back and do the rest of the definition as now. That
398 would allow $(.TARGETS) to be correct without a major change to the way
399 the parser works.
400
401 if (streq (var->name, ".TARGETS"))
402 var->value = build_target_list (var->value);
403 else
404 */
405
406 if (streq (var->name, ".VARIABLES")
407 && global_variable_set.table.ht_fill != last_var_count)
408 {
409 unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
410 unsigned long len;
411 char *p;
412 struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
413 struct variable **end = &vp[global_variable_set.table.ht_size];
414
415 /* Make sure we have at least MAX bytes in the allocated buffer. */
416 var->value = xrealloc (var->value, max);
417
418 /* Walk through the hash of variables, constructing a list of names. */
419 p = var->value;
420 len = 0;
421 for (; vp < end; ++vp)
422 if (!HASH_VACANT (*vp))
423 {
424 struct variable *v = *vp;
425 int l = v->length;
426
427 len += l + 1;
428 if (len > max)
429 {
430 unsigned long off = p - var->value;
431
432 max += EXPANSION_INCREMENT (l + 1);
433 var->value = xrealloc (var->value, max);
434 p = &var->value[off];
435 }
436
437 memcpy (p, v->name, l);
438 p += l;
439 *(p++) = ' ';
440 }
441 *(p-1) = '\0';
442
443 /* Remember how many variables are in our current count. Since we never
444 remove variables from the list, this is a reliable way to know whether
445 the list is up to date or needs to be recomputed. */
446
447 last_var_count = global_variable_set.table.ht_fill;
448 }
449
450 return var;
451}
452
453
454
455/* Lookup a variable whose name is a string starting at NAME
456 and with LENGTH chars. NAME need not be null-terminated.
457 Returns address of the `struct variable' containing all info
458 on the variable, or nil if no such variable is defined. */
459
460struct variable *
461lookup_variable (const char *name, unsigned int length)
462{
463 const struct variable_set_list *setlist;
464 struct variable var_key;
465#ifdef KMK
466 unsigned int hash_2 = 0;
467#endif
468#ifdef CONFIG_WITH_STRCACHE2
469 const char *cached_name;
470
471 /* lookup the name in the string case, if it's not there it won't
472 be in any of the sets either. */
473 cached_name = strcache2_lookup(&variable_strcache, name, length);
474 if (!cached_name)
475 return NULL;
476 name = cached_name;
477#endif /* CONFIG_WITH_STRCACHE2 */
478
479 var_key.name = (char *) name;
480 var_key.length = length;
481
482 for (setlist = current_variable_set_list;
483 setlist != 0; setlist = setlist->next)
484 {
485#ifdef KMK /* bird: speed */
486 struct hash_table *ht = &setlist->set->table;
487 unsigned int hash_1 = strcache2_get_ptr_hash (&variable_strcache, name);
488 struct variable *v;
489
490 ht->ht_lookups++;
491 for (;;)
492 {
493 hash_1 &= (ht->ht_size - 1);
494 v = (struct variable *)ht->ht_vec[hash_1];
495
496 if (v == 0)
497 break;
498 if ((void *)v != hash_deleted_item)
499 {
500 if (variable_hash_cmp(&var_key, v) == 0)
501 {
502# ifndef NDEBUG
503 struct variable *v2 = (struct variable *) hash_find_item ((struct hash_table *) &setlist->set->table, &var_key);
504 assert (v2 == v);
505# endif
506 return v->special ? handle_special_var (v) : v;
507 }
508 ht->ht_collisions++;
509 }
510 if (!hash_2)
511 {
512 hash_2 = strcache2_get_hash2 (&variable_strcache, name);
513 assert (hash_2 & 1);
514 }
515 hash_1 += hash_2;
516 }
517
518#else /* !KMK */
519 const struct variable_set *set = setlist->set;
520 struct variable *v;
521
522 v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
523 if (v)
524 return v->special ? handle_special_var (v) : v;
525#endif /* !KMK */
526 }
527
528#ifdef VMS
529 /* since we don't read envp[] on startup, try to get the
530 variable via getenv() here. */
531 {
532 char *vname = alloca (length + 1);
533 char *value;
534 strncpy (vname, name, length);
535 vname[length] = 0;
536 value = getenv (vname);
537 if (value != 0)
538 {
539 char *sptr;
540 int scnt;
541
542 sptr = value;
543 scnt = 0;
544
545 while ((sptr = strchr (sptr, '$')))
546 {
547 scnt++;
548 sptr++;
549 }
550
551 if (scnt > 0)
552 {
553 char *nvalue;
554 char *nptr;
555
556 nvalue = alloca (strlen (value) + scnt + 1);
557 sptr = value;
558 nptr = nvalue;
559
560 while (*sptr)
561 {
562 if (*sptr == '$')
563 {
564 *nptr++ = '$';
565 *nptr++ = '$';
566 }
567 else
568 {
569 *nptr++ = *sptr;
570 }
571 sptr++;
572 }
573
574 *nptr = '\0';
575 return define_variable (vname, length, nvalue, o_env, 1);
576
577 }
578
579 return define_variable (vname, length, value, o_env, 1);
580 }
581 }
582#endif /* VMS */
583
584 return 0;
585}
586
587
588/* Lookup a variable whose name is a string starting at NAME
589 and with LENGTH chars in set SET. NAME need not be null-terminated.
590 Returns address of the `struct variable' containing all info
591 on the variable, or nil if no such variable is defined. */
592
593struct variable *
594lookup_variable_in_set (const char *name, unsigned int length,
595 const struct variable_set *set)
596{
597 struct variable var_key;
598#ifdef CONFIG_WITH_STRCACHE2
599 const char *cached_name;
600
601 /* lookup the name in the string case, if it's not there it won't
602 be in any of the sets either. Optimize lookups in the global set. */
603 cached_name = strcache2_lookup(&variable_strcache, name, length);
604 if (!cached_name)
605 return NULL;
606
607 if (set == &global_variable_set)
608 {
609 struct variable *v;
610 v = strcache2_get_user_val (&variable_strcache, cached_name);
611 assert (!v || v->name == cached_name);
612 return v;
613 }
614
615 name = cached_name;
616#endif /* CONFIG_WITH_STRCACHE2 */
617
618 var_key.name = (char *) name;
619 var_key.length = length;
620
621 return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
622}
623
624
625/* Initialize FILE's variable set list. If FILE already has a variable set
626 list, the topmost variable set is left intact, but the the rest of the
627 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
628 rule, then we will use the "root" double-colon target's variable set as the
629 parent of FILE's variable set.
630
631 If we're READING a makefile, don't do the pattern variable search now,
632 since the pattern variable might not have been defined yet. */
633
634void
635initialize_file_variables (struct file *file, int reading)
636{
637 struct variable_set_list *l = file->variables;
638
639 if (l == 0)
640 {
641#ifndef CONFIG_WITH_ALLOC_CACHES
642 l = (struct variable_set_list *)
643 xmalloc (sizeof (struct variable_set_list));
644 l->set = xmalloc (sizeof (struct variable_set));
645#else
646 l = (struct variable_set_list *)
647 alloccache_alloc (&variable_set_list_cache);
648 l->set = (struct variable_set *)
649 alloccache_alloc (&variable_set_cache);
650#endif
651 hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
652 variable_hash_1, variable_hash_2, variable_hash_cmp);
653 file->variables = l;
654 }
655
656 /* If this is a double-colon, then our "parent" is the "root" target for
657 this double-colon rule. Since that rule has the same name, parent,
658 etc. we can just use its variables as the "next" for ours. */
659
660 if (file->double_colon && file->double_colon != file)
661 {
662 initialize_file_variables (file->double_colon, reading);
663 l->next = file->double_colon->variables;
664 return;
665 }
666
667 if (file->parent == 0)
668 l->next = &global_setlist;
669 else
670 {
671 initialize_file_variables (file->parent, reading);
672 l->next = file->parent->variables;
673 }
674
675 /* If we're not reading makefiles and we haven't looked yet, see if
676 we can find pattern variables for this target. */
677
678 if (!reading && !file->pat_searched)
679 {
680 struct pattern_var *p;
681
682 p = lookup_pattern_var (0, file->name);
683 if (p != 0)
684 {
685 struct variable_set_list *global = current_variable_set_list;
686
687 /* We found at least one. Set up a new variable set to accumulate
688 all the pattern variables that match this target. */
689
690 file->pat_variables = create_new_variable_set ();
691 current_variable_set_list = file->pat_variables;
692
693 do
694 {
695 /* We found one, so insert it into the set. */
696
697 struct variable *v;
698
699 if (p->variable.flavor == f_simple)
700 {
701 v = define_variable_loc (
702 p->variable.name, strlen (p->variable.name),
703 p->variable.value, p->variable.origin,
704 0, &p->variable.fileinfo);
705
706 v->flavor = f_simple;
707 }
708 else
709 {
710#ifndef CONFIG_WITH_VALUE_LENGTH
711 v = do_variable_definition (
712 &p->variable.fileinfo, p->variable.name,
713 p->variable.value, p->variable.origin,
714 p->variable.flavor, 1);
715#else
716 v = do_variable_definition_2 (
717 &p->variable.fileinfo, p->variable.name,
718 p->variable.value, p->variable.value_length, 0, 0,
719 p->variable.origin, p->variable.flavor, 1);
720#endif
721 }
722
723 /* Also mark it as a per-target and copy export status. */
724 v->per_target = p->variable.per_target;
725 v->export = p->variable.export;
726 }
727 while ((p = lookup_pattern_var (p, file->name)) != 0);
728
729 current_variable_set_list = global;
730 }
731 file->pat_searched = 1;
732 }
733
734 /* If we have a pattern variable match, set it up. */
735
736 if (file->pat_variables != 0)
737 {
738 file->pat_variables->next = l->next;
739 l->next = file->pat_variables;
740 }
741}
742
743
744/* Pop the top set off the current variable set list,
745 and free all its storage. */
746
747struct variable_set_list *
748create_new_variable_set (void)
749{
750 register struct variable_set_list *setlist;
751 register struct variable_set *set;
752
753#ifndef CONFIG_WITH_ALLOC_CACHES
754 set = xmalloc (sizeof (struct variable_set));
755#else
756 set = (struct variable_set *) alloccache_alloc (&variable_set_cache);
757#endif
758 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
759 variable_hash_1, variable_hash_2, variable_hash_cmp);
760
761#ifndef CONFIG_WITH_ALLOC_CACHES
762 setlist = (struct variable_set_list *)
763 xmalloc (sizeof (struct variable_set_list));
764#else
765 setlist = (struct variable_set_list *)
766 alloccache_alloc (&variable_set_list_cache);
767#endif
768 setlist->set = set;
769 setlist->next = current_variable_set_list;
770
771 return setlist;
772}
773
774static void
775free_variable_name_and_value (const void *item)
776{
777 struct variable *v = (struct variable *) item;
778#ifndef CONFIG_WITH_STRCACHE2
779 free (v->name);
780#endif
781 free (v->value);
782}
783
784void
785free_variable_set (struct variable_set_list *list)
786{
787 hash_map (&list->set->table, free_variable_name_and_value);
788#ifndef CONFIG_WITH_ALLOC_CACHES
789 hash_free (&list->set->table, 1);
790 free (list->set);
791 free (list);
792#else
793 hash_free_cached (&list->set->table, 1, &variable_cache);
794 alloccache_free (&variable_set_cache, list->set);
795 alloccache_free (&variable_set_list_cache, list);
796#endif
797}
798
799/* Create a new variable set and push it on the current setlist.
800 If we're pushing a global scope (that is, the current scope is the global
801 scope) then we need to "push" it the other way: file variable sets point
802 directly to the global_setlist so we need to replace that with the new one.
803 */
804
805struct variable_set_list *
806push_new_variable_scope (void)
807{
808 current_variable_set_list = create_new_variable_set();
809 if (current_variable_set_list->next == &global_setlist)
810 {
811 /* It was the global, so instead of new -> &global we want to replace
812 &global with the new one and have &global -> new, with current still
813 pointing to &global */
814 struct variable_set *set = current_variable_set_list->set;
815 current_variable_set_list->set = global_setlist.set;
816 global_setlist.set = set;
817 current_variable_set_list->next = global_setlist.next;
818 global_setlist.next = current_variable_set_list;
819 current_variable_set_list = &global_setlist;
820 }
821 return (current_variable_set_list);
822}
823
824void
825pop_variable_scope (void)
826{
827 struct variable_set_list *setlist;
828 struct variable_set *set;
829
830 /* Can't call this if there's no scope to pop! */
831 assert(current_variable_set_list->next != NULL);
832
833 if (current_variable_set_list != &global_setlist)
834 {
835 /* We're not pointing to the global setlist, so pop this one. */
836 setlist = current_variable_set_list;
837 set = setlist->set;
838 current_variable_set_list = setlist->next;
839 }
840 else
841 {
842 /* This set is the one in the global_setlist, but there is another global
843 set beyond that. We want to copy that set to global_setlist, then
844 delete what used to be in global_setlist. */
845 setlist = global_setlist.next;
846 set = global_setlist.set;
847 global_setlist.set = setlist->set;
848 global_setlist.next = setlist->next;
849 }
850
851 /* Free the one we no longer need. */
852#ifndef CONFIG_WITH_ALLOC_CACHES
853 free (setlist);
854 hash_map (&set->table, free_variable_name_and_value);
855 hash_free (&set->table, 1);
856 free (set);
857#else
858 alloccache_free (&variable_set_list_cache, setlist);
859 hash_map (&set->table, free_variable_name_and_value);
860 hash_free_cached (&set->table, 1, &variable_cache);
861 alloccache_free (&variable_set_cache, set);
862#endif
863}
864
865
866/* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
867
868static void
869merge_variable_sets (struct variable_set *to_set,
870 struct variable_set *from_set)
871{
872 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
873 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
874
875 for ( ; from_var_slot < from_var_end; from_var_slot++)
876 if (! HASH_VACANT (*from_var_slot))
877 {
878 struct variable *from_var = *from_var_slot;
879 struct variable **to_var_slot
880 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
881 if (HASH_VACANT (*to_var_slot))
882 hash_insert_at (&to_set->table, from_var, to_var_slot);
883 else
884 {
885 /* GKM FIXME: delete in from_set->table */
886 free (from_var->value);
887 free (from_var);
888 }
889 }
890}
891
892/* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
893
894void
895merge_variable_set_lists (struct variable_set_list **setlist0,
896 struct variable_set_list *setlist1)
897{
898 struct variable_set_list *to = *setlist0;
899 struct variable_set_list *last0 = 0;
900
901 /* If there's nothing to merge, stop now. */
902 if (!setlist1)
903 return;
904
905 /* This loop relies on the fact that all setlists terminate with the global
906 setlist (before NULL). If that's not true, arguably we SHOULD die. */
907 if (to)
908 while (setlist1 != &global_setlist && to != &global_setlist)
909 {
910 struct variable_set_list *from = setlist1;
911 setlist1 = setlist1->next;
912
913 merge_variable_sets (to->set, from->set);
914
915 last0 = to;
916 to = to->next;
917 }
918
919 if (setlist1 != &global_setlist)
920 {
921 if (last0 == 0)
922 *setlist0 = setlist1;
923 else
924 last0->next = setlist1;
925 }
926}
927
928
929/* Define the automatic variables, and record the addresses
930 of their structures so we can change their values quickly. */
931
932void
933define_automatic_variables (void)
934{
935#if defined(WINDOWS32) || defined(__EMX__)
936 extern char* default_shell;
937#else
938 extern char default_shell[];
939#endif
940 register struct variable *v;
941#ifndef KMK
942 char buf[200];
943#else
944 char buf[1024];
945 const char *val;
946 struct variable *envvar1;
947 struct variable *envvar2;
948#endif
949
950 sprintf (buf, "%u", makelevel);
951 (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
952
953 sprintf (buf, "%s%s%s",
954 version_string,
955 (remote_description == 0 || remote_description[0] == '\0')
956 ? "" : "-",
957 (remote_description == 0 || remote_description[0] == '\0')
958 ? "" : remote_description);
959#ifndef KMK
960 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
961#else /* KMK */
962
963 /* Define KMK_VERSION to indicate kMk. */
964 (void) define_variable ("KMK_VERSION", 11, buf, o_default, 0);
965
966 /* Define KBUILD_VERSION* */
967 sprintf (buf, "%d", KBUILD_VERSION_MAJOR);
968 define_variable ("KBUILD_VERSION_MAJOR", sizeof ("KBUILD_VERSION_MAJOR") - 1,
969 buf, o_default, 0);
970 sprintf (buf, "%d", KBUILD_VERSION_MINOR);
971 define_variable ("KBUILD_VERSION_MINOR", sizeof("KBUILD_VERSION_MINOR") - 1,
972 buf, o_default, 0);
973 sprintf (buf, "%d", KBUILD_VERSION_PATCH);
974 define_variable ("KBUILD_VERSION_PATCH", sizeof ("KBUILD_VERSION_PATCH") - 1,
975 buf, o_default, 0);
976 sprintf (buf, "%d", KBUILD_SVN_REV);
977 define_variable ("KBUILD_KMK_REVISION", sizeof ("KBUILD_KMK_REVISION") - 1,
978 buf, o_default, 0);
979
980 sprintf (buf, "%d.%d.%d-r%d", KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR,
981 KBUILD_VERSION_PATCH, KBUILD_SVN_REV);
982 define_variable ("KBUILD_VERSION", sizeof ("KBUILD_VERSION") - 1,
983 buf, o_default, 0);
984
985 /* The host defaults. The BUILD_* stuff will be replaced by KBUILD_* soon. */
986 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST"));
987 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM"));
988 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST;
989 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
990 error (NULL, _("KBUILD_HOST and BUILD_PLATFORM differs, using KBUILD_HOST=%s."), val);
991 if (!envvar1)
992 define_variable ("KBUILD_HOST", sizeof ("KBUILD_HOST") - 1,
993 val, o_default, 0);
994 if (!envvar2)
995 define_variable ("BUILD_PLATFORM", sizeof ("BUILD_PLATFORM") - 1,
996 val, o_default, 0);
997
998 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_ARCH"));
999 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_ARCH"));
1000 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_ARCH;
1001 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1002 error (NULL, _("KBUILD_HOST_ARCH and BUILD_PLATFORM_ARCH differs, using KBUILD_HOST_ARCH=%s."), val);
1003 if (!envvar1)
1004 define_variable ("KBUILD_HOST_ARCH", sizeof ("KBUILD_HOST_ARCH") - 1,
1005 val, o_default, 0);
1006 if (!envvar2)
1007 define_variable ("BUILD_PLATFORM_ARCH", sizeof ("BUILD_PLATFORM_ARCH") - 1,
1008 val, o_default, 0);
1009
1010 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_CPU"));
1011 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_CPU"));
1012 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_CPU;
1013 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1014 error (NULL, _("KBUILD_HOST_CPU and BUILD_PLATFORM_CPU differs, using KBUILD_HOST_CPU=%s."), val);
1015 if (!envvar1)
1016 define_variable ("KBUILD_HOST_CPU", sizeof ("KBUILD_HOST_CPU") - 1,
1017 val, o_default, 0);
1018 if (!envvar2)
1019 define_variable ("BUILD_PLATFORM_CPU", sizeof ("BUILD_PLATFORM_CPU") - 1,
1020 val, o_default, 0);
1021
1022 /* The kBuild locations. */
1023 define_variable ("KBUILD_PATH", sizeof ("KBUILD_PATH") - 1,
1024 get_kbuild_path (), o_default, 0);
1025 define_variable ("KBUILD_BIN_PATH", sizeof ("KBUILD_BIN_PATH") - 1,
1026 get_kbuild_bin_path (), o_default, 0);
1027
1028 define_variable ("PATH_KBUILD", sizeof ("PATH_KBUILD") - 1,
1029 get_kbuild_path (), o_default, 0);
1030 define_variable ("PATH_KBUILD_BIN", sizeof ("PATH_KBUILD_BIN") - 1,
1031 get_kbuild_bin_path (), o_default, 0);
1032
1033 /* Define KMK_FEATURES to indicate various working KMK features. */
1034# if defined (CONFIG_WITH_RSORT) \
1035 && defined (CONFIG_WITH_ABSPATHEX) \
1036 && defined (CONFIG_WITH_TOUPPER_TOLOWER) \
1037 && defined (CONFIG_WITH_DEFINED) \
1038 && defined (CONFIG_WITH_VALUE_LENGTH) && defined (CONFIG_WITH_COMPARE) \
1039 && defined (CONFIG_WITH_STACK) \
1040 && defined (CONFIG_WITH_MATH) \
1041 && defined (CONFIG_WITH_XARGS) \
1042 && defined (CONFIG_WITH_EXPLICIT_MULTITARGET) \
1043 && defined (CONFIG_WITH_PREPEND_ASSIGNMENT) \
1044 && defined (CONFIG_WITH_SET_CONDITIONALS) \
1045 && defined (CONFIG_WITH_DATE) \
1046 && defined (CONFIG_WITH_FILE_SIZE) \
1047 && defined (CONFIG_WITH_WHICH) \
1048 && defined (CONFIG_WITH_EVALPLUS) \
1049 && defined (CONFIG_WITH_MAKE_STATS) \
1050 && defined (CONFIG_WITH_COMMANDS_FUNC) \
1051 && defined (KMK_HELPERS)
1052 (void) define_variable ("KMK_FEATURES", 12,
1053 "append-dash-n abspath includedep-queue"
1054 " rsort"
1055 " abspathex"
1056 " toupper tolower"
1057 " defined"
1058 " comp-vars comp-cmds comp-cmds-ex"
1059 " stack"
1060 " math-int"
1061 " xargs"
1062 " explicit-multitarget"
1063 " prepend-assignment"
1064 " set-conditionals"
1065 " date"
1066 " file-size"
1067 " expr if-expr"
1068 " which"
1069 " evalctx evalval evalvalctx evalcall evalcall2"
1070 " make-stats"
1071 " commands"
1072 " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one "
1073 , o_default, 0);
1074# else /* MSC can't deal with strings mixed with #if/#endif, thus the slow way. */
1075# error "All features should be enabled by default!"
1076 strcpy (buf, "append-dash-n abspath includedep-queue");
1077# if defined (CONFIG_WITH_RSORT)
1078 strcat (buf, " rsort");
1079# endif
1080# if defined (CONFIG_WITH_ABSPATHEX)
1081 strcat (buf, " abspathex");
1082# endif
1083# if defined (CONFIG_WITH_TOUPPER_TOLOWER)
1084 strcat (buf, " toupper tolower");
1085# endif
1086# if defined (CONFIG_WITH_DEFINED)
1087 strcat (buf, " defined");
1088# endif
1089# if defined (CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
1090 strcat (buf, " comp-vars comp-cmds comp-cmds-ex");
1091# endif
1092# if defined (CONFIG_WITH_STACK)
1093 strcat (buf, " stack");
1094# endif
1095# if defined (CONFIG_WITH_MATH)
1096 strcat (buf, " math-int");
1097# endif
1098# if defined (CONFIG_WITH_XARGS)
1099 strcat (buf, " xargs");
1100# endif
1101# if defined (CONFIG_WITH_EXPLICIT_MULTITARGET)
1102 strcat (buf, " explicit-multitarget");
1103# endif
1104# if defined (CONFIG_WITH_PREPEND_ASSIGNMENT)
1105 strcat (buf, " prepend-assignment");
1106# endif
1107# if defined (CONFIG_WITH_SET_CONDITIONALS)
1108 strcat (buf, " set-conditionals");
1109# endif
1110# if defined (CONFIG_WITH_DATE)
1111 strcat (buf, " date");
1112# endif
1113# if defined (CONFIG_WITH_FILE_SIZE)
1114 strcat (buf, " file-size");
1115# endif
1116# if defined (CONFIG_WITH_IF_CONDITIONALS)
1117 strcat (buf, " expr if-expr");
1118# endif
1119# if defined (CONFIG_WITH_WHICH)
1120 strcat (buf, " which");
1121# endif
1122# if defined (CONFIG_WITH_EVALPLUS)
1123 strcat (buf, " evalctx evalval evalvalctx evalcall evalcall2");
1124# endif
1125# if defined (CONFIG_WITH_MAKE_STATS)
1126 strcat (buf, " make-stats");
1127# endif
1128# if defined (CONFIG_WITH_COMMANDS_FUNC)
1129 strcat (buf, " commands");
1130# endif
1131# if defined (KMK_HELPERS)
1132 strcat (buf, " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one");
1133# endif
1134 (void) define_variable ("KMK_FEATURES", 12, buf, o_default, 0);
1135# endif
1136
1137#endif /* KMK */
1138
1139#ifdef CONFIG_WITH_KMK_BUILTIN
1140 /* The supported kMk Builtin commands. */
1141 (void) define_variable ("KMK_BUILTIN", 11, "append cat chmod cp cmp echo expr install kDepIDB ln md5sum mkdir mv printf rm rmdir test", o_default, 0);
1142#endif
1143
1144#ifdef __MSDOS__
1145 /* Allow to specify a special shell just for Make,
1146 and use $COMSPEC as the default $SHELL when appropriate. */
1147 {
1148 static char shell_str[] = "SHELL";
1149 const int shlen = sizeof (shell_str) - 1;
1150 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
1151 struct variable *comp = lookup_variable ("COMSPEC", 7);
1152
1153 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
1154 if (mshp)
1155 (void) define_variable (shell_str, shlen,
1156 mshp->value, o_env_override, 0);
1157 else if (comp)
1158 {
1159 /* $COMSPEC shouldn't override $SHELL. */
1160 struct variable *shp = lookup_variable (shell_str, shlen);
1161
1162 if (!shp)
1163 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
1164 }
1165 }
1166#elif defined(__EMX__)
1167 {
1168 static char shell_str[] = "SHELL";
1169 const int shlen = sizeof (shell_str) - 1;
1170 struct variable *shell = lookup_variable (shell_str, shlen);
1171 struct variable *replace = lookup_variable ("MAKESHELL", 9);
1172
1173 /* if $MAKESHELL is defined in the environment assume o_env_override */
1174 if (replace && *replace->value && replace->origin == o_env)
1175 replace->origin = o_env_override;
1176
1177 /* if $MAKESHELL is not defined use $SHELL but only if the variable
1178 did not come from the environment */
1179 if (!replace || !*replace->value)
1180 if (shell && *shell->value && (shell->origin == o_env
1181 || shell->origin == o_env_override))
1182 {
1183 /* overwrite whatever we got from the environment */
1184 free(shell->value);
1185 shell->value = xstrdup (default_shell);
1186 shell->origin = o_default;
1187 }
1188
1189 /* Some people do not like cmd to be used as the default
1190 if $SHELL is not defined in the Makefile.
1191 With -DNO_CMD_DEFAULT you can turn off this behaviour */
1192# ifndef NO_CMD_DEFAULT
1193 /* otherwise use $COMSPEC */
1194 if (!replace || !*replace->value)
1195 replace = lookup_variable ("COMSPEC", 7);
1196
1197 /* otherwise use $OS2_SHELL */
1198 if (!replace || !*replace->value)
1199 replace = lookup_variable ("OS2_SHELL", 9);
1200# else
1201# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
1202# endif
1203
1204 if (replace && *replace->value)
1205 /* overwrite $SHELL */
1206 (void) define_variable (shell_str, shlen, replace->value,
1207 replace->origin, 0);
1208 else
1209 /* provide a definition if there is none */
1210 (void) define_variable (shell_str, shlen, default_shell,
1211 o_default, 0);
1212 }
1213
1214#endif
1215
1216 /* This won't override any definition, but it will provide one if there
1217 isn't one there. */
1218 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
1219
1220 /* On MSDOS we do use SHELL from environment, since it isn't a standard
1221 environment variable on MSDOS, so whoever sets it, does that on purpose.
1222 On OS/2 we do not use SHELL from environment but we have already handled
1223 that problem above. */
1224#if !defined(__MSDOS__) && !defined(__EMX__)
1225 /* Don't let SHELL come from the environment. */
1226 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
1227 {
1228 free (v->value);
1229 v->origin = o_file;
1230 v->value = xstrdup (default_shell);
1231#ifdef CONFIG_WITH_VALUE_LENGTH
1232 v->value_length = strlen (v->value);
1233 v->value_alloc_len = v->value_length + 1;
1234#endif
1235 }
1236#endif
1237
1238 /* Make sure MAKEFILES gets exported if it is set. */
1239 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
1240 v->export = v_ifset;
1241
1242 /* Define the magic D and F variables in terms of
1243 the automatic variables they are variations of. */
1244
1245#ifdef VMS
1246 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
1247 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
1248 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
1249 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
1250 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
1251 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
1252 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
1253#else
1254 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
1255 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
1256 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
1257 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
1258 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
1259 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
1260 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
1261#endif
1262 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
1263 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
1264 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
1265 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
1266 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
1267 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
1268 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
1269}
1270
1271
1272int export_all_variables;
1273
1274/* Create a new environment for FILE's commands.
1275 If FILE is nil, this is for the `shell' function.
1276 The child's MAKELEVEL variable is incremented. */
1277
1278char **
1279target_environment (struct file *file)
1280{
1281 struct variable_set_list *set_list;
1282 register struct variable_set_list *s;
1283 struct hash_table table;
1284 struct variable **v_slot;
1285 struct variable **v_end;
1286 struct variable makelevel_key;
1287 char **result_0;
1288 char **result;
1289#ifdef CONFIG_WITH_STRCACHE2
1290 const char *cached_name;
1291#endif
1292
1293 if (file == 0)
1294 set_list = current_variable_set_list;
1295 else
1296 set_list = file->variables;
1297
1298 hash_init (&table, VARIABLE_BUCKETS,
1299 variable_hash_1, variable_hash_2, variable_hash_cmp);
1300
1301 /* Run through all the variable sets in the list,
1302 accumulating variables in TABLE. */
1303 for (s = set_list; s != 0; s = s->next)
1304 {
1305 struct variable_set *set = s->set;
1306 v_slot = (struct variable **) set->table.ht_vec;
1307 v_end = v_slot + set->table.ht_size;
1308 for ( ; v_slot < v_end; v_slot++)
1309 if (! HASH_VACANT (*v_slot))
1310 {
1311 struct variable **new_slot;
1312 struct variable *v = *v_slot;
1313
1314 /* If this is a per-target variable and it hasn't been touched
1315 already then look up the global version and take its export
1316 value. */
1317 if (v->per_target && v->export == v_default)
1318 {
1319 struct variable *gv;
1320
1321#ifndef CONFIG_WITH_VALUE_LENGTH
1322 gv = lookup_variable_in_set (v->name, strlen(v->name),
1323 &global_variable_set);
1324#else
1325 assert ((int)strlen(v->name) == v->length);
1326 gv = lookup_variable_in_set (v->name, v->length,
1327 &global_variable_set);
1328#endif
1329 if (gv)
1330 v->export = gv->export;
1331 }
1332
1333 switch (v->export)
1334 {
1335 case v_default:
1336 if (v->origin == o_default || v->origin == o_automatic)
1337 /* Only export default variables by explicit request. */
1338 continue;
1339
1340 /* The variable doesn't have a name that can be exported. */
1341 if (! v->exportable)
1342 continue;
1343
1344 if (! export_all_variables
1345 && v->origin != o_command
1346 && v->origin != o_env && v->origin != o_env_override)
1347 continue;
1348 break;
1349
1350 case v_export:
1351 break;
1352
1353 case v_noexport:
1354 /* If this is the SHELL variable and it's not exported, then
1355 add the value from our original environment. */
1356 if (streq (v->name, "SHELL"))
1357 {
1358 extern struct variable shell_var;
1359 v = &shell_var;
1360 break;
1361 }
1362 continue;
1363
1364 case v_ifset:
1365 if (v->origin == o_default)
1366 continue;
1367 break;
1368 }
1369
1370 new_slot = (struct variable **) hash_find_slot (&table, v);
1371 if (HASH_VACANT (*new_slot))
1372 hash_insert_at (&table, v, new_slot);
1373 }
1374 }
1375
1376#ifndef CONFIG_WITH_STRCACHE2
1377 makelevel_key.name = MAKELEVEL_NAME;
1378 makelevel_key.length = MAKELEVEL_LENGTH;
1379 hash_delete (&table, &makelevel_key);
1380#else /* CONFIG_WITH_STRCACHE2 */
1381 /* lookup the name in the string case, if it's not there it won't
1382 be in any of the sets either. */
1383 cached_name = strcache2_lookup(&variable_strcache,
1384 MAKELEVEL_NAME, MAKELEVEL_LENGTH);
1385 if (cached_name)
1386 {
1387 makelevel_key.name = cached_name;
1388 makelevel_key.length = MAKELEVEL_LENGTH;
1389 hash_delete (&table, &makelevel_key);
1390 }
1391#endif /* CONFIG_WITH_STRCACHE2 */
1392
1393 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
1394
1395 v_slot = (struct variable **) table.ht_vec;
1396 v_end = v_slot + table.ht_size;
1397 for ( ; v_slot < v_end; v_slot++)
1398 if (! HASH_VACANT (*v_slot))
1399 {
1400 struct variable *v = *v_slot;
1401
1402 /* If V is recursively expanded and didn't come from the environment,
1403 expand its value. If it came from the environment, it should
1404 go back into the environment unchanged. */
1405 if (v->recursive
1406 && v->origin != o_env && v->origin != o_env_override)
1407 {
1408#ifndef CONFIG_WITH_VALUE_LENGTH
1409 char *value = recursively_expand_for_file (v, file);
1410#else
1411 char *value = recursively_expand_for_file (v, file, NULL);
1412#endif
1413#ifdef WINDOWS32
1414 if (strcmp(v->name, "Path") == 0 ||
1415 strcmp(v->name, "PATH") == 0)
1416 convert_Path_to_windows32(value, ';');
1417#endif
1418 *result++ = xstrdup (concat (v->name, "=", value));
1419 free (value);
1420 }
1421 else
1422 {
1423#ifdef WINDOWS32
1424 if (strcmp(v->name, "Path") == 0 ||
1425 strcmp(v->name, "PATH") == 0)
1426 convert_Path_to_windows32(v->value, ';');
1427#endif
1428 *result++ = xstrdup (concat (v->name, "=", v->value));
1429 }
1430 }
1431
1432 *result = xmalloc (100);
1433 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
1434 *++result = 0;
1435
1436 hash_free (&table, 0);
1437
1438 return result_0;
1439}
1440
1441
1442#ifdef CONFIG_WITH_VALUE_LENGTH
1443/* Worker function for do_variable_definition_append() and
1444 append_expanded_string_to_variable().
1445 The APPEND argument indicates whether it's an append or prepend operation. */
1446void append_string_to_variable (struct variable *v, const char *value, unsigned int value_len, int append)
1447{
1448 /* The previous definition of the variable was recursive.
1449 The new value is the unexpanded old and new values. */
1450 unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
1451 int done_1st_prepend_copy = 0;
1452
1453 /* Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
1454 if (!value_len)
1455 return;
1456
1457 /* adjust the size. */
1458 if ((unsigned)v->value_alloc_len <= new_value_len + 1)
1459 {
1460 v->value_alloc_len *= 2;
1461 if ((unsigned)v->value_alloc_len < new_value_len + 1)
1462 v->value_alloc_len = (new_value_len + 1 + value_len + 0x7f) + ~0x7fU;
1463 if (append || !v->value_length)
1464 v->value = xrealloc (v->value, v->value_alloc_len);
1465 else
1466 {
1467 /* avoid the extra memcpy the xrealloc may have to do */
1468 char *new_buf = xmalloc (v->value_alloc_len);
1469 memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
1470 done_1st_prepend_copy = 1;
1471 free (v->value);
1472 v->value = new_buf;
1473 }
1474 }
1475
1476 /* insert the new bits */
1477 if (v->value_length != 0)
1478 {
1479 if (append)
1480 {
1481 v->value[v->value_length] = ' ';
1482 memcpy (&v->value[v->value_length + 1], value, value_len + 1);
1483 }
1484 else
1485 {
1486 if (!done_1st_prepend_copy)
1487 memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
1488 v->value[value_len] = ' ';
1489 memcpy (v->value, value, value_len);
1490 }
1491 }
1492 else
1493 memcpy (v->value, value, value_len + 1);
1494 v->value_length = new_value_len;
1495}
1496
1497static struct variable *
1498do_variable_definition_append (const struct floc *flocp, struct variable *v,
1499 const char *value, unsigned int value_len,
1500 int simple_value, enum variable_origin origin,
1501 int append)
1502{
1503 if (env_overrides && origin == o_env)
1504 origin = o_env_override;
1505
1506 if (env_overrides && v->origin == o_env)
1507 /* V came from in the environment. Since it was defined
1508 before the switches were parsed, it wasn't affected by -e. */
1509 v->origin = o_env_override;
1510
1511 /* A variable of this name is already defined.
1512 If the old definition is from a stronger source
1513 than this one, don't redefine it. */
1514 if ((int) origin < (int) v->origin)
1515 return v;
1516 v->origin = origin;
1517
1518 /* location */
1519 if (flocp != 0)
1520 v->fileinfo = *flocp;
1521
1522 /* The juicy bits, append the specified value to the variable
1523 This is a heavily exercised code path in kBuild. */
1524 if (value_len == ~0U)
1525 value_len = strlen (value);
1526 if (v->recursive || simple_value)
1527 append_string_to_variable (v, value, value_len, append);
1528 else
1529 /* The previous definition of the variable was simple.
1530 The new value comes from the old value, which was expanded
1531 when it was set; and from the expanded new value. */
1532 append_expanded_string_to_variable (v, value, value_len, append);
1533
1534 /* update the variable */
1535 return v;
1536}
1537#endif /* CONFIG_WITH_VALUE_LENGTH */
1538
1539
1540/* Given a variable, a value, and a flavor, define the variable.
1541 See the try_variable_definition() function for details on the parameters. */
1542
1543struct variable *
1544#ifndef CONFIG_WITH_VALUE_LENGTH
1545do_variable_definition (const struct floc *flocp, const char *varname,
1546 const char *value, enum variable_origin origin,
1547 enum variable_flavor flavor, int target_var)
1548#else /* CONFIG_WITH_VALUE_LENGTH */
1549do_variable_definition_2 (const struct floc *flocp,
1550 const char *varname, const char *value,
1551 unsigned int value_len, int simple_value,
1552 char *free_value,
1553 enum variable_origin origin,
1554 enum variable_flavor flavor,
1555 int target_var)
1556#endif /* CONFIG_WITH_VALUE_LENGTH */
1557{
1558 const char *p;
1559 char *alloc_value = NULL;
1560 struct variable *v;
1561 int append = 0;
1562 int conditional = 0;
1563 const size_t varname_len = strlen (varname); /* bird */
1564#ifdef CONFIG_WITH_VALUE_LENGTH
1565 assert (value_len == ~0U || value_len == strlen (value));
1566#endif
1567
1568 /* Calculate the variable's new value in VALUE. */
1569
1570 switch (flavor)
1571 {
1572 default:
1573 case f_bogus:
1574 /* Should not be possible. */
1575 abort ();
1576 case f_simple:
1577 /* A simple variable definition "var := value". Expand the value.
1578 We have to allocate memory since otherwise it'll clobber the
1579 variable buffer, and we may still need that if we're looking at a
1580 target-specific variable. */
1581#ifndef CONFIG_WITH_VALUE_LENGTH
1582 p = alloc_value = allocated_variable_expand (value);
1583#else /* CONFIG_WITH_VALUE_LENGTH */
1584 if (!simple_value)
1585 p = alloc_value = allocated_variable_expand_2 (value, value_len, &value_len);
1586 else
1587 {
1588 if (value_len == ~0U)
1589 value_len = strlen (value);
1590 if (!free_value)
1591 p = alloc_value = savestring (value, value_len);
1592 else
1593 {
1594 assert (value == free_value);
1595 p = alloc_value = free_value;
1596 free_value = 0;
1597 }
1598 }
1599#endif /* CONFIG_WITH_VALUE_LENGTH */
1600 break;
1601 case f_conditional:
1602 /* A conditional variable definition "var ?= value".
1603 The value is set IFF the variable is not defined yet. */
1604 v = lookup_variable (varname, varname_len);
1605 if (v)
1606 return v;
1607
1608 conditional = 1;
1609 flavor = f_recursive;
1610 /* FALLTHROUGH */
1611 case f_recursive:
1612 /* A recursive variable definition "var = value".
1613 The value is used verbatim. */
1614 p = value;
1615 break;
1616#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1617 case f_append:
1618 case f_prepend:
1619 {
1620 const enum variable_flavor org_flavor = flavor;
1621#else
1622 case f_append:
1623 {
1624#endif
1625
1626#ifdef CONFIG_WITH_LOCAL_VARIABLES
1627 /* If we have += but we're in a target or local variable context,
1628 we want to append only with other variables in the context of
1629 this target. */
1630 if (target_var || origin == o_local)
1631#else
1632 /* If we have += but we're in a target variable context, we want to
1633 append only with other variables in the context of this target. */
1634 if (target_var)
1635#endif
1636 {
1637 append = 1;
1638 v = lookup_variable_in_set (varname, varname_len,
1639 current_variable_set_list->set);
1640
1641 /* Don't append from the global set if a previous non-appending
1642 target-specific variable definition exists. */
1643 if (v && !v->append)
1644 append = 0;
1645 }
1646 else
1647 v = lookup_variable (varname, varname_len);
1648
1649 if (v == 0)
1650 {
1651 /* There was no old value.
1652 This becomes a normal recursive definition. */
1653 p = value;
1654 flavor = f_recursive;
1655 }
1656 else
1657 {
1658#ifdef CONFIG_WITH_VALUE_LENGTH
1659 v->append = append;
1660 v = do_variable_definition_append (flocp, v, value, value_len,
1661 simple_value, origin,
1662# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1663 org_flavor == f_append);
1664# else
1665 1);
1666# endif
1667 if (free_value)
1668 free (free_value);
1669 return v;
1670#else /* !CONFIG_WITH_VALUE_LENGTH */
1671
1672 /* Paste the old and new values together in VALUE. */
1673
1674 unsigned int oldlen, vallen;
1675 const char *val;
1676 char *tp;
1677
1678 val = value;
1679 if (v->recursive)
1680 /* The previous definition of the variable was recursive.
1681 The new value is the unexpanded old and new values. */
1682 flavor = f_recursive;
1683 else
1684 /* The previous definition of the variable was simple.
1685 The new value comes from the old value, which was expanded
1686 when it was set; and from the expanded new value. Allocate
1687 memory for the expansion as we may still need the rest of the
1688 buffer if we're looking at a target-specific variable. */
1689 val = alloc_value = allocated_variable_expand (val);
1690
1691 oldlen = strlen (v->value);
1692 vallen = strlen (val);
1693 tp = alloca (oldlen + 1 + vallen + 1);
1694# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1695 if (org_flavor == f_prepend)
1696 {
1697 memcpy (tp, val, vallen);
1698 tp[oldlen] = ' ';
1699 memcpy (&tp[oldlen + 1], v->value, oldlen + 1);
1700 }
1701 else
1702# endif /* CONFIG_WITH_PREPEND_ASSIGNMENT */
1703 {
1704 memcpy (tp, v->value, oldlen);
1705 tp[oldlen] = ' ';
1706 memcpy (&tp[oldlen + 1], val, vallen + 1);
1707 }
1708 p = tp;
1709#endif /* !CONFIG_WITH_VALUE_LENGTH */
1710 }
1711 }
1712 }
1713
1714#ifdef __MSDOS__
1715 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1716 non-Unix systems don't conform to this default configuration (in
1717 fact, most of them don't even have `/bin'). On the other hand,
1718 $SHELL in the environment, if set, points to the real pathname of
1719 the shell.
1720 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1721 the Makefile override $SHELL from the environment. But first, we
1722 look for the basename of the shell in the directory where SHELL=
1723 points, and along the $PATH; if it is found in any of these places,
1724 we define $SHELL to be the actual pathname of the shell. Thus, if
1725 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1726 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1727 defining SHELL to be "d:/unix/bash.exe". */
1728 if ((origin == o_file || origin == o_override)
1729 && strcmp (varname, "SHELL") == 0)
1730 {
1731 PATH_VAR (shellpath);
1732 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1733
1734 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1735 if (__dosexec_find_on_path (p, NULL, shellpath))
1736 {
1737 char *tp;
1738
1739 for (tp = shellpath; *tp; tp++)
1740 if (*tp == '\\')
1741 *tp = '/';
1742
1743 v = define_variable_loc (varname, varname_len,
1744 shellpath, origin, flavor == f_recursive,
1745 flocp);
1746 }
1747 else
1748 {
1749 const char *shellbase, *bslash;
1750 struct variable *pathv = lookup_variable ("PATH", 4);
1751 char *path_string;
1752 char *fake_env[2];
1753 size_t pathlen = 0;
1754
1755 shellbase = strrchr (p, '/');
1756 bslash = strrchr (p, '\\');
1757 if (!shellbase || bslash > shellbase)
1758 shellbase = bslash;
1759 if (!shellbase && p[1] == ':')
1760 shellbase = p + 1;
1761 if (shellbase)
1762 shellbase++;
1763 else
1764 shellbase = p;
1765
1766 /* Search for the basename of the shell (with standard
1767 executable extensions) along the $PATH. */
1768 if (pathv)
1769 pathlen = strlen (pathv->value);
1770 path_string = xmalloc (5 + pathlen + 2 + 1);
1771 /* On MSDOS, current directory is considered as part of $PATH. */
1772 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1773 fake_env[0] = path_string;
1774 fake_env[1] = 0;
1775 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1776 {
1777 char *tp;
1778
1779 for (tp = shellpath; *tp; tp++)
1780 if (*tp == '\\')
1781 *tp = '/';
1782
1783 v = define_variable_loc (varname, varname_len,
1784 shellpath, origin,
1785 flavor == f_recursive, flocp);
1786 }
1787 else
1788 v = lookup_variable (varname, varname_len);
1789
1790 free (path_string);
1791 }
1792 }
1793 else
1794#endif /* __MSDOS__ */
1795#ifdef WINDOWS32
1796 if ( varname_len == sizeof("SHELL") - 1 /* bird */
1797 && (origin == o_file || origin == o_override || origin == o_command)
1798 && streq (varname, "SHELL"))
1799 {
1800 extern char *default_shell;
1801
1802 /* Call shell locator function. If it returns TRUE, then
1803 set no_default_sh_exe to indicate sh was found and
1804 set new value for SHELL variable. */
1805
1806 if (find_and_set_default_shell (p))
1807 {
1808 v = define_variable_in_set (varname, varname_len, default_shell,
1809# ifdef CONFIG_WITH_VALUE_LENGTH
1810 ~0U, 1 /* duplicate_value */,
1811# endif
1812 origin, flavor == f_recursive,
1813 (target_var
1814 ? current_variable_set_list->set
1815 : NULL),
1816 flocp);
1817 no_default_sh_exe = 0;
1818 }
1819 else
1820 v = lookup_variable (varname, varname_len);
1821 }
1822 else
1823#endif
1824
1825 /* If we are defining variables inside an $(eval ...), we might have a
1826 different variable context pushed, not the global context (maybe we're
1827 inside a $(call ...) or something. Since this function is only ever
1828 invoked in places where we want to define globally visible variables,
1829 make sure we define this variable in the global set. */
1830
1831 v = define_variable_in_set (varname, varname_len, p,
1832#ifdef CONFIG_WITH_VALUE_LENGTH
1833 value_len, !alloc_value,
1834#endif
1835 origin, flavor == f_recursive,
1836#ifdef CONFIG_WITH_LOCAL_VARIABLES
1837 (target_var || origin == o_local
1838#else
1839 (target_var
1840#endif
1841 ? current_variable_set_list->set : NULL),
1842 flocp);
1843 v->append = append;
1844 v->conditional = conditional;
1845
1846#ifndef CONFIG_WITH_VALUE_LENGTH
1847 if (alloc_value)
1848 free (alloc_value);
1849#else
1850 if (free_value)
1851 free (free_value);
1852#endif
1853
1854 return v;
1855}
1856
1857
1858/* Try to interpret LINE (a null-terminated string) as a variable definition.
1859
1860 ORIGIN may be o_file, o_override, o_env, o_env_override,
1861 or o_command specifying that the variable definition comes
1862 from a makefile, an override directive, the environment with
1863 or without the -e switch, or the command line.
1864
1865 See the comments for parse_variable_definition().
1866
1867 If LINE was recognized as a variable definition, a pointer to its `struct
1868 variable' is returned. If LINE is not a variable definition, NULL is
1869 returned. */
1870
1871struct variable *
1872#ifndef CONFIG_WITH_VALUE_LENGTH
1873parse_variable_definition (struct variable *v, char *line)
1874#else
1875parse_variable_definition (struct variable *v, char *line, char *eos)
1876#endif
1877{
1878 register int c;
1879 register char *p = line;
1880 register char *beg;
1881 register char *end;
1882 enum variable_flavor flavor = f_bogus;
1883#ifndef CONFIG_WITH_VALUE_LENGTH
1884 char *name;
1885#endif
1886
1887 while (1)
1888 {
1889 c = *p++;
1890 if (c == '\0' || c == '#')
1891 return 0;
1892 if (c == '=')
1893 {
1894 end = p - 1;
1895 flavor = f_recursive;
1896 break;
1897 }
1898 else if (c == ':')
1899 if (*p == '=')
1900 {
1901 end = p++ - 1;
1902 flavor = f_simple;
1903 break;
1904 }
1905 else
1906 /* A colon other than := is a rule line, not a variable defn. */
1907 return 0;
1908 else if (c == '+' && *p == '=')
1909 {
1910 end = p++ - 1;
1911 flavor = f_append;
1912 break;
1913 }
1914#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1915 else if (c == '<' && *p == '=')
1916 {
1917 end = p++ - 1;
1918 flavor = f_prepend;
1919 break;
1920 }
1921#endif
1922 else if (c == '?' && *p == '=')
1923 {
1924 end = p++ - 1;
1925 flavor = f_conditional;
1926 break;
1927 }
1928 else if (c == '$')
1929 {
1930 /* This might begin a variable expansion reference. Make sure we
1931 don't misrecognize chars inside the reference as =, := or +=. */
1932 char closeparen;
1933 int count;
1934 c = *p++;
1935 if (c == '(')
1936 closeparen = ')';
1937 else if (c == '{')
1938 closeparen = '}';
1939 else
1940 continue; /* Nope. */
1941
1942 /* P now points past the opening paren or brace.
1943 Count parens or braces until it is matched. */
1944 count = 0;
1945 for (; *p != '\0'; ++p)
1946 {
1947 if (*p == c)
1948 ++count;
1949 else if (*p == closeparen && --count < 0)
1950 {
1951 ++p;
1952 break;
1953 }
1954 }
1955 }
1956 }
1957 v->flavor = flavor;
1958
1959 beg = next_token (line);
1960 while (end > beg && isblank ((unsigned char)end[-1]))
1961 --end;
1962 p = next_token (p);
1963 v->value = p;
1964#ifdef CONFIG_WITH_VALUE_LENGTH
1965 v->value_alloc_len = -1;
1966 v->value_length = eos != NULL ? eos - p : -1;
1967 assert (eos == NULL || strchr (p, '\0') == eos);
1968#endif
1969
1970 /* Expand the name, so "$(foo)bar = baz" works. */
1971#ifndef CONFIG_WITH_VALUE_LENGTH
1972 name = alloca (end - beg + 1);
1973 memcpy (name, beg, end - beg);
1974 name[end - beg] = '\0';
1975 v->name = allocated_variable_expand (name);
1976#else /* CONFIG_WITH_VALUE_LENGTH */
1977 v->name = allocated_variable_expand_2 (beg, end - beg, NULL);
1978#endif /* CONFIG_WITH_VALUE_LENGTH */
1979
1980 if (v->name[0] == '\0')
1981 fatal (&v->fileinfo, _("empty variable name"));
1982
1983 return v;
1984}
1985
1986
1987/* Try to interpret LINE (a null-terminated string) as a variable definition.
1988
1989 ORIGIN may be o_file, o_override, o_env, o_env_override, o_local,
1990 or o_command specifying that the variable definition comes
1991 from a makefile, an override directive, the environment with
1992 or without the -e switch, or the command line.
1993
1994 See the comments for parse_variable_definition().
1995
1996 If LINE was recognized as a variable definition, a pointer to its `struct
1997 variable' is returned. If LINE is not a variable definition, NULL is
1998 returned. */
1999
2000struct variable *
2001#ifndef CONFIG_WITH_VALUE_LENGTH
2002try_variable_definition (const struct floc *flocp, char *line,
2003 enum variable_origin origin, int target_var)
2004#else
2005try_variable_definition (const struct floc *flocp, char *line, char *eos,
2006 enum variable_origin origin, int target_var)
2007#endif
2008{
2009 struct variable v;
2010 struct variable *vp;
2011
2012 if (flocp != 0)
2013 v.fileinfo = *flocp;
2014 else
2015 v.fileinfo.filenm = 0;
2016
2017#ifndef CONFIG_WITH_VALUE_LENGTH
2018 if (!parse_variable_definition (&v, line))
2019 return 0;
2020
2021 vp = do_variable_definition (flocp, v.name, v.value,
2022 origin, v.flavor, target_var);
2023#else
2024 if (!parse_variable_definition (&v, line, eos))
2025 return 0;
2026
2027 vp = do_variable_definition_2 (flocp, v.name, v.value,
2028 v.value_length != -1 ? (unsigned int)v.value_length : ~0U, /* FIXME */
2029 0, NULL, origin, v.flavor, target_var);
2030#endif
2031
2032#ifndef CONFIG_WITH_STRCACHE2
2033 free (v.name);
2034#else
2035 free ((char *)v.name);
2036#endif
2037
2038 return vp;
2039}
2040
2041
2042/* Print information for variable V, prefixing it with PREFIX. */
2043
2044static void
2045print_variable (const void *item, void *arg)
2046{
2047 const struct variable *v = item;
2048 const char *prefix = arg;
2049 const char *origin;
2050
2051 switch (v->origin)
2052 {
2053 case o_default:
2054 origin = _("default");
2055 break;
2056 case o_env:
2057 origin = _("environment");
2058 break;
2059 case o_file:
2060 origin = _("makefile");
2061 break;
2062 case o_env_override:
2063 origin = _("environment under -e");
2064 break;
2065 case o_command:
2066 origin = _("command line");
2067 break;
2068 case o_override:
2069 origin = _("`override' directive");
2070 break;
2071 case o_automatic:
2072 origin = _("automatic");
2073 break;
2074#ifdef CONFIG_WITH_LOCAL_VARIABLES
2075 case o_local:
2076 origin = _("`local' directive");
2077 break;
2078#endif
2079 case o_invalid:
2080 default:
2081 abort ();
2082 }
2083 fputs ("# ", stdout);
2084 fputs (origin, stdout);
2085 if (v->fileinfo.filenm)
2086 printf (_(" (from `%s', line %lu)"),
2087 v->fileinfo.filenm, v->fileinfo.lineno);
2088 putchar ('\n');
2089 fputs (prefix, stdout);
2090
2091 /* Is this a `define'? */
2092 if (v->recursive && strchr (v->value, '\n') != 0)
2093 printf ("define %s\n%s\nendef\n", v->name, v->value);
2094 else
2095 {
2096 register char *p;
2097
2098 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
2099
2100 /* Check if the value is just whitespace. */
2101 p = next_token (v->value);
2102 if (p != v->value && *p == '\0')
2103 /* All whitespace. */
2104 printf ("$(subst ,,%s)", v->value);
2105 else if (v->recursive)
2106 fputs (v->value, stdout);
2107 else
2108 /* Double up dollar signs. */
2109 for (p = v->value; *p != '\0'; ++p)
2110 {
2111 if (*p == '$')
2112 putchar ('$');
2113 putchar (*p);
2114 }
2115 putchar ('\n');
2116 }
2117}
2118
2119
2120/* Print all the variables in SET. PREFIX is printed before
2121 the actual variable definitions (everything else is comments). */
2122
2123void
2124print_variable_set (struct variable_set *set, char *prefix)
2125{
2126 hash_map_arg (&set->table, print_variable, prefix);
2127
2128 fputs (_("# variable set hash-table stats:\n"), stdout);
2129 fputs ("# ", stdout);
2130 hash_print_stats (&set->table, stdout);
2131 putc ('\n', stdout);
2132}
2133
2134/* Print the data base of variables. */
2135
2136void
2137print_variable_data_base (void)
2138{
2139 puts (_("\n# Variables\n"));
2140
2141 print_variable_set (&global_variable_set, "");
2142
2143 puts (_("\n# Pattern-specific Variable Values"));
2144
2145 {
2146 struct pattern_var *p;
2147 int rules = 0;
2148
2149 for (p = pattern_vars; p != 0; p = p->next)
2150 {
2151 ++rules;
2152 printf ("\n%s :\n", p->target);
2153 print_variable (&p->variable, "# ");
2154 }
2155
2156 if (rules == 0)
2157 puts (_("\n# No pattern-specific variable values."));
2158 else
2159 printf (_("\n# %u pattern-specific variable values"), rules);
2160 }
2161
2162#ifdef CONFIG_WITH_STRCACHE2
2163 strcache2_print_stats (&variable_strcache, "# ");
2164#endif
2165}
2166
2167
2168/* Print all the local variables of FILE. */
2169
2170void
2171print_file_variables (const struct file *file)
2172{
2173 if (file->variables != 0)
2174 print_variable_set (file->variables->set, "# ");
2175}
2176
2177#ifdef WINDOWS32
2178void
2179sync_Path_environment (void)
2180{
2181 char *path = allocated_variable_expand ("$(PATH)");
2182 static char *environ_path = NULL;
2183
2184 if (!path)
2185 return;
2186
2187 /*
2188 * If done this before, don't leak memory unnecessarily.
2189 * Free the previous entry before allocating new one.
2190 */
2191 if (environ_path)
2192 free (environ_path);
2193
2194 /*
2195 * Create something WINDOWS32 world can grok
2196 */
2197 convert_Path_to_windows32 (path, ';');
2198 environ_path = xstrdup (concat ("PATH", "=", path));
2199 putenv (environ_path);
2200 free (path);
2201}
2202#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