VirtualBox

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

Last change on this file since 3273 was 3181, checked in by bird, 7 years ago

variable.c: Added KBUILD_HOST_UNAME_SYSNAME, KBUILD_HOST_UNAME_RELEASE, KBUILD_HOST_UNAME_VERSION, KBUILD_HOST_UNAME_MACHINE and KBUILD_HOST_UNAME_NODENAME variables on non-windows systems. That's help avoid needing to $(shell uname) to figure stuff out.

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

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