VirtualBox

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

Last change on this file since 2989 was 2893, checked in by bird, 8 years ago

variables.c/target_environment: Cache the exported variables in the global_variable_set since there are likely to be very very many of them and they aren't usually changing when we've loaded the makefiles and start running make jobs. Also, use smaller hash tables for collecting exported variables (I've bumped VARIABLE_BUCKETS up a lot).

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

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