VirtualBox

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

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

kmk: Added $(root ) to help out with checking for absolute paths on Windows and OS/2.

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

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