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

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

kmk: Made struct variable capable of holding read only variables to speed up set_file_variables.

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