VirtualBox

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

Last change on this file since 3387 was 3319, checked in by bird, 4 years ago

kmk: Added quote* functions for GNU make and bourne shell style quoting/escaping. Also added q* and *file* versions of a bunch of others, to deal with quoted input and output in a flexible manner.

  • Property svn:eol-style set to native
File size: 107.3 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# ifdef WINDOWS32
1609 OSVERSIONINFOEX oix;
1610# else
1611 struct utsname uts;
1612# endif
1613 unsigned long ulMajor = 0, ulMinor = 0, ulPatch = 0, ul4th = 0;
1614#endif
1615
1616 sprintf (buf, "%u", makelevel);
1617 define_variable_cname (MAKELEVEL_NAME, buf, o_env, 0);
1618
1619 sprintf (buf, "%s%s%s",
1620 version_string,
1621 (remote_description == 0 || remote_description[0] == '\0')
1622 ? "" : "-",
1623 (remote_description == 0 || remote_description[0] == '\0')
1624 ? "" : remote_description);
1625#ifndef KMK
1626 define_variable_cname ("MAKE_VERSION", buf, o_default, 0);
1627 define_variable_cname ("MAKE_HOST", make_host, o_default, 0);
1628#else /* KMK */
1629
1630 /* Define KMK_VERSION to indicate kMk. */
1631 define_variable_cname ("KMK_VERSION", buf, o_default, 0);
1632
1633 /* Define KBUILD_VERSION* */
1634 sprintf (buf, "%d", KBUILD_VERSION_MAJOR);
1635 define_variable_cname ("KBUILD_VERSION_MAJOR", buf, o_default, 0);
1636 sprintf (buf, "%d", KBUILD_VERSION_MINOR);
1637 define_variable_cname ("KBUILD_VERSION_MINOR", buf, o_default, 0);
1638 sprintf (buf, "%d", KBUILD_VERSION_PATCH);
1639 define_variable_cname ("KBUILD_VERSION_PATCH", buf, o_default, 0);
1640 sprintf (buf, "%d", KBUILD_SVN_REV);
1641 define_variable_cname ("KBUILD_KMK_REVISION", buf, o_default, 0);
1642
1643 sprintf (buf, "%d.%d.%d-r%d", KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR,
1644 KBUILD_VERSION_PATCH, KBUILD_SVN_REV);
1645 define_variable_cname ("KBUILD_VERSION", buf, o_default, 0);
1646
1647 /* The host defaults. The BUILD_* stuff will be replaced by KBUILD_* soon. */
1648 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST"));
1649 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM"));
1650 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST;
1651 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1652 OS (error, NULL, _("KBUILD_HOST and BUILD_PLATFORM differs, using KBUILD_HOST=%s."), val);
1653 if (!envvar1)
1654 define_variable_cname ("KBUILD_HOST", val, o_default, 0);
1655 if (!envvar2)
1656 define_variable_cname ("BUILD_PLATFORM", val, o_default, 0);
1657
1658 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_ARCH"));
1659 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_ARCH"));
1660 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_ARCH;
1661 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1662 OS (error, NULL, _("KBUILD_HOST_ARCH and BUILD_PLATFORM_ARCH differs, using KBUILD_HOST_ARCH=%s."), val);
1663 if (!envvar1)
1664 define_variable_cname ("KBUILD_HOST_ARCH", val, o_default, 0);
1665 if (!envvar2)
1666 define_variable_cname ("BUILD_PLATFORM_ARCH", val, o_default, 0);
1667
1668 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_CPU"));
1669 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_CPU"));
1670 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_CPU;
1671 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1672 OS (error, NULL, _("KBUILD_HOST_CPU and BUILD_PLATFORM_CPU differs, using KBUILD_HOST_CPU=%s."), val);
1673 if (!envvar1)
1674 define_variable_cname ("KBUILD_HOST_CPU", val, o_default, 0);
1675 if (!envvar2)
1676 define_variable_cname ("BUILD_PLATFORM_CPU", val, o_default, 0);
1677
1678 /* The host kernel version. */
1679#if defined(WINDOWS32)
1680 memset (&oix, '\0', sizeof (oix));
1681 oix.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1682 if (!GetVersionEx ((LPOSVERSIONINFO)&oix))
1683 {
1684 memset (&oix, '\0', sizeof (oix));
1685 oix.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
1686 GetVersionEx ((LPOSVERSIONINFO)&oix);
1687 }
1688 if (oix.dwPlatformId == VER_PLATFORM_WIN32_NT)
1689 {
1690 ulMajor = oix.dwMajorVersion;
1691 ulMinor = oix.dwMinorVersion;
1692 ulPatch = oix.wServicePackMajor;
1693 ul4th = oix.wServicePackMinor;
1694 }
1695 else
1696 {
1697 ulMajor = oix.dwPlatformId == 1 ? 0 /*Win95/98/ME*/
1698 : oix.dwPlatformId == 3 ? 1 /*WinCE*/
1699 : 2; /*??*/
1700 ulMinor = oix.dwMajorVersion;
1701 ulPatch = oix.dwMinorVersion;
1702 ul4th = oix.wServicePackMajor;
1703 }
1704#else
1705 memset (&uts, 0, sizeof(uts));
1706 uname (&uts);
1707 val = uts.release;
1708 ulMajor = parse_release_number (&val);
1709 ulMinor = parse_release_number (&val);
1710 ulPatch = parse_release_number (&val);
1711 ul4th = parse_release_number (&val);
1712
1713 define_variable_cname ("KBUILD_HOST_UNAME_SYSNAME", uts.sysname, o_default, 0);
1714 define_variable_cname ("KBUILD_HOST_UNAME_RELEASE", uts.release, o_default, 0);
1715 define_variable_cname ("KBUILD_HOST_UNAME_VERSION", uts.version, o_default, 0);
1716 define_variable_cname ("KBUILD_HOST_UNAME_MACHINE", uts.machine, o_default, 0);
1717 define_variable_cname ("KBUILD_HOST_UNAME_NODENAME", uts.nodename, o_default, 0);
1718#endif
1719
1720 sprintf (buf, "%lu.%lu.%lu.%lu", ulMajor, ulMinor, ulPatch, ul4th);
1721 define_variable_cname ("KBUILD_HOST_VERSION", buf, o_default, 0);
1722
1723 sprintf (buf, "%lu", ulMajor);
1724 define_variable_cname ("KBUILD_HOST_VERSION_MAJOR", buf, o_default, 0);
1725
1726 sprintf (buf, "%lu", ulMinor);
1727 define_variable_cname ("KBUILD_HOST_VERSION_MINOR", buf, o_default, 0);
1728
1729 sprintf (buf, "%lu", ulPatch);
1730 define_variable_cname ("KBUILD_HOST_VERSION_PATCH", buf, o_default, 0);
1731
1732 /* The kBuild locations. */
1733 define_variable_cname ("KBUILD_PATH", get_kbuild_path (), o_default, 0);
1734 define_variable_cname ("KBUILD_BIN_PATH", get_kbuild_bin_path (), o_default, 0);
1735
1736 define_variable_cname ("PATH_KBUILD", get_kbuild_path (), o_default, 0);
1737 define_variable_cname ("PATH_KBUILD_BIN", get_kbuild_bin_path (), o_default, 0);
1738
1739 /* Define KMK_FEATURES to indicate various working KMK features. */
1740# if defined (CONFIG_WITH_RSORT) \
1741 && defined (CONFIG_WITH_ABSPATHEX) \
1742 && defined (CONFIG_WITH_TOUPPER_TOLOWER) \
1743 && defined (CONFIG_WITH_DEFINED) \
1744 && defined (CONFIG_WITH_VALUE_LENGTH) \
1745 && defined (CONFIG_WITH_COMPARE) \
1746 && defined (CONFIG_WITH_STACK) \
1747 && defined (CONFIG_WITH_MATH) \
1748 && defined (CONFIG_WITH_XARGS) \
1749 && defined (CONFIG_WITH_EXPLICIT_MULTITARGET) \
1750 && defined (CONFIG_WITH_DOT_MUST_MAKE) \
1751 && defined (CONFIG_WITH_PREPEND_ASSIGNMENT) \
1752 && defined (CONFIG_WITH_SET_CONDITIONALS) \
1753 && defined (CONFIG_WITH_DATE) \
1754 && defined (CONFIG_WITH_FILE_SIZE) \
1755 && defined (CONFIG_WITH_WHERE_FUNCTION) \
1756 && defined (CONFIG_WITH_WHICH) \
1757 && defined (CONFIG_WITH_EVALPLUS) \
1758 && (defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)) \
1759 && defined (CONFIG_WITH_COMMANDS_FUNC) \
1760 && defined (CONFIG_WITH_PRINTF) \
1761 && defined (CONFIG_WITH_LOOP_FUNCTIONS) \
1762 && defined (CONFIG_WITH_ROOT_FUNC) \
1763 && defined (CONFIG_WITH_STRING_FUNCTIONS) \
1764 && defined (CONFIG_WITH_DEFINED_FUNCTIONS) \
1765 && defined (KMK_HELPERS)
1766 define_variable_cname ("KMK_FEATURES",
1767 "append-dash-n abspath includedep-queue install-hard-linking umask quote"
1768 " kBuild-define"
1769 " rsort"
1770 " abspathex"
1771 " toupper tolower"
1772 " defined"
1773 " comp-vars comp-cmds comp-cmds-ex"
1774 " stack"
1775 " math-int"
1776 " xargs"
1777 " explicit-multitarget"
1778 " dot-must-make"
1779 " prepend-assignment"
1780 " set-conditionals intersects"
1781 " date"
1782 " file-size"
1783 " expr if-expr select"
1784 " where"
1785 " which"
1786 " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var"
1787 " make-stats"
1788 " commands"
1789 " printf"
1790 " for while"
1791 " root"
1792 " length insert pos lastpos substr translate"
1793 " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one kb-exp-tmpl"
1794 " firstdefined lastdefined"
1795 , o_default, 0);
1796# else /* MSC can't deal with strings mixed with #if/#endif, thus the slow way. */
1797# error "All features should be enabled by default!"
1798 strcpy (buf, "append-dash-n abspath includedep-queue install-hard-linking umask quote"
1799 " kBuild-define");
1800# if defined (CONFIG_WITH_RSORT)
1801 strcat (buf, " rsort");
1802# endif
1803# if defined (CONFIG_WITH_ABSPATHEX)
1804 strcat (buf, " abspathex");
1805# endif
1806# if defined (CONFIG_WITH_TOUPPER_TOLOWER)
1807 strcat (buf, " toupper tolower");
1808# endif
1809# if defined (CONFIG_WITH_DEFINED)
1810 strcat (buf, " defined");
1811# endif
1812# if defined (CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
1813 strcat (buf, " comp-vars comp-cmds comp-cmds-ex");
1814# endif
1815# if defined (CONFIG_WITH_STACK)
1816 strcat (buf, " stack");
1817# endif
1818# if defined (CONFIG_WITH_MATH)
1819 strcat (buf, " math-int");
1820# endif
1821# if defined (CONFIG_WITH_XARGS)
1822 strcat (buf, " xargs");
1823# endif
1824# if defined (CONFIG_WITH_EXPLICIT_MULTITARGET)
1825 strcat (buf, " explicit-multitarget");
1826# endif
1827# if defined (CONFIG_WITH_DOT_MUST_MAKE)
1828 strcat (buf, " dot-must-make");
1829# endif
1830# if defined (CONFIG_WITH_PREPEND_ASSIGNMENT)
1831 strcat (buf, " prepend-assignment");
1832# endif
1833# if defined (CONFIG_WITH_SET_CONDITIONALS)
1834 strcat (buf, " set-conditionals intersects");
1835# endif
1836# if defined (CONFIG_WITH_DATE)
1837 strcat (buf, " date");
1838# endif
1839# if defined (CONFIG_WITH_FILE_SIZE)
1840 strcat (buf, " file-size");
1841# endif
1842# if defined (CONFIG_WITH_IF_CONDITIONALS)
1843 strcat (buf, " expr if-expr select");
1844# endif
1845# if defined (CONFIG_WITH_WHERE_FUNCTION)
1846 strcat (buf, " where");
1847# endif
1848# if defined (CONFIG_WITH_WHICH)
1849 strcat (buf, " which");
1850# endif
1851# if defined (CONFIG_WITH_EVALPLUS)
1852 strcat (buf, " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var");
1853# endif
1854# if defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)
1855 strcat (buf, " make-stats");
1856# endif
1857# if defined (CONFIG_WITH_COMMANDS_FUNC)
1858 strcat (buf, " commands");
1859# endif
1860# if defined (CONFIG_WITH_PRINTF)
1861 strcat (buf, " printf");
1862# endif
1863# if defined (CONFIG_WITH_LOOP_FUNCTIONS)
1864 strcat (buf, " for while");
1865# endif
1866# if defined (CONFIG_WITH_ROOT_FUNC)
1867 strcat (buf, " root");
1868# endif
1869# if defined (CONFIG_WITH_STRING_FUNCTIONS)
1870 strcat (buf, " length insert pos lastpos substr translate");
1871# endif
1872# if defined (CONFIG_WITH_DEFINED_FUNCTIONS)
1873 strcat (buf, " firstdefined lastdefined");
1874# endif
1875# if defined (KMK_HELPERS)
1876 strcat (buf, " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one kb-exp-tmpl");
1877# endif
1878 define_variable_cname ("KMK_FEATURES", buf, o_default, 0);
1879# endif
1880
1881#endif /* KMK */
1882
1883#ifdef CONFIG_WITH_KMK_BUILTIN
1884 /* The supported kMk Builtin commands. */
1885 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);
1886#endif
1887
1888#ifdef __MSDOS__
1889 /* Allow to specify a special shell just for Make,
1890 and use $COMSPEC as the default $SHELL when appropriate. */
1891 {
1892 static char shell_str[] = "SHELL";
1893 const int shlen = sizeof (shell_str) - 1;
1894 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
1895 struct variable *comp = lookup_variable ("COMSPEC", 7);
1896
1897 /* $(MAKESHELL) overrides $(SHELL) even if -e is in effect. */
1898 if (mshp)
1899 (void) define_variable (shell_str, shlen,
1900 mshp->value, o_env_override, 0);
1901 else if (comp)
1902 {
1903 /* $(COMSPEC) shouldn't override $(SHELL). */
1904 struct variable *shp = lookup_variable (shell_str, shlen);
1905
1906 if (!shp)
1907 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
1908 }
1909 }
1910#elif defined(__EMX__)
1911 {
1912 static char shell_str[] = "SHELL";
1913 const int shlen = sizeof (shell_str) - 1;
1914 struct variable *shell = lookup_variable (shell_str, shlen);
1915 struct variable *replace = lookup_variable ("MAKESHELL", 9);
1916
1917 /* if $MAKESHELL is defined in the environment assume o_env_override */
1918 if (replace && *replace->value && replace->origin == o_env)
1919 replace->origin = o_env_override;
1920
1921 /* if $MAKESHELL is not defined use $SHELL but only if the variable
1922 did not come from the environment */
1923 if (!replace || !*replace->value)
1924 if (shell && *shell->value && (shell->origin == o_env
1925 || shell->origin == o_env_override))
1926 {
1927 /* overwrite whatever we got from the environment */
1928 free (shell->value);
1929 shell->value = xstrdup (default_shell);
1930 shell->origin = o_default;
1931 }
1932
1933 /* Some people do not like cmd to be used as the default
1934 if $SHELL is not defined in the Makefile.
1935 With -DNO_CMD_DEFAULT you can turn off this behaviour */
1936# ifndef NO_CMD_DEFAULT
1937 /* otherwise use $COMSPEC */
1938 if (!replace || !*replace->value)
1939 replace = lookup_variable ("COMSPEC", 7);
1940
1941 /* otherwise use $OS2_SHELL */
1942 if (!replace || !*replace->value)
1943 replace = lookup_variable ("OS2_SHELL", 9);
1944# else
1945# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
1946# endif
1947
1948 if (replace && *replace->value)
1949 /* overwrite $SHELL */
1950 (void) define_variable (shell_str, shlen, replace->value,
1951 replace->origin, 0);
1952 else
1953 /* provide a definition if there is none */
1954 (void) define_variable (shell_str, shlen, default_shell,
1955 o_default, 0);
1956 }
1957
1958#endif
1959
1960 /* This won't override any definition, but it will provide one if there
1961 isn't one there. */
1962 v = define_variable_cname ("SHELL", default_shell, o_default, 0);
1963#ifdef __MSDOS__
1964 v->export = v_export; /* Export always SHELL. */
1965#endif
1966
1967 /* On MSDOS we do use SHELL from environment, since it isn't a standard
1968 environment variable on MSDOS, so whoever sets it, does that on purpose.
1969 On OS/2 we do not use SHELL from environment but we have already handled
1970 that problem above. */
1971#if !defined(__MSDOS__) && !defined(__EMX__)
1972 /* Don't let SHELL come from the environment. */
1973 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
1974 {
1975# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1976 if (v->rdonly_val)
1977 v->rdonly_val = 0;
1978 else
1979# endif
1980 free (v->value);
1981 v->origin = o_file;
1982 v->value = xstrdup (default_shell);
1983# ifdef CONFIG_WITH_VALUE_LENGTH
1984 v->value_length = strlen (v->value);
1985 v->value_alloc_len = v->value_length + 1;
1986# endif
1987 }
1988#endif
1989
1990 /* Make sure MAKEFILES gets exported if it is set. */
1991 v = define_variable_cname ("MAKEFILES", "", o_default, 0);
1992 v->export = v_ifset;
1993
1994 /* Define the magic D and F variables in terms of
1995 the automatic variables they are variations of. */
1996
1997#if defined(__MSDOS__) || defined(WINDOWS32)
1998 /* For consistency, remove the trailing backslash as well as slash. */
1999 define_variable_cname ("@D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $@)))",
2000 o_automatic, 1);
2001 define_variable_cname ("%D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $%)))",
2002 o_automatic, 1);
2003 define_variable_cname ("*D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $*)))",
2004 o_automatic, 1);
2005 define_variable_cname ("<D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $<)))",
2006 o_automatic, 1);
2007 define_variable_cname ("?D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $?)))",
2008 o_automatic, 1);
2009 define_variable_cname ("^D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $^)))",
2010 o_automatic, 1);
2011 define_variable_cname ("+D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $+)))",
2012 o_automatic, 1);
2013#else /* not __MSDOS__, not WINDOWS32 */
2014 define_variable_cname ("@D", "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
2015 define_variable_cname ("%D", "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
2016 define_variable_cname ("*D", "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
2017 define_variable_cname ("<D", "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
2018 define_variable_cname ("?D", "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
2019 define_variable_cname ("^D", "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
2020 define_variable_cname ("+D", "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
2021#endif
2022 define_variable_cname ("@F", "$(notdir $@)", o_automatic, 1);
2023 define_variable_cname ("%F", "$(notdir $%)", o_automatic, 1);
2024 define_variable_cname ("*F", "$(notdir $*)", o_automatic, 1);
2025 define_variable_cname ("<F", "$(notdir $<)", o_automatic, 1);
2026 define_variable_cname ("?F", "$(notdir $?)", o_automatic, 1);
2027 define_variable_cname ("^F", "$(notdir $^)", o_automatic, 1);
2028 define_variable_cname ("+F", "$(notdir $+)", o_automatic, 1);
2029#ifdef CONFIG_WITH_LAZY_DEPS_VARS
2030 define_variable ("^", 1, "$(deps $@)", o_automatic, 1);
2031 define_variable ("+", 1, "$(deps-all $@)", o_automatic, 1);
2032 define_variable ("?", 1, "$(deps-newer $@)", o_automatic, 1);
2033 define_variable ("|", 1, "$(deps-oo $@)", o_automatic, 1);
2034#endif /* CONFIG_WITH_LAZY_DEPS_VARS */
2035}
2036
2037
2038int export_all_variables;
2039
2040#ifdef KMK
2041/* Cached table containing the exports of the global_variable_set. When
2042 there are many global variables, it can be so expensive to construct the
2043 child environment that we have a majority of job slot idle. */
2044static size_t global_variable_set_exports_generation = ~(size_t)0;
2045static struct hash_table global_variable_set_exports;
2046
2047static void update_global_variable_set_exports(void)
2048{
2049 struct variable **v_slot;
2050 struct variable **v_end;
2051
2052 /* Re-initialize the table. */
2053 if (global_variable_set_exports_generation != ~(size_t)0)
2054 hash_free (&global_variable_set_exports, 0);
2055 hash_init_strcached (&global_variable_set_exports, ENVIRONMENT_VARIABLE_BUCKETS,
2056 &variable_strcache, offsetof (struct variable, name));
2057
2058 /* do pretty much the same as target_environment. */
2059 v_slot = (struct variable **) global_variable_set.table.ht_vec;
2060 v_end = v_slot + global_variable_set.table.ht_size;
2061 for ( ; v_slot < v_end; v_slot++)
2062 if (! HASH_VACANT (*v_slot))
2063 {
2064 struct variable **new_slot;
2065 struct variable *v = *v_slot;
2066
2067 switch (v->export)
2068 {
2069 case v_default:
2070 if (v->origin == o_default || v->origin == o_automatic)
2071 /* Only export default variables by explicit request. */
2072 continue;
2073
2074 /* The variable doesn't have a name that can be exported. */
2075 if (! v->exportable)
2076 continue;
2077
2078 if (! export_all_variables
2079 && v->origin != o_command
2080 && v->origin != o_env && v->origin != o_env_override)
2081 continue;
2082 break;
2083
2084 case v_export:
2085 break;
2086
2087 case v_noexport:
2088 {
2089 /* If this is the SHELL variable and it's not exported,
2090 then add the value from our original environment, if
2091 the original environment defined a value for SHELL. */
2092 extern struct variable shell_var;
2093 if (streq (v->name, "SHELL") && shell_var.value)
2094 {
2095 v = &shell_var;
2096 break;
2097 }
2098 continue;
2099 }
2100
2101 case v_ifset:
2102 if (v->origin == o_default)
2103 continue;
2104 break;
2105 }
2106
2107 assert (strcache2_is_cached (&variable_strcache, v->name));
2108 new_slot = (struct variable **) hash_find_slot_strcached (&global_variable_set_exports, v);
2109 if (HASH_VACANT (*new_slot))
2110 hash_insert_at (&global_variable_set_exports, v, new_slot);
2111 }
2112
2113 /* done */
2114 global_variable_set_exports_generation = global_variable_generation;
2115}
2116
2117#endif
2118
2119/* Create a new environment for FILE's commands.
2120 If FILE is nil, this is for the 'shell' function.
2121 The child's MAKELEVEL variable is incremented. */
2122
2123char **
2124target_environment (struct file *file)
2125{
2126 struct variable_set_list *set_list;
2127 register struct variable_set_list *s;
2128 struct hash_table table;
2129 struct variable **v_slot;
2130 struct variable **v_end;
2131 struct variable makelevel_key;
2132 char **result_0;
2133 char **result;
2134#ifdef CONFIG_WITH_STRCACHE2
2135 const char *cached_name;
2136#endif
2137
2138#ifdef KMK
2139 if (global_variable_set_exports_generation != global_variable_generation)
2140 update_global_variable_set_exports();
2141#endif
2142
2143 if (file == 0)
2144 set_list = current_variable_set_list;
2145 else
2146 set_list = file->variables;
2147
2148#ifndef CONFIG_WITH_STRCACHE2
2149 hash_init (&table, ENVIRONMENT_VARIABLE_BUCKETS,
2150 variable_hash_1, variable_hash_2, variable_hash_cmp);
2151#else /* CONFIG_WITH_STRCACHE2 */
2152 hash_init_strcached (&table, ENVIRONMENT_VARIABLE_BUCKETS,
2153 &variable_strcache, offsetof (struct variable, name));
2154#endif /* CONFIG_WITH_STRCACHE2 */
2155
2156 /* Run through all the variable sets in the list,
2157 accumulating variables in TABLE. */
2158 for (s = set_list; s != 0; s = s->next)
2159 {
2160 struct variable_set *set = s->set;
2161#ifdef KMK
2162 if (set == &global_variable_set)
2163 {
2164 assert(s->next == NULL);
2165 break;
2166 }
2167#endif
2168 v_slot = (struct variable **) set->table.ht_vec;
2169 v_end = v_slot + set->table.ht_size;
2170 for ( ; v_slot < v_end; v_slot++)
2171 if (! HASH_VACANT (*v_slot))
2172 {
2173 struct variable **new_slot;
2174 struct variable *v = *v_slot;
2175
2176 /* If this is a per-target variable and it hasn't been touched
2177 already then look up the global version and take its export
2178 value. */
2179 if (v->per_target && v->export == v_default)
2180 {
2181 struct variable *gv;
2182
2183#ifndef CONFIG_WITH_VALUE_LENGTH
2184 gv = lookup_variable_in_set (v->name, strlen (v->name),
2185 &global_variable_set);
2186#else
2187 assert ((int)strlen(v->name) == v->length);
2188 gv = lookup_variable_in_set (v->name, v->length,
2189 &global_variable_set);
2190#endif
2191 if (gv)
2192 v->export = gv->export;
2193 }
2194
2195 switch (v->export)
2196 {
2197 case v_default:
2198 if (v->origin == o_default || v->origin == o_automatic)
2199 /* Only export default variables by explicit request. */
2200 continue;
2201
2202 /* The variable doesn't have a name that can be exported. */
2203 if (! v->exportable)
2204 continue;
2205
2206 if (! export_all_variables
2207 && v->origin != o_command
2208 && v->origin != o_env && v->origin != o_env_override)
2209 continue;
2210 break;
2211
2212 case v_export:
2213 break;
2214
2215 case v_noexport:
2216 {
2217 /* If this is the SHELL variable and it's not exported,
2218 then add the value from our original environment, if
2219 the original environment defined a value for SHELL. */
2220 if (streq (v->name, "SHELL") && shell_var.value)
2221 {
2222 v = &shell_var;
2223 break;
2224 }
2225 continue;
2226 }
2227
2228 case v_ifset:
2229 if (v->origin == o_default)
2230 continue;
2231 break;
2232 }
2233
2234#ifndef CONFIG_WITH_STRCACHE2
2235 new_slot = (struct variable **) hash_find_slot (&table, v);
2236#else /* CONFIG_WITH_STRCACHE2 */
2237 assert (strcache2_is_cached (&variable_strcache, v->name));
2238 new_slot = (struct variable **) hash_find_slot_strcached (&table, v);
2239#endif /* CONFIG_WITH_STRCACHE2 */
2240 if (HASH_VACANT (*new_slot))
2241 hash_insert_at (&table, v, new_slot);
2242 }
2243 }
2244
2245#ifdef KMK
2246 /* Add the global exports to table. */
2247 v_slot = (struct variable **) global_variable_set_exports.ht_vec;
2248 v_end = v_slot + global_variable_set_exports.ht_size;
2249 for ( ; v_slot < v_end; v_slot++)
2250 if (! HASH_VACANT (*v_slot))
2251 {
2252 struct variable **new_slot;
2253 struct variable *v = *v_slot;
2254 assert (strcache2_is_cached (&variable_strcache, v->name));
2255 new_slot = (struct variable **) hash_find_slot_strcached (&table, v);
2256 if (HASH_VACANT (*new_slot))
2257 hash_insert_at (&table, v, new_slot);
2258 }
2259#endif
2260
2261#ifndef CONFIG_WITH_STRCACHE2
2262 makelevel_key.name = (char *)MAKELEVEL_NAME;
2263 makelevel_key.length = MAKELEVEL_LENGTH;
2264 hash_delete (&table, &makelevel_key);
2265#else /* CONFIG_WITH_STRCACHE2 */
2266 /* lookup the name in the string case, if it's not there it won't
2267 be in any of the sets either. */
2268 cached_name = strcache2_lookup (&variable_strcache,
2269 MAKELEVEL_NAME, MAKELEVEL_LENGTH);
2270 if (cached_name)
2271 {
2272 makelevel_key.name = cached_name;
2273 makelevel_key.length = MAKELEVEL_LENGTH;
2274 hash_delete_strcached (&table, &makelevel_key);
2275 }
2276#endif /* CONFIG_WITH_STRCACHE2 */
2277
2278 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
2279
2280 v_slot = (struct variable **) table.ht_vec;
2281 v_end = v_slot + table.ht_size;
2282 for ( ; v_slot < v_end; v_slot++)
2283 if (! HASH_VACANT (*v_slot))
2284 {
2285 struct variable *v = *v_slot;
2286
2287 /* If V is recursively expanded and didn't come from the environment,
2288 expand its value. If it came from the environment, it should
2289 go back into the environment unchanged. */
2290 if (v->recursive
2291 && v->origin != o_env && v->origin != o_env_override)
2292 {
2293#ifndef CONFIG_WITH_VALUE_LENGTH
2294 char *value = recursively_expand_for_file (v, file);
2295#else
2296 char *value = recursively_expand_for_file (v, file, NULL);
2297#endif
2298#ifdef WINDOWS32
2299 if (strcmp (v->name, "Path") == 0 ||
2300 strcmp (v->name, "PATH") == 0)
2301 convert_Path_to_windows32 (value, ';');
2302#endif
2303 *result++ = xstrdup (concat (3, v->name, "=", value));
2304 free (value);
2305 }
2306 else
2307 {
2308#ifdef WINDOWS32
2309 if (strcmp (v->name, "Path") == 0 ||
2310 strcmp (v->name, "PATH") == 0)
2311 convert_Path_to_windows32 (v->value, ';');
2312#endif
2313 *result++ = xstrdup (concat (3, v->name, "=", v->value));
2314 }
2315 }
2316
2317 *result = xmalloc (100);
2318 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
2319 *++result = 0;
2320
2321 hash_free (&table, 0);
2322
2323 return result_0;
2324}
2325
2326
2327#ifdef CONFIG_WITH_VALUE_LENGTH
2328/* Worker function for do_variable_definition_append() and
2329 append_expanded_string_to_variable().
2330 The APPEND argument indicates whether it's an append or prepend operation. */
2331void append_string_to_variable (struct variable *v, const char *value, unsigned int value_len, int append)
2332{
2333 /* The previous definition of the variable was recursive.
2334 The new value is the unexpanded old and new values. */
2335 unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
2336 int done_1st_prepend_copy = 0;
2337#ifdef KMK
2338 assert (!v->alias);
2339#endif
2340
2341 /* Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
2342 if (!value_len)
2343 return;
2344
2345 /* adjust the size. */
2346 if (v->value_alloc_len <= new_value_len + 1)
2347 {
2348 if (v->value_alloc_len < 256)
2349 v->value_alloc_len = 256;
2350 else
2351 v->value_alloc_len *= 2;
2352 if (v->value_alloc_len < new_value_len + 1)
2353 v->value_alloc_len = VAR_ALIGN_VALUE_ALLOC (new_value_len + 1 + value_len /*future*/ );
2354# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2355 if ((append || !v->value_length) && !v->rdonly_val)
2356# else
2357 if (append || !v->value_length)
2358# endif
2359 v->value = xrealloc (v->value, v->value_alloc_len);
2360 else
2361 {
2362 /* avoid the extra memcpy the xrealloc may have to do */
2363 char *new_buf = xmalloc (v->value_alloc_len);
2364 memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
2365 done_1st_prepend_copy = 1;
2366# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2367 if (v->rdonly_val)
2368 v->rdonly_val = 0;
2369 else
2370# endif
2371 free (v->value);
2372 v->value = new_buf;
2373 }
2374 MAKE_STATS_2(v->reallocs++);
2375 }
2376
2377 /* insert the new bits */
2378 if (v->value_length != 0)
2379 {
2380 if (append)
2381 {
2382 v->value[v->value_length] = ' ';
2383 memcpy (&v->value[v->value_length + 1], value, value_len + 1);
2384 }
2385 else
2386 {
2387 if (!done_1st_prepend_copy)
2388 memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
2389 v->value[value_len] = ' ';
2390 memcpy (v->value, value, value_len);
2391 }
2392 }
2393 else
2394 memcpy (v->value, value, value_len + 1);
2395 v->value_length = new_value_len;
2396 VARIABLE_CHANGED (v);
2397}
2398
2399struct variable *
2400do_variable_definition_append (const floc *flocp, struct variable *v,
2401 const char *value, unsigned int value_len,
2402 int simple_value, enum variable_origin origin,
2403 int append)
2404{
2405 if (env_overrides && origin == o_env)
2406 origin = o_env_override;
2407
2408 if (env_overrides && v->origin == o_env)
2409 /* V came from in the environment. Since it was defined
2410 before the switches were parsed, it wasn't affected by -e. */
2411 v->origin = o_env_override;
2412
2413 /* A variable of this name is already defined.
2414 If the old definition is from a stronger source
2415 than this one, don't redefine it. */
2416 if ((int) origin < (int) v->origin)
2417 return v;
2418 v->origin = origin;
2419
2420 /* location */
2421 if (flocp != 0)
2422 v->fileinfo = *flocp;
2423
2424 /* The juicy bits, append the specified value to the variable
2425 This is a heavily exercised code path in kBuild. */
2426 if (value_len == ~0U)
2427 value_len = strlen (value);
2428 if (v->recursive || simple_value)
2429 append_string_to_variable (v, value, value_len, append);
2430 else
2431 /* The previous definition of the variable was simple.
2432 The new value comes from the old value, which was expanded
2433 when it was set; and from the expanded new value. */
2434 append_expanded_string_to_variable (v, value, value_len, append);
2435
2436 /* update the variable */
2437 return v;
2438}
2439#endif /* CONFIG_WITH_VALUE_LENGTH */
2440
2441
2442static struct variable *
2443set_special_var (struct variable *var)
2444{
2445 if (streq (var->name, RECIPEPREFIX_NAME))
2446 {
2447 /* The user is resetting the command introduction prefix. This has to
2448 happen immediately, so that subsequent rules are interpreted
2449 properly. */
2450 cmd_prefix = var->value[0]=='\0' ? RECIPEPREFIX_DEFAULT : var->value[0];
2451 }
2452
2453 return var;
2454}
2455
2456
2457/* Given a string, shell-execute it and return a malloc'ed string of the
2458 * result. This removes only ONE newline (if any) at the end, for maximum
2459 * compatibility with the *BSD makes. If it fails, returns NULL. */
2460
2461static char *
2462shell_result (const char *p)
2463{
2464 char *buf;
2465 unsigned int len;
2466 char *args[2];
2467 char *result;
2468
2469 install_variable_buffer (&buf, &len);
2470
2471 args[0] = (char *) p;
2472 args[1] = NULL;
2473 variable_buffer_output (func_shell_base (variable_buffer, args, 0), "\0", 1);
2474 result = strdup (variable_buffer);
2475
2476 restore_variable_buffer (buf, len);
2477 return result;
2478}
2479
2480
2481/* Given a variable, a value, and a flavor, define the variable.
2482 See the try_variable_definition() function for details on the parameters. */
2483
2484struct variable *
2485#ifndef CONFIG_WITH_VALUE_LENGTH
2486do_variable_definition (const floc *flocp, const char *varname,
2487 const char *value, enum variable_origin origin,
2488 enum variable_flavor flavor, int target_var)
2489#else /* CONFIG_WITH_VALUE_LENGTH */
2490do_variable_definition_2 (const floc *flocp,
2491 const char *varname, const char *value,
2492 unsigned int value_len, int simple_value,
2493 char *free_value,
2494 enum variable_origin origin,
2495 enum variable_flavor flavor,
2496 int target_var)
2497#endif /* CONFIG_WITH_VALUE_LENGTH */
2498{
2499 const char *p;
2500 char *alloc_value = NULL;
2501 struct variable *v;
2502 int append = 0;
2503 int conditional = 0;
2504 const size_t varname_len = strlen (varname); /* bird */
2505
2506#ifdef CONFIG_WITH_VALUE_LENGTH
2507 if (value_len == ~0U)
2508 value_len = strlen (value);
2509 else
2510 assert (value_len == strlen (value));
2511#endif
2512
2513 /* Calculate the variable's new value in VALUE. */
2514
2515 switch (flavor)
2516 {
2517 default:
2518 case f_bogus:
2519 /* Should not be possible. */
2520 abort ();
2521 case f_simple:
2522 /* A simple variable definition "var := value". Expand the value.
2523 We have to allocate memory since otherwise it'll clobber the
2524 variable buffer, and we may still need that if we're looking at a
2525 target-specific variable. */
2526#ifndef CONFIG_WITH_VALUE_LENGTH
2527 p = alloc_value = allocated_variable_expand (value);
2528#else /* CONFIG_WITH_VALUE_LENGTH */
2529 if (!simple_value)
2530 p = alloc_value = allocated_variable_expand_2 (value, value_len, &value_len);
2531 else
2532 {
2533 if (value_len == ~0U)
2534 value_len = strlen (value);
2535 if (!free_value)
2536 p = alloc_value = xstrndup (value, value_len);
2537 else
2538 {
2539 assert (value == free_value);
2540 p = alloc_value = free_value;
2541 free_value = 0;
2542 }
2543 }
2544#endif /* CONFIG_WITH_VALUE_LENGTH */
2545 break;
2546 case f_shell:
2547 {
2548 /* A shell definition "var != value". Expand value, pass it to
2549 the shell, and store the result in recursively-expanded var. */
2550 char *q = allocated_variable_expand (value);
2551 p = alloc_value = shell_result (q);
2552 free (q);
2553 flavor = f_recursive;
2554 break;
2555 }
2556 case f_conditional:
2557 /* A conditional variable definition "var ?= value".
2558 The value is set IFF the variable is not defined yet. */
2559 v = lookup_variable (varname, varname_len);
2560 if (v)
2561#ifndef CONFIG_WITH_VALUE_LENGTH
2562 return v->special ? set_special_var (v) : v;
2563#else /* CONFIG_WITH_VALUE_LENGTH */
2564 {
2565 if (free_value)
2566 free (free_value);
2567 return v->special ? set_special_var (v) : v;
2568 }
2569#endif /* CONFIG_WITH_VALUE_LENGTH */
2570
2571 conditional = 1;
2572 flavor = f_recursive;
2573 /* FALLTHROUGH */
2574 case f_recursive:
2575 /* A recursive variable definition "var = value".
2576 The value is used verbatim. */
2577 p = value;
2578 break;
2579#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2580 case f_append:
2581 case f_prepend:
2582 {
2583 const enum variable_flavor org_flavor = flavor;
2584#else
2585 case f_append:
2586 {
2587#endif
2588
2589 /* If we have += but we're in a target variable context, we want to
2590 append only with other variables in the context of this target. */
2591 if (target_var)
2592 {
2593 append = 1;
2594 v = lookup_variable_in_set (varname, varname_len,
2595 current_variable_set_list->set);
2596
2597 /* Don't append from the global set if a previous non-appending
2598 target-specific variable definition exists. */
2599 if (v && !v->append)
2600 append = 0;
2601 }
2602#ifdef KMK
2603 else if ( g_pTopKbEvalData
2604 || ( varname_len > 3
2605 && varname[0] == '['
2606 && is_kbuild_object_variable_accessor (varname, varname_len)) )
2607 {
2608 v = kbuild_object_variable_pre_append (varname, varname_len,
2609 value, value_len, simple_value,
2610 origin, org_flavor == f_append, flocp);
2611 if (free_value)
2612 free (free_value);
2613 return v;
2614 }
2615#endif
2616#ifdef CONFIG_WITH_LOCAL_VARIABLES
2617 /* If 'local', restrict it to the current variable context. */
2618 else if (origin == o_local)
2619 v = lookup_variable_in_set (varname, varname_len,
2620 current_variable_set_list->set);
2621#endif
2622 else
2623 v = lookup_variable (varname, varname_len);
2624
2625 if (v == 0)
2626 {
2627 /* There was no old value.
2628 This becomes a normal recursive definition. */
2629 p = value;
2630 flavor = f_recursive;
2631 }
2632 else
2633 {
2634#ifdef CONFIG_WITH_VALUE_LENGTH
2635 v->append = append;
2636 v = do_variable_definition_append (flocp, v, value, value_len,
2637 simple_value, origin,
2638# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2639 org_flavor == f_append);
2640# else
2641 1);
2642# endif
2643 if (free_value)
2644 free (free_value);
2645 return v;
2646#else /* !CONFIG_WITH_VALUE_LENGTH */
2647
2648 /* Paste the old and new values together in VALUE. */
2649
2650 unsigned int oldlen, vallen;
2651 const char *val;
2652 char *tp = NULL;
2653
2654 val = value;
2655 if (v->recursive)
2656 /* The previous definition of the variable was recursive.
2657 The new value is the unexpanded old and new values. */
2658 flavor = f_recursive;
2659 else
2660 /* The previous definition of the variable was simple.
2661 The new value comes from the old value, which was expanded
2662 when it was set; and from the expanded new value. Allocate
2663 memory for the expansion as we may still need the rest of the
2664 buffer if we're looking at a target-specific variable. */
2665 val = tp = allocated_variable_expand (val);
2666
2667 oldlen = strlen (v->value);
2668 vallen = strlen (val);
2669 p = alloc_value = xmalloc (oldlen + 1 + vallen + 1);
2670# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2671 if (org_flavor == f_prepend)
2672 {
2673 memcpy (alloc_value, val, vallen);
2674 alloc_value[oldlen] = ' ';
2675 memcpy (&alloc_value[oldlen + 1], v->value, oldlen + 1);
2676 }
2677 else
2678# endif /* CONFIG_WITH_PREPEND_ASSIGNMENT */
2679 {
2680 memcpy (alloc_value, v->value, oldlen);
2681 alloc_value[oldlen] = ' ';
2682 memcpy (&alloc_value[oldlen + 1], val, vallen + 1);
2683 }
2684
2685 free (tp);
2686#endif /* !CONFIG_WITH_VALUE_LENGTH */
2687 }
2688 }
2689 }
2690
2691#ifdef __MSDOS__
2692 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
2693 non-Unix systems don't conform to this default configuration (in
2694 fact, most of them don't even have '/bin'). On the other hand,
2695 $SHELL in the environment, if set, points to the real pathname of
2696 the shell.
2697 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
2698 the Makefile override $SHELL from the environment. But first, we
2699 look for the basename of the shell in the directory where SHELL=
2700 points, and along the $PATH; if it is found in any of these places,
2701 we define $SHELL to be the actual pathname of the shell. Thus, if
2702 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
2703 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
2704 defining SHELL to be "d:/unix/bash.exe". */
2705 if ((origin == o_file || origin == o_override)
2706 && strcmp (varname, "SHELL") == 0)
2707 {
2708 PATH_VAR (shellpath);
2709 extern char * __dosexec_find_on_path (const char *, char *[], char *);
2710
2711 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
2712 if (__dosexec_find_on_path (p, NULL, shellpath))
2713 {
2714 char *tp;
2715
2716 for (tp = shellpath; *tp; tp++)
2717 if (*tp == '\\')
2718 *tp = '/';
2719
2720 v = define_variable_loc (varname, varname_len,
2721 shellpath, origin, flavor == f_recursive,
2722 flocp);
2723 }
2724 else
2725 {
2726 const char *shellbase, *bslash;
2727 struct variable *pathv = lookup_variable ("PATH", 4);
2728 char *path_string;
2729 char *fake_env[2];
2730 size_t pathlen = 0;
2731
2732 shellbase = strrchr (p, '/');
2733 bslash = strrchr (p, '\\');
2734 if (!shellbase || bslash > shellbase)
2735 shellbase = bslash;
2736 if (!shellbase && p[1] == ':')
2737 shellbase = p + 1;
2738 if (shellbase)
2739 shellbase++;
2740 else
2741 shellbase = p;
2742
2743 /* Search for the basename of the shell (with standard
2744 executable extensions) along the $PATH. */
2745 if (pathv)
2746 pathlen = strlen (pathv->value);
2747 path_string = xmalloc (5 + pathlen + 2 + 1);
2748 /* On MSDOS, current directory is considered as part of $PATH. */
2749 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
2750 fake_env[0] = path_string;
2751 fake_env[1] = 0;
2752 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
2753 {
2754 char *tp;
2755
2756 for (tp = shellpath; *tp; tp++)
2757 if (*tp == '\\')
2758 *tp = '/';
2759
2760 v = define_variable_loc (varname, varname_len,
2761 shellpath, origin,
2762 flavor == f_recursive, flocp);
2763 }
2764 else
2765 v = lookup_variable (varname, varname_len);
2766
2767 free (path_string);
2768 }
2769 }
2770 else
2771#endif /* __MSDOS__ */
2772#ifdef WINDOWS32
2773 if ( varname_len == sizeof("SHELL") - 1 /* bird */
2774 && (origin == o_file || origin == o_override || origin == o_command)
2775 && streq (varname, "SHELL"))
2776 {
2777 extern const char *default_shell;
2778
2779 /* Call shell locator function. If it returns TRUE, then
2780 set no_default_sh_exe to indicate sh was found and
2781 set new value for SHELL variable. */
2782
2783 if (find_and_set_default_shell (p))
2784 {
2785 v = define_variable_in_set (varname, varname_len, default_shell,
2786# ifdef CONFIG_WITH_VALUE_LENGTH
2787 ~0U, 1 /* duplicate_value */,
2788# endif
2789 origin, flavor == f_recursive,
2790 (target_var
2791 ? current_variable_set_list->set
2792 : NULL),
2793 flocp);
2794 no_default_sh_exe = 0;
2795 }
2796 else
2797 {
2798 char *tp = alloc_value;
2799
2800 alloc_value = allocated_variable_expand (p);
2801
2802 if (find_and_set_default_shell (alloc_value))
2803 {
2804 v = define_variable_in_set (varname, varname_len, p,
2805#ifdef CONFIG_WITH_VALUE_LENGTH
2806 ~0U, 1 /* duplicate_value */,
2807#endif
2808 origin, flavor == f_recursive,
2809 (target_var
2810 ? current_variable_set_list->set
2811 : NULL),
2812 flocp);
2813 no_default_sh_exe = 0;
2814 }
2815 else
2816 v = lookup_variable (varname, varname_len);
2817
2818 free (tp);
2819 }
2820 }
2821 else
2822#endif
2823
2824 /* If we are defining variables inside an $(eval ...), we might have a
2825 different variable context pushed, not the global context (maybe we're
2826 inside a $(call ...) or something. Since this function is only ever
2827 invoked in places where we want to define globally visible variables,
2828 make sure we define this variable in the global set. */
2829
2830 v = define_variable_in_set (varname, varname_len, p,
2831#ifdef CONFIG_WITH_VALUE_LENGTH
2832 value_len, !alloc_value,
2833#endif
2834 origin, flavor == f_recursive,
2835#ifdef CONFIG_WITH_LOCAL_VARIABLES
2836 (target_var || origin == o_local
2837#else
2838 (target_var
2839#endif
2840 ? current_variable_set_list->set : NULL),
2841 flocp);
2842 v->append = append;
2843 v->conditional = conditional;
2844
2845#ifndef CONFIG_WITH_VALUE_LENGTH
2846 free (alloc_value);
2847#else
2848 if (free_value)
2849 free (free_value);
2850#endif
2851
2852 return v->special ? set_special_var (v) : v;
2853}
2854
2855
2856/* Parse P (a null-terminated string) as a variable definition.
2857
2858 If it is not a variable definition, return NULL and the contents of *VAR
2859 are undefined, except NAME is set to the first non-space character or NIL.
2860
2861 If it is a variable definition, return a pointer to the char after the
2862 assignment token and set the following fields (only) of *VAR:
2863 name : name of the variable (ALWAYS SET) (NOT NUL-TERMINATED!)
2864 length : length of the variable name
2865 value : value of the variable (nul-terminated)
2866 flavor : flavor of the variable
2867 Other values in *VAR are unchanged.
2868 */
2869
2870char *
2871parse_variable_definition (const char *p, struct variable *var)
2872{
2873 int wspace = 0;
2874 const char *e = NULL;
2875
2876/** @todo merge 4.2.1: parse_variable_definition does more now */
2877 NEXT_TOKEN (p);
2878 var->name = (char *)p;
2879 var->length = 0;
2880
2881 while (1)
2882 {
2883 int c = *p++;
2884
2885 /* If we find a comment or EOS, it's not a variable definition. */
2886 if (STOP_SET (c, MAP_COMMENT|MAP_NUL))
2887 return NULL;
2888
2889 if (c == '$')
2890 {
2891 /* This begins a variable expansion reference. Make sure we don't
2892 treat chars inside the reference as assignment tokens. */
2893 char closeparen;
2894 unsigned int count;
2895
2896 c = *p++;
2897 if (c == '(')
2898 closeparen = ')';
2899 else if (c == '{')
2900 closeparen = '}';
2901 else if (c == '\0')
2902 return NULL;
2903 else
2904 /* '$$' or '$X'. Either way, nothing special to do here. */
2905 continue;
2906
2907 /* P now points past the opening paren or brace.
2908 Count parens or braces until it is matched. */
2909 for (count = 1; *p != '\0'; ++p)
2910 {
2911 if (*p == closeparen && --count == 0)
2912 {
2913 ++p;
2914 break;
2915 }
2916 if (*p == c)
2917 ++count;
2918 }
2919 continue;
2920 }
2921
2922 /* If we find whitespace skip it, and remember we found it. */
2923 if (ISBLANK (c))
2924 {
2925 wspace = 1;
2926 e = p - 1;
2927 NEXT_TOKEN (p);
2928 c = *p;
2929 if (c == '\0')
2930 return NULL;
2931 ++p;
2932 }
2933
2934
2935 if (c == '=')
2936 {
2937 var->flavor = f_recursive;
2938 if (! e)
2939 e = p - 1;
2940 break;
2941 }
2942
2943 /* Match assignment variants (:=, +=, ?=, !=) */
2944 if (*p == '=')
2945 {
2946 switch (c)
2947 {
2948 case ':':
2949 var->flavor = f_simple;
2950 break;
2951 case '+':
2952 var->flavor = f_append;
2953 break;
2954#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2955 case '<':
2956 var->flavor = f_prepend;
2957 break;
2958#endif
2959 case '?':
2960 var->flavor = f_conditional;
2961 break;
2962 case '!':
2963 var->flavor = f_shell;
2964 break;
2965 default:
2966 /* If we skipped whitespace, non-assignments means no var. */
2967 if (wspace)
2968 return NULL;
2969
2970 /* Might be assignment, or might be $= or #=. Check. */
2971 continue;
2972 }
2973 if (! e)
2974 e = p - 1;
2975 ++p;
2976 break;
2977 }
2978
2979 /* Check for POSIX ::= syntax */
2980 if (c == ':')
2981 {
2982 /* A colon other than :=/::= is not a variable defn. */
2983 if (*p != ':' || p[1] != '=')
2984 return NULL;
2985
2986 /* POSIX allows ::= to be the same as GNU make's := */
2987 var->flavor = f_simple;
2988 if (! e)
2989 e = p - 1;
2990 p += 2;
2991 break;
2992 }
2993
2994 /* If we skipped whitespace, non-assignments means no var. */
2995 if (wspace)
2996 return NULL;
2997 }
2998
2999 var->length = e - var->name;
3000 var->value = next_token (p);
3001#ifdef CONFIG_WITH_VALUE_LENGTH
3002 var->value_alloc_len = ~(unsigned int)0;
3003 var->value_length = -1;
3004# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
3005 var->rdonly_val = 0;
3006# endif
3007#endif
3008 return (char *)p;
3009}
3010
3011
3012/* Try to interpret LINE (a null-terminated string) as a variable definition.
3013
3014 If LINE was recognized as a variable definition, a pointer to its 'struct
3015 variable' is returned. If LINE is not a variable definition, NULL is
3016 returned. */
3017
3018struct variable *
3019assign_variable_definition (struct variable *v, const char *line IF_WITH_VALUE_LENGTH_PARAM(char *eos))
3020{
3021#ifndef CONFIG_WITH_VALUE_LENGTH
3022 char *name;
3023#endif
3024
3025 if (!parse_variable_definition (line, v))
3026 return NULL;
3027
3028#ifdef CONFIG_WITH_VALUE_LENGTH
3029 if (eos)
3030 {
3031 v->value_length = eos - v->value;
3032 assert (strchr (v->value, '\0') == eos);
3033 }
3034#endif
3035
3036 /* Expand the name, so "$(foo)bar = baz" works. */
3037#ifndef CONFIG_WITH_VALUE_LENGTH
3038 name = alloca (v->length + 1);
3039 memcpy (name, v->name, v->length);
3040 name[v->length] = '\0';
3041 v->name = allocated_variable_expand (name);
3042#else /* CONFIG_WITH_VALUE_LENGTH */
3043 v->name = allocated_variable_expand_2 (v->name, v->length, NULL);
3044#endif /* CONFIG_WITH_VALUE_LENGTH */
3045
3046 if (v->name[0] == '\0')
3047 O (fatal, &v->fileinfo, _("empty variable name"));
3048
3049 return v;
3050}
3051
3052
3053/* Try to interpret LINE (a null-terminated string) as a variable definition.
3054
3055 ORIGIN may be o_file, o_override, o_env, o_env_override, o_local,
3056 or o_command specifying that the variable definition comes
3057 from a makefile, an override directive, the environment with
3058 or without the -e switch, or the command line.
3059
3060 See the comments for assign_variable_definition().
3061
3062 If LINE was recognized as a variable definition, a pointer to its 'struct
3063 variable' is returned. If LINE is not a variable definition, NULL is
3064 returned. */
3065
3066struct variable *
3067try_variable_definition (const floc *flocp, const char *line
3068 IF_WITH_VALUE_LENGTH_PARAM(char *eos),
3069 enum variable_origin origin, int target_var)
3070{
3071 struct variable v;
3072 struct variable *vp;
3073
3074 if (flocp != 0)
3075 v.fileinfo = *flocp;
3076 else
3077 v.fileinfo.filenm = 0;
3078
3079#ifndef CONFIG_WITH_VALUE_LENGTH
3080 if (!assign_variable_definition (&v, line))
3081 return 0;
3082
3083 vp = do_variable_definition (flocp, v.name, v.value,
3084 origin, v.flavor, target_var);
3085#else
3086 if (!assign_variable_definition (&v, line, eos))
3087 return 0;
3088
3089 vp = do_variable_definition_2 (flocp, v.name, v.value, v.value_length,
3090 0, NULL, origin, v.flavor, target_var);
3091#endif
3092
3093#ifndef CONFIG_WITH_STRCACHE2
3094 free (v.name);
3095#else
3096 free ((char *)v.name);
3097#endif
3098
3099 return vp;
3100}
3101
3102
3103#if defined (CONFIG_WITH_COMPILER) || defined (CONFIG_WITH_MAKE_STATS)
3104static unsigned long var_stats_evalvals, var_stats_evalvaled;
3105static unsigned long var_stats_expands, var_stats_expanded;
3106#endif
3107#ifdef CONFIG_WITH_COMPILER
3108static unsigned long var_stats_expandprogs, var_stats_evalprogs;
3109#endif
3110#ifdef CONFIG_WITH_MAKE_STATS
3111static unsigned long var_stats_changes, var_stats_changed;
3112static unsigned long var_stats_reallocs, var_stats_realloced;
3113static unsigned long var_stats_references, var_stats_referenced;
3114static unsigned long var_stats_val_len, var_stats_val_alloc_len;
3115static unsigned long var_stats_val_rdonly_len;
3116#endif
3117
3118/* Print information for variable V, prefixing it with PREFIX. */
3119
3120static void
3121print_variable (const void *item, void *arg)
3122{
3123 const struct variable *v = item;
3124 const char *prefix = arg;
3125 const char *origin;
3126#ifdef KMK
3127 const struct variable *alias = v;
3128 RESOLVE_ALIAS_VARIABLE(v);
3129#endif
3130
3131 switch (v->origin)
3132 {
3133 case o_automatic:
3134 origin = _("automatic");
3135 break;
3136 case o_default:
3137 origin = _("default");
3138 break;
3139 case o_env:
3140 origin = _("environment");
3141 break;
3142 case o_file:
3143 origin = _("makefile");
3144 break;
3145 case o_env_override:
3146 origin = _("environment under -e");
3147 break;
3148 case o_command:
3149 origin = _("command line");
3150 break;
3151 case o_override:
3152 origin = _("'override' directive");
3153 break;
3154#ifdef CONFIG_WITH_LOCAL_VARIABLES
3155 case o_local:
3156 origin = _("`local' directive");
3157 break;
3158#endif
3159 case o_invalid:
3160 default:
3161 abort ();
3162 }
3163 fputs ("# ", stdout);
3164 fputs (origin, stdout);
3165 if (v->private_var)
3166 fputs (" private", stdout);
3167#ifndef KMK
3168 if (v->fileinfo.filenm)
3169 printf (_(" (from '%s', line %lu)"),
3170 v->fileinfo.filenm, v->fileinfo.lineno + v->fileinfo.offset);
3171#else /* KMK */
3172 if (alias->fileinfo.filenm)
3173 printf (_(" (from '%s', line %lu)"),
3174 alias->fileinfo.filenm, alias->fileinfo.lineno);
3175 if (alias->aliased)
3176 fputs (" aliased", stdout);
3177 if (alias->alias)
3178 printf (_(", alias for '%s'"), v->name);
3179#endif /* KMK */
3180
3181#if defined (CONFIG_WITH_COMPILER) || defined (CONFIG_WITH_MAKE_STATS)
3182 if (v->evalval_count != 0)
3183 {
3184# ifdef CONFIG_WITH_MAKE_STATS
3185 printf (_(", %u evalvals (%llu ticks)"), v->evalval_count, v->cTicksEvalVal);
3186# else
3187 printf (_(", %u evalvals"), v->evalval_count);
3188# endif
3189 var_stats_evalvaled++;
3190 }
3191 var_stats_evalvals += v->evalval_count;
3192
3193 if (v->expand_count != 0)
3194 {
3195 printf (_(", %u expands"), v->expand_count);
3196 var_stats_expanded++;
3197 }
3198 var_stats_expands += v->expand_count;
3199
3200# ifdef CONFIG_WITH_COMPILER
3201 if (v->evalprog != 0)
3202 {
3203 printf (_(", evalprog"));
3204 var_stats_evalprogs++;
3205 }
3206 if (v->expandprog != 0)
3207 {
3208 printf (_(", expandprog"));
3209 var_stats_expandprogs++;
3210 }
3211# endif
3212#endif
3213
3214#ifdef CONFIG_WITH_MAKE_STATS
3215 if (v->changes != 0)
3216 {
3217 printf (_(", %u changes"), v->changes);
3218 var_stats_changed++;
3219 }
3220 var_stats_changes += v->changes;
3221
3222 if (v->reallocs != 0)
3223 {
3224 printf (_(", %u reallocs"), v->reallocs);
3225 var_stats_realloced++;
3226 }
3227 var_stats_reallocs += v->reallocs;
3228
3229 if (v->references != 0)
3230 {
3231 printf (_(", %u references"), v->references);
3232 var_stats_referenced++;
3233 }
3234 var_stats_references += v->references;
3235
3236 var_stats_val_len += v->value_length;
3237 if (v->value_alloc_len)
3238 var_stats_val_alloc_len += v->value_alloc_len;
3239 else
3240 var_stats_val_rdonly_len += v->value_length;
3241 assert (v->value_length == strlen (v->value));
3242 /*assert (v->rdonly_val ? !v->value_alloc_len : v->value_alloc_len > v->value_length); - FIXME */
3243#endif /* CONFIG_WITH_MAKE_STATS */
3244 putchar ('\n');
3245 fputs (prefix, stdout);
3246
3247 /* Is this a 'define'? */
3248 if (v->recursive && strchr (v->value, '\n') != 0)
3249#ifndef KMK /** @todo language feature for aliases */
3250 printf ("define %s\n%s\nendef\n", v->name, v->value);
3251#else
3252 printf ("define %s\n%s\nendef\n", alias->name, v->value);
3253#endif
3254 else
3255 {
3256 char *p;
3257
3258#ifndef KMK /** @todo language feature for aliases */
3259 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
3260#else
3261 printf ("%s %s= ", alias->name, v->recursive ? v->append ? "+" : "" : ":");
3262#endif
3263
3264 /* Check if the value is just whitespace. */
3265 p = next_token (v->value);
3266 if (p != v->value && *p == '\0')
3267 /* All whitespace. */
3268 printf ("$(subst ,,%s)", v->value);
3269 else if (v->recursive)
3270 fputs (v->value, stdout);
3271 else
3272 /* Double up dollar signs. */
3273 for (p = v->value; *p != '\0'; ++p)
3274 {
3275 if (*p == '$')
3276 putchar ('$');
3277 putchar (*p);
3278 }
3279 putchar ('\n');
3280 }
3281}
3282
3283
3284static void
3285print_auto_variable (const void *item, void *arg)
3286{
3287 const struct variable *v = item;
3288
3289 if (v->origin == o_automatic)
3290 print_variable (item, arg);
3291}
3292
3293
3294static void
3295print_noauto_variable (const void *item, void *arg)
3296{
3297 const struct variable *v = item;
3298
3299 if (v->origin != o_automatic)
3300 print_variable (item, arg);
3301}
3302
3303
3304/* Print all the variables in SET. PREFIX is printed before
3305 the actual variable definitions (everything else is comments). */
3306
3307#ifndef KMK
3308static
3309#endif
3310void
3311print_variable_set (struct variable_set *set, const char *prefix, int pauto)
3312{
3313#if defined (CONFIG_WITH_COMPILER) || defined (CONFIG_WITH_MAKE_STATS)
3314 var_stats_expands = var_stats_expanded = var_stats_evalvals
3315 = var_stats_evalvaled = 0;
3316#endif
3317#ifdef CONFIG_WITH_COMPILER
3318 var_stats_expandprogs = var_stats_evalprogs = 0;
3319#endif
3320#ifdef CONFIG_WITH_MAKE_STATS
3321 var_stats_changes = var_stats_changed = var_stats_reallocs
3322 = var_stats_realloced = var_stats_references = var_stats_referenced
3323 = var_stats_val_len = var_stats_val_alloc_len
3324 = var_stats_val_rdonly_len = 0;
3325#endif
3326
3327 hash_map_arg (&set->table, (pauto ? print_auto_variable : print_variable),
3328 (void *)prefix);
3329
3330 if (set->table.ht_fill)
3331 {
3332#ifdef CONFIG_WITH_MAKE_STATS
3333 unsigned long fragmentation;
3334
3335 fragmentation = var_stats_val_alloc_len - (var_stats_val_len - var_stats_val_rdonly_len);
3336 printf(_("# variable set value stats:\n\
3337# strings %7lu bytes, readonly %6lu bytes\n"),
3338 var_stats_val_len, var_stats_val_rdonly_len);
3339
3340 if (var_stats_val_alloc_len)
3341 printf(_("# allocated %7lu bytes, fragmentation %6lu bytes (%u%%)\n"),
3342 var_stats_val_alloc_len, fragmentation,
3343 (unsigned int)((100.0 * fragmentation) / var_stats_val_alloc_len));
3344
3345 if (var_stats_changed)
3346 printf(_("# changed %5lu (%2u%%), changes %6lu\n"),
3347 var_stats_changed,
3348 (unsigned int)((100.0 * var_stats_changed) / set->table.ht_fill),
3349 var_stats_changes);
3350
3351 if (var_stats_realloced)
3352 printf(_("# reallocated %5lu (%2u%%), reallocations %6lu\n"),
3353 var_stats_realloced,
3354 (unsigned int)((100.0 * var_stats_realloced) / set->table.ht_fill),
3355 var_stats_reallocs);
3356
3357 if (var_stats_referenced)
3358 printf(_("# referenced %5lu (%2u%%), references %6lu\n"),
3359 var_stats_referenced,
3360 (unsigned int)((100.0 * var_stats_referenced) / set->table.ht_fill),
3361 var_stats_references);
3362#endif
3363#if defined (CONFIG_WITH_COMPILER) || defined (CONFIG_WITH_MAKE_STATS)
3364 if (var_stats_evalvals)
3365 printf(_("# evalvaled %5lu (%2u%%), evalval calls %6lu\n"),
3366 var_stats_evalvaled,
3367 (unsigned int)((100.0 * var_stats_evalvaled) / set->table.ht_fill),
3368 var_stats_evalvals);
3369 if (var_stats_expands)
3370 printf(_("# expanded %5lu (%2u%%), expands %6lu\n"),
3371 var_stats_expanded,
3372 (unsigned int)((100.0 * var_stats_expanded) / set->table.ht_fill),
3373 var_stats_expands);
3374#endif
3375#ifdef CONFIG_WITH_COMPILER
3376 if (var_stats_expandprogs || var_stats_evalprogs)
3377 printf(_("# eval progs %5lu (%2u%%), expand progs %6lu (%2u%%)\n"),
3378 var_stats_evalprogs,
3379 (unsigned int)((100.0 * var_stats_evalprogs) / set->table.ht_fill),
3380 var_stats_expandprogs,
3381 (unsigned int)((100.0 * var_stats_expandprogs) / set->table.ht_fill));
3382#endif
3383 }
3384
3385 fputs (_("# variable set hash-table stats:\n"), stdout);
3386 fputs ("# ", stdout);
3387 hash_print_stats (&set->table, stdout);
3388 putc ('\n', stdout);
3389}
3390
3391/* Print the data base of variables. */
3392
3393void
3394print_variable_data_base (void)
3395{
3396 puts (_("\n# Variables\n"));
3397
3398 print_variable_set (&global_variable_set, "", 0);
3399
3400 puts (_("\n# Pattern-specific Variable Values"));
3401
3402 {
3403 struct pattern_var *p;
3404 unsigned int rules = 0;
3405
3406 for (p = pattern_vars; p != 0; p = p->next)
3407 {
3408 ++rules;
3409 printf ("\n%s :\n", p->target);
3410 print_variable (&p->variable, (void *)"# ");
3411 }
3412
3413 if (rules == 0)
3414 puts (_("\n# No pattern-specific variable values."));
3415 else
3416 printf (_("\n# %u pattern-specific variable values"), rules);
3417 }
3418
3419#ifdef CONFIG_WITH_STRCACHE2
3420 strcache2_print_stats (&variable_strcache, "# ");
3421#endif
3422}
3423
3424#ifdef CONFIG_WITH_PRINT_STATS_SWITCH
3425void
3426print_variable_stats (void)
3427{
3428 fputs (_("\n# Global variable hash-table stats:\n# "), stdout);
3429 hash_print_stats (&global_variable_set.table, stdout);
3430 fputs ("\n", stdout);
3431}
3432#endif
3433
3434/* Print all the local variables of FILE. */
3435
3436void
3437print_file_variables (const struct file *file)
3438{
3439 if (file->variables != 0)
3440 print_variable_set (file->variables->set, "# ", 1);
3441}
3442
3443void
3444print_target_variables (const struct file *file)
3445{
3446 if (file->variables != 0)
3447 {
3448 int l = strlen (file->name);
3449 char *t = alloca (l + 3);
3450
3451 strcpy (t, file->name);
3452 t[l] = ':';
3453 t[l+1] = ' ';
3454 t[l+2] = '\0';
3455
3456 hash_map_arg (&file->variables->set->table, print_noauto_variable, t);
3457 }
3458}
3459
3460#ifdef WINDOWS32
3461void
3462sync_Path_environment (void)
3463{
3464 char *path = allocated_variable_expand ("$(PATH)");
3465 static char *environ_path = NULL;
3466
3467 if (!path)
3468 return;
3469
3470 /* If done this before, free the previous entry before allocating new one. */
3471 free (environ_path);
3472
3473 /* Create something WINDOWS32 world can grok. */
3474 convert_Path_to_windows32 (path, ';');
3475 environ_path = xstrdup (concat (3, "PATH", "=", path));
3476 putenv (environ_path);
3477 free (path);
3478}
3479#endif
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use