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

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

kmk: Moved the strcache hash optimizations into the hash code.

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