source: trunk/src/kmk/variable.c@ 1889

Last change on this file since 1889 was 1889, checked in by bird, 17 years ago

kmk: cleaned out the VARIABLE_HASH code (was removed in favor of the a variable strcache).

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