VirtualBox

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

Last change on this file since 3154 was 3140, checked in by bird, 7 years ago

kmk: Merged in changes from GNU make 4.2.1 (2e55f5e4abdc0e38c1d64be703b446695e70b3b6 / https://git.savannah.gnu.org/git/make.git).

  • Property svn:eol-style set to native
File size: 106.8 KB
Line 
1/* Internals of variables for GNU Make.
2Copyright (C) 1988-2016 Free Software Foundation, Inc.
3This file is part of GNU Make.
4
5GNU Make is free software; you can redistribute it and/or modify it under the
6terms of the GNU General Public License as published by the Free Software
7Foundation; either version 3 of the License, or (at your option) any later
8version.
9
10GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this program. If not, see <http://www.gnu.org/licenses/>. */
16
17#include "makeint.h"
18
19#include <assert.h>
20
21#include "filedef.h"
22#include "dep.h"
23#include "job.h"
24#include "commands.h"
25#include "variable.h"
26#include "rule.h"
27#ifdef WINDOWS32
28#include "pathstuff.h"
29#endif
30#include "hash.h"
31#ifdef KMK
32# include "kbuild.h"
33# ifdef WINDOWS32
34# include <Windows.h>
35# else
36# include <sys/utsname.h>
37# endif
38#endif
39#ifdef CONFIG_WITH_STRCACHE2
40# include <stddef.h>
41#endif
42#ifdef CONFIG_WITH_COMPILER
43# include "kmk_cc_exec.h"
44#endif
45
46#ifdef KMK
47/** Gets the real variable if alias. For use when looking up variables. */
48# define RESOLVE_ALIAS_VARIABLE(v) \
49 do { \
50 if ((v) != NULL && (v)->alias) \
51 { \
52 (v) = (struct variable *)(v)->value; \
53 assert ((v)->aliased); \
54 assert (!(v)->alias); \
55 } \
56 } while (0)
57#endif
58
59#ifdef KMK
60/* Incremented every time a variable is modified, so that target_environment
61 knows when to regenerate the table of exported global variables. */
62static size_t global_variable_generation = 0;
63#endif
64
65/* Incremented every time we add or remove a global variable. */
66static unsigned long variable_changenum;
67
68/* Chain of all pattern-specific variables. */
69
70static struct pattern_var *pattern_vars;
71
72/* Pointer to the last struct in the pack of a specific size, from 1 to 255.*/
73
74static struct pattern_var *last_pattern_vars[256];
75
76/* Create a new pattern-specific variable struct. The new variable is
77 inserted into the PATTERN_VARS list in the shortest patterns first
78 order to support the shortest stem matching (the variables are
79 matched in the reverse order so the ones with the longest pattern
80 will be considered first). Variables with the same pattern length
81 are inserted in the definition order. */
82
83struct pattern_var *
84create_pattern_var (const char *target, const char *suffix)
85{
86 register unsigned int len = strlen (target);
87 register struct pattern_var *p = xmalloc (sizeof (struct pattern_var));
88
89 if (pattern_vars != 0)
90 {
91 if (len < 256 && last_pattern_vars[len] != 0)
92 {
93 p->next = last_pattern_vars[len]->next;
94 last_pattern_vars[len]->next = p;
95 }
96 else
97 {
98 /* Find the position where we can insert this variable. */
99 register struct pattern_var **v;
100
101 for (v = &pattern_vars; ; v = &(*v)->next)
102 {
103 /* Insert at the end of the pack so that patterns with the
104 same length appear in the order they were defined .*/
105
106 if (*v == 0 || (*v)->len > len)
107 {
108 p->next = *v;
109 *v = p;
110 break;
111 }
112 }
113 }
114 }
115 else
116 {
117 pattern_vars = p;
118 p->next = 0;
119 }
120
121 p->target = target;
122 p->len = len;
123 p->suffix = suffix + 1;
124
125 if (len < 256)
126 last_pattern_vars[len] = p;
127
128 return p;
129}
130
131/* Look up a target in the pattern-specific variable list. */
132
133static struct pattern_var *
134lookup_pattern_var (struct pattern_var *start, const char *target)
135{
136 struct pattern_var *p;
137 unsigned int targlen = strlen (target);
138
139 for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
140 {
141 const char *stem;
142 unsigned int stemlen;
143
144 if (p->len > targlen)
145 /* It can't possibly match. */
146 continue;
147
148 /* From the lengths of the filename and the pattern parts,
149 find the stem: the part of the filename that matches the %. */
150 stem = target + (p->suffix - p->target - 1);
151 stemlen = targlen - p->len + 1;
152
153 /* Compare the text in the pattern before the stem, if any. */
154 if (stem > target && !strneq (p->target, target, stem - target))
155 continue;
156
157 /* Compare the text in the pattern after the stem, if any.
158 We could test simply using streq, but this way we compare the
159 first two characters immediately. This saves time in the very
160 common case where the first character matches because it is a
161 period. */
162 if (*p->suffix == stem[stemlen]
163 && (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
164 break;
165 }
166
167 return p;
168}
169
170
171#ifdef CONFIG_WITH_STRCACHE2
172struct strcache2 variable_strcache;
173#endif
174
175/* Hash table of all global variable definitions. */
176
177#ifndef CONFIG_WITH_STRCACHE2
178static unsigned long
179variable_hash_1 (const void *keyv)
180{
181 struct variable const *key = (struct variable const *) keyv;
182 return_STRING_N_HASH_1 (key->name, key->length);
183}
184
185static unsigned long
186variable_hash_2 (const void *keyv)
187{
188 struct variable const *key = (struct variable const *) keyv;
189 return_STRING_N_HASH_2 (key->name, key->length);
190}
191
192static int
193variable_hash_cmp (const void *xv, const void *yv)
194{
195 struct variable const *x = (struct variable const *) xv;
196 struct variable const *y = (struct variable const *) yv;
197 int result = x->length - y->length;
198 if (result)
199 return result;
200
201 return_STRING_N_COMPARE (x->name, y->name, x->length);
202}
203#endif /* !CONFIG_WITH_STRCACHE2 */
204
205#ifndef VARIABLE_BUCKETS
206# ifdef KMK /* Move to Makefile.kmk? (insanely high, but wtf, it gets the collitions down) */
207# define VARIABLE_BUCKETS 65535
208# else /*!KMK*/
209#define VARIABLE_BUCKETS 523
210# endif /*!KMK*/
211#endif
212#ifndef PERFILE_VARIABLE_BUCKETS
213# ifdef KMK /* Move to Makefile.kmk? */
214# define PERFILE_VARIABLE_BUCKETS 127
215# else
216#define PERFILE_VARIABLE_BUCKETS 23
217# endif
218#endif
219#ifndef SMALL_SCOPE_VARIABLE_BUCKETS
220# ifdef KMK /* Move to Makefile.kmk? */
221# define SMALL_SCOPE_VARIABLE_BUCKETS 63
222# else
223#define SMALL_SCOPE_VARIABLE_BUCKETS 13
224# endif
225#endif
226#ifndef ENVIRONMENT_VARIABLE_BUCKETS /* added by bird. */
227# define ENVIRONMENT_VARIABLE_BUCKETS 256
228#endif
229
230
231#ifdef KMK /* Drop the 'static' */
232struct variable_set global_variable_set;
233struct variable_set_list global_setlist
234#else
235static struct variable_set global_variable_set;
236static struct variable_set_list global_setlist
237#endif
238 = { 0, &global_variable_set, 0 };
239struct variable_set_list *current_variable_set_list = &global_setlist;
240
241
242/* Implement variables. */
243
244void
245init_hash_global_variable_set (void)
246{
247#ifndef CONFIG_WITH_STRCACHE2
248 hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
249 variable_hash_1, variable_hash_2, variable_hash_cmp);
250#else /* CONFIG_WITH_STRCACHE2 */
251 strcache2_init (&variable_strcache, "variable", 262144, 0, 0, 0);
252 hash_init_strcached (&global_variable_set.table, VARIABLE_BUCKETS,
253 &variable_strcache, offsetof (struct variable, name));
254#endif /* CONFIG_WITH_STRCACHE2 */
255}
256
257/* Define variable named NAME with value VALUE in SET. VALUE is copied.
258 LENGTH is the length of NAME, which does not need to be null-terminated.
259 ORIGIN specifies the origin of the variable (makefile, command line
260 or environment).
261 If RECURSIVE is nonzero a flag is set in the variable saying
262 that it should be recursively re-expanded. */
263
264#ifdef CONFIG_WITH_VALUE_LENGTH
265struct variable *
266define_variable_in_set (const char *name, unsigned int length,
267 const char *value, unsigned int value_len,
268 int duplicate_value, enum variable_origin origin,
269 int recursive, struct variable_set *set,
270 const floc *flocp)
271#else
272struct variable *
273define_variable_in_set (const char *name, unsigned int length,
274 const char *value, enum variable_origin origin,
275 int recursive, struct variable_set *set,
276 const floc *flocp)
277#endif
278{
279 struct variable *v;
280 struct variable **var_slot;
281 struct variable var_key;
282
283#ifdef KMK
284 if (set == NULL || set == &global_variable_set)
285 global_variable_generation++;
286#endif
287
288 if (env_overrides && origin == o_env)
289 origin = o_env_override;
290
291#ifndef KMK
292 if (set == NULL)
293 set = &global_variable_set;
294#else /* KMK */
295 /* Intercept kBuild object variable definitions. */
296 if (name[0] == '[' && length > 3)
297 {
298 v = try_define_kbuild_object_variable_via_accessor (name, length,
299 value, value_len, duplicate_value,
300 origin, recursive, flocp);
301 if (v != VAR_NOT_KBUILD_ACCESSOR)
302 return v;
303 }
304 if (set == NULL)
305 {
306 if (g_pTopKbEvalData)
307 return define_kbuild_object_variable_in_top_obj (name, length,
308 value, value_len, duplicate_value,
309 origin, recursive, flocp);
310 set = &global_variable_set;
311 }
312#endif /* KMK */
313
314#ifndef CONFIG_WITH_STRCACHE2
315 var_key.name = (char *) name;
316 var_key.length = length;
317 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
318 v = *var_slot;
319
320#ifdef VMS
321 /* VMS does not populate envp[] with DCL symbols and logical names which
322 historically are mapped to environent variables.
323 If the variable is not yet defined, then we need to check if getenv()
324 can find it. Do not do this for origin == o_env to avoid infinte
325 recursion */
326 if (HASH_VACANT (v) && (origin != o_env))
327 {
328 struct variable * vms_variable;
329 char * vname = alloca (length + 1);
330 char * vvalue;
331
332 strncpy (vname, name, length);
333 vvalue = getenv(vname);
334
335 /* Values starting with '$' are probably foreign commands.
336 We want to treat them as Shell aliases and not look them up here */
337 if ((vvalue != NULL) && (vvalue[0] != '$'))
338 {
339 vms_variable = lookup_variable(name, length);
340 /* Refresh the slot */
341 var_slot = (struct variable **) hash_find_slot (&set->table,
342 &var_key);
343 v = *var_slot;
344 }
345 }
346#endif
347
348 /* if (env_overrides && origin == o_env)
349 origin = o_env_override; - bird moved this up */
350
351#else /* CONFIG_WITH_STRCACHE2 */
352 name = strcache2_add (&variable_strcache, name, length);
353 if ( set != &global_variable_set
354 || !(v = strcache2_get_user_val (&variable_strcache, name)))
355 {
356 var_key.name = name;
357 var_key.length = length;
358 var_slot = (struct variable **) hash_find_slot_strcached (&set->table, &var_key);
359 v = *var_slot;
360 }
361 else
362 {
363 assert (!v || (v->name == name && !HASH_VACANT (v)));
364 var_slot = 0;
365 }
366#endif /* CONFIG_WITH_STRCACHE2 */
367 if (! HASH_VACANT (v))
368 {
369#ifdef KMK
370 RESOLVE_ALIAS_VARIABLE(v);
371#endif
372 if (env_overrides && v->origin == o_env)
373 /* V came from in the environment. Since it was defined
374 before the switches were parsed, it wasn't affected by -e. */
375 v->origin = o_env_override;
376
377 /* A variable of this name is already defined.
378 If the old definition is from a stronger source
379 than this one, don't redefine it. */
380 if ((int) origin >= (int) v->origin)
381 {
382#ifdef CONFIG_WITH_VALUE_LENGTH
383 if (value_len == ~0U)
384 value_len = strlen (value);
385 else
386 assert (value_len == strlen (value));
387 if (!duplicate_value || duplicate_value == -1)
388 {
389# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
390 if (v->value != 0 && !v->rdonly_val)
391 free (v->value);
392 v->rdonly_val = duplicate_value == -1;
393 v->value = (char *) value;
394 v->value_alloc_len = 0;
395# else
396 if (v->value != 0)
397 free (v->value);
398 v->value = (char *) value;
399 v->value_alloc_len = value_len + 1;
400# endif
401 }
402 else
403 {
404 if (v->value_alloc_len <= value_len)
405 {
406# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
407 if (v->rdonly_val)
408 v->rdonly_val = 0;
409 else
410# endif
411 free (v->value);
412 v->value_alloc_len = VAR_ALIGN_VALUE_ALLOC (value_len + 1);
413 v->value = xmalloc (v->value_alloc_len);
414 MAKE_STATS_2(v->reallocs++);
415 }
416 memcpy (v->value, value, value_len + 1);
417 }
418 v->value_length = value_len;
419#else /* !CONFIG_WITH_VALUE_LENGTH */
420 free (v->value);
421 v->value = xstrdup (value);
422#endif /* !CONFIG_WITH_VALUE_LENGTH */
423 if (flocp != 0)
424 v->fileinfo = *flocp;
425 else
426 v->fileinfo.filenm = 0;
427 v->origin = origin;
428 v->recursive = recursive;
429 VARIABLE_CHANGED (v);
430 }
431 return v;
432 }
433
434 /* Create a new variable definition and add it to the hash table. */
435
436#ifndef CONFIG_WITH_ALLOC_CACHES
437 v = xmalloc (sizeof (struct variable));
438#else
439 v = alloccache_alloc (&variable_cache);
440#endif
441#ifndef CONFIG_WITH_STRCACHE2
442 v->name = xstrndup (name, length);
443#else
444 v->name = name; /* already cached. */
445#endif
446 v->length = length;
447 hash_insert_at (&set->table, v, var_slot);
448 if (set == &global_variable_set)
449 ++variable_changenum;
450
451#ifdef CONFIG_WITH_VALUE_LENGTH
452 if (value_len == ~0U)
453 value_len = strlen (value);
454 else
455 assert (value_len == strlen (value));
456 v->value_length = value_len;
457 if (!duplicate_value || duplicate_value == -1)
458 {
459# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
460 v->rdonly_val = duplicate_value == -1;
461 v->value_alloc_len = v->rdonly_val ? 0 : value_len + 1;
462# endif
463 v->value = (char *)value;
464 }
465 else
466 {
467# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
468 v->rdonly_val = 0;
469# endif
470 v->value_alloc_len = VAR_ALIGN_VALUE_ALLOC (value_len + 1);
471 v->value = xmalloc (v->value_alloc_len);
472 memcpy (v->value, value, value_len + 1);
473 }
474#else /* !CONFIG_WITH_VALUE_LENGTH */
475 v->value = xstrdup (value);
476#endif /* !CONFIG_WITH_VALUE_LENGTH */
477 if (flocp != 0)
478 v->fileinfo = *flocp;
479 else
480 v->fileinfo.filenm = 0;
481 v->origin = origin;
482 v->recursive = recursive;
483 v->special = 0;
484 v->expanding = 0;
485 v->exp_count = 0;
486 v->per_target = 0;
487 v->append = 0;
488 v->private_var = 0;
489#ifdef KMK
490 v->alias = 0;
491 v->aliased = 0;
492#endif
493 v->export = v_default;
494#ifdef CONFIG_WITH_COMPILER
495 v->recursive_without_dollar = 0;
496 v->evalprog = 0;
497 v->expandprog = 0;
498 v->evalval_count = 0;
499 v->expand_count = 0;
500#else
501 MAKE_STATS_2(v->expand_count = 0);
502 MAKE_STATS_2(v->evalval_count = 0);
503#endif
504 MAKE_STATS_2(v->changes = 0);
505 MAKE_STATS_2(v->reallocs = 0);
506 MAKE_STATS_2(v->references = 0);
507 MAKE_STATS_2(v->cTicksEvalVal = 0);
508
509 v->exportable = 1;
510 if (*name != '_' && (*name < 'A' || *name > 'Z')
511 && (*name < 'a' || *name > 'z'))
512 v->exportable = 0;
513 else
514 {
515 for (++name; *name != '\0'; ++name)
516 if (*name != '_' && (*name < 'a' || *name > 'z')
517 && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
518 break;
519
520 if (*name != '\0')
521 v->exportable = 0;
522 }
523
524#ifdef CONFIG_WITH_STRCACHE2
525 /* If it's the global set, remember the variable. */
526 if (set == &global_variable_set)
527 strcache2_set_user_val (&variable_strcache, v->name, v);
528#endif
529 return v;
530}
531
532
533
534/* Undefine variable named NAME in SET. LENGTH is the length of NAME, which
535 does not need to be null-terminated. ORIGIN specifies the origin of the
536 variable (makefile, command line or environment). */
537
538static void
539free_variable_name_and_value (const void *item)
540{
541 struct variable *v = (struct variable *) item;
542#ifndef CONFIG_WITH_STRCACHE2
543 free (v->name);
544#endif
545#ifdef CONFIG_WITH_COMPILER
546 if (v->evalprog || v->expandprog)
547 kmk_cc_variable_deleted (v);
548#endif
549#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
550 if (!v->rdonly_val)
551#endif
552 free (v->value);
553}
554
555void
556free_variable_set (struct variable_set_list *list)
557{
558 hash_map (&list->set->table, free_variable_name_and_value);
559#ifndef CONFIG_WITH_ALLOC_CACHES
560 hash_free (&list->set->table, 1);
561 free (list->set);
562 free (list);
563#else
564 hash_free_cached (&list->set->table, 1, &variable_cache);
565 alloccache_free (&variable_set_cache, list->set);
566 alloccache_free (&variable_set_list_cache, list);
567#endif
568}
569
570void
571undefine_variable_in_set (const char *name, unsigned int length,
572 enum variable_origin origin,
573 struct variable_set *set)
574{
575 struct variable *v;
576 struct variable **var_slot;
577 struct variable var_key;
578
579 if (set == NULL)
580 set = &global_variable_set;
581
582#ifndef CONFIG_WITH_STRCACHE2
583 var_key.name = (char *) name;
584 var_key.length = length;
585 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
586#else
587 var_key.name = strcache2_lookup(&variable_strcache, name, length);
588 if (!var_key.name)
589 return;
590 var_key.length = length;
591 var_slot = (struct variable **) hash_find_slot_strcached (&set->table, &var_key);
592#endif
593#ifdef KMK
594 if (set == &global_variable_set)
595 global_variable_generation++;
596#endif
597
598 if (env_overrides && origin == o_env)
599 origin = o_env_override;
600
601 v = *var_slot;
602 if (! HASH_VACANT (v))
603 {
604#ifdef KMK
605 if (v->aliased || v->alias)
606 {
607 if (v->aliased)
608 OS (error, NULL, _("Cannot undefine the aliased variable '%s'"), v->name);
609 else
610 OS (error, NULL, _("Cannot undefine the variable alias '%s'"), v->name);
611 return;
612 }
613#endif
614
615 if (env_overrides && v->origin == o_env)
616 /* V came from in the environment. Since it was defined
617 before the switches were parsed, it wasn't affected by -e. */
618 v->origin = o_env_override;
619
620 /* Undefine only if this undefinition is from an equal or stronger
621 source than the variable definition. */
622 if ((int) origin >= (int) v->origin)
623 {
624 hash_delete_at (&set->table, var_slot);
625#ifdef CONFIG_WITH_STRCACHE2
626 if (set == &global_variable_set)
627 strcache2_set_user_val (&variable_strcache, v->name, NULL);
628#endif
629 free_variable_name_and_value (v);
630 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#endif
1709
1710 sprintf (buf, "%lu.%lu.%lu.%lu", ulMajor, ulMinor, ulPatch, ul4th);
1711 define_variable_cname ("KBUILD_HOST_VERSION", buf, o_default, 0);
1712
1713 sprintf (buf, "%lu", ulMajor);
1714 define_variable_cname ("KBUILD_HOST_VERSION_MAJOR", buf, o_default, 0);
1715
1716 sprintf (buf, "%lu", ulMinor);
1717 define_variable_cname ("KBUILD_HOST_VERSION_MINOR", buf, o_default, 0);
1718
1719 sprintf (buf, "%lu", ulPatch);
1720 define_variable_cname ("KBUILD_HOST_VERSION_PATCH", buf, o_default, 0);
1721
1722 /* The kBuild locations. */
1723 define_variable_cname ("KBUILD_PATH", get_kbuild_path (), o_default, 0);
1724 define_variable_cname ("KBUILD_BIN_PATH", get_kbuild_bin_path (), o_default, 0);
1725
1726 define_variable_cname ("PATH_KBUILD", get_kbuild_path (), o_default, 0);
1727 define_variable_cname ("PATH_KBUILD_BIN", get_kbuild_bin_path (), o_default, 0);
1728
1729 /* Define KMK_FEATURES to indicate various working KMK features. */
1730# if defined (CONFIG_WITH_RSORT) \
1731 && defined (CONFIG_WITH_ABSPATHEX) \
1732 && defined (CONFIG_WITH_TOUPPER_TOLOWER) \
1733 && defined (CONFIG_WITH_DEFINED) \
1734 && defined (CONFIG_WITH_VALUE_LENGTH) \
1735 && defined (CONFIG_WITH_COMPARE) \
1736 && defined (CONFIG_WITH_STACK) \
1737 && defined (CONFIG_WITH_MATH) \
1738 && defined (CONFIG_WITH_XARGS) \
1739 && defined (CONFIG_WITH_EXPLICIT_MULTITARGET) \
1740 && defined (CONFIG_WITH_DOT_MUST_MAKE) \
1741 && defined (CONFIG_WITH_PREPEND_ASSIGNMENT) \
1742 && defined (CONFIG_WITH_SET_CONDITIONALS) \
1743 && defined (CONFIG_WITH_DATE) \
1744 && defined (CONFIG_WITH_FILE_SIZE) \
1745 && defined (CONFIG_WITH_WHERE_FUNCTION) \
1746 && defined (CONFIG_WITH_WHICH) \
1747 && defined (CONFIG_WITH_EVALPLUS) \
1748 && (defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)) \
1749 && defined (CONFIG_WITH_COMMANDS_FUNC) \
1750 && defined (CONFIG_WITH_PRINTF) \
1751 && defined (CONFIG_WITH_LOOP_FUNCTIONS) \
1752 && defined (CONFIG_WITH_ROOT_FUNC) \
1753 && defined (CONFIG_WITH_STRING_FUNCTIONS) \
1754 && defined (CONFIG_WITH_DEFINED_FUNCTIONS) \
1755 && defined (KMK_HELPERS)
1756 define_variable_cname ("KMK_FEATURES",
1757 "append-dash-n abspath includedep-queue install-hard-linking umask"
1758 " kBuild-define"
1759 " rsort"
1760 " abspathex"
1761 " toupper tolower"
1762 " defined"
1763 " comp-vars comp-cmds comp-cmds-ex"
1764 " stack"
1765 " math-int"
1766 " xargs"
1767 " explicit-multitarget"
1768 " dot-must-make"
1769 " prepend-assignment"
1770 " set-conditionals intersects"
1771 " date"
1772 " file-size"
1773 " expr if-expr select"
1774 " where"
1775 " which"
1776 " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var"
1777 " make-stats"
1778 " commands"
1779 " printf"
1780 " for while"
1781 " root"
1782 " length insert pos lastpos substr translate"
1783 " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one kb-exp-tmpl"
1784 " firstdefined lastdefined"
1785 , o_default, 0);
1786# else /* MSC can't deal with strings mixed with #if/#endif, thus the slow way. */
1787# error "All features should be enabled by default!"
1788 strcpy (buf, "append-dash-n abspath includedep-queue install-hard-linking umask"
1789 " kBuild-define");
1790# if defined (CONFIG_WITH_RSORT)
1791 strcat (buf, " rsort");
1792# endif
1793# if defined (CONFIG_WITH_ABSPATHEX)
1794 strcat (buf, " abspathex");
1795# endif
1796# if defined (CONFIG_WITH_TOUPPER_TOLOWER)
1797 strcat (buf, " toupper tolower");
1798# endif
1799# if defined (CONFIG_WITH_DEFINED)
1800 strcat (buf, " defined");
1801# endif
1802# if defined (CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
1803 strcat (buf, " comp-vars comp-cmds comp-cmds-ex");
1804# endif
1805# if defined (CONFIG_WITH_STACK)
1806 strcat (buf, " stack");
1807# endif
1808# if defined (CONFIG_WITH_MATH)
1809 strcat (buf, " math-int");
1810# endif
1811# if defined (CONFIG_WITH_XARGS)
1812 strcat (buf, " xargs");
1813# endif
1814# if defined (CONFIG_WITH_EXPLICIT_MULTITARGET)
1815 strcat (buf, " explicit-multitarget");
1816# endif
1817# if defined (CONFIG_WITH_DOT_MUST_MAKE)
1818 strcat (buf, " dot-must-make");
1819# endif
1820# if defined (CONFIG_WITH_PREPEND_ASSIGNMENT)
1821 strcat (buf, " prepend-assignment");
1822# endif
1823# if defined (CONFIG_WITH_SET_CONDITIONALS)
1824 strcat (buf, " set-conditionals intersects");
1825# endif
1826# if defined (CONFIG_WITH_DATE)
1827 strcat (buf, " date");
1828# endif
1829# if defined (CONFIG_WITH_FILE_SIZE)
1830 strcat (buf, " file-size");
1831# endif
1832# if defined (CONFIG_WITH_IF_CONDITIONALS)
1833 strcat (buf, " expr if-expr select");
1834# endif
1835# if defined (CONFIG_WITH_WHERE_FUNCTION)
1836 strcat (buf, " where");
1837# endif
1838# if defined (CONFIG_WITH_WHICH)
1839 strcat (buf, " which");
1840# endif
1841# if defined (CONFIG_WITH_EVALPLUS)
1842 strcat (buf, " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var");
1843# endif
1844# if defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)
1845 strcat (buf, " make-stats");
1846# endif
1847# if defined (CONFIG_WITH_COMMANDS_FUNC)
1848 strcat (buf, " commands");
1849# endif
1850# if defined (CONFIG_WITH_PRINTF)
1851 strcat (buf, " printf");
1852# endif
1853# if defined (CONFIG_WITH_LOOP_FUNCTIONS)
1854 strcat (buf, " for while");
1855# endif
1856# if defined (CONFIG_WITH_ROOT_FUNC)
1857 strcat (buf, " root");
1858# endif
1859# if defined (CONFIG_WITH_STRING_FUNCTIONS)
1860 strcat (buf, " length insert pos lastpos substr translate");
1861# endif
1862# if defined (CONFIG_WITH_DEFINED_FUNCTIONS)
1863 strcat (buf, " firstdefined lastdefined");
1864# endif
1865# if defined (KMK_HELPERS)
1866 strcat (buf, " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one kb-exp-tmpl");
1867# endif
1868 define_variable_cname ("KMK_FEATURES", buf, o_default, 0);
1869# endif
1870
1871#endif /* KMK */
1872
1873#ifdef CONFIG_WITH_KMK_BUILTIN
1874 /* The supported kMk Builtin commands. */
1875 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);
1876#endif
1877
1878#ifdef __MSDOS__
1879 /* Allow to specify a special shell just for Make,
1880 and use $COMSPEC as the default $SHELL when appropriate. */
1881 {
1882 static char shell_str[] = "SHELL";
1883 const int shlen = sizeof (shell_str) - 1;
1884 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
1885 struct variable *comp = lookup_variable ("COMSPEC", 7);
1886
1887 /* $(MAKESHELL) overrides $(SHELL) even if -e is in effect. */
1888 if (mshp)
1889 (void) define_variable (shell_str, shlen,
1890 mshp->value, o_env_override, 0);
1891 else if (comp)
1892 {
1893 /* $(COMSPEC) shouldn't override $(SHELL). */
1894 struct variable *shp = lookup_variable (shell_str, shlen);
1895
1896 if (!shp)
1897 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
1898 }
1899 }
1900#elif defined(__EMX__)
1901 {
1902 static char shell_str[] = "SHELL";
1903 const int shlen = sizeof (shell_str) - 1;
1904 struct variable *shell = lookup_variable (shell_str, shlen);
1905 struct variable *replace = lookup_variable ("MAKESHELL", 9);
1906
1907 /* if $MAKESHELL is defined in the environment assume o_env_override */
1908 if (replace && *replace->value && replace->origin == o_env)
1909 replace->origin = o_env_override;
1910
1911 /* if $MAKESHELL is not defined use $SHELL but only if the variable
1912 did not come from the environment */
1913 if (!replace || !*replace->value)
1914 if (shell && *shell->value && (shell->origin == o_env
1915 || shell->origin == o_env_override))
1916 {
1917 /* overwrite whatever we got from the environment */
1918 free (shell->value);
1919 shell->value = xstrdup (default_shell);
1920 shell->origin = o_default;
1921 }
1922
1923 /* Some people do not like cmd to be used as the default
1924 if $SHELL is not defined in the Makefile.
1925 With -DNO_CMD_DEFAULT you can turn off this behaviour */
1926# ifndef NO_CMD_DEFAULT
1927 /* otherwise use $COMSPEC */
1928 if (!replace || !*replace->value)
1929 replace = lookup_variable ("COMSPEC", 7);
1930
1931 /* otherwise use $OS2_SHELL */
1932 if (!replace || !*replace->value)
1933 replace = lookup_variable ("OS2_SHELL", 9);
1934# else
1935# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
1936# endif
1937
1938 if (replace && *replace->value)
1939 /* overwrite $SHELL */
1940 (void) define_variable (shell_str, shlen, replace->value,
1941 replace->origin, 0);
1942 else
1943 /* provide a definition if there is none */
1944 (void) define_variable (shell_str, shlen, default_shell,
1945 o_default, 0);
1946 }
1947
1948#endif
1949
1950 /* This won't override any definition, but it will provide one if there
1951 isn't one there. */
1952 v = define_variable_cname ("SHELL", default_shell, o_default, 0);
1953#ifdef __MSDOS__
1954 v->export = v_export; /* Export always SHELL. */
1955#endif
1956
1957 /* On MSDOS we do use SHELL from environment, since it isn't a standard
1958 environment variable on MSDOS, so whoever sets it, does that on purpose.
1959 On OS/2 we do not use SHELL from environment but we have already handled
1960 that problem above. */
1961#if !defined(__MSDOS__) && !defined(__EMX__)
1962 /* Don't let SHELL come from the environment. */
1963 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
1964 {
1965# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1966 if (v->rdonly_val)
1967 v->rdonly_val = 0;
1968 else
1969# endif
1970 free (v->value);
1971 v->origin = o_file;
1972 v->value = xstrdup (default_shell);
1973# ifdef CONFIG_WITH_VALUE_LENGTH
1974 v->value_length = strlen (v->value);
1975 v->value_alloc_len = v->value_length + 1;
1976# endif
1977 }
1978#endif
1979
1980 /* Make sure MAKEFILES gets exported if it is set. */
1981 v = define_variable_cname ("MAKEFILES", "", o_default, 0);
1982 v->export = v_ifset;
1983
1984 /* Define the magic D and F variables in terms of
1985 the automatic variables they are variations of. */
1986
1987#if defined(__MSDOS__) || defined(WINDOWS32)
1988 /* For consistency, remove the trailing backslash as well as slash. */
1989 define_variable_cname ("@D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $@)))",
1990 o_automatic, 1);
1991 define_variable_cname ("%D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $%)))",
1992 o_automatic, 1);
1993 define_variable_cname ("*D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $*)))",
1994 o_automatic, 1);
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#else /* not __MSDOS__, not WINDOWS32 */
2004 define_variable_cname ("@D", "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
2005 define_variable_cname ("%D", "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
2006 define_variable_cname ("*D", "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
2007 define_variable_cname ("<D", "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
2008 define_variable_cname ("?D", "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
2009 define_variable_cname ("^D", "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
2010 define_variable_cname ("+D", "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
2011#endif
2012 define_variable_cname ("@F", "$(notdir $@)", o_automatic, 1);
2013 define_variable_cname ("%F", "$(notdir $%)", o_automatic, 1);
2014 define_variable_cname ("*F", "$(notdir $*)", o_automatic, 1);
2015 define_variable_cname ("<F", "$(notdir $<)", o_automatic, 1);
2016 define_variable_cname ("?F", "$(notdir $?)", o_automatic, 1);
2017 define_variable_cname ("^F", "$(notdir $^)", o_automatic, 1);
2018 define_variable_cname ("+F", "$(notdir $+)", o_automatic, 1);
2019#ifdef CONFIG_WITH_LAZY_DEPS_VARS
2020 define_variable ("^", 1, "$(deps $@)", o_automatic, 1);
2021 define_variable ("+", 1, "$(deps-all $@)", o_automatic, 1);
2022 define_variable ("?", 1, "$(deps-newer $@)", o_automatic, 1);
2023 define_variable ("|", 1, "$(deps-oo $@)", o_automatic, 1);
2024#endif /* CONFIG_WITH_LAZY_DEPS_VARS */
2025}
2026
2027
2028int export_all_variables;
2029
2030#ifdef KMK
2031/* Cached table containing the exports of the global_variable_set. When
2032 there are many global variables, it can be so expensive to construct the
2033 child environment that we have a majority of job slot idle. */
2034static size_t global_variable_set_exports_generation = ~(size_t)0;
2035static struct hash_table global_variable_set_exports;
2036
2037static void update_global_variable_set_exports(void)
2038{
2039 struct variable **v_slot;
2040 struct variable **v_end;
2041
2042 /* Re-initialize the table. */
2043 if (global_variable_set_exports_generation != ~(size_t)0)
2044 hash_free (&global_variable_set_exports, 0);
2045 hash_init_strcached (&global_variable_set_exports, ENVIRONMENT_VARIABLE_BUCKETS,
2046 &variable_strcache, offsetof (struct variable, name));
2047
2048 /* do pretty much the same as target_environment. */
2049 v_slot = (struct variable **) global_variable_set.table.ht_vec;
2050 v_end = v_slot + global_variable_set.table.ht_size;
2051 for ( ; v_slot < v_end; v_slot++)
2052 if (! HASH_VACANT (*v_slot))
2053 {
2054 struct variable **new_slot;
2055 struct variable *v = *v_slot;
2056
2057 switch (v->export)
2058 {
2059 case v_default:
2060 if (v->origin == o_default || v->origin == o_automatic)
2061 /* Only export default variables by explicit request. */
2062 continue;
2063
2064 /* The variable doesn't have a name that can be exported. */
2065 if (! v->exportable)
2066 continue;
2067
2068 if (! export_all_variables
2069 && v->origin != o_command
2070 && v->origin != o_env && v->origin != o_env_override)
2071 continue;
2072 break;
2073
2074 case v_export:
2075 break;
2076
2077 case v_noexport:
2078 {
2079 /* If this is the SHELL variable and it's not exported,
2080 then add the value from our original environment, if
2081 the original environment defined a value for SHELL. */
2082 extern struct variable shell_var;
2083 if (streq (v->name, "SHELL") && shell_var.value)
2084 {
2085 v = &shell_var;
2086 break;
2087 }
2088 continue;
2089 }
2090
2091 case v_ifset:
2092 if (v->origin == o_default)
2093 continue;
2094 break;
2095 }
2096
2097 assert (strcache2_is_cached (&variable_strcache, v->name));
2098 new_slot = (struct variable **) hash_find_slot_strcached (&global_variable_set_exports, v);
2099 if (HASH_VACANT (*new_slot))
2100 hash_insert_at (&global_variable_set_exports, v, new_slot);
2101 }
2102
2103 /* done */
2104 global_variable_set_exports_generation = global_variable_generation;
2105}
2106
2107#endif
2108
2109/* Create a new environment for FILE's commands.
2110 If FILE is nil, this is for the 'shell' function.
2111 The child's MAKELEVEL variable is incremented. */
2112
2113char **
2114target_environment (struct file *file)
2115{
2116 struct variable_set_list *set_list;
2117 register struct variable_set_list *s;
2118 struct hash_table table;
2119 struct variable **v_slot;
2120 struct variable **v_end;
2121 struct variable makelevel_key;
2122 char **result_0;
2123 char **result;
2124#ifdef CONFIG_WITH_STRCACHE2
2125 const char *cached_name;
2126#endif
2127
2128#ifdef KMK
2129 if (global_variable_set_exports_generation != global_variable_generation)
2130 update_global_variable_set_exports();
2131#endif
2132
2133 if (file == 0)
2134 set_list = current_variable_set_list;
2135 else
2136 set_list = file->variables;
2137
2138#ifndef CONFIG_WITH_STRCACHE2
2139 hash_init (&table, ENVIRONMENT_VARIABLE_BUCKETS,
2140 variable_hash_1, variable_hash_2, variable_hash_cmp);
2141#else /* CONFIG_WITH_STRCACHE2 */
2142 hash_init_strcached (&table, ENVIRONMENT_VARIABLE_BUCKETS,
2143 &variable_strcache, offsetof (struct variable, name));
2144#endif /* CONFIG_WITH_STRCACHE2 */
2145
2146 /* Run through all the variable sets in the list,
2147 accumulating variables in TABLE. */
2148 for (s = set_list; s != 0; s = s->next)
2149 {
2150 struct variable_set *set = s->set;
2151#ifdef KMK
2152 if (set == &global_variable_set)
2153 {
2154 assert(s->next == NULL);
2155 break;
2156 }
2157#endif
2158 v_slot = (struct variable **) set->table.ht_vec;
2159 v_end = v_slot + set->table.ht_size;
2160 for ( ; v_slot < v_end; v_slot++)
2161 if (! HASH_VACANT (*v_slot))
2162 {
2163 struct variable **new_slot;
2164 struct variable *v = *v_slot;
2165
2166 /* If this is a per-target variable and it hasn't been touched
2167 already then look up the global version and take its export
2168 value. */
2169 if (v->per_target && v->export == v_default)
2170 {
2171 struct variable *gv;
2172
2173#ifndef CONFIG_WITH_VALUE_LENGTH
2174 gv = lookup_variable_in_set (v->name, strlen (v->name),
2175 &global_variable_set);
2176#else
2177 assert ((int)strlen(v->name) == v->length);
2178 gv = lookup_variable_in_set (v->name, v->length,
2179 &global_variable_set);
2180#endif
2181 if (gv)
2182 v->export = gv->export;
2183 }
2184
2185 switch (v->export)
2186 {
2187 case v_default:
2188 if (v->origin == o_default || v->origin == o_automatic)
2189 /* Only export default variables by explicit request. */
2190 continue;
2191
2192 /* The variable doesn't have a name that can be exported. */
2193 if (! v->exportable)
2194 continue;
2195
2196 if (! export_all_variables
2197 && v->origin != o_command
2198 && v->origin != o_env && v->origin != o_env_override)
2199 continue;
2200 break;
2201
2202 case v_export:
2203 break;
2204
2205 case v_noexport:
2206 {
2207 /* If this is the SHELL variable and it's not exported,
2208 then add the value from our original environment, if
2209 the original environment defined a value for SHELL. */
2210 if (streq (v->name, "SHELL") && shell_var.value)
2211 {
2212 v = &shell_var;
2213 break;
2214 }
2215 continue;
2216 }
2217
2218 case v_ifset:
2219 if (v->origin == o_default)
2220 continue;
2221 break;
2222 }
2223
2224#ifndef CONFIG_WITH_STRCACHE2
2225 new_slot = (struct variable **) hash_find_slot (&table, v);
2226#else /* CONFIG_WITH_STRCACHE2 */
2227 assert (strcache2_is_cached (&variable_strcache, v->name));
2228 new_slot = (struct variable **) hash_find_slot_strcached (&table, v);
2229#endif /* CONFIG_WITH_STRCACHE2 */
2230 if (HASH_VACANT (*new_slot))
2231 hash_insert_at (&table, v, new_slot);
2232 }
2233 }
2234
2235#ifdef KMK
2236 /* Add the global exports to table. */
2237 v_slot = (struct variable **) global_variable_set_exports.ht_vec;
2238 v_end = v_slot + global_variable_set_exports.ht_size;
2239 for ( ; v_slot < v_end; v_slot++)
2240 if (! HASH_VACANT (*v_slot))
2241 {
2242 struct variable **new_slot;
2243 struct variable *v = *v_slot;
2244 assert (strcache2_is_cached (&variable_strcache, v->name));
2245 new_slot = (struct variable **) hash_find_slot_strcached (&table, v);
2246 if (HASH_VACANT (*new_slot))
2247 hash_insert_at (&table, v, new_slot);
2248 }
2249#endif
2250
2251#ifndef CONFIG_WITH_STRCACHE2
2252 makelevel_key.name = (char *)MAKELEVEL_NAME;
2253 makelevel_key.length = MAKELEVEL_LENGTH;
2254 hash_delete (&table, &makelevel_key);
2255#else /* CONFIG_WITH_STRCACHE2 */
2256 /* lookup the name in the string case, if it's not there it won't
2257 be in any of the sets either. */
2258 cached_name = strcache2_lookup (&variable_strcache,
2259 MAKELEVEL_NAME, MAKELEVEL_LENGTH);
2260 if (cached_name)
2261 {
2262 makelevel_key.name = cached_name;
2263 makelevel_key.length = MAKELEVEL_LENGTH;
2264 hash_delete_strcached (&table, &makelevel_key);
2265 }
2266#endif /* CONFIG_WITH_STRCACHE2 */
2267
2268 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
2269
2270 v_slot = (struct variable **) table.ht_vec;
2271 v_end = v_slot + table.ht_size;
2272 for ( ; v_slot < v_end; v_slot++)
2273 if (! HASH_VACANT (*v_slot))
2274 {
2275 struct variable *v = *v_slot;
2276
2277 /* If V is recursively expanded and didn't come from the environment,
2278 expand its value. If it came from the environment, it should
2279 go back into the environment unchanged. */
2280 if (v->recursive
2281 && v->origin != o_env && v->origin != o_env_override)
2282 {
2283#ifndef CONFIG_WITH_VALUE_LENGTH
2284 char *value = recursively_expand_for_file (v, file);
2285#else
2286 char *value = recursively_expand_for_file (v, file, NULL);
2287#endif
2288#ifdef WINDOWS32
2289 if (strcmp (v->name, "Path") == 0 ||
2290 strcmp (v->name, "PATH") == 0)
2291 convert_Path_to_windows32 (value, ';');
2292#endif
2293 *result++ = xstrdup (concat (3, v->name, "=", value));
2294 free (value);
2295 }
2296 else
2297 {
2298#ifdef WINDOWS32
2299 if (strcmp (v->name, "Path") == 0 ||
2300 strcmp (v->name, "PATH") == 0)
2301 convert_Path_to_windows32 (v->value, ';');
2302#endif
2303 *result++ = xstrdup (concat (3, v->name, "=", v->value));
2304 }
2305 }
2306
2307 *result = xmalloc (100);
2308 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
2309 *++result = 0;
2310
2311 hash_free (&table, 0);
2312
2313 return result_0;
2314}
2315
2316
2317#ifdef CONFIG_WITH_VALUE_LENGTH
2318/* Worker function for do_variable_definition_append() and
2319 append_expanded_string_to_variable().
2320 The APPEND argument indicates whether it's an append or prepend operation. */
2321void append_string_to_variable (struct variable *v, const char *value, unsigned int value_len, int append)
2322{
2323 /* The previous definition of the variable was recursive.
2324 The new value is the unexpanded old and new values. */
2325 unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
2326 int done_1st_prepend_copy = 0;
2327#ifdef KMK
2328 assert (!v->alias);
2329#endif
2330
2331 /* Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
2332 if (!value_len)
2333 return;
2334
2335 /* adjust the size. */
2336 if (v->value_alloc_len <= new_value_len + 1)
2337 {
2338 if (v->value_alloc_len < 256)
2339 v->value_alloc_len = 256;
2340 else
2341 v->value_alloc_len *= 2;
2342 if (v->value_alloc_len < new_value_len + 1)
2343 v->value_alloc_len = VAR_ALIGN_VALUE_ALLOC (new_value_len + 1 + value_len /*future*/ );
2344# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2345 if ((append || !v->value_length) && !v->rdonly_val)
2346# else
2347 if (append || !v->value_length)
2348# endif
2349 v->value = xrealloc (v->value, v->value_alloc_len);
2350 else
2351 {
2352 /* avoid the extra memcpy the xrealloc may have to do */
2353 char *new_buf = xmalloc (v->value_alloc_len);
2354 memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
2355 done_1st_prepend_copy = 1;
2356# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2357 if (v->rdonly_val)
2358 v->rdonly_val = 0;
2359 else
2360# endif
2361 free (v->value);
2362 v->value = new_buf;
2363 }
2364 MAKE_STATS_2(v->reallocs++);
2365 }
2366
2367 /* insert the new bits */
2368 if (v->value_length != 0)
2369 {
2370 if (append)
2371 {
2372 v->value[v->value_length] = ' ';
2373 memcpy (&v->value[v->value_length + 1], value, value_len + 1);
2374 }
2375 else
2376 {
2377 if (!done_1st_prepend_copy)
2378 memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
2379 v->value[value_len] = ' ';
2380 memcpy (v->value, value, value_len);
2381 }
2382 }
2383 else
2384 memcpy (v->value, value, value_len + 1);
2385 v->value_length = new_value_len;
2386 VARIABLE_CHANGED (v);
2387}
2388
2389struct variable *
2390do_variable_definition_append (const floc *flocp, struct variable *v,
2391 const char *value, unsigned int value_len,
2392 int simple_value, enum variable_origin origin,
2393 int append)
2394{
2395 if (env_overrides && origin == o_env)
2396 origin = o_env_override;
2397
2398 if (env_overrides && v->origin == o_env)
2399 /* V came from in the environment. Since it was defined
2400 before the switches were parsed, it wasn't affected by -e. */
2401 v->origin = o_env_override;
2402
2403 /* A variable of this name is already defined.
2404 If the old definition is from a stronger source
2405 than this one, don't redefine it. */
2406 if ((int) origin < (int) v->origin)
2407 return v;
2408 v->origin = origin;
2409
2410 /* location */
2411 if (flocp != 0)
2412 v->fileinfo = *flocp;
2413
2414 /* The juicy bits, append the specified value to the variable
2415 This is a heavily exercised code path in kBuild. */
2416 if (value_len == ~0U)
2417 value_len = strlen (value);
2418 if (v->recursive || simple_value)
2419 append_string_to_variable (v, value, value_len, append);
2420 else
2421 /* The previous definition of the variable was simple.
2422 The new value comes from the old value, which was expanded
2423 when it was set; and from the expanded new value. */
2424 append_expanded_string_to_variable (v, value, value_len, append);
2425
2426 /* update the variable */
2427 return v;
2428}
2429#endif /* CONFIG_WITH_VALUE_LENGTH */
2430
2431
2432static struct variable *
2433set_special_var (struct variable *var)
2434{
2435 if (streq (var->name, RECIPEPREFIX_NAME))
2436 {
2437 /* The user is resetting the command introduction prefix. This has to
2438 happen immediately, so that subsequent rules are interpreted
2439 properly. */
2440 cmd_prefix = var->value[0]=='\0' ? RECIPEPREFIX_DEFAULT : var->value[0];
2441 }
2442
2443 return var;
2444}
2445
2446
2447/* Given a string, shell-execute it and return a malloc'ed string of the
2448 * result. This removes only ONE newline (if any) at the end, for maximum
2449 * compatibility with the *BSD makes. If it fails, returns NULL. */
2450
2451static char *
2452shell_result (const char *p)
2453{
2454 char *buf;
2455 unsigned int len;
2456 char *args[2];
2457 char *result;
2458
2459 install_variable_buffer (&buf, &len);
2460
2461 args[0] = (char *) p;
2462 args[1] = NULL;
2463 variable_buffer_output (func_shell_base (variable_buffer, args, 0), "\0", 1);
2464 result = strdup (variable_buffer);
2465
2466 restore_variable_buffer (buf, len);
2467 return result;
2468}
2469
2470
2471/* Given a variable, a value, and a flavor, define the variable.
2472 See the try_variable_definition() function for details on the parameters. */
2473
2474struct variable *
2475#ifndef CONFIG_WITH_VALUE_LENGTH
2476do_variable_definition (const floc *flocp, const char *varname,
2477 const char *value, enum variable_origin origin,
2478 enum variable_flavor flavor, int target_var)
2479#else /* CONFIG_WITH_VALUE_LENGTH */
2480do_variable_definition_2 (const floc *flocp,
2481 const char *varname, const char *value,
2482 unsigned int value_len, int simple_value,
2483 char *free_value,
2484 enum variable_origin origin,
2485 enum variable_flavor flavor,
2486 int target_var)
2487#endif /* CONFIG_WITH_VALUE_LENGTH */
2488{
2489 const char *p;
2490 char *alloc_value = NULL;
2491 struct variable *v;
2492 int append = 0;
2493 int conditional = 0;
2494 const size_t varname_len = strlen (varname); /* bird */
2495
2496#ifdef CONFIG_WITH_VALUE_LENGTH
2497 if (value_len == ~0U)
2498 value_len = strlen (value);
2499 else
2500 assert (value_len == strlen (value));
2501#endif
2502
2503 /* Calculate the variable's new value in VALUE. */
2504
2505 switch (flavor)
2506 {
2507 default:
2508 case f_bogus:
2509 /* Should not be possible. */
2510 abort ();
2511 case f_simple:
2512 /* A simple variable definition "var := value". Expand the value.
2513 We have to allocate memory since otherwise it'll clobber the
2514 variable buffer, and we may still need that if we're looking at a
2515 target-specific variable. */
2516#ifndef CONFIG_WITH_VALUE_LENGTH
2517 p = alloc_value = allocated_variable_expand (value);
2518#else /* CONFIG_WITH_VALUE_LENGTH */
2519 if (!simple_value)
2520 p = alloc_value = allocated_variable_expand_2 (value, value_len, &value_len);
2521 else
2522 {
2523 if (value_len == ~0U)
2524 value_len = strlen (value);
2525 if (!free_value)
2526 p = alloc_value = xstrndup (value, value_len);
2527 else
2528 {
2529 assert (value == free_value);
2530 p = alloc_value = free_value;
2531 free_value = 0;
2532 }
2533 }
2534#endif /* CONFIG_WITH_VALUE_LENGTH */
2535 break;
2536 case f_shell:
2537 {
2538 /* A shell definition "var != value". Expand value, pass it to
2539 the shell, and store the result in recursively-expanded var. */
2540 char *q = allocated_variable_expand (value);
2541 p = alloc_value = shell_result (q);
2542 free (q);
2543 flavor = f_recursive;
2544 break;
2545 }
2546 case f_conditional:
2547 /* A conditional variable definition "var ?= value".
2548 The value is set IFF the variable is not defined yet. */
2549 v = lookup_variable (varname, varname_len);
2550 if (v)
2551#ifndef CONFIG_WITH_VALUE_LENGTH
2552 return v->special ? set_special_var (v) : v;
2553#else /* CONFIG_WITH_VALUE_LENGTH */
2554 {
2555 if (free_value)
2556 free (free_value);
2557 return v->special ? set_special_var (v) : v;
2558 }
2559#endif /* CONFIG_WITH_VALUE_LENGTH */
2560
2561 conditional = 1;
2562 flavor = f_recursive;
2563 /* FALLTHROUGH */
2564 case f_recursive:
2565 /* A recursive variable definition "var = value".
2566 The value is used verbatim. */
2567 p = value;
2568 break;
2569#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2570 case f_append:
2571 case f_prepend:
2572 {
2573 const enum variable_flavor org_flavor = flavor;
2574#else
2575 case f_append:
2576 {
2577#endif
2578
2579 /* If we have += but we're in a target variable context, we want to
2580 append only with other variables in the context of this target. */
2581 if (target_var)
2582 {
2583 append = 1;
2584 v = lookup_variable_in_set (varname, varname_len,
2585 current_variable_set_list->set);
2586
2587 /* Don't append from the global set if a previous non-appending
2588 target-specific variable definition exists. */
2589 if (v && !v->append)
2590 append = 0;
2591 }
2592#ifdef KMK
2593 else if ( g_pTopKbEvalData
2594 || ( varname_len > 3
2595 && varname[0] == '['
2596 && is_kbuild_object_variable_accessor (varname, varname_len)) )
2597 {
2598 v = kbuild_object_variable_pre_append (varname, varname_len,
2599 value, value_len, simple_value,
2600 origin, org_flavor == f_append, flocp);
2601 if (free_value)
2602 free (free_value);
2603 return v;
2604 }
2605#endif
2606#ifdef CONFIG_WITH_LOCAL_VARIABLES
2607 /* If 'local', restrict it to the current variable context. */
2608 else if (origin == o_local)
2609 v = lookup_variable_in_set (varname, varname_len,
2610 current_variable_set_list->set);
2611#endif
2612 else
2613 v = lookup_variable (varname, varname_len);
2614
2615 if (v == 0)
2616 {
2617 /* There was no old value.
2618 This becomes a normal recursive definition. */
2619 p = value;
2620 flavor = f_recursive;
2621 }
2622 else
2623 {
2624#ifdef CONFIG_WITH_VALUE_LENGTH
2625 v->append = append;
2626 v = do_variable_definition_append (flocp, v, value, value_len,
2627 simple_value, origin,
2628# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2629 org_flavor == f_append);
2630# else
2631 1);
2632# endif
2633 if (free_value)
2634 free (free_value);
2635 return v;
2636#else /* !CONFIG_WITH_VALUE_LENGTH */
2637
2638 /* Paste the old and new values together in VALUE. */
2639
2640 unsigned int oldlen, vallen;
2641 const char *val;
2642 char *tp = NULL;
2643
2644 val = value;
2645 if (v->recursive)
2646 /* The previous definition of the variable was recursive.
2647 The new value is the unexpanded old and new values. */
2648 flavor = f_recursive;
2649 else
2650 /* The previous definition of the variable was simple.
2651 The new value comes from the old value, which was expanded
2652 when it was set; and from the expanded new value. Allocate
2653 memory for the expansion as we may still need the rest of the
2654 buffer if we're looking at a target-specific variable. */
2655 val = tp = allocated_variable_expand (val);
2656
2657 oldlen = strlen (v->value);
2658 vallen = strlen (val);
2659 p = alloc_value = xmalloc (oldlen + 1 + vallen + 1);
2660# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2661 if (org_flavor == f_prepend)
2662 {
2663 memcpy (alloc_value, val, vallen);
2664 alloc_value[oldlen] = ' ';
2665 memcpy (&alloc_value[oldlen + 1], v->value, oldlen + 1);
2666 }
2667 else
2668# endif /* CONFIG_WITH_PREPEND_ASSIGNMENT */
2669 {
2670 memcpy (alloc_value, v->value, oldlen);
2671 alloc_value[oldlen] = ' ';
2672 memcpy (&alloc_value[oldlen + 1], val, vallen + 1);
2673 }
2674
2675 free (tp);
2676#endif /* !CONFIG_WITH_VALUE_LENGTH */
2677 }
2678 }
2679 }
2680
2681#ifdef __MSDOS__
2682 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
2683 non-Unix systems don't conform to this default configuration (in
2684 fact, most of them don't even have '/bin'). On the other hand,
2685 $SHELL in the environment, if set, points to the real pathname of
2686 the shell.
2687 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
2688 the Makefile override $SHELL from the environment. But first, we
2689 look for the basename of the shell in the directory where SHELL=
2690 points, and along the $PATH; if it is found in any of these places,
2691 we define $SHELL to be the actual pathname of the shell. Thus, if
2692 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
2693 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
2694 defining SHELL to be "d:/unix/bash.exe". */
2695 if ((origin == o_file || origin == o_override)
2696 && strcmp (varname, "SHELL") == 0)
2697 {
2698 PATH_VAR (shellpath);
2699 extern char * __dosexec_find_on_path (const char *, char *[], char *);
2700
2701 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
2702 if (__dosexec_find_on_path (p, NULL, shellpath))
2703 {
2704 char *tp;
2705
2706 for (tp = shellpath; *tp; tp++)
2707 if (*tp == '\\')
2708 *tp = '/';
2709
2710 v = define_variable_loc (varname, varname_len,
2711 shellpath, origin, flavor == f_recursive,
2712 flocp);
2713 }
2714 else
2715 {
2716 const char *shellbase, *bslash;
2717 struct variable *pathv = lookup_variable ("PATH", 4);
2718 char *path_string;
2719 char *fake_env[2];
2720 size_t pathlen = 0;
2721
2722 shellbase = strrchr (p, '/');
2723 bslash = strrchr (p, '\\');
2724 if (!shellbase || bslash > shellbase)
2725 shellbase = bslash;
2726 if (!shellbase && p[1] == ':')
2727 shellbase = p + 1;
2728 if (shellbase)
2729 shellbase++;
2730 else
2731 shellbase = p;
2732
2733 /* Search for the basename of the shell (with standard
2734 executable extensions) along the $PATH. */
2735 if (pathv)
2736 pathlen = strlen (pathv->value);
2737 path_string = xmalloc (5 + pathlen + 2 + 1);
2738 /* On MSDOS, current directory is considered as part of $PATH. */
2739 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
2740 fake_env[0] = path_string;
2741 fake_env[1] = 0;
2742 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
2743 {
2744 char *tp;
2745
2746 for (tp = shellpath; *tp; tp++)
2747 if (*tp == '\\')
2748 *tp = '/';
2749
2750 v = define_variable_loc (varname, varname_len,
2751 shellpath, origin,
2752 flavor == f_recursive, flocp);
2753 }
2754 else
2755 v = lookup_variable (varname, varname_len);
2756
2757 free (path_string);
2758 }
2759 }
2760 else
2761#endif /* __MSDOS__ */
2762#ifdef WINDOWS32
2763 if ( varname_len == sizeof("SHELL") - 1 /* bird */
2764 && (origin == o_file || origin == o_override || origin == o_command)
2765 && streq (varname, "SHELL"))
2766 {
2767 extern const char *default_shell;
2768
2769 /* Call shell locator function. If it returns TRUE, then
2770 set no_default_sh_exe to indicate sh was found and
2771 set new value for SHELL variable. */
2772
2773 if (find_and_set_default_shell (p))
2774 {
2775 v = define_variable_in_set (varname, varname_len, default_shell,
2776# ifdef CONFIG_WITH_VALUE_LENGTH
2777 ~0U, 1 /* duplicate_value */,
2778# endif
2779 origin, flavor == f_recursive,
2780 (target_var
2781 ? current_variable_set_list->set
2782 : NULL),
2783 flocp);
2784 no_default_sh_exe = 0;
2785 }
2786 else
2787 {
2788 char *tp = alloc_value;
2789
2790 alloc_value = allocated_variable_expand (p);
2791
2792 if (find_and_set_default_shell (alloc_value))
2793 {
2794 v = define_variable_in_set (varname, varname_len, p,
2795#ifdef CONFIG_WITH_VALUE_LENGTH
2796 ~0U, 1 /* duplicate_value */,
2797#endif
2798 origin, flavor == f_recursive,
2799 (target_var
2800 ? current_variable_set_list->set
2801 : NULL),
2802 flocp);
2803 no_default_sh_exe = 0;
2804 }
2805 else
2806 v = lookup_variable (varname, varname_len);
2807
2808 free (tp);
2809 }
2810 }
2811 else
2812#endif
2813
2814 /* If we are defining variables inside an $(eval ...), we might have a
2815 different variable context pushed, not the global context (maybe we're
2816 inside a $(call ...) or something. Since this function is only ever
2817 invoked in places where we want to define globally visible variables,
2818 make sure we define this variable in the global set. */
2819
2820 v = define_variable_in_set (varname, varname_len, p,
2821#ifdef CONFIG_WITH_VALUE_LENGTH
2822 value_len, !alloc_value,
2823#endif
2824 origin, flavor == f_recursive,
2825#ifdef CONFIG_WITH_LOCAL_VARIABLES
2826 (target_var || origin == o_local
2827#else
2828 (target_var
2829#endif
2830 ? current_variable_set_list->set : NULL),
2831 flocp);
2832 v->append = append;
2833 v->conditional = conditional;
2834
2835#ifndef CONFIG_WITH_VALUE_LENGTH
2836 free (alloc_value);
2837#else
2838 if (free_value)
2839 free (free_value);
2840#endif
2841
2842 return v->special ? set_special_var (v) : v;
2843}
2844
2845
2846/* Parse P (a null-terminated string) as a variable definition.
2847
2848 If it is not a variable definition, return NULL and the contents of *VAR
2849 are undefined, except NAME is set to the first non-space character or NIL.
2850
2851 If it is a variable definition, return a pointer to the char after the
2852 assignment token and set the following fields (only) of *VAR:
2853 name : name of the variable (ALWAYS SET) (NOT NUL-TERMINATED!)
2854 length : length of the variable name
2855 value : value of the variable (nul-terminated)
2856 flavor : flavor of the variable
2857 Other values in *VAR are unchanged.
2858 */
2859
2860char *
2861parse_variable_definition (const char *p, struct variable *var)
2862{
2863 int wspace = 0;
2864 const char *e = NULL;
2865
2866/** @todo merge 4.2.1: parse_variable_definition does more now */
2867 NEXT_TOKEN (p);
2868 var->name = (char *)p;
2869 var->length = 0;
2870
2871 while (1)
2872 {
2873 int c = *p++;
2874
2875 /* If we find a comment or EOS, it's not a variable definition. */
2876 if (STOP_SET (c, MAP_COMMENT|MAP_NUL))
2877 return NULL;
2878
2879 if (c == '$')
2880 {
2881 /* This begins a variable expansion reference. Make sure we don't
2882 treat chars inside the reference as assignment tokens. */
2883 char closeparen;
2884 unsigned int count;
2885
2886 c = *p++;
2887 if (c == '(')
2888 closeparen = ')';
2889 else if (c == '{')
2890 closeparen = '}';
2891 else if (c == '\0')
2892 return NULL;
2893 else
2894 /* '$$' or '$X'. Either way, nothing special to do here. */
2895 continue;
2896
2897 /* P now points past the opening paren or brace.
2898 Count parens or braces until it is matched. */
2899 for (count = 1; *p != '\0'; ++p)
2900 {
2901 if (*p == closeparen && --count == 0)
2902 {
2903 ++p;
2904 break;
2905 }
2906 if (*p == c)
2907 ++count;
2908 }
2909 continue;
2910 }
2911
2912 /* If we find whitespace skip it, and remember we found it. */
2913 if (ISBLANK (c))
2914 {
2915 wspace = 1;
2916 e = p - 1;
2917 NEXT_TOKEN (p);
2918 c = *p;
2919 if (c == '\0')
2920 return NULL;
2921 ++p;
2922 }
2923
2924
2925 if (c == '=')
2926 {
2927 var->flavor = f_recursive;
2928 if (! e)
2929 e = p - 1;
2930 break;
2931 }
2932
2933 /* Match assignment variants (:=, +=, ?=, !=) */
2934 if (*p == '=')
2935 {
2936 switch (c)
2937 {
2938 case ':':
2939 var->flavor = f_simple;
2940 break;
2941 case '+':
2942 var->flavor = f_append;
2943 break;
2944#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2945 case '<':
2946 var->flavor = f_prepend;
2947 break;
2948#endif
2949 case '?':
2950 var->flavor = f_conditional;
2951 break;
2952 case '!':
2953 var->flavor = f_shell;
2954 break;
2955 default:
2956 /* If we skipped whitespace, non-assignments means no var. */
2957 if (wspace)
2958 return NULL;
2959
2960 /* Might be assignment, or might be $= or #=. Check. */
2961 continue;
2962 }
2963 if (! e)
2964 e = p - 1;
2965 ++p;
2966 break;
2967 }
2968
2969 /* Check for POSIX ::= syntax */
2970 if (c == ':')
2971 {
2972 /* A colon other than :=/::= is not a variable defn. */
2973 if (*p != ':' || p[1] != '=')
2974 return NULL;
2975
2976 /* POSIX allows ::= to be the same as GNU make's := */
2977 var->flavor = f_simple;
2978 if (! e)
2979 e = p - 1;
2980 p += 2;
2981 break;
2982 }
2983
2984 /* If we skipped whitespace, non-assignments means no var. */
2985 if (wspace)
2986 return NULL;
2987 }
2988
2989 var->length = e - var->name;
2990 var->value = next_token (p);
2991#ifdef CONFIG_WITH_VALUE_LENGTH
2992 var->value_alloc_len = ~(unsigned int)0;
2993 var->value_length = -1;
2994# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2995 var->rdonly_val = 0;
2996# endif
2997#endif
2998 return (char *)p;
2999}
3000
3001
3002/* Try to interpret LINE (a null-terminated string) as a variable definition.
3003
3004 If LINE was recognized as a variable definition, a pointer to its 'struct
3005 variable' is returned. If LINE is not a variable definition, NULL is
3006 returned. */
3007
3008struct variable *
3009assign_variable_definition (struct variable *v, const char *line IF_WITH_VALUE_LENGTH_PARAM(char *eos))
3010{
3011#ifndef CONFIG_WITH_VALUE_LENGTH
3012 char *name;
3013#endif
3014
3015 if (!parse_variable_definition (line, v))
3016 return NULL;
3017
3018#ifdef CONFIG_WITH_VALUE_LENGTH
3019 if (eos)
3020 {
3021 v->value_length = eos - v->value;
3022 assert (strchr (v->value, '\0') == eos);
3023 }
3024#endif
3025
3026 /* Expand the name, so "$(foo)bar = baz" works. */
3027#ifndef CONFIG_WITH_VALUE_LENGTH
3028 name = alloca (v->length + 1);
3029 memcpy (name, v->name, v->length);
3030 name[v->length] = '\0';
3031 v->name = allocated_variable_expand (name);
3032#else /* CONFIG_WITH_VALUE_LENGTH */
3033 v->name = allocated_variable_expand_2 (v->name, v->length, NULL);
3034#endif /* CONFIG_WITH_VALUE_LENGTH */
3035
3036 if (v->name[0] == '\0')
3037 O (fatal, &v->fileinfo, _("empty variable name"));
3038
3039 return v;
3040}
3041
3042
3043/* Try to interpret LINE (a null-terminated string) as a variable definition.
3044
3045 ORIGIN may be o_file, o_override, o_env, o_env_override, o_local,
3046 or o_command specifying that the variable definition comes
3047 from a makefile, an override directive, the environment with
3048 or without the -e switch, or the command line.
3049
3050 See the comments for assign_variable_definition().
3051
3052 If LINE was recognized as a variable definition, a pointer to its 'struct
3053 variable' is returned. If LINE is not a variable definition, NULL is
3054 returned. */
3055
3056struct variable *
3057try_variable_definition (const floc *flocp, const char *line
3058 IF_WITH_VALUE_LENGTH_PARAM(char *eos),
3059 enum variable_origin origin, int target_var)
3060{
3061 struct variable v;
3062 struct variable *vp;
3063
3064 if (flocp != 0)
3065 v.fileinfo = *flocp;
3066 else
3067 v.fileinfo.filenm = 0;
3068
3069#ifndef CONFIG_WITH_VALUE_LENGTH
3070 if (!assign_variable_definition (&v, line))
3071 return 0;
3072
3073 vp = do_variable_definition (flocp, v.name, v.value,
3074 origin, v.flavor, target_var);
3075#else
3076 if (!assign_variable_definition (&v, line, eos))
3077 return 0;
3078
3079 vp = do_variable_definition_2 (flocp, v.name, v.value, v.value_length,
3080 0, NULL, origin, v.flavor, target_var);
3081#endif
3082
3083#ifndef CONFIG_WITH_STRCACHE2
3084 free (v.name);
3085#else
3086 free ((char *)v.name);
3087#endif
3088
3089 return vp;
3090}
3091
3092
3093#if defined (CONFIG_WITH_COMPILER) || defined (CONFIG_WITH_MAKE_STATS)
3094static unsigned long var_stats_evalvals, var_stats_evalvaled;
3095static unsigned long var_stats_expands, var_stats_expanded;
3096#endif
3097#ifdef CONFIG_WITH_COMPILER
3098static unsigned long var_stats_expandprogs, var_stats_evalprogs;
3099#endif
3100#ifdef CONFIG_WITH_MAKE_STATS
3101static unsigned long var_stats_changes, var_stats_changed;
3102static unsigned long var_stats_reallocs, var_stats_realloced;
3103static unsigned long var_stats_references, var_stats_referenced;
3104static unsigned long var_stats_val_len, var_stats_val_alloc_len;
3105static unsigned long var_stats_val_rdonly_len;
3106#endif
3107
3108/* Print information for variable V, prefixing it with PREFIX. */
3109
3110static void
3111print_variable (const void *item, void *arg)
3112{
3113 const struct variable *v = item;
3114 const char *prefix = arg;
3115 const char *origin;
3116#ifdef KMK
3117 const struct variable *alias = v;
3118 RESOLVE_ALIAS_VARIABLE(v);
3119#endif
3120
3121 switch (v->origin)
3122 {
3123 case o_automatic:
3124 origin = _("automatic");
3125 break;
3126 case o_default:
3127 origin = _("default");
3128 break;
3129 case o_env:
3130 origin = _("environment");
3131 break;
3132 case o_file:
3133 origin = _("makefile");
3134 break;
3135 case o_env_override:
3136 origin = _("environment under -e");
3137 break;
3138 case o_command:
3139 origin = _("command line");
3140 break;
3141 case o_override:
3142 origin = _("'override' directive");
3143 break;
3144#ifdef CONFIG_WITH_LOCAL_VARIABLES
3145 case o_local:
3146 origin = _("`local' directive");
3147 break;
3148#endif
3149 case o_invalid:
3150 default:
3151 abort ();
3152 }
3153 fputs ("# ", stdout);
3154 fputs (origin, stdout);
3155 if (v->private_var)
3156 fputs (" private", stdout);
3157#ifndef KMK
3158 if (v->fileinfo.filenm)
3159 printf (_(" (from '%s', line %lu)"),
3160 v->fileinfo.filenm, v->fileinfo.lineno + v->fileinfo.offset);
3161#else /* KMK */
3162 if (alias->fileinfo.filenm)
3163 printf (_(" (from '%s', line %lu)"),
3164 alias->fileinfo.filenm, alias->fileinfo.lineno);
3165 if (alias->aliased)
3166 fputs (" aliased", stdout);
3167 if (alias->alias)
3168 printf (_(", alias for '%s'"), v->name);
3169#endif /* KMK */
3170
3171#if defined (CONFIG_WITH_COMPILER) || defined (CONFIG_WITH_MAKE_STATS)
3172 if (v->evalval_count != 0)
3173 {
3174# ifdef CONFIG_WITH_MAKE_STATS
3175 printf (_(", %u evalvals (%llu ticks)"), v->evalval_count, v->cTicksEvalVal);
3176# else
3177 printf (_(", %u evalvals"), v->evalval_count);
3178# endif
3179 var_stats_evalvaled++;
3180 }
3181 var_stats_evalvals += v->evalval_count;
3182
3183 if (v->expand_count != 0)
3184 {
3185 printf (_(", %u expands"), v->expand_count);
3186 var_stats_expanded++;
3187 }
3188 var_stats_expands += v->expand_count;
3189
3190# ifdef CONFIG_WITH_COMPILER
3191 if (v->evalprog != 0)
3192 {
3193 printf (_(", evalprog"));
3194 var_stats_evalprogs++;
3195 }
3196 if (v->expandprog != 0)
3197 {
3198 printf (_(", expandprog"));
3199 var_stats_expandprogs++;
3200 }
3201# endif
3202#endif
3203
3204#ifdef CONFIG_WITH_MAKE_STATS
3205 if (v->changes != 0)
3206 {
3207 printf (_(", %u changes"), v->changes);
3208 var_stats_changed++;
3209 }
3210 var_stats_changes += v->changes;
3211
3212 if (v->reallocs != 0)
3213 {
3214 printf (_(", %u reallocs"), v->reallocs);
3215 var_stats_realloced++;
3216 }
3217 var_stats_reallocs += v->reallocs;
3218
3219 if (v->references != 0)
3220 {
3221 printf (_(", %u references"), v->references);
3222 var_stats_referenced++;
3223 }
3224 var_stats_references += v->references;
3225
3226 var_stats_val_len += v->value_length;
3227 if (v->value_alloc_len)
3228 var_stats_val_alloc_len += v->value_alloc_len;
3229 else
3230 var_stats_val_rdonly_len += v->value_length;
3231 assert (v->value_length == strlen (v->value));
3232 /*assert (v->rdonly_val ? !v->value_alloc_len : v->value_alloc_len > v->value_length); - FIXME */
3233#endif /* CONFIG_WITH_MAKE_STATS */
3234 putchar ('\n');
3235 fputs (prefix, stdout);
3236
3237 /* Is this a 'define'? */
3238 if (v->recursive && strchr (v->value, '\n') != 0)
3239#ifndef KMK /** @todo language feature for aliases */
3240 printf ("define %s\n%s\nendef\n", v->name, v->value);
3241#else
3242 printf ("define %s\n%s\nendef\n", alias->name, v->value);
3243#endif
3244 else
3245 {
3246 char *p;
3247
3248#ifndef KMK /** @todo language feature for aliases */
3249 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
3250#else
3251 printf ("%s %s= ", alias->name, v->recursive ? v->append ? "+" : "" : ":");
3252#endif
3253
3254 /* Check if the value is just whitespace. */
3255 p = next_token (v->value);
3256 if (p != v->value && *p == '\0')
3257 /* All whitespace. */
3258 printf ("$(subst ,,%s)", v->value);
3259 else if (v->recursive)
3260 fputs (v->value, stdout);
3261 else
3262 /* Double up dollar signs. */
3263 for (p = v->value; *p != '\0'; ++p)
3264 {
3265 if (*p == '$')
3266 putchar ('$');
3267 putchar (*p);
3268 }
3269 putchar ('\n');
3270 }
3271}
3272
3273
3274static void
3275print_auto_variable (const void *item, void *arg)
3276{
3277 const struct variable *v = item;
3278
3279 if (v->origin == o_automatic)
3280 print_variable (item, arg);
3281}
3282
3283
3284static void
3285print_noauto_variable (const void *item, void *arg)
3286{
3287 const struct variable *v = item;
3288
3289 if (v->origin != o_automatic)
3290 print_variable (item, arg);
3291}
3292
3293
3294/* Print all the variables in SET. PREFIX is printed before
3295 the actual variable definitions (everything else is comments). */
3296
3297#ifndef KMK
3298static
3299#endif
3300void
3301print_variable_set (struct variable_set *set, const char *prefix, int pauto)
3302{
3303#if defined (CONFIG_WITH_COMPILER) || defined (CONFIG_WITH_MAKE_STATS)
3304 var_stats_expands = var_stats_expanded = var_stats_evalvals
3305 = var_stats_evalvaled = 0;
3306#endif
3307#ifdef CONFIG_WITH_COMPILER
3308 var_stats_expandprogs = var_stats_evalprogs = 0;
3309#endif
3310#ifdef CONFIG_WITH_MAKE_STATS
3311 var_stats_changes = var_stats_changed = var_stats_reallocs
3312 = var_stats_realloced = var_stats_references = var_stats_referenced
3313 = var_stats_val_len = var_stats_val_alloc_len
3314 = var_stats_val_rdonly_len = 0;
3315#endif
3316
3317 hash_map_arg (&set->table, (pauto ? print_auto_variable : print_variable),
3318 (void *)prefix);
3319
3320 if (set->table.ht_fill)
3321 {
3322#ifdef CONFIG_WITH_MAKE_STATS
3323 unsigned long fragmentation;
3324
3325 fragmentation = var_stats_val_alloc_len - (var_stats_val_len - var_stats_val_rdonly_len);
3326 printf(_("# variable set value stats:\n\
3327# strings %7lu bytes, readonly %6lu bytes\n"),
3328 var_stats_val_len, var_stats_val_rdonly_len);
3329
3330 if (var_stats_val_alloc_len)
3331 printf(_("# allocated %7lu bytes, fragmentation %6lu bytes (%u%%)\n"),
3332 var_stats_val_alloc_len, fragmentation,
3333 (unsigned int)((100.0 * fragmentation) / var_stats_val_alloc_len));
3334
3335 if (var_stats_changed)
3336 printf(_("# changed %5lu (%2u%%), changes %6lu\n"),
3337 var_stats_changed,
3338 (unsigned int)((100.0 * var_stats_changed) / set->table.ht_fill),
3339 var_stats_changes);
3340
3341 if (var_stats_realloced)
3342 printf(_("# reallocated %5lu (%2u%%), reallocations %6lu\n"),
3343 var_stats_realloced,
3344 (unsigned int)((100.0 * var_stats_realloced) / set->table.ht_fill),
3345 var_stats_reallocs);
3346
3347 if (var_stats_referenced)
3348 printf(_("# referenced %5lu (%2u%%), references %6lu\n"),
3349 var_stats_referenced,
3350 (unsigned int)((100.0 * var_stats_referenced) / set->table.ht_fill),
3351 var_stats_references);
3352#endif
3353#if defined (CONFIG_WITH_COMPILER) || defined (CONFIG_WITH_MAKE_STATS)
3354 if (var_stats_evalvals)
3355 printf(_("# evalvaled %5lu (%2u%%), evalval calls %6lu\n"),
3356 var_stats_evalvaled,
3357 (unsigned int)((100.0 * var_stats_evalvaled) / set->table.ht_fill),
3358 var_stats_evalvals);
3359 if (var_stats_expands)
3360 printf(_("# expanded %5lu (%2u%%), expands %6lu\n"),
3361 var_stats_expanded,
3362 (unsigned int)((100.0 * var_stats_expanded) / set->table.ht_fill),
3363 var_stats_expands);
3364#endif
3365#ifdef CONFIG_WITH_COMPILER
3366 if (var_stats_expandprogs || var_stats_evalprogs)
3367 printf(_("# eval progs %5lu (%2u%%), expand progs %6lu (%2u%%)\n"),
3368 var_stats_evalprogs,
3369 (unsigned int)((100.0 * var_stats_evalprogs) / set->table.ht_fill),
3370 var_stats_expandprogs,
3371 (unsigned int)((100.0 * var_stats_expandprogs) / set->table.ht_fill));
3372#endif
3373 }
3374
3375 fputs (_("# variable set hash-table stats:\n"), stdout);
3376 fputs ("# ", stdout);
3377 hash_print_stats (&set->table, stdout);
3378 putc ('\n', stdout);
3379}
3380
3381/* Print the data base of variables. */
3382
3383void
3384print_variable_data_base (void)
3385{
3386 puts (_("\n# Variables\n"));
3387
3388 print_variable_set (&global_variable_set, "", 0);
3389
3390 puts (_("\n# Pattern-specific Variable Values"));
3391
3392 {
3393 struct pattern_var *p;
3394 unsigned int rules = 0;
3395
3396 for (p = pattern_vars; p != 0; p = p->next)
3397 {
3398 ++rules;
3399 printf ("\n%s :\n", p->target);
3400 print_variable (&p->variable, (void *)"# ");
3401 }
3402
3403 if (rules == 0)
3404 puts (_("\n# No pattern-specific variable values."));
3405 else
3406 printf (_("\n# %u pattern-specific variable values"), rules);
3407 }
3408
3409#ifdef CONFIG_WITH_STRCACHE2
3410 strcache2_print_stats (&variable_strcache, "# ");
3411#endif
3412}
3413
3414#ifdef CONFIG_WITH_PRINT_STATS_SWITCH
3415void
3416print_variable_stats (void)
3417{
3418 fputs (_("\n# Global variable hash-table stats:\n# "), stdout);
3419 hash_print_stats (&global_variable_set.table, stdout);
3420 fputs ("\n", stdout);
3421}
3422#endif
3423
3424/* Print all the local variables of FILE. */
3425
3426void
3427print_file_variables (const struct file *file)
3428{
3429 if (file->variables != 0)
3430 print_variable_set (file->variables->set, "# ", 1);
3431}
3432
3433void
3434print_target_variables (const struct file *file)
3435{
3436 if (file->variables != 0)
3437 {
3438 int l = strlen (file->name);
3439 char *t = alloca (l + 3);
3440
3441 strcpy (t, file->name);
3442 t[l] = ':';
3443 t[l+1] = ' ';
3444 t[l+2] = '\0';
3445
3446 hash_map_arg (&file->variables->set->table, print_noauto_variable, t);
3447 }
3448}
3449
3450#ifdef WINDOWS32
3451void
3452sync_Path_environment (void)
3453{
3454 char *path = allocated_variable_expand ("$(PATH)");
3455 static char *environ_path = NULL;
3456
3457 if (!path)
3458 return;
3459
3460 /* If done this before, free the previous entry before allocating new one. */
3461 free (environ_path);
3462
3463 /* Create something WINDOWS32 world can grok. */
3464 convert_Path_to_windows32 (path, ';');
3465 environ_path = xstrdup (concat (3, "PATH", "=", path));
3466 putenv (environ_path);
3467 free (path);
3468}
3469#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