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

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

variable.c: Overlooked CONFIG_WITH_RDONLY_VARIABLE_VALUE case.

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