Built win.arm64 against r3658
[kbuild-mirror.git] / src / kmk / variable.c
blobc79601d6fb2aa2132b878313cd8866e82d1e7f67
1 /* Internals of variables for GNU Make.
2 Copyright (C) 1988-2016 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
8 version.
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
17 #include "makeint.h"
19 #include <assert.h>
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
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
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. */
62 static size_t global_variable_generation = 0;
63 #endif
65 /* Incremented every time we add or remove a global variable. */
66 static unsigned long variable_changenum;
68 /* Chain of all pattern-specific variables. */
70 static struct pattern_var *pattern_vars;
72 /* Pointer to the last struct in the pack of a specific size, from 1 to 255.*/
74 static struct pattern_var *last_pattern_vars[256];
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. */
83 struct pattern_var *
84 create_pattern_var (const char *target, const char *suffix)
86 register unsigned int len = strlen (target);
87 register struct pattern_var *p = xmalloc (sizeof (struct pattern_var));
89 if (pattern_vars != 0)
91 if (len < 256 && last_pattern_vars[len] != 0)
93 p->next = last_pattern_vars[len]->next;
94 last_pattern_vars[len]->next = p;
96 else
98 /* Find the position where we can insert this variable. */
99 register struct pattern_var **v;
101 for (v = &pattern_vars; ; v = &(*v)->next)
103 /* Insert at the end of the pack so that patterns with the
104 same length appear in the order they were defined .*/
106 if (*v == 0 || (*v)->len > len)
108 p->next = *v;
109 *v = p;
110 break;
115 else
117 pattern_vars = p;
118 p->next = 0;
121 p->target = target;
122 p->len = len;
123 p->suffix = suffix + 1;
125 if (len < 256)
126 last_pattern_vars[len] = p;
128 return p;
131 /* Look up a target in the pattern-specific variable list. */
133 static struct pattern_var *
134 lookup_pattern_var (struct pattern_var *start, const char *target)
136 struct pattern_var *p;
137 unsigned int targlen = strlen (target);
139 for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
141 const char *stem;
142 unsigned int stemlen;
144 if (p->len > targlen)
145 /* It can't possibly match. */
146 continue;
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;
153 /* Compare the text in the pattern before the stem, if any. */
154 if (stem > target && !strneq (p->target, target, stem - target))
155 continue;
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;
167 return p;
170 #ifdef CONFIG_WITH_STRCACHE2
171 struct strcache2 variable_strcache;
172 #endif
174 /* Hash table of all global variable definitions. */
176 #ifndef CONFIG_WITH_STRCACHE2
177 static unsigned long
178 variable_hash_1 (const void *keyv)
180 struct variable const *key = (struct variable const *) keyv;
181 return_STRING_N_HASH_1 (key->name, key->length);
184 static unsigned long
185 variable_hash_2 (const void *keyv)
187 struct variable const *key = (struct variable const *) keyv;
188 return_STRING_N_HASH_2 (key->name, key->length);
191 static int
192 variable_hash_cmp (const void *xv, const void *yv)
194 struct variable const *x = (struct variable const *) xv;
195 struct variable const *y = (struct variable const *) yv;
196 int result = x->length - y->length;
197 if (result)
198 return result;
200 return_STRING_N_COMPARE (x->name, y->name, x->length);
202 #endif /* !CONFIG_WITH_STRCACHE2 */
204 #ifndef VARIABLE_BUCKETS
205 # ifdef KMK /* Move to Makefile.kmk? (insanely high, but wtf, it gets the collitions down) */
206 # define VARIABLE_BUCKETS 65535
207 # else /*!KMK*/
208 #define VARIABLE_BUCKETS 523
209 # endif /*!KMK*/
210 #endif
211 #ifndef PERFILE_VARIABLE_BUCKETS
212 # ifdef KMK /* Move to Makefile.kmk? */
213 # define PERFILE_VARIABLE_BUCKETS 127
214 # else
215 #define PERFILE_VARIABLE_BUCKETS 23
216 # endif
217 #endif
218 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
219 # ifdef KMK /* Move to Makefile.kmk? */
220 # define SMALL_SCOPE_VARIABLE_BUCKETS 63
221 # else
222 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
223 # endif
224 #endif
225 #ifndef ENVIRONMENT_VARIABLE_BUCKETS /* added by bird. */
226 # define ENVIRONMENT_VARIABLE_BUCKETS 256
227 #endif
230 #ifdef KMK /* Drop the 'static' */
231 struct variable_set global_variable_set;
232 struct variable_set_list global_setlist
233 #else
234 static struct variable_set global_variable_set;
235 static struct variable_set_list global_setlist
236 #endif
237 = { 0, &global_variable_set, 0 };
238 struct variable_set_list *current_variable_set_list = &global_setlist;
240 /* Implement variables. */
242 void
243 init_hash_global_variable_set (void)
245 #ifndef CONFIG_WITH_STRCACHE2
246 hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
247 variable_hash_1, variable_hash_2, variable_hash_cmp);
248 #else /* CONFIG_WITH_STRCACHE2 */
249 strcache2_init (&variable_strcache, "variable", 262144, 0, 0, 0);
250 hash_init_strcached (&global_variable_set.table, VARIABLE_BUCKETS,
251 &variable_strcache, offsetof (struct variable, name));
252 #endif /* CONFIG_WITH_STRCACHE2 */
255 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
256 LENGTH is the length of NAME, which does not need to be null-terminated.
257 ORIGIN specifies the origin of the variable (makefile, command line
258 or environment).
259 If RECURSIVE is nonzero a flag is set in the variable saying
260 that it should be recursively re-expanded. */
262 #ifdef CONFIG_WITH_VALUE_LENGTH
263 struct variable *
264 define_variable_in_set (const char *name, unsigned int length,
265 const char *value, unsigned int value_len,
266 int duplicate_value, enum variable_origin origin,
267 int recursive, struct variable_set *set,
268 const floc *flocp)
269 #else
270 struct variable *
271 define_variable_in_set (const char *name, unsigned int length,
272 const char *value, enum variable_origin origin,
273 int recursive, struct variable_set *set,
274 const floc *flocp)
275 #endif
277 struct variable *v;
278 struct variable **var_slot;
279 struct variable var_key;
281 #ifdef KMK
282 if (set == NULL || set == &global_variable_set)
283 global_variable_generation++;
284 #endif
286 if (env_overrides && origin == o_env)
287 origin = o_env_override;
289 #ifndef KMK
290 if (set == NULL)
291 set = &global_variable_set;
292 #else /* KMK */
293 /* Intercept kBuild object variable definitions. */
294 if (name[0] == '[' && length > 3)
296 v = try_define_kbuild_object_variable_via_accessor (name, length,
297 value, value_len, duplicate_value,
298 origin, recursive, flocp);
299 if (v != VAR_NOT_KBUILD_ACCESSOR)
300 return v;
302 if (set == NULL)
304 if (g_pTopKbEvalData)
305 return define_kbuild_object_variable_in_top_obj (name, length,
306 value, value_len, duplicate_value,
307 origin, recursive, flocp);
308 set = &global_variable_set;
310 #endif /* KMK */
312 #ifndef CONFIG_WITH_STRCACHE2
313 var_key.name = (char *) name;
314 var_key.length = length;
315 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
316 v = *var_slot;
318 #ifdef VMS
319 /* VMS does not populate envp[] with DCL symbols and logical names which
320 historically are mapped to environent variables.
321 If the variable is not yet defined, then we need to check if getenv()
322 can find it. Do not do this for origin == o_env to avoid infinte
323 recursion */
324 if (HASH_VACANT (v) && (origin != o_env))
326 struct variable * vms_variable;
327 char * vname = alloca (length + 1);
328 char * vvalue;
330 strncpy (vname, name, length);
331 vvalue = getenv(vname);
333 /* Values starting with '$' are probably foreign commands.
334 We want to treat them as Shell aliases and not look them up here */
335 if ((vvalue != NULL) && (vvalue[0] != '$'))
337 vms_variable = lookup_variable(name, length);
338 /* Refresh the slot */
339 var_slot = (struct variable **) hash_find_slot (&set->table,
340 &var_key);
341 v = *var_slot;
344 #endif
346 /* if (env_overrides && origin == o_env)
347 origin = o_env_override; - bird moved this up */
349 #else /* CONFIG_WITH_STRCACHE2 */
350 name = strcache2_add (&variable_strcache, name, length);
351 if ( set != &global_variable_set
352 || !(v = strcache2_get_user_val (&variable_strcache, name)))
354 var_key.name = name;
355 var_key.length = length;
356 var_slot = (struct variable **) hash_find_slot_strcached (&set->table, &var_key);
357 v = *var_slot;
359 else
361 assert (!v || (v->name == name && !HASH_VACANT (v)));
362 var_slot = 0;
364 #endif /* CONFIG_WITH_STRCACHE2 */
365 if (! HASH_VACANT (v))
367 #ifdef KMK
368 RESOLVE_ALIAS_VARIABLE(v);
369 #endif
370 if (env_overrides && v->origin == o_env)
371 /* V came from in the environment. Since it was defined
372 before the switches were parsed, it wasn't affected by -e. */
373 v->origin = o_env_override;
375 /* A variable of this name is already defined.
376 If the old definition is from a stronger source
377 than this one, don't redefine it. */
378 if ((int) origin >= (int) v->origin)
380 #ifdef CONFIG_WITH_VALUE_LENGTH
381 if (value_len == ~0U)
382 value_len = strlen (value);
383 else
384 assert (value_len == strlen (value));
385 if (!duplicate_value || duplicate_value == -1)
387 # ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
388 if (v->value != 0 && !v->rdonly_val)
389 free (v->value);
390 v->rdonly_val = duplicate_value == -1;
391 v->value = (char *) value;
392 v->value_alloc_len = 0;
393 # else
394 if (v->value != 0)
395 free (v->value);
396 v->value = (char *) value;
397 v->value_alloc_len = value_len + 1;
398 # endif
400 else
402 if (v->value_alloc_len <= value_len)
404 # ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
405 if (v->rdonly_val)
406 v->rdonly_val = 0;
407 else
408 # endif
409 free (v->value);
410 v->value_alloc_len = VAR_ALIGN_VALUE_ALLOC (value_len + 1);
411 v->value = xmalloc (v->value_alloc_len);
412 MAKE_STATS_2(v->reallocs++);
414 memcpy (v->value, value, value_len + 1);
416 v->value_length = value_len;
417 #else /* !CONFIG_WITH_VALUE_LENGTH */
418 free (v->value);
419 v->value = xstrdup (value);
420 #endif /* !CONFIG_WITH_VALUE_LENGTH */
421 if (flocp != 0)
422 v->fileinfo = *flocp;
423 else
424 v->fileinfo.filenm = 0;
425 v->origin = origin;
426 v->recursive = recursive;
427 VARIABLE_CHANGED (v);
429 return v;
432 /* Create a new variable definition and add it to the hash table. */
434 #ifndef CONFIG_WITH_ALLOC_CACHES
435 v = xmalloc (sizeof (struct variable));
436 #else
437 v = alloccache_alloc (&variable_cache);
438 #endif
439 #ifndef CONFIG_WITH_STRCACHE2
440 v->name = xstrndup (name, length);
441 #else
442 v->name = name; /* already cached. */
443 #endif
444 v->length = length;
445 hash_insert_at (&set->table, v, var_slot);
446 if (set == &global_variable_set)
447 ++variable_changenum;
449 #ifdef CONFIG_WITH_VALUE_LENGTH
450 if (value_len == ~0U)
451 value_len = strlen (value);
452 else
453 assert (value_len == strlen (value));
454 v->value_length = value_len;
455 if (!duplicate_value || duplicate_value == -1)
457 # ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
458 v->rdonly_val = duplicate_value == -1;
459 v->value_alloc_len = v->rdonly_val ? 0 : value_len + 1;
460 # endif
461 v->value = (char *)value;
463 else
465 # ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
466 v->rdonly_val = 0;
467 # endif
468 v->value_alloc_len = VAR_ALIGN_VALUE_ALLOC (value_len + 1);
469 v->value = xmalloc (v->value_alloc_len);
470 memcpy (v->value, value, value_len + 1);
472 #else /* !CONFIG_WITH_VALUE_LENGTH */
473 v->value = xstrdup (value);
474 #endif /* !CONFIG_WITH_VALUE_LENGTH */
475 if (flocp != 0)
476 v->fileinfo = *flocp;
477 else
478 v->fileinfo.filenm = 0;
479 v->origin = origin;
480 v->recursive = recursive;
481 v->special = 0;
482 v->expanding = 0;
483 v->exp_count = 0;
484 v->per_target = 0;
485 v->append = 0;
486 v->private_var = 0;
487 #ifdef KMK
488 v->alias = 0;
489 v->aliased = 0;
490 #endif
491 v->export = v_default;
492 #ifdef CONFIG_WITH_COMPILER
493 v->recursive_without_dollar = 0;
494 v->evalprog = 0;
495 v->expandprog = 0;
496 v->evalval_count = 0;
497 v->expand_count = 0;
498 #else
499 MAKE_STATS_2(v->expand_count = 0);
500 MAKE_STATS_2(v->evalval_count = 0);
501 #endif
502 MAKE_STATS_2(v->changes = 0);
503 MAKE_STATS_2(v->reallocs = 0);
504 MAKE_STATS_2(v->references = 0);
505 MAKE_STATS_2(v->cTicksEvalVal = 0);
507 v->exportable = 1;
508 if (*name != '_' && (*name < 'A' || *name > 'Z')
509 && (*name < 'a' || *name > 'z'))
510 v->exportable = 0;
511 else
513 for (++name; *name != '\0'; ++name)
514 if (*name != '_' && (*name < 'a' || *name > 'z')
515 && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
516 break;
518 if (*name != '\0')
519 v->exportable = 0;
522 #ifdef CONFIG_WITH_STRCACHE2
523 /* If it's the global set, remember the variable. */
524 if (set == &global_variable_set)
525 strcache2_set_user_val (&variable_strcache, v->name, v);
526 #endif
527 return v;
531 /* Undefine variable named NAME in SET. LENGTH is the length of NAME, which
532 does not need to be null-terminated. ORIGIN specifies the origin of the
533 variable (makefile, command line or environment). */
535 static void
536 free_variable_name_and_value (const void *item)
538 struct variable *v = (struct variable *) item;
539 #ifndef CONFIG_WITH_STRCACHE2
540 free (v->name);
541 #endif
542 #ifdef CONFIG_WITH_COMPILER
543 if (v->evalprog || v->expandprog)
544 kmk_cc_variable_deleted (v);
545 #endif
546 #ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
547 if (!v->rdonly_val)
548 #endif
549 free (v->value);
552 void
553 free_variable_set (struct variable_set_list *list)
555 hash_map (&list->set->table, free_variable_name_and_value);
556 #ifndef CONFIG_WITH_ALLOC_CACHES
557 hash_free (&list->set->table, 1);
558 free (list->set);
559 free (list);
560 #else
561 hash_free_cached (&list->set->table, 1, &variable_cache);
562 alloccache_free (&variable_set_cache, list->set);
563 alloccache_free (&variable_set_list_cache, list);
564 #endif
567 void
568 undefine_variable_in_set (const char *name, unsigned int length,
569 enum variable_origin origin,
570 struct variable_set *set)
572 struct variable *v;
573 struct variable **var_slot;
574 struct variable var_key;
576 if (set == NULL)
577 set = &global_variable_set;
579 #ifndef CONFIG_WITH_STRCACHE2
580 var_key.name = (char *) name;
581 var_key.length = length;
582 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
583 #else
584 var_key.name = strcache2_lookup(&variable_strcache, name, length);
585 if (!var_key.name)
586 return;
587 var_key.length = length;
588 var_slot = (struct variable **) hash_find_slot_strcached (&set->table, &var_key);
589 #endif
590 #ifdef KMK
591 if (set == &global_variable_set)
592 global_variable_generation++;
593 #endif
595 if (env_overrides && origin == o_env)
596 origin = o_env_override;
598 v = *var_slot;
599 if (! HASH_VACANT (v))
601 #ifdef KMK
602 if (v->aliased || v->alias)
604 if (v->aliased)
605 OS (error, NULL, _("Cannot undefine the aliased variable '%s'"), v->name);
606 else
607 OS (error, NULL, _("Cannot undefine the variable alias '%s'"), v->name);
608 return;
610 #endif
612 if (env_overrides && v->origin == o_env)
613 /* V came from in the environment. Since it was defined
614 before the switches were parsed, it wasn't affected by -e. */
615 v->origin = o_env_override;
617 /* Undefine only if this undefinition is from an equal or stronger
618 source than the variable definition. */
619 if ((int) origin >= (int) v->origin)
621 hash_delete_at (&set->table, var_slot);
622 #ifdef CONFIG_WITH_STRCACHE2
623 if (set == &global_variable_set)
624 strcache2_set_user_val (&variable_strcache, v->name, NULL);
625 #endif
626 free_variable_name_and_value (v);
627 #ifndef CONFIG_WITH_ALLOC_CACHES
628 free (v);
629 #else
630 alloccache_free (&variable_cache, v);
631 #endif
632 if (set == &global_variable_set)
633 ++variable_changenum;
638 #ifdef KMK
639 /* Define variable named NAME as an alias of the variable TARGET.
640 SET defaults to the global set if NULL. FLOCP is just for completeness. */
642 struct variable *
643 define_variable_alias_in_set (const char *name, unsigned int length,
644 struct variable *target, enum variable_origin origin,
645 struct variable_set *set, const floc *flocp)
647 struct variable *v;
648 struct variable **var_slot;
650 #ifdef KMK
651 if (set == NULL || set == &global_variable_set)
652 global_variable_generation++;
653 #endif
655 /* Look it up the hash table slot for it. */
656 name = strcache2_add (&variable_strcache, name, length);
657 if ( set != &global_variable_set
658 || !(v = strcache2_get_user_val (&variable_strcache, name)))
660 struct variable var_key;
662 var_key.name = name;
663 var_key.length = length;
664 var_slot = (struct variable **) hash_find_slot_strcached (&set->table, &var_key);
665 v = *var_slot;
667 else
669 assert (!v || (v->name == name && !HASH_VACANT (v)));
670 var_slot = 0;
672 if (! HASH_VACANT (v))
674 /* A variable of this name is already defined.
675 If the old definition is from a stronger source
676 than this one, don't redefine it. */
678 if (env_overrides && v->origin == o_env)
679 /* V came from in the environment. Since it was defined
680 before the switches were parsed, it wasn't affected by -e. */
681 v->origin = o_env_override;
683 if ((int) origin < (int) v->origin)
684 return v;
686 if (v->value != 0 && !v->rdonly_val)
687 free (v->value);
688 VARIABLE_CHANGED (v);
690 else
692 /* Create a new variable definition and add it to the hash table. */
693 v = alloccache_alloc (&variable_cache);
694 v->name = name; /* already cached. */
695 v->length = length;
696 hash_insert_at (&set->table, v, var_slot);
697 v->special = 0;
698 v->expanding = 0;
699 v->exp_count = 0;
700 v->per_target = 0;
701 v->append = 0;
702 v->private_var = 0;
703 v->aliased = 0;
704 v->export = v_default;
705 #ifdef CONFIG_WITH_COMPILER
706 v->recursive_without_dollar = 0;
707 v->evalprog = 0;
708 v->expandprog = 0;
709 v->evalval_count = 0;
710 v->expand_count = 0;
711 #else
712 MAKE_STATS_2(v->expand_count = 0);
713 MAKE_STATS_2(v->evalval_count = 0);
714 #endif
715 MAKE_STATS_2(v->changes = 0);
716 MAKE_STATS_2(v->reallocs = 0);
717 MAKE_STATS_2(v->references = 0);
718 MAKE_STATS_2(v->cTicksEvalVal = 0);
719 v->exportable = 1;
720 if (*name != '_' && (*name < 'A' || *name > 'Z')
721 && (*name < 'a' || *name > 'z'))
722 v->exportable = 0;
723 else
725 for (++name; *name != '\0'; ++name)
726 if (*name != '_' && (*name < 'a' || *name > 'z')
727 && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
728 break;
730 if (*name != '\0')
731 v->exportable = 0;
734 /* If it's the global set, remember the variable. */
735 if (set == &global_variable_set)
736 strcache2_set_user_val (&variable_strcache, v->name, v);
739 /* Common variable setup. */
740 v->alias = 1;
741 v->rdonly_val = 1;
742 v->value = (char *)target;
743 v->value_length = sizeof(*target); /* Non-zero to provoke trouble. */
744 v->value_alloc_len = sizeof(*target);
745 if (flocp != 0)
746 v->fileinfo = *flocp;
747 else
748 v->fileinfo.filenm = 0;
749 v->origin = origin;
750 v->recursive = 0;
752 /* Mark the target as aliased. */
753 target->aliased = 1;
755 return v;
757 #endif /* KMK */
759 /* If the variable passed in is "special", handle its special nature.
760 Currently there are two such variables, both used for introspection:
761 .VARIABLES expands to a list of all the variables defined in this instance
762 of make.
763 .TARGETS expands to a list of all the targets defined in this
764 instance of make.
765 Returns the variable reference passed in. */
767 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
769 static struct variable *
770 lookup_special_var (struct variable *var)
772 static unsigned long last_changenum = 0;
775 /* This one actually turns out to be very hard, due to the way the parser
776 records targets. The way it works is that target information is collected
777 internally until make knows the target is completely specified. It unitl
778 it sees that some new construct (a new target or variable) is defined that
779 it knows the previous one is done. In short, this means that if you do
780 this:
782 all:
784 TARGS := $(.TARGETS)
786 then $(TARGS) won't contain "all", because it's not until after the
787 variable is created that the previous target is completed.
789 Changing this would be a major pain. I think a less complex way to do it
790 would be to pre-define the target files as soon as the first line is
791 parsed, then come back and do the rest of the definition as now. That
792 would allow $(.TARGETS) to be correct without a major change to the way
793 the parser works.
795 if (streq (var->name, ".TARGETS"))
796 var->value = build_target_list (var->value);
797 else
800 if (variable_changenum != last_changenum && streq (var->name, ".VARIABLES"))
802 #ifndef CONFIG_WITH_VALUE_LENGTH
803 unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
804 #else
805 unsigned long max = EXPANSION_INCREMENT (var->value_length);
806 #endif
807 unsigned long len;
808 char *p;
809 struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
810 struct variable **end = &vp[global_variable_set.table.ht_size];
812 /* Make sure we have at least MAX bytes in the allocated buffer. */
813 var->value = xrealloc (var->value, max);
814 MAKE_STATS_2(var->reallocs++);
816 /* Walk through the hash of variables, constructing a list of names. */
817 p = var->value;
818 len = 0;
819 for (; vp < end; ++vp)
820 if (!HASH_VACANT (*vp))
822 struct variable *v = *vp;
823 int l = v->length;
825 len += l + 1;
826 if (len > max)
828 unsigned long off = p - var->value;
830 max += EXPANSION_INCREMENT (l + 1);
831 var->value = xrealloc (var->value, max);
832 p = &var->value[off];
833 MAKE_STATS_2(var->reallocs++);
836 memcpy (p, v->name, l);
837 p += l;
838 *(p++) = ' ';
840 *(p-1) = '\0';
841 #ifdef CONFIG_WITH_VALUE_LENGTH
842 var->value_length = p - var->value - 1;
843 var->value_alloc_len = max;
844 #endif
845 VARIABLE_CHANGED (var);
847 /* Remember the current variable change number. */
848 last_changenum = variable_changenum;
851 return var;
855 #if 0 /*FIX THIS - def KMK*/ /* bird: speed */
856 MY_INLINE struct variable *
857 lookup_cached_variable (const char *name)
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;
866 /* first set, first entry, both unrolled. */
868 if (setlist->set == &global_variable_set)
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;
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)
884 if ( (void *)v != hash_deleted_item
885 && v->name == name)
886 return MY_PREDICT_FALSE (v->special) ? lookup_special_var (v) : v;
888 /* the rest of the loop */
889 hash_2 = strcache2_get_hash (&variable_strcache, name) | 1;
890 for (;;)
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. */
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 */
904 else
905 hash_2 = strcache2_get_hash (&variable_strcache, name) | 1;
908 /* The other sets, if any. */
910 setlist = setlist->next;
911 while (setlist)
913 if (setlist->set == &global_variable_set)
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;
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)
929 if ( (void *)v != hash_deleted_item
930 && v->name == name)
931 return MY_PREDICT_FALSE (v->special) ? lookup_special_var (v) : v;
933 /* the rest of the loop */
934 for (;;)
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 */
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 */
949 /* next */
950 setlist = setlist->next;
953 return 0;
956 # ifndef NDEBUG
957 struct variable *
958 lookup_variable_for_assert (const char *name, unsigned int length)
960 const struct variable_set_list *setlist;
961 struct variable var_key;
962 var_key.name = name;
963 var_key.length = length;
965 for (setlist = current_variable_set_list;
966 setlist != 0; setlist = setlist->next)
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;
973 return 0;
975 # endif /* !NDEBUG */
976 #endif /* KMK - need for speed */
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. */
983 struct variable *
984 lookup_variable (const char *name, unsigned int length)
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
997 # ifdef KMK
998 /* Check for kBuild-define- local variable accesses and handle these first. */
999 if (length > 3 && name[0] == '[')
1001 struct variable *v = lookup_kbuild_object_variable_accessor(name, length);
1002 if (v != VAR_NOT_KBUILD_ACCESSOR)
1004 MAKE_STATS_2 (v->references++);
1005 return v;
1008 # endif
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 */
1020 var_key.name = (char *) name;
1021 var_key.length = length;
1023 for (setlist = current_variable_set_list;
1024 setlist != 0; setlist = setlist->next)
1026 const struct variable_set *set = setlist->set;
1027 struct variable *v;
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))
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;
1043 is_parent |= setlist->next_is_parent;
1046 #else /* KMK - need for speed */
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() */
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)
1066 char *sptr;
1067 int scnt;
1069 sptr = value;
1070 scnt = 0;
1072 while ((sptr = strchr (sptr, '$')))
1074 scnt++;
1075 sptr++;
1078 if (scnt > 0)
1080 char *nvalue;
1081 char *nptr;
1083 nvalue = alloca (strlen (value) + scnt + 1);
1084 sptr = value;
1085 nptr = nvalue;
1087 while (*sptr)
1089 if (*sptr == '$')
1091 *nptr++ = '$';
1092 *nptr++ = '$';
1094 else
1096 *nptr++ = *sptr;
1098 sptr++;
1101 *nptr = '\0';
1102 return define_variable (vname, length, nvalue, o_env, 1);
1106 return define_variable (vname, length, value, o_env, 1);
1109 #endif /* VMS */
1111 return 0;
1114 #ifdef CONFIG_WITH_STRCACHE2
1115 /* Alternative version of lookup_variable that takes a name that's already in
1116 the variable string cache. */
1117 struct variable *
1118 lookup_variable_strcached (const char *name)
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;
1127 #ifndef NDEBUG
1128 strcache2_verify_entry (&variable_strcache, name);
1129 #endif
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] == '[')
1135 v = lookup_kbuild_object_variable_accessor(name, strcache2_get_len(&variable_strcache, name));
1136 if (v != VAR_NOT_KBUILD_ACCESSOR)
1138 MAKE_STATS_2 (v->references++);
1139 return v;
1142 #endif
1144 #if 1 /*FIX THIS - ndef KMK */
1146 var_key.name = (char *) name;
1147 var_key.length = strcache2_get_len(&variable_strcache, name);
1149 for (setlist = current_variable_set_list;
1150 setlist != 0; setlist = setlist->next)
1152 const struct variable_set *set = setlist->set;
1154 v = (struct variable *) hash_find_item_strcached ((struct hash_table *) &set->table, &var_key);
1155 if (v && (!is_parent || !v->private_var))
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;
1164 is_parent |= setlist->next_is_parent;
1167 #else /* KMK - need for speed */
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;
1181 #endif
1184 /* Lookup a variable whose name is a string starting at NAME
1185 and with LENGTH chars in set SET. NAME need not be null-terminated.
1186 Returns address of the 'struct variable' containing all info
1187 on the variable, or nil if no such variable is defined. */
1189 struct variable *
1190 lookup_variable_in_set (const char *name, unsigned int length,
1191 const struct variable_set *set)
1193 struct variable var_key;
1194 #ifndef CONFIG_WITH_STRCACHE2
1195 var_key.name = (char *) name;
1196 var_key.length = length;
1198 return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
1199 #else /* CONFIG_WITH_STRCACHE2 */
1200 const char *cached_name;
1201 struct variable *v;
1203 # ifdef KMK
1204 /* Check for kBuild-define- local variable accesses and handle these first. */
1205 if (length > 3 && name[0] == '[' && set == &global_variable_set)
1207 v = lookup_kbuild_object_variable_accessor(name, length);
1208 if (v != VAR_NOT_KBUILD_ACCESSOR)
1210 RESOLVE_ALIAS_VARIABLE(v);
1211 MAKE_STATS_2 (v->references++);
1212 return v;
1215 # endif
1217 /* lookup the name in the string case, if it's not there it won't
1218 be in any of the sets either. Optimize lookups in the global set. */
1219 cached_name = strcache2_lookup(&variable_strcache, name, length);
1220 if (!cached_name)
1221 return NULL;
1223 if (set == &global_variable_set)
1225 v = strcache2_get_user_val (&variable_strcache, cached_name);
1226 assert (!v || v->name == cached_name);
1228 else
1230 var_key.name = cached_name;
1231 var_key.length = length;
1233 v = (struct variable *) hash_find_item_strcached (
1234 (struct hash_table *) &set->table, &var_key);
1236 # ifdef KMK
1237 RESOLVE_ALIAS_VARIABLE(v);
1238 # endif
1239 MAKE_STATS_2 (if (v) v->references++);
1240 return v;
1241 #endif /* CONFIG_WITH_STRCACHE2 */
1244 /* Initialize FILE's variable set list. If FILE already has a variable set
1245 list, the topmost variable set is left intact, but the the rest of the
1246 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
1247 rule, then we will use the "root" double-colon target's variable set as the
1248 parent of FILE's variable set.
1250 If we're READING a makefile, don't do the pattern variable search now,
1251 since the pattern variable might not have been defined yet. */
1253 void
1254 initialize_file_variables (struct file *file, int reading)
1256 struct variable_set_list *l = file->variables;
1258 if (l == 0)
1260 #ifndef CONFIG_WITH_ALLOC_CACHES
1261 l = (struct variable_set_list *)
1262 xmalloc (sizeof (struct variable_set_list));
1263 l->set = xmalloc (sizeof (struct variable_set));
1264 #else /* CONFIG_WITH_ALLOC_CACHES */
1265 l = (struct variable_set_list *)
1266 alloccache_alloc (&variable_set_list_cache);
1267 l->set = (struct variable_set *)
1268 alloccache_alloc (&variable_set_cache);
1269 #endif /* CONFIG_WITH_ALLOC_CACHES */
1270 #ifndef CONFIG_WITH_STRCACHE2
1271 hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
1272 variable_hash_1, variable_hash_2, variable_hash_cmp);
1273 #else /* CONFIG_WITH_STRCACHE2 */
1274 hash_init_strcached (&l->set->table, PERFILE_VARIABLE_BUCKETS,
1275 &variable_strcache, offsetof (struct variable, name));
1276 #endif /* CONFIG_WITH_STRCACHE2 */
1277 file->variables = l;
1280 /* If this is a double-colon, then our "parent" is the "root" target for
1281 this double-colon rule. Since that rule has the same name, parent,
1282 etc. we can just use its variables as the "next" for ours. */
1284 if (file->double_colon && file->double_colon != file)
1286 initialize_file_variables (file->double_colon, reading);
1287 l->next = file->double_colon->variables;
1288 l->next_is_parent = 0;
1289 return;
1292 if (file->parent == 0)
1293 l->next = &global_setlist;
1294 else
1296 initialize_file_variables (file->parent, reading);
1297 l->next = file->parent->variables;
1299 l->next_is_parent = 1;
1301 /* If we're not reading makefiles and we haven't looked yet, see if
1302 we can find pattern variables for this target. */
1304 if (!reading && !file->pat_searched)
1306 struct pattern_var *p;
1308 p = lookup_pattern_var (0, file->name);
1309 if (p != 0)
1311 struct variable_set_list *global = current_variable_set_list;
1313 /* We found at least one. Set up a new variable set to accumulate
1314 all the pattern variables that match this target. */
1316 file->pat_variables = create_new_variable_set ();
1317 current_variable_set_list = file->pat_variables;
1321 /* We found one, so insert it into the set. */
1323 struct variable *v;
1325 if (p->variable.flavor == f_simple)
1327 v = define_variable_loc (
1328 p->variable.name, strlen (p->variable.name),
1329 p->variable.value, p->variable.origin,
1330 0, &p->variable.fileinfo);
1332 v->flavor = f_simple;
1334 else
1336 #ifndef CONFIG_WITH_VALUE_LENGTH
1337 v = do_variable_definition (
1338 &p->variable.fileinfo, p->variable.name,
1339 p->variable.value, p->variable.origin,
1340 p->variable.flavor, 1);
1341 #else
1342 v = do_variable_definition_2 (
1343 &p->variable.fileinfo, p->variable.name,
1344 p->variable.value, p->variable.value_length, 0, 0,
1345 p->variable.origin, p->variable.flavor, 1);
1346 #endif
1349 /* Also mark it as a per-target and copy export status. */
1350 v->per_target = p->variable.per_target;
1351 v->export = p->variable.export;
1352 v->private_var = p->variable.private_var;
1354 while ((p = lookup_pattern_var (p, file->name)) != 0);
1356 current_variable_set_list = global;
1358 file->pat_searched = 1;
1361 /* If we have a pattern variable match, set it up. */
1363 if (file->pat_variables != 0)
1365 file->pat_variables->next = l->next;
1366 file->pat_variables->next_is_parent = l->next_is_parent;
1367 l->next = file->pat_variables;
1368 l->next_is_parent = 0;
1372 /* Pop the top set off the current variable set list,
1373 and free all its storage. */
1375 struct variable_set_list *
1376 create_new_variable_set (void)
1378 register struct variable_set_list *setlist;
1379 register struct variable_set *set;
1381 #ifndef CONFIG_WITH_ALLOC_CACHES
1382 set = xmalloc (sizeof (struct variable_set));
1383 #else
1384 set = (struct variable_set *) alloccache_alloc (&variable_set_cache);
1385 #endif
1386 #ifndef CONFIG_WITH_STRCACHE2
1387 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
1388 variable_hash_1, variable_hash_2, variable_hash_cmp);
1389 #else /* CONFIG_WITH_STRCACHE2 */
1390 hash_init_strcached (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
1391 &variable_strcache, offsetof (struct variable, name));
1392 #endif /* CONFIG_WITH_STRCACHE2 */
1394 #ifndef CONFIG_WITH_ALLOC_CACHES
1395 setlist = (struct variable_set_list *)
1396 xmalloc (sizeof (struct variable_set_list));
1397 #else
1398 setlist = (struct variable_set_list *)
1399 alloccache_alloc (&variable_set_list_cache);
1400 #endif
1401 setlist->set = set;
1402 setlist->next = current_variable_set_list;
1403 setlist->next_is_parent = 0;
1405 return setlist;
1408 /* Create a new variable set and push it on the current setlist.
1409 If we're pushing a global scope (that is, the current scope is the global
1410 scope) then we need to "push" it the other way: file variable sets point
1411 directly to the global_setlist so we need to replace that with the new one.
1414 struct variable_set_list *
1415 push_new_variable_scope (void)
1417 current_variable_set_list = create_new_variable_set ();
1418 if (current_variable_set_list->next == &global_setlist)
1420 /* It was the global, so instead of new -> &global we want to replace
1421 &global with the new one and have &global -> new, with current still
1422 pointing to &global */
1423 struct variable_set *set = current_variable_set_list->set;
1424 current_variable_set_list->set = global_setlist.set;
1425 global_setlist.set = set;
1426 current_variable_set_list->next = global_setlist.next;
1427 global_setlist.next = current_variable_set_list;
1428 current_variable_set_list = &global_setlist;
1430 return (current_variable_set_list);
1433 void
1434 pop_variable_scope (void)
1436 struct variable_set_list *setlist;
1437 struct variable_set *set;
1439 /* Can't call this if there's no scope to pop! */
1440 assert (current_variable_set_list->next != NULL);
1442 if (current_variable_set_list != &global_setlist)
1444 /* We're not pointing to the global setlist, so pop this one. */
1445 setlist = current_variable_set_list;
1446 set = setlist->set;
1447 current_variable_set_list = setlist->next;
1449 else
1451 /* This set is the one in the global_setlist, but there is another global
1452 set beyond that. We want to copy that set to global_setlist, then
1453 delete what used to be in global_setlist. */
1454 setlist = global_setlist.next;
1455 set = global_setlist.set;
1456 global_setlist.set = setlist->set;
1457 global_setlist.next = setlist->next;
1458 global_setlist.next_is_parent = setlist->next_is_parent;
1461 /* Free the one we no longer need. */
1462 #ifndef CONFIG_WITH_ALLOC_CACHES
1463 free (setlist);
1464 hash_map (&set->table, free_variable_name_and_value);
1465 hash_free (&set->table, 1);
1466 free (set);
1467 #else
1468 alloccache_free (&variable_set_list_cache, setlist);
1469 hash_map (&set->table, free_variable_name_and_value);
1470 hash_free_cached (&set->table, 1, &variable_cache);
1471 alloccache_free (&variable_set_cache, set);
1472 #endif
1475 /* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
1477 static void
1478 merge_variable_sets (struct variable_set *to_set,
1479 struct variable_set *from_set)
1481 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
1482 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
1484 int inc = to_set == &global_variable_set ? 1 : 0;
1486 for ( ; from_var_slot < from_var_end; from_var_slot++)
1487 if (! HASH_VACANT (*from_var_slot))
1489 struct variable *from_var = *from_var_slot;
1490 struct variable **to_var_slot
1491 #ifndef CONFIG_WITH_STRCACHE2
1492 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
1493 #else /* CONFIG_WITH_STRCACHE2 */
1494 = (struct variable **) hash_find_slot_strcached (&to_set->table,
1495 *from_var_slot);
1496 #endif /* CONFIG_WITH_STRCACHE2 */
1497 if (HASH_VACANT (*to_var_slot))
1499 hash_insert_at (&to_set->table, from_var, to_var_slot);
1500 variable_changenum += inc;
1502 else
1504 /* GKM FIXME: delete in from_set->table */
1505 #ifdef KMK
1506 if (from_var->aliased)
1507 OS (fatal, NULL, ("Attempting to delete aliased variable '%s'"), from_var->name);
1508 if (from_var->alias)
1509 OS (fatal, NULL, ("Attempting to delete variable aliased '%s'"), from_var->name);
1510 #endif
1511 #ifdef CONFIG_WITH_COMPILER
1512 if (from_var->evalprog || from_var->expandprog)
1513 kmk_cc_variable_deleted (from_var);
1514 #endif
1515 #ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1516 if (!from_var->rdonly_val)
1517 #endif
1518 free (from_var->value);
1519 free (from_var);
1524 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
1526 void
1527 merge_variable_set_lists (struct variable_set_list **setlist0,
1528 struct variable_set_list *setlist1)
1530 struct variable_set_list *to = *setlist0;
1531 struct variable_set_list *last0 = 0;
1533 /* If there's nothing to merge, stop now. */
1534 if (!setlist1)
1535 return;
1537 /* This loop relies on the fact that all setlists terminate with the global
1538 setlist (before NULL). If that's not true, arguably we SHOULD die. */
1539 if (to)
1540 while (setlist1 != &global_setlist && to != &global_setlist)
1542 struct variable_set_list *from = setlist1;
1543 setlist1 = setlist1->next;
1545 merge_variable_sets (to->set, from->set);
1547 last0 = to;
1548 to = to->next;
1551 if (setlist1 != &global_setlist)
1553 if (last0 == 0)
1554 *setlist0 = setlist1;
1555 else
1556 last0->next = setlist1;
1560 #if defined(KMK) && !defined(WINDOWS32)
1561 /* Parses out the next number from the uname release level string. Fast
1562 forwards to the end of the string when encountering some non-conforming
1563 chars. */
1565 static unsigned long parse_release_number (const char **ppsz)
1567 unsigned long ul;
1568 char *psz = (char *)*ppsz;
1569 if (ISDIGIT (*psz))
1571 ul = strtoul (psz, &psz, 10);
1572 if (psz != NULL && *psz == '.')
1573 psz++;
1574 else
1575 psz = strchr (*ppsz, '\0');
1576 *ppsz = psz;
1578 else
1579 ul = 0;
1580 return ul;
1582 #endif
1584 /* Define the automatic variables, and record the addresses
1585 of their structures so we can change their values quickly. */
1587 void
1588 define_automatic_variables (void)
1590 struct variable *v;
1591 #ifndef KMK
1592 char buf[200];
1593 #else
1594 char buf[1024];
1595 const char *val;
1596 struct variable *envvar1;
1597 struct variable *envvar2;
1598 # ifndef WINDOWS32
1599 struct utsname uts;
1600 # endif
1601 unsigned long ulMajor = 0, ulMinor = 0, ulPatch = 0, ul4th = 0;
1602 #endif
1604 sprintf (buf, "%u", makelevel);
1605 define_variable_cname (MAKELEVEL_NAME, buf, o_env, 0);
1607 sprintf (buf, "%s%s%s",
1608 version_string,
1609 (remote_description == 0 || remote_description[0] == '\0')
1610 ? "" : "-",
1611 (remote_description == 0 || remote_description[0] == '\0')
1612 ? "" : remote_description);
1613 #ifndef KMK
1614 define_variable_cname ("MAKE_VERSION", buf, o_default, 0);
1615 define_variable_cname ("MAKE_HOST", make_host, o_default, 0);
1616 #else /* KMK */
1618 /* Define KMK_VERSION to indicate kMk. */
1619 define_variable_cname ("KMK_VERSION", buf, o_default, 0);
1621 /* Define KBUILD_VERSION* */
1622 sprintf (buf, "%d", KBUILD_VERSION_MAJOR);
1623 define_variable_cname ("KBUILD_VERSION_MAJOR", buf, o_default, 0);
1624 sprintf (buf, "%d", KBUILD_VERSION_MINOR);
1625 define_variable_cname ("KBUILD_VERSION_MINOR", buf, o_default, 0);
1626 sprintf (buf, "%d", KBUILD_VERSION_PATCH);
1627 define_variable_cname ("KBUILD_VERSION_PATCH", buf, o_default, 0);
1628 sprintf (buf, "%d", KBUILD_SVN_REV);
1629 define_variable_cname ("KBUILD_KMK_REVISION", buf, o_default, 0);
1631 sprintf (buf, "%d.%d.%d-r%d", KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR,
1632 KBUILD_VERSION_PATCH, KBUILD_SVN_REV);
1633 define_variable_cname ("KBUILD_VERSION", buf, o_default, 0);
1635 /* The host defaults. The BUILD_* stuff will be replaced by KBUILD_* soon. */
1636 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST"));
1637 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM"));
1638 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST;
1639 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1640 OS (error, NULL, _("KBUILD_HOST and BUILD_PLATFORM differs, using KBUILD_HOST=%s."), val);
1641 if (!envvar1)
1642 define_variable_cname ("KBUILD_HOST", val, o_default, 0);
1643 if (!envvar2)
1644 define_variable_cname ("BUILD_PLATFORM", "$(KBUILD_HOST)", o_default, 1);
1646 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_ARCH"));
1647 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_ARCH"));
1648 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_ARCH;
1649 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1650 OS (error, NULL, _("KBUILD_HOST_ARCH and BUILD_PLATFORM_ARCH differs, using KBUILD_HOST_ARCH=%s."), val);
1651 if (!envvar1)
1652 define_variable_cname ("KBUILD_HOST_ARCH", val, o_default, 0);
1653 if (!envvar2)
1654 define_variable_cname ("BUILD_PLATFORM_ARCH", "$(KBUILD_HOST_ARCH)", o_default, 1);
1656 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_CPU"));
1657 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_CPU"));
1658 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_CPU;
1659 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1660 OS (error, NULL, _("KBUILD_HOST_CPU and BUILD_PLATFORM_CPU differs, using KBUILD_HOST_CPU=%s."), val);
1661 if (!envvar1)
1662 define_variable_cname ("KBUILD_HOST_CPU", val, o_default, 0);
1663 if (!envvar2)
1664 define_variable_cname ("BUILD_PLATFORM_CPU", "$(KBUILD_HOST_CPU)", o_default, 1);
1666 /* The host kernel version. */
1667 # if defined(WINDOWS32)
1669 OSVERSIONINFOEXW oix;
1670 NTSTATUS (WINAPI *pfnRtlGetVersion)(OSVERSIONINFOEXW *);
1671 *(FARPROC *)&pfnRtlGetVersion = GetProcAddress (GetModuleHandleW (L"NTDLL.DLL"),
1672 "RtlGetVersion"); /* GetVersionEx lies */
1673 memset (&oix, '\0', sizeof (oix));
1674 oix.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEXW);
1675 if (!pfnRtlGetVersion || pfnRtlGetVersion (&oix) < 0)
1677 memset (&oix, '\0', sizeof (oix));
1678 oix.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEXW);
1679 if (!GetVersionExW((LPOSVERSIONINFOW)&oix))
1681 memset (&oix, '\0', sizeof (oix));
1682 oix.dwOSVersionInfoSize = sizeof (OSVERSIONINFOW);
1683 GetVersionExW ((LPOSVERSIONINFOW)&oix);
1686 if (oix.dwPlatformId == VER_PLATFORM_WIN32_NT)
1688 ulMajor = oix.dwMajorVersion;
1689 ulMinor = oix.dwMinorVersion;
1690 ulPatch = oix.wServicePackMajor;
1691 ul4th = oix.wServicePackMinor;
1693 else
1695 ulMajor = oix.dwPlatformId == 1 ? 0 /*Win95/98/ME*/
1696 : oix.dwPlatformId == 3 ? 1 /*WinCE*/
1697 : 2; /*??*/
1698 ulMinor = oix.dwMajorVersion;
1699 ulPatch = oix.dwMinorVersion;
1700 ul4th = oix.wServicePackMajor;
1702 oix.dwBuildNumber &= 0x3fffffff;
1703 sprintf (buf, "%lu", oix.dwBuildNumber);
1704 define_variable_cname ("KBUILD_HOST_VERSION_BUILD", buf, o_default, 0);
1706 sprintf (buf, "%lu.%lu.%lu.%lu.%lu", ulMajor, ulMinor, ulPatch, ul4th, oix.dwBuildNumber);
1707 define_variable_cname ("KBUILD_HOST_VERSION", buf, o_default, 0);
1709 # else
1710 memset (&uts, 0, sizeof(uts));
1711 uname (&uts);
1712 val = uts.release;
1713 ulMajor = parse_release_number (&val);
1714 ulMinor = parse_release_number (&val);
1715 ulPatch = parse_release_number (&val);
1716 ul4th = parse_release_number (&val);
1718 define_variable_cname ("KBUILD_HOST_UNAME_SYSNAME", uts.sysname, o_default, 0);
1719 define_variable_cname ("KBUILD_HOST_UNAME_RELEASE", uts.release, o_default, 0);
1720 define_variable_cname ("KBUILD_HOST_UNAME_VERSION", uts.version, o_default, 0);
1721 define_variable_cname ("KBUILD_HOST_UNAME_MACHINE", uts.machine, o_default, 0);
1722 define_variable_cname ("KBUILD_HOST_UNAME_NODENAME", uts.nodename, o_default, 0);
1724 sprintf (buf, "%lu.%lu.%lu.%lu", ulMajor, ulMinor, ulPatch, ul4th);
1725 define_variable_cname ("KBUILD_HOST_VERSION", buf, o_default, 0);
1726 # endif
1728 sprintf (buf, "%lu", ulMajor);
1729 define_variable_cname ("KBUILD_HOST_VERSION_MAJOR", buf, o_default, 0);
1731 sprintf (buf, "%lu", ulMinor);
1732 define_variable_cname ("KBUILD_HOST_VERSION_MINOR", buf, o_default, 0);
1734 sprintf (buf, "%lu", ulPatch);
1735 define_variable_cname ("KBUILD_HOST_VERSION_PATCH", buf, o_default, 0);
1737 /* The kBuild locations. */
1738 define_variable_cname ("KBUILD_PATH", get_kbuild_path (), o_default, 0);
1739 define_variable_cname ("KBUILD_BIN_PATH", get_kbuild_bin_path (), o_default, 0);
1741 define_variable_cname ("PATH_KBUILD", "$(KBUILD_PATH)", o_default, 1);
1742 define_variable_cname ("PATH_KBUILD_BIN", "$(KBUILD_BIN_PATH)", o_default, 1);
1744 /* Define KMK_FEATURES to indicate various working KMK features. */
1745 # if defined (CONFIG_WITH_RSORT) \
1746 && defined (CONFIG_WITH_ABSPATHEX) \
1747 && defined (CONFIG_WITH_TOUPPER_TOLOWER) \
1748 && defined (CONFIG_WITH_DEFINED) \
1749 && defined (CONFIG_WITH_VALUE_LENGTH) \
1750 && defined (CONFIG_WITH_COMPARE) \
1751 && defined (CONFIG_WITH_STACK) \
1752 && defined (CONFIG_WITH_MATH) \
1753 && defined (CONFIG_WITH_XARGS) \
1754 && defined (CONFIG_WITH_EXPLICIT_MULTITARGET) \
1755 && defined (CONFIG_WITH_DOT_MUST_MAKE) \
1756 && defined (CONFIG_WITH_PREPEND_ASSIGNMENT) \
1757 && defined (CONFIG_WITH_SET_CONDITIONALS) \
1758 && defined (CONFIG_WITH_DATE) \
1759 && defined (CONFIG_WITH_FILE_SIZE) \
1760 && defined (CONFIG_WITH_WHERE_FUNCTION) \
1761 && defined (CONFIG_WITH_WHICH) \
1762 && defined (CONFIG_WITH_EVALPLUS) \
1763 && (defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)) \
1764 && defined (CONFIG_WITH_COMMANDS_FUNC) \
1765 && defined (CONFIG_WITH_PRINTF) \
1766 && defined (CONFIG_WITH_LOOP_FUNCTIONS) \
1767 && defined (CONFIG_WITH_ROOT_FUNC) \
1768 && defined (CONFIG_WITH_STRING_FUNCTIONS) \
1769 && defined (CONFIG_WITH_DEFINED_FUNCTIONS) \
1770 && defined (KMK_HELPERS)
1771 define_variable_cname ("KMK_FEATURES",
1772 "append-dash-n abspath includedep-queue install-hard-linking umask quote versort"
1773 " kBuild-define"
1774 " rsort"
1775 " abspathex"
1776 " toupper tolower"
1777 " defined"
1778 " comp-vars comp-cmds comp-cmds-ex"
1779 " stack"
1780 " math-int"
1781 " xargs"
1782 " explicit-multitarget"
1783 " dot-must-make"
1784 " prepend-assignment"
1785 " set-conditionals intersects"
1786 " date"
1787 " file-size"
1788 " expr if-expr select"
1789 " where"
1790 " which"
1791 " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var"
1792 " make-stats"
1793 " commands"
1794 " printf"
1795 " for while"
1796 " root"
1797 " length insert pos lastpos substr translate"
1798 " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one kb-exp-tmpl"
1799 " firstdefined lastdefined"
1800 , o_default, 0);
1801 # else /* MSC can't deal with strings mixed with #if/#endif, thus the slow way. */
1802 # error "All features should be enabled by default!"
1803 strcpy (buf, "append-dash-n abspath includedep-queue install-hard-linking umask quote versort"
1804 " kBuild-define");
1805 # if defined (CONFIG_WITH_RSORT)
1806 strcat (buf, " rsort");
1807 # endif
1808 # if defined (CONFIG_WITH_ABSPATHEX)
1809 strcat (buf, " abspathex");
1810 # endif
1811 # if defined (CONFIG_WITH_TOUPPER_TOLOWER)
1812 strcat (buf, " toupper tolower");
1813 # endif
1814 # if defined (CONFIG_WITH_DEFINED)
1815 strcat (buf, " defined");
1816 # endif
1817 # if defined (CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
1818 strcat (buf, " comp-vars comp-cmds comp-cmds-ex");
1819 # endif
1820 # if defined (CONFIG_WITH_STACK)
1821 strcat (buf, " stack");
1822 # endif
1823 # if defined (CONFIG_WITH_MATH)
1824 strcat (buf, " math-int");
1825 # endif
1826 # if defined (CONFIG_WITH_XARGS)
1827 strcat (buf, " xargs");
1828 # endif
1829 # if defined (CONFIG_WITH_EXPLICIT_MULTITARGET)
1830 strcat (buf, " explicit-multitarget");
1831 # endif
1832 # if defined (CONFIG_WITH_DOT_MUST_MAKE)
1833 strcat (buf, " dot-must-make");
1834 # endif
1835 # if defined (CONFIG_WITH_PREPEND_ASSIGNMENT)
1836 strcat (buf, " prepend-assignment");
1837 # endif
1838 # if defined (CONFIG_WITH_SET_CONDITIONALS)
1839 strcat (buf, " set-conditionals intersects");
1840 # endif
1841 # if defined (CONFIG_WITH_DATE)
1842 strcat (buf, " date");
1843 # endif
1844 # if defined (CONFIG_WITH_FILE_SIZE)
1845 strcat (buf, " file-size");
1846 # endif
1847 # if defined (CONFIG_WITH_IF_CONDITIONALS)
1848 strcat (buf, " expr if-expr select");
1849 # endif
1850 # if defined (CONFIG_WITH_WHERE_FUNCTION)
1851 strcat (buf, " where");
1852 # endif
1853 # if defined (CONFIG_WITH_WHICH)
1854 strcat (buf, " which");
1855 # endif
1856 # if defined (CONFIG_WITH_EVALPLUS)
1857 strcat (buf, " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var");
1858 # endif
1859 # if defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)
1860 strcat (buf, " make-stats");
1861 # endif
1862 # if defined (CONFIG_WITH_COMMANDS_FUNC)
1863 strcat (buf, " commands");
1864 # endif
1865 # if defined (CONFIG_WITH_PRINTF)
1866 strcat (buf, " printf");
1867 # endif
1868 # if defined (CONFIG_WITH_LOOP_FUNCTIONS)
1869 strcat (buf, " for while");
1870 # endif
1871 # if defined (CONFIG_WITH_ROOT_FUNC)
1872 strcat (buf, " root");
1873 # endif
1874 # if defined (CONFIG_WITH_STRING_FUNCTIONS)
1875 strcat (buf, " length insert pos lastpos substr translate");
1876 # endif
1877 # if defined (CONFIG_WITH_DEFINED_FUNCTIONS)
1878 strcat (buf, " firstdefined lastdefined");
1879 # endif
1880 # if defined (KMK_HELPERS)
1881 strcat (buf, " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one kb-exp-tmpl");
1882 # endif
1883 define_variable_cname ("KMK_FEATURES", buf, o_default, 0);
1884 # endif
1886 #endif /* KMK */
1888 #ifdef CONFIG_WITH_KMK_BUILTIN
1889 /* The supported kMk Builtin commands. */
1890 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);
1891 #endif
1893 #ifdef __MSDOS__
1894 /* Allow to specify a special shell just for Make,
1895 and use $COMSPEC as the default $SHELL when appropriate. */
1897 static char shell_str[] = "SHELL";
1898 const int shlen = sizeof (shell_str) - 1;
1899 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
1900 struct variable *comp = lookup_variable ("COMSPEC", 7);
1902 /* $(MAKESHELL) overrides $(SHELL) even if -e is in effect. */
1903 if (mshp)
1904 (void) define_variable (shell_str, shlen,
1905 mshp->value, o_env_override, 0);
1906 else if (comp)
1908 /* $(COMSPEC) shouldn't override $(SHELL). */
1909 struct variable *shp = lookup_variable (shell_str, shlen);
1911 if (!shp)
1912 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
1915 #elif defined(__EMX__)
1917 static char shell_str[] = "SHELL";
1918 const int shlen = sizeof (shell_str) - 1;
1919 struct variable *shell = lookup_variable (shell_str, shlen);
1920 struct variable *replace = lookup_variable ("MAKESHELL", 9);
1922 /* if $MAKESHELL is defined in the environment assume o_env_override */
1923 if (replace && *replace->value && replace->origin == o_env)
1924 replace->origin = o_env_override;
1926 /* if $MAKESHELL is not defined use $SHELL but only if the variable
1927 did not come from the environment */
1928 if (!replace || !*replace->value)
1929 if (shell && *shell->value && (shell->origin == o_env
1930 || shell->origin == o_env_override))
1932 /* overwrite whatever we got from the environment */
1933 free (shell->value);
1934 shell->value = xstrdup (default_shell);
1935 shell->origin = o_default;
1938 /* Some people do not like cmd to be used as the default
1939 if $SHELL is not defined in the Makefile.
1940 With -DNO_CMD_DEFAULT you can turn off this behaviour */
1941 # ifndef NO_CMD_DEFAULT
1942 /* otherwise use $COMSPEC */
1943 if (!replace || !*replace->value)
1944 replace = lookup_variable ("COMSPEC", 7);
1946 /* otherwise use $OS2_SHELL */
1947 if (!replace || !*replace->value)
1948 replace = lookup_variable ("OS2_SHELL", 9);
1949 # else
1950 # warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
1951 # endif
1953 if (replace && *replace->value)
1954 /* overwrite $SHELL */
1955 (void) define_variable (shell_str, shlen, replace->value,
1956 replace->origin, 0);
1957 else
1958 /* provide a definition if there is none */
1959 (void) define_variable (shell_str, shlen, default_shell,
1960 o_default, 0);
1963 #endif
1965 /* This won't override any definition, but it will provide one if there
1966 isn't one there. */
1967 v = define_variable_cname ("SHELL", default_shell, o_default, 0);
1968 #ifdef __MSDOS__
1969 v->export = v_export; /* Export always SHELL. */
1970 #endif
1972 /* On MSDOS we do use SHELL from environment, since it isn't a standard
1973 environment variable on MSDOS, so whoever sets it, does that on purpose.
1974 On OS/2 we do not use SHELL from environment but we have already handled
1975 that problem above. */
1976 #if !defined(__MSDOS__) && !defined(__EMX__)
1977 /* Don't let SHELL come from the environment. */
1978 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
1980 # ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1981 if (v->rdonly_val)
1982 v->rdonly_val = 0;
1983 else
1984 # endif
1985 free (v->value);
1986 v->origin = o_file;
1987 v->value = xstrdup (default_shell);
1988 # ifdef CONFIG_WITH_VALUE_LENGTH
1989 v->value_length = strlen (v->value);
1990 v->value_alloc_len = v->value_length + 1;
1991 # endif
1993 #endif
1995 /* Make sure MAKEFILES gets exported if it is set. */
1996 v = define_variable_cname ("MAKEFILES", "", o_default, 0);
1997 v->export = v_ifset;
1999 /* Define the magic D and F variables in terms of
2000 the automatic variables they are variations of. */
2002 #if defined(__MSDOS__) || defined(WINDOWS32)
2003 /* For consistency, remove the trailing backslash as well as slash. */
2004 define_variable_cname ("@D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $@)))",
2005 o_automatic, 1);
2006 define_variable_cname ("%D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $%)))",
2007 o_automatic, 1);
2008 define_variable_cname ("*D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $*)))",
2009 o_automatic, 1);
2010 define_variable_cname ("<D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $<)))",
2011 o_automatic, 1);
2012 define_variable_cname ("?D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $?)))",
2013 o_automatic, 1);
2014 define_variable_cname ("^D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $^)))",
2015 o_automatic, 1);
2016 define_variable_cname ("+D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $+)))",
2017 o_automatic, 1);
2018 #else /* not __MSDOS__, not WINDOWS32 */
2019 define_variable_cname ("@D", "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
2020 define_variable_cname ("%D", "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
2021 define_variable_cname ("*D", "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
2022 define_variable_cname ("<D", "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
2023 define_variable_cname ("?D", "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
2024 define_variable_cname ("^D", "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
2025 define_variable_cname ("+D", "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
2026 #endif
2027 define_variable_cname ("@F", "$(notdir $@)", o_automatic, 1);
2028 define_variable_cname ("%F", "$(notdir $%)", o_automatic, 1);
2029 define_variable_cname ("*F", "$(notdir $*)", o_automatic, 1);
2030 define_variable_cname ("<F", "$(notdir $<)", o_automatic, 1);
2031 define_variable_cname ("?F", "$(notdir $?)", o_automatic, 1);
2032 define_variable_cname ("^F", "$(notdir $^)", o_automatic, 1);
2033 define_variable_cname ("+F", "$(notdir $+)", o_automatic, 1);
2034 #ifdef CONFIG_WITH_LAZY_DEPS_VARS
2035 define_variable ("^", 1, "$(deps $@)", o_automatic, 1);
2036 define_variable ("+", 1, "$(deps-all $@)", o_automatic, 1);
2037 define_variable ("?", 1, "$(deps-newer $@)", o_automatic, 1);
2038 define_variable ("|", 1, "$(deps-oo $@)", o_automatic, 1);
2039 #endif /* CONFIG_WITH_LAZY_DEPS_VARS */
2042 int export_all_variables;
2044 #ifdef KMK
2045 /* Cached table containing the exports of the global_variable_set. When
2046 there are many global variables, it can be so expensive to construct the
2047 child environment that we have a majority of job slot idle. */
2048 static size_t global_variable_set_exports_generation = ~(size_t)0;
2049 static struct hash_table global_variable_set_exports;
2051 static void update_global_variable_set_exports(void)
2053 struct variable **v_slot;
2054 struct variable **v_end;
2056 /* Re-initialize the table. */
2057 if (global_variable_set_exports_generation != ~(size_t)0)
2058 hash_free (&global_variable_set_exports, 0);
2059 hash_init_strcached (&global_variable_set_exports, ENVIRONMENT_VARIABLE_BUCKETS,
2060 &variable_strcache, offsetof (struct variable, name));
2062 /* do pretty much the same as target_environment. */
2063 v_slot = (struct variable **) global_variable_set.table.ht_vec;
2064 v_end = v_slot + global_variable_set.table.ht_size;
2065 for ( ; v_slot < v_end; v_slot++)
2066 if (! HASH_VACANT (*v_slot))
2068 struct variable **new_slot;
2069 struct variable *v = *v_slot;
2071 switch (v->export)
2073 case v_default:
2074 if (v->origin == o_default || v->origin == o_automatic)
2075 /* Only export default variables by explicit request. */
2076 continue;
2078 /* The variable doesn't have a name that can be exported. */
2079 if (! v->exportable)
2080 continue;
2082 if (! export_all_variables
2083 && v->origin != o_command
2084 && v->origin != o_env && v->origin != o_env_override)
2085 continue;
2086 break;
2088 case v_export:
2089 break;
2091 case v_noexport:
2093 /* If this is the SHELL variable and it's not exported,
2094 then add the value from our original environment, if
2095 the original environment defined a value for SHELL. */
2096 extern struct variable shell_var;
2097 if (streq (v->name, "SHELL") && shell_var.value)
2099 v = &shell_var;
2100 break;
2102 continue;
2105 case v_ifset:
2106 if (v->origin == o_default)
2107 continue;
2108 break;
2111 assert (strcache2_is_cached (&variable_strcache, v->name));
2112 new_slot = (struct variable **) hash_find_slot_strcached (&global_variable_set_exports, v);
2113 if (HASH_VACANT (*new_slot))
2114 hash_insert_at (&global_variable_set_exports, v, new_slot);
2117 /* done */
2118 global_variable_set_exports_generation = global_variable_generation;
2121 #endif
2123 /* Create a new environment for FILE's commands.
2124 If FILE is nil, this is for the 'shell' function.
2125 The child's MAKELEVEL variable is incremented. */
2127 char **
2128 target_environment (struct file *file)
2130 struct variable_set_list *set_list;
2131 register struct variable_set_list *s;
2132 struct hash_table table;
2133 struct variable **v_slot;
2134 struct variable **v_end;
2135 struct variable makelevel_key;
2136 char **result_0;
2137 char **result;
2138 #ifdef CONFIG_WITH_STRCACHE2
2139 const char *cached_name;
2140 #endif
2142 #ifdef KMK
2143 if (global_variable_set_exports_generation != global_variable_generation)
2144 update_global_variable_set_exports();
2145 #endif
2147 if (file == 0)
2148 set_list = current_variable_set_list;
2149 else
2150 set_list = file->variables;
2152 #ifndef CONFIG_WITH_STRCACHE2
2153 hash_init (&table, ENVIRONMENT_VARIABLE_BUCKETS,
2154 variable_hash_1, variable_hash_2, variable_hash_cmp);
2155 #else /* CONFIG_WITH_STRCACHE2 */
2156 hash_init_strcached (&table, ENVIRONMENT_VARIABLE_BUCKETS,
2157 &variable_strcache, offsetof (struct variable, name));
2158 #endif /* CONFIG_WITH_STRCACHE2 */
2160 /* Run through all the variable sets in the list,
2161 accumulating variables in TABLE. */
2162 for (s = set_list; s != 0; s = s->next)
2164 struct variable_set *set = s->set;
2165 #ifdef KMK
2166 if (set == &global_variable_set)
2168 assert(s->next == NULL);
2169 break;
2171 #endif
2172 v_slot = (struct variable **) set->table.ht_vec;
2173 v_end = v_slot + set->table.ht_size;
2174 for ( ; v_slot < v_end; v_slot++)
2175 if (! HASH_VACANT (*v_slot))
2177 struct variable **new_slot;
2178 struct variable *v = *v_slot;
2180 /* If this is a per-target variable and it hasn't been touched
2181 already then look up the global version and take its export
2182 value. */
2183 if (v->per_target && v->export == v_default)
2185 struct variable *gv;
2187 #ifndef CONFIG_WITH_VALUE_LENGTH
2188 gv = lookup_variable_in_set (v->name, strlen (v->name),
2189 &global_variable_set);
2190 #else
2191 assert ((int)strlen(v->name) == v->length);
2192 gv = lookup_variable_in_set (v->name, v->length,
2193 &global_variable_set);
2194 #endif
2195 if (gv)
2196 v->export = gv->export;
2199 switch (v->export)
2201 case v_default:
2202 if (v->origin == o_default || v->origin == o_automatic)
2203 /* Only export default variables by explicit request. */
2204 continue;
2206 /* The variable doesn't have a name that can be exported. */
2207 if (! v->exportable)
2208 continue;
2210 if (! export_all_variables
2211 && v->origin != o_command
2212 && v->origin != o_env && v->origin != o_env_override)
2213 continue;
2214 break;
2216 case v_export:
2217 break;
2219 case v_noexport:
2221 /* If this is the SHELL variable and it's not exported,
2222 then add the value from our original environment, if
2223 the original environment defined a value for SHELL. */
2224 if (streq (v->name, "SHELL") && shell_var.value)
2226 v = &shell_var;
2227 break;
2229 continue;
2232 case v_ifset:
2233 if (v->origin == o_default)
2234 continue;
2235 break;
2238 #ifndef CONFIG_WITH_STRCACHE2
2239 new_slot = (struct variable **) hash_find_slot (&table, v);
2240 #else /* CONFIG_WITH_STRCACHE2 */
2241 assert (strcache2_is_cached (&variable_strcache, v->name));
2242 new_slot = (struct variable **) hash_find_slot_strcached (&table, v);
2243 #endif /* CONFIG_WITH_STRCACHE2 */
2244 if (HASH_VACANT (*new_slot))
2245 hash_insert_at (&table, v, new_slot);
2249 #ifdef KMK
2250 /* Add the global exports to table. */
2251 v_slot = (struct variable **) global_variable_set_exports.ht_vec;
2252 v_end = v_slot + global_variable_set_exports.ht_size;
2253 for ( ; v_slot < v_end; v_slot++)
2254 if (! HASH_VACANT (*v_slot))
2256 struct variable **new_slot;
2257 struct variable *v = *v_slot;
2258 assert (strcache2_is_cached (&variable_strcache, v->name));
2259 new_slot = (struct variable **) hash_find_slot_strcached (&table, v);
2260 if (HASH_VACANT (*new_slot))
2261 hash_insert_at (&table, v, new_slot);
2263 #endif
2265 #ifndef CONFIG_WITH_STRCACHE2
2266 makelevel_key.name = (char *)MAKELEVEL_NAME;
2267 makelevel_key.length = MAKELEVEL_LENGTH;
2268 hash_delete (&table, &makelevel_key);
2269 #else /* CONFIG_WITH_STRCACHE2 */
2270 /* lookup the name in the string case, if it's not there it won't
2271 be in any of the sets either. */
2272 cached_name = strcache2_lookup (&variable_strcache,
2273 MAKELEVEL_NAME, MAKELEVEL_LENGTH);
2274 if (cached_name)
2276 makelevel_key.name = cached_name;
2277 makelevel_key.length = MAKELEVEL_LENGTH;
2278 hash_delete_strcached (&table, &makelevel_key);
2280 #endif /* CONFIG_WITH_STRCACHE2 */
2282 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
2284 v_slot = (struct variable **) table.ht_vec;
2285 v_end = v_slot + table.ht_size;
2286 for ( ; v_slot < v_end; v_slot++)
2287 if (! HASH_VACANT (*v_slot))
2289 struct variable *v = *v_slot;
2291 /* If V is recursively expanded and didn't come from the environment,
2292 expand its value. If it came from the environment, it should
2293 go back into the environment unchanged. */
2294 if (v->recursive
2295 && v->origin != o_env && v->origin != o_env_override)
2297 #ifndef CONFIG_WITH_VALUE_LENGTH
2298 char *value = recursively_expand_for_file (v, file);
2299 #else
2300 char *value = recursively_expand_for_file (v, file, NULL);
2301 #endif
2302 #ifdef WINDOWS32
2303 if (strcmp (v->name, "Path") == 0 ||
2304 strcmp (v->name, "PATH") == 0)
2305 convert_Path_to_windows32 (value, ';');
2306 #endif
2307 *result++ = xstrdup (concat (3, v->name, "=", value));
2308 free (value);
2310 else
2312 #ifdef WINDOWS32
2313 if (strcmp (v->name, "Path") == 0 ||
2314 strcmp (v->name, "PATH") == 0)
2315 convert_Path_to_windows32 (v->value, ';');
2316 #endif
2317 *result++ = xstrdup (concat (3, v->name, "=", v->value));
2321 *result = xmalloc (100);
2322 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
2323 *++result = 0;
2325 hash_free (&table, 0);
2327 return result_0;
2330 #ifdef CONFIG_WITH_VALUE_LENGTH
2331 /* Worker function for do_variable_definition_append() and
2332 append_expanded_string_to_variable().
2333 The APPEND argument indicates whether it's an append or prepend operation. */
2334 void append_string_to_variable (struct variable *v, const char *value, unsigned int value_len, int append)
2336 /* The previous definition of the variable was recursive.
2337 The new value is the unexpanded old and new values. */
2338 unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
2339 int done_1st_prepend_copy = 0;
2340 #ifdef KMK
2341 assert (!v->alias);
2342 #endif
2344 /* Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
2345 if (!value_len)
2346 return;
2348 /* adjust the size. */
2349 if (v->value_alloc_len <= new_value_len + 1)
2351 if (v->value_alloc_len < 256)
2352 v->value_alloc_len = 256;
2353 else
2354 v->value_alloc_len *= 2;
2355 if (v->value_alloc_len < new_value_len + 1)
2356 v->value_alloc_len = VAR_ALIGN_VALUE_ALLOC (new_value_len + 1 + value_len /*future*/ );
2357 # ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2358 if ((append || !v->value_length) && !v->rdonly_val)
2359 # else
2360 if (append || !v->value_length)
2361 # endif
2362 v->value = xrealloc (v->value, v->value_alloc_len);
2363 else
2365 /* avoid the extra memcpy the xrealloc may have to do */
2366 char *new_buf = xmalloc (v->value_alloc_len);
2367 memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
2368 done_1st_prepend_copy = 1;
2369 # ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2370 if (v->rdonly_val)
2371 v->rdonly_val = 0;
2372 else
2373 # endif
2374 free (v->value);
2375 v->value = new_buf;
2377 MAKE_STATS_2(v->reallocs++);
2380 /* insert the new bits */
2381 if (v->value_length != 0)
2383 if (append)
2385 v->value[v->value_length] = ' ';
2386 memcpy (&v->value[v->value_length + 1], value, value_len + 1);
2388 else
2390 if (!done_1st_prepend_copy)
2391 memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
2392 v->value[value_len] = ' ';
2393 memcpy (v->value, value, value_len);
2396 else
2397 memcpy (v->value, value, value_len + 1);
2398 v->value_length = new_value_len;
2399 VARIABLE_CHANGED (v);
2402 struct variable *
2403 do_variable_definition_append (const floc *flocp, struct variable *v,
2404 const char *value, unsigned int value_len,
2405 int simple_value, enum variable_origin origin,
2406 int append)
2408 if (env_overrides && origin == o_env)
2409 origin = o_env_override;
2411 if (env_overrides && v->origin == o_env)
2412 /* V came from in the environment. Since it was defined
2413 before the switches were parsed, it wasn't affected by -e. */
2414 v->origin = o_env_override;
2416 /* A variable of this name is already defined.
2417 If the old definition is from a stronger source
2418 than this one, don't redefine it. */
2419 if ((int) origin < (int) v->origin)
2420 return v;
2421 v->origin = origin;
2423 /* location */
2424 if (flocp != 0)
2425 v->fileinfo = *flocp;
2427 /* The juicy bits, append the specified value to the variable
2428 This is a heavily exercised code path in kBuild. */
2429 if (value_len == ~0U)
2430 value_len = strlen (value);
2431 if (v->recursive || simple_value)
2432 append_string_to_variable (v, value, value_len, append);
2433 else
2434 /* The previous definition of the variable was simple.
2435 The new value comes from the old value, which was expanded
2436 when it was set; and from the expanded new value. */
2437 append_expanded_string_to_variable (v, value, value_len, append);
2439 /* update the variable */
2440 return v;
2442 #endif /* CONFIG_WITH_VALUE_LENGTH */
2444 static struct variable *
2445 set_special_var (struct variable *var)
2447 if (streq (var->name, RECIPEPREFIX_NAME))
2449 /* The user is resetting the command introduction prefix. This has to
2450 happen immediately, so that subsequent rules are interpreted
2451 properly. */
2452 cmd_prefix = var->value[0]=='\0' ? RECIPEPREFIX_DEFAULT : var->value[0];
2455 return var;
2458 /* Given a string, shell-execute it and return a malloc'ed string of the
2459 * result. This removes only ONE newline (if any) at the end, for maximum
2460 * compatibility with the *BSD makes. If it fails, returns NULL. */
2462 static char *
2463 shell_result (const char *p)
2465 char *buf;
2466 unsigned int len;
2467 char *args[2];
2468 char *result;
2470 install_variable_buffer (&buf, &len);
2472 args[0] = (char *) p;
2473 args[1] = NULL;
2474 variable_buffer_output (func_shell_base (variable_buffer, args, 0), "\0", 1);
2475 result = strdup (variable_buffer);
2477 restore_variable_buffer (buf, len);
2478 return result;
2481 /* Given a variable, a value, and a flavor, define the variable.
2482 See the try_variable_definition() function for details on the parameters. */
2484 struct variable *
2485 #ifndef CONFIG_WITH_VALUE_LENGTH
2486 do_variable_definition (const floc *flocp, const char *varname,
2487 const char *value, enum variable_origin origin,
2488 enum variable_flavor flavor, int target_var)
2489 #else /* CONFIG_WITH_VALUE_LENGTH */
2490 do_variable_definition_2 (const floc *flocp,
2491 const char *varname, const char *value,
2492 unsigned int value_len, int simple_value,
2493 char *free_value,
2494 enum variable_origin origin,
2495 enum variable_flavor flavor,
2496 int target_var)
2497 #endif /* CONFIG_WITH_VALUE_LENGTH */
2499 const char *p;
2500 char *alloc_value = NULL;
2501 struct variable *v;
2502 int append = 0;
2503 int conditional = 0;
2504 const size_t varname_len = strlen (varname); /* bird */
2506 #ifdef CONFIG_WITH_VALUE_LENGTH
2507 if (value_len == ~0U)
2508 value_len = strlen (value);
2509 else
2510 assert (value_len == strlen (value));
2511 #endif
2513 /* Calculate the variable's new value in VALUE. */
2515 switch (flavor)
2517 default:
2518 case f_bogus:
2519 /* Should not be possible. */
2520 abort ();
2521 case f_simple:
2522 /* A simple variable definition "var := value". Expand the value.
2523 We have to allocate memory since otherwise it'll clobber the
2524 variable buffer, and we may still need that if we're looking at a
2525 target-specific variable. */
2526 #ifndef CONFIG_WITH_VALUE_LENGTH
2527 p = alloc_value = allocated_variable_expand (value);
2528 #else /* CONFIG_WITH_VALUE_LENGTH */
2529 if (!simple_value)
2530 p = alloc_value = allocated_variable_expand_2 (value, value_len, &value_len);
2531 else
2533 if (value_len == ~0U)
2534 value_len = strlen (value);
2535 if (!free_value)
2536 p = alloc_value = xstrndup (value, value_len);
2537 else
2539 assert (value == free_value);
2540 p = alloc_value = free_value;
2541 free_value = 0;
2544 #endif /* CONFIG_WITH_VALUE_LENGTH */
2545 break;
2546 case f_shell:
2548 /* A shell definition "var != value". Expand value, pass it to
2549 the shell, and store the result in recursively-expanded var. */
2550 char *q = allocated_variable_expand (value);
2551 p = alloc_value = shell_result (q);
2552 free (q);
2553 flavor = f_recursive;
2554 break;
2556 case f_conditional:
2557 /* A conditional variable definition "var ?= value".
2558 The value is set IFF the variable is not defined yet. */
2559 v = lookup_variable (varname, varname_len);
2560 if (v)
2561 #ifndef CONFIG_WITH_VALUE_LENGTH
2562 return v->special ? set_special_var (v) : v;
2563 #else /* CONFIG_WITH_VALUE_LENGTH */
2565 if (free_value)
2566 free (free_value);
2567 return v->special ? set_special_var (v) : v;
2569 #endif /* CONFIG_WITH_VALUE_LENGTH */
2571 conditional = 1;
2572 flavor = f_recursive;
2573 /* FALLTHROUGH */
2574 case f_recursive:
2575 /* A recursive variable definition "var = value".
2576 The value is used verbatim. */
2577 p = value;
2578 break;
2579 #ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2580 case f_append:
2581 case f_prepend:
2583 const enum variable_flavor org_flavor = flavor;
2584 #else
2585 case f_append:
2587 #endif
2589 /* If we have += but we're in a target variable context, we want to
2590 append only with other variables in the context of this target. */
2591 if (target_var)
2593 append = 1;
2594 v = lookup_variable_in_set (varname, varname_len,
2595 current_variable_set_list->set);
2597 /* Don't append from the global set if a previous non-appending
2598 target-specific variable definition exists. */
2599 if (v && !v->append)
2600 append = 0;
2602 #ifdef KMK
2603 else if ( g_pTopKbEvalData
2604 || ( varname_len > 3
2605 && varname[0] == '['
2606 && is_kbuild_object_variable_accessor (varname, varname_len)) )
2608 v = kbuild_object_variable_pre_append (varname, varname_len,
2609 value, value_len, simple_value,
2610 origin, org_flavor == f_append, flocp);
2611 if (free_value)
2612 free (free_value);
2613 return v;
2615 #endif
2616 #ifdef CONFIG_WITH_LOCAL_VARIABLES
2617 /* If 'local', restrict it to the current variable context. */
2618 else if (origin == o_local)
2619 v = lookup_variable_in_set (varname, varname_len,
2620 current_variable_set_list->set);
2621 #endif
2622 else
2623 v = lookup_variable (varname, varname_len);
2625 if (v == 0)
2627 /* There was no old value.
2628 This becomes a normal recursive definition. */
2629 p = value;
2630 flavor = f_recursive;
2632 else
2634 #ifdef CONFIG_WITH_VALUE_LENGTH
2635 v->append = append;
2636 v = do_variable_definition_append (flocp, v, value, value_len,
2637 simple_value, origin,
2638 # ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2639 org_flavor == f_append);
2640 # else
2642 # endif
2643 if (free_value)
2644 free (free_value);
2645 return v;
2646 #else /* !CONFIG_WITH_VALUE_LENGTH */
2648 /* Paste the old and new values together in VALUE. */
2650 unsigned int oldlen, vallen;
2651 const char *val;
2652 char *tp = NULL;
2654 val = value;
2655 if (v->recursive)
2656 /* The previous definition of the variable was recursive.
2657 The new value is the unexpanded old and new values. */
2658 flavor = f_recursive;
2659 else
2660 /* The previous definition of the variable was simple.
2661 The new value comes from the old value, which was expanded
2662 when it was set; and from the expanded new value. Allocate
2663 memory for the expansion as we may still need the rest of the
2664 buffer if we're looking at a target-specific variable. */
2665 val = tp = allocated_variable_expand (val);
2667 oldlen = strlen (v->value);
2668 vallen = strlen (val);
2669 p = alloc_value = xmalloc (oldlen + 1 + vallen + 1);
2670 # ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2671 if (org_flavor == f_prepend)
2673 memcpy (alloc_value, val, vallen);
2674 alloc_value[oldlen] = ' ';
2675 memcpy (&alloc_value[oldlen + 1], v->value, oldlen + 1);
2677 else
2678 # endif /* CONFIG_WITH_PREPEND_ASSIGNMENT */
2680 memcpy (alloc_value, v->value, oldlen);
2681 alloc_value[oldlen] = ' ';
2682 memcpy (&alloc_value[oldlen + 1], val, vallen + 1);
2685 free (tp);
2686 #endif /* !CONFIG_WITH_VALUE_LENGTH */
2691 #ifdef __MSDOS__
2692 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
2693 non-Unix systems don't conform to this default configuration (in
2694 fact, most of them don't even have '/bin'). On the other hand,
2695 $SHELL in the environment, if set, points to the real pathname of
2696 the shell.
2697 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
2698 the Makefile override $SHELL from the environment. But first, we
2699 look for the basename of the shell in the directory where SHELL=
2700 points, and along the $PATH; if it is found in any of these places,
2701 we define $SHELL to be the actual pathname of the shell. Thus, if
2702 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
2703 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
2704 defining SHELL to be "d:/unix/bash.exe". */
2705 if ((origin == o_file || origin == o_override)
2706 && strcmp (varname, "SHELL") == 0)
2708 PATH_VAR (shellpath);
2709 extern char * __dosexec_find_on_path (const char *, char *[], char *);
2711 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
2712 if (__dosexec_find_on_path (p, NULL, shellpath))
2714 char *tp;
2716 for (tp = shellpath; *tp; tp++)
2717 if (*tp == '\\')
2718 *tp = '/';
2720 v = define_variable_loc (varname, varname_len,
2721 shellpath, origin, flavor == f_recursive,
2722 flocp);
2724 else
2726 const char *shellbase, *bslash;
2727 struct variable *pathv = lookup_variable ("PATH", 4);
2728 char *path_string;
2729 char *fake_env[2];
2730 size_t pathlen = 0;
2732 shellbase = strrchr (p, '/');
2733 bslash = strrchr (p, '\\');
2734 if (!shellbase || bslash > shellbase)
2735 shellbase = bslash;
2736 if (!shellbase && p[1] == ':')
2737 shellbase = p + 1;
2738 if (shellbase)
2739 shellbase++;
2740 else
2741 shellbase = p;
2743 /* Search for the basename of the shell (with standard
2744 executable extensions) along the $PATH. */
2745 if (pathv)
2746 pathlen = strlen (pathv->value);
2747 path_string = xmalloc (5 + pathlen + 2 + 1);
2748 /* On MSDOS, current directory is considered as part of $PATH. */
2749 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
2750 fake_env[0] = path_string;
2751 fake_env[1] = 0;
2752 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
2754 char *tp;
2756 for (tp = shellpath; *tp; tp++)
2757 if (*tp == '\\')
2758 *tp = '/';
2760 v = define_variable_loc (varname, varname_len,
2761 shellpath, origin,
2762 flavor == f_recursive, flocp);
2764 else
2765 v = lookup_variable (varname, varname_len);
2767 free (path_string);
2770 else
2771 #endif /* __MSDOS__ */
2772 #ifdef WINDOWS32
2773 if ( varname_len == sizeof("SHELL") - 1 /* bird */
2774 && (origin == o_file || origin == o_override || origin == o_command)
2775 && streq (varname, "SHELL"))
2777 extern const char *default_shell;
2779 /* Call shell locator function. If it returns TRUE, then
2780 set no_default_sh_exe to indicate sh was found and
2781 set new value for SHELL variable. */
2783 if (find_and_set_default_shell (p))
2785 v = define_variable_in_set (varname, varname_len, default_shell,
2786 # ifdef CONFIG_WITH_VALUE_LENGTH
2787 ~0U, 1 /* duplicate_value */,
2788 # endif
2789 origin, flavor == f_recursive,
2790 (target_var
2791 ? current_variable_set_list->set
2792 : NULL),
2793 flocp);
2794 no_default_sh_exe = 0;
2796 else
2798 char *tp = alloc_value;
2800 alloc_value = allocated_variable_expand (p);
2802 if (find_and_set_default_shell (alloc_value))
2804 v = define_variable_in_set (varname, varname_len, p,
2805 #ifdef CONFIG_WITH_VALUE_LENGTH
2806 ~0U, 1 /* duplicate_value */,
2807 #endif
2808 origin, flavor == f_recursive,
2809 (target_var
2810 ? current_variable_set_list->set
2811 : NULL),
2812 flocp);
2813 no_default_sh_exe = 0;
2815 else
2816 v = lookup_variable (varname, varname_len);
2818 free (tp);
2821 else
2822 #endif
2824 /* If we are defining variables inside an $(eval ...), we might have a
2825 different variable context pushed, not the global context (maybe we're
2826 inside a $(call ...) or something. Since this function is only ever
2827 invoked in places where we want to define globally visible variables,
2828 make sure we define this variable in the global set. */
2830 v = define_variable_in_set (varname, varname_len, p,
2831 #ifdef CONFIG_WITH_VALUE_LENGTH
2832 value_len, !alloc_value,
2833 #endif
2834 origin, flavor == f_recursive,
2835 #ifdef CONFIG_WITH_LOCAL_VARIABLES
2836 (target_var || origin == o_local
2837 #else
2838 (target_var
2839 #endif
2840 ? current_variable_set_list->set : NULL),
2841 flocp);
2842 v->append = append;
2843 v->conditional = conditional;
2845 #ifndef CONFIG_WITH_VALUE_LENGTH
2846 free (alloc_value);
2847 #else
2848 if (free_value)
2849 free (free_value);
2850 #endif
2852 return v->special ? set_special_var (v) : v;
2855 /* Parse P (a null-terminated string) as a variable definition.
2857 If it is not a variable definition, return NULL and the contents of *VAR
2858 are undefined, except NAME is set to the first non-space character or NIL.
2860 If it is a variable definition, return a pointer to the char after the
2861 assignment token and set the following fields (only) of *VAR:
2862 name : name of the variable (ALWAYS SET) (NOT NUL-TERMINATED!)
2863 length : length of the variable name
2864 value : value of the variable (nul-terminated)
2865 flavor : flavor of the variable
2866 Other values in *VAR are unchanged.
2869 char *
2870 parse_variable_definition (const char *p, struct variable *var)
2872 int wspace = 0;
2873 const char *e = NULL;
2875 /** @todo merge 4.2.1: parse_variable_definition does more now */
2876 NEXT_TOKEN (p);
2877 var->name = (char *)p;
2878 var->length = 0;
2880 while (1)
2882 int c = *p++;
2884 /* If we find a comment or EOS, it's not a variable definition. */
2885 if (STOP_SET (c, MAP_COMMENT|MAP_NUL))
2886 return NULL;
2888 if (c == '$')
2890 /* This begins a variable expansion reference. Make sure we don't
2891 treat chars inside the reference as assignment tokens. */
2892 char closeparen;
2893 unsigned int count;
2895 c = *p++;
2896 if (c == '(')
2897 closeparen = ')';
2898 else if (c == '{')
2899 closeparen = '}';
2900 else if (c == '\0')
2901 return NULL;
2902 else
2903 /* '$$' or '$X'. Either way, nothing special to do here. */
2904 continue;
2906 /* P now points past the opening paren or brace.
2907 Count parens or braces until it is matched. */
2908 for (count = 1; *p != '\0'; ++p)
2910 if (*p == closeparen && --count == 0)
2912 ++p;
2913 break;
2915 if (*p == c)
2916 ++count;
2918 continue;
2921 /* If we find whitespace skip it, and remember we found it. */
2922 if (ISBLANK (c))
2924 wspace = 1;
2925 e = p - 1;
2926 NEXT_TOKEN (p);
2927 c = *p;
2928 if (c == '\0')
2929 return NULL;
2930 ++p;
2934 if (c == '=')
2936 var->flavor = f_recursive;
2937 if (! e)
2938 e = p - 1;
2939 break;
2942 /* Match assignment variants (:=, +=, ?=, !=) */
2943 if (*p == '=')
2945 switch (c)
2947 case ':':
2948 var->flavor = f_simple;
2949 break;
2950 case '+':
2951 var->flavor = f_append;
2952 break;
2953 #ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2954 case '<':
2955 var->flavor = f_prepend;
2956 break;
2957 #endif
2958 case '?':
2959 var->flavor = f_conditional;
2960 break;
2961 case '!':
2962 var->flavor = f_shell;
2963 break;
2964 default:
2965 /* If we skipped whitespace, non-assignments means no var. */
2966 if (wspace)
2967 return NULL;
2969 /* Might be assignment, or might be $= or #=. Check. */
2970 continue;
2972 if (! e)
2973 e = p - 1;
2974 ++p;
2975 break;
2978 /* Check for POSIX ::= syntax */
2979 if (c == ':')
2981 /* A colon other than :=/::= is not a variable defn. */
2982 if (*p != ':' || p[1] != '=')
2983 return NULL;
2985 /* POSIX allows ::= to be the same as GNU make's := */
2986 var->flavor = f_simple;
2987 if (! e)
2988 e = p - 1;
2989 p += 2;
2990 break;
2993 /* If we skipped whitespace, non-assignments means no var. */
2994 if (wspace)
2995 return NULL;
2998 var->length = e - var->name;
2999 var->value = next_token (p);
3000 #ifdef CONFIG_WITH_VALUE_LENGTH
3001 var->value_alloc_len = ~(unsigned int)0;
3002 var->value_length = -1;
3003 # ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
3004 var->rdonly_val = 0;
3005 # endif
3006 #endif
3007 return (char *)p;
3010 /* Try to interpret LINE (a null-terminated string) as a variable definition.
3012 If LINE was recognized as a variable definition, a pointer to its 'struct
3013 variable' is returned. If LINE is not a variable definition, NULL is
3014 returned. */
3016 struct variable *
3017 assign_variable_definition (struct variable *v, const char *line IF_WITH_VALUE_LENGTH_PARAM(char *eos))
3019 #ifndef CONFIG_WITH_VALUE_LENGTH
3020 char *name;
3021 #endif
3023 if (!parse_variable_definition (line, v))
3024 return NULL;
3026 #ifdef CONFIG_WITH_VALUE_LENGTH
3027 if (eos)
3029 v->value_length = eos - v->value;
3030 assert (strchr (v->value, '\0') == eos);
3032 #endif
3034 /* Expand the name, so "$(foo)bar = baz" works. */
3035 #ifndef CONFIG_WITH_VALUE_LENGTH
3036 name = alloca (v->length + 1);
3037 memcpy (name, v->name, v->length);
3038 name[v->length] = '\0';
3039 v->name = allocated_variable_expand (name);
3040 #else /* CONFIG_WITH_VALUE_LENGTH */
3041 v->name = allocated_variable_expand_2 (v->name, v->length, NULL);
3042 #endif /* CONFIG_WITH_VALUE_LENGTH */
3044 if (v->name[0] == '\0')
3045 O (fatal, &v->fileinfo, _("empty variable name"));
3047 return v;
3050 /* Try to interpret LINE (a null-terminated string) as a variable definition.
3052 ORIGIN may be o_file, o_override, o_env, o_env_override, o_local,
3053 or o_command specifying that the variable definition comes
3054 from a makefile, an override directive, the environment with
3055 or without the -e switch, or the command line.
3057 See the comments for assign_variable_definition().
3059 If LINE was recognized as a variable definition, a pointer to its 'struct
3060 variable' is returned. If LINE is not a variable definition, NULL is
3061 returned. */
3063 struct variable *
3064 try_variable_definition (const floc *flocp, const char *line
3065 IF_WITH_VALUE_LENGTH_PARAM(char *eos),
3066 enum variable_origin origin, int target_var)
3068 struct variable v;
3069 struct variable *vp;
3071 if (flocp != 0)
3072 v.fileinfo = *flocp;
3073 else
3074 v.fileinfo.filenm = 0;
3076 #ifndef CONFIG_WITH_VALUE_LENGTH
3077 if (!assign_variable_definition (&v, line))
3078 return 0;
3080 vp = do_variable_definition (flocp, v.name, v.value,
3081 origin, v.flavor, target_var);
3082 #else
3083 if (!assign_variable_definition (&v, line, eos))
3084 return 0;
3086 vp = do_variable_definition_2 (flocp, v.name, v.value, v.value_length,
3087 0, NULL, origin, v.flavor, target_var);
3088 #endif
3090 #ifndef CONFIG_WITH_STRCACHE2
3091 free (v.name);
3092 #else
3093 free ((char *)v.name);
3094 #endif
3096 return vp;
3099 #if defined (CONFIG_WITH_COMPILER) || defined (CONFIG_WITH_MAKE_STATS)
3100 static unsigned long var_stats_evalvals, var_stats_evalvaled;
3101 static unsigned long var_stats_expands, var_stats_expanded;
3102 #endif
3103 #ifdef CONFIG_WITH_COMPILER
3104 static unsigned long var_stats_expandprogs, var_stats_evalprogs;
3105 #endif
3106 #ifdef CONFIG_WITH_MAKE_STATS
3107 static unsigned long var_stats_changes, var_stats_changed;
3108 static unsigned long var_stats_reallocs, var_stats_realloced;
3109 static unsigned long var_stats_references, var_stats_referenced;
3110 static unsigned long var_stats_val_len, var_stats_val_alloc_len;
3111 static unsigned long var_stats_val_rdonly_len;
3112 #endif
3114 /* Print information for variable V, prefixing it with PREFIX. */
3116 static void
3117 print_variable (const void *item, void *arg)
3119 const struct variable *v = item;
3120 const char *prefix = arg;
3121 const char *origin;
3122 #ifdef KMK
3123 const struct variable *alias = v;
3124 RESOLVE_ALIAS_VARIABLE(v);
3125 #endif
3127 switch (v->origin)
3129 case o_automatic:
3130 origin = _("automatic");
3131 break;
3132 case o_default:
3133 origin = _("default");
3134 break;
3135 case o_env:
3136 origin = _("environment");
3137 break;
3138 case o_file:
3139 origin = _("makefile");
3140 break;
3141 case o_env_override:
3142 origin = _("environment under -e");
3143 break;
3144 case o_command:
3145 origin = _("command line");
3146 break;
3147 case o_override:
3148 origin = _("'override' directive");
3149 break;
3150 #ifdef CONFIG_WITH_LOCAL_VARIABLES
3151 case o_local:
3152 origin = _("`local' directive");
3153 break;
3154 #endif
3155 case o_invalid:
3156 default:
3157 abort ();
3159 fputs ("# ", stdout);
3160 fputs (origin, stdout);
3161 if (v->private_var)
3162 fputs (" private", stdout);
3163 #ifndef KMK
3164 if (v->fileinfo.filenm)
3165 printf (_(" (from '%s', line %lu)"),
3166 v->fileinfo.filenm, v->fileinfo.lineno + v->fileinfo.offset);
3167 #else /* KMK */
3168 if (alias->fileinfo.filenm)
3169 printf (_(" (from '%s', line %lu)"),
3170 alias->fileinfo.filenm, alias->fileinfo.lineno);
3171 if (alias->aliased)
3172 fputs (" aliased", stdout);
3173 if (alias->alias)
3174 printf (_(", alias for '%s'"), v->name);
3175 #endif /* KMK */
3177 #if defined (CONFIG_WITH_COMPILER) || defined (CONFIG_WITH_MAKE_STATS)
3178 if (v->evalval_count != 0)
3180 # ifdef CONFIG_WITH_MAKE_STATS
3181 printf (_(", %u evalvals (%llu ticks)"), v->evalval_count, v->cTicksEvalVal);
3182 # else
3183 printf (_(", %u evalvals"), v->evalval_count);
3184 # endif
3185 var_stats_evalvaled++;
3187 var_stats_evalvals += v->evalval_count;
3189 if (v->expand_count != 0)
3191 printf (_(", %u expands"), v->expand_count);
3192 var_stats_expanded++;
3194 var_stats_expands += v->expand_count;
3196 # ifdef CONFIG_WITH_COMPILER
3197 if (v->evalprog != 0)
3199 printf (_(", evalprog"));
3200 var_stats_evalprogs++;
3202 if (v->expandprog != 0)
3204 printf (_(", expandprog"));
3205 var_stats_expandprogs++;
3207 # endif
3208 #endif
3210 #ifdef CONFIG_WITH_MAKE_STATS
3211 if (v->changes != 0)
3213 printf (_(", %u changes"), v->changes);
3214 var_stats_changed++;
3216 var_stats_changes += v->changes;
3218 if (v->reallocs != 0)
3220 printf (_(", %u reallocs"), v->reallocs);
3221 var_stats_realloced++;
3223 var_stats_reallocs += v->reallocs;
3225 if (v->references != 0)
3227 printf (_(", %u references"), v->references);
3228 var_stats_referenced++;
3230 var_stats_references += v->references;
3232 var_stats_val_len += v->value_length;
3233 if (v->value_alloc_len)
3234 var_stats_val_alloc_len += v->value_alloc_len;
3235 else
3236 var_stats_val_rdonly_len += v->value_length;
3237 assert (v->value_length == strlen (v->value));
3238 /*assert (v->rdonly_val ? !v->value_alloc_len : v->value_alloc_len > v->value_length); - FIXME */
3239 #endif /* CONFIG_WITH_MAKE_STATS */
3240 putchar ('\n');
3241 fputs (prefix, stdout);
3243 /* Is this a 'define'? */
3244 if (v->recursive && strchr (v->value, '\n') != 0)
3245 #ifndef KMK /** @todo language feature for aliases */
3246 printf ("define %s\n%s\nendef\n", v->name, v->value);
3247 #else
3248 printf ("define %s\n%s\nendef\n", alias->name, v->value);
3249 #endif
3250 else
3252 char *p;
3254 #ifndef KMK /** @todo language feature for aliases */
3255 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
3256 #else
3257 printf ("%s %s= ", alias->name, v->recursive ? v->append ? "+" : "" : ":");
3258 #endif
3260 /* Check if the value is just whitespace. */
3261 p = next_token (v->value);
3262 if (p != v->value && *p == '\0')
3263 /* All whitespace. */
3264 printf ("$(subst ,,%s)", v->value);
3265 else if (v->recursive)
3266 fputs (v->value, stdout);
3267 else
3268 /* Double up dollar signs. */
3269 for (p = v->value; *p != '\0'; ++p)
3271 if (*p == '$')
3272 putchar ('$');
3273 putchar (*p);
3275 putchar ('\n');
3280 static void
3281 print_auto_variable (const void *item, void *arg)
3283 const struct variable *v = item;
3285 if (v->origin == o_automatic)
3286 print_variable (item, arg);
3290 static void
3291 print_noauto_variable (const void *item, void *arg)
3293 const struct variable *v = item;
3295 if (v->origin != o_automatic)
3296 print_variable (item, arg);
3300 /* Print all the variables in SET. PREFIX is printed before
3301 the actual variable definitions (everything else is comments). */
3303 #ifndef KMK
3304 static
3305 #endif
3306 void
3307 print_variable_set (struct variable_set *set, const char *prefix, int pauto)
3309 #if defined (CONFIG_WITH_COMPILER) || defined (CONFIG_WITH_MAKE_STATS)
3310 var_stats_expands = var_stats_expanded = var_stats_evalvals
3311 = var_stats_evalvaled = 0;
3312 #endif
3313 #ifdef CONFIG_WITH_COMPILER
3314 var_stats_expandprogs = var_stats_evalprogs = 0;
3315 #endif
3316 #ifdef CONFIG_WITH_MAKE_STATS
3317 var_stats_changes = var_stats_changed = var_stats_reallocs
3318 = var_stats_realloced = var_stats_references = var_stats_referenced
3319 = var_stats_val_len = var_stats_val_alloc_len
3320 = var_stats_val_rdonly_len = 0;
3321 #endif
3323 hash_map_arg (&set->table, (pauto ? print_auto_variable : print_variable),
3324 (void *)prefix);
3326 if (set->table.ht_fill)
3328 #ifdef CONFIG_WITH_MAKE_STATS
3329 unsigned long fragmentation;
3331 fragmentation = var_stats_val_alloc_len - (var_stats_val_len - var_stats_val_rdonly_len);
3332 printf(_("# variable set value stats:\n\
3333 # strings %7lu bytes, readonly %6lu bytes\n"),
3334 var_stats_val_len, var_stats_val_rdonly_len);
3336 if (var_stats_val_alloc_len)
3337 printf(_("# allocated %7lu bytes, fragmentation %6lu bytes (%u%%)\n"),
3338 var_stats_val_alloc_len, fragmentation,
3339 (unsigned int)((100.0 * fragmentation) / var_stats_val_alloc_len));
3341 if (var_stats_changed)
3342 printf(_("# changed %5lu (%2u%%), changes %6lu\n"),
3343 var_stats_changed,
3344 (unsigned int)((100.0 * var_stats_changed) / set->table.ht_fill),
3345 var_stats_changes);
3347 if (var_stats_realloced)
3348 printf(_("# reallocated %5lu (%2u%%), reallocations %6lu\n"),
3349 var_stats_realloced,
3350 (unsigned int)((100.0 * var_stats_realloced) / set->table.ht_fill),
3351 var_stats_reallocs);
3353 if (var_stats_referenced)
3354 printf(_("# referenced %5lu (%2u%%), references %6lu\n"),
3355 var_stats_referenced,
3356 (unsigned int)((100.0 * var_stats_referenced) / set->table.ht_fill),
3357 var_stats_references);
3358 #endif
3359 #if defined (CONFIG_WITH_COMPILER) || defined (CONFIG_WITH_MAKE_STATS)
3360 if (var_stats_evalvals)
3361 printf(_("# evalvaled %5lu (%2u%%), evalval calls %6lu\n"),
3362 var_stats_evalvaled,
3363 (unsigned int)((100.0 * var_stats_evalvaled) / set->table.ht_fill),
3364 var_stats_evalvals);
3365 if (var_stats_expands)
3366 printf(_("# expanded %5lu (%2u%%), expands %6lu\n"),
3367 var_stats_expanded,
3368 (unsigned int)((100.0 * var_stats_expanded) / set->table.ht_fill),
3369 var_stats_expands);
3370 #endif
3371 #ifdef CONFIG_WITH_COMPILER
3372 if (var_stats_expandprogs || var_stats_evalprogs)
3373 printf(_("# eval progs %5lu (%2u%%), expand progs %6lu (%2u%%)\n"),
3374 var_stats_evalprogs,
3375 (unsigned int)((100.0 * var_stats_evalprogs) / set->table.ht_fill),
3376 var_stats_expandprogs,
3377 (unsigned int)((100.0 * var_stats_expandprogs) / set->table.ht_fill));
3378 #endif
3381 fputs (_("# variable set hash-table stats:\n"), stdout);
3382 fputs ("# ", stdout);
3383 hash_print_stats (&set->table, stdout);
3384 putc ('\n', stdout);
3387 /* Print the data base of variables. */
3389 void
3390 print_variable_data_base (void)
3392 puts (_("\n# Variables\n"));
3394 print_variable_set (&global_variable_set, "", 0);
3396 puts (_("\n# Pattern-specific Variable Values"));
3399 struct pattern_var *p;
3400 unsigned int rules = 0;
3402 for (p = pattern_vars; p != 0; p = p->next)
3404 ++rules;
3405 printf ("\n%s :\n", p->target);
3406 print_variable (&p->variable, (void *)"# ");
3409 if (rules == 0)
3410 puts (_("\n# No pattern-specific variable values."));
3411 else
3412 printf (_("\n# %u pattern-specific variable values"), rules);
3415 #ifdef CONFIG_WITH_STRCACHE2
3416 strcache2_print_stats (&variable_strcache, "# ");
3417 #endif
3420 #ifdef CONFIG_WITH_PRINT_STATS_SWITCH
3421 void
3422 print_variable_stats (void)
3424 fputs (_("\n# Global variable hash-table stats:\n# "), stdout);
3425 hash_print_stats (&global_variable_set.table, stdout);
3426 fputs ("\n", stdout);
3428 #endif
3430 /* Print all the local variables of FILE. */
3432 void
3433 print_file_variables (const struct file *file)
3435 if (file->variables != 0)
3436 print_variable_set (file->variables->set, "# ", 1);
3439 void
3440 print_target_variables (const struct file *file)
3442 if (file->variables != 0)
3444 int l = strlen (file->name);
3445 char *t = alloca (l + 3);
3447 strcpy (t, file->name);
3448 t[l] = ':';
3449 t[l+1] = ' ';
3450 t[l+2] = '\0';
3452 hash_map_arg (&file->variables->set->table, print_noauto_variable, t);
3456 #ifdef WINDOWS32
3457 void
3458 sync_Path_environment (void)
3460 char *path = allocated_variable_expand ("$(PATH)");
3461 static char *environ_path = NULL;
3463 if (!path)
3464 return;
3466 /* If done this before, free the previous entry before allocating new one. */
3467 free (environ_path);
3469 /* Create something WINDOWS32 world can grok. */
3470 convert_Path_to_windows32 (path, ';');
3471 environ_path = xstrdup (concat (3, "PATH", "=", path));
3472 putenv (environ_path);
3473 free (path);
3475 #endif