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

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

CONFIG_WITH_OPTIMIZATION_HACKS

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