VirtualBox

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

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

kmk: New switch --print-stats which will print variable, file, strcache, allocation and heap statistics.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette