VirtualBox

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

Last change on this file since 3394 was 3394, checked in by bird, 5 years ago

kmk: Added version sort function: versort, rversort, versortfiles, rversortfiles, qversortfiles, qrversortfiles. Try get the real host version on windows by using RtlGetVersion.

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