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

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

Some variable statistics.

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