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
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/>. */
28 #include "pathstuff.h"
36 # include <sys/utsname.h>
39 #ifdef CONFIG_WITH_STRCACHE2
42 #ifdef CONFIG_WITH_COMPILER
43 # include "kmk_cc_exec.h"
47 /** Gets the real variable if alias. For use when looking up variables. */
48 # define RESOLVE_ALIAS_VARIABLE(v) \
50 if ((v) != NULL && (v)->alias) \
52 (v) = (struct variable *)(v)->value; \
53 assert ((v)->aliased); \
54 assert (!(v)->alias); \
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;
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. */
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
;
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
)
123 p
->suffix
= suffix
+ 1;
126 last_pattern_vars
[len
] = 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
)
142 unsigned int stemlen
;
144 if (p
->len
> targlen
)
145 /* It can't possibly match. */
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
))
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
162 if (*p
->suffix
== stem
[stemlen
]
163 && (*p
->suffix
== '\0' || streq (&p
->suffix
[1], &stem
[stemlen
+1])))
170 #ifdef CONFIG_WITH_STRCACHE2
171 struct strcache2 variable_strcache
;
174 /* Hash table of all global variable definitions. */
176 #ifndef CONFIG_WITH_STRCACHE2
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
);
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
);
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
;
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
208 #define VARIABLE_BUCKETS 523
211 #ifndef PERFILE_VARIABLE_BUCKETS
212 # ifdef KMK /* Move to Makefile.kmk? */
213 # define PERFILE_VARIABLE_BUCKETS 127
215 #define PERFILE_VARIABLE_BUCKETS 23
218 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
219 # ifdef KMK /* Move to Makefile.kmk? */
220 # define SMALL_SCOPE_VARIABLE_BUCKETS 63
222 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
225 #ifndef ENVIRONMENT_VARIABLE_BUCKETS /* added by bird. */
226 # define ENVIRONMENT_VARIABLE_BUCKETS 256
230 #ifdef KMK /* Drop the 'static' */
231 struct variable_set global_variable_set
;
232 struct variable_set_list global_setlist
234 static struct variable_set global_variable_set
;
235 static struct variable_set_list global_setlist
237 = { 0, &global_variable_set
, 0 };
238 struct variable_set_list
*current_variable_set_list
= &global_setlist
;
240 /* Implement variables. */
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
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
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
,
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
,
278 struct variable
**var_slot
;
279 struct variable var_key
;
282 if (set
== NULL
|| set
== &global_variable_set
)
283 global_variable_generation
++;
286 if (env_overrides
&& origin
== o_env
)
287 origin
= o_env_override
;
291 set
= &global_variable_set
;
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
)
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
;
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
);
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
324 if (HASH_VACANT (v
) && (origin
!= o_env
))
326 struct variable
* vms_variable
;
327 char * vname
= alloca (length
+ 1);
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
,
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
)))
355 var_key
.length
= length
;
356 var_slot
= (struct variable
**) hash_find_slot_strcached (&set
->table
, &var_key
);
361 assert (!v
|| (v
->name
== name
&& !HASH_VACANT (v
)));
364 #endif /* CONFIG_WITH_STRCACHE2 */
365 if (! HASH_VACANT (v
))
368 RESOLVE_ALIAS_VARIABLE(v
);
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
);
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
)
390 v
->rdonly_val
= duplicate_value
== -1;
391 v
->value
= (char *) value
;
392 v
->value_alloc_len
= 0;
396 v
->value
= (char *) value
;
397 v
->value_alloc_len
= value_len
+ 1;
402 if (v
->value_alloc_len
<= value_len
)
404 # ifdef CONFIG_WITH_RDONLY_VARIABLE_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 */
419 v
->value
= xstrdup (value
);
420 #endif /* !CONFIG_WITH_VALUE_LENGTH */
422 v
->fileinfo
= *flocp
;
424 v
->fileinfo
.filenm
= 0;
426 v
->recursive
= recursive
;
427 VARIABLE_CHANGED (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
));
437 v
= alloccache_alloc (&variable_cache
);
439 #ifndef CONFIG_WITH_STRCACHE2
440 v
->name
= xstrndup (name
, length
);
442 v
->name
= name
; /* already cached. */
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
);
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;
461 v
->value
= (char *)value
;
465 # ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
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 */
476 v
->fileinfo
= *flocp
;
478 v
->fileinfo
.filenm
= 0;
480 v
->recursive
= recursive
;
491 v
->export
= v_default
;
492 #ifdef CONFIG_WITH_COMPILER
493 v
->recursive_without_dollar
= 0;
496 v
->evalval_count
= 0;
499 MAKE_STATS_2(v
->expand_count
= 0);
500 MAKE_STATS_2(v
->evalval_count
= 0);
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);
508 if (*name
!= '_' && (*name
< 'A' || *name
> 'Z')
509 && (*name
< 'a' || *name
> 'z'))
513 for (++name
; *name
!= '\0'; ++name
)
514 if (*name
!= '_' && (*name
< 'a' || *name
> 'z')
515 && (*name
< 'A' || *name
> 'Z') && !ISDIGIT(*name
))
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
);
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). */
536 free_variable_name_and_value (const void *item
)
538 struct variable
*v
= (struct variable
*) item
;
539 #ifndef CONFIG_WITH_STRCACHE2
542 #ifdef CONFIG_WITH_COMPILER
543 if (v
->evalprog
|| v
->expandprog
)
544 kmk_cc_variable_deleted (v
);
546 #ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
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);
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
);
568 undefine_variable_in_set (const char *name
, unsigned int length
,
569 enum variable_origin origin
,
570 struct variable_set
*set
)
573 struct variable
**var_slot
;
574 struct variable var_key
;
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
);
584 var_key
.name
= strcache2_lookup(&variable_strcache
, name
, length
);
587 var_key
.length
= length
;
588 var_slot
= (struct variable
**) hash_find_slot_strcached (&set
->table
, &var_key
);
591 if (set
== &global_variable_set
)
592 global_variable_generation
++;
595 if (env_overrides
&& origin
== o_env
)
596 origin
= o_env_override
;
599 if (! HASH_VACANT (v
))
602 if (v
->aliased
|| v
->alias
)
605 OS (error
, NULL
, _("Cannot undefine the aliased variable '%s'"), v
->name
);
607 OS (error
, NULL
, _("Cannot undefine the variable alias '%s'"), v
->name
);
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
);
626 free_variable_name_and_value (v
);
627 #ifndef CONFIG_WITH_ALLOC_CACHES
630 alloccache_free (&variable_cache
, v
);
632 if (set
== &global_variable_set
)
633 ++variable_changenum
;
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. */
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
)
648 struct variable
**var_slot
;
651 if (set
== NULL
|| set
== &global_variable_set
)
652 global_variable_generation
++;
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
;
663 var_key
.length
= length
;
664 var_slot
= (struct variable
**) hash_find_slot_strcached (&set
->table
, &var_key
);
669 assert (!v
|| (v
->name
== name
&& !HASH_VACANT (v
)));
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
)
686 if (v
->value
!= 0 && !v
->rdonly_val
)
688 VARIABLE_CHANGED (v
);
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. */
696 hash_insert_at (&set
->table
, v
, var_slot
);
704 v
->export
= v_default
;
705 #ifdef CONFIG_WITH_COMPILER
706 v
->recursive_without_dollar
= 0;
709 v
->evalval_count
= 0;
712 MAKE_STATS_2(v
->expand_count
= 0);
713 MAKE_STATS_2(v
->evalval_count
= 0);
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);
720 if (*name
!= '_' && (*name
< 'A' || *name
> 'Z')
721 && (*name
< 'a' || *name
> 'z'))
725 for (++name
; *name
!= '\0'; ++name
)
726 if (*name
!= '_' && (*name
< 'a' || *name
> 'z')
727 && (*name
< 'A' || *name
> 'Z') && !ISDIGIT(*name
))
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. */
742 v
->value
= (char *)target
;
743 v
->value_length
= sizeof(*target
); /* Non-zero to provoke trouble. */
744 v
->value_alloc_len
= sizeof(*target
);
746 v
->fileinfo
= *flocp
;
748 v
->fileinfo
.filenm
= 0;
752 /* Mark the target as aliased. */
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
763 .TARGETS expands to a list of all the targets defined in this
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
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
795 if (streq (var->name, ".TARGETS"))
796 var->value = build_target_list (var->value);
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
));
805 unsigned long max
= EXPANSION_INCREMENT (var
->value_length
);
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. */
819 for (; vp
< end
; ++vp
)
820 if (!HASH_VACANT (*vp
))
822 struct variable
*v
= *vp
;
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
);
841 #ifdef CONFIG_WITH_VALUE_LENGTH
842 var
->value_length
= p
- var
->value
- 1;
843 var
->value_alloc_len
= max
;
845 VARIABLE_CHANGED (var
);
847 /* Remember the current variable change number. */
848 last_changenum
= variable_changenum
;
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
;
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);
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);
884 if ( (void *)v
!= hash_deleted_item
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;
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. */
899 if ( (void *)v
!= hash_deleted_item
901 return MY_PREDICT_FALSE (v
->special
) ? lookup_special_var (v
) : v
;
902 } /* inner collision loop */
905 hash_2
= strcache2_get_hash (&variable_strcache
, name
) | 1;
908 /* The other sets, if any. */
910 setlist
= setlist
->next
;
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);
922 /* first iteration unrolled */
923 ht
= &setlist
->set
->table
;
924 MAKE_STATS (ht
->ht_lookups
++);
925 idx
= hash_1
& (ht
->ht_size
- 1);
929 if ( (void *)v
!= hash_deleted_item
931 return MY_PREDICT_FALSE (v
->special
) ? lookup_special_var (v
) : v
;
933 /* the rest of the loop */
937 idx
&= (ht
->ht_size
- 1);
938 v
= (struct variable
*) ht
->ht_vec
[idx
];
939 MAKE_STATS (ht
->ht_collisions
++); /* see reason above */
943 if ( (void *)v
!= hash_deleted_item
945 return MY_PREDICT_FALSE (v
->special
) ? lookup_special_var (v
) : v
;
946 } /* inner collision loop */
950 setlist
= setlist
->next
;
958 lookup_variable_for_assert (const char *name
, unsigned int length
)
960 const struct variable_set_list
*setlist
;
961 struct variable var_key
;
963 var_key
.length
= length
;
965 for (setlist
= current_variable_set_list
;
966 setlist
!= 0; setlist
= setlist
->next
)
969 v
= (struct variable
*) hash_find_item_strcached (&setlist
->set
->table
, &var_key
);
971 return MY_PREDICT_FALSE (v
->special
) ? lookup_special_var (v
) : v
;
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. */
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
;
993 #ifdef CONFIG_WITH_STRCACHE2
994 const char *cached_name
;
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
++);
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
);
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
;
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
))
1037 RESOLVE_ALIAS_VARIABLE(v
);
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
);
1054 #endif /* KMK - need for speed */
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);
1061 strncpy (vname
, name
, length
);
1063 value
= getenv (vname
);
1072 while ((sptr
= strchr (sptr
, '$')))
1083 nvalue
= alloca (strlen (value
) + scnt
+ 1);
1102 return define_variable (vname
, length
, nvalue
, o_env
, 1);
1106 return define_variable (vname
, length
, value
, o_env
, 1);
1114 #ifdef CONFIG_WITH_STRCACHE2
1115 /* Alternative version of lookup_variable that takes a name that's already in
1116 the variable string cache. */
1118 lookup_variable_strcached (const char *name
)
1121 #if 1 /*FIX THIS - ndef KMK*/
1122 const struct variable_set_list
*setlist
;
1123 struct variable var_key
;
1128 strcache2_verify_entry (&variable_strcache
, name
);
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
++);
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
))
1158 RESOLVE_ALIAS_VARIABLE(v
);
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
);
1175 #endif /* KMK - need for speed */
1177 # error "Port me (split out the relevant code from lookup_varaible and call it)"
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. */
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
;
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
++);
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
);
1223 if (set
== &global_variable_set
)
1225 v
= strcache2_get_user_val (&variable_strcache
, cached_name
);
1226 assert (!v
|| v
->name
== cached_name
);
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
);
1237 RESOLVE_ALIAS_VARIABLE(v
);
1239 MAKE_STATS_2 (if (v
) v
->references
++);
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. */
1254 initialize_file_variables (struct file
*file
, int reading
)
1256 struct variable_set_list
*l
= file
->variables
;
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;
1292 if (file
->parent
== 0)
1293 l
->next
= &global_setlist
;
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
);
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. */
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
;
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);
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);
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
));
1384 set
= (struct variable_set
*) alloccache_alloc (&variable_set_cache
);
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
));
1398 setlist
= (struct variable_set_list
*)
1399 alloccache_alloc (&variable_set_list_cache
);
1402 setlist
->next
= current_variable_set_list
;
1403 setlist
->next_is_parent
= 0;
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
);
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
;
1447 current_variable_set_list
= setlist
->next
;
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
1464 hash_map (&set
->table
, free_variable_name_and_value
);
1465 hash_free (&set
->table
, 1);
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
);
1475 /* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
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
,
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
;
1504 /* GKM FIXME: delete in from_set->table */
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
);
1511 #ifdef CONFIG_WITH_COMPILER
1512 if (from_var
->evalprog
|| from_var
->expandprog
)
1513 kmk_cc_variable_deleted (from_var
);
1515 #ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1516 if (!from_var
->rdonly_val
)
1518 free (from_var
->value
);
1524 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
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. */
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. */
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
);
1551 if (setlist1
!= &global_setlist
)
1554 *setlist0
= setlist1
;
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
1565 static unsigned long parse_release_number (const char **ppsz
)
1568 char *psz
= (char *)*ppsz
;
1571 ul
= strtoul (psz
, &psz
, 10);
1572 if (psz
!= NULL
&& *psz
== '.')
1575 psz
= strchr (*ppsz
, '\0');
1584 /* Define the automatic variables, and record the addresses
1585 of their structures so we can change their values quickly. */
1588 define_automatic_variables (void)
1596 struct variable
*envvar1
;
1597 struct variable
*envvar2
;
1601 unsigned long ulMajor
= 0, ulMinor
= 0, ulPatch
= 0, ul4th
= 0;
1604 sprintf (buf
, "%u", makelevel
);
1605 define_variable_cname (MAKELEVEL_NAME
, buf
, o_env
, 0);
1607 sprintf (buf
, "%s%s%s",
1609 (remote_description
== 0 || remote_description
[0] == '\0')
1611 (remote_description
== 0 || remote_description
[0] == '\0')
1612 ? "" : remote_description
);
1614 define_variable_cname ("MAKE_VERSION", buf
, o_default
, 0);
1615 define_variable_cname ("MAKE_HOST", make_host
, o_default
, 0);
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
);
1642 define_variable_cname ("KBUILD_HOST", val
, o_default
, 0);
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
);
1652 define_variable_cname ("KBUILD_HOST_ARCH", val
, o_default
, 0);
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
);
1662 define_variable_cname ("KBUILD_HOST_CPU", val
, o_default
, 0);
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
;
1695 ulMajor
= oix
.dwPlatformId
== 1 ? 0 /*Win95/98/ME*/
1696 : oix
.dwPlatformId
== 3 ? 1 /*WinCE*/
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);
1710 memset (&uts
, 0, sizeof(uts
));
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);
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"
1778 " comp-vars comp-cmds comp-cmds-ex"
1782 " explicit-multitarget"
1784 " prepend-assignment"
1785 " set-conditionals intersects"
1788 " expr if-expr select"
1791 " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var"
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"
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"
1805 # if defined (CONFIG_WITH_RSORT)
1806 strcat (buf
, " rsort");
1808 # if defined (CONFIG_WITH_ABSPATHEX)
1809 strcat (buf
, " abspathex");
1811 # if defined (CONFIG_WITH_TOUPPER_TOLOWER)
1812 strcat (buf
, " toupper tolower");
1814 # if defined (CONFIG_WITH_DEFINED)
1815 strcat (buf
, " defined");
1817 # if defined (CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
1818 strcat (buf
, " comp-vars comp-cmds comp-cmds-ex");
1820 # if defined (CONFIG_WITH_STACK)
1821 strcat (buf
, " stack");
1823 # if defined (CONFIG_WITH_MATH)
1824 strcat (buf
, " math-int");
1826 # if defined (CONFIG_WITH_XARGS)
1827 strcat (buf
, " xargs");
1829 # if defined (CONFIG_WITH_EXPLICIT_MULTITARGET)
1830 strcat (buf
, " explicit-multitarget");
1832 # if defined (CONFIG_WITH_DOT_MUST_MAKE)
1833 strcat (buf
, " dot-must-make");
1835 # if defined (CONFIG_WITH_PREPEND_ASSIGNMENT)
1836 strcat (buf
, " prepend-assignment");
1838 # if defined (CONFIG_WITH_SET_CONDITIONALS)
1839 strcat (buf
, " set-conditionals intersects");
1841 # if defined (CONFIG_WITH_DATE)
1842 strcat (buf
, " date");
1844 # if defined (CONFIG_WITH_FILE_SIZE)
1845 strcat (buf
, " file-size");
1847 # if defined (CONFIG_WITH_IF_CONDITIONALS)
1848 strcat (buf
, " expr if-expr select");
1850 # if defined (CONFIG_WITH_WHERE_FUNCTION)
1851 strcat (buf
, " where");
1853 # if defined (CONFIG_WITH_WHICH)
1854 strcat (buf
, " which");
1856 # if defined (CONFIG_WITH_EVALPLUS)
1857 strcat (buf
, " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var");
1859 # if defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)
1860 strcat (buf
, " make-stats");
1862 # if defined (CONFIG_WITH_COMMANDS_FUNC)
1863 strcat (buf
, " commands");
1865 # if defined (CONFIG_WITH_PRINTF)
1866 strcat (buf
, " printf");
1868 # if defined (CONFIG_WITH_LOOP_FUNCTIONS)
1869 strcat (buf
, " for while");
1871 # if defined (CONFIG_WITH_ROOT_FUNC)
1872 strcat (buf
, " root");
1874 # if defined (CONFIG_WITH_STRING_FUNCTIONS)
1875 strcat (buf
, " length insert pos lastpos substr translate");
1877 # if defined (CONFIG_WITH_DEFINED_FUNCTIONS)
1878 strcat (buf
, " firstdefined lastdefined");
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");
1883 define_variable_cname ("KMK_FEATURES", buf
, o_default
, 0);
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);
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. */
1904 (void) define_variable (shell_str
, shlen
,
1905 mshp
->value
, o_env_override
, 0);
1908 /* $(COMSPEC) shouldn't override $(SHELL). */
1909 struct variable
*shp
= lookup_variable (shell_str
, shlen
);
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);
1950 # warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
1953 if (replace
&& *replace
->value
)
1954 /* overwrite $SHELL */
1955 (void) define_variable (shell_str
, shlen
, replace
->value
,
1956 replace
->origin
, 0);
1958 /* provide a definition if there is none */
1959 (void) define_variable (shell_str
, shlen
, default_shell
,
1965 /* This won't override any definition, but it will provide one if there
1967 v
= define_variable_cname ("SHELL", default_shell
, o_default
, 0);
1969 v
->export
= v_export
; /* Export always SHELL. */
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
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;
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 $@)))",
2006 define_variable_cname ("%D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $%)))",
2008 define_variable_cname ("*D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $*)))",
2010 define_variable_cname ("<D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $<)))",
2012 define_variable_cname ("?D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $?)))",
2014 define_variable_cname ("^D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $^)))",
2016 define_variable_cname ("+D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $+)))",
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);
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
;
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
;
2074 if (v
->origin
== o_default
|| v
->origin
== o_automatic
)
2075 /* Only export default variables by explicit request. */
2078 /* The variable doesn't have a name that can be exported. */
2079 if (! v
->exportable
)
2082 if (! export_all_variables
2083 && v
->origin
!= o_command
2084 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
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
)
2106 if (v
->origin
== o_default
)
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
);
2118 global_variable_set_exports_generation
= global_variable_generation
;
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. */
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
;
2138 #ifdef CONFIG_WITH_STRCACHE2
2139 const char *cached_name
;
2143 if (global_variable_set_exports_generation
!= global_variable_generation
)
2144 update_global_variable_set_exports();
2148 set_list
= current_variable_set_list
;
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
;
2166 if (set
== &global_variable_set
)
2168 assert(s
->next
== NULL
);
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
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
);
2191 assert ((int)strlen(v
->name
) == v
->length
);
2192 gv
= lookup_variable_in_set (v
->name
, v
->length
,
2193 &global_variable_set
);
2196 v
->export
= gv
->export
;
2202 if (v
->origin
== o_default
|| v
->origin
== o_automatic
)
2203 /* Only export default variables by explicit request. */
2206 /* The variable doesn't have a name that can be exported. */
2207 if (! v
->exportable
)
2210 if (! export_all_variables
2211 && v
->origin
!= o_command
2212 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
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
)
2233 if (v
->origin
== o_default
)
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
);
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
);
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
);
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. */
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
);
2300 char *value
= recursively_expand_for_file (v
, file
, NULL
);
2303 if (strcmp (v
->name
, "Path") == 0 ||
2304 strcmp (v
->name
, "PATH") == 0)
2305 convert_Path_to_windows32 (value
, ';');
2307 *result
++ = xstrdup (concat (3, v
->name
, "=", value
));
2313 if (strcmp (v
->name
, "Path") == 0 ||
2314 strcmp (v
->name
, "PATH") == 0)
2315 convert_Path_to_windows32 (v
->value
, ';');
2317 *result
++ = xstrdup (concat (3, v
->name
, "=", v
->value
));
2321 *result
= xmalloc (100);
2322 sprintf (*result
, "%s=%u", MAKELEVEL_NAME
, makelevel
+ 1);
2325 hash_free (&table
, 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;
2344 /* Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
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;
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
)
2360 if (append
|| !v
->value_length
)
2362 v
->value
= xrealloc (v
->value
, v
->value_alloc_len
);
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
2377 MAKE_STATS_2(v
->reallocs
++);
2380 /* insert the new bits */
2381 if (v
->value_length
!= 0)
2385 v
->value
[v
->value_length
] = ' ';
2386 memcpy (&v
->value
[v
->value_length
+ 1], value
, value_len
+ 1);
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
);
2397 memcpy (v
->value
, value
, value_len
+ 1);
2398 v
->value_length
= new_value_len
;
2399 VARIABLE_CHANGED (v
);
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
,
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
)
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
);
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 */
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
2452 cmd_prefix
= var
->value
[0]=='\0' ? RECIPEPREFIX_DEFAULT
: var
->value
[0];
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. */
2463 shell_result (const char *p
)
2470 install_variable_buffer (&buf
, &len
);
2472 args
[0] = (char *) p
;
2474 variable_buffer_output (func_shell_base (variable_buffer
, args
, 0), "\0", 1);
2475 result
= strdup (variable_buffer
);
2477 restore_variable_buffer (buf
, len
);
2481 /* Given a variable, a value, and a flavor, define the variable.
2482 See the try_variable_definition() function for details on the parameters. */
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
,
2494 enum variable_origin origin
,
2495 enum variable_flavor flavor
,
2497 #endif /* CONFIG_WITH_VALUE_LENGTH */
2500 char *alloc_value
= NULL
;
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
);
2510 assert (value_len
== strlen (value
));
2513 /* Calculate the variable's new value in VALUE. */
2519 /* Should not be possible. */
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 */
2530 p
= alloc_value
= allocated_variable_expand_2 (value
, value_len
, &value_len
);
2533 if (value_len
== ~0U)
2534 value_len
= strlen (value
);
2536 p
= alloc_value
= xstrndup (value
, value_len
);
2539 assert (value
== free_value
);
2540 p
= alloc_value
= free_value
;
2544 #endif /* CONFIG_WITH_VALUE_LENGTH */
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
);
2553 flavor
= f_recursive
;
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
);
2561 #ifndef CONFIG_WITH_VALUE_LENGTH
2562 return v
->special
? set_special_var (v
) : v
;
2563 #else /* CONFIG_WITH_VALUE_LENGTH */
2567 return v
->special
? set_special_var (v
) : v
;
2569 #endif /* CONFIG_WITH_VALUE_LENGTH */
2572 flavor
= f_recursive
;
2575 /* A recursive variable definition "var = value".
2576 The value is used verbatim. */
2579 #ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2583 const enum variable_flavor org_flavor
= flavor
;
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. */
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
)
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
);
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
);
2623 v
= lookup_variable (varname
, varname_len
);
2627 /* There was no old value.
2628 This becomes a normal recursive definition. */
2630 flavor
= f_recursive
;
2634 #ifdef CONFIG_WITH_VALUE_LENGTH
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
);
2646 #else /* !CONFIG_WITH_VALUE_LENGTH */
2648 /* Paste the old and new values together in VALUE. */
2650 unsigned int oldlen
, vallen
;
2656 /* The previous definition of the variable was recursive.
2657 The new value is the unexpanded old and new values. */
2658 flavor
= f_recursive
;
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);
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);
2686 #endif /* !CONFIG_WITH_VALUE_LENGTH */
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
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
))
2716 for (tp
= shellpath
; *tp
; tp
++)
2720 v
= define_variable_loc (varname
, varname_len
,
2721 shellpath
, origin
, flavor
== f_recursive
,
2726 const char *shellbase
, *bslash
;
2727 struct variable
*pathv
= lookup_variable ("PATH", 4);
2732 shellbase
= strrchr (p
, '/');
2733 bslash
= strrchr (p
, '\\');
2734 if (!shellbase
|| bslash
> shellbase
)
2736 if (!shellbase
&& p
[1] == ':')
2743 /* Search for the basename of the shell (with standard
2744 executable extensions) along the $PATH. */
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
;
2752 if (__dosexec_find_on_path (shellbase
, fake_env
, shellpath
))
2756 for (tp
= shellpath
; *tp
; tp
++)
2760 v
= define_variable_loc (varname
, varname_len
,
2762 flavor
== f_recursive
, flocp
);
2765 v
= lookup_variable (varname
, varname_len
);
2771 #endif /* __MSDOS__ */
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 */,
2789 origin
, flavor
== f_recursive
,
2791 ? current_variable_set_list
->set
2794 no_default_sh_exe
= 0;
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 */,
2808 origin
, flavor
== f_recursive
,
2810 ? current_variable_set_list
->set
2813 no_default_sh_exe
= 0;
2816 v
= lookup_variable (varname
, varname_len
);
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
,
2834 origin
, flavor
== f_recursive
,
2835 #ifdef CONFIG_WITH_LOCAL_VARIABLES
2836 (target_var
|| origin
== o_local
2840 ? current_variable_set_list
->set
: NULL
),
2843 v
->conditional
= conditional
;
2845 #ifndef CONFIG_WITH_VALUE_LENGTH
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.
2870 parse_variable_definition (const char *p
, struct variable
*var
)
2873 const char *e
= NULL
;
2875 /** @todo merge 4.2.1: parse_variable_definition does more now */
2877 var
->name
= (char *)p
;
2884 /* If we find a comment or EOS, it's not a variable definition. */
2885 if (STOP_SET (c
, MAP_COMMENT
|MAP_NUL
))
2890 /* This begins a variable expansion reference. Make sure we don't
2891 treat chars inside the reference as assignment tokens. */
2903 /* '$$' or '$X'. Either way, nothing special to do here. */
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)
2921 /* If we find whitespace skip it, and remember we found it. */
2936 var
->flavor
= f_recursive
;
2942 /* Match assignment variants (:=, +=, ?=, !=) */
2948 var
->flavor
= f_simple
;
2951 var
->flavor
= f_append
;
2953 #ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2955 var
->flavor
= f_prepend
;
2959 var
->flavor
= f_conditional
;
2962 var
->flavor
= f_shell
;
2965 /* If we skipped whitespace, non-assignments means no var. */
2969 /* Might be assignment, or might be $= or #=. Check. */
2978 /* Check for POSIX ::= syntax */
2981 /* A colon other than :=/::= is not a variable defn. */
2982 if (*p
!= ':' || p
[1] != '=')
2985 /* POSIX allows ::= to be the same as GNU make's := */
2986 var
->flavor
= f_simple
;
2993 /* If we skipped whitespace, non-assignments means no var. */
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;
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
3017 assign_variable_definition (struct variable
*v
, const char *line
IF_WITH_VALUE_LENGTH_PARAM(char *eos
))
3019 #ifndef CONFIG_WITH_VALUE_LENGTH
3023 if (!parse_variable_definition (line
, v
))
3026 #ifdef CONFIG_WITH_VALUE_LENGTH
3029 v
->value_length
= eos
- v
->value
;
3030 assert (strchr (v
->value
, '\0') == eos
);
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"));
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
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
)
3069 struct variable
*vp
;
3072 v
.fileinfo
= *flocp
;
3074 v
.fileinfo
.filenm
= 0;
3076 #ifndef CONFIG_WITH_VALUE_LENGTH
3077 if (!assign_variable_definition (&v
, line
))
3080 vp
= do_variable_definition (flocp
, v
.name
, v
.value
,
3081 origin
, v
.flavor
, target_var
);
3083 if (!assign_variable_definition (&v
, line
, eos
))
3086 vp
= do_variable_definition_2 (flocp
, v
.name
, v
.value
, v
.value_length
,
3087 0, NULL
, origin
, v
.flavor
, target_var
);
3090 #ifndef CONFIG_WITH_STRCACHE2
3093 free ((char *)v
.name
);
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
;
3103 #ifdef CONFIG_WITH_COMPILER
3104 static unsigned long var_stats_expandprogs
, var_stats_evalprogs
;
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
;
3114 /* Print information for variable V, prefixing it with PREFIX. */
3117 print_variable (const void *item
, void *arg
)
3119 const struct variable
*v
= item
;
3120 const char *prefix
= arg
;
3123 const struct variable
*alias
= v
;
3124 RESOLVE_ALIAS_VARIABLE(v
);
3130 origin
= _("automatic");
3133 origin
= _("default");
3136 origin
= _("environment");
3139 origin
= _("makefile");
3141 case o_env_override
:
3142 origin
= _("environment under -e");
3145 origin
= _("command line");
3148 origin
= _("'override' directive");
3150 #ifdef CONFIG_WITH_LOCAL_VARIABLES
3152 origin
= _("`local' directive");
3159 fputs ("# ", stdout
);
3160 fputs (origin
, stdout
);
3162 fputs (" private", stdout
);
3164 if (v
->fileinfo
.filenm
)
3165 printf (_(" (from '%s', line %lu)"),
3166 v
->fileinfo
.filenm
, v
->fileinfo
.lineno
+ v
->fileinfo
.offset
);
3168 if (alias
->fileinfo
.filenm
)
3169 printf (_(" (from '%s', line %lu)"),
3170 alias
->fileinfo
.filenm
, alias
->fileinfo
.lineno
);
3172 fputs (" aliased", stdout
);
3174 printf (_(", alias for '%s'"), v
->name
);
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
);
3183 printf (_(", %u evalvals"), v
->evalval_count
);
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
++;
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
;
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 */
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
);
3248 printf ("define %s\n%s\nendef\n", alias
->name
, v
->value
);
3254 #ifndef KMK /** @todo language feature for aliases */
3255 printf ("%s %s= ", v
->name
, v
->recursive
? v
->append
? "+" : "" : ":");
3257 printf ("%s %s= ", alias
->name
, v
->recursive
? v
->append
? "+" : "" : ":");
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
);
3268 /* Double up dollar signs. */
3269 for (p
= v
->value
; *p
!= '\0'; ++p
)
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
);
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). */
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;
3313 #ifdef CONFIG_WITH_COMPILER
3314 var_stats_expandprogs
= var_stats_evalprogs
= 0;
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;
3323 hash_map_arg (&set
->table
, (pauto
? print_auto_variable
: print_variable
),
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"),
3344 (unsigned int)((100.0 * var_stats_changed
) / set
->table
.ht_fill
),
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
);
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"),
3368 (unsigned int)((100.0 * var_stats_expanded
) / set
->table
.ht_fill
),
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
));
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. */
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
)
3405 printf ("\n%s :\n", p
->target
);
3406 print_variable (&p
->variable
, (void *)"# ");
3410 puts (_("\n# No pattern-specific variable values."));
3412 printf (_("\n# %u pattern-specific variable values"), rules
);
3415 #ifdef CONFIG_WITH_STRCACHE2
3416 strcache2_print_stats (&variable_strcache
, "# ");
3420 #ifdef CONFIG_WITH_PRINT_STATS_SWITCH
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
);
3430 /* Print all the local variables of FILE. */
3433 print_file_variables (const struct file
*file
)
3435 if (file
->variables
!= 0)
3436 print_variable_set (file
->variables
->set
, "# ", 1);
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
);
3452 hash_map_arg (&file
->variables
->set
->table
, print_noauto_variable
, t
);
3458 sync_Path_environment (void)
3460 char *path
= allocated_variable_expand ("$(PATH)");
3461 static char *environ_path
= NULL
;
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
);