source: trunk/src/kmk/variable.c@ 1334

Last change on this file since 1334 was 1334, checked in by bird, 18 years ago

fixed warnings.

  • Property svn:eol-style set to native
File size: 61.4 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 char buf[200];
976#ifdef KMK
977 const char *envvar;
978#endif
979
980 sprintf (buf, "%u", makelevel);
981 (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
982
983 sprintf (buf, "%s%s%s",
984 version_string,
985 (remote_description == 0 || remote_description[0] == '\0')
986 ? "" : "-",
987 (remote_description == 0 || remote_description[0] == '\0')
988 ? "" : remote_description);
989 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
990
991#ifdef KMK
992 /* Define KMK_VERSION to indicate kMk. */
993 (void) define_variable ("KMK_VERSION", 11, buf, o_default, 0);
994
995 /* Define KBUILD_VERSION* */
996 sprintf (buf, "%d", KBUILD_VERSION_MAJOR);
997 define_variable ("KBUILD_VERSION_MAJOR", sizeof ("KBUILD_VERSION_MAJOR") - 1,
998 buf, o_default, 0);
999 sprintf (buf, "%d", KBUILD_VERSION_MINOR);
1000 define_variable ("KBUILD_VERSION_MINOR", sizeof("KBUILD_VERSION_MINOR") - 1,
1001 buf, o_default, 0);
1002 sprintf (buf, "%d", KBUILD_VERSION_PATCH);
1003 define_variable ("KBUILD_VERSION_PATCH", sizeof ("KBUILD_VERSION_PATCH") - 1,
1004 buf, o_default, 0);
1005
1006 sprintf (buf, "%d.%d.%d", KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR, KBUILD_VERSION_PATCH);
1007 define_variable ("KBUILD_VERSION", sizeof ("KBUILD_VERSION") - 1,
1008 buf, o_default, 0);
1009
1010 /* The build platform defaults. */
1011 envvar = getenv ("BUILD_PLATFORM");
1012 if (!envvar)
1013 define_variable ("BUILD_PLATFORM", sizeof ("BUILD_PLATFORM") - 1,
1014 BUILD_PLATFORM, o_default, 0);
1015 envvar = getenv ("BUILD_PLATFORM_ARCH");
1016 if (!envvar)
1017 define_variable ("BUILD_PLATFORM_ARCH", sizeof ("BUILD_PLATFORM_ARCH") - 1,
1018 BUILD_PLATFORM_ARCH, o_default, 0);
1019 envvar = getenv ("BUILD_PLATFORM_CPU");
1020 if (!envvar)
1021 define_variable ("BUILD_PLATFORM_CPU", sizeof ("BUILD_PLATFORM_CPU") - 1,
1022 BUILD_PLATFORM_CPU, o_default, 0);
1023
1024 /* The kBuild locations. */
1025 define_variable ("PATH_KBUILD", sizeof ("PATH_KBUILD") - 1,
1026 get_path_kbuild (), o_default, 0);
1027 define_variable ("PATH_KBUILD_BIN", sizeof ("PATH_KBUILD_BIN") - 1,
1028 get_path_kbuild_bin (), o_default, 0);
1029
1030 /* Define KMK_FEATURES to indicate various working KMK features. */
1031# if defined (CONFIG_WITH_RSORT) \
1032 && defined (CONFIG_WITH_ABSPATHEX) \
1033 && defined (CONFIG_WITH_TOUPPER_TOLOWER) \
1034 && defined (CONFIG_WITH_VALUE_LENGTH) && defined (CONFIG_WITH_COMPARE) \
1035 && defined (CONFIG_WITH_STACK) \
1036 && defined (CONFIG_WITH_MATH) \
1037 && defined (CONFIG_WITH_XARGS) \
1038 && defined (CONFIG_WITH_EXPLICIT_MULTITARGET) \
1039 && defined (CONFIG_WITH_PREPEND_ASSIGNMENT) \
1040 && defined (CONFIG_WITH_SET_CONDITIONALS) \
1041 && defined (CONFIG_WITH_DATE) \
1042 && defined (CONFIG_WITH_FILE_SIZE) \
1043 && defined (CONFIG_WITH_WHICH) \
1044 && defined (KMK_HELPERS)
1045 (void) define_variable ("KMK_FEATURES", 12,
1046 "append-dash-n abspath"
1047 " rsort"
1048 " abspathex"
1049 " toupper tolower"
1050 " comp-vars comp-cmds"
1051 " stack"
1052 " math-int"
1053 " xargs"
1054 " explicit-multitarget"
1055 " prepend-assignment"
1056 " set-conditionals"
1057 " date"
1058 " file-size"
1059 " which"
1060 " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one "
1061 , o_default, 0);
1062# else /* MSC can't deal with strings mixed with #if/#endif, thus the slow way. */
1063# error "All features should be enabled by default!"
1064 strcpy (buf, "append-dash-n abspath");
1065# if defined (CONFIG_WITH_RSORT)
1066 strcat (buf, " rsort");
1067# endif
1068# if defined (CONFIG_WITH_ABSPATHEX)
1069 strcat (buf, " abspathex");
1070# endif
1071# if defined (CONFIG_WITH_TOUPPER_TOLOWER)
1072 strcat (buf, " toupper tolower");
1073# endif
1074# if defined (CONFIG_WITH_VALUE_LENGTH) && defined(CONFIG_WITH_COMPARE)
1075 strcat (buf, " comp-vars comp-cmds");
1076# endif
1077# if defined (CONFIG_WITH_STACK)
1078 strcat (buf, " stack");
1079# endif
1080# if defined (CONFIG_WITH_MATH)
1081 strcat (buf, " math-int");
1082# endif
1083# if defined (CONFIG_WITH_XARGS)
1084 strcat (buf, " xargs");
1085# endif
1086# if defined (CONFIG_WITH_EXPLICIT_MULTITARGET)
1087 strcat (buf, " explicit-multitarget");
1088# endif
1089# if defined (CONFIG_WITH_PREPEND_ASSIGNMENT)
1090 strcat (buf, " prepend-assignment");
1091# endif
1092# if defined (CONFIG_WITH_SET_CONDITIONALS)
1093 strcat (buf, " set-conditionals");
1094# endif
1095# if defined (CONFIG_WITH_DATE)
1096 strcat (buf, " date");
1097# endif
1098# if defined (CONFIG_WITH_FILE_SIZE)
1099 strcat (buf, " file-size");
1100# endif
1101# if defined (CONFIG_WITH_WHICH)
1102 strcat (buf, " which");
1103# endif
1104# if defined (KMK_HELPERS)
1105 strcat (buf, " kb-src-tool kb-obj-base kb-obj-suff kb-src-prop kb-src-one");
1106# endif
1107 (void) define_variable ("KMK_FEATURES", 12, buf, o_default, 0);
1108# endif
1109
1110#endif /* KMK */
1111
1112#ifdef CONFIG_WITH_KMK_BUILTIN
1113 /* The supported kMk Builtin commands. */
1114 (void) define_variable ("KMK_BUILTIN", 11, "append cat cp cmp echo install kDepIDB ln md5sum mkdir mv printf rm rmdir test", o_default, 0);
1115#endif
1116
1117#ifdef __MSDOS__
1118 /* Allow to specify a special shell just for Make,
1119 and use $COMSPEC as the default $SHELL when appropriate. */
1120 {
1121 static char shell_str[] = "SHELL";
1122 const int shlen = sizeof (shell_str) - 1;
1123 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
1124 struct variable *comp = lookup_variable ("COMSPEC", 7);
1125
1126 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
1127 if (mshp)
1128 (void) define_variable (shell_str, shlen,
1129 mshp->value, o_env_override, 0);
1130 else if (comp)
1131 {
1132 /* $COMSPEC shouldn't override $SHELL. */
1133 struct variable *shp = lookup_variable (shell_str, shlen);
1134
1135 if (!shp)
1136 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
1137 }
1138 }
1139#elif defined(__EMX__)
1140 {
1141 static char shell_str[] = "SHELL";
1142 const int shlen = sizeof (shell_str) - 1;
1143 struct variable *shell = lookup_variable (shell_str, shlen);
1144 struct variable *replace = lookup_variable ("MAKESHELL", 9);
1145
1146 /* if $MAKESHELL is defined in the environment assume o_env_override */
1147 if (replace && *replace->value && replace->origin == o_env)
1148 replace->origin = o_env_override;
1149
1150 /* if $MAKESHELL is not defined use $SHELL but only if the variable
1151 did not come from the environment */
1152 if (!replace || !*replace->value)
1153 if (shell && *shell->value && (shell->origin == o_env
1154 || shell->origin == o_env_override))
1155 {
1156 /* overwrite whatever we got from the environment */
1157 free(shell->value);
1158 shell->value = xstrdup (default_shell);
1159 shell->origin = o_default;
1160 }
1161
1162 /* Some people do not like cmd to be used as the default
1163 if $SHELL is not defined in the Makefile.
1164 With -DNO_CMD_DEFAULT you can turn off this behaviour */
1165# ifndef NO_CMD_DEFAULT
1166 /* otherwise use $COMSPEC */
1167 if (!replace || !*replace->value)
1168 replace = lookup_variable ("COMSPEC", 7);
1169
1170 /* otherwise use $OS2_SHELL */
1171 if (!replace || !*replace->value)
1172 replace = lookup_variable ("OS2_SHELL", 9);
1173# else
1174# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
1175# endif
1176
1177 if (replace && *replace->value)
1178 /* overwrite $SHELL */
1179 (void) define_variable (shell_str, shlen, replace->value,
1180 replace->origin, 0);
1181 else
1182 /* provide a definition if there is none */
1183 (void) define_variable (shell_str, shlen, default_shell,
1184 o_default, 0);
1185 }
1186
1187#endif
1188
1189 /* This won't override any definition, but it will provide one if there
1190 isn't one there. */
1191 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
1192
1193 /* On MSDOS we do use SHELL from environment, since it isn't a standard
1194 environment variable on MSDOS, so whoever sets it, does that on purpose.
1195 On OS/2 we do not use SHELL from environment but we have already handled
1196 that problem above. */
1197#if !defined(__MSDOS__) && !defined(__EMX__)
1198 /* Don't let SHELL come from the environment. */
1199 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
1200 {
1201 free (v->value);
1202 v->origin = o_file;
1203 v->value = xstrdup (default_shell);
1204#ifdef CONFIG_WITH_VALUE_LENGTH
1205 v->value_length = strlen (v->value);
1206 v->value_alloc_len = v->value_length + 1;
1207#endif
1208 }
1209#endif
1210
1211 /* Make sure MAKEFILES gets exported if it is set. */
1212 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
1213 v->export = v_ifset;
1214
1215 /* Define the magic D and F variables in terms of
1216 the automatic variables they are variations of. */
1217
1218#ifdef VMS
1219 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
1220 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
1221 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
1222 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
1223 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
1224 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
1225 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
1226#else
1227 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
1228 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
1229 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
1230 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
1231 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
1232 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
1233 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
1234#endif
1235 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
1236 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
1237 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
1238 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
1239 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
1240 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
1241 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
1242}
1243
1244
1245int export_all_variables;
1246
1247/* Create a new environment for FILE's commands.
1248 If FILE is nil, this is for the `shell' function.
1249 The child's MAKELEVEL variable is incremented. */
1250
1251char **
1252target_environment (struct file *file)
1253{
1254 struct variable_set_list *set_list;
1255 register struct variable_set_list *s;
1256 struct hash_table table;
1257 struct variable **v_slot;
1258 struct variable **v_end;
1259 struct variable makelevel_key;
1260 char **result_0;
1261 char **result;
1262
1263 if (file == 0)
1264 set_list = current_variable_set_list;
1265 else
1266 set_list = file->variables;
1267
1268 hash_init (&table, VARIABLE_BUCKETS,
1269 variable_hash_1, variable_hash_2, variable_hash_cmp);
1270
1271 /* Run through all the variable sets in the list,
1272 accumulating variables in TABLE. */
1273 for (s = set_list; s != 0; s = s->next)
1274 {
1275 struct variable_set *set = s->set;
1276 v_slot = (struct variable **) set->table.ht_vec;
1277 v_end = v_slot + set->table.ht_size;
1278 for ( ; v_slot < v_end; v_slot++)
1279 if (! HASH_VACANT (*v_slot))
1280 {
1281 struct variable **new_slot;
1282 struct variable *v = *v_slot;
1283
1284 /* If this is a per-target variable and it hasn't been touched
1285 already then look up the global version and take its export
1286 value. */
1287 if (v->per_target && v->export == v_default)
1288 {
1289 struct variable *gv;
1290
1291 gv = lookup_variable_in_set (v->name, strlen(v->name),
1292 &global_variable_set);
1293 if (gv)
1294 v->export = gv->export;
1295 }
1296
1297 switch (v->export)
1298 {
1299 case v_default:
1300 if (v->origin == o_default || v->origin == o_automatic)
1301 /* Only export default variables by explicit request. */
1302 continue;
1303
1304 /* The variable doesn't have a name that can be exported. */
1305 if (! v->exportable)
1306 continue;
1307
1308 if (! export_all_variables
1309 && v->origin != o_command
1310 && v->origin != o_env && v->origin != o_env_override)
1311 continue;
1312 break;
1313
1314 case v_export:
1315 break;
1316
1317 case v_noexport:
1318 /* If this is the SHELL variable and it's not exported, then
1319 add the value from our original environment. */
1320 if (streq (v->name, "SHELL"))
1321 {
1322 extern struct variable shell_var;
1323 v = &shell_var;
1324 break;
1325 }
1326 continue;
1327
1328 case v_ifset:
1329 if (v->origin == o_default)
1330 continue;
1331 break;
1332 }
1333
1334 new_slot = (struct variable **) hash_find_slot (&table, v);
1335 if (HASH_VACANT (*new_slot))
1336 hash_insert_at (&table, v, new_slot);
1337 }
1338 }
1339
1340 makelevel_key.name = MAKELEVEL_NAME;
1341 makelevel_key.length = MAKELEVEL_LENGTH;
1342#ifdef VARIABLE_HASH /* bird */
1343 makelevel_key.hash1 = variable_hash_1i (MAKELEVEL_NAME, MAKELEVEL_LENGTH);
1344 makelevel_key.hash2 = 0;
1345#endif
1346 hash_delete (&table, &makelevel_key);
1347
1348 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
1349
1350 v_slot = (struct variable **) table.ht_vec;
1351 v_end = v_slot + table.ht_size;
1352 for ( ; v_slot < v_end; v_slot++)
1353 if (! HASH_VACANT (*v_slot))
1354 {
1355 struct variable *v = *v_slot;
1356
1357 /* If V is recursively expanded and didn't come from the environment,
1358 expand its value. If it came from the environment, it should
1359 go back into the environment unchanged. */
1360 if (v->recursive
1361 && v->origin != o_env && v->origin != o_env_override)
1362 {
1363 char *value = recursively_expand_for_file (v, file);
1364#ifdef WINDOWS32
1365 if (strcmp(v->name, "Path") == 0 ||
1366 strcmp(v->name, "PATH") == 0)
1367 convert_Path_to_windows32(value, ';');
1368#endif
1369 *result++ = xstrdup (concat (v->name, "=", value));
1370 free (value);
1371 }
1372 else
1373 {
1374#ifdef WINDOWS32
1375 if (strcmp(v->name, "Path") == 0 ||
1376 strcmp(v->name, "PATH") == 0)
1377 convert_Path_to_windows32(v->value, ';');
1378#endif
1379 *result++ = xstrdup (concat (v->name, "=", v->value));
1380 }
1381 }
1382
1383 *result = xmalloc (100);
1384 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
1385 *++result = 0;
1386
1387 hash_free (&table, 0);
1388
1389 return result_0;
1390}
1391
1392
1393#ifdef CONFIG_WITH_VALUE_LENGTH
1394static struct variable *
1395do_variable_definition_append (const struct floc *flocp, struct variable *v, const char *value,
1396 enum variable_origin origin, int append)
1397{
1398 if (env_overrides && origin == o_env)
1399 origin = o_env_override;
1400
1401 if (env_overrides && v->origin == o_env)
1402 /* V came from in the environment. Since it was defined
1403 before the switches were parsed, it wasn't affected by -e. */
1404 v->origin = o_env_override;
1405
1406 /* A variable of this name is already defined.
1407 If the old definition is from a stronger source
1408 than this one, don't redefine it. */
1409 if ((int) origin < (int) v->origin)
1410 return v;
1411 v->origin = origin;
1412
1413 /* location */
1414 if (flocp != 0)
1415 v->fileinfo = *flocp;
1416
1417 /* The juicy bits, append the specified value to the variable
1418 This is a heavily exercised code path in kBuild. */
1419 if (v->recursive)
1420 {
1421 /* The previous definition of the variable was recursive.
1422 The new value is the unexpanded old and new values. */
1423 unsigned int value_len = strlen (value);
1424 unsigned int new_value_len = value_len + (v->value_length != 0 ? 1 + v->value_length : 0);
1425 int done_1st_prepend_copy = 0;
1426
1427 /* adjust the size. */
1428 if ((unsigned)v->value_alloc_len <= new_value_len + 1)
1429 {
1430 v->value_alloc_len *= 2;
1431 if (v->value_alloc_len < new_value_len + 1)
1432 v->value_alloc_len = (new_value_len + 1 + value_len + 0x7f) + ~0x7fU;
1433 if (append || !v->value_length)
1434 v->value = xrealloc (v->value, v->value_alloc_len);
1435 else
1436 {
1437 /* avoid the extra memcpy the xrealloc may have to do */
1438 char *new_buf = xmalloc (v->value_alloc_len);
1439 memcpy (&new_buf[value_len + 1], v->value, v->value_length + 1);
1440 done_1st_prepend_copy = 1;
1441 free (v->value);
1442 v->value = new_buf;
1443 }
1444 }
1445
1446 /* insert the new bits */
1447 if (v->value_length != 0)
1448 {
1449 if (append)
1450 {
1451 v->value[v->value_length] = ' ';
1452 memcpy (&v->value[v->value_length + 1], value, value_len + 1);
1453 }
1454 else
1455 {
1456 if (!done_1st_prepend_copy)
1457 memmove (&v->value[value_len + 1], v->value, v->value_length + 1);
1458 v->value[value_len] = ' ';
1459 memcpy (v->value, value, value_len);
1460 }
1461 }
1462 else
1463 memcpy (v->value, value, value_len + 1);
1464 v->value_length = new_value_len;
1465 }
1466 else
1467 {
1468 /* The previous definition of the variable was simple.
1469 The new value comes from the old value, which was expanded
1470 when it was set; and from the expanded new value. */
1471 append_expanded_string_to_variable(v, value);
1472 }
1473
1474 /* update the variable */
1475 return v;
1476}
1477#endif /* CONFIG_WITH_VALUE_LENGTH */
1478
1479
1480/* Given a variable, a value, and a flavor, define the variable.
1481 See the try_variable_definition() function for details on the parameters. */
1482
1483struct variable *
1484do_variable_definition (const struct floc *flocp, const char *varname,
1485 const char *value, enum variable_origin origin,
1486 enum variable_flavor flavor, int target_var)
1487{
1488 const char *p;
1489 char *alloc_value = NULL;
1490 struct variable *v;
1491 int append = 0;
1492 int conditional = 0;
1493 const size_t varname_len = strlen (varname); /* bird */
1494#ifdef CONFIG_WITH_VALUE_LENGTH
1495 unsigned int value_len = ~0U;
1496#endif
1497
1498 /* Calculate the variable's new value in VALUE. */
1499
1500 switch (flavor)
1501 {
1502 default:
1503 case f_bogus:
1504 /* Should not be possible. */
1505 abort ();
1506 case f_simple:
1507 /* A simple variable definition "var := value". Expand the value.
1508 We have to allocate memory since otherwise it'll clobber the
1509 variable buffer, and we may still need that if we're looking at a
1510 target-specific variable. */
1511 p = alloc_value = allocated_variable_expand (value);
1512 break;
1513 case f_conditional:
1514 /* A conditional variable definition "var ?= value".
1515 The value is set IFF the variable is not defined yet. */
1516 v = lookup_variable (varname, varname_len);
1517 if (v)
1518 return v;
1519
1520 conditional = 1;
1521 flavor = f_recursive;
1522 /* FALLTHROUGH */
1523 case f_recursive:
1524 /* A recursive variable definition "var = value".
1525 The value is used verbatim. */
1526 p = value;
1527 break;
1528#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1529 case f_append:
1530 case f_prepend:
1531 {
1532 const enum variable_flavor org_flavor = flavor;
1533#else
1534 case f_append:
1535 {
1536#endif
1537
1538 /* If we have += but we're in a target variable context, we want to
1539 append only with other variables in the context of this target. */
1540 if (target_var)
1541 {
1542 append = 1;
1543 v = lookup_variable_in_set (varname, varname_len,
1544 current_variable_set_list->set);
1545
1546 /* Don't append from the global set if a previous non-appending
1547 target-specific variable definition exists. */
1548 if (v && !v->append)
1549 append = 0;
1550 }
1551 else
1552 v = lookup_variable (varname, varname_len);
1553
1554 if (v == 0)
1555 {
1556 /* There was no old value.
1557 This becomes a normal recursive definition. */
1558 p = value;
1559 flavor = f_recursive;
1560 }
1561 else
1562 {
1563#ifdef CONFIG_WITH_VALUE_LENGTH
1564 v->append = append;
1565# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1566 return do_variable_definition_append (flocp, v, value, origin, org_flavor == f_append);
1567# else
1568 return do_variable_definition_append (flocp, v, value, origin, 1);
1569# endif
1570#else /* !CONFIG_WITH_VALUE_LENGTH */
1571
1572 /* Paste the old and new values together in VALUE. */
1573
1574 unsigned int oldlen, vallen;
1575 const char *val;
1576 char *tp;
1577
1578 val = value;
1579 if (v->recursive)
1580 /* The previous definition of the variable was recursive.
1581 The new value is the unexpanded old and new values. */
1582 flavor = f_recursive;
1583 else
1584 /* The previous definition of the variable was simple.
1585 The new value comes from the old value, which was expanded
1586 when it was set; and from the expanded new value. Allocate
1587 memory for the expansion as we may still need the rest of the
1588 buffer if we're looking at a target-specific variable. */
1589 val = alloc_value = allocated_variable_expand (val);
1590
1591 oldlen = strlen (v->value);
1592 vallen = strlen (val);
1593 tp = alloca (oldlen + 1 + vallen + 1);
1594# ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1595 if (org_flavor == f_prepend)
1596 {
1597 memcpy (tp, val, vallen);
1598 tp[oldlen] = ' ';
1599 memcpy (&tp[oldlen + 1], v->value, oldlen + 1);
1600 }
1601 else
1602# endif /* CONFIG_WITH_PREPEND_ASSIGNMENT */
1603 {
1604 memcpy (tp, v->value, oldlen);
1605 tp[oldlen] = ' ';
1606 memcpy (&tp[oldlen + 1], val, vallen + 1);
1607 }
1608 p = tp;
1609#endif /* !CONFIG_WITH_VALUE_LENGTH */
1610 }
1611 }
1612 }
1613
1614#ifdef __MSDOS__
1615 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1616 non-Unix systems don't conform to this default configuration (in
1617 fact, most of them don't even have `/bin'). On the other hand,
1618 $SHELL in the environment, if set, points to the real pathname of
1619 the shell.
1620 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1621 the Makefile override $SHELL from the environment. But first, we
1622 look for the basename of the shell in the directory where SHELL=
1623 points, and along the $PATH; if it is found in any of these places,
1624 we define $SHELL to be the actual pathname of the shell. Thus, if
1625 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1626 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1627 defining SHELL to be "d:/unix/bash.exe". */
1628 if ((origin == o_file || origin == o_override)
1629 && strcmp (varname, "SHELL") == 0)
1630 {
1631 PATH_VAR (shellpath);
1632 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1633
1634 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1635 if (__dosexec_find_on_path (p, NULL, shellpath))
1636 {
1637 char *tp;
1638
1639 for (tp = shellpath; *tp; tp++)
1640 if (*tp == '\\')
1641 *tp = '/';
1642
1643 v = define_variable_loc (varname, varname_len,
1644 shellpath, origin, flavor == f_recursive,
1645 flocp);
1646 }
1647 else
1648 {
1649 const char *shellbase, *bslash;
1650 struct variable *pathv = lookup_variable ("PATH", 4);
1651 char *path_string;
1652 char *fake_env[2];
1653 size_t pathlen = 0;
1654
1655 shellbase = strrchr (p, '/');
1656 bslash = strrchr (p, '\\');
1657 if (!shellbase || bslash > shellbase)
1658 shellbase = bslash;
1659 if (!shellbase && p[1] == ':')
1660 shellbase = p + 1;
1661 if (shellbase)
1662 shellbase++;
1663 else
1664 shellbase = p;
1665
1666 /* Search for the basename of the shell (with standard
1667 executable extensions) along the $PATH. */
1668 if (pathv)
1669 pathlen = strlen (pathv->value);
1670 path_string = xmalloc (5 + pathlen + 2 + 1);
1671 /* On MSDOS, current directory is considered as part of $PATH. */
1672 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1673 fake_env[0] = path_string;
1674 fake_env[1] = 0;
1675 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1676 {
1677 char *tp;
1678
1679 for (tp = shellpath; *tp; tp++)
1680 if (*tp == '\\')
1681 *tp = '/';
1682
1683 v = define_variable_loc (varname, varname_len,
1684 shellpath, origin,
1685 flavor == f_recursive, flocp);
1686 }
1687 else
1688 v = lookup_variable (varname, varname_len);
1689
1690 free (path_string);
1691 }
1692 }
1693 else
1694#endif /* __MSDOS__ */
1695#ifdef WINDOWS32
1696 if ( varname_len == sizeof("SHELL") - 1 /* bird */
1697 && (origin == o_file || origin == o_override || origin == o_command)
1698 && streq (varname, "SHELL"))
1699 {
1700 extern char *default_shell;
1701
1702 /* Call shell locator function. If it returns TRUE, then
1703 set no_default_sh_exe to indicate sh was found and
1704 set new value for SHELL variable. */
1705
1706 if (find_and_set_default_shell (p))
1707 {
1708 v = define_variable_in_set (varname, varname_len, default_shell,
1709# ifdef CONFIG_WITH_VALUE_LENGTH
1710 ~0U, 1 /* duplicate_value */,
1711# endif
1712 origin, flavor == f_recursive,
1713 (target_var
1714 ? current_variable_set_list->set
1715 : NULL),
1716 flocp);
1717 no_default_sh_exe = 0;
1718 }
1719 else
1720 v = lookup_variable (varname, varname_len);
1721 }
1722 else
1723#endif
1724
1725 /* If we are defining variables inside an $(eval ...), we might have a
1726 different variable context pushed, not the global context (maybe we're
1727 inside a $(call ...) or something. Since this function is only ever
1728 invoked in places where we want to define globally visible variables,
1729 make sure we define this variable in the global set. */
1730
1731 v = define_variable_in_set (varname, varname_len, p,
1732#ifdef CONFIG_WITH_VALUE_LENGTH
1733 value_len, !alloc_value,
1734#endif
1735 origin, flavor == f_recursive,
1736 (target_var
1737 ? current_variable_set_list->set : NULL),
1738 flocp);
1739 v->append = append;
1740 v->conditional = conditional;
1741
1742#ifndef CONFIG_WITH_VALUE_LENGTH
1743 if (alloc_value)
1744 free (alloc_value);
1745#endif
1746
1747 return v;
1748}
1749
1750
1751/* Try to interpret LINE (a null-terminated string) as a variable definition.
1752
1753 ORIGIN may be o_file, o_override, o_env, o_env_override,
1754 or o_command specifying that the variable definition comes
1755 from a makefile, an override directive, the environment with
1756 or without the -e switch, or the command line.
1757
1758 See the comments for parse_variable_definition().
1759
1760 If LINE was recognized as a variable definition, a pointer to its `struct
1761 variable' is returned. If LINE is not a variable definition, NULL is
1762 returned. */
1763
1764struct variable *
1765parse_variable_definition (struct variable *v, char *line)
1766{
1767 register int c;
1768 register char *p = line;
1769 register char *beg;
1770 register char *end;
1771 enum variable_flavor flavor = f_bogus;
1772 char *name;
1773
1774 while (1)
1775 {
1776 c = *p++;
1777 if (c == '\0' || c == '#')
1778 return 0;
1779 if (c == '=')
1780 {
1781 end = p - 1;
1782 flavor = f_recursive;
1783 break;
1784 }
1785 else if (c == ':')
1786 if (*p == '=')
1787 {
1788 end = p++ - 1;
1789 flavor = f_simple;
1790 break;
1791 }
1792 else
1793 /* A colon other than := is a rule line, not a variable defn. */
1794 return 0;
1795 else if (c == '+' && *p == '=')
1796 {
1797 end = p++ - 1;
1798 flavor = f_append;
1799 break;
1800 }
1801#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
1802 else if (c == '<' && *p == '=')
1803 {
1804 end = p++ - 1;
1805 flavor = f_prepend;
1806 break;
1807 }
1808#endif
1809 else if (c == '?' && *p == '=')
1810 {
1811 end = p++ - 1;
1812 flavor = f_conditional;
1813 break;
1814 }
1815 else if (c == '$')
1816 {
1817 /* This might begin a variable expansion reference. Make sure we
1818 don't misrecognize chars inside the reference as =, := or +=. */
1819 char closeparen;
1820 int count;
1821 c = *p++;
1822 if (c == '(')
1823 closeparen = ')';
1824 else if (c == '{')
1825 closeparen = '}';
1826 else
1827 continue; /* Nope. */
1828
1829 /* P now points past the opening paren or brace.
1830 Count parens or braces until it is matched. */
1831 count = 0;
1832 for (; *p != '\0'; ++p)
1833 {
1834 if (*p == c)
1835 ++count;
1836 else if (*p == closeparen && --count < 0)
1837 {
1838 ++p;
1839 break;
1840 }
1841 }
1842 }
1843 }
1844 v->flavor = flavor;
1845
1846 beg = next_token (line);
1847 while (end > beg && isblank ((unsigned char)end[-1]))
1848 --end;
1849 p = next_token (p);
1850 v->value = p;
1851#ifdef CONFIG_WITH_VALUE_LENGTH
1852 v->value_length = v->value_alloc_len = -1;
1853#endif
1854
1855 /* Expand the name, so "$(foo)bar = baz" works. */
1856 name = alloca (end - beg + 1);
1857 memcpy (name, beg, end - beg);
1858 name[end - beg] = '\0';
1859 v->name = allocated_variable_expand (name);
1860
1861 if (v->name[0] == '\0')
1862 fatal (&v->fileinfo, _("empty variable name"));
1863
1864 return v;
1865}
1866
1867
1868/* Try to interpret LINE (a null-terminated string) as a variable definition.
1869
1870 ORIGIN may be o_file, o_override, o_env, o_env_override,
1871 or o_command specifying that the variable definition comes
1872 from a makefile, an override directive, the environment with
1873 or without the -e switch, or the command line.
1874
1875 See the comments for parse_variable_definition().
1876
1877 If LINE was recognized as a variable definition, a pointer to its `struct
1878 variable' is returned. If LINE is not a variable definition, NULL is
1879 returned. */
1880
1881struct variable *
1882try_variable_definition (const struct floc *flocp, char *line,
1883 enum variable_origin origin, int target_var)
1884{
1885 struct variable v;
1886 struct variable *vp;
1887
1888 if (flocp != 0)
1889 v.fileinfo = *flocp;
1890 else
1891 v.fileinfo.filenm = 0;
1892
1893 if (!parse_variable_definition (&v, line))
1894 return 0;
1895
1896 vp = do_variable_definition (flocp, v.name, v.value,
1897 origin, v.flavor, target_var);
1898
1899 free (v.name);
1900
1901 return vp;
1902}
1903
1904
1905/* Print information for variable V, prefixing it with PREFIX. */
1906
1907static void
1908print_variable (const void *item, void *arg)
1909{
1910 const struct variable *v = item;
1911 const char *prefix = arg;
1912 const char *origin;
1913
1914 switch (v->origin)
1915 {
1916 case o_default:
1917 origin = _("default");
1918 break;
1919 case o_env:
1920 origin = _("environment");
1921 break;
1922 case o_file:
1923 origin = _("makefile");
1924 break;
1925 case o_env_override:
1926 origin = _("environment under -e");
1927 break;
1928 case o_command:
1929 origin = _("command line");
1930 break;
1931 case o_override:
1932 origin = _("`override' directive");
1933 break;
1934 case o_automatic:
1935 origin = _("automatic");
1936 break;
1937 case o_invalid:
1938 default:
1939 abort ();
1940 }
1941 fputs ("# ", stdout);
1942 fputs (origin, stdout);
1943 if (v->fileinfo.filenm)
1944 printf (_(" (from `%s', line %lu)"),
1945 v->fileinfo.filenm, v->fileinfo.lineno);
1946 putchar ('\n');
1947 fputs (prefix, stdout);
1948
1949 /* Is this a `define'? */
1950 if (v->recursive && strchr (v->value, '\n') != 0)
1951 printf ("define %s\n%s\nendef\n", v->name, v->value);
1952 else
1953 {
1954 register char *p;
1955
1956 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1957
1958 /* Check if the value is just whitespace. */
1959 p = next_token (v->value);
1960 if (p != v->value && *p == '\0')
1961 /* All whitespace. */
1962 printf ("$(subst ,,%s)", v->value);
1963 else if (v->recursive)
1964 fputs (v->value, stdout);
1965 else
1966 /* Double up dollar signs. */
1967 for (p = v->value; *p != '\0'; ++p)
1968 {
1969 if (*p == '$')
1970 putchar ('$');
1971 putchar (*p);
1972 }
1973 putchar ('\n');
1974 }
1975}
1976
1977
1978/* Print all the variables in SET. PREFIX is printed before
1979 the actual variable definitions (everything else is comments). */
1980
1981void
1982print_variable_set (struct variable_set *set, char *prefix)
1983{
1984 hash_map_arg (&set->table, print_variable, prefix);
1985
1986 fputs (_("# variable set hash-table stats:\n"), stdout);
1987 fputs ("# ", stdout);
1988 hash_print_stats (&set->table, stdout);
1989 putc ('\n', stdout);
1990}
1991
1992/* Print the data base of variables. */
1993
1994void
1995print_variable_data_base (void)
1996{
1997 puts (_("\n# Variables\n"));
1998
1999 print_variable_set (&global_variable_set, "");
2000
2001 puts (_("\n# Pattern-specific Variable Values"));
2002
2003 {
2004 struct pattern_var *p;
2005 int rules = 0;
2006
2007 for (p = pattern_vars; p != 0; p = p->next)
2008 {
2009 ++rules;
2010 printf ("\n%s :\n", p->target);
2011 print_variable (&p->variable, "# ");
2012 }
2013
2014 if (rules == 0)
2015 puts (_("\n# No pattern-specific variable values."));
2016 else
2017 printf (_("\n# %u pattern-specific variable values"), rules);
2018 }
2019}
2020
2021
2022/* Print all the local variables of FILE. */
2023
2024void
2025print_file_variables (const struct file *file)
2026{
2027 if (file->variables != 0)
2028 print_variable_set (file->variables->set, "# ");
2029}
2030
2031#ifdef WINDOWS32
2032void
2033sync_Path_environment (void)
2034{
2035 char *path = allocated_variable_expand ("$(PATH)");
2036 static char *environ_path = NULL;
2037
2038 if (!path)
2039 return;
2040
2041 /*
2042 * If done this before, don't leak memory unnecessarily.
2043 * Free the previous entry before allocating new one.
2044 */
2045 if (environ_path)
2046 free (environ_path);
2047
2048 /*
2049 * Create something WINDOWS32 world can grok
2050 */
2051 convert_Path_to_windows32 (path, ';');
2052 environ_path = xstrdup (concat ("PATH", "=", path));
2053 putenv (environ_path);
2054 free (path);
2055}
2056#endif
Note: See TracBrowser for help on using the repository browser.