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

Last change on this file since 2769 was 2769, checked in by bird, 10 years ago

String expansion 'compilation' and 'execution' code is mostly done.

  • Property svn:eol-style set to native
File size: 96.8 KB
Line 
1/* Internals of variables for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
42010 Free Software Foundation, 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# ifdef WINDOWS32
36# include <Windows.h>
37# else
38# include <sys/utsname.h>
39# endif
40#endif
41#ifdef CONFIG_WITH_STRCACHE2
42# include <stddef.h>
43#endif
44#ifdef CONFIG_WITH_COMPILER
45# include "kmk_cc_exec.h"
46#endif
47
48#ifdef KMK
49/** Gets the real variable if alias. For use when looking up variables. */
50# define RESOLVE_ALIAS_VARIABLE(v) \
51 do { \
52 if ((v) != NULL && (v)->alias) \
53 { \
54 (v) = (struct variable *)(v)->value; \
55 assert ((v)->aliased); \
56 assert (!(v)->alias); \
57 } \
58 } while (0)
59#endif
60
61
62/* Chain of all pattern-specific variables. */
63
64static struct pattern_var *pattern_vars;
65
66/* Pointer to the last struct in the pack of a specific size, from 1 to 255.*/
67
68static struct pattern_var *last_pattern_vars[256];
69
70/* Create a new pattern-specific variable struct. The new variable is
71 inserted into the PATTERN_VARS list in the shortest patterns first
72 order to support the shortest stem matching (the variables are
73 matched in the reverse order so the ones with the longest pattern
74 will be considered first). Variables with the same pattern length
75 are inserted in the definition order. */
76
77struct pattern_var *
78create_pattern_var (const char *target, const char *suffix)
79{
80 register unsigned int len = strlen (target);
81 register struct pattern_var *p = xmalloc (sizeof (struct pattern_var));
82
83 if (pattern_vars != 0)
84 {
85 if (len < 256 && last_pattern_vars[len] != 0)
86 {
87 p->next = last_pattern_vars[len]->next;
88 last_pattern_vars[len]->next = p;
89 }
90 else
91 {
92 /* Find the position where we can insert this variable. */
93 register struct pattern_var **v;
94
95 for (v = &pattern_vars; ; v = &(*v)->next)
96 {
97 /* Insert at the end of the pack so that patterns with the
98 same length appear in the order they were defined .*/
99
100 if (*v == 0 || (*v)->len > len)
101 {
102 p->next = *v;
103 *v = p;
104 break;
105 }
106 }
107 }
108 }
109 else
110 {
111 pattern_vars = p;
112 p->next = 0;
113 }
114
115 p->target = target;
116 p->len = len;
117 p->suffix = suffix + 1;
118
119 if (len < 256)
120 last_pattern_vars[len] = p;
121
122 return p;
123}
124
125/* Look up a target in the pattern-specific variable list. */
126
127static struct pattern_var *
128lookup_pattern_var (struct pattern_var *start, const char *target)
129{
130 struct pattern_var *p;
131 unsigned int targlen = strlen(target);
132
133 for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
134 {
135 const char *stem;
136 unsigned int stemlen;
137
138 if (p->len > targlen)
139 /* It can't possibly match. */
140 continue;
141
142 /* From the lengths of the filename and the pattern parts,
143 find the stem: the part of the filename that matches the %. */
144 stem = target + (p->suffix - p->target - 1);
145 stemlen = targlen - p->len + 1;
146
147 /* Compare the text in the pattern before the stem, if any. */
148 if (stem > target && !strneq (p->target, target, stem - target))
149 continue;
150
151 /* Compare the text in the pattern after the stem, if any.
152 We could test simply using streq, but this way we compare the
153 first two characters immediately. This saves time in the very
154 common case where the first character matches because it is a
155 period. */
156 if (*p->suffix == stem[stemlen]
157 && (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
158 break;
159 }
160
161 return p;
162}
163
164
165#ifdef CONFIG_WITH_STRCACHE2
166struct strcache2 variable_strcache;
167#endif
168
169/* Hash table of all global variable definitions. */
170
171#ifndef CONFIG_WITH_STRCACHE2
172static unsigned long
173variable_hash_1 (const void *keyv)
174{
175 struct variable const *key = (struct variable const *) keyv;
176 return_STRING_N_HASH_1 (key->name, key->length);
177}
178
179static unsigned long
180variable_hash_2 (const void *keyv)
181{
182 struct variable const *key = (struct variable const *) keyv;
183 return_STRING_N_HASH_2 (key->name, key->length);
184}
185
186static int
187variable_hash_cmp (const void *xv, const void *yv)
188{
189 struct variable const *x = (struct variable const *) xv;
190 struct variable const *y = (struct variable const *) yv;
191 int result = x->length - y->length;
192 if (result)
193 return result;
194
195 return_STRING_N_COMPARE (x->name, y->name, x->length);
196}
197#endif /* !CONFIG_WITH_STRCACHE2 */
198
199#ifndef VARIABLE_BUCKETS
200# ifdef KMK /* Move to Makefile.kmk? (insanely high, but wtf, it gets the collitions down) */
201# define VARIABLE_BUCKETS 65535
202# else /*!KMK*/
203#define VARIABLE_BUCKETS 523
204# endif /*!KMK*/
205#endif
206#ifndef PERFILE_VARIABLE_BUCKETS
207# ifdef KMK /* Move to Makefile.kmk? */
208# define PERFILE_VARIABLE_BUCKETS 127
209# else
210#define PERFILE_VARIABLE_BUCKETS 23
211# endif
212#endif
213#ifndef SMALL_SCOPE_VARIABLE_BUCKETS
214# ifdef KMK /* Move to Makefile.kmk? */
215# define SMALL_SCOPE_VARIABLE_BUCKETS 63
216# else
217#define SMALL_SCOPE_VARIABLE_BUCKETS 13
218# endif
219#endif
220
221
222#ifdef KMK /* Drop the 'static' */
223struct variable_set global_variable_set;
224struct variable_set_list global_setlist
225#else
226static struct variable_set global_variable_set;
227static struct variable_set_list global_setlist
228#endif
229 = { 0, &global_variable_set, 0 };
230struct variable_set_list *current_variable_set_list = &global_setlist;
231
232
233/* Implement variables. */
234
235void
236init_hash_global_variable_set (void)
237{
238#ifndef CONFIG_WITH_STRCACHE2
239 hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
240 variable_hash_1, variable_hash_2, variable_hash_cmp);
241#else /* CONFIG_WITH_STRCACHE2 */
242 strcache2_init (&variable_strcache, "variable", 65536, 0, 0, 0);
243 hash_init_strcached (&global_variable_set.table, VARIABLE_BUCKETS,
244 &variable_strcache, offsetof (struct variable, name));
245#endif /* CONFIG_WITH_STRCACHE2 */
246}
247
248/* Define variable named NAME with value VALUE in SET. VALUE is copied.
249 LENGTH is the length of NAME, which does not need to be null-terminated.
250 ORIGIN specifies the origin of the variable (makefile, command line
251 or environment).
252 If RECURSIVE is nonzero a flag is set in the variable saying
253 that it should be recursively re-expanded. */
254
255#ifdef CONFIG_WITH_VALUE_LENGTH
256struct variable *
257define_variable_in_set (const char *name, unsigned int length,
258 const char *value, unsigned int value_len,
259 int duplicate_value, enum variable_origin origin,
260 int recursive, struct variable_set *set,
261 const struct floc *flocp)
262#else
263struct variable *
264define_variable_in_set (const char *name, unsigned int length,
265 const char *value, enum variable_origin origin,
266 int recursive, struct variable_set *set,
267 const struct floc *flocp)
268#endif
269{
270 struct variable *v;
271 struct variable **var_slot;
272 struct variable var_key;
273
274 if (env_overrides && origin == o_env)
275 origin = o_env_override;
276
277#ifndef KMK
278 if (set == NULL)
279 set = &global_variable_set;
280#else /* KMK */
281 /* Intercept kBuild object variable definitions. */
282 if (name[0] == '[' && length > 3)
283 {
284 v = try_define_kbuild_object_variable_via_accessor (name, length,
285 value, value_len, duplicate_value,
286 origin, recursive, flocp);
287 if (v != VAR_NOT_KBUILD_ACCESSOR)
288 return v;
289 }
290 if (set == NULL)
291 {
292 if (g_pTopKbEvalData)
293 return define_kbuild_object_variable_in_top_obj (name, length,
294 value, value_len, duplicate_value,
295 origin, recursive, flocp);
296 set = &global_variable_set;
297 }
298#endif /* KMK */
299
300#ifndef CONFIG_WITH_STRCACHE2
301 var_key.name = (char *) name;
302 var_key.length = length;
303 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
304
305 /* if (env_overrides && origin == o_env)
306 origin = o_env_override; - bird moved this up */
307
308 v = *var_slot;
309#else /* CONFIG_WITH_STRCACHE2 */
310 name = strcache2_add (&variable_strcache, name, length);
311 if ( set != &global_variable_set
312 || !(v = strcache2_get_user_val (&variable_strcache, name)))
313 {
314 var_key.name = name;
315 var_key.length = length;
316 var_slot = (struct variable **) hash_find_slot_strcached (&set->table, &var_key);
317 v = *var_slot;
318 }
319 else
320 {
321 assert (!v || (v->name == name && !HASH_VACANT (v)));
322 var_slot = 0;
323 }
324#endif /* CONFIG_WITH_STRCACHE2 */
325 if (! HASH_VACANT (v))
326 {
327#ifdef KMK
328 RESOLVE_ALIAS_VARIABLE(v);
329#endif
330 if (env_overrides && v->origin == o_env)
331 /* V came from in the environment. Since it was defined
332 before the switches were parsed, it wasn't affected by -e. */
333 v->origin = o_env_override;
334
335 /* A variable of this name is already defined.
336 If the old definition is from a stronger source
337 than this one, don't redefine it. */
338 if ((int) origin >= (int) v->origin)
339 {
340#ifdef CONFIG_WITH_VALUE_LENGTH
341 if (value_len == ~0U)
342 value_len = strlen (value);
343 else
344 assert (value_len == strlen (value));
345 if (!duplicate_value || duplicate_value == -1)
346 {
347# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
348 if (v->value != 0 && !v->rdonly_val)
349 free (v->value);
350 v->rdonly_val = duplicate_value == -1;
351 v->value = (char *) value;
352 v->value_alloc_len = 0;
353# else
354 if (v->value != 0)
355 free (v->value);
356 v->value = (char *) value;
357 v->value_alloc_len = value_len + 1;
358# endif
359 }
360 else
361 {
362 if (v->value_alloc_len <= value_len)
363 {
364# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
365 if (v->rdonly_val)
366 v->rdonly_val = 0;
367 else
368# endif
369 free (v->value);
370 v->value_alloc_len = VAR_ALIGN_VALUE_ALLOC (value_len + 1);
371 v->value = xmalloc (v->value_alloc_len);
372 MAKE_STATS_2(v->reallocs++);
373 }
374 memcpy (v->value, value, value_len + 1);
375 }
376 v->value_length = value_len;
377#else /* !CONFIG_WITH_VALUE_LENGTH */
378 if (v->value != 0)
379 free (v->value);
380 v->value = xstrdup (value);
381#endif /* !CONFIG_WITH_VALUE_LENGTH */
382 if (flocp != 0)
383 v->fileinfo = *flocp;
384 else
385 v->fileinfo.filenm = 0;
386 v->origin = origin;
387 v->recursive = recursive;
388 MAKE_STATS_2(v->changes++);
389#ifdef CONFIG_WITH_COMPILER
390 if (v->evalprog || v->expandprog)
391 kmk_cc_variable_changed (v);
392#endif
393 }
394 return v;
395 }
396
397 /* Create a new variable definition and add it to the hash table. */
398
399#ifndef CONFIG_WITH_ALLOC_CACHES
400 v = xmalloc (sizeof (struct variable));
401#else
402 v = alloccache_alloc (&variable_cache);
403#endif
404#ifndef CONFIG_WITH_STRCACHE2
405 v->name = xstrndup (name, length);
406#else
407 v->name = name; /* already cached. */
408#endif
409 v->length = length;
410 hash_insert_at (&set->table, v, var_slot);
411#ifdef CONFIG_WITH_VALUE_LENGTH
412 if (value_len == ~0U)
413 value_len = strlen (value);
414 else
415 assert (value_len == strlen (value));
416 v->value_length = value_len;
417 if (!duplicate_value || duplicate_value == -1)
418 {
419# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
420 v->rdonly_val = duplicate_value == -1;
421 v->value_alloc_len = v->rdonly_val ? 0 : value_len + 1;
422# endif
423 v->value = (char *)value;
424 }
425 else
426 {
427# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
428 v->rdonly_val = 0;
429# endif
430 v->value_alloc_len = VAR_ALIGN_VALUE_ALLOC (value_len + 1);
431 v->value = xmalloc (v->value_alloc_len);
432 memcpy (v->value, value, value_len + 1);
433 }
434#else /* !CONFIG_WITH_VALUE_LENGTH */
435 v->value = xstrdup (value);
436#endif /* !CONFIG_WITH_VALUE_LENGTH */
437 if (flocp != 0)
438 v->fileinfo = *flocp;
439 else
440 v->fileinfo.filenm = 0;
441 v->origin = origin;
442 v->recursive = recursive;
443 v->special = 0;
444 v->expanding = 0;
445 v->exp_count = 0;
446 v->per_target = 0;
447 v->append = 0;
448 v->private_var = 0;
449#ifdef KMK
450 v->alias = 0;
451 v->aliased = 0;
452#endif
453 v->export = v_default;
454#ifdef CONFIG_WITH_COMPILER
455 v->evalprog = 0;
456 v->expandprog = 0;
457 v->evalval_count = 0;
458 v->expand_count = 0;
459#else
460 MAKE_STATS_2(v->expand_count = 0);
461 MAKE_STATS_2(v->evalval_count = 0);
462#endif
463 MAKE_STATS_2(v->changes = 0);
464 MAKE_STATS_2(v->reallocs = 0);
465 MAKE_STATS_2(v->references = 0);
466 MAKE_STATS_2(v->cTicksEvalVal = 0);
467
468 v->exportable = 1;
469 if (*name != '_' && (*name < 'A' || *name > 'Z')
470 && (*name < 'a' || *name > 'z'))
471 v->exportable = 0;
472 else
473 {
474 for (++name; *name != '\0'; ++name)
475 if (*name != '_' && (*name < 'a' || *name > 'z')
476 && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
477 break;
478
479 if (*name != '\0')
480 v->exportable = 0;
481 }
482
483#ifdef CONFIG_WITH_STRCACHE2
484 /* If it's the global set, remember the variable. */
485 if (set == &global_variable_set)
486 strcache2_set_user_val (&variable_strcache, v->name, v);
487#endif
488 return v;
489}
490
491
492
493/* Undefine variable named NAME in SET. LENGTH is the length of NAME, which
494 does not need to be null-terminated. ORIGIN specifies the origin of the
495 variable (makefile, command line or environment). */
496
497static void
498free_variable_name_and_value (const void *item);
499
500void
501undefine_variable_in_set (const char *name, unsigned int length,
502 enum variable_origin origin,
503 struct variable_set *set)
504{
505 struct variable *v;
506 struct variable **var_slot;
507 struct variable var_key;
508
509 if (set == NULL)
510 set = &global_variable_set;
511
512#ifndef CONFIG_WITH_STRCACHE2
513 var_key.name = (char *) name;
514 var_key.length = length;
515 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
516#else
517 var_key.name = strcache2_lookup(&variable_strcache, name, length);
518 if (!var_key.name)
519 return;
520 var_key.length = length;
521 var_slot = (struct variable **) hash_find_slot_strcached (&set->table, &var_key);
522#endif
523
524 if (env_overrides && origin == o_env)
525 origin = o_env_override;
526
527 v = *var_slot;
528 if (! HASH_VACANT (v))
529 {
530#ifdef KMK
531 if (v->aliased || v->alias)
532 {
533 if (v->aliased)
534 error (NULL, _("Cannot undefine the aliased variable '%s'"), v->name);
535 else
536 error (NULL, _("Cannot undefine the variable alias '%s'"), v->name);
537 return;
538 }
539#endif
540
541 if (env_overrides && v->origin == o_env)
542 /* V came from in the environment. Since it was defined
543 before the switches were parsed, it wasn't affected by -e. */
544 v->origin = o_env_override;
545
546 /* If the definition is from a stronger source than this one, don't
547 undefine it. */
548 if ((int) origin >= (int) v->origin)
549 {
550 hash_delete_at (&set->table, var_slot);
551#ifdef CONFIG_WITH_STRCACHE2
552 if (set == &global_variable_set)
553 strcache2_set_user_val (&variable_strcache, v->name, NULL);
554#endif
555 free_variable_name_and_value (v);
556 }
557 }
558}
559
560#ifdef KMK
561/* Define variable named NAME as an alias of the variable TARGET.
562 SET defaults to the global set if NULL. FLOCP is just for completeness. */
563
564struct variable *
565define_variable_alias_in_set (const char *name, unsigned int length,
566 struct variable *target, enum variable_origin origin,
567 struct variable_set *set, const struct floc *flocp)
568{
569 struct variable *v;
570 struct variable **var_slot;
571
572 /* Look it up the hash table slot for it. */
573 name = strcache2_add (&variable_strcache, name, length);
574 if ( set != &global_variable_set
575 || !(v = strcache2_get_user_val (&variable_strcache, name)))
576 {
577 struct variable var_key;
578
579 var_key.name = name;
580 var_key.length = length;
581 var_slot = (struct variable **) hash_find_slot_strcached (&set->table, &var_key);
582 v = *var_slot;
583 }
584 else
585 {
586 assert (!v || (v->name == name && !HASH_VACANT (v)));
587 var_slot = 0;
588 }
589 if (! HASH_VACANT (v))
590 {
591 /* A variable of this name is already defined.
592 If the old definition is from a stronger source
593 than this one, don't redefine it. */
594
595 if (env_overrides && v->origin == o_env)
596 /* V came from in the environment. Since it was defined
597 before the switches were parsed, it wasn't affected by -e. */
598 v->origin = o_env_override;
599
600 if ((int) origin < (int) v->origin)
601 return v;
602
603 if (v->value != 0 && !v->rdonly_val)
604 free (v->value);
605 MAKE_STATS_2(v->changes++);
606#ifdef CONFIG_WITH_COMPILER
607 if (v->evalprog || v->expandprog)
608 kmk_cc_variable_changed (v);
609#endif
610 }
611 else
612 {
613 /* Create a new variable definition and add it to the hash table. */
614 v = alloccache_alloc (&variable_cache);
615 v->name = name; /* already cached. */
616 v->length = length;
617 hash_insert_at (&set->table, v, var_slot);
618 v->special = 0;
619 v->expanding = 0;
620 v->exp_count = 0;
621 v->per_target = 0;
622 v->append = 0;
623 v->private_var = 0;
624 v->aliased = 0;
625 v->export = v_default;
626#ifdef CONFIG_WITH_COMPILER
627 v->evalprog = 0;
628 v->expandprog = 0;
629 v->evalval_count = 0;
630 v->expand_count = 0;
631#else
632 MAKE_STATS_2(v->expand_count = 0);
633 MAKE_STATS_2(v->evalval_count = 0);
634#endif
635 MAKE_STATS_2(v->changes = 0);
636 MAKE_STATS_2(v->reallocs = 0);
637 MAKE_STATS_2(v->references = 0);
638 MAKE_STATS_2(v->cTicksEvalVal = 0);
639 v->exportable = 1;
640 if (*name != '_' && (*name < 'A' || *name > 'Z')
641 && (*name < 'a' || *name > 'z'))
642 v->exportable = 0;
643 else
644 {
645 for (++name; *name != '\0'; ++name)
646 if (*name != '_' && (*name < 'a' || *name > 'z')
647 && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
648 break;
649
650 if (*name != '\0')
651 v->exportable = 0;
652 }
653
654 /* If it's the global set, remember the variable. */
655 if (set == &global_variable_set)
656 strcache2_set_user_val (&variable_strcache, v->name, v);
657 }
658
659 /* Common variable setup. */
660 v->alias = 1;
661 v->rdonly_val = 1;
662 v->value = (char *)target;
663 v->value_length = sizeof(*target); /* Non-zero to provoke trouble. */
664 v->value_alloc_len = sizeof(*target);
665 if (flocp != 0)
666 v->fileinfo = *flocp;
667 else
668 v->fileinfo.filenm = 0;
669 v->origin = origin;
670 v->recursive = 0;
671
672 /* Mark the target as aliased. */
673 target->aliased = 1;
674
675 return v;
676}
677#endif /* KMK */
678
679/* If the variable passed in is "special", handle its special nature.
680 Currently there are two such variables, both used for introspection:
681 .VARIABLES expands to a list of all the variables defined in this instance
682 of make.
683 .TARGETS expands to a list of all the targets defined in this
684 instance of make.
685 Returns the variable reference passed in. */
686
687#define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
688
689static struct variable *
690lookup_special_var (struct variable *var)
691{
692 static unsigned long last_var_count = 0;
693
694
695 /* This one actually turns out to be very hard, due to the way the parser
696 records targets. The way it works is that target information is collected
697 internally until make knows the target is completely specified. It unitl
698 it sees that some new construct (a new target or variable) is defined that
699 it knows the previous one is done. In short, this means that if you do
700 this:
701
702 all:
703
704 TARGS := $(.TARGETS)
705
706 then $(TARGS) won't contain "all", because it's not until after the
707 variable is created that the previous target is completed.
708
709 Changing this would be a major pain. I think a less complex way to do it
710 would be to pre-define the target files as soon as the first line is
711 parsed, then come back and do the rest of the definition as now. That
712 would allow $(.TARGETS) to be correct without a major change to the way
713 the parser works.
714
715 if (streq (var->name, ".TARGETS"))
716 var->value = build_target_list (var->value);
717 else
718 */
719
720 if (streq (var->name, ".VARIABLES")
721 && global_variable_set.table.ht_fill != last_var_count)
722 {
723#ifndef CONFIG_WITH_VALUE_LENGTH
724 unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
725#else
726 unsigned long max = EXPANSION_INCREMENT (var->value_length);
727#endif
728 unsigned long len;
729 char *p;
730 struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
731 struct variable **end = &vp[global_variable_set.table.ht_size];
732
733 /* Make sure we have at least MAX bytes in the allocated buffer. */
734 var->value = xrealloc (var->value, max);
735 MAKE_STATS_2(var->reallocs++);
736
737 /* Walk through the hash of variables, constructing a list of names. */
738 p = var->value;
739 len = 0;
740 for (; vp < end; ++vp)
741 if (!HASH_VACANT (*vp))
742 {
743 struct variable *v = *vp;
744 int l = v->length;
745
746 len += l + 1;
747 if (len > max)
748 {
749 unsigned long off = p - var->value;
750
751 max += EXPANSION_INCREMENT (l + 1);
752 var->value = xrealloc (var->value, max);
753 p = &var->value[off];
754 MAKE_STATS_2(var->reallocs++);
755 }
756
757 memcpy (p, v->name, l);
758 p += l;
759 *(p++) = ' ';
760 }
761 *(p-1) = '\0';
762#ifdef CONFIG_WITH_VALUE_LENGTH
763 var->value_length = p - var->value - 1;
764 var->value_alloc_len = max;
765#endif
766
767 /* Remember how many variables are in our current count. Since we never
768 remove variables from the list, this is a reliable way to know whether
769 the list is up to date or needs to be recomputed. */
770
771 last_var_count = global_variable_set.table.ht_fill;
772 }
773
774 return var;
775}
776
777
778
779#if 0 /*FIX THIS - def KMK*/ /* bird: speed */
780MY_INLINE struct variable *
781lookup_cached_variable (const char *name)
782{
783 const struct variable_set_list *setlist = current_variable_set_list;
784 struct hash_table *ht;
785 unsigned int hash_1;
786 unsigned int hash_2;
787 unsigned int idx;
788 struct variable *v;
789
790 /* first set, first entry, both unrolled. */
791
792 if (setlist->set == &global_variable_set)
793 {
794 v = (struct variable *) strcache2_get_user_val (&variable_strcache, name);
795 if (MY_PREDICT_TRUE (v))
796 return MY_PREDICT_FALSE (v->special) ? lookup_special_var (v) : v;
797 assert (setlist->next == 0);
798 return 0;
799 }
800
801 hash_1 = strcache2_calc_ptr_hash (&variable_strcache, name);
802 ht = &setlist->set->table;
803 MAKE_STATS (ht->ht_lookups++);
804 idx = hash_1 & (ht->ht_size - 1);
805 v = ht->ht_vec[idx];
806 if (v != 0)
807 {
808 if ( (void *)v != hash_deleted_item
809 && v->name == name)
810 return MY_PREDICT_FALSE (v->special) ? lookup_special_var (v) : v;
811
812 /* the rest of the loop */
813 hash_2 = strcache2_get_hash (&variable_strcache, name) | 1;
814 for (;;)
815 {
816 idx += hash_2;
817 idx &= (ht->ht_size - 1);
818 v = (struct variable *) ht->ht_vec[idx];
819 MAKE_STATS (ht->ht_collisions++); /* there are hardly any deletions, so don't bother with not counting deleted clashes. */
820
821 if (v == 0)
822 break;
823 if ( (void *)v != hash_deleted_item
824 && v->name == name)
825 return MY_PREDICT_FALSE (v->special) ? lookup_special_var (v) : v;
826 } /* inner collision loop */
827 }
828 else
829 hash_2 = strcache2_get_hash (&variable_strcache, name) | 1;
830
831
832 /* The other sets, if any. */
833
834 setlist = setlist->next;
835 while (setlist)
836 {
837 if (setlist->set == &global_variable_set)
838 {
839 v = (struct variable *) strcache2_get_user_val (&variable_strcache, name);
840 if (MY_PREDICT_TRUE (v))
841 return MY_PREDICT_FALSE (v->special) ? lookup_special_var (v) : v;
842 assert (setlist->next == 0);
843 return 0;
844 }
845
846 /* first iteration unrolled */
847 ht = &setlist->set->table;
848 MAKE_STATS (ht->ht_lookups++);
849 idx = hash_1 & (ht->ht_size - 1);
850 v = ht->ht_vec[idx];
851 if (v != 0)
852 {
853 if ( (void *)v != hash_deleted_item
854 && v->name == name)
855 return MY_PREDICT_FALSE (v->special) ? lookup_special_var (v) : v;
856
857 /* the rest of the loop */
858 for (;;)
859 {
860 idx += hash_2;
861 idx &= (ht->ht_size - 1);
862 v = (struct variable *) ht->ht_vec[idx];
863 MAKE_STATS (ht->ht_collisions++); /* see reason above */
864
865 if (v == 0)
866 break;
867 if ( (void *)v != hash_deleted_item
868 && v->name == name)
869 return MY_PREDICT_FALSE (v->special) ? lookup_special_var (v) : v;
870 } /* inner collision loop */
871 }
872
873 /* next */
874 setlist = setlist->next;
875 }
876
877 return 0;
878}
879
880# ifndef NDEBUG
881struct variable *
882lookup_variable_for_assert (const char *name, unsigned int length)
883{
884 const struct variable_set_list *setlist;
885 struct variable var_key;
886 var_key.name = name;
887 var_key.length = length;
888
889 for (setlist = current_variable_set_list;
890 setlist != 0; setlist = setlist->next)
891 {
892 struct variable *v;
893 v = (struct variable *) hash_find_item_strcached (&setlist->set->table, &var_key);
894 if (v)
895 return MY_PREDICT_FALSE (v->special) ? lookup_special_var (v) : v;
896 }
897 return 0;
898}
899# endif /* !NDEBUG */
900#endif /* KMK - need for speed */
901
902/* Lookup a variable whose name is a string starting at NAME
903 and with LENGTH chars. NAME need not be null-terminated.
904 Returns address of the `struct variable' containing all info
905 on the variable, or nil if no such variable is defined. */
906
907struct variable *
908lookup_variable (const char *name, unsigned int length)
909{
910#if 1 /*FIX THIS - ndef KMK*/
911 const struct variable_set_list *setlist;
912 struct variable var_key;
913#else /* KMK */
914 struct variable *v;
915#endif /* KMK */
916 int is_parent = 0;
917#ifdef CONFIG_WITH_STRCACHE2
918 const char *cached_name;
919#endif
920
921# ifdef KMK
922 /* Check for kBuild-define- local variable accesses and handle these first. */
923 if (length > 3 && name[0] == '[')
924 {
925 struct variable *v = lookup_kbuild_object_variable_accessor(name, length);
926 if (v != VAR_NOT_KBUILD_ACCESSOR)
927 {
928 MAKE_STATS_2 (v->references++);
929 return v;
930 }
931 }
932# endif
933
934#ifdef CONFIG_WITH_STRCACHE2
935 /* lookup the name in the string case, if it's not there it won't
936 be in any of the sets either. */
937 cached_name = strcache2_lookup (&variable_strcache, name, length);
938 if (!cached_name)
939 return NULL;
940 name = cached_name;
941#endif /* CONFIG_WITH_STRCACHE2 */
942#if 1 /*FIX THIS - ndef KMK */
943
944 var_key.name = (char *) name;
945 var_key.length = length;
946
947 for (setlist = current_variable_set_list;
948 setlist != 0; setlist = setlist->next)
949 {
950 const struct variable_set *set = setlist->set;
951 struct variable *v;
952
953# ifndef CONFIG_WITH_STRCACHE2
954 v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
955# else /* CONFIG_WITH_STRCACHE2 */
956 v = (struct variable *) hash_find_item_strcached ((struct hash_table *) &set->table, &var_key);
957# endif /* CONFIG_WITH_STRCACHE2 */
958 if (v && (!is_parent || !v->private_var))
959 {
960# ifdef KMK
961 RESOLVE_ALIAS_VARIABLE(v);
962# endif
963 MAKE_STATS_2 (v->references++);
964 return v->special ? lookup_special_var (v) : v;
965 }
966
967 is_parent |= setlist->next_is_parent;
968 }
969
970#else /* KMK - need for speed */
971
972 v = lookup_cached_variable (name);
973 assert (lookup_variable_for_assert(name, length) == v);
974#ifdef VMS
975 if (v)
976#endif
977 return v;
978#endif /* KMK - need for speed */
979#ifdef VMS
980 /* since we don't read envp[] on startup, try to get the
981 variable via getenv() here. */
982 {
983 char *vname = alloca (length + 1);
984 char *value;
985 strncpy (vname, name, length);
986 vname[length] = 0;
987 value = getenv (vname);
988 if (value != 0)
989 {
990 char *sptr;
991 int scnt;
992
993 sptr = value;
994 scnt = 0;
995
996 while ((sptr = strchr (sptr, '$')))
997 {
998 scnt++;
999 sptr++;
1000 }
1001
1002 if (scnt > 0)
1003 {
1004 char *nvalue;
1005 char *nptr;
1006
1007 nvalue = alloca (strlen (value) + scnt + 1);
1008 sptr = value;
1009 nptr = nvalue;
1010
1011 while (*sptr)
1012 {
1013 if (*sptr == '$')
1014 {
1015 *nptr++ = '$';
1016 *nptr++ = '$';
1017 }
1018 else
1019 {
1020 *nptr++ = *sptr;
1021 }
1022 sptr++;
1023 }
1024
1025 *nptr = '\0';
1026 return define_variable (vname, length, nvalue, o_env, 1);
1027
1028 }
1029
1030 return define_variable (vname, length, value, o_env, 1);
1031 }
1032 }
1033#endif /* VMS */
1034
1035 return 0;
1036}
1037
1038#ifdef CONFIG_WITH_STRCACHE2
1039/* Alternative version of lookup_variable that takes a name that's already in
1040 the variable string cache. */
1041struct variable *
1042lookup_variable_strcached (const char *name)
1043{
1044 struct variable *v;
1045#if 1 /*FIX THIS - ndef KMK*/
1046 const struct variable_set_list *setlist;
1047 struct variable var_key;
1048#endif /* KMK */
1049 int is_parent = 0;
1050
1051#ifndef NDEBUG
1052 strcache2_verify_entry (&variable_strcache, name);
1053#endif
1054
1055#ifdef KMK
1056 /* Check for kBuild-define- local variable accesses and handle these first. */
1057 if (strcache2_get_len(&variable_strcache, name) > 3 && name[0] == '[')
1058 {
1059 v = lookup_kbuild_object_variable_accessor(name, strcache2_get_len(&variable_strcache, name));
1060 if (v != VAR_NOT_KBUILD_ACCESSOR)
1061 {
1062 MAKE_STATS_2 (v->references++);
1063 return v;
1064 }
1065 }
1066#endif
1067
1068#if 1 /*FIX THIS - ndef KMK */
1069
1070 var_key.name = (char *) name;
1071 var_key.length = strcache2_get_len(&variable_strcache, name);
1072
1073 for (setlist = current_variable_set_list;
1074 setlist != 0; setlist = setlist->next)
1075 {
1076 const struct variable_set *set = setlist->set;
1077
1078 v = (struct variable *) hash_find_item_strcached ((struct hash_table *) &set->table, &var_key);
1079 if (v && (!is_parent || !v->private_var))
1080 {
1081# ifdef KMK
1082 RESOLVE_ALIAS_VARIABLE(v);
1083# endif
1084 MAKE_STATS_2 (v->references++);
1085 return v->special ? lookup_special_var (v) : v;
1086 }
1087
1088 is_parent |= setlist->next_is_parent;
1089 }
1090
1091#else /* KMK - need for speed */
1092
1093 v = lookup_cached_variable (name);
1094 assert (lookup_variable_for_assert(name, length) == v);
1095#ifdef VMS
1096 if (v)
1097#endif
1098 return v;
1099#endif /* KMK - need for speed */
1100#ifdef VMS
1101# error "Port me (split out the relevant code from lookup_varaible and call it)"
1102#endif
1103 return 0;
1104}
1105#endif
1106
1107
1108
1109/* Lookup a variable whose name is a string starting at NAME
1110 and with LENGTH chars in set SET. NAME need not be null-terminated.
1111 Returns address of the `struct variable' containing all info
1112 on the variable, or nil if no such variable is defined. */
1113
1114struct variable *
1115lookup_variable_in_set (const char *name, unsigned int length,
1116 const struct variable_set *set)
1117{
1118 struct variable var_key;
1119#ifndef CONFIG_WITH_STRCACHE2
1120 var_key.name = (char *) name;
1121 var_key.length = length;
1122
1123 return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
1124#else /* CONFIG_WITH_STRCACHE2 */
1125 const char *cached_name;
1126 struct variable *v;
1127
1128# ifdef KMK
1129 /* Check for kBuild-define- local variable accesses and handle these first. */
1130 if (length > 3 && name[0] == '[' && set == &global_variable_set)
1131 {
1132 v = lookup_kbuild_object_variable_accessor(name, length);
1133 if (v != VAR_NOT_KBUILD_ACCESSOR)
1134 {
1135 RESOLVE_ALIAS_VARIABLE(v);
1136 MAKE_STATS_2 (v->references++);
1137 return v;
1138 }
1139 }
1140# endif
1141
1142 /* lookup the name in the string case, if it's not there it won't
1143 be in any of the sets either. Optimize lookups in the global set. */
1144 cached_name = strcache2_lookup(&variable_strcache, name, length);
1145 if (!cached_name)
1146 return NULL;
1147
1148 if (set == &global_variable_set)
1149 {
1150 v = strcache2_get_user_val (&variable_strcache, cached_name);
1151 assert (!v || v->name == cached_name);
1152 }
1153 else
1154 {
1155 var_key.name = cached_name;
1156 var_key.length = length;
1157
1158 v = (struct variable *) hash_find_item_strcached (
1159 (struct hash_table *) &set->table, &var_key);
1160 }
1161# ifdef KMK
1162 RESOLVE_ALIAS_VARIABLE(v);
1163# endif
1164 MAKE_STATS_2 (if (v) v->references++);
1165 return v;
1166#endif /* CONFIG_WITH_STRCACHE2 */
1167}
1168
1169
1170/* Initialize FILE's variable set list. If FILE already has a variable set
1171 list, the topmost variable set is left intact, but the the rest of the
1172 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
1173 rule, then we will use the "root" double-colon target's variable set as the
1174 parent of FILE's variable set.
1175
1176 If we're READING a makefile, don't do the pattern variable search now,
1177 since the pattern variable might not have been defined yet. */
1178
1179void
1180initialize_file_variables (struct file *file, int reading)
1181{
1182 struct variable_set_list *l = file->variables;
1183
1184 if (l == 0)
1185 {
1186#ifndef CONFIG_WITH_ALLOC_CACHES
1187 l = (struct variable_set_list *)
1188 xmalloc (sizeof (struct variable_set_list));
1189 l->set = xmalloc (sizeof (struct variable_set));
1190#else /* CONFIG_WITH_ALLOC_CACHES */
1191 l = (struct variable_set_list *)
1192 alloccache_alloc (&variable_set_list_cache);
1193 l->set = (struct variable_set *)
1194 alloccache_alloc (&variable_set_cache);
1195#endif /* CONFIG_WITH_ALLOC_CACHES */
1196#ifndef CONFIG_WITH_STRCACHE2
1197 hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
1198 variable_hash_1, variable_hash_2, variable_hash_cmp);
1199#else /* CONFIG_WITH_STRCACHE2 */
1200 hash_init_strcached (&l->set->table, PERFILE_VARIABLE_BUCKETS,
1201 &variable_strcache, offsetof (struct variable, name));
1202#endif /* CONFIG_WITH_STRCACHE2 */
1203 file->variables = l;
1204 }
1205
1206 /* If this is a double-colon, then our "parent" is the "root" target for
1207 this double-colon rule. Since that rule has the same name, parent,
1208 etc. we can just use its variables as the "next" for ours. */
1209
1210 if (file->double_colon && file->double_colon != file)
1211 {
1212 initialize_file_variables (file->double_colon, reading);
1213 l->next = file->double_colon->variables;
1214 l->next_is_parent = 0;
1215 return;
1216 }
1217
1218 if (file->parent == 0)
1219 l->next = &global_setlist;
1220 else
1221 {
1222 initialize_file_variables (file->parent, reading);
1223 l->next = file->parent->variables;
1224 }
1225 l->next_is_parent = 1;
1226
1227 /* If we're not reading makefiles and we haven't looked yet, see if
1228 we can find pattern variables for this target. */
1229
1230 if (!reading && !file->pat_searched)
1231 {
1232 struct pattern_var *p;
1233
1234 p = lookup_pattern_var (0, file->name);
1235 if (p != 0)
1236 {
1237 struct variable_set_list *global = current_variable_set_list;
1238
1239 /* We found at least one. Set up a new variable set to accumulate
1240 all the pattern variables that match this target. */
1241
1242 file->pat_variables = create_new_variable_set ();
1243 current_variable_set_list = file->pat_variables;
1244
1245 do
1246 {
1247 /* We found one, so insert it into the set. */
1248
1249 struct variable *v;
1250
1251 if (p->variable.flavor == f_simple)
1252 {
1253 v = define_variable_loc (
1254 p->variable.name, strlen (p->variable.name),
1255 p->variable.value, p->variable.origin,
1256 0, &p->variable.fileinfo);
1257
1258 v->flavor = f_simple;
1259 }
1260 else
1261 {
1262#ifndef CONFIG_WITH_VALUE_LENGTH
1263 v = do_variable_definition (
1264 &p->variable.fileinfo, p->variable.name,
1265 p->variable.value, p->variable.origin,
1266 p->variable.flavor, 1);
1267#else
1268 v = do_variable_definition_2 (
1269 &p->variable.fileinfo, p->variable.name,
1270 p->variable.value, p->variable.value_length, 0, 0,
1271 p->variable.origin, p->variable.flavor, 1);
1272#endif
1273 }
1274
1275 /* Also mark it as a per-target and copy export status. */
1276 v->per_target = p->variable.per_target;
1277 v->export = p->variable.export;
1278 v->private_var = p->variable.private_var;
1279 }
1280 while ((p = lookup_pattern_var (p, file->name)) != 0);
1281
1282 current_variable_set_list = global;
1283 }
1284 file->pat_searched = 1;
1285 }
1286
1287 /* If we have a pattern variable match, set it up. */
1288
1289 if (file->pat_variables != 0)
1290 {
1291 file->pat_variables->next = l->next;
1292 file->pat_variables->next_is_parent = l->next_is_parent;
1293 l->next = file->pat_variables;
1294 l->next_is_parent = 0;
1295 }
1296}
1297
1298
1299/* Pop the top set off the current variable set list,
1300 and free all its storage. */
1301
1302struct variable_set_list *
1303create_new_variable_set (void)
1304{
1305 register struct variable_set_list *setlist;
1306 register struct variable_set *set;
1307
1308#ifndef CONFIG_WITH_ALLOC_CACHES
1309 set = xmalloc (sizeof (struct variable_set));
1310#else
1311 set = (struct variable_set *) alloccache_alloc (&variable_set_cache);
1312#endif
1313#ifndef CONFIG_WITH_STRCACHE2
1314 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
1315 variable_hash_1, variable_hash_2, variable_hash_cmp);
1316#else /* CONFIG_WITH_STRCACHE2 */
1317 hash_init_strcached (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
1318 &variable_strcache, offsetof (struct variable, name));
1319#endif /* CONFIG_WITH_STRCACHE2 */
1320
1321#ifndef CONFIG_WITH_ALLOC_CACHES
1322 setlist = (struct variable_set_list *)
1323 xmalloc (sizeof (struct variable_set_list));
1324#else
1325 setlist = (struct variable_set_list *)
1326 alloccache_alloc (&variable_set_list_cache);
1327#endif
1328 setlist->set = set;
1329 setlist->next = current_variable_set_list;
1330 setlist->next_is_parent = 0;
1331
1332 return setlist;
1333}
1334
1335static void
1336free_variable_name_and_value (const void *item)
1337{
1338 struct variable *v = (struct variable *) item;
1339#ifndef CONFIG_WITH_STRCACHE2
1340 free (v->name);
1341#endif
1342#ifdef CONFIG_WITH_COMPILER
1343 if (v->evalprog || v->expandprog)
1344 kmk_cc_variable_deleted (v);
1345#endif
1346#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1347 if (!v->rdonly_val)
1348#endif
1349 free (v->value);
1350}
1351
1352void
1353free_variable_set (struct variable_set_list *list)
1354{
1355 hash_map (&list->set->table, free_variable_name_and_value);
1356#ifndef CONFIG_WITH_ALLOC_CACHES
1357 hash_free (&list->set->table, 1);
1358 free (list->set);
1359 free (list);
1360#else
1361 hash_free_cached (&list->set->table, 1, &variable_cache);
1362 alloccache_free (&variable_set_cache, list->set);
1363 alloccache_free (&variable_set_list_cache, list);
1364#endif
1365}
1366
1367/* Create a new variable set and push it on the current setlist.
1368 If we're pushing a global scope (that is, the current scope is the global
1369 scope) then we need to "push" it the other way: file variable sets point
1370 directly to the global_setlist so we need to replace that with the new one.
1371 */
1372
1373struct variable_set_list *
1374push_new_variable_scope (void)
1375{
1376 current_variable_set_list = create_new_variable_set();
1377 if (current_variable_set_list->next == &global_setlist)
1378 {
1379 /* It was the global, so instead of new -> &global we want to replace
1380 &global with the new one and have &global -> new, with current still
1381 pointing to &global */
1382 struct variable_set *set = current_variable_set_list->set;
1383 current_variable_set_list->set = global_setlist.set;
1384 global_setlist.set = set;
1385 current_variable_set_list->next = global_setlist.next;
1386 global_setlist.next = current_variable_set_list;
1387 current_variable_set_list = &global_setlist;
1388 }
1389 return (current_variable_set_list);
1390}
1391
1392void
1393pop_variable_scope (void)
1394{
1395 struct variable_set_list *setlist;
1396 struct variable_set *set;
1397
1398 /* Can't call this if there's no scope to pop! */
1399 assert(current_variable_set_list->next != NULL);
1400
1401 if (current_variable_set_list != &global_setlist)
1402 {
1403 /* We're not pointing to the global setlist, so pop this one. */
1404 setlist = current_variable_set_list;
1405 set = setlist->set;
1406 current_variable_set_list = setlist->next;
1407 }
1408 else
1409 {
1410 /* This set is the one in the global_setlist, but there is another global
1411 set beyond that. We want to copy that set to global_setlist, then
1412 delete what used to be in global_setlist. */
1413 setlist = global_setlist.next;
1414 set = global_setlist.set;
1415 global_setlist.set = setlist->set;
1416 global_setlist.next = setlist->next;
1417 global_setlist.next_is_parent = setlist->next_is_parent;
1418 }
1419
1420 /* Free the one we no longer need. */
1421#ifndef CONFIG_WITH_ALLOC_CACHES
1422 free (setlist);
1423 hash_map (&set->table, free_variable_name_and_value);
1424 hash_free (&set->table, 1);
1425 free (set);
1426#else
1427 alloccache_free (&variable_set_list_cache, setlist);
1428 hash_map (&set->table, free_variable_name_and_value);
1429 hash_free_cached (&set->table, 1, &variable_cache);
1430 alloccache_free (&variable_set_cache, set);
1431#endif
1432}
1433
1434
1435/* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
1436
1437static void
1438merge_variable_sets (struct variable_set *to_set,
1439 struct variable_set *from_set)
1440{
1441 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
1442 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
1443
1444 for ( ; from_var_slot < from_var_end; from_var_slot++)
1445 if (! HASH_VACANT (*from_var_slot))
1446 {
1447 struct variable *from_var = *from_var_slot;
1448 struct variable **to_var_slot
1449#ifndef CONFIG_WITH_STRCACHE2
1450 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
1451#else /* CONFIG_WITH_STRCACHE2 */
1452 = (struct variable **) hash_find_slot_strcached (&to_set->table,
1453 *from_var_slot);
1454#endif /* CONFIG_WITH_STRCACHE2 */
1455 if (HASH_VACANT (*to_var_slot))
1456 hash_insert_at (&to_set->table, from_var, to_var_slot);
1457 else
1458 {
1459 /* GKM FIXME: delete in from_set->table */
1460#ifdef KMK
1461 if (from_var->aliased)
1462 fatal(NULL, ("Attempting to delete aliased variable '%s'"), from_var->name);
1463 if (from_var->alias)
1464 fatal(NULL, ("Attempting to delete variable aliased '%s'"), from_var->name);
1465#endif
1466#ifdef CONFIG_WITH_COMPILER
1467 if (from_var->evalprog || from_var->expandprog)
1468 kmk_cc_variable_deleted (from_var);
1469#endif
1470#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1471 if (!from_var->rdonly_val)
1472#endif
1473 free (from_var->value);
1474 free (from_var);
1475 }
1476 }
1477}
1478
1479/* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
1480
1481void
1482merge_variable_set_lists (struct variable_set_list **setlist0,
1483 struct variable_set_list *setlist1)
1484{
1485 struct variable_set_list *to = *setlist0;
1486 struct variable_set_list *last0 = 0;
1487
1488 /* If there's nothing to merge, stop now. */
1489 if (!setlist1)
1490 return;
1491
1492 /* This loop relies on the fact that all setlists terminate with the global
1493 setlist (before NULL). If that's not true, arguably we SHOULD die. */
1494 if (to)
1495 while (setlist1 != &global_setlist && to != &global_setlist)
1496 {
1497 struct variable_set_list *from = setlist1;
1498 setlist1 = setlist1->next;
1499
1500 merge_variable_sets (to->set, from->set);
1501
1502 last0 = to;
1503 to = to->next;
1504 }
1505
1506 if (setlist1 != &global_setlist)
1507 {
1508 if (last0 == 0)
1509 *setlist0 = setlist1;
1510 else
1511 last0->next = setlist1;
1512 }
1513}
1514
1515
1516#if defined(KMK) && !defined(WINDOWS32)
1517/* Parses out the next number from the uname release level string. Fast
1518 forwards to the end of the string when encountering some non-conforming
1519 chars. */
1520
1521static unsigned long parse_release_number (const char **ppsz)
1522{
1523 unsigned long ul;
1524 char *psz = (char *)*ppsz;
1525 if (ISDIGIT (*psz))
1526 {
1527 ul = strtoul (psz, &psz, 10);
1528 if (psz != NULL && *psz == '.')
1529 psz++;
1530 else
1531 psz = strchr (*ppsz, '\0');
1532 *ppsz = psz;
1533 }
1534 else
1535 ul = 0;
1536 return ul;
1537}
1538#endif
1539
1540
1541/* Define the automatic variables, and record the addresses
1542 of their structures so we can change their values quickly. */
1543
1544void
1545define_automatic_variables (void)
1546{
1547#if defined(WINDOWS32) || defined(__EMX__)
1548 extern char* default_shell;
1549#else
1550 extern char default_shell[];
1551#endif
1552 register struct variable *v;
1553#ifndef KMK
1554 char buf[200];
1555#else
1556 char buf[1024];
1557 const char *val;
1558 struct variable *envvar1;
1559 struct variable *envvar2;
1560# ifdef WINDOWS32
1561 OSVERSIONINFOEX oix;
1562# else
1563 struct utsname uts;
1564# endif
1565 unsigned long ulMajor = 0, ulMinor = 0, ulPatch = 0, ul4th = 0;
1566#endif
1567
1568 sprintf (buf, "%u", makelevel);
1569 define_variable_cname (MAKELEVEL_NAME, buf, o_env, 0);
1570
1571 sprintf (buf, "%s%s%s",
1572 version_string,
1573 (remote_description == 0 || remote_description[0] == '\0')
1574 ? "" : "-",
1575 (remote_description == 0 || remote_description[0] == '\0')
1576 ? "" : remote_description);
1577#ifndef KMK
1578 define_variable_cname ("MAKE_VERSION", buf, o_default, 0);
1579#else /* KMK */
1580
1581 /* Define KMK_VERSION to indicate kMk. */
1582 define_variable_cname ("KMK_VERSION", buf, o_default, 0);
1583
1584 /* Define KBUILD_VERSION* */
1585 sprintf (buf, "%d", KBUILD_VERSION_MAJOR);
1586 define_variable_cname ("KBUILD_VERSION_MAJOR", buf, o_default, 0);
1587 sprintf (buf, "%d", KBUILD_VERSION_MINOR);
1588 define_variable_cname ("KBUILD_VERSION_MINOR", buf, o_default, 0);
1589 sprintf (buf, "%d", KBUILD_VERSION_PATCH);
1590 define_variable_cname ("KBUILD_VERSION_PATCH", buf, o_default, 0);
1591 sprintf (buf, "%d", KBUILD_SVN_REV);
1592 define_variable_cname ("KBUILD_KMK_REVISION", buf, o_default, 0);
1593
1594 sprintf (buf, "%d.%d.%d-r%d", KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR,
1595 KBUILD_VERSION_PATCH, KBUILD_SVN_REV);
1596 define_variable_cname ("KBUILD_VERSION", buf, o_default, 0);
1597
1598 /* The host defaults. The BUILD_* stuff will be replaced by KBUILD_* soon. */
1599 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST"));
1600 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM"));
1601 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST;
1602 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1603 error (NULL, _("KBUILD_HOST and BUILD_PLATFORM differs, using KBUILD_HOST=%s."), val);
1604 if (!envvar1)
1605 define_variable_cname ("KBUILD_HOST", val, o_default, 0);
1606 if (!envvar2)
1607 define_variable_cname ("BUILD_PLATFORM", val, o_default, 0);
1608
1609 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_ARCH"));
1610 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_ARCH"));
1611 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_ARCH;
1612 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1613 error (NULL, _("KBUILD_HOST_ARCH and BUILD_PLATFORM_ARCH differs, using KBUILD_HOST_ARCH=%s."), val);
1614 if (!envvar1)
1615 define_variable_cname ("KBUILD_HOST_ARCH", val, o_default, 0);
1616 if (!envvar2)
1617 define_variable_cname ("BUILD_PLATFORM_ARCH", val, o_default, 0);
1618
1619 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_CPU"));
1620 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_CPU"));
1621 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_CPU;
1622 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1623 error (NULL, _("KBUILD_HOST_CPU and BUILD_PLATFORM_CPU differs, using KBUILD_HOST_CPU=%s."), val);
1624 if (!envvar1)
1625 define_variable_cname ("KBUILD_HOST_CPU", val, o_default, 0);
1626 if (!envvar2)
1627 define_variable_cname ("BUILD_PLATFORM_CPU", val, o_default, 0);
1628
1629 /* The host kernel version. */
1630#if defined(WINDOWS32)
1631 memset (&oix, '\0', sizeof (oix));
1632 oix.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1633 if (!GetVersionEx ((LPOSVERSIONINFO)&oix))
1634 {
1635 memset (&oix, '\0', sizeof (oix));
1636 oix.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
1637 GetVersionEx ((LPOSVERSIONINFO)&oix);
1638 }
1639 if (oix.dwPlatformId == VER_PLATFORM_WIN32_NT)
1640 {
1641 ulMajor = oix.dwMajorVersion;
1642 ulMinor = oix.dwMinorVersion;
1643 ulPatch = oix.wServicePackMajor;
1644 ul4th = oix.wServicePackMinor;
1645 }
1646 else
1647 {
1648 ulMajor = oix.dwPlatformId == 1 ? 0 /*Win95/98/ME*/
1649 : oix.dwPlatformId == 3 ? 1 /*WinCE*/
1650 : 2; /*??*/
1651 ulMinor = oix.dwMajorVersion;
1652 ulPatch = oix.dwMinorVersion;
1653 ul4th = oix.wServicePackMajor;
1654 }
1655#else
1656 memset (&uts, 0, sizeof(uts));
1657 uname (&uts);
1658 val = uts.release;
1659 ulMajor = parse_release_number (&val);
1660 ulMinor = parse_release_number (&val);
1661 ulPatch = parse_release_number (&val);
1662 ul4th = parse_release_number (&val);
1663#endif
1664
1665 sprintf (buf, "%lu.%lu.%lu.%lu", ulMajor, ulMinor, ulPatch, ul4th);
1666 define_variable_cname ("KBUILD_HOST_VERSION", buf, o_default, 0);
1667
1668 sprintf (buf, "%lu", ulMajor);
1669 define_variable_cname ("KBUILD_HOST_VERSION_MAJOR", buf, o_default, 0);
1670
1671 sprintf (buf, "%lu", ulMinor);
1672 define_variable_cname ("KBUILD_HOST_VERSION_MINOR", buf, o_default, 0);
1673
1674 sprintf (buf, "%lu", ulPatch);
1675 define_variable_cname ("KBUILD_HOST_VERSION_PATCH", buf, o_default, 0);
1676
1677 /* The kBuild locations. */
1678 define_variable_cname ("KBUILD_PATH", get_kbuild_path (), o_default, 0);
1679 define_variable_cname ("KBUILD_BIN_PATH", get_kbuild_bin_path (), o_default, 0);
1680
1681 define_variable_cname ("PATH_KBUILD", get_kbuild_path (), o_default, 0);
1682 define_variable_cname ("PATH_KBUILD_BIN", get_kbuild_bin_path (), o_default, 0);
1683
1684 /* Define KMK_FEATURES to indicate various working KMK features. */
1685# if defined (CONFIG_WITH_RSORT) \
1686 && defined (CONFIG_WITH_ABSPATHEX) \
1687 && defined (CONFIG_WITH_TOUPPER_TOLOWER) \
1688 && defined (CONFIG_WITH_DEFINED) \
1689 && defined (CONFIG_WITH_VALUE_LENGTH) \
1690 && defined (CONFIG_WITH_COMPARE) \
1691 && defined (CONFIG_WITH_STACK) \
1692 && defined (CONFIG_WITH_MATH) \
1693 && defined (CONFIG_WITH_XARGS) \
1694 && defined (CONFIG_WITH_EXPLICIT_MULTITARGET) \
1695 && defined (CONFIG_WITH_DOT_MUST_MAKE) \
1696 && defined (CONFIG_WITH_PREPEND_ASSIGNMENT) \
1697 && defined (CONFIG_WITH_SET_CONDITIONALS) \
1698 && defined (CONFIG_WITH_DATE) \
1699 && defined (CONFIG_WITH_FILE_SIZE) \
1700 && defined (CONFIG_WITH_WHERE_FUNCTION) \
1701 && defined (CONFIG_WITH_WHICH) \
1702 && defined (CONFIG_WITH_EVALPLUS) \
1703 && (defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)) \
1704 && defined (CONFIG_WITH_COMMANDS_FUNC) \
1705 && defined (CONFIG_WITH_PRINTF) \
1706 && defined (CONFIG_WITH_LOOP_FUNCTIONS) \
1707 && defined (CONFIG_WITH_ROOT_FUNC) \
1708 && defined (CONFIG_WITH_STRING_FUNCTIONS) \
1709 && defined (CONFIG_WITH_DEFINED_FUNCTIONS) \
1710 && defined (KMK_HELPERS)
1711 define_variable_cname ("KMK_FEATURES",
1712 "append-dash-n abspath includedep-queue install-hard-linking umask"
1713 " kBuild-define"
1714 " rsort"
1715 " abspathex"
1716 " toupper tolower"
1717 " defined"
1718 " comp-vars comp-cmds comp-cmds-ex"
1719 " stack"
1720 " math-int"
1721 " xargs"
1722 " explicit-multitarget"
1723 " dot-must-make"
1724 " prepend-assignment"
1725 " set-conditionals intersects"
1726 " date"
1727 " file-size"
1728 " expr if-expr select"
1729 " where"
1730 " which"
1731 " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var"
1732 " make-stats"
1733 " commands"
1734 " printf"
1735 " for while"
1736 " root"
1737 " length insert pos lastpos substr translate"
1738 " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one kb-exp-tmpl"
1739 " firstdefined lastdefined"
1740 , o_default, 0);
1741# else /* MSC can't deal with strings mixed with #if/#endif, thus the slow way. */
1742# error "All features should be enabled by default!"
1743 strcpy (buf, "append-dash-n abspath includedep-queue install-hard-linking umask"
1744 " kBuild-define");
1745# if defined (CONFIG_WITH_RSORT)
1746 strcat (buf, " rsort");
1747# endif
1748# if defined (CONFIG_WITH_ABSPATHEX)
1749 strcat (buf, " abspathex");
1750# endif
1751# if defined (CONFIG_WITH_TOUPPER_TOLOWER)
1752 strcat (buf, " toupper tolower");
1753# endif
1754# if defined (CONFIG_WITH_DEFINED)
1755 strcat (buf, " defined");
1756# endif
1757# if defined (CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
1758 strcat (buf, " comp-vars comp-cmds comp-cmds-ex");
1759# endif
1760# if defined (CONFIG_WITH_STACK)
1761 strcat (buf, " stack");
1762# endif
1763# if defined (CONFIG_WITH_MATH)
1764 strcat (buf, " math-int");
1765# endif
1766# if defined (CONFIG_WITH_XARGS)
1767 strcat (buf, " xargs");
1768# endif
1769# if defined (CONFIG_WITH_EXPLICIT_MULTITARGET)
1770 strcat (buf, " explicit-multitarget");
1771# endif
1772# if defined (CONFIG_WITH_DOT_MUST_MAKE)
1773 strcat (buf, " dot-must-make");
1774# endif
1775# if defined (CONFIG_WITH_PREPEND_ASSIGNMENT)
1776 strcat (buf, " prepend-assignment");
1777# endif
1778# if defined (CONFIG_WITH_SET_CONDITIONALS)
1779 strcat (buf, " set-conditionals intersects");
1780# endif
1781# if defined (CONFIG_WITH_DATE)
1782 strcat (buf, " date");
1783# endif
1784# if defined (CONFIG_WITH_FILE_SIZE)
1785 strcat (buf, " file-size");
1786# endif
1787# if defined (CONFIG_WITH_IF_CONDITIONALS)
1788 strcat (buf, " expr if-expr select");
1789# endif
1790# if defined (CONFIG_WITH_WHERE_FUNCTION)
1791 strcat (buf, " where");
1792# endif
1793# if defined (CONFIG_WITH_WHICH)
1794 strcat (buf, " which");
1795# endif
1796# if defined (CONFIG_WITH_EVALPLUS)
1797 strcat (buf, " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var");
1798# endif
1799# if defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)
1800 strcat (buf, " make-stats");
1801# endif
1802# if defined (CONFIG_WITH_COMMANDS_FUNC)
1803 strcat (buf, " commands");
1804# endif
1805# if defined (CONFIG_WITH_PRINTF)
1806 strcat (buf, " printf");
1807# endif
1808# if defined (CONFIG_WITH_LOOP_FUNCTIONS)
1809 strcat (buf, " for while");
1810# endif
1811# if defined (CONFIG_WITH_ROOT_FUNC)
1812 strcat (buf, " root");
1813# endif
1814# if defined (CONFIG_WITH_STRING_FUNCTIONS)
1815 strcat (buf, " length insert pos lastpos substr translate");
1816# endif
1817# if defined (CONFIG_WITH_DEFINED_FUNCTIONS)
1818 strcat (buf, " firstdefined lastdefined");
1819# endif
1820# if defined (KMK_HELPERS)
1821 strcat (buf, " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one kb-exp-tmpl");
1822# endif
1823 define_variable_cname ("KMK_FEATURES", buf, o_default, 0);
1824# endif
1825
1826#endif /* KMK */
1827
1828#ifdef CONFIG_WITH_KMK_BUILTIN
1829 /* The supported kMk Builtin commands. */
1830 define_variable_cname ("KMK_BUILTIN", "append cat chmod cp cmp echo expr install kDepIDB ln md5sum mkdir mv printf rm rmdir sleep test", o_default, 0);
1831#endif
1832
1833#ifdef __MSDOS__
1834 /* Allow to specify a special shell just for Make,
1835 and use $COMSPEC as the default $SHELL when appropriate. */
1836 {
1837 static char shell_str[] = "SHELL";
1838 const int shlen = sizeof (shell_str) - 1;
1839 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
1840 struct variable *comp = lookup_variable ("COMSPEC", 7);
1841
1842 /* $(MAKESHELL) overrides $(SHELL) even if -e is in effect. */
1843 if (mshp)
1844 (void) define_variable (shell_str, shlen,
1845 mshp->value, o_env_override, 0);
1846 else if (comp)
1847 {
1848 /* $(COMSPEC) shouldn't override $(SHELL). */
1849 struct variable *shp = lookup_variable (shell_str, shlen);
1850
1851 if (!shp)
1852 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
1853 }
1854 }
1855#elif defined(__EMX__)
1856 {
1857 static char shell_str[] = "SHELL";
1858 const int shlen = sizeof (shell_str) - 1;
1859 struct variable *shell = lookup_variable (shell_str, shlen);
1860 struct variable *replace = lookup_variable ("MAKESHELL", 9);
1861
1862 /* if $MAKESHELL is defined in the environment assume o_env_override */
1863 if (replace && *replace->value && replace->origin == o_env)
1864 replace->origin = o_env_override;
1865
1866 /* if $MAKESHELL is not defined use $SHELL but only if the variable
1867 did not come from the environment */
1868 if (!replace || !*replace->value)
1869 if (shell && *shell->value && (shell->origin == o_env
1870 || shell->origin == o_env_override))
1871 {
1872 /* overwrite whatever we got from the environment */
1873 free(shell->value);
1874 shell->value = xstrdup (default_shell);
1875 shell->origin = o_default;
1876 }
1877
1878 /* Some people do not like cmd to be used as the default
1879 if $SHELL is not defined in the Makefile.
1880 With -DNO_CMD_DEFAULT you can turn off this behaviour */
1881# ifndef NO_CMD_DEFAULT
1882 /* otherwise use $COMSPEC */
1883 if (!replace || !*replace->value)
1884 replace = lookup_variable ("COMSPEC", 7);
1885
1886 /* otherwise use $OS2_SHELL */
1887 if (!replace || !*replace->value)
1888 replace = lookup_variable ("OS2_SHELL", 9);
1889# else
1890# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
1891# endif
1892
1893 if (replace && *replace->value)
1894 /* overwrite $SHELL */
1895 (void) define_variable (shell_str, shlen, replace->value,
1896 replace->origin, 0);
1897 else
1898 /* provide a definition if there is none */
1899 (void) define_variable (shell_str, shlen, default_shell,
1900 o_default, 0);
1901 }
1902
1903#endif
1904
1905 /* This won't override any definition, but it will provide one if there
1906 isn't one there. */
1907 v = define_variable_cname ("SHELL", default_shell, o_default, 0);
1908#ifdef __MSDOS__
1909 v->export = v_export; /* Export always SHELL. */
1910#endif
1911
1912 /* On MSDOS we do use SHELL from environment, since it isn't a standard
1913 environment variable on MSDOS, so whoever sets it, does that on purpose.
1914 On OS/2 we do not use SHELL from environment but we have already handled
1915 that problem above. */
1916#if !defined(__MSDOS__) && !defined(__EMX__)
1917 /* Don't let SHELL come from the environment. */
1918 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
1919 {
1920# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1921 if (v->rdonly_val)
1922 v->rdonly_val = 0;
1923 else
1924# endif
1925 free (v->value);
1926 v->origin = o_file;
1927 v->value = xstrdup (default_shell);
1928# ifdef CONFIG_WITH_VALUE_LENGTH
1929 v->value_length = strlen (v->value);
1930 v->value_alloc_len = v->value_length + 1;
1931# endif
1932 }
1933#endif
1934
1935 /* Make sure MAKEFILES gets exported if it is set. */
1936 v = define_variable_cname ("MAKEFILES", "", o_default, 0);
1937 v->export = v_ifset;
1938
1939 /* Define the magic D and F variables in terms of
1940 the automatic variables they are variations of. */
1941
1942#ifdef VMS
1943 define_variable_cname ("@D", "$(dir $@)", o_automatic, 1);
1944 define_variable_cname ("%D", "$(dir $%)", o_automatic, 1);
1945 define_variable_cname ("*D", "$(dir $*)", o_automatic, 1);
1946 define_variable_cname ("<D", "$(dir $<)", o_automatic, 1);
1947 define_variable_cname ("?D", "$(dir $?)", o_automatic, 1);
1948 define_variable_cname ("^D", "$(dir $^)", o_automatic, 1);
1949 define_variable_cname ("+D", "$(dir $+)", o_automatic, 1);
1950#else
1951 define_variable_cname ("@D", "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
1952 define_variable_cname ("%D", "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
1953 define_variable_cname ("*D", "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
1954 define_variable_cname ("<D", "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
1955 define_variable_cname ("?D", "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
1956 define_variable_cname ("^D", "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
1957 define_variable_cname ("+D", "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
1958#endif
1959 define_variable_cname ("@F", "$(notdir $@)", o_automatic, 1);
1960 define_variable_cname ("%F", "$(notdir $%)", o_automatic, 1);
1961 define_variable_cname ("*F", "$(notdir $*)", o_automatic, 1);
1962 define_variable_cname ("<F", "$(notdir $<)", o_automatic, 1);
1963 define_variable_cname ("?F", "$(notdir $?)", o_automatic, 1);
1964 define_variable_cname ("^F", "$(notdir $^)", o_automatic, 1);
1965 define_variable_cname ("+F", "$(notdir $+)", o_automatic, 1);
1966#ifdef CONFIG_WITH_LAZY_DEPS_VARS
1967 define_variable ("^", 1, "$(deps $@)", o_automatic, 1);
1968 define_variable ("+", 1, "$(deps-all $@)", o_automatic, 1);
1969 define_variable ("?", 1, "$(deps-newer $@)", o_automatic, 1);
1970 define_variable ("|", 1, "$(deps-oo $@)", o_automatic, 1);
1971#endif /* CONFIG_WITH_LAZY_DEPS_VARS */
1972}
1973
1974
1975int export_all_variables;
1976
1977/* Create a new environment for FILE's commands.
1978 If FILE is nil, this is for the `shell' function.
1979 The child's MAKELEVEL variable is incremented. */
1980
1981char **
1982target_environment (struct file *file)
1983{
1984 struct variable_set_list *set_list;
1985 register struct variable_set_list *s;
1986 struct hash_table table;
1987 struct variable **v_slot;
1988 struct variable **v_end;
1989 struct variable makelevel_key;
1990 char **result_0;
1991 char **result;
1992#ifdef CONFIG_WITH_STRCACHE2
1993 const char *cached_name;
1994#endif
1995
1996 if (file == 0)
1997 set_list = current_variable_set_list;
1998 else
1999 set_list = file->variables;
2000
2001#ifndef CONFIG_WITH_STRCACHE2
2002 hash_init (&table, VARIABLE_BUCKETS,
2003 variable_hash_1, variable_hash_2, variable_hash_cmp);
2004#else /* CONFIG_WITH_STRCACHE2 */
2005 hash_init_strcached (&table, VARIABLE_BUCKETS,
2006 &variable_strcache, offsetof (struct variable, name));
2007#endif /* CONFIG_WITH_STRCACHE2 */
2008
2009 /* Run through all the variable sets in the list,
2010 accumulating variables in TABLE. */
2011 for (s = set_list; s != 0; s = s->next)
2012 {
2013 struct variable_set *set = s->set;
2014 v_slot = (struct variable **) set->table.ht_vec;
2015 v_end = v_slot + set->table.ht_size;
2016 for ( ; v_slot < v_end; v_slot++)
2017 if (! HASH_VACANT (*v_slot))
2018 {
2019 struct variable **new_slot;
2020 struct variable *v = *v_slot;
2021
2022 /* If this is a per-target variable and it hasn't been touched
2023 already then look up the global version and take its export
2024 value. */
2025 if (v->per_target && v->export == v_default)
2026 {
2027 struct variable *gv;
2028
2029#ifndef CONFIG_WITH_VALUE_LENGTH
2030 gv = lookup_variable_in_set (v->name, strlen(v->name),
2031 &global_variable_set);
2032#else
2033 assert ((int)strlen(v->name) == v->length);
2034 gv = lookup_variable_in_set (v->name, v->length,
2035 &global_variable_set);
2036#endif
2037 if (gv)
2038 v->export = gv->export;
2039 }
2040
2041 switch (v->export)
2042 {
2043 case v_default:
2044 if (v->origin == o_default || v->origin == o_automatic)
2045 /* Only export default variables by explicit request. */
2046 continue;
2047
2048 /* The variable doesn't have a name that can be exported. */
2049 if (! v->exportable)
2050 continue;
2051
2052 if (! export_all_variables
2053 && v->origin != o_command
2054 && v->origin != o_env && v->origin != o_env_override)
2055 continue;
2056 break;
2057
2058 case v_export:
2059 break;
2060
2061 case v_noexport:
2062 {
2063 /* If this is the SHELL variable and it's not exported,
2064 then add the value from our original environment, if
2065 the original environment defined a value for SHELL. */
2066 extern struct variable shell_var;
2067 if (streq (v->name, "SHELL") && shell_var.value)
2068 {
2069 v = &shell_var;
2070 break;
2071 }
2072 continue;
2073 }
2074
2075 case v_ifset:
2076 if (v->origin == o_default)
2077 continue;
2078 break;
2079 }
2080
2081#ifndef CONFIG_WITH_STRCACHE2
2082 new_slot = (struct variable **) hash_find_slot (&table, v);
2083#else /* CONFIG_WITH_STRCACHE2 */
2084 assert (strcache2_is_cached (&variable_strcache, v->name));
2085 new_slot = (struct variable **) hash_find_slot_strcached (&table, v);
2086#endif /* CONFIG_WITH_STRCACHE2 */
2087 if (HASH_VACANT (*new_slot))
2088 hash_insert_at (&table, v, new_slot);
2089 }
2090 }
2091
2092#ifndef CONFIG_WITH_STRCACHE2
2093 makelevel_key.name = MAKELEVEL_NAME;
2094 makelevel_key.length = MAKELEVEL_LENGTH;
2095 hash_delete (&table, &makelevel_key);
2096#else /* CONFIG_WITH_STRCACHE2 */
2097 /* lookup the name in the string case, if it's not there it won't
2098 be in any of the sets either. */
2099 cached_name = strcache2_lookup (&variable_strcache,
2100 MAKELEVEL_NAME, MAKELEVEL_LENGTH);
2101 if (cached_name)
2102 {
2103 makelevel_key.name = cached_name;
2104 makelevel_key.length = MAKELEVEL_LENGTH;
2105 hash_delete_strcached (&table, &makelevel_key);
2106 }
2107#endif /* CONFIG_WITH_STRCACHE2 */
2108
2109 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
2110
2111 v_slot = (struct variable **) table.ht_vec;
2112 v_end = v_slot + table.ht_size;
2113 for ( ; v_slot < v_end; v_slot++)
2114 if (! HASH_VACANT (*v_slot))
2115 {
2116 struct variable *v = *v_slot;
2117
2118 /* If V is recursively expanded and didn't come from the environment,
2119 expand its value. If it came from the environment, it should
2120 go back into the environment unchanged. */
2121 if (v->recursive
2122 && v->origin != o_env && v->origin != o_env_override)
2123 {
2124#ifndef CONFIG_WITH_VALUE_LENGTH
2125 char *value = recursively_expand_for_file (v, file);
2126#else
2127 char *value = recursively_expand_for_file (v, file, NULL);
2128#endif
2129#ifdef WINDOWS32
2130 if (strcmp(v->name, "Path") == 0 ||
2131 strcmp(v->name, "PATH") == 0)
2132 convert_Path_to_windows32(value, ';');
2133#endif
2134 *result++ = xstrdup (concat (3, v->name, "=", value));
2135 free (value);
2136 }
2137 else
2138 {
2139#ifdef WINDOWS32
2140 if (strcmp(v->name, "Path") == 0 ||
2141 strcmp(v->name, "PATH") == 0)
2142 convert_Path_to_windows32(v->value, ';');
2143#endif
2144 *result++ = xstrdup (concat (3, v->name, "=", v->value));
2145 }
2146 }
2147
2148 *result = xmalloc (100);
2149 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
2150 *++result = 0;
2151
2152 hash_free (&table, 0);
2153
2154 return result_0;
2155}
2156
2157
2158#ifdef CONFIG_WITH_VALUE_LENGTH
2159/* Worker function for do_variable_definition_append() and
2160 append_expanded_string_to_variable().
2161 The APPEND argument indicates whether it's an append or prepend operation. */
2162void append_string_to_variable (struct variable *v, const char *value, unsigned int value_len, int append)
2163{
2164 /* The previous definition of the variable was recursive.
2165 The new value is the unexpanded old and new values. */
2166 unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
2167 int done_1st_prepend_copy = 0;
2168#ifdef KMK
2169 assert (!v->alias);
2170#endif
2171
2172 /* Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
2173 if (!value_len)
2174 return;
2175
2176 /* adjust the size. */
2177 if (v->value_alloc_len <= new_value_len + 1)
2178 {
2179 if (v->value_alloc_len < 256)
2180 v->value_alloc_len = 256;
2181 else
2182 v->value_alloc_len *= 2;
2183 if (v->value_alloc_len < new_value_len + 1)
2184 v->value_alloc_len = VAR_ALIGN_VALUE_ALLOC (new_value_len + 1 + value_len /*future*/ );
2185# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2186 if ((append || !v->value_length) && !v->rdonly_val)
2187# else
2188 if (append || !v->value_length)
2189# endif
2190 v->value = xrealloc (v->value, v->value_alloc_len);
2191 else
2192 {
2193 /* avoid the extra memcpy the xrealloc may have to do */
2194 char *new_buf = xmalloc (v->value_alloc_len);
2195 memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
2196 done_1st_prepend_copy = 1;
2197# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2198 if (v->rdonly_val)
2199 v->rdonly_val = 0;
2200 else
2201# endif
2202 free (v->value);
2203 v->value = new_buf;
2204 }
2205 MAKE_STATS_2(v->reallocs++);
2206 }
2207
2208 /* insert the new bits */
2209 if (v->value_length != 0)
2210 {
2211 if (append)
2212 {
2213 v->value[v->value_length] = ' ';
2214 memcpy (&v->value[v->value_length + 1], value, value_len + 1);
2215 }
2216 else
2217 {
2218 if (!done_1st_prepend_copy)
2219 memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
2220 v->value[value_len] = ' ';
2221 memcpy (v->value, value, value_len);
2222 }
2223 }
2224 else
2225 memcpy (v->value, value, value_len + 1);
2226 v->value_length = new_value_len;
2227}
2228
2229struct variable *
2230do_variable_definition_append (const struct floc *flocp, struct variable *v,
2231 const char *value, unsigned int value_len,
2232 int simple_value, enum variable_origin origin,
2233 int append)
2234{
2235 if (env_overrides && origin == o_env)
2236 origin = o_env_override;
2237
2238 if (env_overrides && v->origin == o_env)
2239 /* V came from in the environment. Since it was defined
2240 before the switches were parsed, it wasn't affected by -e. */
2241 v->origin = o_env_override;
2242
2243 /* A variable of this name is already defined.
2244 If the old definition is from a stronger source
2245 than this one, don't redefine it. */
2246 if ((int) origin < (int) v->origin)
2247 return v;
2248 v->origin = origin;
2249
2250 /* location */
2251 if (flocp != 0)
2252 v->fileinfo = *flocp;
2253
2254 /* The juicy bits, append the specified value to the variable
2255 This is a heavily exercised code path in kBuild. */
2256 if (value_len == ~0U)
2257 value_len = strlen (value);
2258 if (v->recursive || simple_value)
2259 append_string_to_variable (v, value, value_len, append);
2260 else
2261 /* The previous definition of the variable was simple.
2262 The new value comes from the old value, which was expanded
2263 when it was set; and from the expanded new value. */
2264 append_expanded_string_to_variable (v, value, value_len, append);
2265
2266 /* update the variable */
2267 return v;
2268}
2269#endif /* CONFIG_WITH_VALUE_LENGTH */
2270
2271
2272static struct variable *
2273set_special_var (struct variable *var)
2274{
2275 if (streq (var->name, RECIPEPREFIX_NAME))
2276 {
2277 /* The user is resetting the command introduction prefix. This has to
2278 happen immediately, so that subsequent rules are interpreted
2279 properly. */
2280 cmd_prefix = var->value[0]=='\0' ? RECIPEPREFIX_DEFAULT : var->value[0];
2281 }
2282
2283 return var;
2284}
2285
2286
2287/* Given a variable, a value, and a flavor, define the variable.
2288 See the try_variable_definition() function for details on the parameters. */
2289
2290struct variable *
2291#ifndef CONFIG_WITH_VALUE_LENGTH
2292do_variable_definition (const struct floc *flocp, const char *varname,
2293 const char *value, enum variable_origin origin,
2294 enum variable_flavor flavor, int target_var)
2295#else /* CONFIG_WITH_VALUE_LENGTH */
2296do_variable_definition_2 (const struct floc *flocp,
2297 const char *varname, const char *value,
2298 unsigned int value_len, int simple_value,
2299 char *free_value,
2300 enum variable_origin origin,
2301 enum variable_flavor flavor,
2302 int target_var)
2303#endif /* CONFIG_WITH_VALUE_LENGTH */
2304{
2305 const char *p;
2306 char *alloc_value = NULL;
2307 struct variable *v;
2308 int append = 0;
2309 int conditional = 0;
2310 const size_t varname_len = strlen (varname); /* bird */
2311
2312#ifdef CONFIG_WITH_VALUE_LENGTH
2313 if (value_len == ~0U)
2314 value_len = strlen (value);
2315 else
2316 assert (value_len == strlen (value));
2317#endif
2318
2319 /* Calculate the variable's new value in VALUE. */
2320
2321 switch (flavor)
2322 {
2323 default:
2324 case f_bogus:
2325 /* Should not be possible. */
2326 abort ();
2327 case f_simple:
2328 /* A simple variable definition "var := value". Expand the value.
2329 We have to allocate memory since otherwise it'll clobber the
2330 variable buffer, and we may still need that if we're looking at a
2331 target-specific variable. */
2332#ifndef CONFIG_WITH_VALUE_LENGTH
2333 p = alloc_value = allocated_variable_expand (value);
2334#else /* CONFIG_WITH_VALUE_LENGTH */
2335 if (!simple_value)
2336 p = alloc_value = allocated_variable_expand_2 (value, value_len, &value_len);
2337 else
2338 {
2339 if (value_len == ~0U)
2340 value_len = strlen (value);
2341 if (!free_value)
2342 p = alloc_value = xstrndup (value, value_len);
2343 else
2344 {
2345 assert (value == free_value);
2346 p = alloc_value = free_value;
2347 free_value = 0;
2348 }
2349 }
2350#endif /* CONFIG_WITH_VALUE_LENGTH */
2351 break;
2352 case f_conditional:
2353 /* A conditional variable definition "var ?= value".
2354 The value is set IFF the variable is not defined yet. */
2355 v = lookup_variable (varname, varname_len);
2356 if (v)
2357#ifndef CONFIG_WITH_VALUE_LENGTH
2358 return v->special ? set_special_var (v) : v;
2359#else /* CONFIG_WITH_VALUE_LENGTH */
2360 {
2361 if (free_value)
2362 free (free_value);
2363 return v->special ? set_special_var (v) : v;
2364 }
2365#endif /* CONFIG_WITH_VALUE_LENGTH */
2366
2367 conditional = 1;
2368 flavor = f_recursive;
2369 /* FALLTHROUGH */
2370 case f_recursive:
2371 /* A recursive variable definition "var = value".
2372 The value is used verbatim. */
2373 p = value;
2374 break;
2375#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2376 case f_append:
2377 case f_prepend:
2378 {
2379 const enum variable_flavor org_flavor = flavor;
2380#else
2381 case f_append:
2382 {
2383#endif
2384
2385 /* If we have += but we're in a target variable context, we want to
2386 append only with other variables in the context of this target. */
2387 if (target_var)
2388 {
2389 append = 1;
2390 v = lookup_variable_in_set (varname, varname_len,
2391 current_variable_set_list->set);
2392
2393 /* Don't append from the global set if a previous non-appending
2394 target-specific variable definition exists. */
2395 if (v && !v->append)
2396 append = 0;
2397 }
2398#ifdef KMK
2399 else if ( g_pTopKbEvalData
2400 || ( varname_len > 3
2401 && varname[0] == '['
2402 && is_kbuild_object_variable_accessor (varname, varname_len)) )
2403 {
2404 v = kbuild_object_variable_pre_append (varname, varname_len,
2405 value, value_len, simple_value,
2406 origin, org_flavor == f_append, flocp);
2407 if (free_value)
2408 free (free_value);
2409 return v;
2410 }
2411#endif
2412#ifdef CONFIG_WITH_LOCAL_VARIABLES
2413 /* If 'local', restrict it to the current variable context. */
2414 else if (origin == o_local)
2415 v = lookup_variable_in_set (varname, varname_len,
2416 current_variable_set_list->set);
2417#endif
2418 else
2419 v = lookup_variable (varname, varname_len);
2420
2421 if (v == 0)
2422 {
2423 /* There was no old value.
2424 This becomes a normal recursive definition. */
2425 p = value;
2426 flavor = f_recursive;
2427 }
2428 else
2429 {
2430#ifdef CONFIG_WITH_VALUE_LENGTH
2431 v->append = append;
2432 v = do_variable_definition_append (flocp, v, value, value_len,
2433 simple_value, origin,
2434# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2435 org_flavor == f_append);
2436# else
2437 1);
2438# endif
2439 if (free_value)
2440 free (free_value);
2441 MAKE_STATS_2(v->changes++);
2442# ifdef CONFIG_WITH_COMPILER
2443 if (v->evalprog || v->expandprog)
2444 kmk_cc_variable_changed (v);
2445# endif
2446 return v;
2447#else /* !CONFIG_WITH_VALUE_LENGTH */
2448
2449 /* Paste the old and new values together in VALUE. */
2450
2451 unsigned int oldlen, vallen;
2452 const char *val;
2453 char *tp = NULL;
2454
2455 val = value;
2456 if (v->recursive)
2457 /* The previous definition of the variable was recursive.
2458 The new value is the unexpanded old and new values. */
2459 flavor = f_recursive;
2460 else
2461 /* The previous definition of the variable was simple.
2462 The new value comes from the old value, which was expanded
2463 when it was set; and from the expanded new value. Allocate
2464 memory for the expansion as we may still need the rest of the
2465 buffer if we're looking at a target-specific variable. */
2466 val = tp = allocated_variable_expand (val);
2467
2468 oldlen = strlen (v->value);
2469 vallen = strlen (val);
2470 p = alloc_value = xmalloc (oldlen + 1 + vallen + 1);
2471# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2472 if (org_flavor == f_prepend)
2473 {
2474 memcpy (alloc_value, val, vallen);
2475 alloc_value[oldlen] = ' ';
2476 memcpy (&alloc_value[oldlen + 1], v->value, oldlen + 1);
2477 }
2478 else
2479# endif /* CONFIG_WITH_PREPEND_ASSIGNMENT */
2480 {
2481 memcpy (alloc_value, v->value, oldlen);
2482 alloc_value[oldlen] = ' ';
2483 memcpy (&alloc_value[oldlen + 1], val, vallen + 1);
2484 }
2485
2486 if (tp)
2487 free (tp);
2488#endif /* !CONFIG_WITH_VALUE_LENGTH */
2489 }
2490 }
2491 }
2492
2493#ifdef __MSDOS__
2494 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
2495 non-Unix systems don't conform to this default configuration (in
2496 fact, most of them don't even have `/bin'). On the other hand,
2497 $SHELL in the environment, if set, points to the real pathname of
2498 the shell.
2499 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
2500 the Makefile override $SHELL from the environment. But first, we
2501 look for the basename of the shell in the directory where SHELL=
2502 points, and along the $PATH; if it is found in any of these places,
2503 we define $SHELL to be the actual pathname of the shell. Thus, if
2504 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
2505 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
2506 defining SHELL to be "d:/unix/bash.exe". */
2507 if ((origin == o_file || origin == o_override)
2508 && strcmp (varname, "SHELL") == 0)
2509 {
2510 PATH_VAR (shellpath);
2511 extern char * __dosexec_find_on_path (const char *, char *[], char *);
2512
2513 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
2514 if (__dosexec_find_on_path (p, NULL, shellpath))
2515 {
2516 char *tp;
2517
2518 for (tp = shellpath; *tp; tp++)
2519 if (*tp == '\\')
2520 *tp = '/';
2521
2522 v = define_variable_loc (varname, varname_len,
2523 shellpath, origin, flavor == f_recursive,
2524 flocp);
2525 }
2526 else
2527 {
2528 const char *shellbase, *bslash;
2529 struct variable *pathv = lookup_variable ("PATH", 4);
2530 char *path_string;
2531 char *fake_env[2];
2532 size_t pathlen = 0;
2533
2534 shellbase = strrchr (p, '/');
2535 bslash = strrchr (p, '\\');
2536 if (!shellbase || bslash > shellbase)
2537 shellbase = bslash;
2538 if (!shellbase && p[1] == ':')
2539 shellbase = p + 1;
2540 if (shellbase)
2541 shellbase++;
2542 else
2543 shellbase = p;
2544
2545 /* Search for the basename of the shell (with standard
2546 executable extensions) along the $PATH. */
2547 if (pathv)
2548 pathlen = strlen (pathv->value);
2549 path_string = xmalloc (5 + pathlen + 2 + 1);
2550 /* On MSDOS, current directory is considered as part of $PATH. */
2551 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
2552 fake_env[0] = path_string;
2553 fake_env[1] = 0;
2554 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
2555 {
2556 char *tp;
2557
2558 for (tp = shellpath; *tp; tp++)
2559 if (*tp == '\\')
2560 *tp = '/';
2561
2562 v = define_variable_loc (varname, varname_len,
2563 shellpath, origin,
2564 flavor == f_recursive, flocp);
2565 }
2566 else
2567 v = lookup_variable (varname, varname_len);
2568
2569 free (path_string);
2570 }
2571 }
2572 else
2573#endif /* __MSDOS__ */
2574#ifdef WINDOWS32
2575 if ( varname_len == sizeof("SHELL") - 1 /* bird */
2576 && (origin == o_file || origin == o_override || origin == o_command)
2577 && streq (varname, "SHELL"))
2578 {
2579 extern char *default_shell;
2580
2581 /* Call shell locator function. If it returns TRUE, then
2582 set no_default_sh_exe to indicate sh was found and
2583 set new value for SHELL variable. */
2584
2585 if (find_and_set_default_shell (p))
2586 {
2587 v = define_variable_in_set (varname, varname_len, default_shell,
2588# ifdef CONFIG_WITH_VALUE_LENGTH
2589 ~0U, 1 /* duplicate_value */,
2590# endif
2591 origin, flavor == f_recursive,
2592 (target_var
2593 ? current_variable_set_list->set
2594 : NULL),
2595 flocp);
2596 no_default_sh_exe = 0;
2597 }
2598 else
2599 {
2600 char *tp = alloc_value;
2601
2602 alloc_value = allocated_variable_expand (p);
2603
2604 if (find_and_set_default_shell (alloc_value))
2605 {
2606 v = define_variable_in_set (varname, varname_len, p,
2607#ifdef CONFIG_WITH_VALUE_LENGTH
2608 ~0U, 1 /* duplicate_value */,
2609#endif
2610 origin, flavor == f_recursive,
2611 (target_var
2612 ? current_variable_set_list->set
2613 : NULL),
2614 flocp);
2615 no_default_sh_exe = 0;
2616 }
2617 else
2618 v = lookup_variable (varname, varname_len);
2619
2620 if (tp)
2621 free (tp);
2622 }
2623 }
2624 else
2625#endif
2626
2627 /* If we are defining variables inside an $(eval ...), we might have a
2628 different variable context pushed, not the global context (maybe we're
2629 inside a $(call ...) or something. Since this function is only ever
2630 invoked in places where we want to define globally visible variables,
2631 make sure we define this variable in the global set. */
2632
2633 v = define_variable_in_set (varname, varname_len, p,
2634#ifdef CONFIG_WITH_VALUE_LENGTH
2635 value_len, !alloc_value,
2636#endif
2637 origin, flavor == f_recursive,
2638#ifdef CONFIG_WITH_LOCAL_VARIABLES
2639 (target_var || origin == o_local
2640#else
2641 (target_var
2642#endif
2643 ? current_variable_set_list->set : NULL),
2644 flocp);
2645 v->append = append;
2646 v->conditional = conditional;
2647
2648#ifndef CONFIG_WITH_VALUE_LENGTH
2649 if (alloc_value)
2650 free (alloc_value);
2651#else
2652 if (free_value)
2653 free (free_value);
2654#endif
2655
2656 return v->special ? set_special_var (v) : v;
2657}
2658
2659
2660/* Parse P (a null-terminated string) as a variable definition.
2661
2662 If it is not a variable definition, return NULL.
2663
2664 If it is a variable definition, return a pointer to the char after the
2665 assignment token and set *FLAVOR to the type of variable assignment. */
2666
2667char *
2668parse_variable_definition (const char *p, enum variable_flavor *flavor)
2669{
2670 int wspace = 0;
2671
2672 p = next_token (p);
2673
2674 while (1)
2675 {
2676 int c = *p++;
2677
2678 /* If we find a comment or EOS, it's not a variable definition. */
2679 if (c == '\0' || c == '#')
2680 return NULL;
2681
2682 if (c == '$')
2683 {
2684 /* This begins a variable expansion reference. Make sure we don't
2685 treat chars inside the reference as assignment tokens. */
2686 char closeparen;
2687 int count;
2688 c = *p++;
2689 if (c == '(')
2690 closeparen = ')';
2691 else if (c == '{')
2692 closeparen = '}';
2693 else
2694 /* '$$' or '$X'. Either way, nothing special to do here. */
2695 continue;
2696
2697 /* P now points past the opening paren or brace.
2698 Count parens or braces until it is matched. */
2699 count = 0;
2700 for (; *p != '\0'; ++p)
2701 {
2702 if (*p == c)
2703 ++count;
2704 else if (*p == closeparen && --count < 0)
2705 {
2706 ++p;
2707 break;
2708 }
2709 }
2710 continue;
2711 }
2712
2713 /* If we find whitespace skip it, and remember we found it. */
2714 if (isblank ((unsigned char)c))
2715 {
2716 wspace = 1;
2717 p = next_token (p);
2718 c = *p;
2719 if (c == '\0')
2720 return NULL;
2721 ++p;
2722 }
2723
2724
2725 if (c == '=')
2726 {
2727 *flavor = f_recursive;
2728 return (char *)p;
2729 }
2730
2731 /* Match assignment variants (:=, +=, ?=) */
2732 if (*p == '=')
2733 {
2734 switch (c)
2735 {
2736 case ':':
2737 *flavor = f_simple;
2738 break;
2739 case '+':
2740 *flavor = f_append;
2741 break;
2742#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2743 case '<':
2744 *flavor = f_prepend;
2745 break;
2746#endif
2747 case '?':
2748 *flavor = f_conditional;
2749 break;
2750 default:
2751 /* If we skipped whitespace, non-assignments means no var. */
2752 if (wspace)
2753 return NULL;
2754
2755 /* Might be assignment, or might be $= or #=. Check. */
2756 continue;
2757 }
2758 return (char *)++p;
2759 }
2760 else if (c == ':')
2761 /* A colon other than := is a rule line, not a variable defn. */
2762 return NULL;
2763
2764 /* If we skipped whitespace, non-assignments means no var. */
2765 if (wspace)
2766 return NULL;
2767 }
2768
2769 return (char *)p;
2770}
2771
2772
2773/* Try to interpret LINE (a null-terminated string) as a variable definition.
2774
2775 If LINE was recognized as a variable definition, a pointer to its `struct
2776 variable' is returned. If LINE is not a variable definition, NULL is
2777 returned. */
2778
2779struct variable *
2780assign_variable_definition (struct variable *v, char *line IF_WITH_VALUE_LENGTH_PARAM(char *eos))
2781{
2782 char *beg;
2783 char *end;
2784 enum variable_flavor flavor;
2785#ifndef CONFIG_WITH_VALUE_LENGTH
2786 char *name;
2787#endif
2788
2789 beg = next_token (line);
2790 line = parse_variable_definition (beg, &flavor);
2791 if (!line)
2792 return NULL;
2793
2794 end = line - (flavor == f_recursive ? 1 : 2);
2795 while (end > beg && isblank ((unsigned char)end[-1]))
2796 --end;
2797 line = next_token (line);
2798 v->value = line;
2799 v->flavor = flavor;
2800#ifdef CONFIG_WITH_VALUE_LENGTH
2801 v->value_alloc_len = ~(unsigned int)0;
2802 v->value_length = eos != NULL ? eos - line : -1;
2803 assert (eos == NULL || strchr (line, '\0') == eos);
2804# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2805 v->rdonly_val = 0;
2806# endif
2807#endif
2808
2809 /* Expand the name, so "$(foo)bar = baz" works. */
2810#ifndef CONFIG_WITH_VALUE_LENGTH
2811 name = alloca (end - beg + 1);
2812 memcpy (name, beg, end - beg);
2813 name[end - beg] = '\0';
2814 v->name = allocated_variable_expand (name);
2815#else /* CONFIG_WITH_VALUE_LENGTH */
2816 v->name = allocated_variable_expand_2 (beg, end - beg, NULL);
2817#endif /* CONFIG_WITH_VALUE_LENGTH */
2818
2819 if (v->name[0] == '\0')
2820 fatal (&v->fileinfo, _("empty variable name"));
2821
2822 return v;
2823}
2824
2825
2826/* Try to interpret LINE (a null-terminated string) as a variable definition.
2827
2828 ORIGIN may be o_file, o_override, o_env, o_env_override, o_local,
2829 or o_command specifying that the variable definition comes
2830 from a makefile, an override directive, the environment with
2831 or without the -e switch, or the command line.
2832
2833 See the comments for assign_variable_definition().
2834
2835 If LINE was recognized as a variable definition, a pointer to its `struct
2836 variable' is returned. If LINE is not a variable definition, NULL is
2837 returned. */
2838
2839struct variable *
2840try_variable_definition (const struct floc *flocp, char *line
2841 IF_WITH_VALUE_LENGTH_PARAM(char *eos),
2842 enum variable_origin origin, int target_var)
2843{
2844 struct variable v;
2845 struct variable *vp;
2846
2847 if (flocp != 0)
2848 v.fileinfo = *flocp;
2849 else
2850 v.fileinfo.filenm = 0;
2851
2852#ifndef CONFIG_WITH_VALUE_LENGTH
2853 if (!assign_variable_definition (&v, line))
2854 return 0;
2855
2856 vp = do_variable_definition (flocp, v.name, v.value,
2857 origin, v.flavor, target_var);
2858#else
2859 if (!assign_variable_definition (&v, line, eos))
2860 return 0;
2861
2862 vp = do_variable_definition_2 (flocp, v.name, v.value, v.value_length,
2863 0, NULL, origin, v.flavor, target_var);
2864#endif
2865
2866#ifndef CONFIG_WITH_STRCACHE2
2867 free (v.name);
2868#else
2869 free ((char *)v.name);
2870#endif
2871
2872 return vp;
2873}
2874
2875
2876#if defined (CONFIG_WITH_COMPILER) || defined (CONFIG_WITH_MAKE_STATS)
2877static unsigned long var_stats_evalvals, var_stats_evalvaled;
2878static unsigned long var_stats_expands, var_stats_expanded;
2879#endif
2880#ifdef CONFIG_WITH_MAKE_STATS
2881static unsigned long var_stats_changes, var_stats_changed;
2882static unsigned long var_stats_reallocs, var_stats_realloced;
2883static unsigned long var_stats_references, var_stats_referenced;
2884static unsigned long var_stats_val_len, var_stats_val_alloc_len;
2885static unsigned long var_stats_val_rdonly_len;
2886#endif
2887
2888/* Print information for variable V, prefixing it with PREFIX. */
2889
2890static void
2891print_variable (const void *item, void *arg)
2892{
2893 const struct variable *v = item;
2894 const char *prefix = arg;
2895 const char *origin;
2896#ifdef KMK
2897 const struct variable *alias = v;
2898 RESOLVE_ALIAS_VARIABLE(v);
2899#endif
2900
2901 switch (v->origin)
2902 {
2903 case o_default:
2904 origin = _("default");
2905 break;
2906 case o_env:
2907 origin = _("environment");
2908 break;
2909 case o_file:
2910 origin = _("makefile");
2911 break;
2912 case o_env_override:
2913 origin = _("environment under -e");
2914 break;
2915 case o_command:
2916 origin = _("command line");
2917 break;
2918 case o_override:
2919 origin = _("`override' directive");
2920 break;
2921 case o_automatic:
2922 origin = _("automatic");
2923 break;
2924#ifdef CONFIG_WITH_LOCAL_VARIABLES
2925 case o_local:
2926 origin = _("`local' directive");
2927 break;
2928#endif
2929 case o_invalid:
2930 default:
2931 abort ();
2932 }
2933 fputs ("# ", stdout);
2934 fputs (origin, stdout);
2935 if (v->private_var)
2936 fputs (" private", stdout);
2937#ifndef KMK
2938 if (v->fileinfo.filenm)
2939 printf (_(" (from `%s', line %lu)"),
2940 v->fileinfo.filenm, v->fileinfo.lineno);
2941#else /* KMK */
2942 if (alias->fileinfo.filenm)
2943 printf (_(" (from '%s', line %lu)"),
2944 alias->fileinfo.filenm, alias->fileinfo.lineno);
2945 if (alias->aliased)
2946 fputs (" aliased", stdout);
2947 if (alias->alias)
2948 printf (_(", alias for '%s'"), v->name);
2949#endif /* KMK */
2950
2951#if defined (CONFIG_WITH_COMPILER) || defined (CONFIG_WITH_MAKE_STATS)
2952 if (v->evalval_count != 0)
2953# ifdef CONFIG_WITH_MAKE_STATS
2954 printf (_(", %u evalvals (%llu ticks)"), v->evalval_count, v->cTicksEvalVal);
2955# else
2956 printf (_(", %u evalvals"), v->evalval_count);
2957# endif
2958 var_stats_evalvals += v->evalval_count;
2959 var_stats_evalvaled += (v->evalval_count != 0);
2960
2961 if (v->expand_count != 0)
2962 printf (_(", %u expands"), v->expand_count);
2963 var_stats_expands += v->expand_count;
2964 var_stats_expanded += (v->expand_count != 0);
2965# ifdef CONFIG_WITH_COMPILER
2966 if (v->evalprog != 0)
2967 printf (_(", evalprog"));
2968 if (v->expandprog != 0)
2969 printf (_(", expandprog"));
2970# endif
2971#endif
2972
2973#ifdef CONFIG_WITH_MAKE_STATS
2974 if (v->changes != 0)
2975 printf (_(", %u changes"), v->changes);
2976 var_stats_changes += v->changes;
2977 var_stats_changed += (v->changes != 0);
2978
2979 if (v->reallocs != 0)
2980 printf (_(", %u reallocs"), v->reallocs);
2981 var_stats_reallocs += v->reallocs;
2982 var_stats_realloced += (v->reallocs != 0);
2983
2984 if (v->references != 0)
2985 printf (_(", %u references"), v->references);
2986 var_stats_references += v->references;
2987 var_stats_referenced += (v->references != 0);
2988
2989 var_stats_val_len += v->value_length;
2990 if (v->value_alloc_len)
2991 var_stats_val_alloc_len += v->value_alloc_len;
2992 else
2993 var_stats_val_rdonly_len += v->value_length;
2994 assert (v->value_length == strlen (v->value));
2995 /*assert (v->rdonly_val ? !v->value_alloc_len : v->value_alloc_len > v->value_length); - FIXME */
2996#endif /* CONFIG_WITH_MAKE_STATS */
2997 putchar ('\n');
2998 fputs (prefix, stdout);
2999
3000 /* Is this a `define'? */
3001 if (v->recursive && strchr (v->value, '\n') != 0)
3002#ifndef KMK /** @todo language feature for aliases */
3003 printf ("define %s\n%s\nendef\n", v->name, v->value);
3004#else
3005 printf ("define %s\n%s\nendef\n", alias->name, v->value);
3006#endif
3007 else
3008 {
3009 char *p;
3010
3011#ifndef KMK /** @todo language feature for aliases */
3012 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
3013#else
3014 printf ("%s %s= ", alias->name, v->recursive ? v->append ? "+" : "" : ":");
3015#endif
3016
3017 /* Check if the value is just whitespace. */
3018 p = next_token (v->value);
3019 if (p != v->value && *p == '\0')
3020 /* All whitespace. */
3021 printf ("$(subst ,,%s)", v->value);
3022 else if (v->recursive)
3023 fputs (v->value, stdout);
3024 else
3025 /* Double up dollar signs. */
3026 for (p = v->value; *p != '\0'; ++p)
3027 {
3028 if (*p == '$')
3029 putchar ('$');
3030 putchar (*p);
3031 }
3032 putchar ('\n');
3033 }
3034}
3035
3036
3037/* Print all the variables in SET. PREFIX is printed before
3038 the actual variable definitions (everything else is comments). */
3039
3040void
3041print_variable_set (struct variable_set *set, char *prefix)
3042{
3043#if defined (CONFIG_WITH_COMPILER) || defined (CONFIG_WITH_MAKE_STATS)
3044 var_stats_expands = var_stats_expanded = var_stats_evalvals
3045 = var_stats_evalvaled = 0;
3046#endif
3047#ifdef CONFIG_WITH_MAKE_STATS
3048 var_stats_changes = var_stats_changed = var_stats_reallocs
3049 = var_stats_realloced = var_stats_references = var_stats_referenced
3050 = var_stats_val_len = var_stats_val_alloc_len
3051 = var_stats_val_rdonly_len = 0;
3052#endif
3053
3054 hash_map_arg (&set->table, print_variable, prefix);
3055
3056 if (set->table.ht_fill)
3057 {
3058#ifdef CONFIG_WITH_MAKE_STATS
3059 unsigned long fragmentation;
3060
3061 fragmentation = var_stats_val_alloc_len - (var_stats_val_len - var_stats_val_rdonly_len);
3062 printf(_("# variable set value stats:\n\
3063# strings %7lu bytes, readonly %6lu bytes\n"),
3064 var_stats_val_len, var_stats_val_rdonly_len);
3065
3066 if (var_stats_val_alloc_len)
3067 printf(_("# allocated %7lu bytes, fragmentation %6lu bytes (%u%%)\n"),
3068 var_stats_val_alloc_len, fragmentation,
3069 (unsigned int)((100.0 * fragmentation) / var_stats_val_alloc_len));
3070
3071 if (var_stats_changed)
3072 printf(_("# changed %5lu (%2u%%), changes %6lu\n"),
3073 var_stats_changed,
3074 (unsigned int)((100.0 * var_stats_changed) / set->table.ht_fill),
3075 var_stats_changes);
3076
3077 if (var_stats_realloced)
3078 printf(_("# reallocated %5lu (%2u%%), reallocations %6lu\n"),
3079 var_stats_realloced,
3080 (unsigned int)((100.0 * var_stats_realloced) / set->table.ht_fill),
3081 var_stats_reallocs);
3082
3083 if (var_stats_referenced)
3084 printf(_("# referenced %5lu (%2u%%), references %6lu\n"),
3085 var_stats_referenced,
3086 (unsigned int)((100.0 * var_stats_referenced) / set->table.ht_fill),
3087 var_stats_references);
3088#endif
3089#if defined (CONFIG_WITH_COMPILER) || defined (CONFIG_WITH_MAKE_STATS)
3090 if (var_stats_evalvals)
3091 printf(_("# evalvaled %5lu (%2u%%), evalval calls %6lu\n"),
3092 var_stats_evalvaled,
3093 (unsigned int)((100.0 * var_stats_evalvaled) / set->table.ht_fill),
3094 var_stats_evalvals);
3095 if (var_stats_expands)
3096 printf(_("# expanded %5lu (%2u%%), expands %6lu\n"),
3097 var_stats_expanded,
3098 (unsigned int)((100.0 * var_stats_expanded) / set->table.ht_fill),
3099 var_stats_expands);
3100#endif
3101 }
3102
3103 fputs (_("# variable set hash-table stats:\n"), stdout);
3104 fputs ("# ", stdout);
3105 hash_print_stats (&set->table, stdout);
3106 putc ('\n', stdout);
3107}
3108
3109/* Print the data base of variables. */
3110
3111void
3112print_variable_data_base (void)
3113{
3114 puts (_("\n# Variables\n"));
3115
3116 print_variable_set (&global_variable_set, "");
3117
3118 puts (_("\n# Pattern-specific Variable Values"));
3119
3120 {
3121 struct pattern_var *p;
3122 int rules = 0;
3123
3124 for (p = pattern_vars; p != 0; p = p->next)
3125 {
3126 ++rules;
3127 printf ("\n%s :\n", p->target);
3128 print_variable (&p->variable, "# ");
3129 }
3130
3131 if (rules == 0)
3132 puts (_("\n# No pattern-specific variable values."));
3133 else
3134 printf (_("\n# %u pattern-specific variable values"), rules);
3135 }
3136
3137#ifdef CONFIG_WITH_STRCACHE2
3138 strcache2_print_stats (&variable_strcache, "# ");
3139#endif
3140}
3141
3142#ifdef CONFIG_WITH_PRINT_STATS_SWITCH
3143void
3144print_variable_stats (void)
3145{
3146 fputs (_("\n# Global variable hash-table stats:\n# "), stdout);
3147 hash_print_stats (&global_variable_set.table, stdout);
3148 fputs ("\n", stdout);
3149}
3150#endif
3151
3152/* Print all the local variables of FILE. */
3153
3154void
3155print_file_variables (const struct file *file)
3156{
3157 if (file->variables != 0)
3158 print_variable_set (file->variables->set, "# ");
3159}
3160
3161#ifdef WINDOWS32
3162void
3163sync_Path_environment (void)
3164{
3165 char *path = allocated_variable_expand ("$(PATH)");
3166 static char *environ_path = NULL;
3167
3168 if (!path)
3169 return;
3170
3171 /*
3172 * If done this before, don't leak memory unnecessarily.
3173 * Free the previous entry before allocating new one.
3174 */
3175 if (environ_path)
3176 free (environ_path);
3177
3178 /*
3179 * Create something WINDOWS32 world can grok
3180 */
3181 convert_Path_to_windows32 (path, ';');
3182 environ_path = xstrdup (concat (3, "PATH", "=", path));
3183 putenv (environ_path);
3184 free (path);
3185}
3186#endif
Note: See TracBrowser for help on using the repository browser.