Fix for the problem that SDL applications exited
[AROS-Contrib.git] / gnu / make / variable.c
blob7b9d0d8db41c57f3a3fa0210d8ee60acb913253e
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 /* Chain of all pattern-specific variables. */
35 static struct pattern_var *pattern_vars;
37 /* Pointer to last struct in the chain, so we can add onto the end. */
39 static struct pattern_var *last_pattern_var;
41 /* Create a new pattern-specific variable struct. */
43 struct pattern_var *
44 create_pattern_var (char *target, char *suffix)
46 register struct pattern_var *p
47 = (struct pattern_var *) xmalloc (sizeof (struct pattern_var));
49 if (last_pattern_var != 0)
50 last_pattern_var->next = p;
51 else
52 pattern_vars = p;
53 last_pattern_var = p;
54 p->next = 0;
56 p->target = target;
57 p->len = strlen (target);
58 p->suffix = suffix + 1;
60 return p;
63 /* Look up a target in the pattern-specific variable list. */
65 static struct pattern_var *
66 lookup_pattern_var (struct pattern_var *start, char *target)
68 struct pattern_var *p;
69 unsigned int targlen = strlen(target);
71 for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
73 char *stem;
74 unsigned int stemlen;
76 if (p->len > targlen)
77 /* It can't possibly match. */
78 continue;
80 /* From the lengths of the filename and the pattern parts,
81 find the stem: the part of the filename that matches the %. */
82 stem = target + (p->suffix - p->target - 1);
83 stemlen = targlen - p->len + 1;
85 /* Compare the text in the pattern before the stem, if any. */
86 if (stem > target && !strneq (p->target, target, stem - target))
87 continue;
89 /* Compare the text in the pattern after the stem, if any.
90 We could test simply using streq, but this way we compare the
91 first two characters immediately. This saves time in the very
92 common case where the first character matches because it is a
93 period. */
94 if (*p->suffix == stem[stemlen]
95 && (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
96 break;
99 return p;
102 /* Hash table of all global variable definitions. */
104 static unsigned long
105 variable_hash_1 (const void *keyv)
107 struct variable const *key = (struct variable const *) keyv;
108 return_STRING_N_HASH_1 (key->name, key->length);
111 static unsigned long
112 variable_hash_2 (const void *keyv)
114 struct variable const *key = (struct variable const *) keyv;
115 return_STRING_N_HASH_2 (key->name, key->length);
118 static int
119 variable_hash_cmp (const void *xv, const void *yv)
121 struct variable const *x = (struct variable const *) xv;
122 struct variable const *y = (struct variable const *) yv;
123 int result = x->length - y->length;
124 if (result)
125 return result;
126 return_STRING_N_COMPARE (x->name, y->name, x->length);
129 #ifndef VARIABLE_BUCKETS
130 #define VARIABLE_BUCKETS 523
131 #endif
132 #ifndef PERFILE_VARIABLE_BUCKETS
133 #define PERFILE_VARIABLE_BUCKETS 23
134 #endif
135 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
136 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
137 #endif
139 static struct variable_set global_variable_set;
140 static struct variable_set_list global_setlist
141 = { 0, &global_variable_set };
142 struct variable_set_list *current_variable_set_list = &global_setlist;
144 /* Implement variables. */
146 void
147 init_hash_global_variable_set (void)
149 hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
150 variable_hash_1, variable_hash_2, variable_hash_cmp);
153 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
154 LENGTH is the length of NAME, which does not need to be null-terminated.
155 ORIGIN specifies the origin of the variable (makefile, command line
156 or environment).
157 If RECURSIVE is nonzero a flag is set in the variable saying
158 that it should be recursively re-expanded. */
160 struct variable *
161 define_variable_in_set (const char *name, unsigned int length,
162 char *value, enum variable_origin origin,
163 int recursive, struct variable_set *set,
164 const struct floc *flocp)
166 struct variable *v;
167 struct variable **var_slot;
168 struct variable var_key;
170 if (set == NULL)
171 set = &global_variable_set;
173 var_key.name = (char *) name;
174 var_key.length = length;
175 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
177 if (env_overrides && origin == o_env)
178 origin = o_env_override;
180 v = *var_slot;
181 if (! HASH_VACANT (v))
183 if (env_overrides && v->origin == o_env)
184 /* V came from in the environment. Since it was defined
185 before the switches were parsed, it wasn't affected by -e. */
186 v->origin = o_env_override;
188 /* A variable of this name is already defined.
189 If the old definition is from a stronger source
190 than this one, don't redefine it. */
191 if ((int) origin >= (int) v->origin)
193 if (v->value != 0)
194 free (v->value);
195 v->value = xstrdup (value);
196 if (flocp != 0)
197 v->fileinfo = *flocp;
198 else
199 v->fileinfo.filenm = 0;
200 v->origin = origin;
201 v->recursive = recursive;
203 return v;
206 /* Create a new variable definition and add it to the hash table. */
208 v = (struct variable *) xmalloc (sizeof (struct variable));
209 v->name = savestring (name, length);
210 v->length = length;
211 hash_insert_at (&set->table, v, var_slot);
212 v->value = xstrdup (value);
213 if (flocp != 0)
214 v->fileinfo = *flocp;
215 else
216 v->fileinfo.filenm = 0;
217 v->origin = origin;
218 v->recursive = recursive;
219 v->special = 0;
220 v->expanding = 0;
221 v->exp_count = 0;
222 v->per_target = 0;
223 v->append = 0;
224 v->export = v_default;
226 v->exportable = 1;
227 if (*name != '_' && (*name < 'A' || *name > 'Z')
228 && (*name < 'a' || *name > 'z'))
229 v->exportable = 0;
230 else
232 for (++name; *name != '\0'; ++name)
233 if (*name != '_' && (*name < 'a' || *name > 'z')
234 && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
235 break;
237 if (*name != '\0')
238 v->exportable = 0;
241 return v;
244 /* If the variable passed in is "special", handle its special nature.
245 Currently there are two such variables, both used for introspection:
246 .VARIABLES expands to a list of all the variables defined in this instance
247 of make.
248 .TARGETS expands to a list of all the targets defined in this
249 instance of make.
250 Returns the variable reference passed in. */
252 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
254 static struct variable *
255 handle_special_var (struct variable *var)
257 static unsigned long last_var_count = 0;
260 /* This one actually turns out to be very hard, due to the way the parser
261 records targets. The way it works is that target information is collected
262 internally until make knows the target is completely specified. It unitl
263 it sees that some new construct (a new target or variable) is defined that
264 it knows the previous one is done. In short, this means that if you do
265 this:
267 all:
269 TARGS := $(.TARGETS)
271 then $(TARGS) won't contain "all", because it's not until after the
272 variable is created that the previous target is completed.
274 Changing this would be a major pain. I think a less complex way to do it
275 would be to pre-define the target files as soon as the first line is
276 parsed, then come back and do the rest of the definition as now. That
277 would allow $(.TARGETS) to be correct without a major change to the way
278 the parser works.
280 if (streq (var->name, ".TARGETS"))
281 var->value = build_target_list (var->value);
282 else
285 if (streq (var->name, ".VARIABLES")
286 && global_variable_set.table.ht_fill != last_var_count)
288 unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
289 unsigned long len;
290 char *p;
291 struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
292 struct variable **end = &vp[global_variable_set.table.ht_size];
294 /* Make sure we have at least MAX bytes in the allocated buffer. */
295 var->value = xrealloc (var->value, max);
297 /* Walk through the hash of variables, constructing a list of names. */
298 p = var->value;
299 len = 0;
300 for (; vp < end; ++vp)
301 if (!HASH_VACANT (*vp))
303 struct variable *v = *vp;
304 int l = v->length;
306 len += l + 1;
307 if (len > max)
309 unsigned long off = p - var->value;
311 max += EXPANSION_INCREMENT (l + 1);
312 var->value = xrealloc (var->value, max);
313 p = &var->value[off];
316 bcopy (v->name, p, l);
317 p += l;
318 *(p++) = ' ';
320 *(p-1) = '\0';
322 /* Remember how many variables are in our current count. Since we never
323 remove variables from the list, this is a reliable way to know whether
324 the list is up to date or needs to be recomputed. */
326 last_var_count = global_variable_set.table.ht_fill;
329 return var;
333 /* Lookup a variable whose name is a string starting at NAME
334 and with LENGTH chars. NAME need not be null-terminated.
335 Returns address of the `struct variable' containing all info
336 on the variable, or nil if no such variable is defined. */
338 struct variable *
339 lookup_variable (const char *name, unsigned int length)
341 const struct variable_set_list *setlist;
342 struct variable var_key;
344 var_key.name = (char *) name;
345 var_key.length = length;
347 for (setlist = current_variable_set_list;
348 setlist != 0; setlist = setlist->next)
350 const struct variable_set *set = setlist->set;
351 struct variable *v;
353 v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
354 if (v)
355 return v->special ? handle_special_var (v) : v;
358 #ifdef VMS
359 /* since we don't read envp[] on startup, try to get the
360 variable via getenv() here. */
362 char *vname = alloca (length + 1);
363 char *value;
364 strncpy (vname, name, length);
365 vname[length] = 0;
366 value = getenv (vname);
367 if (value != 0)
369 char *sptr;
370 int scnt;
372 sptr = value;
373 scnt = 0;
375 while ((sptr = strchr (sptr, '$')))
377 scnt++;
378 sptr++;
381 if (scnt > 0)
383 char *nvalue;
384 char *nptr;
386 nvalue = alloca (strlen (value) + scnt + 1);
387 sptr = value;
388 nptr = nvalue;
390 while (*sptr)
392 if (*sptr == '$')
394 *nptr++ = '$';
395 *nptr++ = '$';
397 else
399 *nptr++ = *sptr;
401 sptr++;
404 *nptr = '\0';
405 return define_variable (vname, length, nvalue, o_env, 1);
409 return define_variable (vname, length, value, o_env, 1);
412 #endif /* VMS */
414 return 0;
417 /* Lookup a variable whose name is a string starting at NAME
418 and with LENGTH chars in set SET. NAME need not be null-terminated.
419 Returns address of the `struct variable' containing all info
420 on the variable, or nil if no such variable is defined. */
422 struct variable *
423 lookup_variable_in_set (const char *name, unsigned int length,
424 const struct variable_set *set)
426 struct variable var_key;
428 var_key.name = (char *) name;
429 var_key.length = length;
431 return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
434 /* Initialize FILE's variable set list. If FILE already has a variable set
435 list, the topmost variable set is left intact, but the the rest of the
436 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
437 rule, then we will use the "root" double-colon target's variable set as the
438 parent of FILE's variable set.
440 If we're READing a makefile, don't do the pattern variable search now,
441 since the pattern variable might not have been defined yet. */
443 void
444 initialize_file_variables (struct file *file, int reading)
446 register struct variable_set_list *l = file->variables;
448 if (l == 0)
450 l = (struct variable_set_list *)
451 xmalloc (sizeof (struct variable_set_list));
452 l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
453 hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
454 variable_hash_1, variable_hash_2, variable_hash_cmp);
455 file->variables = l;
458 /* If this is a double-colon, then our "parent" is the "root" target for
459 this double-colon rule. Since that rule has the same name, parent,
460 etc. we can just use its variables as the "next" for ours. */
462 if (file->double_colon && file->double_colon != file)
464 initialize_file_variables (file->double_colon, reading);
465 l->next = file->double_colon->variables;
466 return;
469 if (file->parent == 0)
470 l->next = &global_setlist;
471 else
473 initialize_file_variables (file->parent, reading);
474 l->next = file->parent->variables;
477 /* If we're not reading makefiles and we haven't looked yet, see if
478 we can find pattern variables for this target. */
480 if (!reading && !file->pat_searched)
482 struct pattern_var *p;
484 p = lookup_pattern_var (0, file->name);
485 if (p != 0)
487 struct variable_set_list *global = current_variable_set_list;
489 /* We found at least one. Set up a new variable set to accumulate
490 all the pattern variables that match this target. */
492 file->pat_variables = create_new_variable_set ();
493 current_variable_set_list = file->pat_variables;
496 /* We found one, so insert it into the set. */
497 do_variable_definition (&p->variable.fileinfo, p->variable.name,
498 p->variable.value, p->variable.origin,
499 p->variable.flavor, 1);
500 while ((p = lookup_pattern_var (p, file->name)) != 0);
502 current_variable_set_list = global;
504 file->pat_searched = 1;
507 /* If we have a pattern variable match, set it up. */
509 if (file->pat_variables != 0)
511 file->pat_variables->next = l->next;
512 l->next = file->pat_variables;
516 /* Pop the top set off the current variable set list,
517 and free all its storage. */
519 static void
520 free_variable_name_and_value (const void *item)
522 struct variable *v = (struct variable *) item;
523 free (v->name);
524 free (v->value);
527 void
528 pop_variable_scope (void)
530 struct variable_set_list *setlist = current_variable_set_list;
531 struct variable_set *set = setlist->set;
533 current_variable_set_list = setlist->next;
534 free ((char *) setlist);
536 hash_map (&set->table, free_variable_name_and_value);
537 hash_free (&set->table, 1);
539 free ((char *) set);
542 struct variable_set_list *
543 create_new_variable_set (void)
545 register struct variable_set_list *setlist;
546 register struct variable_set *set;
548 set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
549 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
550 variable_hash_1, variable_hash_2, variable_hash_cmp);
552 setlist = (struct variable_set_list *)
553 xmalloc (sizeof (struct variable_set_list));
554 setlist->set = set;
555 setlist->next = current_variable_set_list;
557 return setlist;
560 /* Create a new variable set and push it on the current setlist. */
562 struct variable_set_list *
563 push_new_variable_scope (void)
565 return (current_variable_set_list = create_new_variable_set());
568 /* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
570 static void
571 merge_variable_sets (struct variable_set *to_set,
572 struct variable_set *from_set)
574 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
575 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
577 for ( ; from_var_slot < from_var_end; from_var_slot++)
578 if (! HASH_VACANT (*from_var_slot))
580 struct variable *from_var = *from_var_slot;
581 struct variable **to_var_slot
582 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
583 if (HASH_VACANT (*to_var_slot))
584 hash_insert_at (&to_set->table, from_var, to_var_slot);
585 else
587 /* GKM FIXME: delete in from_set->table */
588 free (from_var->value);
589 free (from_var);
594 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
596 void
597 merge_variable_set_lists (struct variable_set_list **setlist0,
598 struct variable_set_list *setlist1)
600 register struct variable_set_list *list0 = *setlist0;
601 struct variable_set_list *last0 = 0;
603 while (setlist1 != 0 && list0 != 0)
605 struct variable_set_list *next = setlist1;
606 setlist1 = setlist1->next;
608 merge_variable_sets (list0->set, next->set);
610 last0 = list0;
611 list0 = list0->next;
614 if (setlist1 != 0)
616 if (last0 == 0)
617 *setlist0 = setlist1;
618 else
619 last0->next = setlist1;
623 /* Define the automatic variables, and record the addresses
624 of their structures so we can change their values quickly. */
626 void
627 define_automatic_variables (void)
629 #if defined(WINDOWS32) || defined(__EMX__)
630 extern char* default_shell;
631 #else
632 extern char default_shell[];
633 #endif
634 register struct variable *v;
635 char buf[200];
637 sprintf (buf, "%u", makelevel);
638 (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
640 sprintf (buf, "%s%s%s",
641 version_string,
642 (remote_description == 0 || remote_description[0] == '\0')
643 ? "" : "-",
644 (remote_description == 0 || remote_description[0] == '\0')
645 ? "" : remote_description);
646 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
648 #ifdef __MSDOS__
649 /* Allow to specify a special shell just for Make,
650 and use $COMSPEC as the default $SHELL when appropriate. */
652 static char shell_str[] = "SHELL";
653 const int shlen = sizeof (shell_str) - 1;
654 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
655 struct variable *comp = lookup_variable ("COMSPEC", 7);
657 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
658 if (mshp)
659 (void) define_variable (shell_str, shlen,
660 mshp->value, o_env_override, 0);
661 else if (comp)
663 /* $COMSPEC shouldn't override $SHELL. */
664 struct variable *shp = lookup_variable (shell_str, shlen);
666 if (!shp)
667 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
670 #elif defined(__EMX__)
672 static char shell_str[] = "SHELL";
673 const int shlen = sizeof (shell_str) - 1;
674 struct variable *shell = lookup_variable (shell_str, shlen);
675 struct variable *replace = lookup_variable ("MAKESHELL", 9);
677 /* if $MAKESHELL is defined in the environment assume o_env_override */
678 if (replace && *replace->value && replace->origin == o_env)
679 replace->origin = o_env_override;
681 /* if $MAKESHELL is not defined use $SHELL but only if the variable
682 did not come from the environment */
683 if (!replace || !*replace->value)
684 if (shell && *shell->value && (shell->origin == o_env
685 || shell->origin == o_env_override))
687 /* overwrite whatever we got from the environment */
688 free(shell->value);
689 shell->value = xstrdup (default_shell);
690 shell->origin = o_default;
693 /* Some people do not like cmd to be used as the default
694 if $SHELL is not defined in the Makefile.
695 With -DNO_CMD_DEFAULT you can turn off this behaviour */
696 # ifndef NO_CMD_DEFAULT
697 /* otherwise use $COMSPEC */
698 if (!replace || !*replace->value)
699 replace = lookup_variable ("COMSPEC", 7);
701 /* otherwise use $OS2_SHELL */
702 if (!replace || !*replace->value)
703 replace = lookup_variable ("OS2_SHELL", 9);
704 # else
705 # warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
706 # endif
708 if (replace && *replace->value)
709 /* overwrite $SHELL */
710 (void) define_variable (shell_str, shlen, replace->value,
711 replace->origin, 0);
712 else
713 /* provide a definition if there is none */
714 (void) define_variable (shell_str, shlen, default_shell,
715 o_default, 0);
718 #endif
720 /* This won't override any definition, but it
721 will provide one if there isn't one there. */
722 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
723 v->export = v_export; /* Always export SHELL. */
725 /* On MSDOS we do use SHELL from environment, since
726 it isn't a standard environment variable on MSDOS,
727 so whoever sets it, does that on purpose.
728 On OS/2 we do not use SHELL from environment but
729 we have already handled that problem above. */
730 #if !defined(__MSDOS__) && !defined(__EMX__)
731 /* Don't let SHELL come from the environment. */
732 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
734 free (v->value);
735 v->origin = o_file;
736 v->value = xstrdup (default_shell);
738 #endif
740 /* Make sure MAKEFILES gets exported if it is set. */
741 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
742 v->export = v_ifset;
744 /* Define the magic D and F variables in terms of
745 the automatic variables they are variations of. */
747 #ifdef VMS
748 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
749 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
750 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
751 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
752 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
753 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
754 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
755 #else
756 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
757 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
758 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
759 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
760 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
761 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
762 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
763 #endif
764 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
765 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
766 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
767 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
768 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
769 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
770 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
773 int export_all_variables;
775 /* Create a new environment for FILE's commands.
776 If FILE is nil, this is for the `shell' function.
777 The child's MAKELEVEL variable is incremented. */
779 char **
780 target_environment (struct file *file)
782 struct variable_set_list *set_list;
783 register struct variable_set_list *s;
784 struct hash_table table;
785 struct variable **v_slot;
786 struct variable **v_end;
787 struct variable makelevel_key;
788 char **result_0;
789 char **result;
791 if (file == 0)
792 set_list = current_variable_set_list;
793 else
794 set_list = file->variables;
796 hash_init (&table, VARIABLE_BUCKETS,
797 variable_hash_1, variable_hash_2, variable_hash_cmp);
799 /* Run through all the variable sets in the list,
800 accumulating variables in TABLE. */
801 for (s = set_list; s != 0; s = s->next)
803 struct variable_set *set = s->set;
804 v_slot = (struct variable **) set->table.ht_vec;
805 v_end = v_slot + set->table.ht_size;
806 for ( ; v_slot < v_end; v_slot++)
807 if (! HASH_VACANT (*v_slot))
809 struct variable **new_slot;
810 struct variable *v = *v_slot;
812 /* If this is a per-target variable and it hasn't been touched
813 already then look up the global version and take its export
814 value. */
815 if (v->per_target && v->export == v_default)
817 struct variable *gv;
819 gv = lookup_variable_in_set (v->name, strlen(v->name),
820 &global_variable_set);
821 if (gv)
822 v->export = gv->export;
825 switch (v->export)
827 case v_default:
828 if (v->origin == o_default || v->origin == o_automatic)
829 /* Only export default variables by explicit request. */
830 continue;
832 /* The variable doesn't have a name that can be exported. */
833 if (! v->exportable)
834 continue;
836 if (! export_all_variables
837 && v->origin != o_command
838 && v->origin != o_env && v->origin != o_env_override)
839 continue;
840 break;
842 case v_export:
843 break;
845 case v_noexport:
846 continue;
848 case v_ifset:
849 if (v->origin == o_default)
850 continue;
851 break;
854 new_slot = (struct variable **) hash_find_slot (&table, v);
855 if (HASH_VACANT (*new_slot))
856 hash_insert_at (&table, v, new_slot);
860 makelevel_key.name = MAKELEVEL_NAME;
861 makelevel_key.length = MAKELEVEL_LENGTH;
862 hash_delete (&table, &makelevel_key);
864 result = result_0 = (char **) xmalloc ((table.ht_fill + 2) * sizeof (char *));
866 v_slot = (struct variable **) table.ht_vec;
867 v_end = v_slot + table.ht_size;
868 for ( ; v_slot < v_end; v_slot++)
869 if (! HASH_VACANT (*v_slot))
871 struct variable *v = *v_slot;
873 /* If V is recursively expanded and didn't come from the environment,
874 expand its value. If it came from the environment, it should
875 go back into the environment unchanged. */
876 if (v->recursive
877 && v->origin != o_env && v->origin != o_env_override)
879 char *value = recursively_expand_for_file (v, file);
880 #ifdef WINDOWS32
881 if (strcmp(v->name, "Path") == 0 ||
882 strcmp(v->name, "PATH") == 0)
883 convert_Path_to_windows32(value, ';');
884 #endif
885 *result++ = concat (v->name, "=", value);
886 free (value);
888 else
890 #ifdef WINDOWS32
891 if (strcmp(v->name, "Path") == 0 ||
892 strcmp(v->name, "PATH") == 0)
893 convert_Path_to_windows32(v->value, ';');
894 #endif
895 *result++ = concat (v->name, "=", v->value);
899 *result = (char *) xmalloc (100);
900 (void) sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
901 *++result = 0;
903 hash_free (&table, 0);
905 return result_0;
908 /* Given a variable, a value, and a flavor, define the variable.
909 See the try_variable_definition() function for details on the parameters. */
911 struct variable *
912 do_variable_definition (const struct floc *flocp, const char *varname,
913 char *value, enum variable_origin origin,
914 enum variable_flavor flavor, int target_var)
916 char *p, *alloc_value = NULL;
917 struct variable *v;
918 int append = 0;
919 int conditional = 0;
921 /* Calculate the variable's new value in VALUE. */
923 switch (flavor)
925 default:
926 case f_bogus:
927 /* Should not be possible. */
928 abort ();
929 case f_simple:
930 /* A simple variable definition "var := value". Expand the value.
931 We have to allocate memory since otherwise it'll clobber the
932 variable buffer, and we may still need that if we're looking at a
933 target-specific variable. */
934 p = alloc_value = allocated_variable_expand (value);
935 break;
936 case f_conditional:
937 /* A conditional variable definition "var ?= value".
938 The value is set IFF the variable is not defined yet. */
939 v = lookup_variable (varname, strlen (varname));
940 if (v)
941 return v;
943 conditional = 1;
944 flavor = f_recursive;
945 /* FALLTHROUGH */
946 case f_recursive:
947 /* A recursive variable definition "var = value".
948 The value is used verbatim. */
949 p = value;
950 break;
951 case f_append:
953 /* If we have += but we're in a target variable context, we want to
954 append only with other variables in the context of this target. */
955 if (target_var)
957 append = 1;
958 v = lookup_variable_in_set (varname, strlen (varname),
959 current_variable_set_list->set);
961 /* Don't append from the global set if a previous non-appending
962 target-specific variable definition exists. */
963 if (v && !v->append)
964 append = 0;
966 else
967 v = lookup_variable (varname, strlen (varname));
969 if (v == 0)
971 /* There was no old value.
972 This becomes a normal recursive definition. */
973 p = value;
974 flavor = f_recursive;
976 else
978 /* Paste the old and new values together in VALUE. */
980 unsigned int oldlen, vallen;
981 char *val;
983 val = value;
984 if (v->recursive)
985 /* The previous definition of the variable was recursive.
986 The new value is the unexpanded old and new values. */
987 flavor = f_recursive;
988 else
989 /* The previous definition of the variable was simple.
990 The new value comes from the old value, which was expanded
991 when it was set; and from the expanded new value. Allocate
992 memory for the expansion as we may still need the rest of the
993 buffer if we're looking at a target-specific variable. */
994 val = alloc_value = allocated_variable_expand (val);
996 oldlen = strlen (v->value);
997 vallen = strlen (val);
998 p = (char *) alloca (oldlen + 1 + vallen + 1);
999 bcopy (v->value, p, oldlen);
1000 p[oldlen] = ' ';
1001 bcopy (val, &p[oldlen + 1], vallen + 1);
1006 #ifdef __MSDOS__
1007 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1008 non-Unix systems don't conform to this default configuration (in
1009 fact, most of them don't even have `/bin'). On the other hand,
1010 $SHELL in the environment, if set, points to the real pathname of
1011 the shell.
1012 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1013 the Makefile override $SHELL from the environment. But first, we
1014 look for the basename of the shell in the directory where SHELL=
1015 points, and along the $PATH; if it is found in any of these places,
1016 we define $SHELL to be the actual pathname of the shell. Thus, if
1017 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1018 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1019 defining SHELL to be "d:/unix/bash.exe". */
1020 if ((origin == o_file || origin == o_override)
1021 && strcmp (varname, "SHELL") == 0)
1023 char shellpath[PATH_MAX];
1024 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1026 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1027 if (__dosexec_find_on_path (p, (char **)0, shellpath))
1029 char *p;
1031 for (p = shellpath; *p; p++)
1033 if (*p == '\\')
1034 *p = '/';
1036 v = define_variable_loc (varname, strlen (varname),
1037 shellpath, origin, flavor == f_recursive,
1038 flocp);
1040 else
1042 char *shellbase, *bslash;
1043 struct variable *pathv = lookup_variable ("PATH", 4);
1044 char *path_string;
1045 char *fake_env[2];
1046 size_t pathlen = 0;
1048 shellbase = strrchr (p, '/');
1049 bslash = strrchr (p, '\\');
1050 if (!shellbase || bslash > shellbase)
1051 shellbase = bslash;
1052 if (!shellbase && p[1] == ':')
1053 shellbase = p + 1;
1054 if (shellbase)
1055 shellbase++;
1056 else
1057 shellbase = p;
1059 /* Search for the basename of the shell (with standard
1060 executable extensions) along the $PATH. */
1061 if (pathv)
1062 pathlen = strlen (pathv->value);
1063 path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
1064 /* On MSDOS, current directory is considered as part of $PATH. */
1065 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1066 fake_env[0] = path_string;
1067 fake_env[1] = (char *)0;
1068 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1070 char *p;
1072 for (p = shellpath; *p; p++)
1074 if (*p == '\\')
1075 *p = '/';
1077 v = define_variable_loc (varname, strlen (varname),
1078 shellpath, origin,
1079 flavor == f_recursive, flocp);
1081 else
1082 v = lookup_variable (varname, strlen (varname));
1084 free (path_string);
1087 else
1088 #endif /* __MSDOS__ */
1089 #ifdef WINDOWS32
1090 if ((origin == o_file || origin == o_override) && streq (varname, "SHELL"))
1092 extern char *default_shell;
1094 /* Call shell locator function. If it returns TRUE, then
1095 set no_default_sh_exe to indicate sh was found and
1096 set new value for SHELL variable. */
1098 if (find_and_set_default_shell (p))
1100 v = define_variable_in_set (varname, strlen (varname), default_shell,
1101 origin, flavor == f_recursive,
1102 (target_var
1103 ? current_variable_set_list->set
1104 : NULL),
1105 flocp);
1106 no_default_sh_exe = 0;
1108 else
1109 v = lookup_variable (varname, strlen (varname));
1111 else
1112 #endif
1114 /* If we are defining variables inside an $(eval ...), we might have a
1115 different variable context pushed, not the global context (maybe we're
1116 inside a $(call ...) or something. Since this function is only ever
1117 invoked in places where we want to define globally visible variables,
1118 make sure we define this variable in the global set. */
1120 v = define_variable_in_set (varname, strlen (varname), p,
1121 origin, flavor == f_recursive,
1122 (target_var
1123 ? current_variable_set_list->set : NULL),
1124 flocp);
1125 v->append = append;
1126 v->conditional = conditional;
1128 if (alloc_value)
1129 free (alloc_value);
1131 return v;
1134 /* Try to interpret LINE (a null-terminated string) as a variable definition.
1136 ORIGIN may be o_file, o_override, o_env, o_env_override,
1137 or o_command specifying that the variable definition comes
1138 from a makefile, an override directive, the environment with
1139 or without the -e switch, or the command line.
1141 See the comments for parse_variable_definition().
1143 If LINE was recognized as a variable definition, a pointer to its `struct
1144 variable' is returned. If LINE is not a variable definition, NULL is
1145 returned. */
1147 struct variable *
1148 parse_variable_definition (struct variable *v, char *line)
1150 register int c;
1151 register char *p = line;
1152 register char *beg;
1153 register char *end;
1154 enum variable_flavor flavor = f_bogus;
1155 char *name;
1157 while (1)
1159 c = *p++;
1160 if (c == '\0' || c == '#')
1161 return 0;
1162 if (c == '=')
1164 end = p - 1;
1165 flavor = f_recursive;
1166 break;
1168 else if (c == ':')
1169 if (*p == '=')
1171 end = p++ - 1;
1172 flavor = f_simple;
1173 break;
1175 else
1176 /* A colon other than := is a rule line, not a variable defn. */
1177 return 0;
1178 else if (c == '+' && *p == '=')
1180 end = p++ - 1;
1181 flavor = f_append;
1182 break;
1184 else if (c == '?' && *p == '=')
1186 end = p++ - 1;
1187 flavor = f_conditional;
1188 break;
1190 else if (c == '$')
1192 /* This might begin a variable expansion reference. Make sure we
1193 don't misrecognize chars inside the reference as =, := or +=. */
1194 char closeparen;
1195 int count;
1196 c = *p++;
1197 if (c == '(')
1198 closeparen = ')';
1199 else if (c == '{')
1200 closeparen = '}';
1201 else
1202 continue; /* Nope. */
1204 /* P now points past the opening paren or brace.
1205 Count parens or braces until it is matched. */
1206 count = 0;
1207 for (; *p != '\0'; ++p)
1209 if (*p == c)
1210 ++count;
1211 else if (*p == closeparen && --count < 0)
1213 ++p;
1214 break;
1219 v->flavor = flavor;
1221 beg = next_token (line);
1222 while (end > beg && isblank ((unsigned char)end[-1]))
1223 --end;
1224 p = next_token (p);
1225 v->value = p;
1227 /* Expand the name, so "$(foo)bar = baz" works. */
1228 name = (char *) alloca (end - beg + 1);
1229 bcopy (beg, name, end - beg);
1230 name[end - beg] = '\0';
1231 v->name = allocated_variable_expand (name);
1233 if (v->name[0] == '\0')
1234 fatal (&v->fileinfo, _("empty variable name"));
1236 return v;
1239 /* Try to interpret LINE (a null-terminated string) as a variable definition.
1241 ORIGIN may be o_file, o_override, o_env, o_env_override,
1242 or o_command specifying that the variable definition comes
1243 from a makefile, an override directive, the environment with
1244 or without the -e switch, or the command line.
1246 See the comments for parse_variable_definition().
1248 If LINE was recognized as a variable definition, a pointer to its `struct
1249 variable' is returned. If LINE is not a variable definition, NULL is
1250 returned. */
1252 struct variable *
1253 try_variable_definition (const struct floc *flocp, char *line,
1254 enum variable_origin origin, int target_var)
1256 struct variable v;
1257 struct variable *vp;
1259 if (flocp != 0)
1260 v.fileinfo = *flocp;
1261 else
1262 v.fileinfo.filenm = 0;
1264 if (!parse_variable_definition (&v, line))
1265 return 0;
1267 vp = do_variable_definition (flocp, v.name, v.value,
1268 origin, v.flavor, target_var);
1270 free (v.name);
1272 return vp;
1275 /* Print information for variable V, prefixing it with PREFIX. */
1277 static void
1278 print_variable (const void *item, void *arg)
1280 const struct variable *v = (struct variable *) item;
1281 const char *prefix = (char *) arg;
1282 const char *origin;
1284 switch (v->origin)
1286 case o_default:
1287 origin = _("default");
1288 break;
1289 case o_env:
1290 origin = _("environment");
1291 break;
1292 case o_file:
1293 origin = _("makefile");
1294 break;
1295 case o_env_override:
1296 origin = _("environment under -e");
1297 break;
1298 case o_command:
1299 origin = _("command line");
1300 break;
1301 case o_override:
1302 origin = _("`override' directive");
1303 break;
1304 case o_automatic:
1305 origin = _("automatic");
1306 break;
1307 case o_invalid:
1308 default:
1309 abort ();
1311 fputs ("# ", stdout);
1312 fputs (origin, stdout);
1313 if (v->fileinfo.filenm)
1314 printf (_(" (from `%s', line %lu)"),
1315 v->fileinfo.filenm, v->fileinfo.lineno);
1316 putchar ('\n');
1317 fputs (prefix, stdout);
1319 /* Is this a `define'? */
1320 if (v->recursive && strchr (v->value, '\n') != 0)
1321 printf ("define %s\n%s\nendef\n", v->name, v->value);
1322 else
1324 register char *p;
1326 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1328 /* Check if the value is just whitespace. */
1329 p = next_token (v->value);
1330 if (p != v->value && *p == '\0')
1331 /* All whitespace. */
1332 printf ("$(subst ,,%s)", v->value);
1333 else if (v->recursive)
1334 fputs (v->value, stdout);
1335 else
1336 /* Double up dollar signs. */
1337 for (p = v->value; *p != '\0'; ++p)
1339 if (*p == '$')
1340 putchar ('$');
1341 putchar (*p);
1343 putchar ('\n');
1348 /* Print all the variables in SET. PREFIX is printed before
1349 the actual variable definitions (everything else is comments). */
1351 void
1352 print_variable_set (struct variable_set *set, char *prefix)
1354 hash_map_arg (&set->table, print_variable, prefix);
1356 fputs (_("# variable set hash-table stats:\n"), stdout);
1357 fputs ("# ", stdout);
1358 hash_print_stats (&set->table, stdout);
1359 putc ('\n', stdout);
1362 /* Print the data base of variables. */
1364 void
1365 print_variable_data_base (void)
1367 puts (_("\n# Variables\n"));
1369 print_variable_set (&global_variable_set, "");
1371 puts (_("\n# Pattern-specific Variable Values"));
1374 struct pattern_var *p;
1375 int rules = 0;
1377 for (p = pattern_vars; p != 0; p = p->next)
1379 ++rules;
1380 printf ("\n%s :\n", p->target);
1381 print_variable (&p->variable, "# ");
1384 if (rules == 0)
1385 puts (_("\n# No pattern-specific variable values."));
1386 else
1387 printf (_("\n# %u pattern-specific variable values"), rules);
1392 /* Print all the local variables of FILE. */
1394 void
1395 print_file_variables (struct file *file)
1397 if (file->variables != 0)
1398 print_variable_set (file->variables->set, "# ");
1401 #ifdef WINDOWS32
1402 void
1403 sync_Path_environment (void)
1405 char *path = allocated_variable_expand ("$(Path)");
1406 static char *environ_path = NULL;
1408 if (!path)
1409 return;
1412 * If done this before, don't leak memory unnecessarily.
1413 * Free the previous entry before allocating new one.
1415 if (environ_path)
1416 free (environ_path);
1419 * Create something WINDOWS32 world can grok
1421 convert_Path_to_windows32 (path, ';');
1422 environ_path = concat ("Path", "=", path);
1423 putenv (environ_path);
1424 free (path);
1426 #endif