Fix for EINTR problems when using jobserver.
[make.git] / variable.c
blobaec6edaa3477ceeea776660845fe1cf98e96a077
1 /* Internals of variables for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,96,97 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include "make.h"
21 #include "dep.h"
22 #include "filedef.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
31 /* Hash table of all global variable definitions. */
33 #ifndef VARIABLE_BUCKETS
34 #define VARIABLE_BUCKETS 523
35 #endif
36 #ifndef PERFILE_VARIABLE_BUCKETS
37 #define PERFILE_VARIABLE_BUCKETS 23
38 #endif
39 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
40 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
41 #endif
42 static struct variable *variable_table[VARIABLE_BUCKETS];
43 static struct variable_set global_variable_set
44 = { variable_table, VARIABLE_BUCKETS };
45 static struct variable_set_list global_setlist
46 = { 0, &global_variable_set };
47 struct variable_set_list *current_variable_set_list = &global_setlist;
49 /* Implement variables. */
51 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
52 LENGTH is the length of NAME, which does not need to be null-terminated.
53 ORIGIN specifies the origin of the variable (makefile, command line
54 or environment).
55 If RECURSIVE is nonzero a flag is set in the variable saying
56 that it should be recursively re-expanded. */
58 struct variable *
59 define_variable_in_set (name, length, value, origin, recursive, set, flocp)
60 char *name;
61 unsigned int length;
62 char *value;
63 enum variable_origin origin;
64 int recursive;
65 struct variable_set *set;
66 const struct floc *flocp;
68 register unsigned int i;
69 register unsigned int hashval;
70 register struct variable *v;
72 hashval = 0;
73 for (i = 0; i < length; ++i)
74 HASH (hashval, name[i]);
75 hashval %= set->buckets;
77 for (v = set->table[hashval]; v != 0; v = v->next)
78 if (*v->name == *name
79 && strneq (v->name + 1, name + 1, length - 1)
80 && v->name[length] == '\0')
81 break;
83 if (env_overrides && origin == o_env)
84 origin = o_env_override;
86 if (v != 0)
88 if (env_overrides && v->origin == o_env)
89 /* V came from in the environment. Since it was defined
90 before the switches were parsed, it wasn't affected by -e. */
91 v->origin = o_env_override;
93 /* A variable of this name is already defined.
94 If the old definition is from a stronger source
95 than this one, don't redefine it. */
96 if ((int) origin >= (int) v->origin)
98 if (v->value != 0)
99 free (v->value);
100 v->value = xstrdup (value);
101 if (flocp != 0)
102 v->fileinfo = *flocp;
103 else
104 v->fileinfo.filenm = 0;
105 v->origin = origin;
106 v->recursive = recursive;
108 return v;
111 /* Create a new variable definition and add it to the hash table. */
113 v = (struct variable *) xmalloc (sizeof (struct variable));
114 v->name = savestring (name, length);
115 v->value = xstrdup (value);
116 if (flocp != 0)
117 v->fileinfo = *flocp;
118 else
119 v->fileinfo.filenm = 0;
120 v->origin = origin;
121 v->recursive = recursive;
122 v->expanding = 0;
123 v->per_target = 0;
124 v->append = 0;
125 v->export = v_default;
126 v->next = set->table[hashval];
127 set->table[hashval] = v;
128 return v;
131 /* Lookup a variable whose name is a string starting at NAME
132 and with LENGTH chars. NAME need not be null-terminated.
133 Returns address of the `struct variable' containing all info
134 on the variable, or nil if no such variable is defined. */
136 struct variable *
137 lookup_variable (name, length)
138 const char *name;
139 unsigned int length;
141 const struct variable_set_list *setlist;
143 unsigned int i;
144 unsigned int rawhash = 0;
146 for (i = 0; i < length; ++i)
147 HASH (rawhash, name[i]);
149 for (setlist = current_variable_set_list;
150 setlist != 0; setlist = setlist->next)
152 const struct variable_set *set = setlist->set;
153 unsigned int hashval = rawhash % set->buckets;
154 struct variable *v;
156 /* Look through this set list; return it if found. */
157 for (v = set->table[hashval]; v != 0; v = v->next)
158 if (*v->name == *name
159 && strneq (v->name + 1, name + 1, length - 1)
160 && v->name[length] == '\0')
161 return v;
164 #ifdef VMS
165 /* since we don't read envp[] on startup, try to get the
166 variable via getenv() here. */
168 char *vname = alloca (length + 1);
169 char *value;
170 strncpy (vname, name, length);
171 vname[length] = 0;
172 value = getenv (vname);
173 if (value != 0)
175 char *sptr;
176 int scnt;
178 sptr = value;
179 scnt = 0;
181 while ((sptr = strchr (sptr, '$')))
183 scnt++;
184 sptr++;
187 if (scnt > 0)
189 char *nvalue;
190 char *nptr;
192 nvalue = alloca (strlen (value) + scnt + 1);
193 sptr = value;
194 nptr = nvalue;
196 while (*sptr)
198 if (*sptr == '$')
200 *nptr++ = '$';
201 *nptr++ = '$';
203 else
205 *nptr++ = *sptr;
207 sptr++;
210 *nptr = '\0';
211 return define_variable (vname, length, nvalue, o_env, 1);
215 return define_variable (vname, length, value, o_env, 1);
218 #endif /* VMS */
220 return 0;
223 /* Lookup a variable whose name is a string starting at NAME
224 and with LENGTH chars in set SET. NAME need not be null-terminated.
225 Returns address of the `struct variable' containing all info
226 on the variable, or nil if no such variable is defined. */
228 struct variable *
229 lookup_variable_in_set (name, length, set)
230 const char *name;
231 unsigned int length;
232 const struct variable_set *set;
234 unsigned int i;
235 unsigned int hash = 0;
236 struct variable *v;
238 for (i = 0; i < length; ++i)
239 HASH (hash, name[i]);
240 hash %= set->buckets;
242 for (v = set->table[hash]; v != 0; v = v->next)
243 if (*v->name == *name
244 && strneq (v->name + 1, name + 1, length - 1)
245 && v->name[length] == 0)
246 return v;
248 return 0;
251 /* Initialize FILE's variable set list. If FILE already has a variable set
252 list, the topmost variable set is left intact, but the the rest of the
253 chain is replaced with FILE->parent's setlist. If we're READing a
254 makefile, don't do the pattern variable search now, since the pattern
255 variable might not have been defined yet. */
257 void
258 initialize_file_variables (file, reading)
259 struct file *file;
260 int reading;
262 register struct variable_set_list *l = file->variables;
264 if (l == 0)
266 l = (struct variable_set_list *)
267 xmalloc (sizeof (struct variable_set_list));
268 l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
269 l->set->buckets = PERFILE_VARIABLE_BUCKETS;
270 l->set->table = (struct variable **)
271 xmalloc (l->set->buckets * sizeof (struct variable *));
272 bzero ((char *) l->set->table,
273 l->set->buckets * sizeof (struct variable *));
274 file->variables = l;
277 if (file->parent == 0)
278 l->next = &global_setlist;
279 else
281 initialize_file_variables (file->parent, reading);
282 l->next = file->parent->variables;
285 /* If we're not reading makefiles and we haven't looked yet, see if
286 we can find a pattern variable. */
288 if (!reading && !file->pat_searched)
290 struct pattern_var *p = lookup_pattern_var (file->name);
292 file->pat_searched = 1;
293 if (p != 0)
295 /* If we found one, insert it between the current target's
296 variables and the next set, whatever it is. */
297 file->pat_variables = (struct variable_set_list *)
298 xmalloc (sizeof (struct variable_set_list));
299 file->pat_variables->set = p->vars->set;
303 /* If we have a pattern variable match, set it up. */
305 if (file->pat_variables != 0)
307 file->pat_variables->next = l->next;
308 l->next = file->pat_variables;
312 /* Pop the top set off the current variable set list,
313 and free all its storage. */
315 void
316 pop_variable_scope ()
318 register struct variable_set_list *setlist = current_variable_set_list;
319 register struct variable_set *set = setlist->set;
320 register unsigned int i;
322 current_variable_set_list = setlist->next;
323 free ((char *) setlist);
325 for (i = 0; i < set->buckets; ++i)
327 register struct variable *next = set->table[i];
328 while (next != 0)
330 register struct variable *v = next;
331 next = v->next;
333 free (v->name);
334 if (v->value)
335 free (v->value);
336 free ((char *) v);
339 free ((char *) set->table);
340 free ((char *) set);
343 struct variable_set_list *
344 create_new_variable_set ()
346 register struct variable_set_list *setlist;
347 register struct variable_set *set;
349 set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
350 set->buckets = SMALL_SCOPE_VARIABLE_BUCKETS;
351 set->table = (struct variable **)
352 xmalloc (set->buckets * sizeof (struct variable *));
353 bzero ((char *) set->table, set->buckets * sizeof (struct variable *));
355 setlist = (struct variable_set_list *)
356 xmalloc (sizeof (struct variable_set_list));
357 setlist->set = set;
358 setlist->next = current_variable_set_list;
360 return setlist;
363 /* Create a new variable set and push it on the current setlist. */
365 struct variable_set_list *
366 push_new_variable_scope ()
368 return (current_variable_set_list = create_new_variable_set());
371 /* Merge SET1 into SET0, freeing unused storage in SET1. */
373 static void
374 merge_variable_sets (set0, set1)
375 struct variable_set *set0, *set1;
377 register unsigned int bucket1;
379 for (bucket1 = 0; bucket1 < set1->buckets; ++bucket1)
381 register struct variable *v1 = set1->table[bucket1];
382 while (v1 != 0)
384 struct variable *next = v1->next;
385 unsigned int bucket0;
386 register struct variable *v0;
388 if (set1->buckets >= set0->buckets)
389 bucket0 = bucket1;
390 else
392 register char *n;
393 bucket0 = 0;
394 for (n = v1->name; *n != '\0'; ++n)
395 HASH (bucket0, *n);
397 bucket0 %= set0->buckets;
399 for (v0 = set0->table[bucket0]; v0 != 0; v0 = v0->next)
400 if (streq (v0->name, v1->name))
401 break;
403 if (v0 == 0)
405 /* There is no variable in SET0 with the same name. */
406 v1->next = set0->table[bucket0];
407 set0->table[bucket0] = v1;
409 else
411 /* The same variable exists in both sets.
412 SET0 takes precedence. */
413 free (v1->value);
414 free ((char *) v1);
417 v1 = next;
422 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
424 void
425 merge_variable_set_lists (setlist0, setlist1)
426 struct variable_set_list **setlist0, *setlist1;
428 register struct variable_set_list *list0 = *setlist0;
429 struct variable_set_list *last0 = 0;
431 while (setlist1 != 0 && list0 != 0)
433 struct variable_set_list *next = setlist1;
434 setlist1 = setlist1->next;
436 merge_variable_sets (list0->set, next->set);
438 last0 = list0;
439 list0 = list0->next;
442 if (setlist1 != 0)
444 if (last0 == 0)
445 *setlist0 = setlist1;
446 else
447 last0->next = setlist1;
451 /* Define the automatic variables, and record the addresses
452 of their structures so we can change their values quickly. */
454 void
455 define_automatic_variables ()
457 #ifdef WINDOWS32
458 extern char* default_shell;
459 #else
460 extern char default_shell[];
461 #endif
462 register struct variable *v;
463 char buf[200];
465 sprintf (buf, "%u", makelevel);
466 (void) define_variable ("MAKELEVEL", 9, buf, o_env, 0);
468 sprintf (buf, "%s%s%s",
469 version_string,
470 (remote_description == 0 || remote_description[0] == '\0')
471 ? "" : "-",
472 (remote_description == 0 || remote_description[0] == '\0')
473 ? "" : remote_description);
474 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
476 #ifdef __MSDOS__
477 /* Allow to specify a special shell just for Make,
478 and use $COMSPEC as the default $SHELL when appropriate. */
480 static char shell_str[] = "SHELL";
481 const int shlen = sizeof (shell_str) - 1;
482 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
483 struct variable *comp = lookup_variable ("COMSPEC", 7);
485 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
486 if (mshp)
487 (void) define_variable (shell_str, shlen,
488 mshp->value, o_env_override, 0);
489 else if (comp)
491 /* $COMSPEC shouldn't override $SHELL. */
492 struct variable *shp = lookup_variable (shell_str, shlen);
494 if (!shp)
495 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
498 #endif
500 /* This won't override any definition, but it
501 will provide one if there isn't one there. */
502 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
503 v->export = v_export; /* Always export SHELL. */
505 /* On MSDOS we do use SHELL from environment, since
506 it isn't a standard environment variable on MSDOS,
507 so whoever sets it, does that on purpose. */
508 #ifndef __MSDOS__
509 /* Don't let SHELL come from the environment. */
510 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
512 free (v->value);
513 v->origin = o_file;
514 v->value = xstrdup (default_shell);
516 #endif
518 /* Make sure MAKEFILES gets exported if it is set. */
519 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
520 v->export = v_ifset;
522 /* Define the magic D and F variables in terms of
523 the automatic variables they are variations of. */
525 #ifdef VMS
526 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
527 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
528 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
529 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
530 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
531 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
532 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
533 #else
534 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
535 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
536 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
537 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
538 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
539 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
540 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
541 #endif
542 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
543 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
544 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
545 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
546 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
547 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
548 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
551 int export_all_variables;
553 /* Create a new environment for FILE's commands.
554 If FILE is nil, this is for the `shell' function.
555 The child's MAKELEVEL variable is incremented. */
557 char **
558 target_environment (file)
559 struct file *file;
561 struct variable_set_list *set_list;
562 register struct variable_set_list *s;
563 struct variable_bucket
565 struct variable_bucket *next;
566 struct variable *variable;
568 struct variable_bucket **table;
569 unsigned int buckets;
570 register unsigned int i;
571 register unsigned nvariables;
572 char **result;
573 unsigned int mklev_hash;
575 if (file == 0)
576 set_list = current_variable_set_list;
577 else
578 set_list = file->variables;
580 /* Find the lowest number of buckets in any set in the list. */
581 s = set_list;
582 buckets = s->set->buckets;
583 for (s = s->next; s != 0; s = s->next)
584 if (s->set->buckets < buckets)
585 buckets = s->set->buckets;
587 /* Find the hash value of the bucket `MAKELEVEL' will fall into. */
589 char *p = "MAKELEVEL";
590 mklev_hash = 0;
591 while (*p != '\0')
592 HASH (mklev_hash, *p++);
595 /* Temporarily allocate a table with that many buckets. */
596 table = (struct variable_bucket **)
597 alloca (buckets * sizeof (struct variable_bucket *));
598 bzero ((char *) table, buckets * sizeof (struct variable_bucket *));
600 /* Run through all the variable sets in the list,
601 accumulating variables in TABLE. */
602 nvariables = 0;
603 for (s = set_list; s != 0; s = s->next)
605 register struct variable_set *set = s->set;
606 for (i = 0; i < set->buckets; ++i)
608 register struct variable *v;
609 for (v = set->table[i]; v != 0; v = v->next)
611 unsigned int j = i % buckets;
612 register struct variable_bucket *ov;
613 register char *p = v->name;
615 if (i == mklev_hash % set->buckets
616 && streq (v->name, "MAKELEVEL"))
617 /* Don't include MAKELEVEL because it will be
618 added specially at the end. */
619 continue;
621 /* If this is a per-target variable and it hasn't been touched
622 already then look up the global version and take its export
623 value. */
624 if (v->per_target && v->export == v_default)
626 struct variable *gv;
628 gv = lookup_variable_in_set(v->name, strlen(v->name),
629 &global_variable_set);
630 if (gv)
631 v->export = gv->export;
634 switch (v->export)
636 case v_default:
637 if (v->origin == o_default || v->origin == o_automatic)
638 /* Only export default variables by explicit request. */
639 continue;
641 if (! export_all_variables
642 && v->origin != o_command
643 && v->origin != o_env && v->origin != o_env_override)
644 continue;
646 if (*p != '_' && (*p < 'A' || *p > 'Z')
647 && (*p < 'a' || *p > 'z'))
648 continue;
649 for (++p; *p != '\0'; ++p)
650 if (*p != '_' && (*p < 'a' || *p > 'z')
651 && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9'))
652 continue;
653 if (*p != '\0')
654 continue;
655 break;
657 case v_export:
658 break;
660 case v_noexport:
661 continue;
663 case v_ifset:
664 if (v->origin == o_default)
665 continue;
666 break;
669 /* If this was from a different-sized hash table, then
670 recalculate the bucket it goes in. */
671 if (set->buckets != buckets)
673 register char *np;
675 j = 0;
676 for (np = v->name; *np != '\0'; ++np)
677 HASH (j, *np);
678 j %= buckets;
681 for (ov = table[j]; ov != 0; ov = ov->next)
682 if (streq (v->name, ov->variable->name))
683 break;
685 if (ov == 0)
687 register struct variable_bucket *entry;
688 entry = (struct variable_bucket *)
689 alloca (sizeof (struct variable_bucket));
690 entry->next = table[j];
691 entry->variable = v;
692 table[j] = entry;
693 ++nvariables;
699 result = (char **) xmalloc ((nvariables + 2) * sizeof (char *));
700 nvariables = 0;
701 for (i = 0; i < buckets; ++i)
703 register struct variable_bucket *b;
704 for (b = table[i]; b != 0; b = b->next)
706 register struct variable *v = b->variable;
708 /* If V is recursively expanded and didn't come from the environment,
709 expand its value. If it came from the environment, it should
710 go back into the environment unchanged. */
711 if (v->recursive
712 && v->origin != o_env && v->origin != o_env_override)
714 char *value = recursively_expand (v);
715 #ifdef WINDOWS32
716 if (strcmp(v->name, "Path") == 0 ||
717 strcmp(v->name, "PATH") == 0)
718 convert_Path_to_windows32(value, ';');
719 #endif
720 result[nvariables++] = concat (v->name, "=", value);
721 free (value);
723 else
724 #ifdef WINDOWS32
726 if (strcmp(v->name, "Path") == 0 ||
727 strcmp(v->name, "PATH") == 0)
728 convert_Path_to_windows32(v->value, ';');
729 result[nvariables++] = concat (v->name, "=", v->value);
731 #else
732 result[nvariables++] = concat (v->name, "=", v->value);
733 #endif
736 result[nvariables] = (char *) xmalloc (100);
737 (void) sprintf (result[nvariables], "MAKELEVEL=%u", makelevel + 1);
738 result[++nvariables] = 0;
740 return result;
743 /* Try to interpret LINE (a null-terminated string) as a variable definition.
745 ORIGIN may be o_file, o_override, o_env, o_env_override,
746 or o_command specifying that the variable definition comes
747 from a makefile, an override directive, the environment with
748 or without the -e switch, or the command line.
750 See the comments for parse_variable_definition().
752 If LINE was recognized as a variable definition, a pointer to its `struct
753 variable' is returned. If LINE is not a variable definition, NULL is
754 returned. */
756 struct variable *
757 try_variable_definition (flocp, line, origin, target_var)
758 const struct floc *flocp;
759 char *line;
760 enum variable_origin origin;
761 int target_var;
763 register int c;
764 register char *p = line;
765 register char *beg;
766 register char *end;
767 enum { f_bogus,
768 f_simple, f_recursive, f_append, f_conditional } flavor = f_bogus;
769 char *name, *expanded_name, *value=0, *alloc_value=NULL;
770 struct variable *v;
771 int append = 0;
773 while (1)
775 c = *p++;
776 if (c == '\0' || c == '#')
777 return 0;
778 if (c == '=')
780 end = p - 1;
781 flavor = f_recursive;
782 break;
784 else if (c == ':')
785 if (*p == '=')
787 end = p++ - 1;
788 flavor = f_simple;
789 break;
791 else
792 /* A colon other than := is a rule line, not a variable defn. */
793 return 0;
794 else if (c == '+' && *p == '=')
796 end = p++ - 1;
797 flavor = f_append;
798 break;
800 else if (c == '?' && *p == '=')
802 end = p++ - 1;
803 flavor = f_conditional;
804 break;
806 else if (c == '$')
808 /* This might begin a variable expansion reference. Make sure we
809 don't misrecognize chars inside the reference as =, := or +=. */
810 char closeparen;
811 int count;
812 c = *p++;
813 if (c == '(')
814 closeparen = ')';
815 else if (c == '{')
816 closeparen = '}';
817 else
818 continue; /* Nope. */
820 /* P now points past the opening paren or brace.
821 Count parens or braces until it is matched. */
822 count = 0;
823 for (; *p != '\0'; ++p)
825 if (*p == c)
826 ++count;
827 else if (*p == closeparen && --count < 0)
829 ++p;
830 break;
836 beg = next_token (line);
837 while (end > beg && isblank ((unsigned char)end[-1]))
838 --end;
839 p = next_token (p);
841 /* Expand the name, so "$(foo)bar = baz" works. */
842 name = (char *) alloca (end - beg + 1);
843 bcopy (beg, name, end - beg);
844 name[end - beg] = '\0';
845 expanded_name = allocated_variable_expand (name);
847 if (expanded_name[0] == '\0')
848 fatal (flocp, _("empty variable name"));
850 /* Calculate the variable's new value in VALUE. */
852 switch (flavor)
854 case f_bogus:
855 /* Should not be possible. */
856 abort ();
857 case f_simple:
858 /* A simple variable definition "var := value". Expand the value.
859 We have to allocate memory since otherwise it'll clobber the
860 variable buffer, and we may still need that if we're looking at a
861 target-specific variable. */
862 value = alloc_value = allocated_variable_expand (p);
863 break;
864 case f_conditional:
865 /* A conditional variable definition "var ?= value".
866 The value is set IFF the variable is not defined yet. */
867 v = lookup_variable(expanded_name, strlen(expanded_name));
868 if (v)
870 free(expanded_name);
871 return v;
873 flavor = f_recursive;
874 /* FALLTHROUGH */
875 case f_recursive:
876 /* A recursive variable definition "var = value".
877 The value is used verbatim. */
878 value = p;
879 break;
880 case f_append:
882 /* If we have += but we're in a target variable context, we want to
883 append only with other variables in the context of this target. */
884 if (target_var)
886 append = 1;
887 v = lookup_variable_in_set (expanded_name, strlen (expanded_name),
888 current_variable_set_list->set);
890 else
891 v = lookup_variable (expanded_name, strlen (expanded_name));
893 if (v == 0)
895 /* There was no old value.
896 This becomes a normal recursive definition. */
897 value = p;
898 flavor = f_recursive;
900 else
902 /* Paste the old and new values together in VALUE. */
904 unsigned int oldlen, newlen;
906 if (v->recursive)
907 /* The previous definition of the variable was recursive.
908 The new value is the unexpanded old and new values. */
909 flavor = f_recursive;
910 else
911 /* The previous definition of the variable was simple.
912 The new value comes from the old value, which was expanded
913 when it was set; and from the expanded new value. Allocate
914 memory for the expansion as we may still need the rest of the
915 buffer if we're looking at a target-specific variable. */
916 p = alloc_value = allocated_variable_expand (p);
918 oldlen = strlen (v->value);
919 newlen = strlen (p);
920 value = (char *) alloca (oldlen + 1 + newlen + 1);
921 bcopy (v->value, value, oldlen);
922 value[oldlen] = ' ';
923 bcopy (p, &value[oldlen + 1], newlen + 1);
928 #ifdef __MSDOS__
929 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
930 non-Unix systems don't conform to this default configuration (in
931 fact, most of them don't even have `/bin'). On the other hand,
932 $SHELL in the environment, if set, points to the real pathname of
933 the shell.
934 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
935 the Makefile override $SHELL from the environment. But first, we
936 look for the basename of the shell in the directory where SHELL=
937 points, and along the $PATH; if it is found in any of these places,
938 we define $SHELL to be the actual pathname of the shell. Thus, if
939 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
940 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
941 defining SHELL to be "d:/unix/bash.exe". */
942 if ((origin == o_file || origin == o_override)
943 && strcmp (expanded_name, "SHELL") == 0)
945 char shellpath[PATH_MAX];
946 extern char * __dosexec_find_on_path (const char *, char *[], char *);
948 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
949 if (__dosexec_find_on_path (value, (char **)0, shellpath))
951 char *p;
953 for (p = shellpath; *p; p++)
955 if (*p == '\\')
956 *p = '/';
958 v = define_variable_loc (expanded_name, strlen (expanded_name),
959 shellpath, origin, flavor == f_recursive,
960 flocp);
962 else
964 char *shellbase, *bslash;
965 struct variable *pathv = lookup_variable ("PATH", 4);
966 char *path_string;
967 char *fake_env[2];
968 size_t pathlen = 0;
970 shellbase = strrchr (value, '/');
971 bslash = strrchr (value, '\\');
972 if (!shellbase || bslash > shellbase)
973 shellbase = bslash;
974 if (!shellbase && value[1] == ':')
975 shellbase = value + 1;
976 if (shellbase)
977 shellbase++;
978 else
979 shellbase = value;
981 /* Search for the basename of the shell (with standard
982 executable extensions) along the $PATH. */
983 if (pathv)
984 pathlen = strlen (pathv->value);
985 path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
986 /* On MSDOS, current directory is considered as part of $PATH. */
987 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
988 fake_env[0] = path_string;
989 fake_env[1] = (char *)0;
990 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
992 char *p;
994 for (p = shellpath; *p; p++)
996 if (*p == '\\')
997 *p = '/';
999 v = define_variable_loc (expanded_name, strlen (expanded_name),
1000 shellpath, origin,
1001 flavor == f_recursive, flocp);
1003 else
1004 v = lookup_variable (expanded_name, strlen (expanded_name));
1006 free (path_string);
1009 else
1010 #endif /* __MSDOS__ */
1011 #ifdef WINDOWS32
1012 if ((origin == o_file || origin == o_override)
1013 && strcmp (expanded_name, "SHELL") == 0)
1015 extern char* default_shell;
1018 * Call shell locator function. If it returns TRUE, then
1019 * set no_default_sh_exe to indicate sh was found and
1020 * set new value for SHELL variable.
1022 if (find_and_set_default_shell(value)) {
1023 v = define_variable_loc (expanded_name, strlen (expanded_name),
1024 default_shell, origin, flavor == f_recursive,
1025 flocp);
1026 no_default_sh_exe = 0;
1029 else
1030 #endif
1032 v = define_variable_loc (expanded_name, strlen (expanded_name), value,
1033 origin, flavor == f_recursive, flocp);
1035 v->append = append;
1037 if (alloc_value)
1038 free (alloc_value);
1039 free (expanded_name);
1041 return v;
1044 /* Print information for variable V, prefixing it with PREFIX. */
1046 static void
1047 print_variable (v, prefix)
1048 register struct variable *v;
1049 char *prefix;
1051 const char *origin;
1053 switch (v->origin)
1055 case o_default:
1056 origin = _("default");
1057 break;
1058 case o_env:
1059 origin = _("environment");
1060 break;
1061 case o_file:
1062 origin = _("makefile");
1063 break;
1064 case o_env_override:
1065 origin = _("environment under -e");
1066 break;
1067 case o_command:
1068 origin = _("command line");
1069 break;
1070 case o_override:
1071 origin = _("`override' directive");
1072 break;
1073 case o_automatic:
1074 origin = _("automatic");
1075 break;
1076 case o_invalid:
1077 default:
1078 abort ();
1080 fputs ("# ", stdout);
1081 fputs (origin, stdout);
1082 if (v->fileinfo.filenm)
1083 printf (" (from `%s', line %lu)", v->fileinfo.filenm, v->fileinfo.lineno);
1084 putchar ('\n');
1085 fputs (prefix, stdout);
1087 /* Is this a `define'? */
1088 if (v->recursive && strchr (v->value, '\n') != 0)
1089 printf ("define %s\n%s\nendef\n", v->name, v->value);
1090 else
1092 register char *p;
1094 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1096 /* Check if the value is just whitespace. */
1097 p = next_token (v->value);
1098 if (p != v->value && *p == '\0')
1099 /* All whitespace. */
1100 printf ("$(subst ,,%s)", v->value);
1101 else if (v->recursive)
1102 fputs (v->value, stdout);
1103 else
1104 /* Double up dollar signs. */
1105 for (p = v->value; *p != '\0'; ++p)
1107 if (*p == '$')
1108 putchar ('$');
1109 putchar (*p);
1111 putchar ('\n');
1116 /* Print all the variables in SET. PREFIX is printed before
1117 the actual variable definitions (everything else is comments). */
1119 void
1120 print_variable_set (set, prefix)
1121 register struct variable_set *set;
1122 char *prefix;
1124 register unsigned int i, nvariables, per_bucket;
1125 register struct variable *v;
1127 per_bucket = nvariables = 0;
1128 for (i = 0; i < set->buckets; ++i)
1130 register unsigned int this_bucket = 0;
1132 for (v = set->table[i]; v != 0; v = v->next)
1134 ++this_bucket;
1135 print_variable (v, prefix);
1138 nvariables += this_bucket;
1139 if (this_bucket > per_bucket)
1140 per_bucket = this_bucket;
1143 if (nvariables == 0)
1144 puts (_("# No variables."));
1145 else
1147 printf (_("# %u variables in %u hash buckets.\n"),
1148 nvariables, set->buckets);
1149 #ifndef NO_FLOAT
1150 printf (_("# average of %.1f variables per bucket, \
1151 max %u in one bucket.\n"),
1152 (double) nvariables / (double) set->buckets,
1153 per_bucket);
1154 #else
1156 int f = (nvariables * 1000 + 5) / set->buckets;
1157 printf (_("# average of %d.%d variables per bucket, \
1158 max %u in one bucket.\n"),
1159 f/10, f%10,
1160 per_bucket);
1162 #endif
1167 /* Print the data base of variables. */
1169 void
1170 print_variable_data_base ()
1172 puts (_("\n# Variables\n"));
1174 print_variable_set (&global_variable_set, "");
1178 /* Print all the local variables of FILE. */
1180 void
1181 print_file_variables (file)
1182 struct file *file;
1184 if (file->variables != 0)
1185 print_variable_set (file->variables->set, "# ");
1188 #ifdef WINDOWS32
1189 void
1190 sync_Path_environment(void)
1192 char* path = allocated_variable_expand("$(Path)");
1193 static char* environ_path = NULL;
1195 if (!path)
1196 return;
1199 * If done this before, don't leak memory unnecessarily.
1200 * Free the previous entry before allocating new one.
1202 if (environ_path)
1203 free(environ_path);
1206 * Create something WINDOWS32 world can grok
1208 convert_Path_to_windows32(path, ';');
1209 environ_path = concat("Path", "=", path);
1210 putenv(environ_path);
1211 free(path);
1213 #endif