VirtualBox

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

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

Started on a make expression and string expansion 'compiler'.

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

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