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

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

Added another variable statistic under #ifdef CONFIG_WITH_MAKE_STATS and made the makefile check for CONFIG_WITH_MAKE_STATS (instead of only enabling it for debug builds).

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