Whoops; configure wasn't looking for memmove.
[make/kirr.git] / variable.c
blob27a38e21ab65f41a5b0aa4e6d60f88f3612dda2d
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 (void const *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 (void const *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 (void const *xv, void const *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 ()
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 (name, length, value, origin, recursive, set, flocp)
93 const char *name;
94 unsigned int length;
95 char *value;
96 enum variable_origin origin;
97 int recursive;
98 struct variable_set *set;
99 const struct floc *flocp;
101 struct variable *v;
102 struct variable **var_slot;
103 struct variable var_key;
105 if (set == NULL)
106 set = &global_variable_set;
108 var_key.name = (char *) name;
109 var_key.length = length;
110 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
112 if (env_overrides && origin == o_env)
113 origin = o_env_override;
115 v = *var_slot;
116 if (! HASH_VACANT (v))
118 if (env_overrides && v->origin == o_env)
119 /* V came from in the environment. Since it was defined
120 before the switches were parsed, it wasn't affected by -e. */
121 v->origin = o_env_override;
123 /* A variable of this name is already defined.
124 If the old definition is from a stronger source
125 than this one, don't redefine it. */
126 if ((int) origin >= (int) v->origin)
128 if (v->value != 0)
129 free (v->value);
130 v->value = xstrdup (value);
131 if (flocp != 0)
132 v->fileinfo = *flocp;
133 else
134 v->fileinfo.filenm = 0;
135 v->origin = origin;
136 v->recursive = recursive;
138 return v;
141 /* Create a new variable definition and add it to the hash table. */
143 v = (struct variable *) xmalloc (sizeof (struct variable));
144 v->name = savestring (name, length);
145 v->length = length;
146 hash_insert_at (&set->table, v, var_slot);
147 v->value = xstrdup (value);
148 if (flocp != 0)
149 v->fileinfo = *flocp;
150 else
151 v->fileinfo.filenm = 0;
152 v->origin = origin;
153 v->recursive = recursive;
154 v->expanding = 0;
155 v->exp_count = 0;
156 v->per_target = 0;
157 v->append = 0;
158 v->export = v_default;
160 v->exportable = 1;
161 if (*name != '_' && (*name < 'A' || *name > 'Z')
162 && (*name < 'a' || *name > 'z'))
163 v->exportable = 0;
164 else
166 for (++name; *name != '\0'; ++name)
167 if (*name != '_' && (*name < 'a' || *name > 'z')
168 && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
169 break;
171 if (*name != '\0')
172 v->exportable = 0;
175 return v;
178 /* If the variable passed in is "special", handle its special nature.
179 Currently there are two such variables, both used for introspection:
180 .VARIABLES expands to a list of all the variables defined in this instance
181 of make.
182 .TARGETS expands to a list of all the targets defined in this
183 instance of make.
184 Returns the variable reference passed in. */
186 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
188 static struct variable *
189 handle_special_var (var)
190 struct variable *var;
192 static unsigned long last_var_count = 0;
195 /* This one actually turns out to be very hard, due to the way the parser
196 records targets. The way it works is that target information is collected
197 internally until make knows the target is completely specified. It unitl
198 it sees that some new construct (a new target or variable) is defined that
199 it knows the previous one is done. In short, this means that if you do
200 this:
202 all:
204 TARGS := $(.TARGETS)
206 then $(TARGS) won't contain "all", because it's not until after the
207 variable is created that the previous target is completed.
209 Changing this would be a major pain. I think a less complex way to do it
210 would be to pre-define the target files as soon as the first line is
211 parsed, then come back and do the rest of the definition as now. That
212 would allow $(.TARGETS) to be correct without a major change to the way
213 the parser works.
215 if (streq (var->name, ".TARGETS"))
216 var->value = build_target_list (var->value);
217 else
220 if (streq (var->name, ".VARIABLES")
221 && global_variable_set.table.ht_fill != last_var_count)
223 unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
224 unsigned long len;
225 char *p;
226 struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
227 struct variable **end = &vp[global_variable_set.table.ht_size];
229 /* Make sure we have at least MAX bytes in the allocated buffer. */
230 var->value = xrealloc (var->value, max);
232 /* Walk through the hash of variables, constructing a list of names. */
233 p = var->value;
234 len = 0;
235 for (; vp < end; ++vp)
236 if (!HASH_VACANT (*vp))
238 struct variable *v = *vp;
239 int l = v->length;
241 len += l + 1;
242 if (len > max)
244 unsigned long off = p - var->value;
246 max += EXPANSION_INCREMENT (l + 1);
247 var->value = xrealloc (var->value, max);
248 p = &var->value[off];
251 bcopy (v->name, p, l);
252 p += l;
253 *(p++) = ' ';
255 *(p-1) = '\0';
257 /* Remember how many variables are in our current count. Since we never
258 remove variables from the list, this is a reliable way to know whether
259 the list is up to date or needs to be recomputed. */
261 last_var_count = global_variable_set.table.ht_fill;
264 return var;
268 /* Lookup a variable whose name is a string starting at NAME
269 and with LENGTH chars. NAME need not be null-terminated.
270 Returns address of the `struct variable' containing all info
271 on the variable, or nil if no such variable is defined. */
273 struct variable *
274 lookup_variable (name, length)
275 const char *name;
276 unsigned int length;
278 const struct variable_set_list *setlist;
279 struct variable var_key;
281 var_key.name = (char *) name;
282 var_key.length = length;
284 for (setlist = current_variable_set_list;
285 setlist != 0; setlist = setlist->next)
287 const struct variable_set *set = setlist->set;
288 struct variable *v;
290 v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
291 if (v)
292 return v->special ? handle_special_var (v) : v;
295 #ifdef VMS
296 /* since we don't read envp[] on startup, try to get the
297 variable via getenv() here. */
299 char *vname = alloca (length + 1);
300 char *value;
301 strncpy (vname, name, length);
302 vname[length] = 0;
303 value = getenv (vname);
304 if (value != 0)
306 char *sptr;
307 int scnt;
309 sptr = value;
310 scnt = 0;
312 while ((sptr = strchr (sptr, '$')))
314 scnt++;
315 sptr++;
318 if (scnt > 0)
320 char *nvalue;
321 char *nptr;
323 nvalue = alloca (strlen (value) + scnt + 1);
324 sptr = value;
325 nptr = nvalue;
327 while (*sptr)
329 if (*sptr == '$')
331 *nptr++ = '$';
332 *nptr++ = '$';
334 else
336 *nptr++ = *sptr;
338 sptr++;
341 *nptr = '\0';
342 return define_variable (vname, length, nvalue, o_env, 1);
346 return define_variable (vname, length, value, o_env, 1);
349 #endif /* VMS */
351 return 0;
354 /* Lookup a variable whose name is a string starting at NAME
355 and with LENGTH chars in set SET. NAME need not be null-terminated.
356 Returns address of the `struct variable' containing all info
357 on the variable, or nil if no such variable is defined. */
359 struct variable *
360 lookup_variable_in_set (name, length, set)
361 const char *name;
362 unsigned int length;
363 const struct variable_set *set;
365 struct variable var_key;
367 var_key.name = (char *) name;
368 var_key.length = length;
370 return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
373 /* Initialize FILE's variable set list. If FILE already has a variable set
374 list, the topmost variable set is left intact, but the the rest of the
375 chain is replaced with FILE->parent's setlist. If we're READing a
376 makefile, don't do the pattern variable search now, since the pattern
377 variable might not have been defined yet. */
379 void
380 initialize_file_variables (file, reading)
381 struct file *file;
382 int reading;
384 register struct variable_set_list *l = file->variables;
386 if (l == 0)
388 l = (struct variable_set_list *)
389 xmalloc (sizeof (struct variable_set_list));
390 l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
391 hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
392 variable_hash_1, variable_hash_2, variable_hash_cmp);
393 file->variables = l;
396 if (file->parent == 0)
397 l->next = &global_setlist;
398 else
400 initialize_file_variables (file->parent, reading);
401 l->next = file->parent->variables;
404 /* If we're not reading makefiles and we haven't looked yet, see if
405 we can find a pattern variable. */
407 if (!reading && !file->pat_searched)
409 struct pattern_var *p = lookup_pattern_var (file->name);
411 file->pat_searched = 1;
412 if (p != 0)
414 /* If we found one, insert it between the current target's
415 variables and the next set, whatever it is. */
416 file->pat_variables = (struct variable_set_list *)
417 xmalloc (sizeof (struct variable_set_list));
418 file->pat_variables->set = p->vars->set;
422 /* If we have a pattern variable match, set it up. */
424 if (file->pat_variables != 0)
426 file->pat_variables->next = l->next;
427 l->next = file->pat_variables;
431 /* Pop the top set off the current variable set list,
432 and free all its storage. */
434 static void
435 free_variable_name_and_value (item)
436 void *item;
438 struct variable *v = (struct variable *) item;
439 free (v->name);
440 free (v->value);
443 void
444 pop_variable_scope ()
446 struct variable_set_list *setlist = current_variable_set_list;
447 struct variable_set *set = setlist->set;
449 current_variable_set_list = setlist->next;
450 free ((char *) setlist);
452 hash_map (&set->table, free_variable_name_and_value);
453 hash_free (&set->table, 1);
455 free ((char *) set);
458 struct variable_set_list *
459 create_new_variable_set ()
461 register struct variable_set_list *setlist;
462 register struct variable_set *set;
464 set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
465 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
466 variable_hash_1, variable_hash_2, variable_hash_cmp);
468 setlist = (struct variable_set_list *)
469 xmalloc (sizeof (struct variable_set_list));
470 setlist->set = set;
471 setlist->next = current_variable_set_list;
473 return setlist;
476 /* Create a new variable set and push it on the current setlist. */
478 struct variable_set_list *
479 push_new_variable_scope ()
481 return (current_variable_set_list = create_new_variable_set());
484 /* Merge SET1 into SET0, freeing unused storage in SET1. */
486 static void
487 merge_variable_sets (to_set, from_set)
488 struct variable_set *to_set, *from_set;
490 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
491 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
493 for ( ; from_var_slot < from_var_end; from_var_slot++)
494 if (! HASH_VACANT (*from_var_slot))
496 struct variable *from_var = *from_var_slot;
497 struct variable **to_var_slot
498 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
499 if (HASH_VACANT (*to_var_slot))
500 hash_insert_at (&to_set->table, from_var, to_var_slot);
501 else
503 /* GKM FIXME: delete in from_set->table */
504 free (from_var->value);
505 free (from_var);
510 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
512 void
513 merge_variable_set_lists (setlist0, setlist1)
514 struct variable_set_list **setlist0, *setlist1;
516 register struct variable_set_list *list0 = *setlist0;
517 struct variable_set_list *last0 = 0;
519 while (setlist1 != 0 && list0 != 0)
521 struct variable_set_list *next = setlist1;
522 setlist1 = setlist1->next;
524 merge_variable_sets (list0->set, next->set);
526 last0 = list0;
527 list0 = list0->next;
530 if (setlist1 != 0)
532 if (last0 == 0)
533 *setlist0 = setlist1;
534 else
535 last0->next = setlist1;
539 /* Define the automatic variables, and record the addresses
540 of their structures so we can change their values quickly. */
542 void
543 define_automatic_variables ()
545 #ifdef WINDOWS32
546 extern char* default_shell;
547 #else
548 extern char default_shell[];
549 #endif
550 register struct variable *v;
551 char buf[200];
553 sprintf (buf, "%u", makelevel);
554 (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
556 sprintf (buf, "%s%s%s",
557 version_string,
558 (remote_description == 0 || remote_description[0] == '\0')
559 ? "" : "-",
560 (remote_description == 0 || remote_description[0] == '\0')
561 ? "" : remote_description);
562 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
564 #ifdef __MSDOS__
565 /* Allow to specify a special shell just for Make,
566 and use $COMSPEC as the default $SHELL when appropriate. */
568 static char shell_str[] = "SHELL";
569 const int shlen = sizeof (shell_str) - 1;
570 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
571 struct variable *comp = lookup_variable ("COMSPEC", 7);
573 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
574 if (mshp)
575 (void) define_variable (shell_str, shlen,
576 mshp->value, o_env_override, 0);
577 else if (comp)
579 /* $COMSPEC shouldn't override $SHELL. */
580 struct variable *shp = lookup_variable (shell_str, shlen);
582 if (!shp)
583 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
586 #endif
588 /* This won't override any definition, but it
589 will provide one if there isn't one there. */
590 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
591 v->export = v_export; /* Always export SHELL. */
593 /* On MSDOS we do use SHELL from environment, since
594 it isn't a standard environment variable on MSDOS,
595 so whoever sets it, does that on purpose. */
596 #ifndef __MSDOS__
597 /* Don't let SHELL come from the environment. */
598 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
600 free (v->value);
601 v->origin = o_file;
602 v->value = xstrdup (default_shell);
604 #endif
606 /* Make sure MAKEFILES gets exported if it is set. */
607 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
608 v->export = v_ifset;
610 /* Define the magic D and F variables in terms of
611 the automatic variables they are variations of. */
613 #ifdef VMS
614 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
615 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
616 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
617 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
618 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
619 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
620 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
621 #else
622 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
623 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
624 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
625 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
626 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
627 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
628 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
629 #endif
630 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
631 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
632 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
633 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
634 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
635 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
636 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
639 int export_all_variables;
641 /* Create a new environment for FILE's commands.
642 If FILE is nil, this is for the `shell' function.
643 The child's MAKELEVEL variable is incremented. */
645 char **
646 target_environment (file)
647 struct file *file;
649 struct variable_set_list *set_list;
650 register struct variable_set_list *s;
651 struct hash_table table;
652 struct variable **v_slot;
653 struct variable **v_end;
654 struct variable makelevel_key;
655 char **result_0;
656 char **result;
658 if (file == 0)
659 set_list = current_variable_set_list;
660 else
661 set_list = file->variables;
663 hash_init (&table, VARIABLE_BUCKETS,
664 variable_hash_1, variable_hash_2, variable_hash_cmp);
666 /* Run through all the variable sets in the list,
667 accumulating variables in TABLE. */
668 for (s = set_list; s != 0; s = s->next)
670 struct variable_set *set = s->set;
671 v_slot = (struct variable **) set->table.ht_vec;
672 v_end = v_slot + set->table.ht_size;
673 for ( ; v_slot < v_end; v_slot++)
674 if (! HASH_VACANT (*v_slot))
676 struct variable **new_slot;
677 struct variable *v = *v_slot;
679 /* If this is a per-target variable and it hasn't been touched
680 already then look up the global version and take its export
681 value. */
682 if (v->per_target && v->export == v_default)
684 struct variable *gv;
686 gv = lookup_variable_in_set (v->name, strlen(v->name),
687 &global_variable_set);
688 if (gv)
689 v->export = gv->export;
692 switch (v->export)
694 case v_default:
695 if (v->origin == o_default || v->origin == o_automatic)
696 /* Only export default variables by explicit request. */
697 continue;
699 /* The variable doesn't have a name that can be exported. */
700 if (! v->exportable)
701 continue;
703 if (! export_all_variables
704 && v->origin != o_command
705 && v->origin != o_env && v->origin != o_env_override)
706 continue;
707 break;
709 case v_export:
710 break;
712 case v_noexport:
713 continue;
715 case v_ifset:
716 if (v->origin == o_default)
717 continue;
718 break;
721 new_slot = (struct variable **) hash_find_slot (&table, v);
722 if (HASH_VACANT (*new_slot))
723 hash_insert_at (&table, v, new_slot);
727 makelevel_key.name = MAKELEVEL_NAME;
728 makelevel_key.length = MAKELEVEL_LENGTH;
729 hash_delete (&table, &makelevel_key);
731 result = result_0 = (char **) xmalloc ((table.ht_fill + 2) * sizeof (char *));
733 v_slot = (struct variable **) table.ht_vec;
734 v_end = v_slot + table.ht_size;
735 for ( ; v_slot < v_end; v_slot++)
736 if (! HASH_VACANT (*v_slot))
738 struct variable *v = *v_slot;
740 /* If V is recursively expanded and didn't come from the environment,
741 expand its value. If it came from the environment, it should
742 go back into the environment unchanged. */
743 if (v->recursive
744 && v->origin != o_env && v->origin != o_env_override)
746 char *value = recursively_expand_for_file (v, file);
747 #ifdef WINDOWS32
748 if (strcmp(v->name, "Path") == 0 ||
749 strcmp(v->name, "PATH") == 0)
750 convert_Path_to_windows32(value, ';');
751 #endif
752 *result++ = concat (v->name, "=", value);
753 free (value);
755 else
757 #ifdef WINDOWS32
758 if (strcmp(v->name, "Path") == 0 ||
759 strcmp(v->name, "PATH") == 0)
760 convert_Path_to_windows32(v->value, ';');
761 #endif
762 *result++ = concat (v->name, "=", v->value);
766 *result = (char *) xmalloc (100);
767 (void) sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
768 *++result = 0;
770 hash_free (&table, 0);
772 return result_0;
775 /* Given a variable, a value, and a flavor, define the variable.
776 See the try_variable_definition() function for details on the parameters. */
778 struct variable *
779 do_variable_definition (flocp, varname, value, origin, flavor, target_var)
780 const struct floc *flocp;
781 const char *varname;
782 char *value;
783 enum variable_origin origin;
784 enum variable_flavor flavor;
785 int target_var;
787 char *p, *alloc_value = NULL;
788 struct variable *v;
789 int append = 0;
791 /* Calculate the variable's new value in VALUE. */
793 switch (flavor)
795 default:
796 case f_bogus:
797 /* Should not be possible. */
798 abort ();
799 case f_simple:
800 /* A simple variable definition "var := value". Expand the value.
801 We have to allocate memory since otherwise it'll clobber the
802 variable buffer, and we may still need that if we're looking at a
803 target-specific variable. */
804 p = alloc_value = allocated_variable_expand (value);
805 break;
806 case f_conditional:
807 /* A conditional variable definition "var ?= value".
808 The value is set IFF the variable is not defined yet. */
809 v = lookup_variable (varname, strlen (varname));
810 if (v)
811 return v;
813 flavor = f_recursive;
814 /* FALLTHROUGH */
815 case f_recursive:
816 /* A recursive variable definition "var = value".
817 The value is used verbatim. */
818 p = value;
819 break;
820 case f_append:
822 /* If we have += but we're in a target variable context, we want to
823 append only with other variables in the context of this target. */
824 if (target_var)
826 append = 1;
827 v = lookup_variable_in_set (varname, strlen (varname),
828 current_variable_set_list->set);
830 else
831 v = lookup_variable (varname, strlen (varname));
833 if (v == 0)
835 /* There was no old value.
836 This becomes a normal recursive definition. */
837 p = value;
838 flavor = f_recursive;
840 else
842 /* Paste the old and new values together in VALUE. */
844 unsigned int oldlen, vallen;
845 char *val;
847 val = value;
848 if (v->recursive)
849 /* The previous definition of the variable was recursive.
850 The new value is the unexpanded old and new values. */
851 flavor = f_recursive;
852 else
853 /* The previous definition of the variable was simple.
854 The new value comes from the old value, which was expanded
855 when it was set; and from the expanded new value. Allocate
856 memory for the expansion as we may still need the rest of the
857 buffer if we're looking at a target-specific variable. */
858 val = alloc_value = allocated_variable_expand (val);
860 oldlen = strlen (v->value);
861 vallen = strlen (val);
862 p = (char *) alloca (oldlen + 1 + vallen + 1);
863 bcopy (v->value, p, oldlen);
864 p[oldlen] = ' ';
865 bcopy (val, &p[oldlen + 1], vallen + 1);
870 #ifdef __MSDOS__
871 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
872 non-Unix systems don't conform to this default configuration (in
873 fact, most of them don't even have `/bin'). On the other hand,
874 $SHELL in the environment, if set, points to the real pathname of
875 the shell.
876 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
877 the Makefile override $SHELL from the environment. But first, we
878 look for the basename of the shell in the directory where SHELL=
879 points, and along the $PATH; if it is found in any of these places,
880 we define $SHELL to be the actual pathname of the shell. Thus, if
881 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
882 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
883 defining SHELL to be "d:/unix/bash.exe". */
884 if ((origin == o_file || origin == o_override)
885 && strcmp (varname, "SHELL") == 0)
887 char shellpath[PATH_MAX];
888 extern char * __dosexec_find_on_path (const char *, char *[], char *);
890 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
891 if (__dosexec_find_on_path (p, (char **)0, shellpath))
893 char *p;
895 for (p = shellpath; *p; p++)
897 if (*p == '\\')
898 *p = '/';
900 v = define_variable_loc (varname, strlen (varname),
901 shellpath, origin, flavor == f_recursive,
902 flocp);
904 else
906 char *shellbase, *bslash;
907 struct variable *pathv = lookup_variable ("PATH", 4);
908 char *path_string;
909 char *fake_env[2];
910 size_t pathlen = 0;
912 shellbase = strrchr (p, '/');
913 bslash = strrchr (p, '\\');
914 if (!shellbase || bslash > shellbase)
915 shellbase = bslash;
916 if (!shellbase && p[1] == ':')
917 shellbase = p + 1;
918 if (shellbase)
919 shellbase++;
920 else
921 shellbase = p;
923 /* Search for the basename of the shell (with standard
924 executable extensions) along the $PATH. */
925 if (pathv)
926 pathlen = strlen (pathv->value);
927 path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
928 /* On MSDOS, current directory is considered as part of $PATH. */
929 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
930 fake_env[0] = path_string;
931 fake_env[1] = (char *)0;
932 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
934 char *p;
936 for (p = shellpath; *p; p++)
938 if (*p == '\\')
939 *p = '/';
941 v = define_variable_loc (varname, strlen (varname),
942 shellpath, origin,
943 flavor == f_recursive, flocp);
945 else
946 v = lookup_variable (varname, strlen (varname));
948 free (path_string);
951 else
952 #endif /* __MSDOS__ */
953 #ifdef WINDOWS32
954 if ((origin == o_file || origin == o_override) && streq (varname, "SHELL"))
956 extern char *default_shell;
958 /* Call shell locator function. If it returns TRUE, then
959 set no_default_sh_exe to indicate sh was found and
960 set new value for SHELL variable. */
962 if (find_and_set_default_shell (p))
964 v = define_variable_in_set (varname, strlen (varname), default_shell,
965 origin, flavor == f_recursive,
966 (target_var
967 ? current_variable_set_list->set
968 : NULL),
969 flocp);
970 no_default_sh_exe = 0;
972 else
973 v = lookup_variable (varname, strlen (varname));
975 else
976 #endif
978 /* If we are defining variables inside an $(eval ...), we might have a
979 different variable context pushed, not the global context (maybe we're
980 inside a $(call ...) or something. Since this function is only ever
981 invoked in places where we want to define globally visible variables,
982 make sure we define this variable in the global set. */
984 v = define_variable_in_set (varname, strlen (varname), p,
985 origin, flavor == f_recursive,
986 (target_var
987 ? current_variable_set_list->set : NULL),
988 flocp);
989 v->append = append;
991 if (alloc_value)
992 free (alloc_value);
994 return v;
997 /* Try to interpret LINE (a null-terminated string) as a variable definition.
999 ORIGIN may be o_file, o_override, o_env, o_env_override,
1000 or o_command specifying that the variable definition comes
1001 from a makefile, an override directive, the environment with
1002 or without the -e switch, or the command line.
1004 See the comments for parse_variable_definition().
1006 If LINE was recognized as a variable definition, a pointer to its `struct
1007 variable' is returned. If LINE is not a variable definition, NULL is
1008 returned. */
1010 struct variable *
1011 try_variable_definition (flocp, line, origin, target_var)
1012 const struct floc *flocp;
1013 char *line;
1014 enum variable_origin origin;
1015 int target_var;
1017 register int c;
1018 register char *p = line;
1019 register char *beg;
1020 register char *end;
1021 enum variable_flavor flavor = f_bogus;
1022 char *name, *expanded_name;
1023 struct variable *v;
1025 while (1)
1027 c = *p++;
1028 if (c == '\0' || c == '#')
1029 return 0;
1030 if (c == '=')
1032 end = p - 1;
1033 flavor = f_recursive;
1034 break;
1036 else if (c == ':')
1037 if (*p == '=')
1039 end = p++ - 1;
1040 flavor = f_simple;
1041 break;
1043 else
1044 /* A colon other than := is a rule line, not a variable defn. */
1045 return 0;
1046 else if (c == '+' && *p == '=')
1048 end = p++ - 1;
1049 flavor = f_append;
1050 break;
1052 else if (c == '?' && *p == '=')
1054 end = p++ - 1;
1055 flavor = f_conditional;
1056 break;
1058 else if (c == '$')
1060 /* This might begin a variable expansion reference. Make sure we
1061 don't misrecognize chars inside the reference as =, := or +=. */
1062 char closeparen;
1063 int count;
1064 c = *p++;
1065 if (c == '(')
1066 closeparen = ')';
1067 else if (c == '{')
1068 closeparen = '}';
1069 else
1070 continue; /* Nope. */
1072 /* P now points past the opening paren or brace.
1073 Count parens or braces until it is matched. */
1074 count = 0;
1075 for (; *p != '\0'; ++p)
1077 if (*p == c)
1078 ++count;
1079 else if (*p == closeparen && --count < 0)
1081 ++p;
1082 break;
1088 beg = next_token (line);
1089 while (end > beg && isblank ((unsigned char)end[-1]))
1090 --end;
1091 p = next_token (p);
1093 /* Expand the name, so "$(foo)bar = baz" works. */
1094 name = (char *) alloca (end - beg + 1);
1095 bcopy (beg, name, end - beg);
1096 name[end - beg] = '\0';
1097 expanded_name = allocated_variable_expand (name);
1099 if (expanded_name[0] == '\0')
1100 fatal (flocp, _("empty variable name"));
1102 v = do_variable_definition (flocp, expanded_name, p,
1103 origin, flavor, target_var);
1105 free (expanded_name);
1107 return v;
1110 /* Print information for variable V, prefixing it with PREFIX. */
1112 static void
1113 print_variable (v, prefix)
1114 register struct variable *v;
1115 char *prefix;
1117 const char *origin;
1119 switch (v->origin)
1121 case o_default:
1122 origin = _("default");
1123 break;
1124 case o_env:
1125 origin = _("environment");
1126 break;
1127 case o_file:
1128 origin = _("makefile");
1129 break;
1130 case o_env_override:
1131 origin = _("environment under -e");
1132 break;
1133 case o_command:
1134 origin = _("command line");
1135 break;
1136 case o_override:
1137 origin = _("`override' directive");
1138 break;
1139 case o_automatic:
1140 origin = _("automatic");
1141 break;
1142 case o_invalid:
1143 default:
1144 abort ();
1146 fputs ("# ", stdout);
1147 fputs (origin, stdout);
1148 if (v->fileinfo.filenm)
1149 printf (_(" (from `%s', line %lu)"),
1150 v->fileinfo.filenm, v->fileinfo.lineno);
1151 putchar ('\n');
1152 fputs (prefix, stdout);
1154 /* Is this a `define'? */
1155 if (v->recursive && strchr (v->value, '\n') != 0)
1156 printf ("define %s\n%s\nendef\n", v->name, v->value);
1157 else
1159 register char *p;
1161 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1163 /* Check if the value is just whitespace. */
1164 p = next_token (v->value);
1165 if (p != v->value && *p == '\0')
1166 /* All whitespace. */
1167 printf ("$(subst ,,%s)", v->value);
1168 else if (v->recursive)
1169 fputs (v->value, stdout);
1170 else
1171 /* Double up dollar signs. */
1172 for (p = v->value; *p != '\0'; ++p)
1174 if (*p == '$')
1175 putchar ('$');
1176 putchar (*p);
1178 putchar ('\n');
1183 /* Print all the variables in SET. PREFIX is printed before
1184 the actual variable definitions (everything else is comments). */
1186 void
1187 print_variable_set (set, prefix)
1188 register struct variable_set *set;
1189 char *prefix;
1191 hash_map_arg (&set->table, print_variable, prefix);
1193 fputs (_("# variable set hash-table stats:\n"), stdout);
1194 fputs ("# ", stdout);
1195 hash_print_stats (&set->table, stdout);
1196 putc ('\n', stdout);
1199 /* Print the data base of variables. */
1201 void
1202 print_variable_data_base ()
1204 puts (_("\n# Variables\n"));
1206 print_variable_set (&global_variable_set, "");
1210 /* Print all the local variables of FILE. */
1212 void
1213 print_file_variables (file)
1214 struct file *file;
1216 if (file->variables != 0)
1217 print_variable_set (file->variables->set, "# ");
1220 #ifdef WINDOWS32
1221 void
1222 sync_Path_environment(void)
1224 char* path = allocated_variable_expand("$(Path)");
1225 static char* environ_path = NULL;
1227 if (!path)
1228 return;
1231 * If done this before, don't leak memory unnecessarily.
1232 * Free the previous entry before allocating new one.
1234 if (environ_path)
1235 free(environ_path);
1238 * Create something WINDOWS32 world can grok
1240 convert_Path_to_windows32(path, ';');
1241 environ_path = concat("Path", "=", path);
1242 putenv(environ_path);
1243 free(path);
1245 #endif