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

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

kmk: new function - eval-opt-var

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