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

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

string expansion debugged and enabled. fixed access-after-alloc bug in func_sort and could lead to heap corruption.

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