VirtualBox

source: kBuild/vendor/gnumake/4.2.1-git/variable.c

Last change on this file was 3138, checked in by bird, 7 years ago

Imported make 4.2.1 (2e55f5e4abdc0e38c1d64be703b446695e70b3b6) from https://git.savannah.gnu.org/git/make.git.

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