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

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

variable.c: redid the need-for-speed stuff in lookup_variable.

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