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

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

fixed bug in previous revision.

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