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

Last change on this file since 2734 was 2718, checked in by bird, 12 years ago

kmk: Variable aliases.

  • 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#ifdef KMK
1013 struct variable *v;
1014#endif
1015#ifndef CONFIG_WITH_STRCACHE2
1016 var_key.name = (char *) name;
1017 var_key.length = length;
1018
1019 return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
1020#else /* CONFIG_WITH_STRCACHE2 */
1021 const char *cached_name;
1022
1023# ifdef KMK
1024 /* Check for kBuild-define- local variable accesses and handle these first. */
1025 if (length > 3 && name[0] == '[' && set == &global_variable_set)
1026 {
1027 struct variable *v = lookup_kbuild_object_variable_accessor(name, length);
1028 if (v != VAR_NOT_KBUILD_ACCESSOR)
1029 {
1030 RESOLVE_ALIAS_VARIABLE(v);
1031 return v;
1032 }
1033 }
1034# endif
1035
1036 /* lookup the name in the string case, if it's not there it won't
1037 be in any of the sets either. Optimize lookups in the global set. */
1038 cached_name = strcache2_lookup(&variable_strcache, name, length);
1039 if (!cached_name)
1040 return NULL;
1041
1042 if (set == &global_variable_set)
1043 {
1044 v = strcache2_get_user_val (&variable_strcache, cached_name);
1045 assert (!v || v->name == cached_name);
1046 }
1047 else
1048 {
1049 var_key.name = cached_name;
1050 var_key.length = length;
1051
1052 v = (struct variable *) hash_find_item_strcached (
1053 (struct hash_table *) &set->table, &var_key);
1054 }
1055# ifdef KMK
1056 RESOLVE_ALIAS_VARIABLE(v);
1057# endif
1058 return v;
1059#endif /* CONFIG_WITH_STRCACHE2 */
1060}
1061
1062
1063/* Initialize FILE's variable set list. If FILE already has a variable set
1064 list, the topmost variable set is left intact, but the the rest of the
1065 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
1066 rule, then we will use the "root" double-colon target's variable set as the
1067 parent of FILE's variable set.
1068
1069 If we're READING a makefile, don't do the pattern variable search now,
1070 since the pattern variable might not have been defined yet. */
1071
1072void
1073initialize_file_variables (struct file *file, int reading)
1074{
1075 struct variable_set_list *l = file->variables;
1076
1077 if (l == 0)
1078 {
1079#ifndef CONFIG_WITH_ALLOC_CACHES
1080 l = (struct variable_set_list *)
1081 xmalloc (sizeof (struct variable_set_list));
1082 l->set = xmalloc (sizeof (struct variable_set));
1083#else /* CONFIG_WITH_ALLOC_CACHES */
1084 l = (struct variable_set_list *)
1085 alloccache_alloc (&variable_set_list_cache);
1086 l->set = (struct variable_set *)
1087 alloccache_alloc (&variable_set_cache);
1088#endif /* CONFIG_WITH_ALLOC_CACHES */
1089#ifndef CONFIG_WITH_STRCACHE2
1090 hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
1091 variable_hash_1, variable_hash_2, variable_hash_cmp);
1092#else /* CONFIG_WITH_STRCACHE2 */
1093 hash_init_strcached (&l->set->table, PERFILE_VARIABLE_BUCKETS,
1094 &variable_strcache, offsetof (struct variable, name));
1095#endif /* CONFIG_WITH_STRCACHE2 */
1096 file->variables = l;
1097 }
1098
1099 /* If this is a double-colon, then our "parent" is the "root" target for
1100 this double-colon rule. Since that rule has the same name, parent,
1101 etc. we can just use its variables as the "next" for ours. */
1102
1103 if (file->double_colon && file->double_colon != file)
1104 {
1105 initialize_file_variables (file->double_colon, reading);
1106 l->next = file->double_colon->variables;
1107 l->next_is_parent = 0;
1108 return;
1109 }
1110
1111 if (file->parent == 0)
1112 l->next = &global_setlist;
1113 else
1114 {
1115 initialize_file_variables (file->parent, reading);
1116 l->next = file->parent->variables;
1117 }
1118 l->next_is_parent = 1;
1119
1120 /* If we're not reading makefiles and we haven't looked yet, see if
1121 we can find pattern variables for this target. */
1122
1123 if (!reading && !file->pat_searched)
1124 {
1125 struct pattern_var *p;
1126
1127 p = lookup_pattern_var (0, file->name);
1128 if (p != 0)
1129 {
1130 struct variable_set_list *global = current_variable_set_list;
1131
1132 /* We found at least one. Set up a new variable set to accumulate
1133 all the pattern variables that match this target. */
1134
1135 file->pat_variables = create_new_variable_set ();
1136 current_variable_set_list = file->pat_variables;
1137
1138 do
1139 {
1140 /* We found one, so insert it into the set. */
1141
1142 struct variable *v;
1143
1144 if (p->variable.flavor == f_simple)
1145 {
1146 v = define_variable_loc (
1147 p->variable.name, strlen (p->variable.name),
1148 p->variable.value, p->variable.origin,
1149 0, &p->variable.fileinfo);
1150
1151 v->flavor = f_simple;
1152 }
1153 else
1154 {
1155#ifndef CONFIG_WITH_VALUE_LENGTH
1156 v = do_variable_definition (
1157 &p->variable.fileinfo, p->variable.name,
1158 p->variable.value, p->variable.origin,
1159 p->variable.flavor, 1);
1160#else
1161 v = do_variable_definition_2 (
1162 &p->variable.fileinfo, p->variable.name,
1163 p->variable.value, p->variable.value_length, 0, 0,
1164 p->variable.origin, p->variable.flavor, 1);
1165#endif
1166 }
1167
1168 /* Also mark it as a per-target and copy export status. */
1169 v->per_target = p->variable.per_target;
1170 v->export = p->variable.export;
1171 v->private_var = p->variable.private_var;
1172 }
1173 while ((p = lookup_pattern_var (p, file->name)) != 0);
1174
1175 current_variable_set_list = global;
1176 }
1177 file->pat_searched = 1;
1178 }
1179
1180 /* If we have a pattern variable match, set it up. */
1181
1182 if (file->pat_variables != 0)
1183 {
1184 file->pat_variables->next = l->next;
1185 file->pat_variables->next_is_parent = l->next_is_parent;
1186 l->next = file->pat_variables;
1187 l->next_is_parent = 0;
1188 }
1189}
1190
1191
1192/* Pop the top set off the current variable set list,
1193 and free all its storage. */
1194
1195struct variable_set_list *
1196create_new_variable_set (void)
1197{
1198 register struct variable_set_list *setlist;
1199 register struct variable_set *set;
1200
1201#ifndef CONFIG_WITH_ALLOC_CACHES
1202 set = xmalloc (sizeof (struct variable_set));
1203#else
1204 set = (struct variable_set *) alloccache_alloc (&variable_set_cache);
1205#endif
1206#ifndef CONFIG_WITH_STRCACHE2
1207 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
1208 variable_hash_1, variable_hash_2, variable_hash_cmp);
1209#else /* CONFIG_WITH_STRCACHE2 */
1210 hash_init_strcached (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
1211 &variable_strcache, offsetof (struct variable, name));
1212#endif /* CONFIG_WITH_STRCACHE2 */
1213
1214#ifndef CONFIG_WITH_ALLOC_CACHES
1215 setlist = (struct variable_set_list *)
1216 xmalloc (sizeof (struct variable_set_list));
1217#else
1218 setlist = (struct variable_set_list *)
1219 alloccache_alloc (&variable_set_list_cache);
1220#endif
1221 setlist->set = set;
1222 setlist->next = current_variable_set_list;
1223 setlist->next_is_parent = 0;
1224
1225 return setlist;
1226}
1227
1228static void
1229free_variable_name_and_value (const void *item)
1230{
1231 struct variable *v = (struct variable *) item;
1232#ifndef CONFIG_WITH_STRCACHE2
1233 free (v->name);
1234#endif
1235#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1236 if (!v->rdonly_val)
1237#endif
1238 free (v->value);
1239}
1240
1241void
1242free_variable_set (struct variable_set_list *list)
1243{
1244 hash_map (&list->set->table, free_variable_name_and_value);
1245#ifndef CONFIG_WITH_ALLOC_CACHES
1246 hash_free (&list->set->table, 1);
1247 free (list->set);
1248 free (list);
1249#else
1250 hash_free_cached (&list->set->table, 1, &variable_cache);
1251 alloccache_free (&variable_set_cache, list->set);
1252 alloccache_free (&variable_set_list_cache, list);
1253#endif
1254}
1255
1256/* Create a new variable set and push it on the current setlist.
1257 If we're pushing a global scope (that is, the current scope is the global
1258 scope) then we need to "push" it the other way: file variable sets point
1259 directly to the global_setlist so we need to replace that with the new one.
1260 */
1261
1262struct variable_set_list *
1263push_new_variable_scope (void)
1264{
1265 current_variable_set_list = create_new_variable_set();
1266 if (current_variable_set_list->next == &global_setlist)
1267 {
1268 /* It was the global, so instead of new -> &global we want to replace
1269 &global with the new one and have &global -> new, with current still
1270 pointing to &global */
1271 struct variable_set *set = current_variable_set_list->set;
1272 current_variable_set_list->set = global_setlist.set;
1273 global_setlist.set = set;
1274 current_variable_set_list->next = global_setlist.next;
1275 global_setlist.next = current_variable_set_list;
1276 current_variable_set_list = &global_setlist;
1277 }
1278 return (current_variable_set_list);
1279}
1280
1281void
1282pop_variable_scope (void)
1283{
1284 struct variable_set_list *setlist;
1285 struct variable_set *set;
1286
1287 /* Can't call this if there's no scope to pop! */
1288 assert(current_variable_set_list->next != NULL);
1289
1290 if (current_variable_set_list != &global_setlist)
1291 {
1292 /* We're not pointing to the global setlist, so pop this one. */
1293 setlist = current_variable_set_list;
1294 set = setlist->set;
1295 current_variable_set_list = setlist->next;
1296 }
1297 else
1298 {
1299 /* This set is the one in the global_setlist, but there is another global
1300 set beyond that. We want to copy that set to global_setlist, then
1301 delete what used to be in global_setlist. */
1302 setlist = global_setlist.next;
1303 set = global_setlist.set;
1304 global_setlist.set = setlist->set;
1305 global_setlist.next = setlist->next;
1306 global_setlist.next_is_parent = setlist->next_is_parent;
1307 }
1308
1309 /* Free the one we no longer need. */
1310#ifndef CONFIG_WITH_ALLOC_CACHES
1311 free (setlist);
1312 hash_map (&set->table, free_variable_name_and_value);
1313 hash_free (&set->table, 1);
1314 free (set);
1315#else
1316 alloccache_free (&variable_set_list_cache, setlist);
1317 hash_map (&set->table, free_variable_name_and_value);
1318 hash_free_cached (&set->table, 1, &variable_cache);
1319 alloccache_free (&variable_set_cache, set);
1320#endif
1321}
1322
1323
1324/* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
1325
1326static void
1327merge_variable_sets (struct variable_set *to_set,
1328 struct variable_set *from_set)
1329{
1330 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
1331 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
1332
1333 for ( ; from_var_slot < from_var_end; from_var_slot++)
1334 if (! HASH_VACANT (*from_var_slot))
1335 {
1336 struct variable *from_var = *from_var_slot;
1337 struct variable **to_var_slot
1338#ifndef CONFIG_WITH_STRCACHE2
1339 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
1340#else /* CONFIG_WITH_STRCACHE2 */
1341 = (struct variable **) hash_find_slot_strcached (&to_set->table,
1342 *from_var_slot);
1343#endif /* CONFIG_WITH_STRCACHE2 */
1344 if (HASH_VACANT (*to_var_slot))
1345 hash_insert_at (&to_set->table, from_var, to_var_slot);
1346 else
1347 {
1348 /* GKM FIXME: delete in from_set->table */
1349#ifdef KMK
1350 if (from_var->aliased)
1351 fatal(NULL, ("Attempting to delete aliased variable '%s'"), from_var->name);
1352 if (from_var->alias)
1353 fatal(NULL, ("Attempting to delete variable aliased '%s'"), from_var->name);
1354#endif
1355#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1356 if (!from_var->rdonly_val)
1357#endif
1358 free (from_var->value);
1359 free (from_var);
1360 }
1361 }
1362}
1363
1364/* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
1365
1366void
1367merge_variable_set_lists (struct variable_set_list **setlist0,
1368 struct variable_set_list *setlist1)
1369{
1370 struct variable_set_list *to = *setlist0;
1371 struct variable_set_list *last0 = 0;
1372
1373 /* If there's nothing to merge, stop now. */
1374 if (!setlist1)
1375 return;
1376
1377 /* This loop relies on the fact that all setlists terminate with the global
1378 setlist (before NULL). If that's not true, arguably we SHOULD die. */
1379 if (to)
1380 while (setlist1 != &global_setlist && to != &global_setlist)
1381 {
1382 struct variable_set_list *from = setlist1;
1383 setlist1 = setlist1->next;
1384
1385 merge_variable_sets (to->set, from->set);
1386
1387 last0 = to;
1388 to = to->next;
1389 }
1390
1391 if (setlist1 != &global_setlist)
1392 {
1393 if (last0 == 0)
1394 *setlist0 = setlist1;
1395 else
1396 last0->next = setlist1;
1397 }
1398}
1399
1400
1401#if defined(KMK) && !defined(WINDOWS32)
1402/* Parses out the next number from the uname release level string. Fast
1403 forwards to the end of the string when encountering some non-conforming
1404 chars. */
1405
1406static unsigned long parse_release_number (const char **ppsz)
1407{
1408 unsigned long ul;
1409 char *psz = (char *)*ppsz;
1410 if (ISDIGIT (*psz))
1411 {
1412 ul = strtoul (psz, &psz, 10);
1413 if (psz != NULL && *psz == '.')
1414 psz++;
1415 else
1416 psz = strchr (*ppsz, '\0');
1417 *ppsz = psz;
1418 }
1419 else
1420 ul = 0;
1421 return ul;
1422}
1423#endif
1424
1425
1426/* Define the automatic variables, and record the addresses
1427 of their structures so we can change their values quickly. */
1428
1429void
1430define_automatic_variables (void)
1431{
1432#if defined(WINDOWS32) || defined(__EMX__)
1433 extern char* default_shell;
1434#else
1435 extern char default_shell[];
1436#endif
1437 register struct variable *v;
1438#ifndef KMK
1439 char buf[200];
1440#else
1441 char buf[1024];
1442 const char *val;
1443 struct variable *envvar1;
1444 struct variable *envvar2;
1445# ifdef WINDOWS32
1446 OSVERSIONINFOEX oix;
1447# else
1448 struct utsname uts;
1449# endif
1450 unsigned long ulMajor = 0, ulMinor = 0, ulPatch = 0, ul4th = 0;
1451#endif
1452
1453 sprintf (buf, "%u", makelevel);
1454 define_variable_cname (MAKELEVEL_NAME, buf, o_env, 0);
1455
1456 sprintf (buf, "%s%s%s",
1457 version_string,
1458 (remote_description == 0 || remote_description[0] == '\0')
1459 ? "" : "-",
1460 (remote_description == 0 || remote_description[0] == '\0')
1461 ? "" : remote_description);
1462#ifndef KMK
1463 define_variable_cname ("MAKE_VERSION", buf, o_default, 0);
1464#else /* KMK */
1465
1466 /* Define KMK_VERSION to indicate kMk. */
1467 define_variable_cname ("KMK_VERSION", buf, o_default, 0);
1468
1469 /* Define KBUILD_VERSION* */
1470 sprintf (buf, "%d", KBUILD_VERSION_MAJOR);
1471 define_variable_cname ("KBUILD_VERSION_MAJOR", buf, o_default, 0);
1472 sprintf (buf, "%d", KBUILD_VERSION_MINOR);
1473 define_variable_cname ("KBUILD_VERSION_MINOR", buf, o_default, 0);
1474 sprintf (buf, "%d", KBUILD_VERSION_PATCH);
1475 define_variable_cname ("KBUILD_VERSION_PATCH", buf, o_default, 0);
1476 sprintf (buf, "%d", KBUILD_SVN_REV);
1477 define_variable_cname ("KBUILD_KMK_REVISION", buf, o_default, 0);
1478
1479 sprintf (buf, "%d.%d.%d-r%d", KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR,
1480 KBUILD_VERSION_PATCH, KBUILD_SVN_REV);
1481 define_variable_cname ("KBUILD_VERSION", buf, o_default, 0);
1482
1483 /* The host defaults. The BUILD_* stuff will be replaced by KBUILD_* soon. */
1484 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST"));
1485 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM"));
1486 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST;
1487 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1488 error (NULL, _("KBUILD_HOST and BUILD_PLATFORM differs, using KBUILD_HOST=%s."), val);
1489 if (!envvar1)
1490 define_variable_cname ("KBUILD_HOST", val, o_default, 0);
1491 if (!envvar2)
1492 define_variable_cname ("BUILD_PLATFORM", val, o_default, 0);
1493
1494 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_ARCH"));
1495 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_ARCH"));
1496 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_ARCH;
1497 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1498 error (NULL, _("KBUILD_HOST_ARCH and BUILD_PLATFORM_ARCH differs, using KBUILD_HOST_ARCH=%s."), val);
1499 if (!envvar1)
1500 define_variable_cname ("KBUILD_HOST_ARCH", val, o_default, 0);
1501 if (!envvar2)
1502 define_variable_cname ("BUILD_PLATFORM_ARCH", val, o_default, 0);
1503
1504 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_CPU"));
1505 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_CPU"));
1506 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_CPU;
1507 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1508 error (NULL, _("KBUILD_HOST_CPU and BUILD_PLATFORM_CPU differs, using KBUILD_HOST_CPU=%s."), val);
1509 if (!envvar1)
1510 define_variable_cname ("KBUILD_HOST_CPU", val, o_default, 0);
1511 if (!envvar2)
1512 define_variable_cname ("BUILD_PLATFORM_CPU", val, o_default, 0);
1513
1514 /* The host kernel version. */
1515#if defined(WINDOWS32)
1516 memset (&oix, '\0', sizeof (oix));
1517 oix.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1518 if (!GetVersionEx ((LPOSVERSIONINFO)&oix))
1519 {
1520 memset (&oix, '\0', sizeof (oix));
1521 oix.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
1522 GetVersionEx ((LPOSVERSIONINFO)&oix);
1523 }
1524 if (oix.dwPlatformId == VER_PLATFORM_WIN32_NT)
1525 {
1526 ulMajor = oix.dwMajorVersion;
1527 ulMinor = oix.dwMinorVersion;
1528 ulPatch = oix.wServicePackMajor;
1529 ul4th = oix.wServicePackMinor;
1530 }
1531 else
1532 {
1533 ulMajor = oix.dwPlatformId == 1 ? 0 /*Win95/98/ME*/
1534 : oix.dwPlatformId == 3 ? 1 /*WinCE*/
1535 : 2; /*??*/
1536 ulMinor = oix.dwMajorVersion;
1537 ulPatch = oix.dwMinorVersion;
1538 ul4th = oix.wServicePackMajor;
1539 }
1540#else
1541 memset (&uts, 0, sizeof(uts));
1542 uname (&uts);
1543 val = uts.release;
1544 ulMajor = parse_release_number (&val);
1545 ulMinor = parse_release_number (&val);
1546 ulPatch = parse_release_number (&val);
1547 ul4th = parse_release_number (&val);
1548#endif
1549
1550 sprintf (buf, "%lu.%lu.%lu.%lu", ulMajor, ulMinor, ulPatch, ul4th);
1551 define_variable_cname ("KBUILD_HOST_VERSION", buf, o_default, 0);
1552
1553 sprintf (buf, "%lu", ulMajor);
1554 define_variable_cname ("KBUILD_HOST_VERSION_MAJOR", buf, o_default, 0);
1555
1556 sprintf (buf, "%lu", ulMinor);
1557 define_variable_cname ("KBUILD_HOST_VERSION_MINOR", buf, o_default, 0);
1558
1559 sprintf (buf, "%lu", ulPatch);
1560 define_variable_cname ("KBUILD_HOST_VERSION_PATCH", buf, o_default, 0);
1561
1562 /* The kBuild locations. */
1563 define_variable_cname ("KBUILD_PATH", get_kbuild_path (), o_default, 0);
1564 define_variable_cname ("KBUILD_BIN_PATH", get_kbuild_bin_path (), o_default, 0);
1565
1566 define_variable_cname ("PATH_KBUILD", get_kbuild_path (), o_default, 0);
1567 define_variable_cname ("PATH_KBUILD_BIN", get_kbuild_bin_path (), o_default, 0);
1568
1569 /* Define KMK_FEATURES to indicate various working KMK features. */
1570# if defined (CONFIG_WITH_RSORT) \
1571 && defined (CONFIG_WITH_ABSPATHEX) \
1572 && defined (CONFIG_WITH_TOUPPER_TOLOWER) \
1573 && defined (CONFIG_WITH_DEFINED) \
1574 && defined (CONFIG_WITH_VALUE_LENGTH) \
1575 && defined (CONFIG_WITH_COMPARE) \
1576 && defined (CONFIG_WITH_STACK) \
1577 && defined (CONFIG_WITH_MATH) \
1578 && defined (CONFIG_WITH_XARGS) \
1579 && defined (CONFIG_WITH_EXPLICIT_MULTITARGET) \
1580 && defined (CONFIG_WITH_DOT_MUST_MAKE) \
1581 && defined (CONFIG_WITH_PREPEND_ASSIGNMENT) \
1582 && defined (CONFIG_WITH_SET_CONDITIONALS) \
1583 && defined (CONFIG_WITH_DATE) \
1584 && defined (CONFIG_WITH_FILE_SIZE) \
1585 && defined (CONFIG_WITH_WHERE_FUNCTION) \
1586 && defined (CONFIG_WITH_WHICH) \
1587 && defined (CONFIG_WITH_EVALPLUS) \
1588 && (defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)) \
1589 && defined (CONFIG_WITH_COMMANDS_FUNC) \
1590 && defined (CONFIG_WITH_PRINTF) \
1591 && defined (CONFIG_WITH_LOOP_FUNCTIONS) \
1592 && defined (CONFIG_WITH_ROOT_FUNC) \
1593 && defined (CONFIG_WITH_STRING_FUNCTIONS) \
1594 && defined (CONFIG_WITH_DEFINED_FUNCTIONS) \
1595 && defined (KMK_HELPERS)
1596 define_variable_cname ("KMK_FEATURES",
1597 "append-dash-n abspath includedep-queue install-hard-linking umask"
1598 " kBuild-define"
1599 " rsort"
1600 " abspathex"
1601 " toupper tolower"
1602 " defined"
1603 " comp-vars comp-cmds comp-cmds-ex"
1604 " stack"
1605 " math-int"
1606 " xargs"
1607 " explicit-multitarget"
1608 " dot-must-make"
1609 " prepend-assignment"
1610 " set-conditionals intersects"
1611 " date"
1612 " file-size"
1613 " expr if-expr select"
1614 " where"
1615 " which"
1616 " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var"
1617 " make-stats"
1618 " commands"
1619 " printf"
1620 " for while"
1621 " root"
1622 " length insert pos lastpos substr translate"
1623 " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one kb-exp-tmpl"
1624 " firstdefined lastdefined"
1625 , o_default, 0);
1626# else /* MSC can't deal with strings mixed with #if/#endif, thus the slow way. */
1627# error "All features should be enabled by default!"
1628 strcpy (buf, "append-dash-n abspath includedep-queue install-hard-linking umask"
1629 " kBuild-define");
1630# if defined (CONFIG_WITH_RSORT)
1631 strcat (buf, " rsort");
1632# endif
1633# if defined (CONFIG_WITH_ABSPATHEX)
1634 strcat (buf, " abspathex");
1635# endif
1636# if defined (CONFIG_WITH_TOUPPER_TOLOWER)
1637 strcat (buf, " toupper tolower");
1638# endif
1639# if defined (CONFIG_WITH_DEFINED)
1640 strcat (buf, " defined");
1641# endif
1642# if defined (CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
1643 strcat (buf, " comp-vars comp-cmds comp-cmds-ex");
1644# endif
1645# if defined (CONFIG_WITH_STACK)
1646 strcat (buf, " stack");
1647# endif
1648# if defined (CONFIG_WITH_MATH)
1649 strcat (buf, " math-int");
1650# endif
1651# if defined (CONFIG_WITH_XARGS)
1652 strcat (buf, " xargs");
1653# endif
1654# if defined (CONFIG_WITH_EXPLICIT_MULTITARGET)
1655 strcat (buf, " explicit-multitarget");
1656# endif
1657# if defined (CONFIG_WITH_DOT_MUST_MAKE)
1658 strcat (buf, " dot-must-make");
1659# endif
1660# if defined (CONFIG_WITH_PREPEND_ASSIGNMENT)
1661 strcat (buf, " prepend-assignment");
1662# endif
1663# if defined (CONFIG_WITH_SET_CONDITIONALS)
1664 strcat (buf, " set-conditionals intersects");
1665# endif
1666# if defined (CONFIG_WITH_DATE)
1667 strcat (buf, " date");
1668# endif
1669# if defined (CONFIG_WITH_FILE_SIZE)
1670 strcat (buf, " file-size");
1671# endif
1672# if defined (CONFIG_WITH_IF_CONDITIONALS)
1673 strcat (buf, " expr if-expr select");
1674# endif
1675# if defined (CONFIG_WITH_WHERE_FUNCTION)
1676 strcat (buf, " where");
1677# endif
1678# if defined (CONFIG_WITH_WHICH)
1679 strcat (buf, " which");
1680# endif
1681# if defined (CONFIG_WITH_EVALPLUS)
1682 strcat (buf, " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var");
1683# endif
1684# if defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)
1685 strcat (buf, " make-stats");
1686# endif
1687# if defined (CONFIG_WITH_COMMANDS_FUNC)
1688 strcat (buf, " commands");
1689# endif
1690# if defined (CONFIG_WITH_PRINTF)
1691 strcat (buf, " printf");
1692# endif
1693# if defined (CONFIG_WITH_LOOP_FUNCTIONS)
1694 strcat (buf, " for while");
1695# endif
1696# if defined (CONFIG_WITH_ROOT_FUNC)
1697 strcat (buf, " root");
1698# endif
1699# if defined (CONFIG_WITH_STRING_FUNCTIONS)
1700 strcat (buf, " length insert pos lastpos substr translate");
1701# endif
1702# if defined (CONFIG_WITH_DEFINED_FUNCTIONS)
1703 strcat (buf, " firstdefined lastdefined");
1704# endif
1705# if defined (KMK_HELPERS)
1706 strcat (buf, " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one kb-exp-tmpl");
1707# endif
1708 define_variable_cname ("KMK_FEATURES", buf, o_default, 0);
1709# endif
1710
1711#endif /* KMK */
1712
1713#ifdef CONFIG_WITH_KMK_BUILTIN
1714 /* The supported kMk Builtin commands. */
1715 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);
1716#endif
1717
1718#ifdef __MSDOS__
1719 /* Allow to specify a special shell just for Make,
1720 and use $COMSPEC as the default $SHELL when appropriate. */
1721 {
1722 static char shell_str[] = "SHELL";
1723 const int shlen = sizeof (shell_str) - 1;
1724 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
1725 struct variable *comp = lookup_variable ("COMSPEC", 7);
1726
1727 /* $(MAKESHELL) overrides $(SHELL) even if -e is in effect. */
1728 if (mshp)
1729 (void) define_variable (shell_str, shlen,
1730 mshp->value, o_env_override, 0);
1731 else if (comp)
1732 {
1733 /* $(COMSPEC) shouldn't override $(SHELL). */
1734 struct variable *shp = lookup_variable (shell_str, shlen);
1735
1736 if (!shp)
1737 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
1738 }
1739 }
1740#elif defined(__EMX__)
1741 {
1742 static char shell_str[] = "SHELL";
1743 const int shlen = sizeof (shell_str) - 1;
1744 struct variable *shell = lookup_variable (shell_str, shlen);
1745 struct variable *replace = lookup_variable ("MAKESHELL", 9);
1746
1747 /* if $MAKESHELL is defined in the environment assume o_env_override */
1748 if (replace && *replace->value && replace->origin == o_env)
1749 replace->origin = o_env_override;
1750
1751 /* if $MAKESHELL is not defined use $SHELL but only if the variable
1752 did not come from the environment */
1753 if (!replace || !*replace->value)
1754 if (shell && *shell->value && (shell->origin == o_env
1755 || shell->origin == o_env_override))
1756 {
1757 /* overwrite whatever we got from the environment */
1758 free(shell->value);
1759 shell->value = xstrdup (default_shell);
1760 shell->origin = o_default;
1761 }
1762
1763 /* Some people do not like cmd to be used as the default
1764 if $SHELL is not defined in the Makefile.
1765 With -DNO_CMD_DEFAULT you can turn off this behaviour */
1766# ifndef NO_CMD_DEFAULT
1767 /* otherwise use $COMSPEC */
1768 if (!replace || !*replace->value)
1769 replace = lookup_variable ("COMSPEC", 7);
1770
1771 /* otherwise use $OS2_SHELL */
1772 if (!replace || !*replace->value)
1773 replace = lookup_variable ("OS2_SHELL", 9);
1774# else
1775# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
1776# endif
1777
1778 if (replace && *replace->value)
1779 /* overwrite $SHELL */
1780 (void) define_variable (shell_str, shlen, replace->value,
1781 replace->origin, 0);
1782 else
1783 /* provide a definition if there is none */
1784 (void) define_variable (shell_str, shlen, default_shell,
1785 o_default, 0);
1786 }
1787
1788#endif
1789
1790 /* This won't override any definition, but it will provide one if there
1791 isn't one there. */
1792 v = define_variable_cname ("SHELL", default_shell, o_default, 0);
1793#ifdef __MSDOS__
1794 v->export = v_export; /* Export always SHELL. */
1795#endif
1796
1797 /* On MSDOS we do use SHELL from environment, since it isn't a standard
1798 environment variable on MSDOS, so whoever sets it, does that on purpose.
1799 On OS/2 we do not use SHELL from environment but we have already handled
1800 that problem above. */
1801#if !defined(__MSDOS__) && !defined(__EMX__)
1802 /* Don't let SHELL come from the environment. */
1803 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
1804 {
1805# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1806 if (v->rdonly_val)
1807 v->rdonly_val = 0;
1808 else
1809# endif
1810 free (v->value);
1811 v->origin = o_file;
1812 v->value = xstrdup (default_shell);
1813# ifdef CONFIG_WITH_VALUE_LENGTH
1814 v->value_length = strlen (v->value);
1815 v->value_alloc_len = v->value_length + 1;
1816# endif
1817 }
1818#endif
1819
1820 /* Make sure MAKEFILES gets exported if it is set. */
1821 v = define_variable_cname ("MAKEFILES", "", o_default, 0);
1822 v->export = v_ifset;
1823
1824 /* Define the magic D and F variables in terms of
1825 the automatic variables they are variations of. */
1826
1827#ifdef VMS
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 define_variable_cname ("^D", "$(dir $^)", o_automatic, 1);
1834 define_variable_cname ("+D", "$(dir $+)", o_automatic, 1);
1835#else
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 define_variable_cname ("^D", "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
1842 define_variable_cname ("+D", "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
1843#endif
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 define_variable_cname ("^F", "$(notdir $^)", o_automatic, 1);
1850 define_variable_cname ("+F", "$(notdir $+)", o_automatic, 1);
1851#ifdef CONFIG_WITH_LAZY_DEPS_VARS
1852 define_variable ("^", 1, "$(deps $@)", o_automatic, 1);
1853 define_variable ("+", 1, "$(deps-all $@)", o_automatic, 1);
1854 define_variable ("?", 1, "$(deps-newer $@)", o_automatic, 1);
1855 define_variable ("|", 1, "$(deps-oo $@)", o_automatic, 1);
1856#endif /* CONFIG_WITH_LAZY_DEPS_VARS */
1857}
1858
1859
1860int export_all_variables;
1861
1862/* Create a new environment for FILE's commands.
1863 If FILE is nil, this is for the `shell' function.
1864 The child's MAKELEVEL variable is incremented. */
1865
1866char **
1867target_environment (struct file *file)
1868{
1869 struct variable_set_list *set_list;
1870 register struct variable_set_list *s;
1871 struct hash_table table;
1872 struct variable **v_slot;
1873 struct variable **v_end;
1874 struct variable makelevel_key;
1875 char **result_0;
1876 char **result;
1877#ifdef CONFIG_WITH_STRCACHE2
1878 const char *cached_name;
1879#endif
1880
1881 if (file == 0)
1882 set_list = current_variable_set_list;
1883 else
1884 set_list = file->variables;
1885
1886#ifndef CONFIG_WITH_STRCACHE2
1887 hash_init (&table, VARIABLE_BUCKETS,
1888 variable_hash_1, variable_hash_2, variable_hash_cmp);
1889#else /* CONFIG_WITH_STRCACHE2 */
1890 hash_init_strcached (&table, VARIABLE_BUCKETS,
1891 &variable_strcache, offsetof (struct variable, name));
1892#endif /* CONFIG_WITH_STRCACHE2 */
1893
1894 /* Run through all the variable sets in the list,
1895 accumulating variables in TABLE. */
1896 for (s = set_list; s != 0; s = s->next)
1897 {
1898 struct variable_set *set = s->set;
1899 v_slot = (struct variable **) set->table.ht_vec;
1900 v_end = v_slot + set->table.ht_size;
1901 for ( ; v_slot < v_end; v_slot++)
1902 if (! HASH_VACANT (*v_slot))
1903 {
1904 struct variable **new_slot;
1905 struct variable *v = *v_slot;
1906
1907 /* If this is a per-target variable and it hasn't been touched
1908 already then look up the global version and take its export
1909 value. */
1910 if (v->per_target && v->export == v_default)
1911 {
1912 struct variable *gv;
1913
1914#ifndef CONFIG_WITH_VALUE_LENGTH
1915 gv = lookup_variable_in_set (v->name, strlen(v->name),
1916 &global_variable_set);
1917#else
1918 assert ((int)strlen(v->name) == v->length);
1919 gv = lookup_variable_in_set (v->name, v->length,
1920 &global_variable_set);
1921#endif
1922 if (gv)
1923 v->export = gv->export;
1924 }
1925
1926 switch (v->export)
1927 {
1928 case v_default:
1929 if (v->origin == o_default || v->origin == o_automatic)
1930 /* Only export default variables by explicit request. */
1931 continue;
1932
1933 /* The variable doesn't have a name that can be exported. */
1934 if (! v->exportable)
1935 continue;
1936
1937 if (! export_all_variables
1938 && v->origin != o_command
1939 && v->origin != o_env && v->origin != o_env_override)
1940 continue;
1941 break;
1942
1943 case v_export:
1944 break;
1945
1946 case v_noexport:
1947 {
1948 /* If this is the SHELL variable and it's not exported,
1949 then add the value from our original environment, if
1950 the original environment defined a value for SHELL. */
1951 extern struct variable shell_var;
1952 if (streq (v->name, "SHELL") && shell_var.value)
1953 {
1954 v = &shell_var;
1955 break;
1956 }
1957 continue;
1958 }
1959
1960 case v_ifset:
1961 if (v->origin == o_default)
1962 continue;
1963 break;
1964 }
1965
1966#ifndef CONFIG_WITH_STRCACHE2
1967 new_slot = (struct variable **) hash_find_slot (&table, v);
1968#else /* CONFIG_WITH_STRCACHE2 */
1969 assert (strcache2_is_cached (&variable_strcache, v->name));
1970 new_slot = (struct variable **) hash_find_slot_strcached (&table, v);
1971#endif /* CONFIG_WITH_STRCACHE2 */
1972 if (HASH_VACANT (*new_slot))
1973 hash_insert_at (&table, v, new_slot);
1974 }
1975 }
1976
1977#ifndef CONFIG_WITH_STRCACHE2
1978 makelevel_key.name = MAKELEVEL_NAME;
1979 makelevel_key.length = MAKELEVEL_LENGTH;
1980 hash_delete (&table, &makelevel_key);
1981#else /* CONFIG_WITH_STRCACHE2 */
1982 /* lookup the name in the string case, if it's not there it won't
1983 be in any of the sets either. */
1984 cached_name = strcache2_lookup (&variable_strcache,
1985 MAKELEVEL_NAME, MAKELEVEL_LENGTH);
1986 if (cached_name)
1987 {
1988 makelevel_key.name = cached_name;
1989 makelevel_key.length = MAKELEVEL_LENGTH;
1990 hash_delete_strcached (&table, &makelevel_key);
1991 }
1992#endif /* CONFIG_WITH_STRCACHE2 */
1993
1994 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
1995
1996 v_slot = (struct variable **) table.ht_vec;
1997 v_end = v_slot + table.ht_size;
1998 for ( ; v_slot < v_end; v_slot++)
1999 if (! HASH_VACANT (*v_slot))
2000 {
2001 struct variable *v = *v_slot;
2002
2003 /* If V is recursively expanded and didn't come from the environment,
2004 expand its value. If it came from the environment, it should
2005 go back into the environment unchanged. */
2006 if (v->recursive
2007 && v->origin != o_env && v->origin != o_env_override)
2008 {
2009#ifndef CONFIG_WITH_VALUE_LENGTH
2010 char *value = recursively_expand_for_file (v, file);
2011#else
2012 char *value = recursively_expand_for_file (v, file, NULL);
2013#endif
2014#ifdef WINDOWS32
2015 if (strcmp(v->name, "Path") == 0 ||
2016 strcmp(v->name, "PATH") == 0)
2017 convert_Path_to_windows32(value, ';');
2018#endif
2019 *result++ = xstrdup (concat (3, v->name, "=", value));
2020 free (value);
2021 }
2022 else
2023 {
2024#ifdef WINDOWS32
2025 if (strcmp(v->name, "Path") == 0 ||
2026 strcmp(v->name, "PATH") == 0)
2027 convert_Path_to_windows32(v->value, ';');
2028#endif
2029 *result++ = xstrdup (concat (3, v->name, "=", v->value));
2030 }
2031 }
2032
2033 *result = xmalloc (100);
2034 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
2035 *++result = 0;
2036
2037 hash_free (&table, 0);
2038
2039 return result_0;
2040}
2041
2042
2043#ifdef CONFIG_WITH_VALUE_LENGTH
2044/* Worker function for do_variable_definition_append() and
2045 append_expanded_string_to_variable().
2046 The APPEND argument indicates whether it's an append or prepend operation. */
2047void append_string_to_variable (struct variable *v, const char *value, unsigned int value_len, int append)
2048{
2049 /* The previous definition of the variable was recursive.
2050 The new value is the unexpanded old and new values. */
2051 unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
2052 int done_1st_prepend_copy = 0;
2053#ifdef KMK
2054 assert (!v->alias);
2055#endif
2056
2057 /* Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
2058 if (!value_len)
2059 return;
2060
2061 /* adjust the size. */
2062 if (v->value_alloc_len <= new_value_len + 1)
2063 {
2064 if (v->value_alloc_len < 256)
2065 v->value_alloc_len = 256;
2066 else
2067 v->value_alloc_len *= 2;
2068 if (v->value_alloc_len < new_value_len + 1)
2069 v->value_alloc_len = VAR_ALIGN_VALUE_ALLOC (new_value_len + 1 + value_len /*future*/ );
2070# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2071 if ((append || !v->value_length) && !v->rdonly_val)
2072# else
2073 if (append || !v->value_length)
2074# endif
2075 v->value = xrealloc (v->value, v->value_alloc_len);
2076 else
2077 {
2078 /* avoid the extra memcpy the xrealloc may have to do */
2079 char *new_buf = xmalloc (v->value_alloc_len);
2080 memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
2081 done_1st_prepend_copy = 1;
2082# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2083 if (v->rdonly_val)
2084 v->rdonly_val = 0;
2085 else
2086# endif
2087 free (v->value);
2088 v->value = new_buf;
2089 }
2090 MAKE_STATS_2(v->reallocs++);
2091 }
2092
2093 /* insert the new bits */
2094 if (v->value_length != 0)
2095 {
2096 if (append)
2097 {
2098 v->value[v->value_length] = ' ';
2099 memcpy (&v->value[v->value_length + 1], value, value_len + 1);
2100 }
2101 else
2102 {
2103 if (!done_1st_prepend_copy)
2104 memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
2105 v->value[value_len] = ' ';
2106 memcpy (v->value, value, value_len);
2107 }
2108 }
2109 else
2110 memcpy (v->value, value, value_len + 1);
2111 v->value_length = new_value_len;
2112}
2113
2114struct variable *
2115do_variable_definition_append (const struct floc *flocp, struct variable *v,
2116 const char *value, unsigned int value_len,
2117 int simple_value, enum variable_origin origin,
2118 int append)
2119{
2120 if (env_overrides && origin == o_env)
2121 origin = o_env_override;
2122
2123 if (env_overrides && v->origin == o_env)
2124 /* V came from in the environment. Since it was defined
2125 before the switches were parsed, it wasn't affected by -e. */
2126 v->origin = o_env_override;
2127
2128 /* A variable of this name is already defined.
2129 If the old definition is from a stronger source
2130 than this one, don't redefine it. */
2131 if ((int) origin < (int) v->origin)
2132 return v;
2133 v->origin = origin;
2134
2135 /* location */
2136 if (flocp != 0)
2137 v->fileinfo = *flocp;
2138
2139 /* The juicy bits, append the specified value to the variable
2140 This is a heavily exercised code path in kBuild. */
2141 if (value_len == ~0U)
2142 value_len = strlen (value);
2143 if (v->recursive || simple_value)
2144 append_string_to_variable (v, value, value_len, append);
2145 else
2146 /* The previous definition of the variable was simple.
2147 The new value comes from the old value, which was expanded
2148 when it was set; and from the expanded new value. */
2149 append_expanded_string_to_variable (v, value, value_len, append);
2150
2151 /* update the variable */
2152 return v;
2153}
2154#endif /* CONFIG_WITH_VALUE_LENGTH */
2155
2156
2157static struct variable *
2158set_special_var (struct variable *var)
2159{
2160 if (streq (var->name, RECIPEPREFIX_NAME))
2161 {
2162 /* The user is resetting the command introduction prefix. This has to
2163 happen immediately, so that subsequent rules are interpreted
2164 properly. */
2165 cmd_prefix = var->value[0]=='\0' ? RECIPEPREFIX_DEFAULT : var->value[0];
2166 }
2167
2168 return var;
2169}
2170
2171
2172/* Given a variable, a value, and a flavor, define the variable.
2173 See the try_variable_definition() function for details on the parameters. */
2174
2175struct variable *
2176#ifndef CONFIG_WITH_VALUE_LENGTH
2177do_variable_definition (const struct floc *flocp, const char *varname,
2178 const char *value, enum variable_origin origin,
2179 enum variable_flavor flavor, int target_var)
2180#else /* CONFIG_WITH_VALUE_LENGTH */
2181do_variable_definition_2 (const struct floc *flocp,
2182 const char *varname, const char *value,
2183 unsigned int value_len, int simple_value,
2184 char *free_value,
2185 enum variable_origin origin,
2186 enum variable_flavor flavor,
2187 int target_var)
2188#endif /* CONFIG_WITH_VALUE_LENGTH */
2189{
2190 const char *p;
2191 char *alloc_value = NULL;
2192 struct variable *v;
2193 int append = 0;
2194 int conditional = 0;
2195 const size_t varname_len = strlen (varname); /* bird */
2196
2197#ifdef CONFIG_WITH_VALUE_LENGTH
2198 if (value_len == ~0U)
2199 value_len = strlen (value);
2200 else
2201 assert (value_len == strlen (value));
2202#endif
2203
2204 /* Calculate the variable's new value in VALUE. */
2205
2206 switch (flavor)
2207 {
2208 default:
2209 case f_bogus:
2210 /* Should not be possible. */
2211 abort ();
2212 case f_simple:
2213 /* A simple variable definition "var := value". Expand the value.
2214 We have to allocate memory since otherwise it'll clobber the
2215 variable buffer, and we may still need that if we're looking at a
2216 target-specific variable. */
2217#ifndef CONFIG_WITH_VALUE_LENGTH
2218 p = alloc_value = allocated_variable_expand (value);
2219#else /* CONFIG_WITH_VALUE_LENGTH */
2220 if (!simple_value)
2221 p = alloc_value = allocated_variable_expand_2 (value, value_len, &value_len);
2222 else
2223 {
2224 if (value_len == ~0U)
2225 value_len = strlen (value);
2226 if (!free_value)
2227 p = alloc_value = xstrndup (value, value_len);
2228 else
2229 {
2230 assert (value == free_value);
2231 p = alloc_value = free_value;
2232 free_value = 0;
2233 }
2234 }
2235#endif /* CONFIG_WITH_VALUE_LENGTH */
2236 break;
2237 case f_conditional:
2238 /* A conditional variable definition "var ?= value".
2239 The value is set IFF the variable is not defined yet. */
2240 v = lookup_variable (varname, varname_len);
2241 if (v)
2242#ifndef CONFIG_WITH_VALUE_LENGTH
2243 return v->special ? set_special_var (v) : v;
2244#else /* CONFIG_WITH_VALUE_LENGTH */
2245 {
2246 if (free_value)
2247 free (free_value);
2248 return v->special ? set_special_var (v) : v;
2249 }
2250#endif /* CONFIG_WITH_VALUE_LENGTH */
2251
2252 conditional = 1;
2253 flavor = f_recursive;
2254 /* FALLTHROUGH */
2255 case f_recursive:
2256 /* A recursive variable definition "var = value".
2257 The value is used verbatim. */
2258 p = value;
2259 break;
2260#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2261 case f_append:
2262 case f_prepend:
2263 {
2264 const enum variable_flavor org_flavor = flavor;
2265#else
2266 case f_append:
2267 {
2268#endif
2269
2270 /* If we have += but we're in a target variable context, we want to
2271 append only with other variables in the context of this target. */
2272 if (target_var)
2273 {
2274 append = 1;
2275 v = lookup_variable_in_set (varname, varname_len,
2276 current_variable_set_list->set);
2277
2278 /* Don't append from the global set if a previous non-appending
2279 target-specific variable definition exists. */
2280 if (v && !v->append)
2281 append = 0;
2282 }
2283#ifdef KMK
2284 else if ( g_pTopKbEvalData
2285 || ( varname_len > 3
2286 && varname[0] == '['
2287 && is_kbuild_object_variable_accessor (varname, varname_len)) )
2288 {
2289 v = kbuild_object_variable_pre_append (varname, varname_len,
2290 value, value_len, simple_value,
2291 origin, org_flavor == f_append, flocp);
2292 if (free_value)
2293 free (free_value);
2294 return v;
2295 }
2296#endif
2297#ifdef CONFIG_WITH_LOCAL_VARIABLES
2298 /* If 'local', restrict it to the current variable context. */
2299 else if (origin == o_local)
2300 v = lookup_variable_in_set (varname, varname_len,
2301 current_variable_set_list->set);
2302#endif
2303 else
2304 v = lookup_variable (varname, varname_len);
2305
2306 if (v == 0)
2307 {
2308 /* There was no old value.
2309 This becomes a normal recursive definition. */
2310 p = value;
2311 flavor = f_recursive;
2312 }
2313 else
2314 {
2315#ifdef CONFIG_WITH_VALUE_LENGTH
2316 v->append = append;
2317 v = do_variable_definition_append (flocp, v, value, value_len,
2318 simple_value, origin,
2319# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2320 org_flavor == f_append);
2321# else
2322 1);
2323# endif
2324 if (free_value)
2325 free (free_value);
2326 MAKE_STATS_2(v->changes++);
2327 return v;
2328#else /* !CONFIG_WITH_VALUE_LENGTH */
2329
2330 /* Paste the old and new values together in VALUE. */
2331
2332 unsigned int oldlen, vallen;
2333 const char *val;
2334 char *tp = NULL;
2335
2336 val = value;
2337 if (v->recursive)
2338 /* The previous definition of the variable was recursive.
2339 The new value is the unexpanded old and new values. */
2340 flavor = f_recursive;
2341 else
2342 /* The previous definition of the variable was simple.
2343 The new value comes from the old value, which was expanded
2344 when it was set; and from the expanded new value. Allocate
2345 memory for the expansion as we may still need the rest of the
2346 buffer if we're looking at a target-specific variable. */
2347 val = tp = allocated_variable_expand (val);
2348
2349 oldlen = strlen (v->value);
2350 vallen = strlen (val);
2351 p = alloc_value = xmalloc (oldlen + 1 + vallen + 1);
2352# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2353 if (org_flavor == f_prepend)
2354 {
2355 memcpy (alloc_value, val, vallen);
2356 alloc_value[oldlen] = ' ';
2357 memcpy (&alloc_value[oldlen + 1], v->value, oldlen + 1);
2358 }
2359 else
2360# endif /* CONFIG_WITH_PREPEND_ASSIGNMENT */
2361 {
2362 memcpy (alloc_value, v->value, oldlen);
2363 alloc_value[oldlen] = ' ';
2364 memcpy (&alloc_value[oldlen + 1], val, vallen + 1);
2365 }
2366
2367 if (tp)
2368 free (tp);
2369#endif /* !CONFIG_WITH_VALUE_LENGTH */
2370 }
2371 }
2372 }
2373
2374#ifdef __MSDOS__
2375 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
2376 non-Unix systems don't conform to this default configuration (in
2377 fact, most of them don't even have `/bin'). On the other hand,
2378 $SHELL in the environment, if set, points to the real pathname of
2379 the shell.
2380 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
2381 the Makefile override $SHELL from the environment. But first, we
2382 look for the basename of the shell in the directory where SHELL=
2383 points, and along the $PATH; if it is found in any of these places,
2384 we define $SHELL to be the actual pathname of the shell. Thus, if
2385 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
2386 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
2387 defining SHELL to be "d:/unix/bash.exe". */
2388 if ((origin == o_file || origin == o_override)
2389 && strcmp (varname, "SHELL") == 0)
2390 {
2391 PATH_VAR (shellpath);
2392 extern char * __dosexec_find_on_path (const char *, char *[], char *);
2393
2394 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
2395 if (__dosexec_find_on_path (p, NULL, shellpath))
2396 {
2397 char *tp;
2398
2399 for (tp = shellpath; *tp; tp++)
2400 if (*tp == '\\')
2401 *tp = '/';
2402
2403 v = define_variable_loc (varname, varname_len,
2404 shellpath, origin, flavor == f_recursive,
2405 flocp);
2406 }
2407 else
2408 {
2409 const char *shellbase, *bslash;
2410 struct variable *pathv = lookup_variable ("PATH", 4);
2411 char *path_string;
2412 char *fake_env[2];
2413 size_t pathlen = 0;
2414
2415 shellbase = strrchr (p, '/');
2416 bslash = strrchr (p, '\\');
2417 if (!shellbase || bslash > shellbase)
2418 shellbase = bslash;
2419 if (!shellbase && p[1] == ':')
2420 shellbase = p + 1;
2421 if (shellbase)
2422 shellbase++;
2423 else
2424 shellbase = p;
2425
2426 /* Search for the basename of the shell (with standard
2427 executable extensions) along the $PATH. */
2428 if (pathv)
2429 pathlen = strlen (pathv->value);
2430 path_string = xmalloc (5 + pathlen + 2 + 1);
2431 /* On MSDOS, current directory is considered as part of $PATH. */
2432 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
2433 fake_env[0] = path_string;
2434 fake_env[1] = 0;
2435 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
2436 {
2437 char *tp;
2438
2439 for (tp = shellpath; *tp; tp++)
2440 if (*tp == '\\')
2441 *tp = '/';
2442
2443 v = define_variable_loc (varname, varname_len,
2444 shellpath, origin,
2445 flavor == f_recursive, flocp);
2446 }
2447 else
2448 v = lookup_variable (varname, varname_len);
2449
2450 free (path_string);
2451 }
2452 }
2453 else
2454#endif /* __MSDOS__ */
2455#ifdef WINDOWS32
2456 if ( varname_len == sizeof("SHELL") - 1 /* bird */
2457 && (origin == o_file || origin == o_override || origin == o_command)
2458 && streq (varname, "SHELL"))
2459 {
2460 extern char *default_shell;
2461
2462 /* Call shell locator function. If it returns TRUE, then
2463 set no_default_sh_exe to indicate sh was found and
2464 set new value for SHELL variable. */
2465
2466 if (find_and_set_default_shell (p))
2467 {
2468 v = define_variable_in_set (varname, varname_len, default_shell,
2469# ifdef CONFIG_WITH_VALUE_LENGTH
2470 ~0U, 1 /* duplicate_value */,
2471# endif
2472 origin, flavor == f_recursive,
2473 (target_var
2474 ? current_variable_set_list->set
2475 : NULL),
2476 flocp);
2477 no_default_sh_exe = 0;
2478 }
2479 else
2480 {
2481 char *tp = alloc_value;
2482
2483 alloc_value = allocated_variable_expand (p);
2484
2485 if (find_and_set_default_shell (alloc_value))
2486 {
2487 v = define_variable_in_set (varname, varname_len, p,
2488#ifdef CONFIG_WITH_VALUE_LENGTH
2489 ~0U, 1 /* duplicate_value */,
2490#endif
2491 origin, flavor == f_recursive,
2492 (target_var
2493 ? current_variable_set_list->set
2494 : NULL),
2495 flocp);
2496 no_default_sh_exe = 0;
2497 }
2498 else
2499 v = lookup_variable (varname, varname_len);
2500
2501 if (tp)
2502 free (tp);
2503 }
2504 }
2505 else
2506#endif
2507
2508 /* If we are defining variables inside an $(eval ...), we might have a
2509 different variable context pushed, not the global context (maybe we're
2510 inside a $(call ...) or something. Since this function is only ever
2511 invoked in places where we want to define globally visible variables,
2512 make sure we define this variable in the global set. */
2513
2514 v = define_variable_in_set (varname, varname_len, p,
2515#ifdef CONFIG_WITH_VALUE_LENGTH
2516 value_len, !alloc_value,
2517#endif
2518 origin, flavor == f_recursive,
2519#ifdef CONFIG_WITH_LOCAL_VARIABLES
2520 (target_var || origin == o_local
2521#else
2522 (target_var
2523#endif
2524 ? current_variable_set_list->set : NULL),
2525 flocp);
2526 v->append = append;
2527 v->conditional = conditional;
2528
2529#ifndef CONFIG_WITH_VALUE_LENGTH
2530 if (alloc_value)
2531 free (alloc_value);
2532#else
2533 if (free_value)
2534 free (free_value);
2535#endif
2536
2537 return v->special ? set_special_var (v) : v;
2538}
2539
2540
2541/* Parse P (a null-terminated string) as a variable definition.
2542
2543 If it is not a variable definition, return NULL.
2544
2545 If it is a variable definition, return a pointer to the char after the
2546 assignment token and set *FLAVOR to the type of variable assignment. */
2547
2548char *
2549parse_variable_definition (const char *p, enum variable_flavor *flavor)
2550{
2551 int wspace = 0;
2552
2553 p = next_token (p);
2554
2555 while (1)
2556 {
2557 int c = *p++;
2558
2559 /* If we find a comment or EOS, it's not a variable definition. */
2560 if (c == '\0' || c == '#')
2561 return NULL;
2562
2563 if (c == '$')
2564 {
2565 /* This begins a variable expansion reference. Make sure we don't
2566 treat chars inside the reference as assignment tokens. */
2567 char closeparen;
2568 int count;
2569 c = *p++;
2570 if (c == '(')
2571 closeparen = ')';
2572 else if (c == '{')
2573 closeparen = '}';
2574 else
2575 /* '$$' or '$X'. Either way, nothing special to do here. */
2576 continue;
2577
2578 /* P now points past the opening paren or brace.
2579 Count parens or braces until it is matched. */
2580 count = 0;
2581 for (; *p != '\0'; ++p)
2582 {
2583 if (*p == c)
2584 ++count;
2585 else if (*p == closeparen && --count < 0)
2586 {
2587 ++p;
2588 break;
2589 }
2590 }
2591 continue;
2592 }
2593
2594 /* If we find whitespace skip it, and remember we found it. */
2595 if (isblank ((unsigned char)c))
2596 {
2597 wspace = 1;
2598 p = next_token (p);
2599 c = *p;
2600 if (c == '\0')
2601 return NULL;
2602 ++p;
2603 }
2604
2605
2606 if (c == '=')
2607 {
2608 *flavor = f_recursive;
2609 return (char *)p;
2610 }
2611
2612 /* Match assignment variants (:=, +=, ?=) */
2613 if (*p == '=')
2614 {
2615 switch (c)
2616 {
2617 case ':':
2618 *flavor = f_simple;
2619 break;
2620 case '+':
2621 *flavor = f_append;
2622 break;
2623#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2624 case '<':
2625 *flavor = f_prepend;
2626 break;
2627#endif
2628 case '?':
2629 *flavor = f_conditional;
2630 break;
2631 default:
2632 /* If we skipped whitespace, non-assignments means no var. */
2633 if (wspace)
2634 return NULL;
2635
2636 /* Might be assignment, or might be $= or #=. Check. */
2637 continue;
2638 }
2639 return (char *)++p;
2640 }
2641 else if (c == ':')
2642 /* A colon other than := is a rule line, not a variable defn. */
2643 return NULL;
2644
2645 /* If we skipped whitespace, non-assignments means no var. */
2646 if (wspace)
2647 return NULL;
2648 }
2649
2650 return (char *)p;
2651}
2652
2653
2654/* Try to interpret LINE (a null-terminated string) as a variable definition.
2655
2656 If LINE was recognized as a variable definition, a pointer to its `struct
2657 variable' is returned. If LINE is not a variable definition, NULL is
2658 returned. */
2659
2660struct variable *
2661assign_variable_definition (struct variable *v, char *line IF_WITH_VALUE_LENGTH_PARAM(char *eos))
2662{
2663 char *beg;
2664 char *end;
2665 enum variable_flavor flavor;
2666#ifndef CONFIG_WITH_VALUE_LENGTH
2667 char *name;
2668#endif
2669
2670 beg = next_token (line);
2671 line = parse_variable_definition (beg, &flavor);
2672 if (!line)
2673 return NULL;
2674
2675 end = line - (flavor == f_recursive ? 1 : 2);
2676 while (end > beg && isblank ((unsigned char)end[-1]))
2677 --end;
2678 line = next_token (line);
2679 v->value = line;
2680 v->flavor = flavor;
2681#ifdef CONFIG_WITH_VALUE_LENGTH
2682 v->value_alloc_len = ~(unsigned int)0;
2683 v->value_length = eos != NULL ? eos - line : -1;
2684 assert (eos == NULL || strchr (line, '\0') == eos);
2685# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2686 v->rdonly_val = 0;
2687# endif
2688#endif
2689
2690 /* Expand the name, so "$(foo)bar = baz" works. */
2691#ifndef CONFIG_WITH_VALUE_LENGTH
2692 name = alloca (end - beg + 1);
2693 memcpy (name, beg, end - beg);
2694 name[end - beg] = '\0';
2695 v->name = allocated_variable_expand (name);
2696#else /* CONFIG_WITH_VALUE_LENGTH */
2697 v->name = allocated_variable_expand_2 (beg, end - beg, NULL);
2698#endif /* CONFIG_WITH_VALUE_LENGTH */
2699
2700 if (v->name[0] == '\0')
2701 fatal (&v->fileinfo, _("empty variable name"));
2702
2703 return v;
2704}
2705
2706
2707/* Try to interpret LINE (a null-terminated string) as a variable definition.
2708
2709 ORIGIN may be o_file, o_override, o_env, o_env_override, o_local,
2710 or o_command specifying that the variable definition comes
2711 from a makefile, an override directive, the environment with
2712 or without the -e switch, or the command line.
2713
2714 See the comments for assign_variable_definition().
2715
2716 If LINE was recognized as a variable definition, a pointer to its `struct
2717 variable' is returned. If LINE is not a variable definition, NULL is
2718 returned. */
2719
2720struct variable *
2721try_variable_definition (const struct floc *flocp, char *line
2722 IF_WITH_VALUE_LENGTH_PARAM(char *eos),
2723 enum variable_origin origin, int target_var)
2724{
2725 struct variable v;
2726 struct variable *vp;
2727
2728 if (flocp != 0)
2729 v.fileinfo = *flocp;
2730 else
2731 v.fileinfo.filenm = 0;
2732
2733#ifndef CONFIG_WITH_VALUE_LENGTH
2734 if (!assign_variable_definition (&v, line))
2735 return 0;
2736
2737 vp = do_variable_definition (flocp, v.name, v.value,
2738 origin, v.flavor, target_var);
2739#else
2740 if (!assign_variable_definition (&v, line, eos))
2741 return 0;
2742
2743 vp = do_variable_definition_2 (flocp, v.name, v.value, v.value_length,
2744 0, NULL, origin, v.flavor, target_var);
2745#endif
2746
2747#ifndef CONFIG_WITH_STRCACHE2
2748 free (v.name);
2749#else
2750 free ((char *)v.name);
2751#endif
2752
2753 return vp;
2754}
2755
2756
2757#ifdef CONFIG_WITH_MAKE_STATS
2758static unsigned long var_stats_changes, var_stats_changed;
2759static unsigned long var_stats_reallocs, var_stats_realloced;
2760static unsigned long var_stats_val_len, var_stats_val_alloc_len;
2761static unsigned long var_stats_val_rdonly_len;
2762#endif
2763
2764/* Print information for variable V, prefixing it with PREFIX. */
2765
2766static void
2767print_variable (const void *item, void *arg)
2768{
2769 const struct variable *v = item;
2770 const char *prefix = arg;
2771 const char *origin;
2772#ifdef KMK
2773 const struct variable *alias = v;
2774 RESOLVE_ALIAS_VARIABLE(v);
2775#endif
2776
2777 switch (v->origin)
2778 {
2779 case o_default:
2780 origin = _("default");
2781 break;
2782 case o_env:
2783 origin = _("environment");
2784 break;
2785 case o_file:
2786 origin = _("makefile");
2787 break;
2788 case o_env_override:
2789 origin = _("environment under -e");
2790 break;
2791 case o_command:
2792 origin = _("command line");
2793 break;
2794 case o_override:
2795 origin = _("`override' directive");
2796 break;
2797 case o_automatic:
2798 origin = _("automatic");
2799 break;
2800#ifdef CONFIG_WITH_LOCAL_VARIABLES
2801 case o_local:
2802 origin = _("`local' directive");
2803 break;
2804#endif
2805 case o_invalid:
2806 default:
2807 abort ();
2808 }
2809 fputs ("# ", stdout);
2810 fputs (origin, stdout);
2811 if (v->private_var)
2812 fputs (" private", stdout);
2813#ifndef KMK
2814 if (v->fileinfo.filenm)
2815 printf (_(" (from `%s', line %lu)"),
2816 v->fileinfo.filenm, v->fileinfo.lineno);
2817#else /* KMK */
2818 if (alias->fileinfo.filenm)
2819 printf (_(" (from '%s', line %lu)"),
2820 alias->fileinfo.filenm, alias->fileinfo.lineno);
2821 if (alias->aliased)
2822 fputs (" aliased", stdout);
2823 if (alias->alias)
2824 printf (_(", alias for '%s'"), v->name);
2825#endif /* KMK */
2826
2827#ifdef CONFIG_WITH_MAKE_STATS
2828 if (v->changes != 0)
2829 printf (_(", %u changes"), v->changes);
2830 var_stats_changes += v->changes;
2831 var_stats_changed += (v->changes != 0);
2832 if (v->reallocs != 0)
2833 printf (_(", %u reallocs"), v->reallocs);
2834 var_stats_reallocs += v->reallocs;
2835 var_stats_realloced += (v->reallocs != 0);
2836 var_stats_val_len += v->value_length;
2837 if (v->value_alloc_len)
2838 var_stats_val_alloc_len += v->value_alloc_len;
2839 else
2840 var_stats_val_rdonly_len += v->value_length;
2841 assert (v->value_length == strlen (v->value));
2842 /*assert (v->rdonly_val ? !v->value_alloc_len : v->value_alloc_len > v->value_length); - FIXME */
2843#endif /* CONFIG_WITH_MAKE_STATS */
2844 putchar ('\n');
2845 fputs (prefix, stdout);
2846
2847 /* Is this a `define'? */
2848 if (v->recursive && strchr (v->value, '\n') != 0)
2849#ifndef KMK /** @todo language feature for aliases */
2850 printf ("define %s\n%s\nendef\n", v->name, v->value);
2851#else
2852 printf ("define %s\n%s\nendef\n", alias->name, v->value);
2853#endif
2854 else
2855 {
2856 char *p;
2857
2858#ifndef KMK /** @todo language feature for aliases */
2859 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
2860#else
2861 printf ("%s %s= ", alias->name, v->recursive ? v->append ? "+" : "" : ":");
2862#endif
2863
2864 /* Check if the value is just whitespace. */
2865 p = next_token (v->value);
2866 if (p != v->value && *p == '\0')
2867 /* All whitespace. */
2868 printf ("$(subst ,,%s)", v->value);
2869 else if (v->recursive)
2870 fputs (v->value, stdout);
2871 else
2872 /* Double up dollar signs. */
2873 for (p = v->value; *p != '\0'; ++p)
2874 {
2875 if (*p == '$')
2876 putchar ('$');
2877 putchar (*p);
2878 }
2879 putchar ('\n');
2880 }
2881}
2882
2883
2884/* Print all the variables in SET. PREFIX is printed before
2885 the actual variable definitions (everything else is comments). */
2886
2887void
2888print_variable_set (struct variable_set *set, char *prefix)
2889{
2890#ifdef CONFIG_WITH_MAKE_STATS
2891 var_stats_changes = var_stats_changed = var_stats_reallocs
2892 = var_stats_realloced = var_stats_val_len = var_stats_val_alloc_len
2893 = var_stats_val_rdonly_len = 0;
2894
2895 hash_map_arg (&set->table, print_variable, prefix);
2896
2897 if (set->table.ht_fill)
2898 {
2899 unsigned long fragmentation;
2900
2901 fragmentation = var_stats_val_alloc_len - (var_stats_val_len - var_stats_val_rdonly_len);
2902 printf(_("# variable set value stats:\n\
2903# strings %7lu bytes, readonly %6lu bytes\n"),
2904 var_stats_val_len, var_stats_val_rdonly_len);
2905
2906 if (var_stats_val_alloc_len)
2907 printf(_("# allocated %7lu bytes, fragmentation %6lu bytes (%u%%)\n"),
2908 var_stats_val_alloc_len, fragmentation,
2909 (unsigned int)((100.0 * fragmentation) / var_stats_val_alloc_len));
2910
2911 if (var_stats_changed)
2912 printf(_("# changed %5lu (%2u%%), changes %6lu\n"),
2913 var_stats_changed,
2914 (unsigned int)((100.0 * var_stats_changed) / set->table.ht_fill),
2915 var_stats_changes);
2916
2917 if (var_stats_realloced)
2918 printf(_("# reallocated %5lu (%2u%%), reallocations %6lu\n"),
2919 var_stats_realloced,
2920 (unsigned int)((100.0 * var_stats_realloced) / set->table.ht_fill),
2921 var_stats_reallocs);
2922 }
2923#else
2924 hash_map_arg (&set->table, print_variable, prefix);
2925#endif
2926
2927 fputs (_("# variable set hash-table stats:\n"), stdout);
2928 fputs ("# ", stdout);
2929 hash_print_stats (&set->table, stdout);
2930 putc ('\n', stdout);
2931}
2932
2933/* Print the data base of variables. */
2934
2935void
2936print_variable_data_base (void)
2937{
2938 puts (_("\n# Variables\n"));
2939
2940 print_variable_set (&global_variable_set, "");
2941
2942 puts (_("\n# Pattern-specific Variable Values"));
2943
2944 {
2945 struct pattern_var *p;
2946 int rules = 0;
2947
2948 for (p = pattern_vars; p != 0; p = p->next)
2949 {
2950 ++rules;
2951 printf ("\n%s :\n", p->target);
2952 print_variable (&p->variable, "# ");
2953 }
2954
2955 if (rules == 0)
2956 puts (_("\n# No pattern-specific variable values."));
2957 else
2958 printf (_("\n# %u pattern-specific variable values"), rules);
2959 }
2960
2961#ifdef CONFIG_WITH_STRCACHE2
2962 strcache2_print_stats (&variable_strcache, "# ");
2963#endif
2964}
2965
2966#ifdef CONFIG_WITH_PRINT_STATS_SWITCH
2967void
2968print_variable_stats (void)
2969{
2970 fputs (_("\n# Global variable hash-table stats:\n# "), stdout);
2971 hash_print_stats (&global_variable_set.table, stdout);
2972 fputs ("\n", stdout);
2973}
2974#endif
2975
2976/* Print all the local variables of FILE. */
2977
2978void
2979print_file_variables (const struct file *file)
2980{
2981 if (file->variables != 0)
2982 print_variable_set (file->variables->set, "# ");
2983}
2984
2985#ifdef WINDOWS32
2986void
2987sync_Path_environment (void)
2988{
2989 char *path = allocated_variable_expand ("$(PATH)");
2990 static char *environ_path = NULL;
2991
2992 if (!path)
2993 return;
2994
2995 /*
2996 * If done this before, don't leak memory unnecessarily.
2997 * Free the previous entry before allocating new one.
2998 */
2999 if (environ_path)
3000 free (environ_path);
3001
3002 /*
3003 * Create something WINDOWS32 world can grok
3004 */
3005 convert_Path_to_windows32 (path, ';');
3006 environ_path = xstrdup (concat (3, "PATH", "=", path));
3007 putenv (environ_path);
3008 free (path);
3009}
3010#endif
Note: See TracBrowser for help on using the repository browser.