source: trunk/src/gmake/variable.c@ 530

Last change on this file since 530 was 530, checked in by bird, 19 years ago

Added kBuild specific functions for speeding up source processing.

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