Fix bug #2846.
[make.git] / variable.c
blob723c9504a8a33d1c561883422a39126863ca367c
1 /* Internals of variables for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997,
3 2002 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Make is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Make; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "make.h"
22 #include "dep.h"
23 #include "filedef.h"
24 #include "job.h"
25 #include "commands.h"
26 #include "variable.h"
27 #include "rule.h"
28 #ifdef WINDOWS32
29 #include "pathstuff.h"
30 #endif
31 #include "hash.h"
33 /* Hash table of all global variable definitions. */
35 static unsigned long
36 variable_hash_1 (const void *keyv)
38 struct variable const *key = (struct variable const *) keyv;
39 return_STRING_N_HASH_1 (key->name, key->length);
42 static unsigned long
43 variable_hash_2 (const void *keyv)
45 struct variable const *key = (struct variable const *) keyv;
46 return_STRING_N_HASH_2 (key->name, key->length);
49 static int
50 variable_hash_cmp (const void *xv, const void *yv)
52 struct variable const *x = (struct variable const *) xv;
53 struct variable const *y = (struct variable const *) yv;
54 int result = x->length - y->length;
55 if (result)
56 return result;
57 return_STRING_N_COMPARE (x->name, y->name, x->length);
60 #ifndef VARIABLE_BUCKETS
61 #define VARIABLE_BUCKETS 523
62 #endif
63 #ifndef PERFILE_VARIABLE_BUCKETS
64 #define PERFILE_VARIABLE_BUCKETS 23
65 #endif
66 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
67 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
68 #endif
70 static struct variable_set global_variable_set;
71 static struct variable_set_list global_setlist
72 = { 0, &global_variable_set };
73 struct variable_set_list *current_variable_set_list = &global_setlist;
75 /* Implement variables. */
77 void
78 init_hash_global_variable_set (void)
80 hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
81 variable_hash_1, variable_hash_2, variable_hash_cmp);
84 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
85 LENGTH is the length of NAME, which does not need to be null-terminated.
86 ORIGIN specifies the origin of the variable (makefile, command line
87 or environment).
88 If RECURSIVE is nonzero a flag is set in the variable saying
89 that it should be recursively re-expanded. */
91 struct variable *
92 define_variable_in_set (const char *name, unsigned int length,
93 char *value, enum variable_origin origin,
94 int recursive, struct variable_set *set,
95 const struct floc *flocp)
97 struct variable *v;
98 struct variable **var_slot;
99 struct variable var_key;
101 if (set == NULL)
102 set = &global_variable_set;
104 var_key.name = (char *) name;
105 var_key.length = length;
106 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
108 if (env_overrides && origin == o_env)
109 origin = o_env_override;
111 v = *var_slot;
112 if (! HASH_VACANT (v))
114 if (env_overrides && v->origin == o_env)
115 /* V came from in the environment. Since it was defined
116 before the switches were parsed, it wasn't affected by -e. */
117 v->origin = o_env_override;
119 /* A variable of this name is already defined.
120 If the old definition is from a stronger source
121 than this one, don't redefine it. */
122 if ((int) origin >= (int) v->origin)
124 if (v->value != 0)
125 free (v->value);
126 v->value = xstrdup (value);
127 if (flocp != 0)
128 v->fileinfo = *flocp;
129 else
130 v->fileinfo.filenm = 0;
131 v->origin = origin;
132 v->recursive = recursive;
134 return v;
137 /* Create a new variable definition and add it to the hash table. */
139 v = (struct variable *) xmalloc (sizeof (struct variable));
140 v->name = savestring (name, length);
141 v->length = length;
142 hash_insert_at (&set->table, v, var_slot);
143 v->value = xstrdup (value);
144 if (flocp != 0)
145 v->fileinfo = *flocp;
146 else
147 v->fileinfo.filenm = 0;
148 v->origin = origin;
149 v->recursive = recursive;
150 v->expanding = 0;
151 v->exp_count = 0;
152 v->per_target = 0;
153 v->append = 0;
154 v->export = v_default;
156 v->exportable = 1;
157 if (*name != '_' && (*name < 'A' || *name > 'Z')
158 && (*name < 'a' || *name > 'z'))
159 v->exportable = 0;
160 else
162 for (++name; *name != '\0'; ++name)
163 if (*name != '_' && (*name < 'a' || *name > 'z')
164 && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
165 break;
167 if (*name != '\0')
168 v->exportable = 0;
171 return v;
174 /* If the variable passed in is "special", handle its special nature.
175 Currently there are two such variables, both used for introspection:
176 .VARIABLES expands to a list of all the variables defined in this instance
177 of make.
178 .TARGETS expands to a list of all the targets defined in this
179 instance of make.
180 Returns the variable reference passed in. */
182 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
184 static struct variable *
185 handle_special_var (struct variable *var)
187 static unsigned long last_var_count = 0;
190 /* This one actually turns out to be very hard, due to the way the parser
191 records targets. The way it works is that target information is collected
192 internally until make knows the target is completely specified. It unitl
193 it sees that some new construct (a new target or variable) is defined that
194 it knows the previous one is done. In short, this means that if you do
195 this:
197 all:
199 TARGS := $(.TARGETS)
201 then $(TARGS) won't contain "all", because it's not until after the
202 variable is created that the previous target is completed.
204 Changing this would be a major pain. I think a less complex way to do it
205 would be to pre-define the target files as soon as the first line is
206 parsed, then come back and do the rest of the definition as now. That
207 would allow $(.TARGETS) to be correct without a major change to the way
208 the parser works.
210 if (streq (var->name, ".TARGETS"))
211 var->value = build_target_list (var->value);
212 else
215 if (streq (var->name, ".VARIABLES")
216 && global_variable_set.table.ht_fill != last_var_count)
218 unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
219 unsigned long len;
220 char *p;
221 struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
222 struct variable **end = &vp[global_variable_set.table.ht_size];
224 /* Make sure we have at least MAX bytes in the allocated buffer. */
225 var->value = xrealloc (var->value, max);
227 /* Walk through the hash of variables, constructing a list of names. */
228 p = var->value;
229 len = 0;
230 for (; vp < end; ++vp)
231 if (!HASH_VACANT (*vp))
233 struct variable *v = *vp;
234 int l = v->length;
236 len += l + 1;
237 if (len > max)
239 unsigned long off = p - var->value;
241 max += EXPANSION_INCREMENT (l + 1);
242 var->value = xrealloc (var->value, max);
243 p = &var->value[off];
246 bcopy (v->name, p, l);
247 p += l;
248 *(p++) = ' ';
250 *(p-1) = '\0';
252 /* Remember how many variables are in our current count. Since we never
253 remove variables from the list, this is a reliable way to know whether
254 the list is up to date or needs to be recomputed. */
256 last_var_count = global_variable_set.table.ht_fill;
259 return var;
263 /* Lookup a variable whose name is a string starting at NAME
264 and with LENGTH chars. NAME need not be null-terminated.
265 Returns address of the `struct variable' containing all info
266 on the variable, or nil if no such variable is defined. */
268 struct variable *
269 lookup_variable (const char *name, unsigned int length)
271 const struct variable_set_list *setlist;
272 struct variable var_key;
274 var_key.name = (char *) name;
275 var_key.length = length;
277 for (setlist = current_variable_set_list;
278 setlist != 0; setlist = setlist->next)
280 const struct variable_set *set = setlist->set;
281 struct variable *v;
283 v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
284 if (v)
285 return v->special ? handle_special_var (v) : v;
288 #ifdef VMS
289 /* since we don't read envp[] on startup, try to get the
290 variable via getenv() here. */
292 char *vname = alloca (length + 1);
293 char *value;
294 strncpy (vname, name, length);
295 vname[length] = 0;
296 value = getenv (vname);
297 if (value != 0)
299 char *sptr;
300 int scnt;
302 sptr = value;
303 scnt = 0;
305 while ((sptr = strchr (sptr, '$')))
307 scnt++;
308 sptr++;
311 if (scnt > 0)
313 char *nvalue;
314 char *nptr;
316 nvalue = alloca (strlen (value) + scnt + 1);
317 sptr = value;
318 nptr = nvalue;
320 while (*sptr)
322 if (*sptr == '$')
324 *nptr++ = '$';
325 *nptr++ = '$';
327 else
329 *nptr++ = *sptr;
331 sptr++;
334 *nptr = '\0';
335 return define_variable (vname, length, nvalue, o_env, 1);
339 return define_variable (vname, length, value, o_env, 1);
342 #endif /* VMS */
344 return 0;
347 /* Lookup a variable whose name is a string starting at NAME
348 and with LENGTH chars in set SET. NAME need not be null-terminated.
349 Returns address of the `struct variable' containing all info
350 on the variable, or nil if no such variable is defined. */
352 struct variable *
353 lookup_variable_in_set (const char *name, unsigned int length,
354 const struct variable_set *set)
356 struct variable var_key;
358 var_key.name = (char *) name;
359 var_key.length = length;
361 return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
364 /* Initialize FILE's variable set list. If FILE already has a variable set
365 list, the topmost variable set is left intact, but the the rest of the
366 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
367 rule, then we will use the "root" double-colon target's variable set as the
368 parent of FILE's variable set.
370 If we're READing a makefile, don't do the pattern variable search now,
371 since the pattern variable might not have been defined yet. */
373 void
374 initialize_file_variables (struct file *file, int reading)
376 register struct variable_set_list *l = file->variables;
378 if (l == 0)
380 l = (struct variable_set_list *)
381 xmalloc (sizeof (struct variable_set_list));
382 l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
383 hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
384 variable_hash_1, variable_hash_2, variable_hash_cmp);
385 file->variables = l;
388 /* If this is a double-colon, then our "parent" is the "root" target for
389 this double-colon rule. Since that rule has the same name, parent,
390 etc. we can just use its variables as the "next" for ours. */
392 if (file->double_colon && file->double_colon != file)
394 initialize_file_variables (file->double_colon, reading);
395 l->next = file->double_colon->variables;
396 return;
399 if (file->parent == 0)
400 l->next = &global_setlist;
401 else
403 initialize_file_variables (file->parent, reading);
404 l->next = file->parent->variables;
407 /* If we're not reading makefiles and we haven't looked yet, see if
408 we can find a pattern variable. */
410 if (!reading && !file->pat_searched)
412 struct pattern_var *p = lookup_pattern_var (file->name);
414 file->pat_searched = 1;
415 if (p != 0)
417 /* If we found one, insert it between the current target's
418 variables and the next set, whatever it is. */
419 file->pat_variables = (struct variable_set_list *)
420 xmalloc (sizeof (struct variable_set_list));
421 file->pat_variables->set = p->vars->set;
425 /* If we have a pattern variable match, set it up. */
427 if (file->pat_variables != 0)
429 file->pat_variables->next = l->next;
430 l->next = file->pat_variables;
434 /* Pop the top set off the current variable set list,
435 and free all its storage. */
437 static void
438 free_variable_name_and_value (const void *item)
440 struct variable *v = (struct variable *) item;
441 free (v->name);
442 free (v->value);
445 void
446 pop_variable_scope (void)
448 struct variable_set_list *setlist = current_variable_set_list;
449 struct variable_set *set = setlist->set;
451 current_variable_set_list = setlist->next;
452 free ((char *) setlist);
454 hash_map (&set->table, free_variable_name_and_value);
455 hash_free (&set->table, 1);
457 free ((char *) set);
460 struct variable_set_list *
461 create_new_variable_set (void)
463 register struct variable_set_list *setlist;
464 register struct variable_set *set;
466 set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
467 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
468 variable_hash_1, variable_hash_2, variable_hash_cmp);
470 setlist = (struct variable_set_list *)
471 xmalloc (sizeof (struct variable_set_list));
472 setlist->set = set;
473 setlist->next = current_variable_set_list;
475 return setlist;
478 /* Create a new variable set and push it on the current setlist. */
480 struct variable_set_list *
481 push_new_variable_scope (void)
483 return (current_variable_set_list = create_new_variable_set());
486 /* Merge SET1 into SET0, freeing unused storage in SET1. */
488 static void
489 merge_variable_sets (struct variable_set *to_set,
490 struct variable_set *from_set)
492 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
493 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
495 for ( ; from_var_slot < from_var_end; from_var_slot++)
496 if (! HASH_VACANT (*from_var_slot))
498 struct variable *from_var = *from_var_slot;
499 struct variable **to_var_slot
500 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
501 if (HASH_VACANT (*to_var_slot))
502 hash_insert_at (&to_set->table, from_var, to_var_slot);
503 else
505 /* GKM FIXME: delete in from_set->table */
506 free (from_var->value);
507 free (from_var);
512 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
514 void
515 merge_variable_set_lists (struct variable_set_list **setlist0,
516 struct variable_set_list *setlist1)
518 register struct variable_set_list *list0 = *setlist0;
519 struct variable_set_list *last0 = 0;
521 while (setlist1 != 0 && list0 != 0)
523 struct variable_set_list *next = setlist1;
524 setlist1 = setlist1->next;
526 merge_variable_sets (list0->set, next->set);
528 last0 = list0;
529 list0 = list0->next;
532 if (setlist1 != 0)
534 if (last0 == 0)
535 *setlist0 = setlist1;
536 else
537 last0->next = setlist1;
541 /* Define the automatic variables, and record the addresses
542 of their structures so we can change their values quickly. */
544 void
545 define_automatic_variables (void)
547 #if defined(WINDOWS32) || defined(__EMX__)
548 extern char* default_shell;
549 #else
550 extern char default_shell[];
551 #endif
552 register struct variable *v;
553 char buf[200];
555 sprintf (buf, "%u", makelevel);
556 (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
558 sprintf (buf, "%s%s%s",
559 version_string,
560 (remote_description == 0 || remote_description[0] == '\0')
561 ? "" : "-",
562 (remote_description == 0 || remote_description[0] == '\0')
563 ? "" : remote_description);
564 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
566 #ifdef __MSDOS__
567 /* Allow to specify a special shell just for Make,
568 and use $COMSPEC as the default $SHELL when appropriate. */
570 static char shell_str[] = "SHELL";
571 const int shlen = sizeof (shell_str) - 1;
572 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
573 struct variable *comp = lookup_variable ("COMSPEC", 7);
575 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
576 if (mshp)
577 (void) define_variable (shell_str, shlen,
578 mshp->value, o_env_override, 0);
579 else if (comp)
581 /* $COMSPEC shouldn't override $SHELL. */
582 struct variable *shp = lookup_variable (shell_str, shlen);
584 if (!shp)
585 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
588 #elif defined(__EMX__)
590 static char shell_str[] = "SHELL";
591 const int shlen = sizeof (shell_str) - 1;
592 struct variable *shell = lookup_variable (shell_str, shlen);
593 struct variable *replace = lookup_variable ("MAKESHELL", 9);
595 /* if $MAKESHELL is defined in the environment assume o_env_override */
596 if (replace && *replace->value && replace->origin == o_env)
597 replace->origin = o_env_override;
599 /* if $MAKESHELL is not defined use $SHELL but only if the variable
600 did not come from the environment */
601 if (!replace || !*replace->value)
602 if (shell && *shell->value && (shell->origin == o_env
603 || shell->origin == o_env_override))
605 /* overwrite whatever we got from the environment */
606 free(shell->value);
607 shell->value = xstrdup (default_shell);
608 shell->origin = o_default;
611 /* Some people do not like cmd to be used as the default
612 if $SHELL is not defined in the Makefile.
613 With -DNO_CMD_DEFAULT you can turn off this behaviour */
614 # ifndef NO_CMD_DEFAULT
615 /* otherwise use $COMSPEC */
616 if (!replace || !*replace->value)
617 replace = lookup_variable ("COMSPEC", 7);
619 /* otherwise use $OS2_SHELL */
620 if (!replace || !*replace->value)
621 replace = lookup_variable ("OS2_SHELL", 9);
622 # else
623 # warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
624 # endif
626 if (replace && *replace->value)
627 /* overwrite $SHELL */
628 (void) define_variable (shell_str, shlen, replace->value,
629 replace->origin, 0);
630 else
631 /* provide a definition if there is none */
632 (void) define_variable (shell_str, shlen, default_shell,
633 o_default, 0);
636 #endif
638 /* This won't override any definition, but it
639 will provide one if there isn't one there. */
640 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
641 v->export = v_export; /* Always export SHELL. */
643 /* On MSDOS we do use SHELL from environment, since
644 it isn't a standard environment variable on MSDOS,
645 so whoever sets it, does that on purpose.
646 On OS/2 we do not use SHELL from environment but
647 we have already handled that problem above. */
648 #if !defined(__MSDOS__) && !defined(__EMX__)
649 /* Don't let SHELL come from the environment. */
650 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
652 free (v->value);
653 v->origin = o_file;
654 v->value = xstrdup (default_shell);
656 #endif
658 /* Make sure MAKEFILES gets exported if it is set. */
659 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
660 v->export = v_ifset;
662 /* Define the magic D and F variables in terms of
663 the automatic variables they are variations of. */
665 #ifdef VMS
666 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
667 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
668 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
669 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
670 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
671 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
672 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
673 #else
674 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
675 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
676 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
677 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
678 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
679 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
680 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
681 #endif
682 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
683 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
684 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
685 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
686 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
687 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
688 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
691 int export_all_variables;
693 /* Create a new environment for FILE's commands.
694 If FILE is nil, this is for the `shell' function.
695 The child's MAKELEVEL variable is incremented. */
697 char **
698 target_environment (struct file *file)
700 struct variable_set_list *set_list;
701 register struct variable_set_list *s;
702 struct hash_table table;
703 struct variable **v_slot;
704 struct variable **v_end;
705 struct variable makelevel_key;
706 char **result_0;
707 char **result;
709 if (file == 0)
710 set_list = current_variable_set_list;
711 else
712 set_list = file->variables;
714 hash_init (&table, VARIABLE_BUCKETS,
715 variable_hash_1, variable_hash_2, variable_hash_cmp);
717 /* Run through all the variable sets in the list,
718 accumulating variables in TABLE. */
719 for (s = set_list; s != 0; s = s->next)
721 struct variable_set *set = s->set;
722 v_slot = (struct variable **) set->table.ht_vec;
723 v_end = v_slot + set->table.ht_size;
724 for ( ; v_slot < v_end; v_slot++)
725 if (! HASH_VACANT (*v_slot))
727 struct variable **new_slot;
728 struct variable *v = *v_slot;
730 /* If this is a per-target variable and it hasn't been touched
731 already then look up the global version and take its export
732 value. */
733 if (v->per_target && v->export == v_default)
735 struct variable *gv;
737 gv = lookup_variable_in_set (v->name, strlen(v->name),
738 &global_variable_set);
739 if (gv)
740 v->export = gv->export;
743 switch (v->export)
745 case v_default:
746 if (v->origin == o_default || v->origin == o_automatic)
747 /* Only export default variables by explicit request. */
748 continue;
750 /* The variable doesn't have a name that can be exported. */
751 if (! v->exportable)
752 continue;
754 if (! export_all_variables
755 && v->origin != o_command
756 && v->origin != o_env && v->origin != o_env_override)
757 continue;
758 break;
760 case v_export:
761 break;
763 case v_noexport:
764 continue;
766 case v_ifset:
767 if (v->origin == o_default)
768 continue;
769 break;
772 new_slot = (struct variable **) hash_find_slot (&table, v);
773 if (HASH_VACANT (*new_slot))
774 hash_insert_at (&table, v, new_slot);
778 makelevel_key.name = MAKELEVEL_NAME;
779 makelevel_key.length = MAKELEVEL_LENGTH;
780 hash_delete (&table, &makelevel_key);
782 result = result_0 = (char **) xmalloc ((table.ht_fill + 2) * sizeof (char *));
784 v_slot = (struct variable **) table.ht_vec;
785 v_end = v_slot + table.ht_size;
786 for ( ; v_slot < v_end; v_slot++)
787 if (! HASH_VACANT (*v_slot))
789 struct variable *v = *v_slot;
791 /* If V is recursively expanded and didn't come from the environment,
792 expand its value. If it came from the environment, it should
793 go back into the environment unchanged. */
794 if (v->recursive
795 && v->origin != o_env && v->origin != o_env_override)
797 char *value = recursively_expand_for_file (v, file);
798 #ifdef WINDOWS32
799 if (strcmp(v->name, "Path") == 0 ||
800 strcmp(v->name, "PATH") == 0)
801 convert_Path_to_windows32(value, ';');
802 #endif
803 *result++ = concat (v->name, "=", value);
804 free (value);
806 else
808 #ifdef WINDOWS32
809 if (strcmp(v->name, "Path") == 0 ||
810 strcmp(v->name, "PATH") == 0)
811 convert_Path_to_windows32(v->value, ';');
812 #endif
813 *result++ = concat (v->name, "=", v->value);
817 *result = (char *) xmalloc (100);
818 (void) sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
819 *++result = 0;
821 hash_free (&table, 0);
823 return result_0;
826 /* Given a variable, a value, and a flavor, define the variable.
827 See the try_variable_definition() function for details on the parameters. */
829 struct variable *
830 do_variable_definition (const struct floc *flocp, const char *varname,
831 char *value, enum variable_origin origin,
832 enum variable_flavor flavor, int target_var)
834 char *p, *alloc_value = NULL;
835 struct variable *v;
836 int append = 0;
838 /* Calculate the variable's new value in VALUE. */
840 switch (flavor)
842 default:
843 case f_bogus:
844 /* Should not be possible. */
845 abort ();
846 case f_simple:
847 /* A simple variable definition "var := value". Expand the value.
848 We have to allocate memory since otherwise it'll clobber the
849 variable buffer, and we may still need that if we're looking at a
850 target-specific variable. */
851 p = alloc_value = allocated_variable_expand (value);
852 break;
853 case f_conditional:
854 /* A conditional variable definition "var ?= value".
855 The value is set IFF the variable is not defined yet. */
856 v = lookup_variable (varname, strlen (varname));
857 if (v)
858 return v;
860 flavor = f_recursive;
861 /* FALLTHROUGH */
862 case f_recursive:
863 /* A recursive variable definition "var = value".
864 The value is used verbatim. */
865 p = value;
866 break;
867 case f_append:
869 /* If we have += but we're in a target variable context, we want to
870 append only with other variables in the context of this target. */
871 if (target_var)
873 append = 1;
874 v = lookup_variable_in_set (varname, strlen (varname),
875 current_variable_set_list->set);
877 else
878 v = lookup_variable (varname, strlen (varname));
880 if (v == 0)
882 /* There was no old value.
883 This becomes a normal recursive definition. */
884 p = value;
885 flavor = f_recursive;
887 else
889 /* Paste the old and new values together in VALUE. */
891 unsigned int oldlen, vallen;
892 char *val;
894 val = value;
895 if (v->recursive)
896 /* The previous definition of the variable was recursive.
897 The new value is the unexpanded old and new values. */
898 flavor = f_recursive;
899 else
900 /* The previous definition of the variable was simple.
901 The new value comes from the old value, which was expanded
902 when it was set; and from the expanded new value. Allocate
903 memory for the expansion as we may still need the rest of the
904 buffer if we're looking at a target-specific variable. */
905 val = alloc_value = allocated_variable_expand (val);
907 oldlen = strlen (v->value);
908 vallen = strlen (val);
909 p = (char *) alloca (oldlen + 1 + vallen + 1);
910 bcopy (v->value, p, oldlen);
911 p[oldlen] = ' ';
912 bcopy (val, &p[oldlen + 1], vallen + 1);
917 #ifdef __MSDOS__
918 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
919 non-Unix systems don't conform to this default configuration (in
920 fact, most of them don't even have `/bin'). On the other hand,
921 $SHELL in the environment, if set, points to the real pathname of
922 the shell.
923 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
924 the Makefile override $SHELL from the environment. But first, we
925 look for the basename of the shell in the directory where SHELL=
926 points, and along the $PATH; if it is found in any of these places,
927 we define $SHELL to be the actual pathname of the shell. Thus, if
928 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
929 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
930 defining SHELL to be "d:/unix/bash.exe". */
931 if ((origin == o_file || origin == o_override)
932 && strcmp (varname, "SHELL") == 0)
934 char shellpath[PATH_MAX];
935 extern char * __dosexec_find_on_path (const char *, char *[], char *);
937 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
938 if (__dosexec_find_on_path (p, (char **)0, shellpath))
940 char *p;
942 for (p = shellpath; *p; p++)
944 if (*p == '\\')
945 *p = '/';
947 v = define_variable_loc (varname, strlen (varname),
948 shellpath, origin, flavor == f_recursive,
949 flocp);
951 else
953 char *shellbase, *bslash;
954 struct variable *pathv = lookup_variable ("PATH", 4);
955 char *path_string;
956 char *fake_env[2];
957 size_t pathlen = 0;
959 shellbase = strrchr (p, '/');
960 bslash = strrchr (p, '\\');
961 if (!shellbase || bslash > shellbase)
962 shellbase = bslash;
963 if (!shellbase && p[1] == ':')
964 shellbase = p + 1;
965 if (shellbase)
966 shellbase++;
967 else
968 shellbase = p;
970 /* Search for the basename of the shell (with standard
971 executable extensions) along the $PATH. */
972 if (pathv)
973 pathlen = strlen (pathv->value);
974 path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
975 /* On MSDOS, current directory is considered as part of $PATH. */
976 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
977 fake_env[0] = path_string;
978 fake_env[1] = (char *)0;
979 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
981 char *p;
983 for (p = shellpath; *p; p++)
985 if (*p == '\\')
986 *p = '/';
988 v = define_variable_loc (varname, strlen (varname),
989 shellpath, origin,
990 flavor == f_recursive, flocp);
992 else
993 v = lookup_variable (varname, strlen (varname));
995 free (path_string);
998 else
999 #endif /* __MSDOS__ */
1000 #ifdef WINDOWS32
1001 if ((origin == o_file || origin == o_override) && streq (varname, "SHELL"))
1003 extern char *default_shell;
1005 /* Call shell locator function. If it returns TRUE, then
1006 set no_default_sh_exe to indicate sh was found and
1007 set new value for SHELL variable. */
1009 if (find_and_set_default_shell (p))
1011 v = define_variable_in_set (varname, strlen (varname), default_shell,
1012 origin, flavor == f_recursive,
1013 (target_var
1014 ? current_variable_set_list->set
1015 : NULL),
1016 flocp);
1017 no_default_sh_exe = 0;
1019 else
1020 v = lookup_variable (varname, strlen (varname));
1022 else
1023 #endif
1025 /* If we are defining variables inside an $(eval ...), we might have a
1026 different variable context pushed, not the global context (maybe we're
1027 inside a $(call ...) or something. Since this function is only ever
1028 invoked in places where we want to define globally visible variables,
1029 make sure we define this variable in the global set. */
1031 v = define_variable_in_set (varname, strlen (varname), p,
1032 origin, flavor == f_recursive,
1033 (target_var
1034 ? current_variable_set_list->set : NULL),
1035 flocp);
1036 v->append = append;
1038 if (alloc_value)
1039 free (alloc_value);
1041 return v;
1044 /* Try to interpret LINE (a null-terminated string) as a variable definition.
1046 ORIGIN may be o_file, o_override, o_env, o_env_override,
1047 or o_command specifying that the variable definition comes
1048 from a makefile, an override directive, the environment with
1049 or without the -e switch, or the command line.
1051 See the comments for parse_variable_definition().
1053 If LINE was recognized as a variable definition, a pointer to its `struct
1054 variable' is returned. If LINE is not a variable definition, NULL is
1055 returned. */
1057 struct variable *
1058 try_variable_definition (const struct floc *flocp, char *line,
1059 enum variable_origin origin, int target_var)
1061 register int c;
1062 register char *p = line;
1063 register char *beg;
1064 register char *end;
1065 enum variable_flavor flavor = f_bogus;
1066 char *name, *expanded_name;
1067 struct variable *v;
1069 while (1)
1071 c = *p++;
1072 if (c == '\0' || c == '#')
1073 return 0;
1074 if (c == '=')
1076 end = p - 1;
1077 flavor = f_recursive;
1078 break;
1080 else if (c == ':')
1081 if (*p == '=')
1083 end = p++ - 1;
1084 flavor = f_simple;
1085 break;
1087 else
1088 /* A colon other than := is a rule line, not a variable defn. */
1089 return 0;
1090 else if (c == '+' && *p == '=')
1092 end = p++ - 1;
1093 flavor = f_append;
1094 break;
1096 else if (c == '?' && *p == '=')
1098 end = p++ - 1;
1099 flavor = f_conditional;
1100 break;
1102 else if (c == '$')
1104 /* This might begin a variable expansion reference. Make sure we
1105 don't misrecognize chars inside the reference as =, := or +=. */
1106 char closeparen;
1107 int count;
1108 c = *p++;
1109 if (c == '(')
1110 closeparen = ')';
1111 else if (c == '{')
1112 closeparen = '}';
1113 else
1114 continue; /* Nope. */
1116 /* P now points past the opening paren or brace.
1117 Count parens or braces until it is matched. */
1118 count = 0;
1119 for (; *p != '\0'; ++p)
1121 if (*p == c)
1122 ++count;
1123 else if (*p == closeparen && --count < 0)
1125 ++p;
1126 break;
1132 beg = next_token (line);
1133 while (end > beg && isblank ((unsigned char)end[-1]))
1134 --end;
1135 p = next_token (p);
1137 /* Expand the name, so "$(foo)bar = baz" works. */
1138 name = (char *) alloca (end - beg + 1);
1139 bcopy (beg, name, end - beg);
1140 name[end - beg] = '\0';
1141 expanded_name = allocated_variable_expand (name);
1143 if (expanded_name[0] == '\0')
1144 fatal (flocp, _("empty variable name"));
1146 v = do_variable_definition (flocp, expanded_name, p,
1147 origin, flavor, target_var);
1149 free (expanded_name);
1151 return v;
1154 /* Print information for variable V, prefixing it with PREFIX. */
1156 static void
1157 print_variable (const void *item, void *arg)
1159 const struct variable *v = (struct variable *) item;
1160 const char *prefix = (char *) arg;
1161 const char *origin;
1163 switch (v->origin)
1165 case o_default:
1166 origin = _("default");
1167 break;
1168 case o_env:
1169 origin = _("environment");
1170 break;
1171 case o_file:
1172 origin = _("makefile");
1173 break;
1174 case o_env_override:
1175 origin = _("environment under -e");
1176 break;
1177 case o_command:
1178 origin = _("command line");
1179 break;
1180 case o_override:
1181 origin = _("`override' directive");
1182 break;
1183 case o_automatic:
1184 origin = _("automatic");
1185 break;
1186 case o_invalid:
1187 default:
1188 abort ();
1190 fputs ("# ", stdout);
1191 fputs (origin, stdout);
1192 if (v->fileinfo.filenm)
1193 printf (_(" (from `%s', line %lu)"),
1194 v->fileinfo.filenm, v->fileinfo.lineno);
1195 putchar ('\n');
1196 fputs (prefix, stdout);
1198 /* Is this a `define'? */
1199 if (v->recursive && strchr (v->value, '\n') != 0)
1200 printf ("define %s\n%s\nendef\n", v->name, v->value);
1201 else
1203 register char *p;
1205 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1207 /* Check if the value is just whitespace. */
1208 p = next_token (v->value);
1209 if (p != v->value && *p == '\0')
1210 /* All whitespace. */
1211 printf ("$(subst ,,%s)", v->value);
1212 else if (v->recursive)
1213 fputs (v->value, stdout);
1214 else
1215 /* Double up dollar signs. */
1216 for (p = v->value; *p != '\0'; ++p)
1218 if (*p == '$')
1219 putchar ('$');
1220 putchar (*p);
1222 putchar ('\n');
1227 /* Print all the variables in SET. PREFIX is printed before
1228 the actual variable definitions (everything else is comments). */
1230 void
1231 print_variable_set (struct variable_set *set, char *prefix)
1233 hash_map_arg (&set->table, print_variable, prefix);
1235 fputs (_("# variable set hash-table stats:\n"), stdout);
1236 fputs ("# ", stdout);
1237 hash_print_stats (&set->table, stdout);
1238 putc ('\n', stdout);
1241 /* Print the data base of variables. */
1243 void
1244 print_variable_data_base (void)
1246 puts (_("\n# Variables\n"));
1248 print_variable_set (&global_variable_set, "");
1252 /* Print all the local variables of FILE. */
1254 void
1255 print_file_variables (struct file *file)
1257 if (file->variables != 0)
1258 print_variable_set (file->variables->set, "# ");
1261 #ifdef WINDOWS32
1262 void
1263 sync_Path_environment (void)
1265 char *path = allocated_variable_expand ("$(Path)");
1266 static char *environ_path = NULL;
1268 if (!path)
1269 return;
1272 * If done this before, don't leak memory unnecessarily.
1273 * Free the previous entry before allocating new one.
1275 if (environ_path)
1276 free (environ_path);
1279 * Create something WINDOWS32 world can grok
1281 convert_Path_to_windows32 (path, ';');
1282 environ_path = concat ("Path", "=", path);
1283 putenv (environ_path);
1284 free (path);
1286 #endif