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

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

kmk: Added $(length ), $(length-var ), $(insert ), $(pos ), $(lastpos ), $(substr ) and $(translate ). (all untested)

  • Property svn:eol-style set to native
File size: 77.7 KB
Line 
1/* Internals of variables for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 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_STRING_FUNCTIONS) \
1209 && defined (KMK_HELPERS)
1210 (void) define_variable ("KMK_FEATURES", 12,
1211 "append-dash-n abspath includedep-queue"
1212 " rsort"
1213 " abspathex"
1214 " toupper tolower"
1215 " defined"
1216 " comp-vars comp-cmds comp-cmds-ex"
1217 " stack"
1218 " math-int"
1219 " xargs"
1220 " explicit-multitarget"
1221 " dot-must-make"
1222 " prepend-assignment"
1223 " set-conditionals intersects"
1224 " date"
1225 " file-size"
1226 " expr if-expr"
1227 " which"
1228 " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var"
1229 " make-stats"
1230 " commands"
1231 " printf"
1232 " length insert pos lastpos substr translate"
1233 " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one kb-exp-tmpl "
1234 , o_default, 0);
1235# else /* MSC can't deal with strings mixed with #if/#endif, thus the slow way. */
1236# error "All features should be enabled by default!"
1237 strcpy (buf, "append-dash-n abspath includedep-queue");
1238# if defined (CONFIG_WITH_RSORT)
1239 strcat (buf, " rsort");
1240# endif
1241# if defined (CONFIG_WITH_ABSPATHEX)
1242 strcat (buf, " abspathex");
1243# endif
1244# if defined (CONFIG_WITH_TOUPPER_TOLOWER)
1245 strcat (buf, " toupper tolower");
1246# endif
1247# if defined (CONFIG_WITH_DEFINED)
1248 strcat (buf, " defined");
1249# endif
1250# if defined (CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
1251 strcat (buf, " comp-vars comp-cmds comp-cmds-ex");
1252# endif
1253# if defined (CONFIG_WITH_STACK)
1254 strcat (buf, " stack");
1255# endif
1256# if defined (CONFIG_WITH_MATH)
1257 strcat (buf, " math-int");
1258# endif
1259# if defined (CONFIG_WITH_XARGS)
1260 strcat (buf, " xargs");
1261# endif
1262# if defined (CONFIG_WITH_EXPLICIT_MULTITARGET)
1263 strcat (buf, " explicit-multitarget");
1264# endif
1265# if defined (CONFIG_WITH_DOT_MUST_MAKE)
1266 strcat (buf, " dot-must-make");
1267# endif
1268# if defined (CONFIG_WITH_PREPEND_ASSIGNMENT)
1269 strcat (buf, " prepend-assignment");
1270# endif
1271# if defined (CONFIG_WITH_SET_CONDITIONALS)
1272 strcat (buf, " set-conditionals intersects");
1273# endif
1274# if defined (CONFIG_WITH_DATE)
1275 strcat (buf, " date");
1276# endif
1277# if defined (CONFIG_WITH_FILE_SIZE)
1278 strcat (buf, " file-size");
1279# endif
1280# if defined (CONFIG_WITH_IF_CONDITIONALS)
1281 strcat (buf, " expr if-expr");
1282# endif
1283# if defined (CONFIG_WITH_WHICH)
1284 strcat (buf, " which");
1285# endif
1286# if defined (CONFIG_WITH_EVALPLUS)
1287 strcat (buf, " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var");
1288# endif
1289# if defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)
1290 strcat (buf, " make-stats");
1291# endif
1292# if defined (CONFIG_WITH_COMMANDS_FUNC)
1293 strcat (buf, " commands");
1294# endif
1295# if defined (CONFIG_WITH_PRINTF)
1296 strcat (buf, " printf");
1297# endif
1298# if defined (CONFIG_WITH_STRING_FUNCTIONS)
1299 strcat (buf, " length insert pos lastpos substr translate");
1300# endif
1301# if defined (KMK_HELPERS)
1302 strcat (buf, " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one kb-exp-tmpl");
1303# endif
1304 (void) define_variable ("KMK_FEATURES", 12, buf, o_default, 0);
1305# endif
1306
1307#endif /* KMK */
1308
1309#ifdef CONFIG_WITH_KMK_BUILTIN
1310 /* The supported kMk Builtin commands. */
1311 (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);
1312#endif
1313
1314#ifdef __MSDOS__
1315 /* Allow to specify a special shell just for Make,
1316 and use $COMSPEC as the default $SHELL when appropriate. */
1317 {
1318 static char shell_str[] = "SHELL";
1319 const int shlen = sizeof (shell_str) - 1;
1320 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
1321 struct variable *comp = lookup_variable ("COMSPEC", 7);
1322
1323 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
1324 if (mshp)
1325 (void) define_variable (shell_str, shlen,
1326 mshp->value, o_env_override, 0);
1327 else if (comp)
1328 {
1329 /* $COMSPEC shouldn't override $SHELL. */
1330 struct variable *shp = lookup_variable (shell_str, shlen);
1331
1332 if (!shp)
1333 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
1334 }
1335 }
1336#elif defined(__EMX__)
1337 {
1338 static char shell_str[] = "SHELL";
1339 const int shlen = sizeof (shell_str) - 1;
1340 struct variable *shell = lookup_variable (shell_str, shlen);
1341 struct variable *replace = lookup_variable ("MAKESHELL", 9);
1342
1343 /* if $MAKESHELL is defined in the environment assume o_env_override */
1344 if (replace && *replace->value && replace->origin == o_env)
1345 replace->origin = o_env_override;
1346
1347 /* if $MAKESHELL is not defined use $SHELL but only if the variable
1348 did not come from the environment */
1349 if (!replace || !*replace->value)
1350 if (shell && *shell->value && (shell->origin == o_env
1351 || shell->origin == o_env_override))
1352 {
1353 /* overwrite whatever we got from the environment */
1354 free(shell->value);
1355 shell->value = xstrdup (default_shell);
1356 shell->origin = o_default;
1357 }
1358
1359 /* Some people do not like cmd to be used as the default
1360 if $SHELL is not defined in the Makefile.
1361 With -DNO_CMD_DEFAULT you can turn off this behaviour */
1362# ifndef NO_CMD_DEFAULT
1363 /* otherwise use $COMSPEC */
1364 if (!replace || !*replace->value)
1365 replace = lookup_variable ("COMSPEC", 7);
1366
1367 /* otherwise use $OS2_SHELL */
1368 if (!replace || !*replace->value)
1369 replace = lookup_variable ("OS2_SHELL", 9);
1370# else
1371# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
1372# endif
1373
1374 if (replace && *replace->value)
1375 /* overwrite $SHELL */
1376 (void) define_variable (shell_str, shlen, replace->value,
1377 replace->origin, 0);
1378 else
1379 /* provide a definition if there is none */
1380 (void) define_variable (shell_str, shlen, default_shell,
1381 o_default, 0);
1382 }
1383
1384#endif
1385
1386 /* This won't override any definition, but it will provide one if there
1387 isn't one there. */
1388 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
1389#ifdef __MSDOS__
1390 v->export = v_export; /* Export always SHELL. */
1391#endif
1392
1393 /* On MSDOS we do use SHELL from environment, since it isn't a standard
1394 environment variable on MSDOS, so whoever sets it, does that on purpose.
1395 On OS/2 we do not use SHELL from environment but we have already handled
1396 that problem above. */
1397#if !defined(__MSDOS__) && !defined(__EMX__)
1398 /* Don't let SHELL come from the environment. */
1399 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
1400 {
1401# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1402 if (v->rdonly_val)
1403 v->rdonly_val = 0;
1404 else
1405# endif
1406 free (v->value);
1407 v->origin = o_file;
1408 v->value = xstrdup (default_shell);
1409# ifdef CONFIG_WITH_VALUE_LENGTH
1410 v->value_length = strlen (v->value);
1411 v->value_alloc_len = v->value_length + 1;
1412# endif
1413 }
1414#endif
1415
1416 /* Make sure MAKEFILES gets exported if it is set. */
1417 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
1418 v->export = v_ifset;
1419
1420 /* Define the magic D and F variables in terms of
1421 the automatic variables they are variations of. */
1422
1423#ifdef VMS
1424 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
1425 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
1426 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
1427 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
1428 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
1429 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
1430 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
1431#else
1432 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
1433 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
1434 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
1435 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
1436 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
1437 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
1438 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
1439#endif
1440 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
1441 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
1442 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
1443 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
1444 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
1445 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
1446 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
1447#ifdef CONFIG_WITH_LAZY_DEPS_VARS
1448 define_variable ("^", 1, "$(deps $@)", o_automatic, 1);
1449 define_variable ("+", 1, "$(deps-all $@)", o_automatic, 1);
1450 define_variable ("?", 1, "$(deps-newer $@)", o_automatic, 1);
1451 define_variable ("|", 1, "$(deps-oo $@)", o_automatic, 1);
1452#endif /* CONFIG_WITH_LAZY_DEPS_VARS */
1453}
1454
1455
1456int export_all_variables;
1457
1458/* Create a new environment for FILE's commands.
1459 If FILE is nil, this is for the `shell' function.
1460 The child's MAKELEVEL variable is incremented. */
1461
1462char **
1463target_environment (struct file *file)
1464{
1465 struct variable_set_list *set_list;
1466 register struct variable_set_list *s;
1467 struct hash_table table;
1468 struct variable **v_slot;
1469 struct variable **v_end;
1470 struct variable makelevel_key;
1471 char **result_0;
1472 char **result;
1473#ifdef CONFIG_WITH_STRCACHE2
1474 const char *cached_name;
1475#endif
1476
1477 if (file == 0)
1478 set_list = current_variable_set_list;
1479 else
1480 set_list = file->variables;
1481
1482#ifndef CONFIG_WITH_STRCACHE2
1483 hash_init (&table, VARIABLE_BUCKETS,
1484 variable_hash_1, variable_hash_2, variable_hash_cmp);
1485#else /* CONFIG_WITH_STRCACHE2 */
1486 hash_init_strcached (&table, VARIABLE_BUCKETS,
1487 &variable_strcache, offsetof (struct variable, name));
1488#endif /* CONFIG_WITH_STRCACHE2 */
1489
1490 /* Run through all the variable sets in the list,
1491 accumulating variables in TABLE. */
1492 for (s = set_list; s != 0; s = s->next)
1493 {
1494 struct variable_set *set = s->set;
1495 v_slot = (struct variable **) set->table.ht_vec;
1496 v_end = v_slot + set->table.ht_size;
1497 for ( ; v_slot < v_end; v_slot++)
1498 if (! HASH_VACANT (*v_slot))
1499 {
1500 struct variable **new_slot;
1501 struct variable *v = *v_slot;
1502
1503 /* If this is a per-target variable and it hasn't been touched
1504 already then look up the global version and take its export
1505 value. */
1506 if (v->per_target && v->export == v_default)
1507 {
1508 struct variable *gv;
1509
1510#ifndef CONFIG_WITH_VALUE_LENGTH
1511 gv = lookup_variable_in_set (v->name, strlen(v->name),
1512 &global_variable_set);
1513#else
1514 assert ((int)strlen(v->name) == v->length);
1515 gv = lookup_variable_in_set (v->name, v->length,
1516 &global_variable_set);
1517#endif
1518 if (gv)
1519 v->export = gv->export;
1520 }
1521
1522 switch (v->export)
1523 {
1524 case v_default:
1525 if (v->origin == o_default || v->origin == o_automatic)
1526 /* Only export default variables by explicit request. */
1527 continue;
1528
1529 /* The variable doesn't have a name that can be exported. */
1530 if (! v->exportable)
1531 continue;
1532
1533 if (! export_all_variables
1534 && v->origin != o_command
1535 && v->origin != o_env && v->origin != o_env_override)
1536 continue;
1537 break;
1538
1539 case v_export:
1540 break;
1541
1542 case v_noexport:
1543 {
1544 /* If this is the SHELL variable and it's not exported,
1545 then add the value from our original environment, if
1546 the original environment defined a value for SHELL. */
1547 extern struct variable shell_var;
1548 if (streq (v->name, "SHELL") && shell_var.value)
1549 {
1550 v = &shell_var;
1551 break;
1552 }
1553 continue;
1554 }
1555
1556 case v_ifset:
1557 if (v->origin == o_default)
1558 continue;
1559 break;
1560 }
1561
1562#ifndef CONFIG_WITH_STRCACHE2
1563 new_slot = (struct variable **) hash_find_slot (&table, v);
1564#else /* CONFIG_WITH_STRCACHE2 */
1565 assert (strcache2_is_cached (&variable_strcache, v->name));
1566 new_slot = (struct variable **) hash_find_slot_strcached (&table, v);
1567#endif /* CONFIG_WITH_STRCACHE2 */
1568 if (HASH_VACANT (*new_slot))
1569 hash_insert_at (&table, v, new_slot);
1570 }
1571 }
1572
1573#ifndef CONFIG_WITH_STRCACHE2
1574 makelevel_key.name = MAKELEVEL_NAME;
1575 makelevel_key.length = MAKELEVEL_LENGTH;
1576 hash_delete (&table, &makelevel_key);
1577#else /* CONFIG_WITH_STRCACHE2 */
1578 /* lookup the name in the string case, if it's not there it won't
1579 be in any of the sets either. */
1580 cached_name = strcache2_lookup (&variable_strcache,
1581 MAKELEVEL_NAME, MAKELEVEL_LENGTH);
1582 if (cached_name)
1583 {
1584 makelevel_key.name = cached_name;
1585 makelevel_key.length = MAKELEVEL_LENGTH;
1586 hash_delete_strcached (&table, &makelevel_key);
1587 }
1588#endif /* CONFIG_WITH_STRCACHE2 */
1589
1590 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
1591
1592 v_slot = (struct variable **) table.ht_vec;
1593 v_end = v_slot + table.ht_size;
1594 for ( ; v_slot < v_end; v_slot++)
1595 if (! HASH_VACANT (*v_slot))
1596 {
1597 struct variable *v = *v_slot;
1598
1599 /* If V is recursively expanded and didn't come from the environment,
1600 expand its value. If it came from the environment, it should
1601 go back into the environment unchanged. */
1602 if (v->recursive
1603 && v->origin != o_env && v->origin != o_env_override)
1604 {
1605#ifndef CONFIG_WITH_VALUE_LENGTH
1606 char *value = recursively_expand_for_file (v, file);
1607#else
1608 char *value = recursively_expand_for_file (v, file, NULL);
1609#endif
1610#ifdef WINDOWS32
1611 if (strcmp(v->name, "Path") == 0 ||
1612 strcmp(v->name, "PATH") == 0)
1613 convert_Path_to_windows32(value, ';');
1614#endif
1615 *result++ = xstrdup (concat (v->name, "=", value));
1616 free (value);
1617 }
1618 else
1619 {
1620#ifdef WINDOWS32
1621 if (strcmp(v->name, "Path") == 0 ||
1622 strcmp(v->name, "PATH") == 0)
1623 convert_Path_to_windows32(v->value, ';');
1624#endif
1625 *result++ = xstrdup (concat (v->name, "=", v->value));
1626 }
1627 }
1628
1629 *result = xmalloc (100);
1630 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
1631 *++result = 0;
1632
1633 hash_free (&table, 0);
1634
1635 return result_0;
1636}
1637
1638
1639#ifdef CONFIG_WITH_VALUE_LENGTH
1640/* Worker function for do_variable_definition_append() and
1641 append_expanded_string_to_variable().
1642 The APPEND argument indicates whether it's an append or prepend operation. */
1643void append_string_to_variable (struct variable *v, const char *value, unsigned int value_len, int append)
1644{
1645 /* The previous definition of the variable was recursive.
1646 The new value is the unexpanded old and new values. */
1647 unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
1648 int done_1st_prepend_copy = 0;
1649
1650 /* Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
1651 if (!value_len)
1652 return;
1653
1654 /* adjust the size. */
1655 if (v->value_alloc_len <= new_value_len + 1)
1656 {
1657 if (v->value_alloc_len < 256)
1658 v->value_alloc_len = 256;
1659 else
1660 v->value_alloc_len *= 2;
1661 if (v->value_alloc_len < new_value_len + 1)
1662 v->value_alloc_len = VAR_ALIGN_VALUE_ALLOC (new_value_len + 1 + value_len /*future*/ );
1663# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1664 if ((append || !v->value_length) && !v->rdonly_val)
1665# else
1666 if (append || !v->value_length)
1667# endif
1668 v->value = xrealloc (v->value, v->value_alloc_len);
1669 else
1670 {
1671 /* avoid the extra memcpy the xrealloc may have to do */
1672 char *new_buf = xmalloc (v->value_alloc_len);
1673 memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
1674 done_1st_prepend_copy = 1;
1675# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1676 if (v->rdonly_val)
1677 v->rdonly_val = 0;
1678 else
1679# endif
1680 free (v->value);
1681 v->value = new_buf;
1682 }
1683 MAKE_STATS_2(v->reallocs++);
1684 }
1685
1686 /* insert the new bits */
1687 if (v->value_length != 0)
1688 {
1689 if (append)
1690 {
1691 v->value[v->value_length] = ' ';
1692 memcpy (&v->value[v->value_length + 1], value, value_len + 1);
1693 }
1694 else
1695 {
1696 if (!done_1st_prepend_copy)
1697 memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
1698 v->value[value_len] = ' ';
1699 memcpy (v->value, value, value_len);
1700 }
1701 }
1702 else
1703 memcpy (v->value, value, value_len + 1);
1704 v->value_length = new_value_len;
1705}
1706
1707static struct variable *
1708do_variable_definition_append (const struct floc *flocp, struct variable *v,
1709 const char *value, unsigned int value_len,
1710 int simple_value, enum variable_origin origin,
1711 int append)
1712{
1713 if (env_overrides && origin == o_env)
1714 origin = o_env_override;
1715
1716 if (env_overrides && v->origin == o_env)
1717 /* V came from in the environment. Since it was defined
1718 before the switches were parsed, it wasn't affected by -e. */
1719 v->origin = o_env_override;
1720
1721 /* A variable of this name is already defined.
1722 If the old definition is from a stronger source
1723 than this one, don't redefine it. */
1724 if ((int) origin < (int) v->origin)
1725 return v;
1726 v->origin = origin;
1727
1728 /* location */
1729 if (flocp != 0)
1730 v->fileinfo = *flocp;
1731
1732 /* The juicy bits, append the specified value to the variable
1733 This is a heavily exercised code path in kBuild. */
1734 if (value_len == ~0U)
1735 value_len = strlen (value);
1736 if (v->recursive || simple_value)
1737 append_string_to_variable (v, value, value_len, append);
1738 else
1739 /* The previous definition of the variable was simple.
1740 The new value comes from the old value, which was expanded
1741 when it was set; and from the expanded new value. */
1742 append_expanded_string_to_variable (v, value, value_len, append);
1743
1744 /* update the variable */
1745 return v;
1746}
1747#endif /* CONFIG_WITH_VALUE_LENGTH */
1748
1749
1750static struct variable *
1751set_special_var (struct variable *var)
1752{
1753 if (streq (var->name, RECIPEPREFIX_NAME))
1754 {
1755 /* The user is resetting the command introduction prefix. This has to
1756 happen immediately, so that subsequent rules are interpreted
1757 properly. */
1758 cmd_prefix = var->value[0]=='\0' ? RECIPEPREFIX_DEFAULT : var->value[0];
1759 }
1760
1761 return var;
1762}
1763
1764
1765/* Given a variable, a value, and a flavor, define the variable.
1766 See the try_variable_definition() function for details on the parameters. */
1767
1768struct variable *
1769#ifndef CONFIG_WITH_VALUE_LENGTH
1770do_variable_definition (const struct floc *flocp, const char *varname,
1771 const char *value, enum variable_origin origin,
1772 enum variable_flavor flavor, int target_var)
1773#else /* CONFIG_WITH_VALUE_LENGTH */
1774do_variable_definition_2 (const struct floc *flocp,
1775 const char *varname, const char *value,
1776 unsigned int value_len, int simple_value,
1777 char *free_value,
1778 enum variable_origin origin,
1779 enum variable_flavor flavor,
1780 int target_var)
1781#endif /* CONFIG_WITH_VALUE_LENGTH */
1782{
1783 const char *p;
1784 char *alloc_value = NULL;
1785 struct variable *v;
1786 int append = 0;
1787 int conditional = 0;
1788 const size_t varname_len = strlen (varname); /* bird */
1789#ifdef CONFIG_WITH_VALUE_LENGTH
1790 assert (value_len == ~0U || value_len == strlen (value));
1791#endif
1792
1793 /* Calculate the variable's new value in VALUE. */
1794
1795 switch (flavor)
1796 {
1797 default:
1798 case f_bogus:
1799 /* Should not be possible. */
1800 abort ();
1801 case f_simple:
1802 /* A simple variable definition "var := value". Expand the value.
1803 We have to allocate memory since otherwise it'll clobber the
1804 variable buffer, and we may still need that if we're looking at a
1805 target-specific variable. */
1806#ifndef CONFIG_WITH_VALUE_LENGTH
1807 p = alloc_value = allocated_variable_expand (value);
1808#else /* CONFIG_WITH_VALUE_LENGTH */
1809 if (!simple_value)
1810 p = alloc_value = allocated_variable_expand_2 (value, value_len, &value_len);
1811 else
1812 {
1813 if (value_len == ~0U)
1814 value_len = strlen (value);
1815 if (!free_value)
1816 p = alloc_value = savestring (value, value_len);
1817 else
1818 {
1819 assert (value == free_value);
1820 p = alloc_value = free_value;
1821 free_value = 0;
1822 }
1823 }
1824#endif /* CONFIG_WITH_VALUE_LENGTH */
1825 break;
1826 case f_conditional:
1827 /* A conditional variable definition "var ?= value".
1828 The value is set IFF the variable is not defined yet. */
1829 v = lookup_variable (varname, varname_len);
1830 if (v)
1831#ifndef CONFIG_WITH_VALUE_LENGTH
1832 return v->special ? set_special_var (v) : v;
1833#else /* CONFIG_WITH_VALUE_LENGTH */
1834 {
1835 if (free_value)
1836 free (free_value);
1837 return v->special ? set_special_var (v) : v;
1838 }
1839#endif /* CONFIG_WITH_VALUE_LENGTH */
1840
1841 conditional = 1;
1842 flavor = f_recursive;
1843 /* FALLTHROUGH */
1844 case f_recursive:
1845 /* A recursive variable definition "var = value".
1846 The value is used verbatim. */
1847 p = value;
1848 break;
1849#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1850 case f_append:
1851 case f_prepend:
1852 {
1853 const enum variable_flavor org_flavor = flavor;
1854#else
1855 case f_append:
1856 {
1857#endif
1858
1859#ifdef CONFIG_WITH_LOCAL_VARIABLES
1860 /* If we have += but we're in a target or local variable context,
1861 we want to append only with other variables in the context of
1862 this target. */
1863 if (target_var || origin == o_local)
1864#else
1865 /* If we have += but we're in a target variable context, we want to
1866 append only with other variables in the context of this target. */
1867 if (target_var)
1868#endif
1869 {
1870 append = 1;
1871 v = lookup_variable_in_set (varname, varname_len,
1872 current_variable_set_list->set);
1873
1874 /* Don't append from the global set if a previous non-appending
1875 target-specific variable definition exists. */
1876 if (v && !v->append)
1877 append = 0;
1878 }
1879 else
1880 v = lookup_variable (varname, varname_len);
1881
1882 if (v == 0)
1883 {
1884 /* There was no old value.
1885 This becomes a normal recursive definition. */
1886 p = value;
1887 flavor = f_recursive;
1888 }
1889 else
1890 {
1891#ifdef CONFIG_WITH_VALUE_LENGTH
1892 v->append = append;
1893 v = do_variable_definition_append (flocp, v, value, value_len,
1894 simple_value, origin,
1895# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1896 org_flavor == f_append);
1897# else
1898 1);
1899# endif
1900 if (free_value)
1901 free (free_value);
1902 MAKE_STATS_2(v->changes++);
1903 return v;
1904#else /* !CONFIG_WITH_VALUE_LENGTH */
1905
1906 /* Paste the old and new values together in VALUE. */
1907
1908 unsigned int oldlen, vallen;
1909 const char *val;
1910 char *tp;
1911
1912 val = value;
1913 if (v->recursive)
1914 /* The previous definition of the variable was recursive.
1915 The new value is the unexpanded old and new values. */
1916 flavor = f_recursive;
1917 else
1918 /* The previous definition of the variable was simple.
1919 The new value comes from the old value, which was expanded
1920 when it was set; and from the expanded new value. Allocate
1921 memory for the expansion as we may still need the rest of the
1922 buffer if we're looking at a target-specific variable. */
1923 val = alloc_value = allocated_variable_expand (val);
1924
1925 oldlen = strlen (v->value);
1926 vallen = strlen (val);
1927 tp = alloca (oldlen + 1 + vallen + 1);
1928# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1929 if (org_flavor == f_prepend)
1930 {
1931 memcpy (tp, val, vallen);
1932 tp[oldlen] = ' ';
1933 memcpy (&tp[oldlen + 1], v->value, oldlen + 1);
1934 }
1935 else
1936# endif /* CONFIG_WITH_PREPEND_ASSIGNMENT */
1937 {
1938 memcpy (tp, v->value, oldlen);
1939 tp[oldlen] = ' ';
1940 memcpy (&tp[oldlen + 1], val, vallen + 1);
1941 }
1942 p = tp;
1943#endif /* !CONFIG_WITH_VALUE_LENGTH */
1944 }
1945 }
1946 }
1947
1948#ifdef __MSDOS__
1949 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1950 non-Unix systems don't conform to this default configuration (in
1951 fact, most of them don't even have `/bin'). On the other hand,
1952 $SHELL in the environment, if set, points to the real pathname of
1953 the shell.
1954 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1955 the Makefile override $SHELL from the environment. But first, we
1956 look for the basename of the shell in the directory where SHELL=
1957 points, and along the $PATH; if it is found in any of these places,
1958 we define $SHELL to be the actual pathname of the shell. Thus, if
1959 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1960 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1961 defining SHELL to be "d:/unix/bash.exe". */
1962 if ((origin == o_file || origin == o_override)
1963 && strcmp (varname, "SHELL") == 0)
1964 {
1965 PATH_VAR (shellpath);
1966 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1967
1968 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1969 if (__dosexec_find_on_path (p, NULL, shellpath))
1970 {
1971 char *tp;
1972
1973 for (tp = shellpath; *tp; tp++)
1974 if (*tp == '\\')
1975 *tp = '/';
1976
1977 v = define_variable_loc (varname, varname_len,
1978 shellpath, origin, flavor == f_recursive,
1979 flocp);
1980 }
1981 else
1982 {
1983 const char *shellbase, *bslash;
1984 struct variable *pathv = lookup_variable ("PATH", 4);
1985 char *path_string;
1986 char *fake_env[2];
1987 size_t pathlen = 0;
1988
1989 shellbase = strrchr (p, '/');
1990 bslash = strrchr (p, '\\');
1991 if (!shellbase || bslash > shellbase)
1992 shellbase = bslash;
1993 if (!shellbase && p[1] == ':')
1994 shellbase = p + 1;
1995 if (shellbase)
1996 shellbase++;
1997 else
1998 shellbase = p;
1999
2000 /* Search for the basename of the shell (with standard
2001 executable extensions) along the $PATH. */
2002 if (pathv)
2003 pathlen = strlen (pathv->value);
2004 path_string = xmalloc (5 + pathlen + 2 + 1);
2005 /* On MSDOS, current directory is considered as part of $PATH. */
2006 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
2007 fake_env[0] = path_string;
2008 fake_env[1] = 0;
2009 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
2010 {
2011 char *tp;
2012
2013 for (tp = shellpath; *tp; tp++)
2014 if (*tp == '\\')
2015 *tp = '/';
2016
2017 v = define_variable_loc (varname, varname_len,
2018 shellpath, origin,
2019 flavor == f_recursive, flocp);
2020 }
2021 else
2022 v = lookup_variable (varname, varname_len);
2023
2024 free (path_string);
2025 }
2026 }
2027 else
2028#endif /* __MSDOS__ */
2029#ifdef WINDOWS32
2030 if ( varname_len == sizeof("SHELL") - 1 /* bird */
2031 && (origin == o_file || origin == o_override || origin == o_command)
2032 && streq (varname, "SHELL"))
2033 {
2034 extern char *default_shell;
2035
2036 /* Call shell locator function. If it returns TRUE, then
2037 set no_default_sh_exe to indicate sh was found and
2038 set new value for SHELL variable. */
2039
2040 if (find_and_set_default_shell (p))
2041 {
2042 v = define_variable_in_set (varname, varname_len, default_shell,
2043# ifdef CONFIG_WITH_VALUE_LENGTH
2044 ~0U, 1 /* duplicate_value */,
2045# endif
2046 origin, flavor == f_recursive,
2047 (target_var
2048 ? current_variable_set_list->set
2049 : NULL),
2050 flocp);
2051 no_default_sh_exe = 0;
2052 }
2053 else
2054 {
2055 if (alloc_value)
2056 free (alloc_value);
2057
2058 alloc_value = allocated_variable_expand (p);
2059 if (find_and_set_default_shell (alloc_value))
2060 {
2061 v = define_variable_in_set (varname, varname_len, p,
2062#ifdef CONFIG_WITH_VALUE_LENGTH
2063 ~0U, 1 /* duplicate_value */,
2064#endif
2065 origin, flavor == f_recursive,
2066 (target_var
2067 ? current_variable_set_list->set
2068 : NULL),
2069 flocp);
2070 no_default_sh_exe = 0;
2071 }
2072 else
2073 v = lookup_variable (varname, varname_len);
2074 }
2075 }
2076 else
2077#endif
2078
2079 /* If we are defining variables inside an $(eval ...), we might have a
2080 different variable context pushed, not the global context (maybe we're
2081 inside a $(call ...) or something. Since this function is only ever
2082 invoked in places where we want to define globally visible variables,
2083 make sure we define this variable in the global set. */
2084
2085 v = define_variable_in_set (varname, varname_len, p,
2086#ifdef CONFIG_WITH_VALUE_LENGTH
2087 value_len, !alloc_value,
2088#endif
2089 origin, flavor == f_recursive,
2090#ifdef CONFIG_WITH_LOCAL_VARIABLES
2091 (target_var || origin == o_local
2092#else
2093 (target_var
2094#endif
2095 ? current_variable_set_list->set : NULL),
2096 flocp);
2097 v->append = append;
2098 v->conditional = conditional;
2099
2100#ifndef CONFIG_WITH_VALUE_LENGTH
2101 if (alloc_value)
2102 free (alloc_value);
2103#else
2104 if (free_value)
2105 free (free_value);
2106#endif
2107
2108 return v->special ? set_special_var (v) : v;
2109}
2110
2111
2112/* Try to interpret LINE (a null-terminated string) as a variable definition.
2113
2114 ORIGIN may be o_file, o_override, o_env, o_env_override,
2115 or o_command specifying that the variable definition comes
2116 from a makefile, an override directive, the environment with
2117 or without the -e switch, or the command line.
2118
2119 See the comments for parse_variable_definition().
2120
2121 If LINE was recognized as a variable definition, a pointer to its `struct
2122 variable' is returned. If LINE is not a variable definition, NULL is
2123 returned. */
2124
2125struct variable *
2126#ifndef CONFIG_WITH_VALUE_LENGTH
2127parse_variable_definition (struct variable *v, char *line)
2128#else
2129parse_variable_definition (struct variable *v, char *line, char *eos)
2130#endif
2131{
2132 register int c;
2133 register char *p = line;
2134 register char *beg;
2135 register char *end;
2136 enum variable_flavor flavor = f_bogus;
2137#ifndef CONFIG_WITH_VALUE_LENGTH
2138 char *name;
2139#endif
2140
2141 while (1)
2142 {
2143 c = *p++;
2144 if (c == '\0' || c == '#')
2145 return 0;
2146 if (c == '=')
2147 {
2148 end = p - 1;
2149 flavor = f_recursive;
2150 break;
2151 }
2152 else if (c == ':')
2153 if (*p == '=')
2154 {
2155 end = p++ - 1;
2156 flavor = f_simple;
2157 break;
2158 }
2159 else
2160 /* A colon other than := is a rule line, not a variable defn. */
2161 return 0;
2162 else if (c == '+' && *p == '=')
2163 {
2164 end = p++ - 1;
2165 flavor = f_append;
2166 break;
2167 }
2168#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2169 else if (c == '<' && *p == '=')
2170 {
2171 end = p++ - 1;
2172 flavor = f_prepend;
2173 break;
2174 }
2175#endif
2176 else if (c == '?' && *p == '=')
2177 {
2178 end = p++ - 1;
2179 flavor = f_conditional;
2180 break;
2181 }
2182 else if (c == '$')
2183 {
2184 /* This might begin a variable expansion reference. Make sure we
2185 don't misrecognize chars inside the reference as =, := or +=. */
2186 char closeparen;
2187 int count;
2188 c = *p++;
2189 if (c == '(')
2190 closeparen = ')';
2191 else if (c == '{')
2192 closeparen = '}';
2193 else
2194 continue; /* Nope. */
2195
2196 /* P now points past the opening paren or brace.
2197 Count parens or braces until it is matched. */
2198 count = 0;
2199 for (; *p != '\0'; ++p)
2200 {
2201 if (*p == c)
2202 ++count;
2203 else if (*p == closeparen && --count < 0)
2204 {
2205 ++p;
2206 break;
2207 }
2208 }
2209 }
2210 }
2211 v->flavor = flavor;
2212
2213 beg = next_token (line);
2214 while (end > beg && isblank ((unsigned char)end[-1]))
2215 --end;
2216 p = next_token (p);
2217 v->value = p;
2218#ifdef CONFIG_WITH_VALUE_LENGTH
2219 v->value_alloc_len = ~(unsigned int)0;
2220 v->value_length = eos != NULL ? eos - p : -1;
2221 assert (eos == NULL || strchr (p, '\0') == eos);
2222# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2223 v->rdonly_val = 0;
2224# endif
2225#endif
2226
2227 /* Expand the name, so "$(foo)bar = baz" works. */
2228#ifndef CONFIG_WITH_VALUE_LENGTH
2229 name = alloca (end - beg + 1);
2230 memcpy (name, beg, end - beg);
2231 name[end - beg] = '\0';
2232 v->name = allocated_variable_expand (name);
2233#else /* CONFIG_WITH_VALUE_LENGTH */
2234 v->name = allocated_variable_expand_2 (beg, end - beg, NULL);
2235#endif /* CONFIG_WITH_VALUE_LENGTH */
2236
2237 if (v->name[0] == '\0')
2238 fatal (&v->fileinfo, _("empty variable name"));
2239
2240 return v;
2241}
2242
2243
2244/* Try to interpret LINE (a null-terminated string) as a variable definition.
2245
2246 ORIGIN may be o_file, o_override, o_env, o_env_override, o_local,
2247 or o_command specifying that the variable definition comes
2248 from a makefile, an override directive, the environment with
2249 or without the -e switch, or the command line.
2250
2251 See the comments for parse_variable_definition().
2252
2253 If LINE was recognized as a variable definition, a pointer to its `struct
2254 variable' is returned. If LINE is not a variable definition, NULL is
2255 returned. */
2256
2257struct variable *
2258#ifndef CONFIG_WITH_VALUE_LENGTH
2259try_variable_definition (const struct floc *flocp, char *line,
2260 enum variable_origin origin, int target_var)
2261#else
2262try_variable_definition (const struct floc *flocp, char *line, char *eos,
2263 enum variable_origin origin, int target_var)
2264#endif
2265{
2266 struct variable v;
2267 struct variable *vp;
2268
2269 if (flocp != 0)
2270 v.fileinfo = *flocp;
2271 else
2272 v.fileinfo.filenm = 0;
2273
2274#ifndef CONFIG_WITH_VALUE_LENGTH
2275 if (!parse_variable_definition (&v, line))
2276 return 0;
2277
2278 vp = do_variable_definition (flocp, v.name, v.value,
2279 origin, v.flavor, target_var);
2280#else
2281 if (!parse_variable_definition (&v, line, eos))
2282 return 0;
2283
2284 vp = do_variable_definition_2 (flocp, v.name, v.value, v.value_length,
2285 0, NULL, origin, v.flavor, target_var);
2286#endif
2287
2288#ifndef CONFIG_WITH_STRCACHE2
2289 free (v.name);
2290#else
2291 free ((char *)v.name);
2292#endif
2293
2294 return vp;
2295}
2296
2297
2298#ifdef CONFIG_WITH_MAKE_STATS
2299static unsigned long var_stats_changes, var_stats_changed;
2300static unsigned long var_stats_reallocs, var_stats_realloced;
2301static unsigned long var_stats_val_len, var_stats_val_alloc_len;
2302static unsigned long var_stats_val_rdonly_len;
2303#endif
2304
2305/* Print information for variable V, prefixing it with PREFIX. */
2306
2307static void
2308print_variable (const void *item, void *arg)
2309{
2310 const struct variable *v = item;
2311 const char *prefix = arg;
2312 const char *origin;
2313
2314 switch (v->origin)
2315 {
2316 case o_default:
2317 origin = _("default");
2318 break;
2319 case o_env:
2320 origin = _("environment");
2321 break;
2322 case o_file:
2323 origin = _("makefile");
2324 break;
2325 case o_env_override:
2326 origin = _("environment under -e");
2327 break;
2328 case o_command:
2329 origin = _("command line");
2330 break;
2331 case o_override:
2332 origin = _("`override' directive");
2333 break;
2334 case o_automatic:
2335 origin = _("automatic");
2336 break;
2337#ifdef CONFIG_WITH_LOCAL_VARIABLES
2338 case o_local:
2339 origin = _("`local' directive");
2340 break;
2341#endif
2342 case o_invalid:
2343 default:
2344 abort ();
2345 }
2346 fputs ("# ", stdout);
2347 fputs (origin, stdout);
2348 if (v->fileinfo.filenm)
2349 printf (_(" (from `%s', line %lu)"),
2350 v->fileinfo.filenm, v->fileinfo.lineno);
2351#ifdef CONFIG_WITH_MAKE_STATS
2352 if (v->changes != 0)
2353 printf (_(", %u changes"), v->changes);
2354 var_stats_changes += v->changes;
2355 var_stats_changed += (v->changes != 0);
2356 if (v->reallocs != 0)
2357 printf (_(", %u reallocs"), v->reallocs);
2358 var_stats_reallocs += v->reallocs;
2359 var_stats_realloced += (v->reallocs != 0);
2360 var_stats_val_len += v->value_length;
2361 if (v->value_alloc_len)
2362 var_stats_val_alloc_len += v->value_alloc_len;
2363 else
2364 var_stats_val_rdonly_len += v->value_length;
2365 assert (v->value_length == strlen (v->value));
2366 /*assert (v->rdonly_val ? !v->value_alloc_len : v->value_alloc_len > v->value_length); - FIXME */
2367#endif /* CONFIG_WITH_MAKE_STATS */
2368 putchar ('\n');
2369 fputs (prefix, stdout);
2370
2371 /* Is this a `define'? */
2372 if (v->recursive && strchr (v->value, '\n') != 0)
2373 printf ("define %s\n%s\nendef\n", v->name, v->value);
2374 else
2375 {
2376 register char *p;
2377
2378 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
2379
2380 /* Check if the value is just whitespace. */
2381 p = next_token (v->value);
2382 if (p != v->value && *p == '\0')
2383 /* All whitespace. */
2384 printf ("$(subst ,,%s)", v->value);
2385 else if (v->recursive)
2386 fputs (v->value, stdout);
2387 else
2388 /* Double up dollar signs. */
2389 for (p = v->value; *p != '\0'; ++p)
2390 {
2391 if (*p == '$')
2392 putchar ('$');
2393 putchar (*p);
2394 }
2395 putchar ('\n');
2396 }
2397}
2398
2399
2400/* Print all the variables in SET. PREFIX is printed before
2401 the actual variable definitions (everything else is comments). */
2402
2403void
2404print_variable_set (struct variable_set *set, char *prefix)
2405{
2406#ifdef CONFIG_WITH_MAKE_STATS
2407 var_stats_changes = var_stats_changed = var_stats_reallocs
2408 = var_stats_realloced = var_stats_val_len = var_stats_val_alloc_len
2409 = var_stats_val_rdonly_len = 0;
2410
2411 hash_map_arg (&set->table, print_variable, prefix);
2412
2413 if (set->table.ht_fill)
2414 {
2415 unsigned long fragmentation;
2416
2417 fragmentation = var_stats_val_alloc_len - (var_stats_val_len - var_stats_val_rdonly_len);
2418 printf(_("# variable set value stats:\n\
2419# strings %7lu bytes, readonly %6lu bytes\n"),
2420 var_stats_val_len, var_stats_val_rdonly_len);
2421
2422 if (var_stats_val_alloc_len)
2423 printf(_("# allocated %7lu bytes, fragmentation %6lu bytes (%u%%)\n"),
2424 var_stats_val_alloc_len, fragmentation,
2425 (unsigned int)((100.0 * fragmentation) / var_stats_val_alloc_len));
2426
2427 if (var_stats_changed)
2428 printf(_("# changed %5lu (%2u%%), changes %6lu\n"),
2429 var_stats_changed,
2430 (unsigned int)((100.0 * var_stats_changed) / set->table.ht_fill),
2431 var_stats_changes);
2432
2433 if (var_stats_realloced)
2434 printf(_("# reallocated %5lu (%2u%%), reallocations %6lu\n"),
2435 var_stats_realloced,
2436 (unsigned int)((100.0 * var_stats_realloced) / set->table.ht_fill),
2437 var_stats_reallocs);
2438 }
2439#else
2440 hash_map_arg (&set->table, print_variable, prefix);
2441#endif
2442
2443 fputs (_("# variable set hash-table stats:\n"), stdout);
2444 fputs ("# ", stdout);
2445 hash_print_stats (&set->table, stdout);
2446 putc ('\n', stdout);
2447}
2448
2449/* Print the data base of variables. */
2450
2451void
2452print_variable_data_base (void)
2453{
2454 puts (_("\n# Variables\n"));
2455
2456 print_variable_set (&global_variable_set, "");
2457
2458 puts (_("\n# Pattern-specific Variable Values"));
2459
2460 {
2461 struct pattern_var *p;
2462 int rules = 0;
2463
2464 for (p = pattern_vars; p != 0; p = p->next)
2465 {
2466 ++rules;
2467 printf ("\n%s :\n", p->target);
2468 print_variable (&p->variable, "# ");
2469 }
2470
2471 if (rules == 0)
2472 puts (_("\n# No pattern-specific variable values."));
2473 else
2474 printf (_("\n# %u pattern-specific variable values"), rules);
2475 }
2476
2477#ifdef CONFIG_WITH_STRCACHE2
2478 strcache2_print_stats (&variable_strcache, "# ");
2479#endif
2480}
2481
2482#ifdef CONFIG_WITH_PRINT_STATS_SWITCH
2483void
2484print_variable_stats (void)
2485{
2486 fputs (_("\n# Global variable hash-table stats:\n# "), stdout);
2487 hash_print_stats (&global_variable_set.table, stdout);
2488 fputs ("\n", stdout);
2489}
2490#endif
2491
2492/* Print all the local variables of FILE. */
2493
2494void
2495print_file_variables (const struct file *file)
2496{
2497 if (file->variables != 0)
2498 print_variable_set (file->variables->set, "# ");
2499}
2500
2501#ifdef WINDOWS32
2502void
2503sync_Path_environment (void)
2504{
2505 char *path = allocated_variable_expand ("$(PATH)");
2506 static char *environ_path = NULL;
2507
2508 if (!path)
2509 return;
2510
2511 /*
2512 * If done this before, don't leak memory unnecessarily.
2513 * Free the previous entry before allocating new one.
2514 */
2515 if (environ_path)
2516 free (environ_path);
2517
2518 /*
2519 * Create something WINDOWS32 world can grok
2520 */
2521 convert_Path_to_windows32 (path, ';');
2522 environ_path = xstrdup (concat ("PATH", "=", path));
2523 putenv (environ_path);
2524 free (path);
2525}
2526#endif
Note: See TracBrowser for help on using the repository browser.