VirtualBox

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

Last change on this file since 1440 was 1440, checked in by bird, 17 years ago

Added comp-cmds-ex, commands, commands-sc and commands-usr. Added a '%' command prefix that make the commands functions skip the line. Added a -c flag to append that'll make it call commands on each argument (similar to -v). Fixed a little bug in comp-cmds/vars.

  • Property svn:eol-style set to native
File size: 62.5 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 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 2, or (at your option) any later version.
10
11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16GNU Make; see the file COPYING. If not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
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#endif
36
37/* Chain of all pattern-specific variables. */
38
39static struct pattern_var *pattern_vars;
40
41/* Pointer to last struct in the chain, so we can add onto the end. */
42
43static struct pattern_var *last_pattern_var;
44
45/* Create a new pattern-specific variable struct. */
46
47struct pattern_var *
48create_pattern_var (const char *target, const char *suffix)
49{
50 register struct pattern_var *p = xmalloc (sizeof (struct pattern_var));
51
52 if (last_pattern_var != 0)
53 last_pattern_var->next = p;
54 else
55 pattern_vars = p;
56 last_pattern_var = p;
57 p->next = 0;
58
59 p->target = target;
60 p->len = strlen (target);
61 p->suffix = suffix + 1;
62
63 return p;
64}
65
66/* Look up a target in the pattern-specific variable list. */
67
68static struct pattern_var *
69lookup_pattern_var (struct pattern_var *start, const char *target)
70{
71 struct pattern_var *p;
72 unsigned int targlen = strlen(target);
73
74 for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
75 {
76 const char *stem;
77 unsigned int stemlen;
78
79 if (p->len > targlen)
80 /* It can't possibly match. */
81 continue;
82
83 /* From the lengths of the filename and the pattern parts,
84 find the stem: the part of the filename that matches the %. */
85 stem = target + (p->suffix - p->target - 1);
86 stemlen = targlen - p->len + 1;
87
88 /* Compare the text in the pattern before the stem, if any. */
89 if (stem > target && !strneq (p->target, target, stem - target))
90 continue;
91
92 /* Compare the text in the pattern after the stem, if any.
93 We could test simply using streq, but this way we compare the
94 first two characters immediately. This saves time in the very
95 common case where the first character matches because it is a
96 period. */
97 if (*p->suffix == stem[stemlen]
98 && (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
99 break;
100 }
101
102 return p;
103}
104
105
106/* Hash table of all global variable definitions. */
107
108#ifdef VARIABLE_HASH
109# ifdef _MSC_VER
110# define inline _inline
111typedef signed int int32_t;
112typedef signed short int int16_t;
113# endif
114static inline unsigned long variable_hash_2i(register const char *var, register int length)
115{
116 register const unsigned char *uvar = (const unsigned char *)var;
117 register unsigned int hash = 0;
118 uvar += length;
119 switch (length)
120 {
121 default:
122 case 16: hash = *--uvar + (hash << 6) + (hash << 16) - hash;
123 case 15: hash = *--uvar + (hash << 6) + (hash << 16) - hash;
124 case 14: hash = *--uvar + (hash << 6) + (hash << 16) - hash;
125 case 13: hash = *--uvar + (hash << 6) + (hash << 16) - hash;
126 case 12: hash = *--uvar + (hash << 6) + (hash << 16) - hash;
127 case 11: hash = *--uvar + (hash << 6) + (hash << 16) - hash;
128 case 10: hash = *--uvar + (hash << 6) + (hash << 16) - hash;
129 case 9: hash = *--uvar + (hash << 6) + (hash << 16) - hash;
130 case 8: hash = *--uvar + (hash << 6) + (hash << 16) - hash;
131 case 7: hash = *--uvar + (hash << 6) + (hash << 16) - hash;
132 case 6: hash = *--uvar + (hash << 6) + (hash << 16) - hash;
133 case 5: hash = *--uvar + (hash << 6) + (hash << 16) - hash;
134 case 4: hash = *--uvar + (hash << 6) + (hash << 16) - hash;
135 case 3: hash = *--uvar + (hash << 6) + (hash << 16) - hash;
136 case 2: hash = *--uvar + (hash << 6) + (hash << 16) - hash;
137 case 1: hash = *--uvar + (hash << 6) + (hash << 16) - hash;
138 case 0:
139 break;
140 }
141 return hash;
142}
143
144static inline unsigned long variable_hash_1i(register const char *var, register int length)
145{
146 register const unsigned char *uvar = (const unsigned char *)var;
147 register unsigned int hash = ((5381 << 5) + 5381) + *uvar;
148 switch (length)
149 {
150 default:
151 case 8: hash = ((hash << 5) + hash) + uvar[7];
152 case 7: hash = ((hash << 5) + hash) + uvar[6];
153 case 6: hash = ((hash << 5) + hash) + uvar[5];
154 case 5: hash = ((hash << 5) + hash) + uvar[4];
155 case 4: hash = ((hash << 5) + hash) + uvar[3];
156 case 3: hash = ((hash << 5) + hash) + uvar[2];
157 case 2: hash = ((hash << 5) + hash) + uvar[1];
158 case 1: return hash;
159 case 0: return 5381; /* shouldn't happen */
160 }
161}
162#endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
163
164static unsigned long
165variable_hash_1 (const void *keyv)
166{
167 struct variable const *key = (struct variable const *) keyv;
168#ifdef VARIABLE_HASH /* bird */
169# ifdef VARIABLE_HASH_STRICT
170 if (key->hash1 != variable_hash_1i (key->name, key->length))
171 __asm__("int3");
172 if (key->hash2 && key->hash2 != variable_hash_2i (key->name, key->length))
173 __asm__("int3");
174# endif
175 return key->hash1;
176#else
177# ifdef CONFIG_WITH_OPTIMIZATION_HACKS
178 return variable_hash_1i (key->name, key->length);
179# else
180 return_STRING_N_HASH_1 (key->name, key->length);
181# endif
182#endif
183}
184
185static unsigned long
186variable_hash_2 (const void *keyv)
187{
188#ifdef VARIABLE_HASH /* bird */
189 struct variable *key = (struct variable *) keyv;
190 if (!key->hash2)
191 key->hash2 = variable_hash_2i (key->name, key->length);
192 return key->hash2;
193#else
194 struct variable const *key = (struct variable const *) keyv;
195# ifdef CONFIG_WITH_OPTIMIZATION_HACKS
196 return variable_hash_2i (key->name, key->length);
197# else
198 return_STRING_N_HASH_2 (key->name, key->length);
199# endif
200#endif
201}
202
203static int
204variable_hash_cmp (const void *xv, const void *yv)
205{
206 struct variable const *x = (struct variable const *) xv;
207 struct variable const *y = (struct variable const *) yv;
208 int result = x->length - y->length;
209 if (result)
210 return result;
211#ifdef VARIABLE_HASH_STRICT /* bird */
212 if (x->hash1 != variable_hash_1i (x->name, x->length))
213 __asm__("int3");
214 if (x->hash2 && x->hash2 != variable_hash_2i (x->name, x->length))
215 __asm__("int3");
216 if (y->hash1 != variable_hash_1i (y->name, y->length))
217 __asm__("int3");
218 if (y->hash2 && y->hash2 != variable_hash_2i (y->name, y->length))
219 __asm__("int3");
220#endif
221#ifdef VARIABLE_HASH
222 /* hash 1 */
223 result = x->hash1 - y->hash1;
224 if (result)
225 return result;
226#endif
227#ifdef CONFIG_WITH_OPTIMIZATION_HACKS /* bird: speed */
228 {
229 const char *xs = x->name;
230 const char *ys = y->name;
231 switch (x->length)
232 {
233 case 8:
234 result = *(int32_t*)(xs + 4) - *(int32_t*)(ys + 4);
235 if (result)
236 return result;
237 return *(int32_t*)xs - *(int32_t*)ys;
238 case 7:
239 result = xs[6] - ys[6];
240 if (result)
241 return result;
242 case 6:
243 result = *(int32_t*)xs - *(int32_t*)ys;
244 if (result)
245 return result;
246 return *(int16_t*)(xs + 4) - *(int16_t*)(ys + 4);
247 case 5:
248 result = xs[4] - ys[4];
249 if (result)
250 return result;
251 case 4:
252 return *(int32_t*)xs - *(int32_t*)ys;
253 case 3:
254 result = xs[2] - ys[2];
255 if (result)
256 return result;
257 case 2:
258 return *(int16_t*)xs - *(int16_t*)ys;
259 case 1:
260 return *xs - *ys;
261 case 0:
262 return 0;
263 }
264 }
265#endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
266#ifdef VARIABLE_HASH
267 /* hash 2 */
268 if (!x->hash2)
269 ((struct variable *)x)->hash2 = variable_hash_2i (x->name, x->length);
270 if (!y->hash2)
271 ((struct variable *)y)->hash2 = variable_hash_2i (y->name, y->length);
272 result = x->hash2 - y->hash2;
273 if (result)
274 return result;
275#endif
276#ifdef CONFIG_WITH_OPTIMIZATION_HACKS
277 return memcmp (x->name, y->name, x->length);
278#else
279 return_STRING_N_COMPARE (x->name, y->name, x->length);
280#endif
281}
282
283#ifndef VARIABLE_BUCKETS
284# ifdef KMK /* Move to Makefile.kmk? */
285# define VARIABLE_BUCKETS 16384
286# else /*!KMK*/
287#define VARIABLE_BUCKETS 523
288# endif /*!KMK*/
289#endif
290#ifndef PERFILE_VARIABLE_BUCKETS
291#define PERFILE_VARIABLE_BUCKETS 23
292#endif
293#ifndef SMALL_SCOPE_VARIABLE_BUCKETS
294#define SMALL_SCOPE_VARIABLE_BUCKETS 13
295#endif
296
297static struct variable_set global_variable_set;
298static struct variable_set_list global_setlist
299 = { 0, &global_variable_set };
300struct variable_set_list *current_variable_set_list = &global_setlist;
301
302
303/* Implement variables. */
304
305void
306init_hash_global_variable_set (void)
307{
308 hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
309 variable_hash_1, variable_hash_2, variable_hash_cmp);
310}
311
312/* Define variable named NAME with value VALUE in SET. VALUE is copied.
313 LENGTH is the length of NAME, which does not need to be null-terminated.
314 ORIGIN specifies the origin of the variable (makefile, command line
315 or environment).
316 If RECURSIVE is nonzero a flag is set in the variable saying
317 that it should be recursively re-expanded. */
318
319#ifdef CONFIG_WITH_VALUE_LENGTH
320struct variable *
321define_variable_in_set (const char *name, unsigned int length,
322 const char *value, unsigned int value_length, int duplicate_value,
323 enum variable_origin origin, int recursive,
324 struct variable_set *set, const struct floc *flocp)
325#else
326struct variable *
327define_variable_in_set (const char *name, unsigned int length,
328 const char *value, enum variable_origin origin,
329 int recursive, struct variable_set *set,
330 const struct floc *flocp)
331#endif
332{
333 struct variable *v;
334 struct variable **var_slot;
335 struct variable var_key;
336
337 if (set == NULL)
338 set = &global_variable_set;
339
340 var_key.name = (char *) name;
341 var_key.length = length;
342#ifdef VARIABLE_HASH /* bird */
343 var_key.hash1 = variable_hash_1i (name, length);
344 var_key.hash2 = 0;
345#endif
346 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
347
348 if (env_overrides && origin == o_env)
349 origin = o_env_override;
350
351 v = *var_slot;
352 if (! HASH_VACANT (v))
353 {
354 if (env_overrides && v->origin == o_env)
355 /* V came from in the environment. Since it was defined
356 before the switches were parsed, it wasn't affected by -e. */
357 v->origin = o_env_override;
358
359 /* A variable of this name is already defined.
360 If the old definition is from a stronger source
361 than this one, don't redefine it. */
362 if ((int) origin >= (int) v->origin)
363 {
364#ifdef CONFIG_WITH_VALUE_LENGTH
365 if (value_length == ~0U)
366 value_length = strlen (value);
367 else
368 assert (value_length == strlen (value));
369 if (!duplicate_value)
370 {
371 if (v->value != 0)
372 free (v->value);
373 v->value = (char *)value;
374 v->value_alloc_len = value_length + 1;
375 }
376 else
377 {
378 if ((unsigned int)v->value_alloc_len <= value_length)
379 {
380 free (v->value);
381 v->value_alloc_len = (value_length + 0x40) & ~0x3f;
382 v->value = xmalloc (v->value_alloc_len);
383 }
384 memcpy (v->value, value, value_length + 1);
385 }
386 v->value_length = value_length;
387#else
388 if (v->value != 0)
389 free (v->value);
390 v->value = xstrdup (value);
391#endif
392 if (flocp != 0)
393 v->fileinfo = *flocp;
394 else
395 v->fileinfo.filenm = 0;
396 v->origin = origin;
397 v->recursive = recursive;
398 }
399 return v;
400 }
401
402 /* Create a new variable definition and add it to the hash table. */
403
404 v = xmalloc (sizeof (struct variable));
405 v->name = savestring (name, length);
406 v->length = length;
407#ifdef VARIABLE_HASH /* bird */
408 v->hash1 = variable_hash_1i (name, length);
409 v->hash2 = 0;
410#endif
411 hash_insert_at (&set->table, v, var_slot);
412#ifdef CONFIG_WITH_VALUE_LENGTH
413 if (value_length == ~0U)
414 value_length = strlen (value);
415 else
416 assert (value_length == strlen (value));
417 v->value_length = value_length;
418 if (!duplicate_value)
419 {
420 v->value_alloc_len = value_length + 1;
421 v->value = (char *)value;
422 }
423 else
424 {
425 v->value_alloc_len = (value_length + 32) & ~31;
426 v->value = xmalloc (v->value_alloc_len);
427 memcpy (v->value, value, value_length + 1);
428 }
429#else
430 v->value = xstrdup (value);
431#endif
432 if (flocp != 0)
433 v->fileinfo = *flocp;
434 else
435 v->fileinfo.filenm = 0;
436 v->origin = origin;
437 v->recursive = recursive;
438 v->special = 0;
439 v->expanding = 0;
440 v->exp_count = 0;
441 v->per_target = 0;
442 v->append = 0;
443 v->export = v_default;
444
445 v->exportable = 1;
446 if (*name != '_' && (*name < 'A' || *name > 'Z')
447 && (*name < 'a' || *name > 'z'))
448 v->exportable = 0;
449 else
450 {
451 for (++name; *name != '\0'; ++name)
452 if (*name != '_' && (*name < 'a' || *name > 'z')
453 && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
454 break;
455
456 if (*name != '\0')
457 v->exportable = 0;
458 }
459
460 return v;
461}
462
463
464/* If the variable passed in is "special", handle its special nature.
465 Currently there are two such variables, both used for introspection:
466 .VARIABLES expands to a list of all the variables defined in this instance
467 of make.
468 .TARGETS expands to a list of all the targets defined in this
469 instance of make.
470 Returns the variable reference passed in. */
471
472#define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
473
474static struct variable *
475handle_special_var (struct variable *var)
476{
477 static unsigned long last_var_count = 0;
478
479
480 /* This one actually turns out to be very hard, due to the way the parser
481 records targets. The way it works is that target information is collected
482 internally until make knows the target is completely specified. It unitl
483 it sees that some new construct (a new target or variable) is defined that
484 it knows the previous one is done. In short, this means that if you do
485 this:
486
487 all:
488
489 TARGS := $(.TARGETS)
490
491 then $(TARGS) won't contain "all", because it's not until after the
492 variable is created that the previous target is completed.
493
494 Changing this would be a major pain. I think a less complex way to do it
495 would be to pre-define the target files as soon as the first line is
496 parsed, then come back and do the rest of the definition as now. That
497 would allow $(.TARGETS) to be correct without a major change to the way
498 the parser works.
499
500 if (streq (var->name, ".TARGETS"))
501 var->value = build_target_list (var->value);
502 else
503 */
504
505 if (streq (var->name, ".VARIABLES")
506 && global_variable_set.table.ht_fill != last_var_count)
507 {
508 unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
509 unsigned long len;
510 char *p;
511 struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
512 struct variable **end = &vp[global_variable_set.table.ht_size];
513
514 /* Make sure we have at least MAX bytes in the allocated buffer. */
515 var->value = xrealloc (var->value, max);
516
517 /* Walk through the hash of variables, constructing a list of names. */
518 p = var->value;
519 len = 0;
520 for (; vp < end; ++vp)
521 if (!HASH_VACANT (*vp))
522 {
523 struct variable *v = *vp;
524 int l = v->length;
525
526 len += l + 1;
527 if (len > max)
528 {
529 unsigned long off = p - var->value;
530
531 max += EXPANSION_INCREMENT (l + 1);
532 var->value = xrealloc (var->value, max);
533 p = &var->value[off];
534 }
535
536 memcpy (p, v->name, l);
537 p += l;
538 *(p++) = ' ';
539 }
540 *(p-1) = '\0';
541
542 /* Remember how many variables are in our current count. Since we never
543 remove variables from the list, this is a reliable way to know whether
544 the list is up to date or needs to be recomputed. */
545
546 last_var_count = global_variable_set.table.ht_fill;
547 }
548
549 return var;
550}
551
552
553
554/* Lookup a variable whose name is a string starting at NAME
555 and with LENGTH chars. NAME need not be null-terminated.
556 Returns address of the `struct variable' containing all info
557 on the variable, or nil if no such variable is defined. */
558
559struct variable *
560lookup_variable (const char *name, unsigned int length)
561{
562 const struct variable_set_list *setlist;
563 struct variable var_key;
564
565 var_key.name = (char *) name;
566 var_key.length = length;
567#ifdef VARIABLE_HASH /* bird */
568 var_key.hash1 = variable_hash_1i (name, length);
569 var_key.hash2 = 0;
570#endif
571
572 for (setlist = current_variable_set_list;
573 setlist != 0; setlist = setlist->next)
574 {
575#ifdef VARIABLE_HASH /* bird: speed */
576 struct hash_table *ht = &setlist->set->table;
577 unsigned int hash_1 = var_key.hash1;
578 struct variable *v;
579
580 ht->ht_lookups++;
581 for (;;)
582 {
583 hash_1 &= (ht->ht_size - 1);
584 v = (struct variable *)ht->ht_vec[hash_1];
585
586 if (v == 0)
587 break;
588 if ((void *)v != hash_deleted_item)
589 {
590 if (variable_hash_cmp(&var_key, v) == 0)
591 {
592# ifdef VARIABLE_HASH_STRICT /* bird */
593 struct variable *v2 = (struct variable *) hash_find_item ((struct hash_table *) &setlist->set->table, &var_key);
594 assert(v2 == v);
595# endif
596 return v->special ? handle_special_var (v) : v;
597 }
598 ht->ht_collisions++;
599 }
600 if (!var_key.hash2)
601 var_key.hash2 = variable_hash_2i(name, length);
602 hash_1 += (var_key.hash2 | 1);
603 }
604
605#else /* !VARIABLE_HASH */
606 const struct variable_set *set = setlist->set;
607 struct variable *v;
608
609 v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
610 if (v)
611 return v->special ? handle_special_var (v) : v;
612#endif /* !VARIABLE_HASH */
613 }
614
615#ifdef VMS
616 /* since we don't read envp[] on startup, try to get the
617 variable via getenv() here. */
618 {
619 char *vname = alloca (length + 1);
620 char *value;
621 strncpy (vname, name, length);
622 vname[length] = 0;
623 value = getenv (vname);
624 if (value != 0)
625 {
626 char *sptr;
627 int scnt;
628
629 sptr = value;
630 scnt = 0;
631
632 while ((sptr = strchr (sptr, '$')))
633 {
634 scnt++;
635 sptr++;
636 }
637
638 if (scnt > 0)
639 {
640 char *nvalue;
641 char *nptr;
642
643 nvalue = alloca (strlen (value) + scnt + 1);
644 sptr = value;
645 nptr = nvalue;
646
647 while (*sptr)
648 {
649 if (*sptr == '$')
650 {
651 *nptr++ = '$';
652 *nptr++ = '$';
653 }
654 else
655 {
656 *nptr++ = *sptr;
657 }
658 sptr++;
659 }
660
661 *nptr = '\0';
662 return define_variable (vname, length, nvalue, o_env, 1);
663
664 }
665
666 return define_variable (vname, length, value, o_env, 1);
667 }
668 }
669#endif /* VMS */
670
671 return 0;
672}
673
674
675/* Lookup a variable whose name is a string starting at NAME
676 and with LENGTH chars in set SET. NAME need not be null-terminated.
677 Returns address of the `struct variable' containing all info
678 on the variable, or nil if no such variable is defined. */
679
680struct variable *
681lookup_variable_in_set (const char *name, unsigned int length,
682 const struct variable_set *set)
683{
684 struct variable var_key;
685
686 var_key.name = (char *) name;
687 var_key.length = length;
688#ifdef VARIABLE_HASH /* bird */
689 var_key.hash1 = variable_hash_1i (name, length);
690 var_key.hash2 = 0;
691#endif
692
693 return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
694}
695
696
697/* Initialize FILE's variable set list. If FILE already has a variable set
698 list, the topmost variable set is left intact, but the the rest of the
699 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
700 rule, then we will use the "root" double-colon target's variable set as the
701 parent of FILE's variable set.
702
703 If we're READING a makefile, don't do the pattern variable search now,
704 since the pattern variable might not have been defined yet. */
705
706void
707initialize_file_variables (struct file *file, int reading)
708{
709 struct variable_set_list *l = file->variables;
710
711 if (l == 0)
712 {
713 l = (struct variable_set_list *)
714 xmalloc (sizeof (struct variable_set_list));
715 l->set = xmalloc (sizeof (struct variable_set));
716 hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
717 variable_hash_1, variable_hash_2, variable_hash_cmp);
718 file->variables = l;
719 }
720
721 /* If this is a double-colon, then our "parent" is the "root" target for
722 this double-colon rule. Since that rule has the same name, parent,
723 etc. we can just use its variables as the "next" for ours. */
724
725 if (file->double_colon && file->double_colon != file)
726 {
727 initialize_file_variables (file->double_colon, reading);
728 l->next = file->double_colon->variables;
729 return;
730 }
731
732 if (file->parent == 0)
733 l->next = &global_setlist;
734 else
735 {
736 initialize_file_variables (file->parent, reading);
737 l->next = file->parent->variables;
738 }
739
740 /* If we're not reading makefiles and we haven't looked yet, see if
741 we can find pattern variables for this target. */
742
743 if (!reading && !file->pat_searched)
744 {
745 struct pattern_var *p;
746
747 p = lookup_pattern_var (0, file->name);
748 if (p != 0)
749 {
750 struct variable_set_list *global = current_variable_set_list;
751
752 /* We found at least one. Set up a new variable set to accumulate
753 all the pattern variables that match this target. */
754
755 file->pat_variables = create_new_variable_set ();
756 current_variable_set_list = file->pat_variables;
757
758 do
759 {
760 /* We found one, so insert it into the set. */
761
762 struct variable *v;
763
764 if (p->variable.flavor == f_simple)
765 {
766 v = define_variable_loc (
767 p->variable.name, strlen (p->variable.name),
768 p->variable.value, p->variable.origin,
769 0, &p->variable.fileinfo);
770
771 v->flavor = f_simple;
772 }
773 else
774 {
775 v = do_variable_definition (
776 &p->variable.fileinfo, p->variable.name,
777 p->variable.value, p->variable.origin,
778 p->variable.flavor, 1);
779 }
780
781 /* Also mark it as a per-target and copy export status. */
782 v->per_target = p->variable.per_target;
783 v->export = p->variable.export;
784 }
785 while ((p = lookup_pattern_var (p, file->name)) != 0);
786
787 current_variable_set_list = global;
788 }
789 file->pat_searched = 1;
790 }
791
792 /* If we have a pattern variable match, set it up. */
793
794 if (file->pat_variables != 0)
795 {
796 file->pat_variables->next = l->next;
797 l->next = file->pat_variables;
798 }
799}
800
801
802/* Pop the top set off the current variable set list,
803 and free all its storage. */
804
805struct variable_set_list *
806create_new_variable_set (void)
807{
808 register struct variable_set_list *setlist;
809 register struct variable_set *set;
810
811 set = xmalloc (sizeof (struct variable_set));
812 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
813 variable_hash_1, variable_hash_2, variable_hash_cmp);
814
815 setlist = (struct variable_set_list *)
816 xmalloc (sizeof (struct variable_set_list));
817 setlist->set = set;
818 setlist->next = current_variable_set_list;
819
820 return setlist;
821}
822
823static void
824free_variable_name_and_value (const void *item)
825{
826 struct variable *v = (struct variable *) item;
827 free (v->name);
828 free (v->value);
829}
830
831void
832free_variable_set (struct variable_set_list *list)
833{
834 hash_map (&list->set->table, free_variable_name_and_value);
835 hash_free (&list->set->table, 1);
836 free (list->set);
837 free (list);
838}
839
840/* Create a new variable set and push it on the current setlist.
841 If we're pushing a global scope (that is, the current scope is the global
842 scope) then we need to "push" it the other way: file variable sets point
843 directly to the global_setlist so we need to replace that with the new one.
844 */
845
846struct variable_set_list *
847push_new_variable_scope (void)
848{
849 current_variable_set_list = create_new_variable_set();
850 if (current_variable_set_list->next == &global_setlist)
851 {
852 /* It was the global, so instead of new -> &global we want to replace
853 &global with the new one and have &global -> new, with current still
854 pointing to &global */
855 struct variable_set *set = current_variable_set_list->set;
856 current_variable_set_list->set = global_setlist.set;
857 global_setlist.set = set;
858 current_variable_set_list->next = global_setlist.next;
859 global_setlist.next = current_variable_set_list;
860 current_variable_set_list = &global_setlist;
861 }
862 return (current_variable_set_list);
863}
864
865void
866pop_variable_scope (void)
867{
868 struct variable_set_list *setlist;
869 struct variable_set *set;
870
871 /* Can't call this if there's no scope to pop! */
872 assert(current_variable_set_list->next != NULL);
873
874 if (current_variable_set_list != &global_setlist)
875 {
876 /* We're not pointing to the global setlist, so pop this one. */
877 setlist = current_variable_set_list;
878 set = setlist->set;
879 current_variable_set_list = setlist->next;
880 }
881 else
882 {
883 /* This set is the one in the global_setlist, but there is another global
884 set beyond that. We want to copy that set to global_setlist, then
885 delete what used to be in global_setlist. */
886 setlist = global_setlist.next;
887 set = global_setlist.set;
888 global_setlist.set = setlist->set;
889 global_setlist.next = setlist->next;
890 }
891
892 /* Free the one we no longer need. */
893 free (setlist);
894 hash_map (&set->table, free_variable_name_and_value);
895 hash_free (&set->table, 1);
896 free (set);
897}
898
899
900/* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
901
902static void
903merge_variable_sets (struct variable_set *to_set,
904 struct variable_set *from_set)
905{
906 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
907 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
908
909 for ( ; from_var_slot < from_var_end; from_var_slot++)
910 if (! HASH_VACANT (*from_var_slot))
911 {
912 struct variable *from_var = *from_var_slot;
913 struct variable **to_var_slot
914 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
915 if (HASH_VACANT (*to_var_slot))
916 hash_insert_at (&to_set->table, from_var, to_var_slot);
917 else
918 {
919 /* GKM FIXME: delete in from_set->table */
920 free (from_var->value);
921 free (from_var);
922 }
923 }
924}
925
926/* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
927
928void
929merge_variable_set_lists (struct variable_set_list **setlist0,
930 struct variable_set_list *setlist1)
931{
932 struct variable_set_list *to = *setlist0;
933 struct variable_set_list *last0 = 0;
934
935 /* If there's nothing to merge, stop now. */
936 if (!setlist1)
937 return;
938
939 /* This loop relies on the fact that all setlists terminate with the global
940 setlist (before NULL). If that's not true, arguably we SHOULD die. */
941 if (to)
942 while (setlist1 != &global_setlist && to != &global_setlist)
943 {
944 struct variable_set_list *from = setlist1;
945 setlist1 = setlist1->next;
946
947 merge_variable_sets (to->set, from->set);
948
949 last0 = to;
950 to = to->next;
951 }
952
953 if (setlist1 != &global_setlist)
954 {
955 if (last0 == 0)
956 *setlist0 = setlist1;
957 else
958 last0->next = setlist1;
959 }
960}
961
962
963/* Define the automatic variables, and record the addresses
964 of their structures so we can change their values quickly. */
965
966void
967define_automatic_variables (void)
968{
969#if defined(WINDOWS32) || defined(__EMX__)
970 extern char* default_shell;
971#else
972 extern char default_shell[];
973#endif
974 register struct variable *v;
975#ifndef KMK
976 char buf[200];
977#else
978 char buf[1024];
979 const char *envvar;
980#endif
981
982 sprintf (buf, "%u", makelevel);
983 (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
984
985 sprintf (buf, "%s%s%s",
986 version_string,
987 (remote_description == 0 || remote_description[0] == '\0')
988 ? "" : "-",
989 (remote_description == 0 || remote_description[0] == '\0')
990 ? "" : remote_description);
991 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
992
993#ifdef KMK
994 /* Define KMK_VERSION to indicate kMk. */
995 (void) define_variable ("KMK_VERSION", 11, buf, o_default, 0);
996
997 /* Define KBUILD_VERSION* */
998 sprintf (buf, "%d", KBUILD_VERSION_MAJOR);
999 define_variable ("KBUILD_VERSION_MAJOR", sizeof ("KBUILD_VERSION_MAJOR") - 1,
1000 buf, o_default, 0);
1001 sprintf (buf, "%d", KBUILD_VERSION_MINOR);
1002 define_variable ("KBUILD_VERSION_MINOR", sizeof("KBUILD_VERSION_MINOR") - 1,
1003 buf, o_default, 0);
1004 sprintf (buf, "%d", KBUILD_VERSION_PATCH);
1005 define_variable ("KBUILD_VERSION_PATCH", sizeof ("KBUILD_VERSION_PATCH") - 1,
1006 buf, o_default, 0);
1007
1008 sprintf (buf, "%d.%d.%d", KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR, KBUILD_VERSION_PATCH);
1009 define_variable ("KBUILD_VERSION", sizeof ("KBUILD_VERSION") - 1,
1010 buf, o_default, 0);
1011
1012 /* The build platform defaults. */
1013 envvar = getenv ("BUILD_PLATFORM");
1014 if (!envvar)
1015 define_variable ("BUILD_PLATFORM", sizeof ("BUILD_PLATFORM") - 1,
1016 BUILD_PLATFORM, o_default, 0);
1017 envvar = getenv ("BUILD_PLATFORM_ARCH");
1018 if (!envvar)
1019 define_variable ("BUILD_PLATFORM_ARCH", sizeof ("BUILD_PLATFORM_ARCH") - 1,
1020 BUILD_PLATFORM_ARCH, o_default, 0);
1021 envvar = getenv ("BUILD_PLATFORM_CPU");
1022 if (!envvar)
1023 define_variable ("BUILD_PLATFORM_CPU", sizeof ("BUILD_PLATFORM_CPU") - 1,
1024 BUILD_PLATFORM_CPU, o_default, 0);
1025
1026 /* The kBuild locations. */
1027 define_variable ("PATH_KBUILD", sizeof ("PATH_KBUILD") - 1,
1028 get_path_kbuild (), o_default, 0);
1029 define_variable ("PATH_KBUILD_BIN", sizeof ("PATH_KBUILD_BIN") - 1,
1030 get_path_kbuild_bin (), o_default, 0);
1031
1032 /* Define KMK_FEATURES to indicate various working KMK features. */
1033# if defined (CONFIG_WITH_RSORT) \
1034 && defined (CONFIG_WITH_ABSPATHEX) \
1035 && defined (CONFIG_WITH_TOUPPER_TOLOWER) \
1036 && defined (CONFIG_WITH_VALUE_LENGTH) && defined (CONFIG_WITH_COMPARE) \
1037 && defined (CONFIG_WITH_STACK) \
1038 && defined (CONFIG_WITH_MATH) \
1039 && defined (CONFIG_WITH_XARGS) \
1040 && defined (CONFIG_WITH_EXPLICIT_MULTITARGET) \
1041 && defined (CONFIG_WITH_PREPEND_ASSIGNMENT) \
1042 && defined (CONFIG_WITH_SET_CONDITIONALS) \
1043 && defined (CONFIG_WITH_DATE) \
1044 && defined (CONFIG_WITH_FILE_SIZE) \
1045 && defined (CONFIG_WITH_WHICH) \
1046 && defined (CONFIG_WITH_EVALPLUS) \
1047 && defined (CONFIG_WITH_MAKE_STATS) \
1048 && defined (CONFIG_WITH_COMMANDS_FUNC) \
1049 && defined (KMK_HELPERS)
1050 (void) define_variable ("KMK_FEATURES", 12,
1051 "append-dash-n abspath"
1052 " rsort"
1053 " abspathex"
1054 " toupper tolower"
1055 " comp-vars comp-cmds comp-cmds-ex"
1056 " stack"
1057 " math-int"
1058 " xargs"
1059 " explicit-multitarget"
1060 " prepend-assignment"
1061 " set-conditionals"
1062 " date"
1063 " file-size"
1064 " which"
1065 " evalctx evalval evalvalctx evalcall evalcall2"
1066 " make-stats"
1067 " commands"
1068 " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one "
1069 , o_default, 0);
1070# else /* MSC can't deal with strings mixed with #if/#endif, thus the slow way. */
1071# error "All features should be enabled by default!"
1072 strcpy (buf, "append-dash-n abspath");
1073# if defined (CONFIG_WITH_RSORT)
1074 strcat (buf, " rsort");
1075# endif
1076# if defined (CONFIG_WITH_ABSPATHEX)
1077 strcat (buf, " abspathex");
1078# endif
1079# if defined (CONFIG_WITH_TOUPPER_TOLOWER)
1080 strcat (buf, " toupper tolower");
1081# endif
1082# if defined (CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
1083 strcat (buf, " comp-vars comp-cmds comp-cmds-ex");
1084# endif
1085# if defined (CONFIG_WITH_STACK)
1086 strcat (buf, " stack");
1087# endif
1088# if defined (CONFIG_WITH_MATH)
1089 strcat (buf, " math-int");
1090# endif
1091# if defined (CONFIG_WITH_XARGS)
1092 strcat (buf, " xargs");
1093# endif
1094# if defined (CONFIG_WITH_EXPLICIT_MULTITARGET)
1095 strcat (buf, " explicit-multitarget");
1096# endif
1097# if defined (CONFIG_WITH_PREPEND_ASSIGNMENT)
1098 strcat (buf, " prepend-assignment");
1099# endif
1100# if defined (CONFIG_WITH_SET_CONDITIONALS)
1101 strcat (buf, " set-conditionals");
1102# endif
1103# if defined (CONFIG_WITH_DATE)
1104 strcat (buf, " date");
1105# endif
1106# if defined (CONFIG_WITH_FILE_SIZE)
1107 strcat (buf, " file-size");
1108# endif
1109# if defined (CONFIG_WITH_WHICH)
1110 strcat (buf, " which");
1111# endif
1112# if defined (CONFIG_WITH_EVALPLUS)
1113 strcat (buf, " evalctx evalval evalvalctx evalcall evalcall2");
1114# endif
1115# if defined (CONFIG_WITH_MAKE_STATS)
1116 strcat (buf, " make-stats");
1117# endif
1118# if defined (CONFIG_WITH_COMMANDS_FUNC)
1119 strcat (buf, " commands");
1120# endif
1121# if defined (KMK_HELPERS)
1122 strcat (buf, " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one");
1123# endif
1124 (void) define_variable ("KMK_FEATURES", 12, buf, o_default, 0);
1125# endif
1126
1127#endif /* KMK */
1128
1129#ifdef CONFIG_WITH_KMK_BUILTIN
1130 /* The supported kMk Builtin commands. */
1131 (void) define_variable ("KMK_BUILTIN", 11, "append cat cp cmp echo install kDepIDB ln md5sum mkdir mv printf rm rmdir test", o_default, 0);
1132#endif
1133
1134#ifdef __MSDOS__
1135 /* Allow to specify a special shell just for Make,
1136 and use $COMSPEC as the default $SHELL when appropriate. */
1137 {
1138 static char shell_str[] = "SHELL";
1139 const int shlen = sizeof (shell_str) - 1;
1140 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
1141 struct variable *comp = lookup_variable ("COMSPEC", 7);
1142
1143 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
1144 if (mshp)
1145 (void) define_variable (shell_str, shlen,
1146 mshp->value, o_env_override, 0);
1147 else if (comp)
1148 {
1149 /* $COMSPEC shouldn't override $SHELL. */
1150 struct variable *shp = lookup_variable (shell_str, shlen);
1151
1152 if (!shp)
1153 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
1154 }
1155 }
1156#elif defined(__EMX__)
1157 {
1158 static char shell_str[] = "SHELL";
1159 const int shlen = sizeof (shell_str) - 1;
1160 struct variable *shell = lookup_variable (shell_str, shlen);
1161 struct variable *replace = lookup_variable ("MAKESHELL", 9);
1162
1163 /* if $MAKESHELL is defined in the environment assume o_env_override */
1164 if (replace && *replace->value && replace->origin == o_env)
1165 replace->origin = o_env_override;
1166
1167 /* if $MAKESHELL is not defined use $SHELL but only if the variable
1168 did not come from the environment */
1169 if (!replace || !*replace->value)
1170 if (shell && *shell->value && (shell->origin == o_env
1171 || shell->origin == o_env_override))
1172 {
1173 /* overwrite whatever we got from the environment */
1174 free(shell->value);
1175 shell->value = xstrdup (default_shell);
1176 shell->origin = o_default;
1177 }
1178
1179 /* Some people do not like cmd to be used as the default
1180 if $SHELL is not defined in the Makefile.
1181 With -DNO_CMD_DEFAULT you can turn off this behaviour */
1182# ifndef NO_CMD_DEFAULT
1183 /* otherwise use $COMSPEC */
1184 if (!replace || !*replace->value)
1185 replace = lookup_variable ("COMSPEC", 7);
1186
1187 /* otherwise use $OS2_SHELL */
1188 if (!replace || !*replace->value)
1189 replace = lookup_variable ("OS2_SHELL", 9);
1190# else
1191# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
1192# endif
1193
1194 if (replace && *replace->value)
1195 /* overwrite $SHELL */
1196 (void) define_variable (shell_str, shlen, replace->value,
1197 replace->origin, 0);
1198 else
1199 /* provide a definition if there is none */
1200 (void) define_variable (shell_str, shlen, default_shell,
1201 o_default, 0);
1202 }
1203
1204#endif
1205
1206 /* This won't override any definition, but it will provide one if there
1207 isn't one there. */
1208 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
1209
1210 /* On MSDOS we do use SHELL from environment, since it isn't a standard
1211 environment variable on MSDOS, so whoever sets it, does that on purpose.
1212 On OS/2 we do not use SHELL from environment but we have already handled
1213 that problem above. */
1214#if !defined(__MSDOS__) && !defined(__EMX__)
1215 /* Don't let SHELL come from the environment. */
1216 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
1217 {
1218 free (v->value);
1219 v->origin = o_file;
1220 v->value = xstrdup (default_shell);
1221#ifdef CONFIG_WITH_VALUE_LENGTH
1222 v->value_length = strlen (v->value);
1223 v->value_alloc_len = v->value_length + 1;
1224#endif
1225 }
1226#endif
1227
1228 /* Make sure MAKEFILES gets exported if it is set. */
1229 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
1230 v->export = v_ifset;
1231
1232 /* Define the magic D and F variables in terms of
1233 the automatic variables they are variations of. */
1234
1235#ifdef VMS
1236 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
1237 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
1238 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
1239 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
1240 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
1241 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
1242 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
1243#else
1244 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
1245 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
1246 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
1247 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
1248 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
1249 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
1250 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
1251#endif
1252 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
1253 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
1254 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
1255 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
1256 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
1257 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
1258 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
1259}
1260
1261
1262int export_all_variables;
1263
1264/* Create a new environment for FILE's commands.
1265 If FILE is nil, this is for the `shell' function.
1266 The child's MAKELEVEL variable is incremented. */
1267
1268char **
1269target_environment (struct file *file)
1270{
1271 struct variable_set_list *set_list;
1272 register struct variable_set_list *s;
1273 struct hash_table table;
1274 struct variable **v_slot;
1275 struct variable **v_end;
1276 struct variable makelevel_key;
1277 char **result_0;
1278 char **result;
1279
1280 if (file == 0)
1281 set_list = current_variable_set_list;
1282 else
1283 set_list = file->variables;
1284
1285 hash_init (&table, VARIABLE_BUCKETS,
1286 variable_hash_1, variable_hash_2, variable_hash_cmp);
1287
1288 /* Run through all the variable sets in the list,
1289 accumulating variables in TABLE. */
1290 for (s = set_list; s != 0; s = s->next)
1291 {
1292 struct variable_set *set = s->set;
1293 v_slot = (struct variable **) set->table.ht_vec;
1294 v_end = v_slot + set->table.ht_size;
1295 for ( ; v_slot < v_end; v_slot++)
1296 if (! HASH_VACANT (*v_slot))
1297 {
1298 struct variable **new_slot;
1299 struct variable *v = *v_slot;
1300
1301 /* If this is a per-target variable and it hasn't been touched
1302 already then look up the global version and take its export
1303 value. */
1304 if (v->per_target && v->export == v_default)
1305 {
1306 struct variable *gv;
1307
1308 gv = lookup_variable_in_set (v->name, strlen(v->name),
1309 &global_variable_set);
1310 if (gv)
1311 v->export = gv->export;
1312 }
1313
1314 switch (v->export)
1315 {
1316 case v_default:
1317 if (v->origin == o_default || v->origin == o_automatic)
1318 /* Only export default variables by explicit request. */
1319 continue;
1320
1321 /* The variable doesn't have a name that can be exported. */
1322 if (! v->exportable)
1323 continue;
1324
1325 if (! export_all_variables
1326 && v->origin != o_command
1327 && v->origin != o_env && v->origin != o_env_override)
1328 continue;
1329 break;
1330
1331 case v_export:
1332 break;
1333
1334 case v_noexport:
1335 /* If this is the SHELL variable and it's not exported, then
1336 add the value from our original environment. */
1337 if (streq (v->name, "SHELL"))
1338 {
1339 extern struct variable shell_var;
1340 v = &shell_var;
1341 break;
1342 }
1343 continue;
1344
1345 case v_ifset:
1346 if (v->origin == o_default)
1347 continue;
1348 break;
1349 }
1350
1351 new_slot = (struct variable **) hash_find_slot (&table, v);
1352 if (HASH_VACANT (*new_slot))
1353 hash_insert_at (&table, v, new_slot);
1354 }
1355 }
1356
1357 makelevel_key.name = MAKELEVEL_NAME;
1358 makelevel_key.length = MAKELEVEL_LENGTH;
1359#ifdef VARIABLE_HASH /* bird */
1360 makelevel_key.hash1 = variable_hash_1i (MAKELEVEL_NAME, MAKELEVEL_LENGTH);
1361 makelevel_key.hash2 = 0;
1362#endif
1363 hash_delete (&table, &makelevel_key);
1364
1365 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
1366
1367 v_slot = (struct variable **) table.ht_vec;
1368 v_end = v_slot + table.ht_size;
1369 for ( ; v_slot < v_end; v_slot++)
1370 if (! HASH_VACANT (*v_slot))
1371 {
1372 struct variable *v = *v_slot;
1373
1374 /* If V is recursively expanded and didn't come from the environment,
1375 expand its value. If it came from the environment, it should
1376 go back into the environment unchanged. */
1377 if (v->recursive
1378 && v->origin != o_env && v->origin != o_env_override)
1379 {
1380 char *value = recursively_expand_for_file (v, file);
1381#ifdef WINDOWS32
1382 if (strcmp(v->name, "Path") == 0 ||
1383 strcmp(v->name, "PATH") == 0)
1384 convert_Path_to_windows32(value, ';');
1385#endif
1386 *result++ = xstrdup (concat (v->name, "=", value));
1387 free (value);
1388 }
1389 else
1390 {
1391#ifdef WINDOWS32
1392 if (strcmp(v->name, "Path") == 0 ||
1393 strcmp(v->name, "PATH") == 0)
1394 convert_Path_to_windows32(v->value, ';');
1395#endif
1396 *result++ = xstrdup (concat (v->name, "=", v->value));
1397 }
1398 }
1399
1400 *result = xmalloc (100);
1401 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
1402 *++result = 0;
1403
1404 hash_free (&table, 0);
1405
1406 return result_0;
1407}
1408
1409
1410#ifdef CONFIG_WITH_VALUE_LENGTH
1411static struct variable *
1412do_variable_definition_append (const struct floc *flocp, struct variable *v, const char *value,
1413 enum variable_origin origin, int append)
1414{
1415 if (env_overrides && origin == o_env)
1416 origin = o_env_override;
1417
1418 if (env_overrides && v->origin == o_env)
1419 /* V came from in the environment. Since it was defined
1420 before the switches were parsed, it wasn't affected by -e. */
1421 v->origin = o_env_override;
1422
1423 /* A variable of this name is already defined.
1424 If the old definition is from a stronger source
1425 than this one, don't redefine it. */
1426 if ((int) origin < (int) v->origin)
1427 return v;
1428 v->origin = origin;
1429
1430 /* location */
1431 if (flocp != 0)
1432 v->fileinfo = *flocp;
1433
1434 /* The juicy bits, append the specified value to the variable
1435 This is a heavily exercised code path in kBuild. */
1436 if (v->recursive)
1437 {
1438 /* The previous definition of the variable was recursive.
1439 The new value is the unexpanded old and new values. */
1440 unsigned int value_len = strlen (value);
1441 unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
1442 int done_1st_prepend_copy = 0;
1443
1444 /* adjust the size. */
1445 if ((unsigned)v->value_alloc_len <= new_value_len + 1)
1446 {
1447 v->value_alloc_len *= 2;
1448 if (v->value_alloc_len < new_value_len + 1)
1449 v->value_alloc_len = (new_value_len + 1 + value_len + 0x7f) + ~0x7fU;
1450 if (append || !v->value_length)
1451 v->value = xrealloc (v->value, v->value_alloc_len);
1452 else
1453 {
1454 /* avoid the extra memcpy the xrealloc may have to do */
1455 char *new_buf = xmalloc (v->value_alloc_len);
1456 memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
1457 done_1st_prepend_copy = 1;
1458 free (v->value);
1459 v->value = new_buf;
1460 }
1461 }
1462
1463 /* insert the new bits */
1464 if (v->value_length != 0)
1465 {
1466 if (append)
1467 {
1468 v->value[v->value_length] = ' ';
1469 memcpy (&v->value[v->value_length + 1], value, value_len + 1);
1470 }
1471 else
1472 {
1473 if (!done_1st_prepend_copy)
1474 memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
1475 v->value[value_len] = ' ';
1476 memcpy (v->value, value, value_len);
1477 }
1478 }
1479 else
1480 memcpy (v->value, value, value_len + 1);
1481 v->value_length = new_value_len;
1482 }
1483 else
1484 {
1485 /* The previous definition of the variable was simple.
1486 The new value comes from the old value, which was expanded
1487 when it was set; and from the expanded new value. */
1488 append_expanded_string_to_variable(v, value);
1489 }
1490
1491 /* update the variable */
1492 return v;
1493}
1494#endif /* CONFIG_WITH_VALUE_LENGTH */
1495
1496
1497/* Given a variable, a value, and a flavor, define the variable.
1498 See the try_variable_definition() function for details on the parameters. */
1499
1500struct variable *
1501do_variable_definition (const struct floc *flocp, const char *varname,
1502 const char *value, enum variable_origin origin,
1503 enum variable_flavor flavor, int target_var)
1504{
1505 const char *p;
1506 char *alloc_value = NULL;
1507 struct variable *v;
1508 int append = 0;
1509 int conditional = 0;
1510 const size_t varname_len = strlen (varname); /* bird */
1511#ifdef CONFIG_WITH_VALUE_LENGTH
1512 unsigned int value_len = ~0U;
1513#endif
1514
1515 /* Calculate the variable's new value in VALUE. */
1516
1517 switch (flavor)
1518 {
1519 default:
1520 case f_bogus:
1521 /* Should not be possible. */
1522 abort ();
1523 case f_simple:
1524 /* A simple variable definition "var := value". Expand the value.
1525 We have to allocate memory since otherwise it'll clobber the
1526 variable buffer, and we may still need that if we're looking at a
1527 target-specific variable. */
1528 p = alloc_value = allocated_variable_expand (value);
1529 break;
1530 case f_conditional:
1531 /* A conditional variable definition "var ?= value".
1532 The value is set IFF the variable is not defined yet. */
1533 v = lookup_variable (varname, varname_len);
1534 if (v)
1535 return v;
1536
1537 conditional = 1;
1538 flavor = f_recursive;
1539 /* FALLTHROUGH */
1540 case f_recursive:
1541 /* A recursive variable definition "var = value".
1542 The value is used verbatim. */
1543 p = value;
1544 break;
1545#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1546 case f_append:
1547 case f_prepend:
1548 {
1549 const enum variable_flavor org_flavor = flavor;
1550#else
1551 case f_append:
1552 {
1553#endif
1554
1555#ifdef CONFIG_WITH_LOCAL_VARIABLES
1556 /* If we have += but we're in a target or local variable context,
1557 we want to append only with other variables in the context of
1558 this target. */
1559 if (target_var || origin == o_local)
1560#else
1561 /* If we have += but we're in a target variable context, we want to
1562 append only with other variables in the context of this target. */
1563 if (target_var)
1564#endif
1565 {
1566 append = 1;
1567 v = lookup_variable_in_set (varname, varname_len,
1568 current_variable_set_list->set);
1569
1570 /* Don't append from the global set if a previous non-appending
1571 target-specific variable definition exists. */
1572 if (v && !v->append)
1573 append = 0;
1574 }
1575 else
1576 v = lookup_variable (varname, varname_len);
1577
1578 if (v == 0)
1579 {
1580 /* There was no old value.
1581 This becomes a normal recursive definition. */
1582 p = value;
1583 flavor = f_recursive;
1584 }
1585 else
1586 {
1587#ifdef CONFIG_WITH_VALUE_LENGTH
1588 v->append = append;
1589# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1590 return do_variable_definition_append (flocp, v, value, origin, org_flavor == f_append);
1591# else
1592 return do_variable_definition_append (flocp, v, value, origin, 1);
1593# endif
1594#else /* !CONFIG_WITH_VALUE_LENGTH */
1595
1596 /* Paste the old and new values together in VALUE. */
1597
1598 unsigned int oldlen, vallen;
1599 const char *val;
1600 char *tp;
1601
1602 val = value;
1603 if (v->recursive)
1604 /* The previous definition of the variable was recursive.
1605 The new value is the unexpanded old and new values. */
1606 flavor = f_recursive;
1607 else
1608 /* The previous definition of the variable was simple.
1609 The new value comes from the old value, which was expanded
1610 when it was set; and from the expanded new value. Allocate
1611 memory for the expansion as we may still need the rest of the
1612 buffer if we're looking at a target-specific variable. */
1613 val = alloc_value = allocated_variable_expand (val);
1614
1615 oldlen = strlen (v->value);
1616 vallen = strlen (val);
1617 tp = alloca (oldlen + 1 + vallen + 1);
1618# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1619 if (org_flavor == f_prepend)
1620 {
1621 memcpy (tp, val, vallen);
1622 tp[oldlen] = ' ';
1623 memcpy (&tp[oldlen + 1], v->value, oldlen + 1);
1624 }
1625 else
1626# endif /* CONFIG_WITH_PREPEND_ASSIGNMENT */
1627 {
1628 memcpy (tp, v->value, oldlen);
1629 tp[oldlen] = ' ';
1630 memcpy (&tp[oldlen + 1], val, vallen + 1);
1631 }
1632 p = tp;
1633#endif /* !CONFIG_WITH_VALUE_LENGTH */
1634 }
1635 }
1636 }
1637
1638#ifdef __MSDOS__
1639 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1640 non-Unix systems don't conform to this default configuration (in
1641 fact, most of them don't even have `/bin'). On the other hand,
1642 $SHELL in the environment, if set, points to the real pathname of
1643 the shell.
1644 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1645 the Makefile override $SHELL from the environment. But first, we
1646 look for the basename of the shell in the directory where SHELL=
1647 points, and along the $PATH; if it is found in any of these places,
1648 we define $SHELL to be the actual pathname of the shell. Thus, if
1649 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1650 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1651 defining SHELL to be "d:/unix/bash.exe". */
1652 if ((origin == o_file || origin == o_override)
1653 && strcmp (varname, "SHELL") == 0)
1654 {
1655 PATH_VAR (shellpath);
1656 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1657
1658 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1659 if (__dosexec_find_on_path (p, NULL, shellpath))
1660 {
1661 char *tp;
1662
1663 for (tp = shellpath; *tp; tp++)
1664 if (*tp == '\\')
1665 *tp = '/';
1666
1667 v = define_variable_loc (varname, varname_len,
1668 shellpath, origin, flavor == f_recursive,
1669 flocp);
1670 }
1671 else
1672 {
1673 const char *shellbase, *bslash;
1674 struct variable *pathv = lookup_variable ("PATH", 4);
1675 char *path_string;
1676 char *fake_env[2];
1677 size_t pathlen = 0;
1678
1679 shellbase = strrchr (p, '/');
1680 bslash = strrchr (p, '\\');
1681 if (!shellbase || bslash > shellbase)
1682 shellbase = bslash;
1683 if (!shellbase && p[1] == ':')
1684 shellbase = p + 1;
1685 if (shellbase)
1686 shellbase++;
1687 else
1688 shellbase = p;
1689
1690 /* Search for the basename of the shell (with standard
1691 executable extensions) along the $PATH. */
1692 if (pathv)
1693 pathlen = strlen (pathv->value);
1694 path_string = xmalloc (5 + pathlen + 2 + 1);
1695 /* On MSDOS, current directory is considered as part of $PATH. */
1696 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1697 fake_env[0] = path_string;
1698 fake_env[1] = 0;
1699 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1700 {
1701 char *tp;
1702
1703 for (tp = shellpath; *tp; tp++)
1704 if (*tp == '\\')
1705 *tp = '/';
1706
1707 v = define_variable_loc (varname, varname_len,
1708 shellpath, origin,
1709 flavor == f_recursive, flocp);
1710 }
1711 else
1712 v = lookup_variable (varname, varname_len);
1713
1714 free (path_string);
1715 }
1716 }
1717 else
1718#endif /* __MSDOS__ */
1719#ifdef WINDOWS32
1720 if ( varname_len == sizeof("SHELL") - 1 /* bird */
1721 && (origin == o_file || origin == o_override || origin == o_command)
1722 && streq (varname, "SHELL"))
1723 {
1724 extern char *default_shell;
1725
1726 /* Call shell locator function. If it returns TRUE, then
1727 set no_default_sh_exe to indicate sh was found and
1728 set new value for SHELL variable. */
1729
1730 if (find_and_set_default_shell (p))
1731 {
1732 v = define_variable_in_set (varname, varname_len, default_shell,
1733# ifdef CONFIG_WITH_VALUE_LENGTH
1734 ~0U, 1 /* duplicate_value */,
1735# endif
1736 origin, flavor == f_recursive,
1737 (target_var
1738 ? current_variable_set_list->set
1739 : NULL),
1740 flocp);
1741 no_default_sh_exe = 0;
1742 }
1743 else
1744 v = lookup_variable (varname, varname_len);
1745 }
1746 else
1747#endif
1748
1749 /* If we are defining variables inside an $(eval ...), we might have a
1750 different variable context pushed, not the global context (maybe we're
1751 inside a $(call ...) or something. Since this function is only ever
1752 invoked in places where we want to define globally visible variables,
1753 make sure we define this variable in the global set. */
1754
1755 v = define_variable_in_set (varname, varname_len, p,
1756#ifdef CONFIG_WITH_VALUE_LENGTH
1757 value_len, !alloc_value,
1758#endif
1759 origin, flavor == f_recursive,
1760#ifdef CONFIG_WITH_LOCAL_VARIABLES
1761 (target_var || origin == o_local
1762#else
1763 (target_var
1764#endif
1765 ? current_variable_set_list->set : NULL),
1766 flocp);
1767 v->append = append;
1768 v->conditional = conditional;
1769
1770#ifndef CONFIG_WITH_VALUE_LENGTH
1771 if (alloc_value)
1772 free (alloc_value);
1773#endif
1774
1775 return v;
1776}
1777
1778
1779/* Try to interpret LINE (a null-terminated string) as a variable definition.
1780
1781 ORIGIN may be o_file, o_override, o_env, o_env_override,
1782 or o_command specifying that the variable definition comes
1783 from a makefile, an override directive, the environment with
1784 or without the -e switch, or the command line.
1785
1786 See the comments for parse_variable_definition().
1787
1788 If LINE was recognized as a variable definition, a pointer to its `struct
1789 variable' is returned. If LINE is not a variable definition, NULL is
1790 returned. */
1791
1792struct variable *
1793parse_variable_definition (struct variable *v, char *line)
1794{
1795 register int c;
1796 register char *p = line;
1797 register char *beg;
1798 register char *end;
1799 enum variable_flavor flavor = f_bogus;
1800 char *name;
1801
1802 while (1)
1803 {
1804 c = *p++;
1805 if (c == '\0' || c == '#')
1806 return 0;
1807 if (c == '=')
1808 {
1809 end = p - 1;
1810 flavor = f_recursive;
1811 break;
1812 }
1813 else if (c == ':')
1814 if (*p == '=')
1815 {
1816 end = p++ - 1;
1817 flavor = f_simple;
1818 break;
1819 }
1820 else
1821 /* A colon other than := is a rule line, not a variable defn. */
1822 return 0;
1823 else if (c == '+' && *p == '=')
1824 {
1825 end = p++ - 1;
1826 flavor = f_append;
1827 break;
1828 }
1829#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1830 else if (c == '<' && *p == '=')
1831 {
1832 end = p++ - 1;
1833 flavor = f_prepend;
1834 break;
1835 }
1836#endif
1837 else if (c == '?' && *p == '=')
1838 {
1839 end = p++ - 1;
1840 flavor = f_conditional;
1841 break;
1842 }
1843 else if (c == '$')
1844 {
1845 /* This might begin a variable expansion reference. Make sure we
1846 don't misrecognize chars inside the reference as =, := or +=. */
1847 char closeparen;
1848 int count;
1849 c = *p++;
1850 if (c == '(')
1851 closeparen = ')';
1852 else if (c == '{')
1853 closeparen = '}';
1854 else
1855 continue; /* Nope. */
1856
1857 /* P now points past the opening paren or brace.
1858 Count parens or braces until it is matched. */
1859 count = 0;
1860 for (; *p != '\0'; ++p)
1861 {
1862 if (*p == c)
1863 ++count;
1864 else if (*p == closeparen && --count < 0)
1865 {
1866 ++p;
1867 break;
1868 }
1869 }
1870 }
1871 }
1872 v->flavor = flavor;
1873
1874 beg = next_token (line);
1875 while (end > beg && isblank ((unsigned char)end[-1]))
1876 --end;
1877 p = next_token (p);
1878 v->value = p;
1879#ifdef CONFIG_WITH_VALUE_LENGTH
1880 v->value_length = v->value_alloc_len = -1;
1881#endif
1882
1883 /* Expand the name, so "$(foo)bar = baz" works. */
1884 name = alloca (end - beg + 1);
1885 memcpy (name, beg, end - beg);
1886 name[end - beg] = '\0';
1887 v->name = allocated_variable_expand (name);
1888
1889 if (v->name[0] == '\0')
1890 fatal (&v->fileinfo, _("empty variable name"));
1891
1892 return v;
1893}
1894
1895
1896/* Try to interpret LINE (a null-terminated string) as a variable definition.
1897
1898 ORIGIN may be o_file, o_override, o_env, o_env_override, o_local,
1899 or o_command specifying that the variable definition comes
1900 from a makefile, an override directive, the environment with
1901 or without the -e switch, or the command line.
1902
1903 See the comments for parse_variable_definition().
1904
1905 If LINE was recognized as a variable definition, a pointer to its `struct
1906 variable' is returned. If LINE is not a variable definition, NULL is
1907 returned. */
1908
1909struct variable *
1910try_variable_definition (const struct floc *flocp, char *line,
1911 enum variable_origin origin, int target_var)
1912{
1913 struct variable v;
1914 struct variable *vp;
1915
1916 if (flocp != 0)
1917 v.fileinfo = *flocp;
1918 else
1919 v.fileinfo.filenm = 0;
1920
1921 if (!parse_variable_definition (&v, line))
1922 return 0;
1923
1924 vp = do_variable_definition (flocp, v.name, v.value,
1925 origin, v.flavor, target_var);
1926
1927 free (v.name);
1928
1929 return vp;
1930}
1931
1932
1933/* Print information for variable V, prefixing it with PREFIX. */
1934
1935static void
1936print_variable (const void *item, void *arg)
1937{
1938 const struct variable *v = item;
1939 const char *prefix = arg;
1940 const char *origin;
1941
1942 switch (v->origin)
1943 {
1944 case o_default:
1945 origin = _("default");
1946 break;
1947 case o_env:
1948 origin = _("environment");
1949 break;
1950 case o_file:
1951 origin = _("makefile");
1952 break;
1953 case o_env_override:
1954 origin = _("environment under -e");
1955 break;
1956 case o_command:
1957 origin = _("command line");
1958 break;
1959 case o_override:
1960 origin = _("`override' directive");
1961 break;
1962 case o_automatic:
1963 origin = _("automatic");
1964 break;
1965#ifdef CONFIG_WITH_LOCAL_VARIABLES
1966 case o_local:
1967 origin = _("`local' directive");
1968 break;
1969#endif
1970 case o_invalid:
1971 default:
1972 abort ();
1973 }
1974 fputs ("# ", stdout);
1975 fputs (origin, stdout);
1976 if (v->fileinfo.filenm)
1977 printf (_(" (from `%s', line %lu)"),
1978 v->fileinfo.filenm, v->fileinfo.lineno);
1979 putchar ('\n');
1980 fputs (prefix, stdout);
1981
1982 /* Is this a `define'? */
1983 if (v->recursive && strchr (v->value, '\n') != 0)
1984 printf ("define %s\n%s\nendef\n", v->name, v->value);
1985 else
1986 {
1987 register char *p;
1988
1989 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1990
1991 /* Check if the value is just whitespace. */
1992 p = next_token (v->value);
1993 if (p != v->value && *p == '\0')
1994 /* All whitespace. */
1995 printf ("$(subst ,,%s)", v->value);
1996 else if (v->recursive)
1997 fputs (v->value, stdout);
1998 else
1999 /* Double up dollar signs. */
2000 for (p = v->value; *p != '\0'; ++p)
2001 {
2002 if (*p == '$')
2003 putchar ('$');
2004 putchar (*p);
2005 }
2006 putchar ('\n');
2007 }
2008}
2009
2010
2011/* Print all the variables in SET. PREFIX is printed before
2012 the actual variable definitions (everything else is comments). */
2013
2014void
2015print_variable_set (struct variable_set *set, char *prefix)
2016{
2017 hash_map_arg (&set->table, print_variable, prefix);
2018
2019 fputs (_("# variable set hash-table stats:\n"), stdout);
2020 fputs ("# ", stdout);
2021 hash_print_stats (&set->table, stdout);
2022 putc ('\n', stdout);
2023}
2024
2025/* Print the data base of variables. */
2026
2027void
2028print_variable_data_base (void)
2029{
2030 puts (_("\n# Variables\n"));
2031
2032 print_variable_set (&global_variable_set, "");
2033
2034 puts (_("\n# Pattern-specific Variable Values"));
2035
2036 {
2037 struct pattern_var *p;
2038 int rules = 0;
2039
2040 for (p = pattern_vars; p != 0; p = p->next)
2041 {
2042 ++rules;
2043 printf ("\n%s :\n", p->target);
2044 print_variable (&p->variable, "# ");
2045 }
2046
2047 if (rules == 0)
2048 puts (_("\n# No pattern-specific variable values."));
2049 else
2050 printf (_("\n# %u pattern-specific variable values"), rules);
2051 }
2052}
2053
2054
2055/* Print all the local variables of FILE. */
2056
2057void
2058print_file_variables (const struct file *file)
2059{
2060 if (file->variables != 0)
2061 print_variable_set (file->variables->set, "# ");
2062}
2063
2064#ifdef WINDOWS32
2065void
2066sync_Path_environment (void)
2067{
2068 char *path = allocated_variable_expand ("$(PATH)");
2069 static char *environ_path = NULL;
2070
2071 if (!path)
2072 return;
2073
2074 /*
2075 * If done this before, don't leak memory unnecessarily.
2076 * Free the previous entry before allocating new one.
2077 */
2078 if (environ_path)
2079 free (environ_path);
2080
2081 /*
2082 * Create something WINDOWS32 world can grok
2083 */
2084 convert_Path_to_windows32 (path, ';');
2085 environ_path = xstrdup (concat ("PATH", "=", path));
2086 putenv (environ_path);
2087 free (path);
2088}
2089#endif
Note: See TracBrowser for help on using the repository browser.

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