VirtualBox

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

Last change on this file since 2549 was 2549, checked in by bird, 14 years ago

kmk: hacking on a new kmk/kBuild language extension...

  • Property svn:eol-style set to native
File size: 82.0 KB
Line 
1/* Internals of variables for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
4Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 3 of the License, or (at your option) any later
10version.
11
12GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License along with
17this 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
45/* Chain of all pattern-specific variables. */
46
47static struct pattern_var *pattern_vars;
48
49/* Pointer to last struct in the chain, so we can add onto the end. */
50
51static struct pattern_var *last_pattern_var;
52
53/* Create a new pattern-specific variable struct. */
54
55struct pattern_var *
56create_pattern_var (const char *target, const char *suffix)
57{
58 register struct pattern_var *p = xmalloc (sizeof (struct pattern_var));
59
60 if (last_pattern_var != 0)
61 last_pattern_var->next = p;
62 else
63 pattern_vars = p;
64 last_pattern_var = p;
65 p->next = 0;
66
67 p->target = target;
68 p->len = strlen (target);
69 p->suffix = suffix + 1;
70
71 return p;
72}
73
74/* Look up a target in the pattern-specific variable list. */
75
76static struct pattern_var *
77lookup_pattern_var (struct pattern_var *start, const char *target)
78{
79 struct pattern_var *p;
80 unsigned int targlen = strlen(target);
81
82 for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
83 {
84 const char *stem;
85 unsigned int stemlen;
86
87 if (p->len > targlen)
88 /* It can't possibly match. */
89 continue;
90
91 /* From the lengths of the filename and the pattern parts,
92 find the stem: the part of the filename that matches the %. */
93 stem = target + (p->suffix - p->target - 1);
94 stemlen = targlen - p->len + 1;
95
96 /* Compare the text in the pattern before the stem, if any. */
97 if (stem > target && !strneq (p->target, target, stem - target))
98 continue;
99
100 /* Compare the text in the pattern after the stem, if any.
101 We could test simply using streq, but this way we compare the
102 first two characters immediately. This saves time in the very
103 common case where the first character matches because it is a
104 period. */
105 if (*p->suffix == stem[stemlen]
106 && (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
107 break;
108 }
109
110 return p;
111}
112
113
114#ifdef CONFIG_WITH_STRCACHE2
115struct strcache2 variable_strcache;
116#endif
117
118/* Hash table of all global variable definitions. */
119
120#ifndef CONFIG_WITH_STRCACHE2
121static unsigned long
122variable_hash_1 (const void *keyv)
123{
124 struct variable const *key = (struct variable const *) keyv;
125 return_STRING_N_HASH_1 (key->name, key->length);
126}
127
128static unsigned long
129variable_hash_2 (const void *keyv)
130{
131 struct variable const *key = (struct variable const *) keyv;
132 return_STRING_N_HASH_2 (key->name, key->length);
133}
134
135static int
136variable_hash_cmp (const void *xv, const void *yv)
137{
138 struct variable const *x = (struct variable const *) xv;
139 struct variable const *y = (struct variable const *) yv;
140 int result = x->length - y->length;
141 if (result)
142 return result;
143
144 return_STRING_N_COMPARE (x->name, y->name, x->length);
145}
146#endif /* !CONFIG_WITH_STRCACHE2 */
147
148#ifndef VARIABLE_BUCKETS
149# ifdef KMK /* Move to Makefile.kmk? (insanely high, but wtf, it gets the collitions down) */
150# define VARIABLE_BUCKETS 65535
151# else /*!KMK*/
152#define VARIABLE_BUCKETS 523
153# endif /*!KMK*/
154#endif
155#ifndef PERFILE_VARIABLE_BUCKETS
156# ifdef KMK /* Move to Makefile.kmk? */
157# define PERFILE_VARIABLE_BUCKETS 127
158# else
159#define PERFILE_VARIABLE_BUCKETS 23
160# endif
161#endif
162#ifndef SMALL_SCOPE_VARIABLE_BUCKETS
163# ifdef KMK /* Move to Makefile.kmk? */
164# define SMALL_SCOPE_VARIABLE_BUCKETS 63
165# else
166#define SMALL_SCOPE_VARIABLE_BUCKETS 13
167# endif
168#endif
169
170static struct variable_set global_variable_set;
171static struct variable_set_list global_setlist
172 = { 0, &global_variable_set };
173struct variable_set_list *current_variable_set_list = &global_setlist;
174
175
176/* Implement variables. */
177
178void
179init_hash_global_variable_set (void)
180{
181#ifndef CONFIG_WITH_STRCACHE2
182 hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
183 variable_hash_1, variable_hash_2, variable_hash_cmp);
184#else /* CONFIG_WITH_STRCACHE2 */
185 strcache2_init (&variable_strcache, "variable", 65536, 0, 0, 0);
186 hash_init_strcached (&global_variable_set.table, VARIABLE_BUCKETS,
187 &variable_strcache, offsetof (struct variable, name));
188#endif /* CONFIG_WITH_STRCACHE2 */
189}
190
191/* Define variable named NAME with value VALUE in SET. VALUE is copied.
192 LENGTH is the length of NAME, which does not need to be null-terminated.
193 ORIGIN specifies the origin of the variable (makefile, command line
194 or environment).
195 If RECURSIVE is nonzero a flag is set in the variable saying
196 that it should be recursively re-expanded. */
197
198#ifdef CONFIG_WITH_VALUE_LENGTH
199struct variable *
200define_variable_in_set (const char *name, unsigned int length,
201 const char *value, unsigned int value_len,
202 int duplicate_value, enum variable_origin origin,
203 int recursive, struct variable_set *set,
204 const struct floc *flocp)
205#else
206struct variable *
207define_variable_in_set (const char *name, unsigned int length,
208 const char *value, enum variable_origin origin,
209 int recursive, struct variable_set *set,
210 const struct floc *flocp)
211#endif
212{
213 struct variable *v;
214 struct variable **var_slot;
215 struct variable var_key;
216
217#ifndef KMK
218 if (set == NULL)
219 set = &global_variable_set;
220#else
221 if (set == NULL)
222 {
223 /* underscore prefixed variables are automatically local in
224 kBuild-define-* scopes. They also get a global definition with
225 the current scope prefix. */
226 if (g_pTopKbDef && length > 0 && name[0] == '_')
227 {
228 char *prefixed_nm;
229 unsigned int prefixed_nm_len;
230
231 set = get_top_kbuild_variable_set();
232 v = define_variable_in_set(name, length, value, value_len,
233 1 /* duplicate_value */,
234 origin, recursive, set, flocp);
235
236 prefixed_nm_len = length;
237 prefixed_nm = kbuild_prefix_variable(name, &prefixed_nm_len);
238 define_variable_in_set(prefixed_nm, prefixed_nm_len,
239 value, value_len, duplicate_value,
240 origin, recursive, &global_variable_set,
241 flocp);
242 free(prefixed_nm);
243 return v;
244 }
245 set = &global_variable_set;
246 }
247#endif
248
249#ifndef CONFIG_WITH_STRCACHE2
250 var_key.name = (char *) name;
251 var_key.length = length;
252 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
253
254 if (env_overrides && origin == o_env)
255 origin = o_env_override;
256
257 v = *var_slot;
258#else /* CONFIG_WITH_STRCACHE2 */
259 var_key.name = name = strcache2_add (&variable_strcache, name, length);
260 var_key.length = length;
261 if ( set != &global_variable_set
262 || !(v = strcache2_get_user_val (&variable_strcache, var_key.name)))
263 {
264 var_slot = (struct variable **) hash_find_slot_strcached (&set->table, &var_key);
265 v = *var_slot;
266 }
267 else
268 {
269 assert (!v || (v->name == name && !HASH_VACANT (v)));
270 var_slot = 0;
271 }
272#endif /* CONFIG_WITH_STRCACHE2 */
273 if (! HASH_VACANT (v))
274 {
275 if (env_overrides && v->origin == o_env)
276 /* V came from in the environment. Since it was defined
277 before the switches were parsed, it wasn't affected by -e. */
278 v->origin = o_env_override;
279
280 /* A variable of this name is already defined.
281 If the old definition is from a stronger source
282 than this one, don't redefine it. */
283 if ((int) origin >= (int) v->origin)
284 {
285#ifdef CONFIG_WITH_VALUE_LENGTH
286 if (value_len == ~0U)
287 value_len = strlen (value);
288 else
289 assert (value_len == strlen (value));
290 if (!duplicate_value || duplicate_value == -1)
291 {
292# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
293 if (v->value != 0 && !v->rdonly_val)
294 free (v->value);
295 v->rdonly_val = duplicate_value == -1;
296 v->value = (char *) value;
297 v->value_alloc_len = 0;
298# else
299 if (v->value != 0)
300 free (v->value);
301 v->value = (char *) value;
302 v->value_alloc_len = value_len + 1;
303# endif
304 }
305 else
306 {
307 if (v->value_alloc_len <= value_len)
308 {
309# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
310 if (v->rdonly_val)
311 v->rdonly_val = 0;
312 else
313# endif
314 free (v->value);
315 v->value_alloc_len = VAR_ALIGN_VALUE_ALLOC (value_len + 1);
316 v->value = xmalloc (v->value_alloc_len);
317 MAKE_STATS_2(v->reallocs++);
318 }
319 memcpy (v->value, value, value_len + 1);
320 }
321 v->value_length = value_len;
322#else /* !CONFIG_WITH_VALUE_LENGTH */
323 if (v->value != 0)
324 free (v->value);
325 v->value = xstrdup (value);
326#endif /* !CONFIG_WITH_VALUE_LENGTH */
327 if (flocp != 0)
328 v->fileinfo = *flocp;
329 else
330 v->fileinfo.filenm = 0;
331 v->origin = origin;
332 v->recursive = recursive;
333 MAKE_STATS_2(v->changes++);
334 }
335 return v;
336 }
337
338 /* Create a new variable definition and add it to the hash table. */
339
340#ifndef CONFIG_WITH_ALLOC_CACHES
341 v = xmalloc (sizeof (struct variable));
342#else
343 v = alloccache_alloc (&variable_cache);
344#endif
345#ifndef CONFIG_WITH_STRCACHE2
346 v->name = savestring (name, length);
347#else
348 v->name = name; /* already cached. */
349#endif
350 v->length = length;
351 hash_insert_at (&set->table, v, var_slot);
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 v->value_length = value_len;
358 if (!duplicate_value || duplicate_value == -1)
359 {
360# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
361 v->rdonly_val = duplicate_value == -1;
362 v->value_alloc_len = v->rdonly_val ? 0 : value_len + 1;
363# endif
364 v->value = (char *)value;
365 }
366 else
367 {
368# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
369 v->rdonly_val = 0;
370# endif
371 v->value_alloc_len = VAR_ALIGN_VALUE_ALLOC (value_len + 1);
372 v->value = xmalloc (v->value_alloc_len);
373 memcpy (v->value, value, value_len + 1);
374 }
375#else /* !CONFIG_WITH_VALUE_LENGTH */
376 v->value = xstrdup (value);
377#endif /* !CONFIG_WITH_VALUE_LENGTH */
378 if (flocp != 0)
379 v->fileinfo = *flocp;
380 else
381 v->fileinfo.filenm = 0;
382 v->origin = origin;
383 v->recursive = recursive;
384 v->special = 0;
385 v->expanding = 0;
386 v->exp_count = 0;
387 v->per_target = 0;
388 v->append = 0;
389 v->export = v_default;
390 MAKE_STATS_2(v->changes = 0);
391 MAKE_STATS_2(v->reallocs = 0);
392
393 v->exportable = 1;
394 if (*name != '_' && (*name < 'A' || *name > 'Z')
395 && (*name < 'a' || *name > 'z'))
396 v->exportable = 0;
397 else
398 {
399 for (++name; *name != '\0'; ++name)
400 if (*name != '_' && (*name < 'a' || *name > 'z')
401 && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
402 break;
403
404 if (*name != '\0')
405 v->exportable = 0;
406 }
407
408#ifdef CONFIG_WITH_STRCACHE2
409 /* If it's the global set, remember the variable. */
410 if (set == &global_variable_set)
411 strcache2_set_user_val (&variable_strcache, v->name, v);
412#endif
413 return v;
414}
415
416
417/* If the variable passed in is "special", handle its special nature.
418 Currently there are two such variables, both used for introspection:
419 .VARIABLES expands to a list of all the variables defined in this instance
420 of make.
421 .TARGETS expands to a list of all the targets defined in this
422 instance of make.
423 Returns the variable reference passed in. */
424
425#define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
426
427static struct variable *
428lookup_special_var (struct variable *var)
429{
430 static unsigned long last_var_count = 0;
431
432
433 /* This one actually turns out to be very hard, due to the way the parser
434 records targets. The way it works is that target information is collected
435 internally until make knows the target is completely specified. It unitl
436 it sees that some new construct (a new target or variable) is defined that
437 it knows the previous one is done. In short, this means that if you do
438 this:
439
440 all:
441
442 TARGS := $(.TARGETS)
443
444 then $(TARGS) won't contain "all", because it's not until after the
445 variable is created that the previous target is completed.
446
447 Changing this would be a major pain. I think a less complex way to do it
448 would be to pre-define the target files as soon as the first line is
449 parsed, then come back and do the rest of the definition as now. That
450 would allow $(.TARGETS) to be correct without a major change to the way
451 the parser works.
452
453 if (streq (var->name, ".TARGETS"))
454 var->value = build_target_list (var->value);
455 else
456 */
457
458 if (streq (var->name, ".VARIABLES")
459 && global_variable_set.table.ht_fill != last_var_count)
460 {
461#ifndef CONFIG_WITH_VALUE_LENGTH
462 unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
463#else
464 unsigned long max = EXPANSION_INCREMENT (var->value_length);
465#endif
466 unsigned long len;
467 char *p;
468 struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
469 struct variable **end = &vp[global_variable_set.table.ht_size];
470
471 /* Make sure we have at least MAX bytes in the allocated buffer. */
472 var->value = xrealloc (var->value, max);
473 MAKE_STATS_2(var->reallocs++);
474
475 /* Walk through the hash of variables, constructing a list of names. */
476 p = var->value;
477 len = 0;
478 for (; vp < end; ++vp)
479 if (!HASH_VACANT (*vp))
480 {
481 struct variable *v = *vp;
482 int l = v->length;
483
484 len += l + 1;
485 if (len > max)
486 {
487 unsigned long off = p - var->value;
488
489 max += EXPANSION_INCREMENT (l + 1);
490 var->value = xrealloc (var->value, max);
491 p = &var->value[off];
492 MAKE_STATS_2(var->reallocs++);
493 }
494
495 memcpy (p, v->name, l);
496 p += l;
497 *(p++) = ' ';
498 }
499 *(p-1) = '\0';
500#ifdef CONFIG_WITH_VALUE_LENGTH
501 var->value_length = p - var->value - 1;
502 var->value_alloc_len = max;
503#endif
504
505 /* Remember how many variables are in our current count. Since we never
506 remove variables from the list, this is a reliable way to know whether
507 the list is up to date or needs to be recomputed. */
508
509 last_var_count = global_variable_set.table.ht_fill;
510 }
511
512 return var;
513}
514
515
516
517#ifdef KMK /* bird: speed */
518MY_INLINE struct variable *
519lookup_cached_variable (const char *name)
520{
521 const struct variable_set_list *setlist = current_variable_set_list;
522 struct hash_table *ht;
523 unsigned int hash_1;
524 unsigned int hash_2;
525 unsigned int idx;
526 struct variable *v;
527
528 /* first set, first entry, both unrolled. */
529
530 if (setlist->set == &global_variable_set)
531 {
532 v = (struct variable *) strcache2_get_user_val (&variable_strcache, name);
533 if (MY_PREDICT_TRUE (v))
534 return MY_PREDICT_FALSE (v->special) ? lookup_special_var (v) : v;
535 assert (setlist->next == 0);
536 return 0;
537 }
538
539 hash_1 = strcache2_calc_ptr_hash (&variable_strcache, name);
540 ht = &setlist->set->table;
541 MAKE_STATS (ht->ht_lookups++);
542 idx = hash_1 & (ht->ht_size - 1);
543 v = ht->ht_vec[idx];
544 if (v != 0)
545 {
546 if ( (void *)v != hash_deleted_item
547 && v->name == name)
548 return MY_PREDICT_FALSE (v->special) ? lookup_special_var (v) : v;
549
550 /* the rest of the loop */
551 hash_2 = strcache2_get_hash (&variable_strcache, name) | 1;
552 for (;;)
553 {
554 idx += hash_2;
555 idx &= (ht->ht_size - 1);
556 v = (struct variable *) ht->ht_vec[idx];
557 MAKE_STATS (ht->ht_collisions++); /* there are hardly any deletions, so don't bother with not counting deleted clashes. */
558
559 if (v == 0)
560 break;
561 if ( (void *)v != hash_deleted_item
562 && v->name == name)
563 return MY_PREDICT_FALSE (v->special) ? lookup_special_var (v) : v;
564 } /* inner collision loop */
565 }
566 else
567 hash_2 = strcache2_get_hash (&variable_strcache, name) | 1;
568
569
570 /* The other sets, if any. */
571
572 setlist = setlist->next;
573 while (setlist)
574 {
575 if (setlist->set == &global_variable_set)
576 {
577 v = (struct variable *) strcache2_get_user_val (&variable_strcache, name);
578 if (MY_PREDICT_TRUE (v))
579 return MY_PREDICT_FALSE (v->special) ? lookup_special_var (v) : v;
580 assert (setlist->next == 0);
581 return 0;
582 }
583
584 /* first iteration unrolled */
585 ht = &setlist->set->table;
586 MAKE_STATS (ht->ht_lookups++);
587 idx = hash_1 & (ht->ht_size - 1);
588 v = ht->ht_vec[idx];
589 if (v != 0)
590 {
591 if ( (void *)v != hash_deleted_item
592 && v->name == name)
593 return MY_PREDICT_FALSE (v->special) ? lookup_special_var (v) : v;
594
595 /* the rest of the loop */
596 for (;;)
597 {
598 idx += hash_2;
599 idx &= (ht->ht_size - 1);
600 v = (struct variable *) ht->ht_vec[idx];
601 MAKE_STATS (ht->ht_collisions++); /* see reason above */
602
603 if (v == 0)
604 break;
605 if ( (void *)v != hash_deleted_item
606 && v->name == name)
607 return MY_PREDICT_FALSE (v->special) ? lookup_special_var (v) : v;
608 } /* inner collision loop */
609 }
610
611 /* next */
612 setlist = setlist->next;
613 }
614
615 return 0;
616}
617
618# ifndef NDEBUG
619struct variable *
620lookup_variable_for_assert (const char *name, unsigned int length)
621{
622 const struct variable_set_list *setlist;
623 struct variable var_key;
624 var_key.name = name;
625 var_key.length = length;
626
627 for (setlist = current_variable_set_list;
628 setlist != 0; setlist = setlist->next)
629 {
630 struct variable *v;
631 v = (struct variable *) hash_find_item_strcached (&setlist->set->table, &var_key);
632 if (v)
633 return MY_PREDICT_FALSE (v->special) ? lookup_special_var (v) : v;
634 }
635 return 0;
636}
637# endif /* !NDEBUG */
638#endif /* KMK - need for speed */
639
640/* Lookup a variable whose name is a string starting at NAME
641 and with LENGTH chars. NAME need not be null-terminated.
642 Returns address of the `struct variable' containing all info
643 on the variable, or nil if no such variable is defined. */
644
645struct variable *
646lookup_variable (const char *name, unsigned int length)
647{
648#ifndef KMK
649 const struct variable_set_list *setlist;
650 struct variable var_key;
651#else /* KMK */
652 struct variable *v;
653#endif /* KMK */
654#ifdef CONFIG_WITH_STRCACHE2
655 const char *cached_name;
656
657 /* lookup the name in the string case, if it's not there it won't
658 be in any of the sets either. */
659 cached_name = strcache2_lookup (&variable_strcache, name, length);
660 if (!cached_name)
661 return NULL;
662 name = cached_name;
663#endif /* CONFIG_WITH_STRCACHE2 */
664#ifndef KMK
665
666 var_key.name = (char *) name;
667 var_key.length = length;
668
669 for (setlist = current_variable_set_list;
670 setlist != 0; setlist = setlist->next)
671 {
672 const struct variable_set *set = setlist->set;
673 struct variable *v;
674
675# ifndef CONFIG_WITH_STRCACHE2
676 v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
677# else /* CONFIG_WITH_STRCACHE2 */
678 v = (struct variable *) hash_find_item_strcached ((struct hash_table *) &set->table, &var_key);
679# endif /* CONFIG_WITH_STRCACHE2 */
680 if (v)
681 return v->special ? lookup_special_var (v) : v;
682 }
683
684#else /* KMK - need for speed */
685
686 v = lookup_cached_variable (name);
687 assert (lookup_variable_for_assert(name, length) == v);
688#ifdef VMS
689 if (v)
690#endif
691 return v;
692#endif /* KMK - need for speed */
693#ifdef VMS
694 /* since we don't read envp[] on startup, try to get the
695 variable via getenv() here. */
696 {
697 char *vname = alloca (length + 1);
698 char *value;
699 strncpy (vname, name, length);
700 vname[length] = 0;
701 value = getenv (vname);
702 if (value != 0)
703 {
704 char *sptr;
705 int scnt;
706
707 sptr = value;
708 scnt = 0;
709
710 while ((sptr = strchr (sptr, '$')))
711 {
712 scnt++;
713 sptr++;
714 }
715
716 if (scnt > 0)
717 {
718 char *nvalue;
719 char *nptr;
720
721 nvalue = alloca (strlen (value) + scnt + 1);
722 sptr = value;
723 nptr = nvalue;
724
725 while (*sptr)
726 {
727 if (*sptr == '$')
728 {
729 *nptr++ = '$';
730 *nptr++ = '$';
731 }
732 else
733 {
734 *nptr++ = *sptr;
735 }
736 sptr++;
737 }
738
739 *nptr = '\0';
740 return define_variable (vname, length, nvalue, o_env, 1);
741
742 }
743
744 return define_variable (vname, length, value, o_env, 1);
745 }
746 }
747#endif /* VMS */
748
749#if !defined (KMK) || defined(VMS)
750 return 0;
751#endif
752}
753
754
755/* Lookup a variable whose name is a string starting at NAME
756 and with LENGTH chars in set SET. NAME need not be null-terminated.
757 Returns address of the `struct variable' containing all info
758 on the variable, or nil if no such variable is defined. */
759
760struct variable *
761lookup_variable_in_set (const char *name, unsigned int length,
762 const struct variable_set *set)
763{
764 struct variable var_key;
765#ifndef CONFIG_WITH_STRCACHE2
766 var_key.name = (char *) name;
767 var_key.length = length;
768
769 return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
770#else /* CONFIG_WITH_STRCACHE2 */
771 const char *cached_name;
772
773 /* lookup the name in the string case, if it's not there it won't
774 be in any of the sets either. Optimize lookups in the global set. */
775 cached_name = strcache2_lookup(&variable_strcache, name, length);
776 if (!cached_name)
777 return NULL;
778
779 if (set == &global_variable_set)
780 {
781 struct variable *v;
782 v = strcache2_get_user_val (&variable_strcache, cached_name);
783 assert (!v || v->name == cached_name);
784 return v;
785 }
786
787 var_key.name = cached_name;
788 var_key.length = length;
789
790 return (struct variable *) hash_find_item_strcached (
791 (struct hash_table *) &set->table, &var_key);
792#endif /* CONFIG_WITH_STRCACHE2 */
793}
794
795
796/* Initialize FILE's variable set list. If FILE already has a variable set
797 list, the topmost variable set is left intact, but the the rest of the
798 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
799 rule, then we will use the "root" double-colon target's variable set as the
800 parent of FILE's variable set.
801
802 If we're READING a makefile, don't do the pattern variable search now,
803 since the pattern variable might not have been defined yet. */
804
805void
806initialize_file_variables (struct file *file, int reading)
807{
808 struct variable_set_list *l = file->variables;
809
810 if (l == 0)
811 {
812#ifndef CONFIG_WITH_ALLOC_CACHES
813 l = (struct variable_set_list *)
814 xmalloc (sizeof (struct variable_set_list));
815 l->set = xmalloc (sizeof (struct variable_set));
816#else /* CONFIG_WITH_ALLOC_CACHES */
817 l = (struct variable_set_list *)
818 alloccache_alloc (&variable_set_list_cache);
819 l->set = (struct variable_set *)
820 alloccache_alloc (&variable_set_cache);
821#endif /* CONFIG_WITH_ALLOC_CACHES */
822#ifndef CONFIG_WITH_STRCACHE2
823 hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
824 variable_hash_1, variable_hash_2, variable_hash_cmp);
825#else /* CONFIG_WITH_STRCACHE2 */
826 hash_init_strcached (&l->set->table, PERFILE_VARIABLE_BUCKETS,
827 &variable_strcache, offsetof (struct variable, name));
828#endif /* CONFIG_WITH_STRCACHE2 */
829 file->variables = l;
830 }
831
832 /* If this is a double-colon, then our "parent" is the "root" target for
833 this double-colon rule. Since that rule has the same name, parent,
834 etc. we can just use its variables as the "next" for ours. */
835
836 if (file->double_colon && file->double_colon != file)
837 {
838 initialize_file_variables (file->double_colon, reading);
839 l->next = file->double_colon->variables;
840 return;
841 }
842
843 if (file->parent == 0)
844 l->next = &global_setlist;
845 else
846 {
847 initialize_file_variables (file->parent, reading);
848 l->next = file->parent->variables;
849 }
850
851 /* If we're not reading makefiles and we haven't looked yet, see if
852 we can find pattern variables for this target. */
853
854 if (!reading && !file->pat_searched)
855 {
856 struct pattern_var *p;
857
858 p = lookup_pattern_var (0, file->name);
859 if (p != 0)
860 {
861 struct variable_set_list *global = current_variable_set_list;
862
863 /* We found at least one. Set up a new variable set to accumulate
864 all the pattern variables that match this target. */
865
866 file->pat_variables = create_new_variable_set ();
867 current_variable_set_list = file->pat_variables;
868
869 do
870 {
871 /* We found one, so insert it into the set. */
872
873 struct variable *v;
874
875 if (p->variable.flavor == f_simple)
876 {
877 v = define_variable_loc (
878 p->variable.name, strlen (p->variable.name),
879 p->variable.value, p->variable.origin,
880 0, &p->variable.fileinfo);
881
882 v->flavor = f_simple;
883 }
884 else
885 {
886#ifndef CONFIG_WITH_VALUE_LENGTH
887 v = do_variable_definition (
888 &p->variable.fileinfo, p->variable.name,
889 p->variable.value, p->variable.origin,
890 p->variable.flavor, 1);
891#else
892 v = do_variable_definition_2 (
893 &p->variable.fileinfo, p->variable.name,
894 p->variable.value, p->variable.value_length, 0, 0,
895 p->variable.origin, p->variable.flavor, 1);
896#endif
897 }
898
899 /* Also mark it as a per-target and copy export status. */
900 v->per_target = p->variable.per_target;
901 v->export = p->variable.export;
902 }
903 while ((p = lookup_pattern_var (p, file->name)) != 0);
904
905 current_variable_set_list = global;
906 }
907 file->pat_searched = 1;
908 }
909
910 /* If we have a pattern variable match, set it up. */
911
912 if (file->pat_variables != 0)
913 {
914 file->pat_variables->next = l->next;
915 l->next = file->pat_variables;
916 }
917}
918
919
920/* Pop the top set off the current variable set list,
921 and free all its storage. */
922
923struct variable_set_list *
924create_new_variable_set (void)
925{
926 register struct variable_set_list *setlist;
927 register struct variable_set *set;
928
929#ifndef CONFIG_WITH_ALLOC_CACHES
930 set = xmalloc (sizeof (struct variable_set));
931#else
932 set = (struct variable_set *) alloccache_alloc (&variable_set_cache);
933#endif
934#ifndef CONFIG_WITH_STRCACHE2
935 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
936 variable_hash_1, variable_hash_2, variable_hash_cmp);
937#else /* CONFIG_WITH_STRCACHE2 */
938 hash_init_strcached (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
939 &variable_strcache, offsetof (struct variable, name));
940#endif /* CONFIG_WITH_STRCACHE2 */
941
942#ifndef CONFIG_WITH_ALLOC_CACHES
943 setlist = (struct variable_set_list *)
944 xmalloc (sizeof (struct variable_set_list));
945#else
946 setlist = (struct variable_set_list *)
947 alloccache_alloc (&variable_set_list_cache);
948#endif
949 setlist->set = set;
950 setlist->next = current_variable_set_list;
951
952 return setlist;
953}
954
955static void
956free_variable_name_and_value (const void *item)
957{
958 struct variable *v = (struct variable *) item;
959#ifndef CONFIG_WITH_STRCACHE2
960 free (v->name);
961#endif
962#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
963 if (!v->rdonly_val)
964#endif
965 free (v->value);
966}
967
968void
969free_variable_set (struct variable_set_list *list)
970{
971 hash_map (&list->set->table, free_variable_name_and_value);
972#ifndef CONFIG_WITH_ALLOC_CACHES
973 hash_free (&list->set->table, 1);
974 free (list->set);
975 free (list);
976#else
977 hash_free_cached (&list->set->table, 1, &variable_cache);
978 alloccache_free (&variable_set_cache, list->set);
979 alloccache_free (&variable_set_list_cache, list);
980#endif
981}
982
983/* Create a new variable set and push it on the current setlist.
984 If we're pushing a global scope (that is, the current scope is the global
985 scope) then we need to "push" it the other way: file variable sets point
986 directly to the global_setlist so we need to replace that with the new one.
987 */
988
989struct variable_set_list *
990push_new_variable_scope (void)
991{
992 current_variable_set_list = create_new_variable_set();
993 if (current_variable_set_list->next == &global_setlist)
994 {
995 /* It was the global, so instead of new -> &global we want to replace
996 &global with the new one and have &global -> new, with current still
997 pointing to &global */
998 struct variable_set *set = current_variable_set_list->set;
999 current_variable_set_list->set = global_setlist.set;
1000 global_setlist.set = set;
1001 current_variable_set_list->next = global_setlist.next;
1002 global_setlist.next = current_variable_set_list;
1003 current_variable_set_list = &global_setlist;
1004 }
1005 return (current_variable_set_list);
1006}
1007
1008void
1009pop_variable_scope (void)
1010{
1011 struct variable_set_list *setlist;
1012 struct variable_set *set;
1013
1014 /* Can't call this if there's no scope to pop! */
1015 assert(current_variable_set_list->next != NULL);
1016
1017 if (current_variable_set_list != &global_setlist)
1018 {
1019 /* We're not pointing to the global setlist, so pop this one. */
1020 setlist = current_variable_set_list;
1021 set = setlist->set;
1022 current_variable_set_list = setlist->next;
1023 }
1024 else
1025 {
1026 /* This set is the one in the global_setlist, but there is another global
1027 set beyond that. We want to copy that set to global_setlist, then
1028 delete what used to be in global_setlist. */
1029 setlist = global_setlist.next;
1030 set = global_setlist.set;
1031 global_setlist.set = setlist->set;
1032 global_setlist.next = setlist->next;
1033 }
1034
1035 /* Free the one we no longer need. */
1036#ifndef CONFIG_WITH_ALLOC_CACHES
1037 free (setlist);
1038 hash_map (&set->table, free_variable_name_and_value);
1039 hash_free (&set->table, 1);
1040 free (set);
1041#else
1042 alloccache_free (&variable_set_list_cache, setlist);
1043 hash_map (&set->table, free_variable_name_and_value);
1044 hash_free_cached (&set->table, 1, &variable_cache);
1045 alloccache_free (&variable_set_cache, set);
1046#endif
1047}
1048
1049
1050/* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
1051
1052static void
1053merge_variable_sets (struct variable_set *to_set,
1054 struct variable_set *from_set)
1055{
1056 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
1057 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
1058
1059 for ( ; from_var_slot < from_var_end; from_var_slot++)
1060 if (! HASH_VACANT (*from_var_slot))
1061 {
1062 struct variable *from_var = *from_var_slot;
1063 struct variable **to_var_slot
1064#ifndef CONFIG_WITH_STRCACHE2
1065 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
1066#else /* CONFIG_WITH_STRCACHE2 */
1067 = (struct variable **) hash_find_slot_strcached (&to_set->table,
1068 *from_var_slot);
1069#endif /* CONFIG_WITH_STRCACHE2 */
1070 if (HASH_VACANT (*to_var_slot))
1071 hash_insert_at (&to_set->table, from_var, to_var_slot);
1072 else
1073 {
1074 /* GKM FIXME: delete in from_set->table */
1075 free (from_var->value);
1076 free (from_var);
1077 }
1078 }
1079}
1080
1081/* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
1082
1083void
1084merge_variable_set_lists (struct variable_set_list **setlist0,
1085 struct variable_set_list *setlist1)
1086{
1087 struct variable_set_list *to = *setlist0;
1088 struct variable_set_list *last0 = 0;
1089
1090 /* If there's nothing to merge, stop now. */
1091 if (!setlist1)
1092 return;
1093
1094 /* This loop relies on the fact that all setlists terminate with the global
1095 setlist (before NULL). If that's not true, arguably we SHOULD die. */
1096 if (to)
1097 while (setlist1 != &global_setlist && to != &global_setlist)
1098 {
1099 struct variable_set_list *from = setlist1;
1100 setlist1 = setlist1->next;
1101
1102 merge_variable_sets (to->set, from->set);
1103
1104 last0 = to;
1105 to = to->next;
1106 }
1107
1108 if (setlist1 != &global_setlist)
1109 {
1110 if (last0 == 0)
1111 *setlist0 = setlist1;
1112 else
1113 last0->next = setlist1;
1114 }
1115}
1116
1117
1118#if defined(KMK) && !defined(WINDOWS32)
1119/* Parses out the next number from the uname release level string. Fast
1120 forwards to the end of the string when encountering some non-conforming
1121 chars. */
1122
1123static unsigned long parse_release_number (const char **ppsz)
1124{
1125 unsigned long ul;
1126 char *psz = (char *)*ppsz;
1127 if (ISDIGIT (*psz))
1128 {
1129 ul = strtoul (psz, &psz, 10);
1130 if (psz != NULL && *psz == '.')
1131 psz++;
1132 else
1133 psz = strchr (*ppsz, '\0');
1134 *ppsz = psz;
1135 }
1136 else
1137 ul = 0;
1138 return ul;
1139}
1140#endif
1141
1142
1143/* Define the automatic variables, and record the addresses
1144 of their structures so we can change their values quickly. */
1145
1146void
1147define_automatic_variables (void)
1148{
1149#if defined(WINDOWS32) || defined(__EMX__)
1150 extern char* default_shell;
1151#else
1152 extern char default_shell[];
1153#endif
1154 register struct variable *v;
1155#ifndef KMK
1156 char buf[200];
1157#else
1158 char buf[1024];
1159 const char *val;
1160 struct variable *envvar1;
1161 struct variable *envvar2;
1162# ifdef WINDOWS32
1163 OSVERSIONINFOEX oix;
1164# else
1165 struct utsname uts;
1166# endif
1167 unsigned long ulMajor = 0, ulMinor = 0, ulPatch = 0, ul4th = 0;
1168#endif
1169
1170 sprintf (buf, "%u", makelevel);
1171 (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
1172
1173 sprintf (buf, "%s%s%s",
1174 version_string,
1175 (remote_description == 0 || remote_description[0] == '\0')
1176 ? "" : "-",
1177 (remote_description == 0 || remote_description[0] == '\0')
1178 ? "" : remote_description);
1179#ifndef KMK
1180 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
1181#else /* KMK */
1182
1183 /* Define KMK_VERSION to indicate kMk. */
1184 (void) define_variable ("KMK_VERSION", 11, buf, o_default, 0);
1185
1186 /* Define KBUILD_VERSION* */
1187 sprintf (buf, "%d", KBUILD_VERSION_MAJOR);
1188 define_variable ("KBUILD_VERSION_MAJOR", sizeof ("KBUILD_VERSION_MAJOR") - 1,
1189 buf, o_default, 0);
1190 sprintf (buf, "%d", KBUILD_VERSION_MINOR);
1191 define_variable ("KBUILD_VERSION_MINOR", sizeof("KBUILD_VERSION_MINOR") - 1,
1192 buf, o_default, 0);
1193 sprintf (buf, "%d", KBUILD_VERSION_PATCH);
1194 define_variable ("KBUILD_VERSION_PATCH", sizeof ("KBUILD_VERSION_PATCH") - 1,
1195 buf, o_default, 0);
1196 sprintf (buf, "%d", KBUILD_SVN_REV);
1197 define_variable ("KBUILD_KMK_REVISION", sizeof ("KBUILD_KMK_REVISION") - 1,
1198 buf, o_default, 0);
1199
1200 sprintf (buf, "%d.%d.%d-r%d", KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR,
1201 KBUILD_VERSION_PATCH, KBUILD_SVN_REV);
1202 define_variable ("KBUILD_VERSION", sizeof ("KBUILD_VERSION") - 1,
1203 buf, o_default, 0);
1204
1205 /* The host defaults. The BUILD_* stuff will be replaced by KBUILD_* soon. */
1206 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST"));
1207 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM"));
1208 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST;
1209 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1210 error (NULL, _("KBUILD_HOST and BUILD_PLATFORM differs, using KBUILD_HOST=%s."), val);
1211 if (!envvar1)
1212 define_variable ("KBUILD_HOST", sizeof ("KBUILD_HOST") - 1,
1213 val, o_default, 0);
1214 if (!envvar2)
1215 define_variable ("BUILD_PLATFORM", sizeof ("BUILD_PLATFORM") - 1,
1216 val, o_default, 0);
1217
1218 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_ARCH"));
1219 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_ARCH"));
1220 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_ARCH;
1221 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1222 error (NULL, _("KBUILD_HOST_ARCH and BUILD_PLATFORM_ARCH differs, using KBUILD_HOST_ARCH=%s."), val);
1223 if (!envvar1)
1224 define_variable ("KBUILD_HOST_ARCH", sizeof ("KBUILD_HOST_ARCH") - 1,
1225 val, o_default, 0);
1226 if (!envvar2)
1227 define_variable ("BUILD_PLATFORM_ARCH", sizeof ("BUILD_PLATFORM_ARCH") - 1,
1228 val, o_default, 0);
1229
1230 envvar1 = lookup_variable (STRING_SIZE_TUPLE ("KBUILD_HOST_CPU"));
1231 envvar2 = lookup_variable (STRING_SIZE_TUPLE ("BUILD_PLATFORM_CPU"));
1232 val = envvar1 ? envvar1->value : envvar2 ? envvar2->value : KBUILD_HOST_CPU;
1233 if (envvar1 && envvar2 && strcmp (envvar1->value, envvar2->value))
1234 error (NULL, _("KBUILD_HOST_CPU and BUILD_PLATFORM_CPU differs, using KBUILD_HOST_CPU=%s."), val);
1235 if (!envvar1)
1236 define_variable ("KBUILD_HOST_CPU", sizeof ("KBUILD_HOST_CPU") - 1,
1237 val, o_default, 0);
1238 if (!envvar2)
1239 define_variable ("BUILD_PLATFORM_CPU", sizeof ("BUILD_PLATFORM_CPU") - 1,
1240 val, o_default, 0);
1241
1242 /* The host kernel version. */
1243#if defined(WINDOWS32)
1244 memset (&oix, '\0', sizeof (oix));
1245 oix.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1246 if (!GetVersionEx ((LPOSVERSIONINFO)&oix))
1247 {
1248 memset (&oix, '\0', sizeof (oix));
1249 oix.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
1250 GetVersionEx ((LPOSVERSIONINFO)&oix);
1251 }
1252 if (oix.dwPlatformId == VER_PLATFORM_WIN32_NT)
1253 {
1254 ulMajor = oix.dwMajorVersion;
1255 ulMinor = oix.dwMinorVersion;
1256 ulPatch = oix.wServicePackMajor;
1257 ul4th = oix.wServicePackMinor;
1258 }
1259 else
1260 {
1261 ulMajor = oix.dwPlatformId == 1 ? 0 /*Win95/98/ME*/
1262 : oix.dwPlatformId == 3 ? 1 /*WinCE*/
1263 : 2; /*??*/
1264 ulMinor = oix.dwMajorVersion;
1265 ulPatch = oix.dwMinorVersion;
1266 ul4th = oix.wServicePackMajor;
1267 }
1268#else
1269 memset (&uts, 0, sizeof(uts));
1270 uname (&uts);
1271 val = uts.release;
1272 ulMajor = parse_release_number (&val);
1273 ulMinor = parse_release_number (&val);
1274 ulPatch = parse_release_number (&val);
1275 ul4th = parse_release_number (&val);
1276#endif
1277
1278 sprintf (buf, "%lu.%lu.%lu.%lu", ulMajor, ulMinor, ulPatch, ul4th);
1279 define_variable ("KBUILD_HOST_VERSION", sizeof ("KBUILD_HOST_VERSION") - 1,
1280 buf, o_default, 0);
1281
1282 sprintf (buf, "%lu", ulMajor);
1283 define_variable ("KBUILD_HOST_VERSION_MAJOR", sizeof ("KBUILD_HOST_VERSION_MAJOR") - 1,
1284 buf, o_default, 0);
1285
1286 sprintf (buf, "%lu", ulMinor);
1287 define_variable ("KBUILD_HOST_VERSION_MINOR", sizeof ("KBUILD_HOST_VERSION_MINOR") - 1,
1288 buf, o_default, 0);
1289
1290 sprintf (buf, "%lu", ulPatch);
1291 define_variable ("KBUILD_HOST_VERSION_PATCH", sizeof ("KBUILD_HOST_VERSION_PATCH") - 1,
1292 buf, o_default, 0);
1293
1294 /* The kBuild locations. */
1295 define_variable ("KBUILD_PATH", sizeof ("KBUILD_PATH") - 1,
1296 get_kbuild_path (), o_default, 0);
1297 define_variable ("KBUILD_BIN_PATH", sizeof ("KBUILD_BIN_PATH") - 1,
1298 get_kbuild_bin_path (), o_default, 0);
1299
1300 define_variable ("PATH_KBUILD", sizeof ("PATH_KBUILD") - 1,
1301 get_kbuild_path (), o_default, 0);
1302 define_variable ("PATH_KBUILD_BIN", sizeof ("PATH_KBUILD_BIN") - 1,
1303 get_kbuild_bin_path (), o_default, 0);
1304
1305 /* Define KMK_FEATURES to indicate various working KMK features. */
1306# if defined (CONFIG_WITH_RSORT) \
1307 && defined (CONFIG_WITH_ABSPATHEX) \
1308 && defined (CONFIG_WITH_TOUPPER_TOLOWER) \
1309 && defined (CONFIG_WITH_DEFINED) \
1310 && defined (CONFIG_WITH_VALUE_LENGTH) \
1311 && defined (CONFIG_WITH_COMPARE) \
1312 && defined (CONFIG_WITH_STACK) \
1313 && defined (CONFIG_WITH_MATH) \
1314 && defined (CONFIG_WITH_XARGS) \
1315 && defined (CONFIG_WITH_EXPLICIT_MULTITARGET) \
1316 && defined (CONFIG_WITH_DOT_MUST_MAKE) \
1317 && defined (CONFIG_WITH_PREPEND_ASSIGNMENT) \
1318 && defined (CONFIG_WITH_SET_CONDITIONALS) \
1319 && defined (CONFIG_WITH_DATE) \
1320 && defined (CONFIG_WITH_FILE_SIZE) \
1321 && defined (CONFIG_WITH_WHERE_FUNCTION) \
1322 && defined (CONFIG_WITH_WHICH) \
1323 && defined (CONFIG_WITH_EVALPLUS) \
1324 && (defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)) \
1325 && defined (CONFIG_WITH_COMMANDS_FUNC) \
1326 && defined (CONFIG_WITH_PRINTF) \
1327 && defined (CONFIG_WITH_LOOP_FUNCTIONS) \
1328 && defined (CONFIG_WITH_ROOT_FUNC) \
1329 && defined (CONFIG_WITH_STRING_FUNCTIONS) \
1330 && defined (CONFIG_WITH_DEFINED_FUNCTIONS) \
1331 && defined (KMK_HELPERS)
1332 (void) define_variable ("KMK_FEATURES", 12,
1333 "append-dash-n abspath includedep-queue install-hard-linking"
1334 " kBuild-define"
1335 " rsort"
1336 " abspathex"
1337 " toupper tolower"
1338 " defined"
1339 " comp-vars comp-cmds comp-cmds-ex"
1340 " stack"
1341 " math-int"
1342 " xargs"
1343 " explicit-multitarget"
1344 " dot-must-make"
1345 " prepend-assignment"
1346 " set-conditionals intersects"
1347 " date"
1348 " file-size"
1349 " expr if-expr select"
1350 " where"
1351 " which"
1352 " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var"
1353 " make-stats"
1354 " commands"
1355 " printf"
1356 " for while"
1357 " root"
1358 " length insert pos lastpos substr translate"
1359 " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one kb-exp-tmpl "
1360 " firstdefined lastdefined "
1361 , o_default, 0);
1362# else /* MSC can't deal with strings mixed with #if/#endif, thus the slow way. */
1363# error "All features should be enabled by default!"
1364 strcpy (buf, "append-dash-n abspath includedep-queue install-hard-linking"
1365 " kBuild-define");
1366# if defined (CONFIG_WITH_RSORT)
1367 strcat (buf, " rsort");
1368# endif
1369# if defined (CONFIG_WITH_ABSPATHEX)
1370 strcat (buf, " abspathex");
1371# endif
1372# if defined (CONFIG_WITH_TOUPPER_TOLOWER)
1373 strcat (buf, " toupper tolower");
1374# endif
1375# if defined (CONFIG_WITH_DEFINED)
1376 strcat (buf, " defined");
1377# endif
1378# if defined (CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
1379 strcat (buf, " comp-vars comp-cmds comp-cmds-ex");
1380# endif
1381# if defined (CONFIG_WITH_STACK)
1382 strcat (buf, " stack");
1383# endif
1384# if defined (CONFIG_WITH_MATH)
1385 strcat (buf, " math-int");
1386# endif
1387# if defined (CONFIG_WITH_XARGS)
1388 strcat (buf, " xargs");
1389# endif
1390# if defined (CONFIG_WITH_EXPLICIT_MULTITARGET)
1391 strcat (buf, " explicit-multitarget");
1392# endif
1393# if defined (CONFIG_WITH_DOT_MUST_MAKE)
1394 strcat (buf, " dot-must-make");
1395# endif
1396# if defined (CONFIG_WITH_PREPEND_ASSIGNMENT)
1397 strcat (buf, " prepend-assignment");
1398# endif
1399# if defined (CONFIG_WITH_SET_CONDITIONALS)
1400 strcat (buf, " set-conditionals intersects");
1401# endif
1402# if defined (CONFIG_WITH_DATE)
1403 strcat (buf, " date");
1404# endif
1405# if defined (CONFIG_WITH_FILE_SIZE)
1406 strcat (buf, " file-size");
1407# endif
1408# if defined (CONFIG_WITH_IF_CONDITIONALS)
1409 strcat (buf, " expr if-expr select");
1410# endif
1411# if defined (CONFIG_WITH_WHERE_FUNCTION)
1412 strcat (buf, " where");
1413# endif
1414# if defined (CONFIG_WITH_WHICH)
1415 strcat (buf, " which");
1416# endif
1417# if defined (CONFIG_WITH_EVALPLUS)
1418 strcat (buf, " evalctx evalval evalvalctx evalcall evalcall2 eval-opt-var");
1419# endif
1420# if defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)
1421 strcat (buf, " make-stats");
1422# endif
1423# if defined (CONFIG_WITH_COMMANDS_FUNC)
1424 strcat (buf, " commands");
1425# endif
1426# if defined (CONFIG_WITH_PRINTF)
1427 strcat (buf, " printf");
1428# endif
1429# if defined (CONFIG_WITH_LOOP_FUNCTIONS)
1430 strcat (buf, " for while");
1431# endif
1432# if defined (CONFIG_WITH_ROOT_FUNC)
1433 strcat (buf, " root");
1434# endif
1435# if defined (CONFIG_WITH_STRING_FUNCTIONS)
1436 strcat (buf, " length insert pos lastpos substr translate");
1437# endif
1438# if defined (CONFIG_WITH_DEFINED_FUNCTIONS)
1439 strcat (buf, " firstdefined lastdefined");
1440# endif
1441# if defined (KMK_HELPERS)
1442 strcat (buf, " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one kb-exp-tmpl");
1443# endif
1444 (void) define_variable ("KMK_FEATURES", 12, buf, o_default, 0);
1445# endif
1446
1447#endif /* KMK */
1448
1449#ifdef CONFIG_WITH_KMK_BUILTIN
1450 /* The supported kMk Builtin commands. */
1451 (void) define_variable ("KMK_BUILTIN", 11, "append cat chmod cp cmp echo expr install kDepIDB ln md5sum mkdir mv printf rm rmdir sleep test", o_default, 0);
1452#endif
1453
1454#ifdef __MSDOS__
1455 /* Allow to specify a special shell just for Make,
1456 and use $COMSPEC as the default $SHELL when appropriate. */
1457 {
1458 static char shell_str[] = "SHELL";
1459 const int shlen = sizeof (shell_str) - 1;
1460 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
1461 struct variable *comp = lookup_variable ("COMSPEC", 7);
1462
1463 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
1464 if (mshp)
1465 (void) define_variable (shell_str, shlen,
1466 mshp->value, o_env_override, 0);
1467 else if (comp)
1468 {
1469 /* $COMSPEC shouldn't override $SHELL. */
1470 struct variable *shp = lookup_variable (shell_str, shlen);
1471
1472 if (!shp)
1473 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
1474 }
1475 }
1476#elif defined(__EMX__)
1477 {
1478 static char shell_str[] = "SHELL";
1479 const int shlen = sizeof (shell_str) - 1;
1480 struct variable *shell = lookup_variable (shell_str, shlen);
1481 struct variable *replace = lookup_variable ("MAKESHELL", 9);
1482
1483 /* if $MAKESHELL is defined in the environment assume o_env_override */
1484 if (replace && *replace->value && replace->origin == o_env)
1485 replace->origin = o_env_override;
1486
1487 /* if $MAKESHELL is not defined use $SHELL but only if the variable
1488 did not come from the environment */
1489 if (!replace || !*replace->value)
1490 if (shell && *shell->value && (shell->origin == o_env
1491 || shell->origin == o_env_override))
1492 {
1493 /* overwrite whatever we got from the environment */
1494 free(shell->value);
1495 shell->value = xstrdup (default_shell);
1496 shell->origin = o_default;
1497 }
1498
1499 /* Some people do not like cmd to be used as the default
1500 if $SHELL is not defined in the Makefile.
1501 With -DNO_CMD_DEFAULT you can turn off this behaviour */
1502# ifndef NO_CMD_DEFAULT
1503 /* otherwise use $COMSPEC */
1504 if (!replace || !*replace->value)
1505 replace = lookup_variable ("COMSPEC", 7);
1506
1507 /* otherwise use $OS2_SHELL */
1508 if (!replace || !*replace->value)
1509 replace = lookup_variable ("OS2_SHELL", 9);
1510# else
1511# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
1512# endif
1513
1514 if (replace && *replace->value)
1515 /* overwrite $SHELL */
1516 (void) define_variable (shell_str, shlen, replace->value,
1517 replace->origin, 0);
1518 else
1519 /* provide a definition if there is none */
1520 (void) define_variable (shell_str, shlen, default_shell,
1521 o_default, 0);
1522 }
1523
1524#endif
1525
1526 /* This won't override any definition, but it will provide one if there
1527 isn't one there. */
1528 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
1529#ifdef __MSDOS__
1530 v->export = v_export; /* Export always SHELL. */
1531#endif
1532
1533 /* On MSDOS we do use SHELL from environment, since it isn't a standard
1534 environment variable on MSDOS, so whoever sets it, does that on purpose.
1535 On OS/2 we do not use SHELL from environment but we have already handled
1536 that problem above. */
1537#if !defined(__MSDOS__) && !defined(__EMX__)
1538 /* Don't let SHELL come from the environment. */
1539 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
1540 {
1541# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1542 if (v->rdonly_val)
1543 v->rdonly_val = 0;
1544 else
1545# endif
1546 free (v->value);
1547 v->origin = o_file;
1548 v->value = xstrdup (default_shell);
1549# ifdef CONFIG_WITH_VALUE_LENGTH
1550 v->value_length = strlen (v->value);
1551 v->value_alloc_len = v->value_length + 1;
1552# endif
1553 }
1554#endif
1555
1556 /* Make sure MAKEFILES gets exported if it is set. */
1557 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
1558 v->export = v_ifset;
1559
1560 /* Define the magic D and F variables in terms of
1561 the automatic variables they are variations of. */
1562
1563#ifdef VMS
1564 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
1565 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
1566 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
1567 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
1568 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
1569 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
1570 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
1571#else
1572 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
1573 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
1574 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
1575 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
1576 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
1577 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
1578 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
1579#endif
1580 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
1581 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
1582 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
1583 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
1584 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
1585 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
1586 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
1587#ifdef CONFIG_WITH_LAZY_DEPS_VARS
1588 define_variable ("^", 1, "$(deps $@)", o_automatic, 1);
1589 define_variable ("+", 1, "$(deps-all $@)", o_automatic, 1);
1590 define_variable ("?", 1, "$(deps-newer $@)", o_automatic, 1);
1591 define_variable ("|", 1, "$(deps-oo $@)", o_automatic, 1);
1592#endif /* CONFIG_WITH_LAZY_DEPS_VARS */
1593}
1594
1595
1596int export_all_variables;
1597
1598/* Create a new environment for FILE's commands.
1599 If FILE is nil, this is for the `shell' function.
1600 The child's MAKELEVEL variable is incremented. */
1601
1602char **
1603target_environment (struct file *file)
1604{
1605 struct variable_set_list *set_list;
1606 register struct variable_set_list *s;
1607 struct hash_table table;
1608 struct variable **v_slot;
1609 struct variable **v_end;
1610 struct variable makelevel_key;
1611 char **result_0;
1612 char **result;
1613#ifdef CONFIG_WITH_STRCACHE2
1614 const char *cached_name;
1615#endif
1616
1617 if (file == 0)
1618 set_list = current_variable_set_list;
1619 else
1620 set_list = file->variables;
1621
1622#ifndef CONFIG_WITH_STRCACHE2
1623 hash_init (&table, VARIABLE_BUCKETS,
1624 variable_hash_1, variable_hash_2, variable_hash_cmp);
1625#else /* CONFIG_WITH_STRCACHE2 */
1626 hash_init_strcached (&table, VARIABLE_BUCKETS,
1627 &variable_strcache, offsetof (struct variable, name));
1628#endif /* CONFIG_WITH_STRCACHE2 */
1629
1630 /* Run through all the variable sets in the list,
1631 accumulating variables in TABLE. */
1632 for (s = set_list; s != 0; s = s->next)
1633 {
1634 struct variable_set *set = s->set;
1635 v_slot = (struct variable **) set->table.ht_vec;
1636 v_end = v_slot + set->table.ht_size;
1637 for ( ; v_slot < v_end; v_slot++)
1638 if (! HASH_VACANT (*v_slot))
1639 {
1640 struct variable **new_slot;
1641 struct variable *v = *v_slot;
1642
1643 /* If this is a per-target variable and it hasn't been touched
1644 already then look up the global version and take its export
1645 value. */
1646 if (v->per_target && v->export == v_default)
1647 {
1648 struct variable *gv;
1649
1650#ifndef CONFIG_WITH_VALUE_LENGTH
1651 gv = lookup_variable_in_set (v->name, strlen(v->name),
1652 &global_variable_set);
1653#else
1654 assert ((int)strlen(v->name) == v->length);
1655 gv = lookup_variable_in_set (v->name, v->length,
1656 &global_variable_set);
1657#endif
1658 if (gv)
1659 v->export = gv->export;
1660 }
1661
1662 switch (v->export)
1663 {
1664 case v_default:
1665 if (v->origin == o_default || v->origin == o_automatic)
1666 /* Only export default variables by explicit request. */
1667 continue;
1668
1669 /* The variable doesn't have a name that can be exported. */
1670 if (! v->exportable)
1671 continue;
1672
1673 if (! export_all_variables
1674 && v->origin != o_command
1675 && v->origin != o_env && v->origin != o_env_override)
1676 continue;
1677 break;
1678
1679 case v_export:
1680 break;
1681
1682 case v_noexport:
1683 {
1684 /* If this is the SHELL variable and it's not exported,
1685 then add the value from our original environment, if
1686 the original environment defined a value for SHELL. */
1687 extern struct variable shell_var;
1688 if (streq (v->name, "SHELL") && shell_var.value)
1689 {
1690 v = &shell_var;
1691 break;
1692 }
1693 continue;
1694 }
1695
1696 case v_ifset:
1697 if (v->origin == o_default)
1698 continue;
1699 break;
1700 }
1701
1702#ifndef CONFIG_WITH_STRCACHE2
1703 new_slot = (struct variable **) hash_find_slot (&table, v);
1704#else /* CONFIG_WITH_STRCACHE2 */
1705 assert (strcache2_is_cached (&variable_strcache, v->name));
1706 new_slot = (struct variable **) hash_find_slot_strcached (&table, v);
1707#endif /* CONFIG_WITH_STRCACHE2 */
1708 if (HASH_VACANT (*new_slot))
1709 hash_insert_at (&table, v, new_slot);
1710 }
1711 }
1712
1713#ifndef CONFIG_WITH_STRCACHE2
1714 makelevel_key.name = MAKELEVEL_NAME;
1715 makelevel_key.length = MAKELEVEL_LENGTH;
1716 hash_delete (&table, &makelevel_key);
1717#else /* CONFIG_WITH_STRCACHE2 */
1718 /* lookup the name in the string case, if it's not there it won't
1719 be in any of the sets either. */
1720 cached_name = strcache2_lookup (&variable_strcache,
1721 MAKELEVEL_NAME, MAKELEVEL_LENGTH);
1722 if (cached_name)
1723 {
1724 makelevel_key.name = cached_name;
1725 makelevel_key.length = MAKELEVEL_LENGTH;
1726 hash_delete_strcached (&table, &makelevel_key);
1727 }
1728#endif /* CONFIG_WITH_STRCACHE2 */
1729
1730 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
1731
1732 v_slot = (struct variable **) table.ht_vec;
1733 v_end = v_slot + table.ht_size;
1734 for ( ; v_slot < v_end; v_slot++)
1735 if (! HASH_VACANT (*v_slot))
1736 {
1737 struct variable *v = *v_slot;
1738
1739 /* If V is recursively expanded and didn't come from the environment,
1740 expand its value. If it came from the environment, it should
1741 go back into the environment unchanged. */
1742 if (v->recursive
1743 && v->origin != o_env && v->origin != o_env_override)
1744 {
1745#ifndef CONFIG_WITH_VALUE_LENGTH
1746 char *value = recursively_expand_for_file (v, file);
1747#else
1748 char *value = recursively_expand_for_file (v, file, NULL);
1749#endif
1750#ifdef WINDOWS32
1751 if (strcmp(v->name, "Path") == 0 ||
1752 strcmp(v->name, "PATH") == 0)
1753 convert_Path_to_windows32(value, ';');
1754#endif
1755 *result++ = xstrdup (concat (v->name, "=", value));
1756 free (value);
1757 }
1758 else
1759 {
1760#ifdef WINDOWS32
1761 if (strcmp(v->name, "Path") == 0 ||
1762 strcmp(v->name, "PATH") == 0)
1763 convert_Path_to_windows32(v->value, ';');
1764#endif
1765 *result++ = xstrdup (concat (v->name, "=", v->value));
1766 }
1767 }
1768
1769 *result = xmalloc (100);
1770 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
1771 *++result = 0;
1772
1773 hash_free (&table, 0);
1774
1775 return result_0;
1776}
1777
1778
1779#ifdef CONFIG_WITH_VALUE_LENGTH
1780/* Worker function for do_variable_definition_append() and
1781 append_expanded_string_to_variable().
1782 The APPEND argument indicates whether it's an append or prepend operation. */
1783void append_string_to_variable (struct variable *v, const char *value, unsigned int value_len, int append)
1784{
1785 /* The previous definition of the variable was recursive.
1786 The new value is the unexpanded old and new values. */
1787 unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
1788 int done_1st_prepend_copy = 0;
1789
1790 /* Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
1791 if (!value_len)
1792 return;
1793
1794 /* adjust the size. */
1795 if (v->value_alloc_len <= new_value_len + 1)
1796 {
1797 if (v->value_alloc_len < 256)
1798 v->value_alloc_len = 256;
1799 else
1800 v->value_alloc_len *= 2;
1801 if (v->value_alloc_len < new_value_len + 1)
1802 v->value_alloc_len = VAR_ALIGN_VALUE_ALLOC (new_value_len + 1 + value_len /*future*/ );
1803# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1804 if ((append || !v->value_length) && !v->rdonly_val)
1805# else
1806 if (append || !v->value_length)
1807# endif
1808 v->value = xrealloc (v->value, v->value_alloc_len);
1809 else
1810 {
1811 /* avoid the extra memcpy the xrealloc may have to do */
1812 char *new_buf = xmalloc (v->value_alloc_len);
1813 memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
1814 done_1st_prepend_copy = 1;
1815# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1816 if (v->rdonly_val)
1817 v->rdonly_val = 0;
1818 else
1819# endif
1820 free (v->value);
1821 v->value = new_buf;
1822 }
1823 MAKE_STATS_2(v->reallocs++);
1824 }
1825
1826 /* insert the new bits */
1827 if (v->value_length != 0)
1828 {
1829 if (append)
1830 {
1831 v->value[v->value_length] = ' ';
1832 memcpy (&v->value[v->value_length + 1], value, value_len + 1);
1833 }
1834 else
1835 {
1836 if (!done_1st_prepend_copy)
1837 memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
1838 v->value[value_len] = ' ';
1839 memcpy (v->value, value, value_len);
1840 }
1841 }
1842 else
1843 memcpy (v->value, value, value_len + 1);
1844 v->value_length = new_value_len;
1845}
1846
1847static struct variable *
1848do_variable_definition_append (const struct floc *flocp, struct variable *v,
1849 const char *value, unsigned int value_len,
1850 int simple_value, enum variable_origin origin,
1851 int append)
1852{
1853 if (env_overrides && origin == o_env)
1854 origin = o_env_override;
1855
1856 if (env_overrides && v->origin == o_env)
1857 /* V came from in the environment. Since it was defined
1858 before the switches were parsed, it wasn't affected by -e. */
1859 v->origin = o_env_override;
1860
1861 /* A variable of this name is already defined.
1862 If the old definition is from a stronger source
1863 than this one, don't redefine it. */
1864 if ((int) origin < (int) v->origin)
1865 return v;
1866 v->origin = origin;
1867
1868 /* location */
1869 if (flocp != 0)
1870 v->fileinfo = *flocp;
1871
1872 /* The juicy bits, append the specified value to the variable
1873 This is a heavily exercised code path in kBuild. */
1874 if (value_len == ~0U)
1875 value_len = strlen (value);
1876 if (v->recursive || simple_value)
1877 append_string_to_variable (v, value, value_len, append);
1878 else
1879 /* The previous definition of the variable was simple.
1880 The new value comes from the old value, which was expanded
1881 when it was set; and from the expanded new value. */
1882 append_expanded_string_to_variable (v, value, value_len, append);
1883
1884 /* update the variable */
1885 return v;
1886}
1887#endif /* CONFIG_WITH_VALUE_LENGTH */
1888
1889
1890static struct variable *
1891set_special_var (struct variable *var)
1892{
1893 if (streq (var->name, RECIPEPREFIX_NAME))
1894 {
1895 /* The user is resetting the command introduction prefix. This has to
1896 happen immediately, so that subsequent rules are interpreted
1897 properly. */
1898 cmd_prefix = var->value[0]=='\0' ? RECIPEPREFIX_DEFAULT : var->value[0];
1899 }
1900
1901 return var;
1902}
1903
1904
1905/* Given a variable, a value, and a flavor, define the variable.
1906 See the try_variable_definition() function for details on the parameters. */
1907
1908struct variable *
1909#ifndef CONFIG_WITH_VALUE_LENGTH
1910do_variable_definition (const struct floc *flocp, const char *varname,
1911 const char *value, enum variable_origin origin,
1912 enum variable_flavor flavor, int target_var)
1913#else /* CONFIG_WITH_VALUE_LENGTH */
1914do_variable_definition_2 (const struct floc *flocp,
1915 const char *varname, const char *value,
1916 unsigned int value_len, int simple_value,
1917 char *free_value,
1918 enum variable_origin origin,
1919 enum variable_flavor flavor,
1920 int target_var)
1921#endif /* CONFIG_WITH_VALUE_LENGTH */
1922{
1923 const char *p;
1924 char *alloc_value = NULL;
1925 struct variable *v;
1926 int append = 0;
1927 int conditional = 0;
1928 const size_t varname_len = strlen (varname); /* bird */
1929#ifdef CONFIG_WITH_VALUE_LENGTH
1930 assert (value_len == ~0U || value_len == strlen (value));
1931#endif
1932
1933 /* Calculate the variable's new value in VALUE. */
1934
1935 switch (flavor)
1936 {
1937 default:
1938 case f_bogus:
1939 /* Should not be possible. */
1940 abort ();
1941 case f_simple:
1942 /* A simple variable definition "var := value". Expand the value.
1943 We have to allocate memory since otherwise it'll clobber the
1944 variable buffer, and we may still need that if we're looking at a
1945 target-specific variable. */
1946#ifndef CONFIG_WITH_VALUE_LENGTH
1947 p = alloc_value = allocated_variable_expand (value);
1948#else /* CONFIG_WITH_VALUE_LENGTH */
1949 if (!simple_value)
1950 p = alloc_value = allocated_variable_expand_2 (value, value_len, &value_len);
1951 else
1952 {
1953 if (value_len == ~0U)
1954 value_len = strlen (value);
1955 if (!free_value)
1956 p = alloc_value = savestring (value, value_len);
1957 else
1958 {
1959 assert (value == free_value);
1960 p = alloc_value = free_value;
1961 free_value = 0;
1962 }
1963 }
1964#endif /* CONFIG_WITH_VALUE_LENGTH */
1965 break;
1966 case f_conditional:
1967 /* A conditional variable definition "var ?= value".
1968 The value is set IFF the variable is not defined yet. */
1969 v = lookup_variable (varname, varname_len);
1970 if (v)
1971#ifndef CONFIG_WITH_VALUE_LENGTH
1972 return v->special ? set_special_var (v) : v;
1973#else /* CONFIG_WITH_VALUE_LENGTH */
1974 {
1975 if (free_value)
1976 free (free_value);
1977 return v->special ? set_special_var (v) : v;
1978 }
1979#endif /* CONFIG_WITH_VALUE_LENGTH */
1980
1981 conditional = 1;
1982 flavor = f_recursive;
1983 /* FALLTHROUGH */
1984 case f_recursive:
1985 /* A recursive variable definition "var = value".
1986 The value is used verbatim. */
1987 p = value;
1988 break;
1989#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1990 case f_append:
1991 case f_prepend:
1992 {
1993 const enum variable_flavor org_flavor = flavor;
1994#else
1995 case f_append:
1996 {
1997#endif
1998
1999#ifdef CONFIG_WITH_LOCAL_VARIABLES
2000 /* If we have += but we're in a target or local variable context,
2001 we want to append only with other variables in the context of
2002 this target. */
2003 if (target_var || origin == o_local)
2004#else
2005 /* If we have += but we're in a target variable context, we want to
2006 append only with other variables in the context of this target. */
2007 if (target_var)
2008#endif
2009 {
2010 append = 1;
2011 v = lookup_variable_in_set (varname, varname_len,
2012 current_variable_set_list->set);
2013
2014 /* Don't append from the global set if a previous non-appending
2015 target-specific variable definition exists. */
2016 if (v && !v->append)
2017 append = 0;
2018 }
2019 else
2020 v = lookup_variable (varname, varname_len);
2021
2022 if (v == 0)
2023 {
2024 /* There was no old value.
2025 This becomes a normal recursive definition. */
2026 p = value;
2027 flavor = f_recursive;
2028 }
2029 else
2030 {
2031#ifdef CONFIG_WITH_VALUE_LENGTH
2032 v->append = append;
2033 v = do_variable_definition_append (flocp, v, value, value_len,
2034 simple_value, origin,
2035# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2036 org_flavor == f_append);
2037# else
2038 1);
2039# endif
2040 if (free_value)
2041 free (free_value);
2042 MAKE_STATS_2(v->changes++);
2043 return v;
2044#else /* !CONFIG_WITH_VALUE_LENGTH */
2045
2046 /* Paste the old and new values together in VALUE. */
2047
2048 unsigned int oldlen, vallen;
2049 const char *val;
2050 char *tp;
2051
2052 val = value;
2053 if (v->recursive)
2054 /* The previous definition of the variable was recursive.
2055 The new value is the unexpanded old and new values. */
2056 flavor = f_recursive;
2057 else
2058 /* The previous definition of the variable was simple.
2059 The new value comes from the old value, which was expanded
2060 when it was set; and from the expanded new value. Allocate
2061 memory for the expansion as we may still need the rest of the
2062 buffer if we're looking at a target-specific variable. */
2063 val = alloc_value = allocated_variable_expand (val);
2064
2065 oldlen = strlen (v->value);
2066 vallen = strlen (val);
2067 tp = alloca (oldlen + 1 + vallen + 1);
2068# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2069 if (org_flavor == f_prepend)
2070 {
2071 memcpy (tp, val, vallen);
2072 tp[oldlen] = ' ';
2073 memcpy (&tp[oldlen + 1], v->value, oldlen + 1);
2074 }
2075 else
2076# endif /* CONFIG_WITH_PREPEND_ASSIGNMENT */
2077 {
2078 memcpy (tp, v->value, oldlen);
2079 tp[oldlen] = ' ';
2080 memcpy (&tp[oldlen + 1], val, vallen + 1);
2081 }
2082 p = tp;
2083#endif /* !CONFIG_WITH_VALUE_LENGTH */
2084 }
2085 }
2086 }
2087
2088#ifdef __MSDOS__
2089 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
2090 non-Unix systems don't conform to this default configuration (in
2091 fact, most of them don't even have `/bin'). On the other hand,
2092 $SHELL in the environment, if set, points to the real pathname of
2093 the shell.
2094 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
2095 the Makefile override $SHELL from the environment. But first, we
2096 look for the basename of the shell in the directory where SHELL=
2097 points, and along the $PATH; if it is found in any of these places,
2098 we define $SHELL to be the actual pathname of the shell. Thus, if
2099 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
2100 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
2101 defining SHELL to be "d:/unix/bash.exe". */
2102 if ((origin == o_file || origin == o_override)
2103 && strcmp (varname, "SHELL") == 0)
2104 {
2105 PATH_VAR (shellpath);
2106 extern char * __dosexec_find_on_path (const char *, char *[], char *);
2107
2108 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
2109 if (__dosexec_find_on_path (p, NULL, shellpath))
2110 {
2111 char *tp;
2112
2113 for (tp = shellpath; *tp; tp++)
2114 if (*tp == '\\')
2115 *tp = '/';
2116
2117 v = define_variable_loc (varname, varname_len,
2118 shellpath, origin, flavor == f_recursive,
2119 flocp);
2120 }
2121 else
2122 {
2123 const char *shellbase, *bslash;
2124 struct variable *pathv = lookup_variable ("PATH", 4);
2125 char *path_string;
2126 char *fake_env[2];
2127 size_t pathlen = 0;
2128
2129 shellbase = strrchr (p, '/');
2130 bslash = strrchr (p, '\\');
2131 if (!shellbase || bslash > shellbase)
2132 shellbase = bslash;
2133 if (!shellbase && p[1] == ':')
2134 shellbase = p + 1;
2135 if (shellbase)
2136 shellbase++;
2137 else
2138 shellbase = p;
2139
2140 /* Search for the basename of the shell (with standard
2141 executable extensions) along the $PATH. */
2142 if (pathv)
2143 pathlen = strlen (pathv->value);
2144 path_string = xmalloc (5 + pathlen + 2 + 1);
2145 /* On MSDOS, current directory is considered as part of $PATH. */
2146 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
2147 fake_env[0] = path_string;
2148 fake_env[1] = 0;
2149 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
2150 {
2151 char *tp;
2152
2153 for (tp = shellpath; *tp; tp++)
2154 if (*tp == '\\')
2155 *tp = '/';
2156
2157 v = define_variable_loc (varname, varname_len,
2158 shellpath, origin,
2159 flavor == f_recursive, flocp);
2160 }
2161 else
2162 v = lookup_variable (varname, varname_len);
2163
2164 free (path_string);
2165 }
2166 }
2167 else
2168#endif /* __MSDOS__ */
2169#ifdef WINDOWS32
2170 if ( varname_len == sizeof("SHELL") - 1 /* bird */
2171 && (origin == o_file || origin == o_override || origin == o_command)
2172 && streq (varname, "SHELL"))
2173 {
2174 extern char *default_shell;
2175
2176 /* Call shell locator function. If it returns TRUE, then
2177 set no_default_sh_exe to indicate sh was found and
2178 set new value for SHELL variable. */
2179
2180 if (find_and_set_default_shell (p))
2181 {
2182 v = define_variable_in_set (varname, varname_len, default_shell,
2183# ifdef CONFIG_WITH_VALUE_LENGTH
2184 ~0U, 1 /* duplicate_value */,
2185# endif
2186 origin, flavor == f_recursive,
2187 (target_var
2188 ? current_variable_set_list->set
2189 : NULL),
2190 flocp);
2191 no_default_sh_exe = 0;
2192 }
2193 else
2194 {
2195 if (alloc_value)
2196 free (alloc_value);
2197
2198 alloc_value = allocated_variable_expand (p);
2199 if (find_and_set_default_shell (alloc_value))
2200 {
2201 v = define_variable_in_set (varname, varname_len, p,
2202#ifdef CONFIG_WITH_VALUE_LENGTH
2203 ~0U, 1 /* duplicate_value */,
2204#endif
2205 origin, flavor == f_recursive,
2206 (target_var
2207 ? current_variable_set_list->set
2208 : NULL),
2209 flocp);
2210 no_default_sh_exe = 0;
2211 }
2212 else
2213 v = lookup_variable (varname, varname_len);
2214 }
2215 }
2216 else
2217#endif
2218
2219 /* If we are defining variables inside an $(eval ...), we might have a
2220 different variable context pushed, not the global context (maybe we're
2221 inside a $(call ...) or something. Since this function is only ever
2222 invoked in places where we want to define globally visible variables,
2223 make sure we define this variable in the global set. */
2224
2225 v = define_variable_in_set (varname, varname_len, p,
2226#ifdef CONFIG_WITH_VALUE_LENGTH
2227 value_len, !alloc_value,
2228#endif
2229 origin, flavor == f_recursive,
2230#ifdef CONFIG_WITH_LOCAL_VARIABLES
2231 (target_var || origin == o_local
2232#else
2233 (target_var
2234#endif
2235 ? current_variable_set_list->set : NULL),
2236 flocp);
2237 v->append = append;
2238 v->conditional = conditional;
2239
2240#ifndef CONFIG_WITH_VALUE_LENGTH
2241 if (alloc_value)
2242 free (alloc_value);
2243#else
2244 if (free_value)
2245 free (free_value);
2246#endif
2247
2248 return v->special ? set_special_var (v) : v;
2249}
2250
2251
2252/* Try to interpret LINE (a null-terminated string) as a variable definition.
2253
2254 ORIGIN may be o_file, o_override, o_env, o_env_override,
2255 or o_command specifying that the variable definition comes
2256 from a makefile, an override directive, the environment with
2257 or without the -e switch, or the command line.
2258
2259 See the comments for parse_variable_definition().
2260
2261 If LINE was recognized as a variable definition, a pointer to its `struct
2262 variable' is returned. If LINE is not a variable definition, NULL is
2263 returned. */
2264
2265struct variable *
2266#ifndef CONFIG_WITH_VALUE_LENGTH
2267parse_variable_definition (struct variable *v, char *line)
2268#else
2269parse_variable_definition (struct variable *v, char *line, char *eos)
2270#endif
2271{
2272 register int c;
2273 register char *p = line;
2274 register char *beg;
2275 register char *end;
2276 enum variable_flavor flavor = f_bogus;
2277#ifndef CONFIG_WITH_VALUE_LENGTH
2278 char *name;
2279#endif
2280
2281 while (1)
2282 {
2283 c = *p++;
2284 if (c == '\0' || c == '#')
2285 return 0;
2286 if (c == '=')
2287 {
2288 end = p - 1;
2289 flavor = f_recursive;
2290 break;
2291 }
2292 else if (c == ':')
2293 if (*p == '=')
2294 {
2295 end = p++ - 1;
2296 flavor = f_simple;
2297 break;
2298 }
2299 else
2300 /* A colon other than := is a rule line, not a variable defn. */
2301 return 0;
2302 else if (c == '+' && *p == '=')
2303 {
2304 end = p++ - 1;
2305 flavor = f_append;
2306 break;
2307 }
2308#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
2309 else if (c == '<' && *p == '=')
2310 {
2311 end = p++ - 1;
2312 flavor = f_prepend;
2313 break;
2314 }
2315#endif
2316 else if (c == '?' && *p == '=')
2317 {
2318 end = p++ - 1;
2319 flavor = f_conditional;
2320 break;
2321 }
2322 else if (c == '$')
2323 {
2324 /* This might begin a variable expansion reference. Make sure we
2325 don't misrecognize chars inside the reference as =, := or +=. */
2326 char closeparen;
2327 int count;
2328 c = *p++;
2329 if (c == '(')
2330 closeparen = ')';
2331 else if (c == '{')
2332 closeparen = '}';
2333 else
2334 continue; /* Nope. */
2335
2336 /* P now points past the opening paren or brace.
2337 Count parens or braces until it is matched. */
2338 count = 0;
2339 for (; *p != '\0'; ++p)
2340 {
2341 if (*p == c)
2342 ++count;
2343 else if (*p == closeparen && --count < 0)
2344 {
2345 ++p;
2346 break;
2347 }
2348 }
2349 }
2350 }
2351 v->flavor = flavor;
2352
2353 beg = next_token (line);
2354 while (end > beg && isblank ((unsigned char)end[-1]))
2355 --end;
2356 p = next_token (p);
2357 v->value = p;
2358#ifdef CONFIG_WITH_VALUE_LENGTH
2359 v->value_alloc_len = ~(unsigned int)0;
2360 v->value_length = eos != NULL ? eos - p : -1;
2361 assert (eos == NULL || strchr (p, '\0') == eos);
2362# ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2363 v->rdonly_val = 0;
2364# endif
2365#endif
2366
2367 /* Expand the name, so "$(foo)bar = baz" works. */
2368#ifndef CONFIG_WITH_VALUE_LENGTH
2369 name = alloca (end - beg + 1);
2370 memcpy (name, beg, end - beg);
2371 name[end - beg] = '\0';
2372 v->name = allocated_variable_expand (name);
2373#else /* CONFIG_WITH_VALUE_LENGTH */
2374 v->name = allocated_variable_expand_2 (beg, end - beg, NULL);
2375#endif /* CONFIG_WITH_VALUE_LENGTH */
2376
2377 if (v->name[0] == '\0')
2378 fatal (&v->fileinfo, _("empty variable name"));
2379
2380 return v;
2381}
2382
2383
2384/* Try to interpret LINE (a null-terminated string) as a variable definition.
2385
2386 ORIGIN may be o_file, o_override, o_env, o_env_override, o_local,
2387 or o_command specifying that the variable definition comes
2388 from a makefile, an override directive, the environment with
2389 or without the -e switch, or the command line.
2390
2391 See the comments for parse_variable_definition().
2392
2393 If LINE was recognized as a variable definition, a pointer to its `struct
2394 variable' is returned. If LINE is not a variable definition, NULL is
2395 returned. */
2396
2397struct variable *
2398#ifndef CONFIG_WITH_VALUE_LENGTH
2399try_variable_definition (const struct floc *flocp, char *line,
2400 enum variable_origin origin, int target_var)
2401#else
2402try_variable_definition (const struct floc *flocp, char *line, char *eos,
2403 enum variable_origin origin, int target_var)
2404#endif
2405{
2406 struct variable v;
2407 struct variable *vp;
2408
2409 if (flocp != 0)
2410 v.fileinfo = *flocp;
2411 else
2412 v.fileinfo.filenm = 0;
2413
2414#ifndef CONFIG_WITH_VALUE_LENGTH
2415 if (!parse_variable_definition (&v, line))
2416 return 0;
2417
2418 vp = do_variable_definition (flocp, v.name, v.value,
2419 origin, v.flavor, target_var);
2420#else
2421 if (!parse_variable_definition (&v, line, eos))
2422 return 0;
2423
2424 vp = do_variable_definition_2 (flocp, v.name, v.value, v.value_length,
2425 0, NULL, origin, v.flavor, target_var);
2426#endif
2427
2428#ifndef CONFIG_WITH_STRCACHE2
2429 free (v.name);
2430#else
2431 free ((char *)v.name);
2432#endif
2433
2434 return vp;
2435}
2436
2437
2438#ifdef CONFIG_WITH_MAKE_STATS
2439static unsigned long var_stats_changes, var_stats_changed;
2440static unsigned long var_stats_reallocs, var_stats_realloced;
2441static unsigned long var_stats_val_len, var_stats_val_alloc_len;
2442static unsigned long var_stats_val_rdonly_len;
2443#endif
2444
2445/* Print information for variable V, prefixing it with PREFIX. */
2446
2447static void
2448print_variable (const void *item, void *arg)
2449{
2450 const struct variable *v = item;
2451 const char *prefix = arg;
2452 const char *origin;
2453
2454 switch (v->origin)
2455 {
2456 case o_default:
2457 origin = _("default");
2458 break;
2459 case o_env:
2460 origin = _("environment");
2461 break;
2462 case o_file:
2463 origin = _("makefile");
2464 break;
2465 case o_env_override:
2466 origin = _("environment under -e");
2467 break;
2468 case o_command:
2469 origin = _("command line");
2470 break;
2471 case o_override:
2472 origin = _("`override' directive");
2473 break;
2474 case o_automatic:
2475 origin = _("automatic");
2476 break;
2477#ifdef CONFIG_WITH_LOCAL_VARIABLES
2478 case o_local:
2479 origin = _("`local' directive");
2480 break;
2481#endif
2482 case o_invalid:
2483 default:
2484 abort ();
2485 }
2486 fputs ("# ", stdout);
2487 fputs (origin, stdout);
2488 if (v->fileinfo.filenm)
2489 printf (_(" (from `%s', line %lu)"),
2490 v->fileinfo.filenm, v->fileinfo.lineno);
2491#ifdef CONFIG_WITH_MAKE_STATS
2492 if (v->changes != 0)
2493 printf (_(", %u changes"), v->changes);
2494 var_stats_changes += v->changes;
2495 var_stats_changed += (v->changes != 0);
2496 if (v->reallocs != 0)
2497 printf (_(", %u reallocs"), v->reallocs);
2498 var_stats_reallocs += v->reallocs;
2499 var_stats_realloced += (v->reallocs != 0);
2500 var_stats_val_len += v->value_length;
2501 if (v->value_alloc_len)
2502 var_stats_val_alloc_len += v->value_alloc_len;
2503 else
2504 var_stats_val_rdonly_len += v->value_length;
2505 assert (v->value_length == strlen (v->value));
2506 /*assert (v->rdonly_val ? !v->value_alloc_len : v->value_alloc_len > v->value_length); - FIXME */
2507#endif /* CONFIG_WITH_MAKE_STATS */
2508 putchar ('\n');
2509 fputs (prefix, stdout);
2510
2511 /* Is this a `define'? */
2512 if (v->recursive && strchr (v->value, '\n') != 0)
2513 printf ("define %s\n%s\nendef\n", v->name, v->value);
2514 else
2515 {
2516 register char *p;
2517
2518 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
2519
2520 /* Check if the value is just whitespace. */
2521 p = next_token (v->value);
2522 if (p != v->value && *p == '\0')
2523 /* All whitespace. */
2524 printf ("$(subst ,,%s)", v->value);
2525 else if (v->recursive)
2526 fputs (v->value, stdout);
2527 else
2528 /* Double up dollar signs. */
2529 for (p = v->value; *p != '\0'; ++p)
2530 {
2531 if (*p == '$')
2532 putchar ('$');
2533 putchar (*p);
2534 }
2535 putchar ('\n');
2536 }
2537}
2538
2539
2540/* Print all the variables in SET. PREFIX is printed before
2541 the actual variable definitions (everything else is comments). */
2542
2543void
2544print_variable_set (struct variable_set *set, char *prefix)
2545{
2546#ifdef CONFIG_WITH_MAKE_STATS
2547 var_stats_changes = var_stats_changed = var_stats_reallocs
2548 = var_stats_realloced = var_stats_val_len = var_stats_val_alloc_len
2549 = var_stats_val_rdonly_len = 0;
2550
2551 hash_map_arg (&set->table, print_variable, prefix);
2552
2553 if (set->table.ht_fill)
2554 {
2555 unsigned long fragmentation;
2556
2557 fragmentation = var_stats_val_alloc_len - (var_stats_val_len - var_stats_val_rdonly_len);
2558 printf(_("# variable set value stats:\n\
2559# strings %7lu bytes, readonly %6lu bytes\n"),
2560 var_stats_val_len, var_stats_val_rdonly_len);
2561
2562 if (var_stats_val_alloc_len)
2563 printf(_("# allocated %7lu bytes, fragmentation %6lu bytes (%u%%)\n"),
2564 var_stats_val_alloc_len, fragmentation,
2565 (unsigned int)((100.0 * fragmentation) / var_stats_val_alloc_len));
2566
2567 if (var_stats_changed)
2568 printf(_("# changed %5lu (%2u%%), changes %6lu\n"),
2569 var_stats_changed,
2570 (unsigned int)((100.0 * var_stats_changed) / set->table.ht_fill),
2571 var_stats_changes);
2572
2573 if (var_stats_realloced)
2574 printf(_("# reallocated %5lu (%2u%%), reallocations %6lu\n"),
2575 var_stats_realloced,
2576 (unsigned int)((100.0 * var_stats_realloced) / set->table.ht_fill),
2577 var_stats_reallocs);
2578 }
2579#else
2580 hash_map_arg (&set->table, print_variable, prefix);
2581#endif
2582
2583 fputs (_("# variable set hash-table stats:\n"), stdout);
2584 fputs ("# ", stdout);
2585 hash_print_stats (&set->table, stdout);
2586 putc ('\n', stdout);
2587}
2588
2589/* Print the data base of variables. */
2590
2591void
2592print_variable_data_base (void)
2593{
2594 puts (_("\n# Variables\n"));
2595
2596 print_variable_set (&global_variable_set, "");
2597
2598 puts (_("\n# Pattern-specific Variable Values"));
2599
2600 {
2601 struct pattern_var *p;
2602 int rules = 0;
2603
2604 for (p = pattern_vars; p != 0; p = p->next)
2605 {
2606 ++rules;
2607 printf ("\n%s :\n", p->target);
2608 print_variable (&p->variable, "# ");
2609 }
2610
2611 if (rules == 0)
2612 puts (_("\n# No pattern-specific variable values."));
2613 else
2614 printf (_("\n# %u pattern-specific variable values"), rules);
2615 }
2616
2617#ifdef CONFIG_WITH_STRCACHE2
2618 strcache2_print_stats (&variable_strcache, "# ");
2619#endif
2620}
2621
2622#ifdef CONFIG_WITH_PRINT_STATS_SWITCH
2623void
2624print_variable_stats (void)
2625{
2626 fputs (_("\n# Global variable hash-table stats:\n# "), stdout);
2627 hash_print_stats (&global_variable_set.table, stdout);
2628 fputs ("\n", stdout);
2629}
2630#endif
2631
2632/* Print all the local variables of FILE. */
2633
2634void
2635print_file_variables (const struct file *file)
2636{
2637 if (file->variables != 0)
2638 print_variable_set (file->variables->set, "# ");
2639}
2640
2641#ifdef WINDOWS32
2642void
2643sync_Path_environment (void)
2644{
2645 char *path = allocated_variable_expand ("$(PATH)");
2646 static char *environ_path = NULL;
2647
2648 if (!path)
2649 return;
2650
2651 /*
2652 * If done this before, don't leak memory unnecessarily.
2653 * Free the previous entry before allocating new one.
2654 */
2655 if (environ_path)
2656 free (environ_path);
2657
2658 /*
2659 * Create something WINDOWS32 world can grok
2660 */
2661 convert_Path_to_windows32 (path, ';');
2662 environ_path = xstrdup (concat ("PATH", "=", path));
2663 putenv (environ_path);
2664 free (path);
2665}
2666#endif
Note: See TracBrowser for help on using the repository browser.

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