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