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

Last change on this file since 2740 was 2736, checked in by bird, 11 years ago

llvm build fixes.

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