VirtualBox

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

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

skip some unnecessary copies and allocations.

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