Major updates in preparation for 3.80.
[make.git] / variable.c
blobca9265cf6aa57e4148ad8f06b30e4483ec383343
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 const 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 if (set == NULL)
73 set = &global_variable_set;
75 hashval = 0;
76 for (i = 0; i < length; ++i)
77 HASH (hashval, name[i]);
78 hashval %= set->buckets;
80 for (v = set->table[hashval]; v != 0; v = v->next)
81 if (*v->name == *name
82 && strneq (v->name + 1, name + 1, length - 1)
83 && v->name[length] == '\0')
84 break;
86 if (env_overrides && origin == o_env)
87 origin = o_env_override;
89 if (v != 0)
91 if (env_overrides && v->origin == o_env)
92 /* V came from in the environment. Since it was defined
93 before the switches were parsed, it wasn't affected by -e. */
94 v->origin = o_env_override;
96 /* A variable of this name is already defined.
97 If the old definition is from a stronger source
98 than this one, don't redefine it. */
99 if ((int) origin >= (int) v->origin)
101 if (v->value != 0)
102 free (v->value);
103 v->value = xstrdup (value);
104 if (flocp != 0)
105 v->fileinfo = *flocp;
106 else
107 v->fileinfo.filenm = 0;
108 v->origin = origin;
109 v->recursive = recursive;
111 return v;
114 /* Create a new variable definition and add it to the hash table. */
116 v = (struct variable *) xmalloc (sizeof (struct variable));
117 v->name = savestring (name, length);
118 v->value = xstrdup (value);
119 if (flocp != 0)
120 v->fileinfo = *flocp;
121 else
122 v->fileinfo.filenm = 0;
123 v->origin = origin;
124 v->recursive = recursive;
125 v->expanding = 0;
126 v->exp_count = 0;
127 v->per_target = 0;
128 v->append = 0;
129 v->export = v_default;
130 v->next = set->table[hashval];
131 set->table[hashval] = v;
132 return v;
135 /* Lookup a variable whose name is a string starting at NAME
136 and with LENGTH chars. NAME need not be null-terminated.
137 Returns address of the `struct variable' containing all info
138 on the variable, or nil if no such variable is defined. */
140 struct variable *
141 lookup_variable (name, length)
142 const char *name;
143 unsigned int length;
145 const struct variable_set_list *setlist;
147 unsigned int i;
148 unsigned int rawhash = 0;
150 for (i = 0; i < length; ++i)
151 HASH (rawhash, name[i]);
153 for (setlist = current_variable_set_list;
154 setlist != 0; setlist = setlist->next)
156 const struct variable_set *set = setlist->set;
157 unsigned int hashval = rawhash % set->buckets;
158 struct variable *v;
160 /* Look through this set list; return it if found. */
161 for (v = set->table[hashval]; v != 0; v = v->next)
162 if (*v->name == *name
163 && strneq (v->name + 1, name + 1, length - 1)
164 && v->name[length] == '\0')
165 return v;
168 #ifdef VMS
169 /* since we don't read envp[] on startup, try to get the
170 variable via getenv() here. */
172 char *vname = alloca (length + 1);
173 char *value;
174 strncpy (vname, name, length);
175 vname[length] = 0;
176 value = getenv (vname);
177 if (value != 0)
179 char *sptr;
180 int scnt;
182 sptr = value;
183 scnt = 0;
185 while ((sptr = strchr (sptr, '$')))
187 scnt++;
188 sptr++;
191 if (scnt > 0)
193 char *nvalue;
194 char *nptr;
196 nvalue = alloca (strlen (value) + scnt + 1);
197 sptr = value;
198 nptr = nvalue;
200 while (*sptr)
202 if (*sptr == '$')
204 *nptr++ = '$';
205 *nptr++ = '$';
207 else
209 *nptr++ = *sptr;
211 sptr++;
214 *nptr = '\0';
215 return define_variable (vname, length, nvalue, o_env, 1);
219 return define_variable (vname, length, value, o_env, 1);
222 #endif /* VMS */
224 return 0;
227 /* Lookup a variable whose name is a string starting at NAME
228 and with LENGTH chars in set SET. NAME need not be null-terminated.
229 Returns address of the `struct variable' containing all info
230 on the variable, or nil if no such variable is defined. */
232 struct variable *
233 lookup_variable_in_set (name, length, set)
234 const char *name;
235 unsigned int length;
236 const struct variable_set *set;
238 unsigned int i;
239 unsigned int hash = 0;
240 struct variable *v;
242 for (i = 0; i < length; ++i)
243 HASH (hash, name[i]);
244 hash %= set->buckets;
246 for (v = set->table[hash]; v != 0; v = v->next)
247 if (*v->name == *name
248 && strneq (v->name + 1, name + 1, length - 1)
249 && v->name[length] == 0)
250 return v;
252 return 0;
255 /* Initialize FILE's variable set list. If FILE already has a variable set
256 list, the topmost variable set is left intact, but the the rest of the
257 chain is replaced with FILE->parent's setlist. If we're READing a
258 makefile, don't do the pattern variable search now, since the pattern
259 variable might not have been defined yet. */
261 void
262 initialize_file_variables (file, reading)
263 struct file *file;
264 int reading;
266 register struct variable_set_list *l = file->variables;
268 if (l == 0)
270 l = (struct variable_set_list *)
271 xmalloc (sizeof (struct variable_set_list));
272 l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
273 l->set->buckets = PERFILE_VARIABLE_BUCKETS;
274 l->set->table = (struct variable **)
275 xmalloc (l->set->buckets * sizeof (struct variable *));
276 bzero ((char *) l->set->table,
277 l->set->buckets * sizeof (struct variable *));
278 file->variables = l;
281 if (file->parent == 0)
282 l->next = &global_setlist;
283 else
285 initialize_file_variables (file->parent, reading);
286 l->next = file->parent->variables;
289 /* If we're not reading makefiles and we haven't looked yet, see if
290 we can find a pattern variable. */
292 if (!reading && !file->pat_searched)
294 struct pattern_var *p = lookup_pattern_var (file->name);
296 file->pat_searched = 1;
297 if (p != 0)
299 /* If we found one, insert it between the current target's
300 variables and the next set, whatever it is. */
301 file->pat_variables = (struct variable_set_list *)
302 xmalloc (sizeof (struct variable_set_list));
303 file->pat_variables->set = p->vars->set;
307 /* If we have a pattern variable match, set it up. */
309 if (file->pat_variables != 0)
311 file->pat_variables->next = l->next;
312 l->next = file->pat_variables;
316 /* Pop the top set off the current variable set list,
317 and free all its storage. */
319 void
320 pop_variable_scope ()
322 register struct variable_set_list *setlist = current_variable_set_list;
323 register struct variable_set *set = setlist->set;
324 register unsigned int i;
326 current_variable_set_list = setlist->next;
327 free ((char *) setlist);
329 for (i = 0; i < set->buckets; ++i)
331 register struct variable *next = set->table[i];
332 while (next != 0)
334 register struct variable *v = next;
335 next = v->next;
337 free (v->name);
338 if (v->value)
339 free (v->value);
340 free ((char *) v);
343 free ((char *) set->table);
344 free ((char *) set);
347 struct variable_set_list *
348 create_new_variable_set ()
350 register struct variable_set_list *setlist;
351 register struct variable_set *set;
353 set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
354 set->buckets = SMALL_SCOPE_VARIABLE_BUCKETS;
355 set->table = (struct variable **)
356 xmalloc (set->buckets * sizeof (struct variable *));
357 bzero ((char *) set->table, set->buckets * sizeof (struct variable *));
359 setlist = (struct variable_set_list *)
360 xmalloc (sizeof (struct variable_set_list));
361 setlist->set = set;
362 setlist->next = current_variable_set_list;
364 return setlist;
367 /* Create a new variable set and push it on the current setlist. */
369 struct variable_set_list *
370 push_new_variable_scope ()
372 return (current_variable_set_list = create_new_variable_set());
375 /* Merge SET1 into SET0, freeing unused storage in SET1. */
377 static void
378 merge_variable_sets (set0, set1)
379 struct variable_set *set0, *set1;
381 register unsigned int bucket1;
383 for (bucket1 = 0; bucket1 < set1->buckets; ++bucket1)
385 register struct variable *v1 = set1->table[bucket1];
386 while (v1 != 0)
388 struct variable *next = v1->next;
389 unsigned int bucket0;
390 register struct variable *v0;
392 if (set1->buckets >= set0->buckets)
393 bucket0 = bucket1;
394 else
396 register char *n;
397 bucket0 = 0;
398 for (n = v1->name; *n != '\0'; ++n)
399 HASH (bucket0, *n);
401 bucket0 %= set0->buckets;
403 for (v0 = set0->table[bucket0]; v0 != 0; v0 = v0->next)
404 if (streq (v0->name, v1->name))
405 break;
407 if (v0 == 0)
409 /* There is no variable in SET0 with the same name. */
410 v1->next = set0->table[bucket0];
411 set0->table[bucket0] = v1;
413 else
415 /* The same variable exists in both sets.
416 SET0 takes precedence. */
417 free (v1->value);
418 free ((char *) v1);
421 v1 = next;
426 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
428 void
429 merge_variable_set_lists (setlist0, setlist1)
430 struct variable_set_list **setlist0, *setlist1;
432 register struct variable_set_list *list0 = *setlist0;
433 struct variable_set_list *last0 = 0;
435 while (setlist1 != 0 && list0 != 0)
437 struct variable_set_list *next = setlist1;
438 setlist1 = setlist1->next;
440 merge_variable_sets (list0->set, next->set);
442 last0 = list0;
443 list0 = list0->next;
446 if (setlist1 != 0)
448 if (last0 == 0)
449 *setlist0 = setlist1;
450 else
451 last0->next = setlist1;
455 /* Define the automatic variables, and record the addresses
456 of their structures so we can change their values quickly. */
458 void
459 define_automatic_variables ()
461 #ifdef WINDOWS32
462 extern char* default_shell;
463 #else
464 extern char default_shell[];
465 #endif
466 register struct variable *v;
467 char buf[200];
469 sprintf (buf, "%u", makelevel);
470 (void) define_variable ("MAKELEVEL", 9, buf, o_env, 0);
472 sprintf (buf, "%s%s%s",
473 version_string,
474 (remote_description == 0 || remote_description[0] == '\0')
475 ? "" : "-",
476 (remote_description == 0 || remote_description[0] == '\0')
477 ? "" : remote_description);
478 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
480 #ifdef __MSDOS__
481 /* Allow to specify a special shell just for Make,
482 and use $COMSPEC as the default $SHELL when appropriate. */
484 static char shell_str[] = "SHELL";
485 const int shlen = sizeof (shell_str) - 1;
486 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
487 struct variable *comp = lookup_variable ("COMSPEC", 7);
489 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
490 if (mshp)
491 (void) define_variable (shell_str, shlen,
492 mshp->value, o_env_override, 0);
493 else if (comp)
495 /* $COMSPEC shouldn't override $SHELL. */
496 struct variable *shp = lookup_variable (shell_str, shlen);
498 if (!shp)
499 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
502 #endif
504 /* This won't override any definition, but it
505 will provide one if there isn't one there. */
506 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
507 v->export = v_export; /* Always export SHELL. */
509 /* On MSDOS we do use SHELL from environment, since
510 it isn't a standard environment variable on MSDOS,
511 so whoever sets it, does that on purpose. */
512 #ifndef __MSDOS__
513 /* Don't let SHELL come from the environment. */
514 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
516 free (v->value);
517 v->origin = o_file;
518 v->value = xstrdup (default_shell);
520 #endif
522 /* Make sure MAKEFILES gets exported if it is set. */
523 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
524 v->export = v_ifset;
526 /* Define the magic D and F variables in terms of
527 the automatic variables they are variations of. */
529 #ifdef VMS
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 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
534 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
535 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
536 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
537 #else
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 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
542 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
543 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
544 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
545 #endif
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);
549 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
550 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
551 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
552 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
555 int export_all_variables;
557 /* Create a new environment for FILE's commands.
558 If FILE is nil, this is for the `shell' function.
559 The child's MAKELEVEL variable is incremented. */
561 char **
562 target_environment (file)
563 struct file *file;
565 struct variable_set_list *set_list;
566 register struct variable_set_list *s;
567 struct variable_bucket
569 struct variable_bucket *next;
570 struct variable *variable;
572 struct variable_bucket **table;
573 unsigned int buckets;
574 register unsigned int i;
575 register unsigned nvariables;
576 char **result;
577 unsigned int mklev_hash;
579 if (file == 0)
580 set_list = current_variable_set_list;
581 else
582 set_list = file->variables;
584 /* Find the lowest number of buckets in any set in the list. */
585 s = set_list;
586 buckets = s->set->buckets;
587 for (s = s->next; s != 0; s = s->next)
588 if (s->set->buckets < buckets)
589 buckets = s->set->buckets;
591 /* Find the hash value of the bucket `MAKELEVEL' will fall into. */
593 char *p = "MAKELEVEL";
594 mklev_hash = 0;
595 while (*p != '\0')
596 HASH (mklev_hash, *p++);
599 /* Temporarily allocate a table with that many buckets. */
600 table = (struct variable_bucket **)
601 alloca (buckets * sizeof (struct variable_bucket *));
602 bzero ((char *) table, buckets * sizeof (struct variable_bucket *));
604 /* Run through all the variable sets in the list,
605 accumulating variables in TABLE. */
606 nvariables = 0;
607 for (s = set_list; s != 0; s = s->next)
609 register struct variable_set *set = s->set;
610 for (i = 0; i < set->buckets; ++i)
612 register struct variable *v;
613 for (v = set->table[i]; v != 0; v = v->next)
615 unsigned int j = i % buckets;
616 register struct variable_bucket *ov;
617 register char *p = v->name;
619 if (i == mklev_hash % set->buckets
620 && streq (v->name, "MAKELEVEL"))
621 /* Don't include MAKELEVEL because it will be
622 added specially at the end. */
623 continue;
625 /* If this is a per-target variable and it hasn't been touched
626 already then look up the global version and take its export
627 value. */
628 if (v->per_target && v->export == v_default)
630 struct variable *gv;
632 gv = lookup_variable_in_set(v->name, strlen(v->name),
633 &global_variable_set);
634 if (gv)
635 v->export = gv->export;
638 switch (v->export)
640 case v_default:
641 if (v->origin == o_default || v->origin == o_automatic)
642 /* Only export default variables by explicit request. */
643 continue;
645 if (! export_all_variables
646 && v->origin != o_command
647 && v->origin != o_env && v->origin != o_env_override)
648 continue;
650 if (*p != '_' && (*p < 'A' || *p > 'Z')
651 && (*p < 'a' || *p > 'z'))
652 continue;
653 for (++p; *p != '\0'; ++p)
654 if (*p != '_' && (*p < 'a' || *p > 'z')
655 && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9'))
656 continue;
657 if (*p != '\0')
658 continue;
659 break;
661 case v_export:
662 break;
664 case v_noexport:
665 continue;
667 case v_ifset:
668 if (v->origin == o_default)
669 continue;
670 break;
673 /* If this was from a different-sized hash table, then
674 recalculate the bucket it goes in. */
675 if (set->buckets != buckets)
677 register char *np;
679 j = 0;
680 for (np = v->name; *np != '\0'; ++np)
681 HASH (j, *np);
682 j %= buckets;
685 for (ov = table[j]; ov != 0; ov = ov->next)
686 if (streq (v->name, ov->variable->name))
687 break;
689 if (ov == 0)
691 register struct variable_bucket *entry;
692 entry = (struct variable_bucket *)
693 alloca (sizeof (struct variable_bucket));
694 entry->next = table[j];
695 entry->variable = v;
696 table[j] = entry;
697 ++nvariables;
703 result = (char **) xmalloc ((nvariables + 2) * sizeof (char *));
704 nvariables = 0;
705 for (i = 0; i < buckets; ++i)
707 register struct variable_bucket *b;
708 for (b = table[i]; b != 0; b = b->next)
710 register struct variable *v = b->variable;
712 /* If V is recursively expanded and didn't come from the environment,
713 expand its value. If it came from the environment, it should
714 go back into the environment unchanged. */
715 if (v->recursive
716 && v->origin != o_env && v->origin != o_env_override)
718 char *value = recursively_expand_for_file (v, file);
719 #ifdef WINDOWS32
720 if (strcmp(v->name, "Path") == 0 ||
721 strcmp(v->name, "PATH") == 0)
722 convert_Path_to_windows32(value, ';');
723 #endif
724 result[nvariables++] = concat (v->name, "=", value);
725 free (value);
727 else
728 #ifdef WINDOWS32
730 if (strcmp(v->name, "Path") == 0 ||
731 strcmp(v->name, "PATH") == 0)
732 convert_Path_to_windows32(v->value, ';');
733 result[nvariables++] = concat (v->name, "=", v->value);
735 #else
736 result[nvariables++] = concat (v->name, "=", v->value);
737 #endif
740 result[nvariables] = (char *) xmalloc (100);
741 (void) sprintf (result[nvariables], "MAKELEVEL=%u", makelevel + 1);
742 result[++nvariables] = 0;
744 return result;
747 /* Given a variable, a value, and a flavor, define the variable.
748 See the try_variable_definition() function for details on the parameters. */
750 struct variable *
751 do_variable_definition (flocp, varname, value, origin, flavor, target_var)
752 const struct floc *flocp;
753 const char *varname;
754 char *value;
755 enum variable_origin origin;
756 enum variable_flavor flavor;
757 int target_var;
759 char *p, *alloc_value = NULL;
760 struct variable *v;
761 int append = 0;
763 /* Calculate the variable's new value in VALUE. */
765 switch (flavor)
767 default:
768 case f_bogus:
769 /* Should not be possible. */
770 abort ();
771 case f_simple:
772 /* A simple variable definition "var := value". Expand the value.
773 We have to allocate memory since otherwise it'll clobber the
774 variable buffer, and we may still need that if we're looking at a
775 target-specific variable. */
776 p = alloc_value = allocated_variable_expand (value);
777 break;
778 case f_conditional:
779 /* A conditional variable definition "var ?= value".
780 The value is set IFF the variable is not defined yet. */
781 v = lookup_variable (varname, strlen (varname));
782 if (v)
783 return v;
785 flavor = f_recursive;
786 /* FALLTHROUGH */
787 case f_recursive:
788 /* A recursive variable definition "var = value".
789 The value is used verbatim. */
790 p = value;
791 break;
792 case f_append:
794 /* If we have += but we're in a target variable context, we want to
795 append only with other variables in the context of this target. */
796 if (target_var)
798 append = 1;
799 v = lookup_variable_in_set (varname, strlen (varname),
800 current_variable_set_list->set);
802 else
803 v = lookup_variable (varname, strlen (varname));
805 if (v == 0)
807 /* There was no old value.
808 This becomes a normal recursive definition. */
809 p = value;
810 flavor = f_recursive;
812 else
814 /* Paste the old and new values together in VALUE. */
816 unsigned int oldlen, newlen;
818 p = value;
819 if (v->recursive)
820 /* The previous definition of the variable was recursive.
821 The new value is the unexpanded old and new values. */
822 flavor = f_recursive;
823 else
824 /* The previous definition of the variable was simple.
825 The new value comes from the old value, which was expanded
826 when it was set; and from the expanded new value. Allocate
827 memory for the expansion as we may still need the rest of the
828 buffer if we're looking at a target-specific variable. */
829 p = alloc_value = allocated_variable_expand (p);
831 oldlen = strlen (v->value);
832 newlen = strlen (p);
833 p = (char *) alloca (oldlen + 1 + newlen + 1);
834 bcopy (v->value, p, oldlen);
835 p[oldlen] = ' ';
836 bcopy (value, &p[oldlen + 1], newlen + 1);
841 #ifdef __MSDOS__
842 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
843 non-Unix systems don't conform to this default configuration (in
844 fact, most of them don't even have `/bin'). On the other hand,
845 $SHELL in the environment, if set, points to the real pathname of
846 the shell.
847 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
848 the Makefile override $SHELL from the environment. But first, we
849 look for the basename of the shell in the directory where SHELL=
850 points, and along the $PATH; if it is found in any of these places,
851 we define $SHELL to be the actual pathname of the shell. Thus, if
852 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
853 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
854 defining SHELL to be "d:/unix/bash.exe". */
855 if ((origin == o_file || origin == o_override)
856 && strcmp (varname, "SHELL") == 0)
858 char shellpath[PATH_MAX];
859 extern char * __dosexec_find_on_path (const char *, char *[], char *);
861 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
862 if (__dosexec_find_on_path (p, (char **)0, shellpath))
864 char *p;
866 for (p = shellpath; *p; p++)
868 if (*p == '\\')
869 *p = '/';
871 v = define_variable_loc (varname, strlen (varname),
872 shellpath, origin, flavor == f_recursive,
873 flocp);
875 else
877 char *shellbase, *bslash;
878 struct variable *pathv = lookup_variable ("PATH", 4);
879 char *path_string;
880 char *fake_env[2];
881 size_t pathlen = 0;
883 shellbase = strrchr (p, '/');
884 bslash = strrchr (p, '\\');
885 if (!shellbase || bslash > shellbase)
886 shellbase = bslash;
887 if (!shellbase && p[1] == ':')
888 shellbase = p + 1;
889 if (shellbase)
890 shellbase++;
891 else
892 shellbase = p;
894 /* Search for the basename of the shell (with standard
895 executable extensions) along the $PATH. */
896 if (pathv)
897 pathlen = strlen (pathv->value);
898 path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
899 /* On MSDOS, current directory is considered as part of $PATH. */
900 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
901 fake_env[0] = path_string;
902 fake_env[1] = (char *)0;
903 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
905 char *p;
907 for (p = shellpath; *p; p++)
909 if (*p == '\\')
910 *p = '/';
912 v = define_variable_loc (varname, strlen (varname),
913 shellpath, origin,
914 flavor == f_recursive, flocp);
916 else
917 v = lookup_variable (varname, strlen (varname));
919 free (path_string);
922 else
923 #endif /* __MSDOS__ */
924 #ifdef WINDOWS32
925 if ((origin == o_file || origin == o_override) && streq (varname, "SHELL"))
927 extern char *default_shell;
929 /* Call shell locator function. If it returns TRUE, then
930 set no_default_sh_exe to indicate sh was found and
931 set new value for SHELL variable. */
933 if (find_and_set_default_shell (p))
935 v = define_variable_in_set (varname, strlen (varname), default_shell,
936 origin, flavor == f_recursive,
937 (target_var
938 ? current_variable_set_list->set
939 : NULL),
940 flocp);
941 no_default_sh_exe = 0;
943 else
944 v = lookup_variable (varname, strlen (varname));
946 else
947 #endif
949 /* If we are defining variables inside an $(eval ...), we might have a
950 different variable context pushed, not the global context (maybe we're
951 inside a $(call ...) or something. Since this function is only ever
952 invoked in places where we want to define globally visible variables,
953 make sure we define this variable in the global set. */
955 v = define_variable_in_set (varname, strlen (varname), p,
956 origin, flavor == f_recursive,
957 (target_var
958 ? current_variable_set_list->set : NULL),
959 flocp);
960 v->append = append;
962 if (alloc_value)
963 free (alloc_value);
965 return v;
968 /* Try to interpret LINE (a null-terminated string) as a variable definition.
970 ORIGIN may be o_file, o_override, o_env, o_env_override,
971 or o_command specifying that the variable definition comes
972 from a makefile, an override directive, the environment with
973 or without the -e switch, or the command line.
975 See the comments for parse_variable_definition().
977 If LINE was recognized as a variable definition, a pointer to its `struct
978 variable' is returned. If LINE is not a variable definition, NULL is
979 returned. */
981 struct variable *
982 try_variable_definition (flocp, line, origin, target_var)
983 const struct floc *flocp;
984 char *line;
985 enum variable_origin origin;
986 int target_var;
988 register int c;
989 register char *p = line;
990 register char *beg;
991 register char *end;
992 enum variable_flavor flavor = f_bogus;
993 char *name, *expanded_name;
994 struct variable *v;
996 while (1)
998 c = *p++;
999 if (c == '\0' || c == '#')
1000 return 0;
1001 if (c == '=')
1003 end = p - 1;
1004 flavor = f_recursive;
1005 break;
1007 else if (c == ':')
1008 if (*p == '=')
1010 end = p++ - 1;
1011 flavor = f_simple;
1012 break;
1014 else
1015 /* A colon other than := is a rule line, not a variable defn. */
1016 return 0;
1017 else if (c == '+' && *p == '=')
1019 end = p++ - 1;
1020 flavor = f_append;
1021 break;
1023 else if (c == '?' && *p == '=')
1025 end = p++ - 1;
1026 flavor = f_conditional;
1027 break;
1029 else if (c == '$')
1031 /* This might begin a variable expansion reference. Make sure we
1032 don't misrecognize chars inside the reference as =, := or +=. */
1033 char closeparen;
1034 int count;
1035 c = *p++;
1036 if (c == '(')
1037 closeparen = ')';
1038 else if (c == '{')
1039 closeparen = '}';
1040 else
1041 continue; /* Nope. */
1043 /* P now points past the opening paren or brace.
1044 Count parens or braces until it is matched. */
1045 count = 0;
1046 for (; *p != '\0'; ++p)
1048 if (*p == c)
1049 ++count;
1050 else if (*p == closeparen && --count < 0)
1052 ++p;
1053 break;
1059 beg = next_token (line);
1060 while (end > beg && isblank ((unsigned char)end[-1]))
1061 --end;
1062 p = next_token (p);
1064 /* Expand the name, so "$(foo)bar = baz" works. */
1065 name = (char *) alloca (end - beg + 1);
1066 bcopy (beg, name, end - beg);
1067 name[end - beg] = '\0';
1068 expanded_name = allocated_variable_expand (name);
1070 if (expanded_name[0] == '\0')
1071 fatal (flocp, _("empty variable name"));
1073 v = do_variable_definition (flocp, expanded_name, p,
1074 origin, flavor, target_var);
1076 free (expanded_name);
1078 return v;
1081 /* Print information for variable V, prefixing it with PREFIX. */
1083 static void
1084 print_variable (v, prefix)
1085 register struct variable *v;
1086 char *prefix;
1088 const char *origin;
1090 switch (v->origin)
1092 case o_default:
1093 origin = _("default");
1094 break;
1095 case o_env:
1096 origin = _("environment");
1097 break;
1098 case o_file:
1099 origin = _("makefile");
1100 break;
1101 case o_env_override:
1102 origin = _("environment under -e");
1103 break;
1104 case o_command:
1105 origin = _("command line");
1106 break;
1107 case o_override:
1108 origin = _("`override' directive");
1109 break;
1110 case o_automatic:
1111 origin = _("automatic");
1112 break;
1113 case o_invalid:
1114 default:
1115 abort ();
1117 fputs ("# ", stdout);
1118 fputs (origin, stdout);
1119 if (v->fileinfo.filenm)
1120 printf (_(" (from `%s', line %lu)"),
1121 v->fileinfo.filenm, v->fileinfo.lineno);
1122 putchar ('\n');
1123 fputs (prefix, stdout);
1125 /* Is this a `define'? */
1126 if (v->recursive && strchr (v->value, '\n') != 0)
1127 printf ("define %s\n%s\nendef\n", v->name, v->value);
1128 else
1130 register char *p;
1132 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1134 /* Check if the value is just whitespace. */
1135 p = next_token (v->value);
1136 if (p != v->value && *p == '\0')
1137 /* All whitespace. */
1138 printf ("$(subst ,,%s)", v->value);
1139 else if (v->recursive)
1140 fputs (v->value, stdout);
1141 else
1142 /* Double up dollar signs. */
1143 for (p = v->value; *p != '\0'; ++p)
1145 if (*p == '$')
1146 putchar ('$');
1147 putchar (*p);
1149 putchar ('\n');
1154 /* Print all the variables in SET. PREFIX is printed before
1155 the actual variable definitions (everything else is comments). */
1157 void
1158 print_variable_set (set, prefix)
1159 register struct variable_set *set;
1160 char *prefix;
1162 register unsigned int i, nvariables, per_bucket;
1163 register struct variable *v;
1165 per_bucket = nvariables = 0;
1166 for (i = 0; i < set->buckets; ++i)
1168 register unsigned int this_bucket = 0;
1170 for (v = set->table[i]; v != 0; v = v->next)
1172 ++this_bucket;
1173 print_variable (v, prefix);
1176 nvariables += this_bucket;
1177 if (this_bucket > per_bucket)
1178 per_bucket = this_bucket;
1181 if (nvariables == 0)
1182 puts (_("# No variables."));
1183 else
1185 printf (_("# %u variables in %u hash buckets.\n"),
1186 nvariables, set->buckets);
1187 #ifndef NO_FLOAT
1188 printf (_("# average of %.1f variables per bucket, \
1189 max %u in one bucket.\n"),
1190 (double) nvariables / (double) set->buckets,
1191 per_bucket);
1192 #else
1194 int f = (nvariables * 1000 + 5) / set->buckets;
1195 printf (_("# average of %d.%d variables per bucket, \
1196 max %u in one bucket.\n"),
1197 f/10, f%10,
1198 per_bucket);
1200 #endif
1205 /* Print the data base of variables. */
1207 void
1208 print_variable_data_base ()
1210 puts (_("\n# Variables\n"));
1212 print_variable_set (&global_variable_set, "");
1216 /* Print all the local variables of FILE. */
1218 void
1219 print_file_variables (file)
1220 struct file *file;
1222 if (file->variables != 0)
1223 print_variable_set (file->variables->set, "# ");
1226 #ifdef WINDOWS32
1227 void
1228 sync_Path_environment(void)
1230 char* path = allocated_variable_expand("$(Path)");
1231 static char* environ_path = NULL;
1233 if (!path)
1234 return;
1237 * If done this before, don't leak memory unnecessarily.
1238 * Free the previous entry before allocating new one.
1240 if (environ_path)
1241 free(environ_path);
1244 * Create something WINDOWS32 world can grok
1246 convert_Path_to_windows32(path, ';');
1247 environ_path = concat("Path", "=", path);
1248 putenv(environ_path);
1249 free(path);
1251 #endif