VirtualBox

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

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

Made it build on windows.

  • Property svn:eol-style set to native
File size: 49.5 KB
Line 
1/* Internals of variables for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 2, or (at your option) any later version.
10
11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16GNU Make; see the file COPYING. If not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
18
19#include "make.h"
20
21#include <assert.h>
22
23#include "dep.h"
24#include "filedef.h"
25#include "job.h"
26#include "commands.h"
27#include "variable.h"
28#include "rule.h"
29#ifdef WINDOWS32
30#include "pathstuff.h"
31#endif
32#include "hash.h"
33
34/* Chain of all pattern-specific variables. */
35
36static struct pattern_var *pattern_vars;
37
38/* Pointer to last struct in the chain, so we can add onto the end. */
39
40static struct pattern_var *last_pattern_var;
41
42/* Create a new pattern-specific variable struct. */
43
44struct pattern_var *
45create_pattern_var (char *target, char *suffix)
46{
47 register struct pattern_var *p
48 = (struct pattern_var *) xmalloc (sizeof (struct pattern_var));
49
50 if (last_pattern_var != 0)
51 last_pattern_var->next = p;
52 else
53 pattern_vars = p;
54 last_pattern_var = p;
55 p->next = 0;
56
57 p->target = target;
58 p->len = strlen (target);
59 p->suffix = suffix + 1;
60
61 return p;
62}
63
64/* Look up a target in the pattern-specific variable list. */
65
66static struct pattern_var *
67lookup_pattern_var (struct pattern_var *start, char *target)
68{
69 struct pattern_var *p;
70 unsigned int targlen = strlen(target);
71
72 for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
73 {
74 char *stem;
75 unsigned int stemlen;
76
77 if (p->len > targlen)
78 /* It can't possibly match. */
79 continue;
80
81 /* From the lengths of the filename and the pattern parts,
82 find the stem: the part of the filename that matches the %. */
83 stem = target + (p->suffix - p->target - 1);
84 stemlen = targlen - p->len + 1;
85
86 /* Compare the text in the pattern before the stem, if any. */
87 if (stem > target && !strneq (p->target, target, stem - target))
88 continue;
89
90 /* Compare the text in the pattern after the stem, if any.
91 We could test simply using streq, but this way we compare the
92 first two characters immediately. This saves time in the very
93 common case where the first character matches because it is a
94 period. */
95 if (*p->suffix == stem[stemlen]
96 && (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
97 break;
98 }
99
100 return p;
101}
102
103
104/* Hash table of all global variable definitions. */
105
106#ifdef 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
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
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 /* 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
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
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
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
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
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 /* Define KMK_VERSION to indicate kMk. */
906 (void) define_variable ("KMK_VERSION", 11, buf, o_default, 0);
907
908 /* Define KMK_FEATURES to indicate various working KMK features. */
909 (void) define_variable ("KMK_FEATURES", 12, "abspath toupper tolower", o_default, 0);
910
911#ifdef CONFIG_WITH_KMK_BUILTIN
912 /* The supported kMk Builtin commands. */
913 (void) define_variable ("KMK_BUILTIN", 11, "append cp echo install ln mkdir rm", o_default, 0);
914#endif
915
916#ifdef __MSDOS__
917 /* Allow to specify a special shell just for Make,
918 and use $COMSPEC as the default $SHELL when appropriate. */
919 {
920 static char shell_str[] = "SHELL";
921 const int shlen = sizeof (shell_str) - 1;
922 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
923 struct variable *comp = lookup_variable ("COMSPEC", 7);
924
925 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
926 if (mshp)
927 (void) define_variable (shell_str, shlen,
928 mshp->value, o_env_override, 0);
929 else if (comp)
930 {
931 /* $COMSPEC shouldn't override $SHELL. */
932 struct variable *shp = lookup_variable (shell_str, shlen);
933
934 if (!shp)
935 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
936 }
937 }
938#elif defined(__EMX__)
939 {
940 static char shell_str[] = "SHELL";
941 const int shlen = sizeof (shell_str) - 1;
942 struct variable *shell = lookup_variable (shell_str, shlen);
943 struct variable *replace = lookup_variable ("MAKESHELL", 9);
944
945 /* if $MAKESHELL is defined in the environment assume o_env_override */
946 if (replace && *replace->value && replace->origin == o_env)
947 replace->origin = o_env_override;
948
949 /* if $MAKESHELL is not defined use $SHELL but only if the variable
950 did not come from the environment */
951 if (!replace || !*replace->value)
952 if (shell && *shell->value && (shell->origin == o_env
953 || shell->origin == o_env_override))
954 {
955 /* overwrite whatever we got from the environment */
956 free(shell->value);
957 shell->value = xstrdup (default_shell);
958 shell->origin = o_default;
959 }
960
961 /* Some people do not like cmd to be used as the default
962 if $SHELL is not defined in the Makefile.
963 With -DNO_CMD_DEFAULT you can turn off this behaviour */
964# ifndef NO_CMD_DEFAULT
965 /* otherwise use $COMSPEC */
966 if (!replace || !*replace->value)
967 replace = lookup_variable ("COMSPEC", 7);
968
969 /* otherwise use $OS2_SHELL */
970 if (!replace || !*replace->value)
971 replace = lookup_variable ("OS2_SHELL", 9);
972# else
973# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
974# endif
975
976 if (replace && *replace->value)
977 /* overwrite $SHELL */
978 (void) define_variable (shell_str, shlen, replace->value,
979 replace->origin, 0);
980 else
981 /* provide a definition if there is none */
982 (void) define_variable (shell_str, shlen, default_shell,
983 o_default, 0);
984 }
985
986#endif
987
988 /* This won't override any definition, but it will provide one if there
989 isn't one there. */
990 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
991
992 /* On MSDOS we do use SHELL from environment, since it isn't a standard
993 environment variable on MSDOS, so whoever sets it, does that on purpose.
994 On OS/2 we do not use SHELL from environment but we have already handled
995 that problem above. */
996#if !defined(__MSDOS__) && !defined(__EMX__)
997 /* Don't let SHELL come from the environment. */
998 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
999 {
1000 free (v->value);
1001 v->origin = o_file;
1002 v->value = xstrdup (default_shell);
1003 }
1004#endif
1005
1006 /* Make sure MAKEFILES gets exported if it is set. */
1007 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
1008 v->export = v_ifset;
1009
1010 /* Define the magic D and F variables in terms of
1011 the automatic variables they are variations of. */
1012
1013#ifdef VMS
1014 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
1015 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
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#else
1022 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
1023 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
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#endif
1030 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
1031 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
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}
1038
1039
1040int export_all_variables;
1041
1042/* Create a new environment for FILE's commands.
1043 If FILE is nil, this is for the `shell' function.
1044 The child's MAKELEVEL variable is incremented. */
1045
1046char **
1047target_environment (struct file *file)
1048{
1049 struct variable_set_list *set_list;
1050 register struct variable_set_list *s;
1051 struct hash_table table;
1052 struct variable **v_slot;
1053 struct variable **v_end;
1054 struct variable makelevel_key;
1055 char **result_0;
1056 char **result;
1057
1058 if (file == 0)
1059 set_list = current_variable_set_list;
1060 else
1061 set_list = file->variables;
1062
1063 hash_init (&table, VARIABLE_BUCKETS,
1064 variable_hash_1, variable_hash_2, variable_hash_cmp);
1065
1066 /* Run through all the variable sets in the list,
1067 accumulating variables in TABLE. */
1068 for (s = set_list; s != 0; s = s->next)
1069 {
1070 struct variable_set *set = s->set;
1071 v_slot = (struct variable **) set->table.ht_vec;
1072 v_end = v_slot + set->table.ht_size;
1073 for ( ; v_slot < v_end; v_slot++)
1074 if (! HASH_VACANT (*v_slot))
1075 {
1076 struct variable **new_slot;
1077 struct variable *v = *v_slot;
1078
1079 /* If this is a per-target variable and it hasn't been touched
1080 already then look up the global version and take its export
1081 value. */
1082 if (v->per_target && v->export == v_default)
1083 {
1084 struct variable *gv;
1085
1086 gv = lookup_variable_in_set (v->name, strlen(v->name),
1087 &global_variable_set);
1088 if (gv)
1089 v->export = gv->export;
1090 }
1091
1092 switch (v->export)
1093 {
1094 case v_default:
1095 if (v->origin == o_default || v->origin == o_automatic)
1096 /* Only export default variables by explicit request. */
1097 continue;
1098
1099 /* The variable doesn't have a name that can be exported. */
1100 if (! v->exportable)
1101 continue;
1102
1103 if (! export_all_variables
1104 && v->origin != o_command
1105 && v->origin != o_env && v->origin != o_env_override)
1106 continue;
1107 break;
1108
1109 case v_export:
1110 break;
1111
1112 case v_noexport:
1113 /* If this is the SHELL variable and it's not exported, then
1114 add the value from our original environment. */
1115 if (streq (v->name, "SHELL"))
1116 {
1117 extern struct variable shell_var;
1118 v = &shell_var;
1119 break;
1120 }
1121 continue;
1122
1123 case v_ifset:
1124 if (v->origin == o_default)
1125 continue;
1126 break;
1127 }
1128
1129 new_slot = (struct variable **) hash_find_slot (&table, v);
1130 if (HASH_VACANT (*new_slot))
1131 hash_insert_at (&table, v, new_slot);
1132 }
1133 }
1134
1135 makelevel_key.name = MAKELEVEL_NAME;
1136 makelevel_key.length = MAKELEVEL_LENGTH;
1137#ifdef VARIABLE_HASH
1138 makelevel_key.hash1 = variable_hash_a (MAKELEVEL_NAME, MAKELEVEL_LENGTH);
1139 makelevel_key.hash2 = 0;
1140#endif
1141 hash_delete (&table, &makelevel_key);
1142
1143 result = result_0 = (char **) xmalloc ((table.ht_fill + 2) * sizeof (char *));
1144
1145 v_slot = (struct variable **) table.ht_vec;
1146 v_end = v_slot + table.ht_size;
1147 for ( ; v_slot < v_end; v_slot++)
1148 if (! HASH_VACANT (*v_slot))
1149 {
1150 struct variable *v = *v_slot;
1151
1152 /* If V is recursively expanded and didn't come from the environment,
1153 expand its value. If it came from the environment, it should
1154 go back into the environment unchanged. */
1155 if (v->recursive
1156 && v->origin != o_env && v->origin != o_env_override)
1157 {
1158 char *value = recursively_expand_for_file (v, file);
1159#ifdef WINDOWS32
1160 if (strcmp(v->name, "Path") == 0 ||
1161 strcmp(v->name, "PATH") == 0)
1162 convert_Path_to_windows32(value, ';');
1163#endif
1164 *result++ = concat (v->name, "=", value);
1165 free (value);
1166 }
1167 else
1168 {
1169#ifdef WINDOWS32
1170 if (strcmp(v->name, "Path") == 0 ||
1171 strcmp(v->name, "PATH") == 0)
1172 convert_Path_to_windows32(v->value, ';');
1173#endif
1174 *result++ = concat (v->name, "=", v->value);
1175 }
1176 }
1177
1178 *result = (char *) xmalloc (100);
1179 (void) sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
1180 *++result = 0;
1181
1182 hash_free (&table, 0);
1183
1184 return result_0;
1185}
1186
1187
1188/* Given a variable, a value, and a flavor, define the variable.
1189 See the try_variable_definition() function for details on the parameters. */
1190
1191struct variable *
1192do_variable_definition (const struct floc *flocp, const char *varname,
1193 char *value, enum variable_origin origin,
1194 enum variable_flavor flavor, int target_var)
1195{
1196 char *p, *alloc_value = NULL;
1197 struct variable *v;
1198 int append = 0;
1199 int conditional = 0;
1200
1201 /* Calculate the variable's new value in VALUE. */
1202
1203 switch (flavor)
1204 {
1205 default:
1206 case f_bogus:
1207 /* Should not be possible. */
1208 abort ();
1209 case f_simple:
1210 /* A simple variable definition "var := value". Expand the value.
1211 We have to allocate memory since otherwise it'll clobber the
1212 variable buffer, and we may still need that if we're looking at a
1213 target-specific variable. */
1214 p = alloc_value = allocated_variable_expand (value);
1215 break;
1216 case f_conditional:
1217 /* A conditional variable definition "var ?= value".
1218 The value is set IFF the variable is not defined yet. */
1219 v = lookup_variable (varname, strlen (varname));
1220 if (v)
1221 return v;
1222
1223 conditional = 1;
1224 flavor = f_recursive;
1225 /* FALLTHROUGH */
1226 case f_recursive:
1227 /* A recursive variable definition "var = value".
1228 The value is used verbatim. */
1229 p = value;
1230 break;
1231 case f_append:
1232 {
1233 /* If we have += but we're in a target variable context, we want to
1234 append only with other variables in the context of this target. */
1235 if (target_var)
1236 {
1237 append = 1;
1238 v = lookup_variable_in_set (varname, strlen (varname),
1239 current_variable_set_list->set);
1240
1241 /* Don't append from the global set if a previous non-appending
1242 target-specific variable definition exists. */
1243 if (v && !v->append)
1244 append = 0;
1245 }
1246 else
1247 v = lookup_variable (varname, strlen (varname));
1248
1249 if (v == 0)
1250 {
1251 /* There was no old value.
1252 This becomes a normal recursive definition. */
1253 p = value;
1254 flavor = f_recursive;
1255 }
1256 else
1257 {
1258 /* Paste the old and new values together in VALUE. */
1259
1260 unsigned int oldlen, vallen;
1261 char *val;
1262
1263 val = value;
1264 if (v->recursive)
1265 /* The previous definition of the variable was recursive.
1266 The new value is the unexpanded old and new values. */
1267 flavor = f_recursive;
1268 else
1269 /* The previous definition of the variable was simple.
1270 The new value comes from the old value, which was expanded
1271 when it was set; and from the expanded new value. Allocate
1272 memory for the expansion as we may still need the rest of the
1273 buffer if we're looking at a target-specific variable. */
1274 val = alloc_value = allocated_variable_expand (val);
1275
1276 oldlen = strlen (v->value);
1277 vallen = strlen (val);
1278 p = (char *) alloca (oldlen + 1 + vallen + 1);
1279 bcopy (v->value, p, oldlen);
1280 p[oldlen] = ' ';
1281 bcopy (val, &p[oldlen + 1], vallen + 1);
1282 }
1283 }
1284 }
1285
1286#ifdef __MSDOS__
1287 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1288 non-Unix systems don't conform to this default configuration (in
1289 fact, most of them don't even have `/bin'). On the other hand,
1290 $SHELL in the environment, if set, points to the real pathname of
1291 the shell.
1292 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1293 the Makefile override $SHELL from the environment. But first, we
1294 look for the basename of the shell in the directory where SHELL=
1295 points, and along the $PATH; if it is found in any of these places,
1296 we define $SHELL to be the actual pathname of the shell. Thus, if
1297 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1298 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1299 defining SHELL to be "d:/unix/bash.exe". */
1300 if ((origin == o_file || origin == o_override)
1301 && strcmp (varname, "SHELL") == 0)
1302 {
1303 PATH_VAR (shellpath);
1304 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1305
1306 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1307 if (__dosexec_find_on_path (p, (char **)0, shellpath))
1308 {
1309 char *p;
1310
1311 for (p = shellpath; *p; p++)
1312 {
1313 if (*p == '\\')
1314 *p = '/';
1315 }
1316 v = define_variable_loc (varname, strlen (varname),
1317 shellpath, origin, flavor == f_recursive,
1318 flocp);
1319 }
1320 else
1321 {
1322 char *shellbase, *bslash;
1323 struct variable *pathv = lookup_variable ("PATH", 4);
1324 char *path_string;
1325 char *fake_env[2];
1326 size_t pathlen = 0;
1327
1328 shellbase = strrchr (p, '/');
1329 bslash = strrchr (p, '\\');
1330 if (!shellbase || bslash > shellbase)
1331 shellbase = bslash;
1332 if (!shellbase && p[1] == ':')
1333 shellbase = p + 1;
1334 if (shellbase)
1335 shellbase++;
1336 else
1337 shellbase = p;
1338
1339 /* Search for the basename of the shell (with standard
1340 executable extensions) along the $PATH. */
1341 if (pathv)
1342 pathlen = strlen (pathv->value);
1343 path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
1344 /* On MSDOS, current directory is considered as part of $PATH. */
1345 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1346 fake_env[0] = path_string;
1347 fake_env[1] = (char *)0;
1348 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1349 {
1350 char *p;
1351
1352 for (p = shellpath; *p; p++)
1353 {
1354 if (*p == '\\')
1355 *p = '/';
1356 }
1357 v = define_variable_loc (varname, strlen (varname),
1358 shellpath, origin,
1359 flavor == f_recursive, flocp);
1360 }
1361 else
1362 v = lookup_variable (varname, strlen (varname));
1363
1364 free (path_string);
1365 }
1366 }
1367 else
1368#endif /* __MSDOS__ */
1369#ifdef WINDOWS32
1370 if ((origin == o_file || origin == o_override || origin == o_command)
1371 && streq (varname, "SHELL"))
1372 {
1373 extern char *default_shell;
1374
1375 /* Call shell locator function. If it returns TRUE, then
1376 set no_default_sh_exe to indicate sh was found and
1377 set new value for SHELL variable. */
1378
1379 if (find_and_set_default_shell (p))
1380 {
1381 v = define_variable_in_set (varname, strlen (varname), default_shell,
1382 origin, flavor == f_recursive,
1383 (target_var
1384 ? current_variable_set_list->set
1385 : NULL),
1386 flocp);
1387 no_default_sh_exe = 0;
1388 }
1389 else
1390 v = lookup_variable (varname, strlen (varname));
1391 }
1392 else
1393#endif
1394
1395 /* If we are defining variables inside an $(eval ...), we might have a
1396 different variable context pushed, not the global context (maybe we're
1397 inside a $(call ...) or something. Since this function is only ever
1398 invoked in places where we want to define globally visible variables,
1399 make sure we define this variable in the global set. */
1400
1401 v = define_variable_in_set (varname, strlen (varname), p,
1402 origin, flavor == f_recursive,
1403 (target_var
1404 ? current_variable_set_list->set : NULL),
1405 flocp);
1406 v->append = append;
1407 v->conditional = conditional;
1408
1409 if (alloc_value)
1410 free (alloc_value);
1411
1412 return v;
1413}
1414
1415
1416/* Try to interpret LINE (a null-terminated string) as a variable definition.
1417
1418 ORIGIN may be o_file, o_override, o_env, o_env_override,
1419 or o_command specifying that the variable definition comes
1420 from a makefile, an override directive, the environment with
1421 or without the -e switch, or the command line.
1422
1423 See the comments for parse_variable_definition().
1424
1425 If LINE was recognized as a variable definition, a pointer to its `struct
1426 variable' is returned. If LINE is not a variable definition, NULL is
1427 returned. */
1428
1429struct variable *
1430parse_variable_definition (struct variable *v, char *line)
1431{
1432 register int c;
1433 register char *p = line;
1434 register char *beg;
1435 register char *end;
1436 enum variable_flavor flavor = f_bogus;
1437 char *name;
1438
1439 while (1)
1440 {
1441 c = *p++;
1442 if (c == '\0' || c == '#')
1443 return 0;
1444 if (c == '=')
1445 {
1446 end = p - 1;
1447 flavor = f_recursive;
1448 break;
1449 }
1450 else if (c == ':')
1451 if (*p == '=')
1452 {
1453 end = p++ - 1;
1454 flavor = f_simple;
1455 break;
1456 }
1457 else
1458 /* A colon other than := is a rule line, not a variable defn. */
1459 return 0;
1460 else if (c == '+' && *p == '=')
1461 {
1462 end = p++ - 1;
1463 flavor = f_append;
1464 break;
1465 }
1466 else if (c == '?' && *p == '=')
1467 {
1468 end = p++ - 1;
1469 flavor = f_conditional;
1470 break;
1471 }
1472 else if (c == '$')
1473 {
1474 /* This might begin a variable expansion reference. Make sure we
1475 don't misrecognize chars inside the reference as =, := or +=. */
1476 char closeparen;
1477 int count;
1478 c = *p++;
1479 if (c == '(')
1480 closeparen = ')';
1481 else if (c == '{')
1482 closeparen = '}';
1483 else
1484 continue; /* Nope. */
1485
1486 /* P now points past the opening paren or brace.
1487 Count parens or braces until it is matched. */
1488 count = 0;
1489 for (; *p != '\0'; ++p)
1490 {
1491 if (*p == c)
1492 ++count;
1493 else if (*p == closeparen && --count < 0)
1494 {
1495 ++p;
1496 break;
1497 }
1498 }
1499 }
1500 }
1501 v->flavor = flavor;
1502
1503 beg = next_token (line);
1504 while (end > beg && isblank ((unsigned char)end[-1]))
1505 --end;
1506 p = next_token (p);
1507 v->value = p;
1508
1509 /* Expand the name, so "$(foo)bar = baz" works. */
1510 name = (char *) alloca (end - beg + 1);
1511 bcopy (beg, name, end - beg);
1512 name[end - beg] = '\0';
1513 v->name = allocated_variable_expand (name);
1514
1515 if (v->name[0] == '\0')
1516 fatal (&v->fileinfo, _("empty variable name"));
1517
1518 return v;
1519}
1520
1521
1522/* Try to interpret LINE (a null-terminated string) as a variable definition.
1523
1524 ORIGIN may be o_file, o_override, o_env, o_env_override,
1525 or o_command specifying that the variable definition comes
1526 from a makefile, an override directive, the environment with
1527 or without the -e switch, or the command line.
1528
1529 See the comments for parse_variable_definition().
1530
1531 If LINE was recognized as a variable definition, a pointer to its `struct
1532 variable' is returned. If LINE is not a variable definition, NULL is
1533 returned. */
1534
1535struct variable *
1536try_variable_definition (const struct floc *flocp, char *line,
1537 enum variable_origin origin, int target_var)
1538{
1539 struct variable v;
1540 struct variable *vp;
1541
1542 if (flocp != 0)
1543 v.fileinfo = *flocp;
1544 else
1545 v.fileinfo.filenm = 0;
1546
1547 if (!parse_variable_definition (&v, line))
1548 return 0;
1549
1550 vp = do_variable_definition (flocp, v.name, v.value,
1551 origin, v.flavor, target_var);
1552
1553 free (v.name);
1554
1555 return vp;
1556}
1557
1558
1559/* Print information for variable V, prefixing it with PREFIX. */
1560
1561static void
1562print_variable (const void *item, void *arg)
1563{
1564 const struct variable *v = (struct variable *) item;
1565 const char *prefix = (char *) arg;
1566 const char *origin;
1567
1568 switch (v->origin)
1569 {
1570 case o_default:
1571 origin = _("default");
1572 break;
1573 case o_env:
1574 origin = _("environment");
1575 break;
1576 case o_file:
1577 origin = _("makefile");
1578 break;
1579 case o_env_override:
1580 origin = _("environment under -e");
1581 break;
1582 case o_command:
1583 origin = _("command line");
1584 break;
1585 case o_override:
1586 origin = _("`override' directive");
1587 break;
1588 case o_automatic:
1589 origin = _("automatic");
1590 break;
1591 case o_invalid:
1592 default:
1593 abort ();
1594 }
1595 fputs ("# ", stdout);
1596 fputs (origin, stdout);
1597 if (v->fileinfo.filenm)
1598 printf (_(" (from `%s', line %lu)"),
1599 v->fileinfo.filenm, v->fileinfo.lineno);
1600 putchar ('\n');
1601 fputs (prefix, stdout);
1602
1603 /* Is this a `define'? */
1604 if (v->recursive && strchr (v->value, '\n') != 0)
1605 printf ("define %s\n%s\nendef\n", v->name, v->value);
1606 else
1607 {
1608 register char *p;
1609
1610 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1611
1612 /* Check if the value is just whitespace. */
1613 p = next_token (v->value);
1614 if (p != v->value && *p == '\0')
1615 /* All whitespace. */
1616 printf ("$(subst ,,%s)", v->value);
1617 else if (v->recursive)
1618 fputs (v->value, stdout);
1619 else
1620 /* Double up dollar signs. */
1621 for (p = v->value; *p != '\0'; ++p)
1622 {
1623 if (*p == '$')
1624 putchar ('$');
1625 putchar (*p);
1626 }
1627 putchar ('\n');
1628 }
1629}
1630
1631
1632/* Print all the variables in SET. PREFIX is printed before
1633 the actual variable definitions (everything else is comments). */
1634
1635void
1636print_variable_set (struct variable_set *set, char *prefix)
1637{
1638 hash_map_arg (&set->table, print_variable, prefix);
1639
1640 fputs (_("# variable set hash-table stats:\n"), stdout);
1641 fputs ("# ", stdout);
1642 hash_print_stats (&set->table, stdout);
1643 putc ('\n', stdout);
1644}
1645
1646/* Print the data base of variables. */
1647
1648void
1649print_variable_data_base (void)
1650{
1651 puts (_("\n# Variables\n"));
1652
1653 print_variable_set (&global_variable_set, "");
1654
1655 puts (_("\n# Pattern-specific Variable Values"));
1656
1657 {
1658 struct pattern_var *p;
1659 int rules = 0;
1660
1661 for (p = pattern_vars; p != 0; p = p->next)
1662 {
1663 ++rules;
1664 printf ("\n%s :\n", p->target);
1665 print_variable (&p->variable, "# ");
1666 }
1667
1668 if (rules == 0)
1669 puts (_("\n# No pattern-specific variable values."));
1670 else
1671 printf (_("\n# %u pattern-specific variable values"), rules);
1672 }
1673}
1674
1675
1676/* Print all the local variables of FILE. */
1677
1678void
1679print_file_variables (struct file *file)
1680{
1681 if (file->variables != 0)
1682 print_variable_set (file->variables->set, "# ");
1683}
1684
1685#ifdef WINDOWS32
1686void
1687sync_Path_environment (void)
1688{
1689 char *path = allocated_variable_expand ("$(PATH)");
1690 static char *environ_path = NULL;
1691
1692 if (!path)
1693 return;
1694
1695 /*
1696 * If done this before, don't leak memory unnecessarily.
1697 * Free the previous entry before allocating new one.
1698 */
1699 if (environ_path)
1700 free (environ_path);
1701
1702 /*
1703 * Create something WINDOWS32 world can grok
1704 */
1705 convert_Path_to_windows32 (path, ';');
1706 environ_path = concat ("PATH", "=", path);
1707 putenv (environ_path);
1708 free (path);
1709}
1710#endif
Note: See TracBrowser for help on using the repository browser.

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