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

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

Cleaning up the modifications. Changes are now either configurable or marked, and dead stuff has been removed (dll shell).

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