VirtualBox

source: kBuild/trunk/src/gmake/variable.c@ 503

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

Untested merge with GNU Make v3.81 (vendor/gnumake/2005-05-16 -> vendor/gnumake/current).

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

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