VirtualBox

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

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

Some more variable stats I added the other day (not in release builds).

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

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