Don't put .cvsignore files in the distributed tar file.
[make/kirr.git] / variable.c
blobbb8d8108a7bc14be5ed4e4682541d8ddac8f59a7
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 (keyv)
37 const void *keyv;
39 struct variable const *key = (struct variable const *) keyv;
40 return_STRING_N_HASH_1 (key->name, key->length);
43 static unsigned long
44 variable_hash_2 (keyv)
45 const void *keyv;
47 struct variable const *key = (struct variable const *) keyv;
48 return_STRING_N_HASH_2 (key->name, key->length);
51 static int
52 variable_hash_cmp (xv, yv)
53 const void *xv;
54 const void *yv;
56 struct variable const *x = (struct variable const *) xv;
57 struct variable const *y = (struct variable const *) yv;
58 int result = x->length - y->length;
59 if (result)
60 return result;
61 return_STRING_N_COMPARE (x->name, y->name, x->length);
64 #ifndef VARIABLE_BUCKETS
65 #define VARIABLE_BUCKETS 523
66 #endif
67 #ifndef PERFILE_VARIABLE_BUCKETS
68 #define PERFILE_VARIABLE_BUCKETS 23
69 #endif
70 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
71 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
72 #endif
74 static struct variable_set global_variable_set;
75 static struct variable_set_list global_setlist
76 = { 0, &global_variable_set };
77 struct variable_set_list *current_variable_set_list = &global_setlist;
79 /* Implement variables. */
81 void
82 init_hash_global_variable_set ()
84 hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
85 variable_hash_1, variable_hash_2, variable_hash_cmp);
88 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
89 LENGTH is the length of NAME, which does not need to be null-terminated.
90 ORIGIN specifies the origin of the variable (makefile, command line
91 or environment).
92 If RECURSIVE is nonzero a flag is set in the variable saying
93 that it should be recursively re-expanded. */
95 struct variable *
96 define_variable_in_set (name, length, value, origin, recursive, set, flocp)
97 const char *name;
98 unsigned int length;
99 char *value;
100 enum variable_origin origin;
101 int recursive;
102 struct variable_set *set;
103 const struct floc *flocp;
105 struct variable *v;
106 struct variable **var_slot;
107 struct variable var_key;
109 if (set == NULL)
110 set = &global_variable_set;
112 var_key.name = (char *) name;
113 var_key.length = length;
114 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
116 if (env_overrides && origin == o_env)
117 origin = o_env_override;
119 v = *var_slot;
120 if (! HASH_VACANT (v))
122 if (env_overrides && v->origin == o_env)
123 /* V came from in the environment. Since it was defined
124 before the switches were parsed, it wasn't affected by -e. */
125 v->origin = o_env_override;
127 /* A variable of this name is already defined.
128 If the old definition is from a stronger source
129 than this one, don't redefine it. */
130 if ((int) origin >= (int) v->origin)
132 if (v->value != 0)
133 free (v->value);
134 v->value = xstrdup (value);
135 if (flocp != 0)
136 v->fileinfo = *flocp;
137 else
138 v->fileinfo.filenm = 0;
139 v->origin = origin;
140 v->recursive = recursive;
142 return v;
145 /* Create a new variable definition and add it to the hash table. */
147 v = (struct variable *) xmalloc (sizeof (struct variable));
148 v->name = savestring (name, length);
149 v->length = length;
150 hash_insert_at (&set->table, v, var_slot);
151 v->value = xstrdup (value);
152 if (flocp != 0)
153 v->fileinfo = *flocp;
154 else
155 v->fileinfo.filenm = 0;
156 v->origin = origin;
157 v->recursive = recursive;
158 v->expanding = 0;
159 v->exp_count = 0;
160 v->per_target = 0;
161 v->append = 0;
162 v->export = v_default;
164 v->exportable = 1;
165 if (*name != '_' && (*name < 'A' || *name > 'Z')
166 && (*name < 'a' || *name > 'z'))
167 v->exportable = 0;
168 else
170 for (++name; *name != '\0'; ++name)
171 if (*name != '_' && (*name < 'a' || *name > 'z')
172 && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
173 break;
175 if (*name != '\0')
176 v->exportable = 0;
179 return v;
182 /* If the variable passed in is "special", handle its special nature.
183 Currently there are two such variables, both used for introspection:
184 .VARIABLES expands to a list of all the variables defined in this instance
185 of make.
186 .TARGETS expands to a list of all the targets defined in this
187 instance of make.
188 Returns the variable reference passed in. */
190 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
192 static struct variable *
193 handle_special_var (var)
194 struct variable *var;
196 static unsigned long last_var_count = 0;
199 /* This one actually turns out to be very hard, due to the way the parser
200 records targets. The way it works is that target information is collected
201 internally until make knows the target is completely specified. It unitl
202 it sees that some new construct (a new target or variable) is defined that
203 it knows the previous one is done. In short, this means that if you do
204 this:
206 all:
208 TARGS := $(.TARGETS)
210 then $(TARGS) won't contain "all", because it's not until after the
211 variable is created that the previous target is completed.
213 Changing this would be a major pain. I think a less complex way to do it
214 would be to pre-define the target files as soon as the first line is
215 parsed, then come back and do the rest of the definition as now. That
216 would allow $(.TARGETS) to be correct without a major change to the way
217 the parser works.
219 if (streq (var->name, ".TARGETS"))
220 var->value = build_target_list (var->value);
221 else
224 if (streq (var->name, ".VARIABLES")
225 && global_variable_set.table.ht_fill != last_var_count)
227 unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
228 unsigned long len;
229 char *p;
230 struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
231 struct variable **end = &vp[global_variable_set.table.ht_size];
233 /* Make sure we have at least MAX bytes in the allocated buffer. */
234 var->value = xrealloc (var->value, max);
236 /* Walk through the hash of variables, constructing a list of names. */
237 p = var->value;
238 len = 0;
239 for (; vp < end; ++vp)
240 if (!HASH_VACANT (*vp))
242 struct variable *v = *vp;
243 int l = v->length;
245 len += l + 1;
246 if (len > max)
248 unsigned long off = p - var->value;
250 max += EXPANSION_INCREMENT (l + 1);
251 var->value = xrealloc (var->value, max);
252 p = &var->value[off];
255 bcopy (v->name, p, l);
256 p += l;
257 *(p++) = ' ';
259 *(p-1) = '\0';
261 /* Remember how many variables are in our current count. Since we never
262 remove variables from the list, this is a reliable way to know whether
263 the list is up to date or needs to be recomputed. */
265 last_var_count = global_variable_set.table.ht_fill;
268 return var;
272 /* Lookup a variable whose name is a string starting at NAME
273 and with LENGTH chars. NAME need not be null-terminated.
274 Returns address of the `struct variable' containing all info
275 on the variable, or nil if no such variable is defined. */
277 struct variable *
278 lookup_variable (name, length)
279 const char *name;
280 unsigned int length;
282 const struct variable_set_list *setlist;
283 struct variable var_key;
285 var_key.name = (char *) name;
286 var_key.length = length;
288 for (setlist = current_variable_set_list;
289 setlist != 0; setlist = setlist->next)
291 const struct variable_set *set = setlist->set;
292 struct variable *v;
294 v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
295 if (v)
296 return v->special ? handle_special_var (v) : v;
299 #ifdef VMS
300 /* since we don't read envp[] on startup, try to get the
301 variable via getenv() here. */
303 char *vname = alloca (length + 1);
304 char *value;
305 strncpy (vname, name, length);
306 vname[length] = 0;
307 value = getenv (vname);
308 if (value != 0)
310 char *sptr;
311 int scnt;
313 sptr = value;
314 scnt = 0;
316 while ((sptr = strchr (sptr, '$')))
318 scnt++;
319 sptr++;
322 if (scnt > 0)
324 char *nvalue;
325 char *nptr;
327 nvalue = alloca (strlen (value) + scnt + 1);
328 sptr = value;
329 nptr = nvalue;
331 while (*sptr)
333 if (*sptr == '$')
335 *nptr++ = '$';
336 *nptr++ = '$';
338 else
340 *nptr++ = *sptr;
342 sptr++;
345 *nptr = '\0';
346 return define_variable (vname, length, nvalue, o_env, 1);
350 return define_variable (vname, length, value, o_env, 1);
353 #endif /* VMS */
355 return 0;
358 /* Lookup a variable whose name is a string starting at NAME
359 and with LENGTH chars in set SET. NAME need not be null-terminated.
360 Returns address of the `struct variable' containing all info
361 on the variable, or nil if no such variable is defined. */
363 struct variable *
364 lookup_variable_in_set (name, length, set)
365 const char *name;
366 unsigned int length;
367 const struct variable_set *set;
369 struct variable var_key;
371 var_key.name = (char *) name;
372 var_key.length = length;
374 return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
377 /* Initialize FILE's variable set list. If FILE already has a variable set
378 list, the topmost variable set is left intact, but the the rest of the
379 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
380 rule, then we will use the "root" double-colon target's variable set as the
381 parent of FILE's variable set.
383 If we're READing a makefile, don't do the pattern variable search now,
384 since the pattern variable might not have been defined yet. */
386 void
387 initialize_file_variables (file, reading)
388 struct file *file;
389 int reading;
391 register struct variable_set_list *l = file->variables;
393 if (l == 0)
395 l = (struct variable_set_list *)
396 xmalloc (sizeof (struct variable_set_list));
397 l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
398 hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
399 variable_hash_1, variable_hash_2, variable_hash_cmp);
400 file->variables = l;
403 /* If this is a double-colon, then our "parent" is the "root" target for
404 this double-colon rule. Since that rule has the same name, parent,
405 etc. we can just use its variables as the "next" for ours. */
407 if (file->double_colon && file->double_colon != file)
409 initialize_file_variables (file->double_colon, reading);
410 l->next = file->double_colon->variables;
411 return;
414 if (file->parent == 0)
415 l->next = &global_setlist;
416 else
418 initialize_file_variables (file->parent, reading);
419 l->next = file->parent->variables;
422 /* If we're not reading makefiles and we haven't looked yet, see if
423 we can find a pattern variable. */
425 if (!reading && !file->pat_searched)
427 struct pattern_var *p = lookup_pattern_var (file->name);
429 file->pat_searched = 1;
430 if (p != 0)
432 /* If we found one, insert it between the current target's
433 variables and the next set, whatever it is. */
434 file->pat_variables = (struct variable_set_list *)
435 xmalloc (sizeof (struct variable_set_list));
436 file->pat_variables->set = p->vars->set;
440 /* If we have a pattern variable match, set it up. */
442 if (file->pat_variables != 0)
444 file->pat_variables->next = l->next;
445 l->next = file->pat_variables;
449 /* Pop the top set off the current variable set list,
450 and free all its storage. */
452 static void
453 free_variable_name_and_value (item)
454 void *item;
456 struct variable *v = (struct variable *) item;
457 free (v->name);
458 free (v->value);
461 void
462 pop_variable_scope ()
464 struct variable_set_list *setlist = current_variable_set_list;
465 struct variable_set *set = setlist->set;
467 current_variable_set_list = setlist->next;
468 free ((char *) setlist);
470 hash_map (&set->table, free_variable_name_and_value);
471 hash_free (&set->table, 1);
473 free ((char *) set);
476 struct variable_set_list *
477 create_new_variable_set ()
479 register struct variable_set_list *setlist;
480 register struct variable_set *set;
482 set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
483 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
484 variable_hash_1, variable_hash_2, variable_hash_cmp);
486 setlist = (struct variable_set_list *)
487 xmalloc (sizeof (struct variable_set_list));
488 setlist->set = set;
489 setlist->next = current_variable_set_list;
491 return setlist;
494 /* Create a new variable set and push it on the current setlist. */
496 struct variable_set_list *
497 push_new_variable_scope ()
499 return (current_variable_set_list = create_new_variable_set());
502 /* Merge SET1 into SET0, freeing unused storage in SET1. */
504 static void
505 merge_variable_sets (to_set, from_set)
506 struct variable_set *to_set, *from_set;
508 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
509 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
511 for ( ; from_var_slot < from_var_end; from_var_slot++)
512 if (! HASH_VACANT (*from_var_slot))
514 struct variable *from_var = *from_var_slot;
515 struct variable **to_var_slot
516 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
517 if (HASH_VACANT (*to_var_slot))
518 hash_insert_at (&to_set->table, from_var, to_var_slot);
519 else
521 /* GKM FIXME: delete in from_set->table */
522 free (from_var->value);
523 free (from_var);
528 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
530 void
531 merge_variable_set_lists (setlist0, setlist1)
532 struct variable_set_list **setlist0, *setlist1;
534 register struct variable_set_list *list0 = *setlist0;
535 struct variable_set_list *last0 = 0;
537 while (setlist1 != 0 && list0 != 0)
539 struct variable_set_list *next = setlist1;
540 setlist1 = setlist1->next;
542 merge_variable_sets (list0->set, next->set);
544 last0 = list0;
545 list0 = list0->next;
548 if (setlist1 != 0)
550 if (last0 == 0)
551 *setlist0 = setlist1;
552 else
553 last0->next = setlist1;
557 /* Define the automatic variables, and record the addresses
558 of their structures so we can change their values quickly. */
560 void
561 define_automatic_variables ()
563 #ifdef WINDOWS32
564 extern char* default_shell;
565 #else
566 extern char default_shell[];
567 #endif
568 register struct variable *v;
569 char buf[200];
571 sprintf (buf, "%u", makelevel);
572 (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
574 sprintf (buf, "%s%s%s",
575 version_string,
576 (remote_description == 0 || remote_description[0] == '\0')
577 ? "" : "-",
578 (remote_description == 0 || remote_description[0] == '\0')
579 ? "" : remote_description);
580 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
582 #ifdef __MSDOS__
583 /* Allow to specify a special shell just for Make,
584 and use $COMSPEC as the default $SHELL when appropriate. */
586 static char shell_str[] = "SHELL";
587 const int shlen = sizeof (shell_str) - 1;
588 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
589 struct variable *comp = lookup_variable ("COMSPEC", 7);
591 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
592 if (mshp)
593 (void) define_variable (shell_str, shlen,
594 mshp->value, o_env_override, 0);
595 else if (comp)
597 /* $COMSPEC shouldn't override $SHELL. */
598 struct variable *shp = lookup_variable (shell_str, shlen);
600 if (!shp)
601 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
604 #endif
606 /* This won't override any definition, but it
607 will provide one if there isn't one there. */
608 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
609 v->export = v_export; /* Always export SHELL. */
611 /* On MSDOS we do use SHELL from environment, since
612 it isn't a standard environment variable on MSDOS,
613 so whoever sets it, does that on purpose. */
614 #ifndef __MSDOS__
615 /* Don't let SHELL come from the environment. */
616 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
618 free (v->value);
619 v->origin = o_file;
620 v->value = xstrdup (default_shell);
622 #endif
624 /* Make sure MAKEFILES gets exported if it is set. */
625 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
626 v->export = v_ifset;
628 /* Define the magic D and F variables in terms of
629 the automatic variables they are variations of. */
631 #ifdef VMS
632 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
633 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
634 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
635 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
636 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
637 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
638 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
639 #else
640 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
641 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
642 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
643 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
644 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
645 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
646 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
647 #endif
648 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
649 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
650 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
651 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
652 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
653 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
654 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
657 int export_all_variables;
659 /* Create a new environment for FILE's commands.
660 If FILE is nil, this is for the `shell' function.
661 The child's MAKELEVEL variable is incremented. */
663 char **
664 target_environment (file)
665 struct file *file;
667 struct variable_set_list *set_list;
668 register struct variable_set_list *s;
669 struct hash_table table;
670 struct variable **v_slot;
671 struct variable **v_end;
672 struct variable makelevel_key;
673 char **result_0;
674 char **result;
676 if (file == 0)
677 set_list = current_variable_set_list;
678 else
679 set_list = file->variables;
681 hash_init (&table, VARIABLE_BUCKETS,
682 variable_hash_1, variable_hash_2, variable_hash_cmp);
684 /* Run through all the variable sets in the list,
685 accumulating variables in TABLE. */
686 for (s = set_list; s != 0; s = s->next)
688 struct variable_set *set = s->set;
689 v_slot = (struct variable **) set->table.ht_vec;
690 v_end = v_slot + set->table.ht_size;
691 for ( ; v_slot < v_end; v_slot++)
692 if (! HASH_VACANT (*v_slot))
694 struct variable **new_slot;
695 struct variable *v = *v_slot;
697 /* If this is a per-target variable and it hasn't been touched
698 already then look up the global version and take its export
699 value. */
700 if (v->per_target && v->export == v_default)
702 struct variable *gv;
704 gv = lookup_variable_in_set (v->name, strlen(v->name),
705 &global_variable_set);
706 if (gv)
707 v->export = gv->export;
710 switch (v->export)
712 case v_default:
713 if (v->origin == o_default || v->origin == o_automatic)
714 /* Only export default variables by explicit request. */
715 continue;
717 /* The variable doesn't have a name that can be exported. */
718 if (! v->exportable)
719 continue;
721 if (! export_all_variables
722 && v->origin != o_command
723 && v->origin != o_env && v->origin != o_env_override)
724 continue;
725 break;
727 case v_export:
728 break;
730 case v_noexport:
731 continue;
733 case v_ifset:
734 if (v->origin == o_default)
735 continue;
736 break;
739 new_slot = (struct variable **) hash_find_slot (&table, v);
740 if (HASH_VACANT (*new_slot))
741 hash_insert_at (&table, v, new_slot);
745 makelevel_key.name = MAKELEVEL_NAME;
746 makelevel_key.length = MAKELEVEL_LENGTH;
747 hash_delete (&table, &makelevel_key);
749 result = result_0 = (char **) xmalloc ((table.ht_fill + 2) * sizeof (char *));
751 v_slot = (struct variable **) table.ht_vec;
752 v_end = v_slot + table.ht_size;
753 for ( ; v_slot < v_end; v_slot++)
754 if (! HASH_VACANT (*v_slot))
756 struct variable *v = *v_slot;
758 /* If V is recursively expanded and didn't come from the environment,
759 expand its value. If it came from the environment, it should
760 go back into the environment unchanged. */
761 if (v->recursive
762 && v->origin != o_env && v->origin != o_env_override)
764 char *value = recursively_expand_for_file (v, file);
765 #ifdef WINDOWS32
766 if (strcmp(v->name, "Path") == 0 ||
767 strcmp(v->name, "PATH") == 0)
768 convert_Path_to_windows32(value, ';');
769 #endif
770 *result++ = concat (v->name, "=", value);
771 free (value);
773 else
775 #ifdef WINDOWS32
776 if (strcmp(v->name, "Path") == 0 ||
777 strcmp(v->name, "PATH") == 0)
778 convert_Path_to_windows32(v->value, ';');
779 #endif
780 *result++ = concat (v->name, "=", v->value);
784 *result = (char *) xmalloc (100);
785 (void) sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
786 *++result = 0;
788 hash_free (&table, 0);
790 return result_0;
793 /* Given a variable, a value, and a flavor, define the variable.
794 See the try_variable_definition() function for details on the parameters. */
796 struct variable *
797 do_variable_definition (flocp, varname, value, origin, flavor, target_var)
798 const struct floc *flocp;
799 const char *varname;
800 char *value;
801 enum variable_origin origin;
802 enum variable_flavor flavor;
803 int target_var;
805 char *p, *alloc_value = NULL;
806 struct variable *v;
807 int append = 0;
809 /* Calculate the variable's new value in VALUE. */
811 switch (flavor)
813 default:
814 case f_bogus:
815 /* Should not be possible. */
816 abort ();
817 case f_simple:
818 /* A simple variable definition "var := value". Expand the value.
819 We have to allocate memory since otherwise it'll clobber the
820 variable buffer, and we may still need that if we're looking at a
821 target-specific variable. */
822 p = alloc_value = allocated_variable_expand (value);
823 break;
824 case f_conditional:
825 /* A conditional variable definition "var ?= value".
826 The value is set IFF the variable is not defined yet. */
827 v = lookup_variable (varname, strlen (varname));
828 if (v)
829 return v;
831 flavor = f_recursive;
832 /* FALLTHROUGH */
833 case f_recursive:
834 /* A recursive variable definition "var = value".
835 The value is used verbatim. */
836 p = value;
837 break;
838 case f_append:
840 /* If we have += but we're in a target variable context, we want to
841 append only with other variables in the context of this target. */
842 if (target_var)
844 append = 1;
845 v = lookup_variable_in_set (varname, strlen (varname),
846 current_variable_set_list->set);
848 else
849 v = lookup_variable (varname, strlen (varname));
851 if (v == 0)
853 /* There was no old value.
854 This becomes a normal recursive definition. */
855 p = value;
856 flavor = f_recursive;
858 else
860 /* Paste the old and new values together in VALUE. */
862 unsigned int oldlen, vallen;
863 char *val;
865 val = value;
866 if (v->recursive)
867 /* The previous definition of the variable was recursive.
868 The new value is the unexpanded old and new values. */
869 flavor = f_recursive;
870 else
871 /* The previous definition of the variable was simple.
872 The new value comes from the old value, which was expanded
873 when it was set; and from the expanded new value. Allocate
874 memory for the expansion as we may still need the rest of the
875 buffer if we're looking at a target-specific variable. */
876 val = alloc_value = allocated_variable_expand (val);
878 oldlen = strlen (v->value);
879 vallen = strlen (val);
880 p = (char *) alloca (oldlen + 1 + vallen + 1);
881 bcopy (v->value, p, oldlen);
882 p[oldlen] = ' ';
883 bcopy (val, &p[oldlen + 1], vallen + 1);
888 #ifdef __MSDOS__
889 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
890 non-Unix systems don't conform to this default configuration (in
891 fact, most of them don't even have `/bin'). On the other hand,
892 $SHELL in the environment, if set, points to the real pathname of
893 the shell.
894 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
895 the Makefile override $SHELL from the environment. But first, we
896 look for the basename of the shell in the directory where SHELL=
897 points, and along the $PATH; if it is found in any of these places,
898 we define $SHELL to be the actual pathname of the shell. Thus, if
899 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
900 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
901 defining SHELL to be "d:/unix/bash.exe". */
902 if ((origin == o_file || origin == o_override)
903 && strcmp (varname, "SHELL") == 0)
905 char shellpath[PATH_MAX];
906 extern char * __dosexec_find_on_path (const char *, char *[], char *);
908 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
909 if (__dosexec_find_on_path (p, (char **)0, shellpath))
911 char *p;
913 for (p = shellpath; *p; p++)
915 if (*p == '\\')
916 *p = '/';
918 v = define_variable_loc (varname, strlen (varname),
919 shellpath, origin, flavor == f_recursive,
920 flocp);
922 else
924 char *shellbase, *bslash;
925 struct variable *pathv = lookup_variable ("PATH", 4);
926 char *path_string;
927 char *fake_env[2];
928 size_t pathlen = 0;
930 shellbase = strrchr (p, '/');
931 bslash = strrchr (p, '\\');
932 if (!shellbase || bslash > shellbase)
933 shellbase = bslash;
934 if (!shellbase && p[1] == ':')
935 shellbase = p + 1;
936 if (shellbase)
937 shellbase++;
938 else
939 shellbase = p;
941 /* Search for the basename of the shell (with standard
942 executable extensions) along the $PATH. */
943 if (pathv)
944 pathlen = strlen (pathv->value);
945 path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
946 /* On MSDOS, current directory is considered as part of $PATH. */
947 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
948 fake_env[0] = path_string;
949 fake_env[1] = (char *)0;
950 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
952 char *p;
954 for (p = shellpath; *p; p++)
956 if (*p == '\\')
957 *p = '/';
959 v = define_variable_loc (varname, strlen (varname),
960 shellpath, origin,
961 flavor == f_recursive, flocp);
963 else
964 v = lookup_variable (varname, strlen (varname));
966 free (path_string);
969 else
970 #endif /* __MSDOS__ */
971 #ifdef WINDOWS32
972 if ((origin == o_file || origin == o_override) && streq (varname, "SHELL"))
974 extern char *default_shell;
976 /* Call shell locator function. If it returns TRUE, then
977 set no_default_sh_exe to indicate sh was found and
978 set new value for SHELL variable. */
980 if (find_and_set_default_shell (p))
982 v = define_variable_in_set (varname, strlen (varname), default_shell,
983 origin, flavor == f_recursive,
984 (target_var
985 ? current_variable_set_list->set
986 : NULL),
987 flocp);
988 no_default_sh_exe = 0;
990 else
991 v = lookup_variable (varname, strlen (varname));
993 else
994 #endif
996 /* If we are defining variables inside an $(eval ...), we might have a
997 different variable context pushed, not the global context (maybe we're
998 inside a $(call ...) or something. Since this function is only ever
999 invoked in places where we want to define globally visible variables,
1000 make sure we define this variable in the global set. */
1002 v = define_variable_in_set (varname, strlen (varname), p,
1003 origin, flavor == f_recursive,
1004 (target_var
1005 ? current_variable_set_list->set : NULL),
1006 flocp);
1007 v->append = append;
1009 if (alloc_value)
1010 free (alloc_value);
1012 return v;
1015 /* Try to interpret LINE (a null-terminated string) as a variable definition.
1017 ORIGIN may be o_file, o_override, o_env, o_env_override,
1018 or o_command specifying that the variable definition comes
1019 from a makefile, an override directive, the environment with
1020 or without the -e switch, or the command line.
1022 See the comments for parse_variable_definition().
1024 If LINE was recognized as a variable definition, a pointer to its `struct
1025 variable' is returned. If LINE is not a variable definition, NULL is
1026 returned. */
1028 struct variable *
1029 try_variable_definition (flocp, line, origin, target_var)
1030 const struct floc *flocp;
1031 char *line;
1032 enum variable_origin origin;
1033 int target_var;
1035 register int c;
1036 register char *p = line;
1037 register char *beg;
1038 register char *end;
1039 enum variable_flavor flavor = f_bogus;
1040 char *name, *expanded_name;
1041 struct variable *v;
1043 while (1)
1045 c = *p++;
1046 if (c == '\0' || c == '#')
1047 return 0;
1048 if (c == '=')
1050 end = p - 1;
1051 flavor = f_recursive;
1052 break;
1054 else if (c == ':')
1055 if (*p == '=')
1057 end = p++ - 1;
1058 flavor = f_simple;
1059 break;
1061 else
1062 /* A colon other than := is a rule line, not a variable defn. */
1063 return 0;
1064 else if (c == '+' && *p == '=')
1066 end = p++ - 1;
1067 flavor = f_append;
1068 break;
1070 else if (c == '?' && *p == '=')
1072 end = p++ - 1;
1073 flavor = f_conditional;
1074 break;
1076 else if (c == '$')
1078 /* This might begin a variable expansion reference. Make sure we
1079 don't misrecognize chars inside the reference as =, := or +=. */
1080 char closeparen;
1081 int count;
1082 c = *p++;
1083 if (c == '(')
1084 closeparen = ')';
1085 else if (c == '{')
1086 closeparen = '}';
1087 else
1088 continue; /* Nope. */
1090 /* P now points past the opening paren or brace.
1091 Count parens or braces until it is matched. */
1092 count = 0;
1093 for (; *p != '\0'; ++p)
1095 if (*p == c)
1096 ++count;
1097 else if (*p == closeparen && --count < 0)
1099 ++p;
1100 break;
1106 beg = next_token (line);
1107 while (end > beg && isblank ((unsigned char)end[-1]))
1108 --end;
1109 p = next_token (p);
1111 /* Expand the name, so "$(foo)bar = baz" works. */
1112 name = (char *) alloca (end - beg + 1);
1113 bcopy (beg, name, end - beg);
1114 name[end - beg] = '\0';
1115 expanded_name = allocated_variable_expand (name);
1117 if (expanded_name[0] == '\0')
1118 fatal (flocp, _("empty variable name"));
1120 v = do_variable_definition (flocp, expanded_name, p,
1121 origin, flavor, target_var);
1123 free (expanded_name);
1125 return v;
1128 /* Print information for variable V, prefixing it with PREFIX. */
1130 static void
1131 print_variable (v, prefix)
1132 register struct variable *v;
1133 char *prefix;
1135 const char *origin;
1137 switch (v->origin)
1139 case o_default:
1140 origin = _("default");
1141 break;
1142 case o_env:
1143 origin = _("environment");
1144 break;
1145 case o_file:
1146 origin = _("makefile");
1147 break;
1148 case o_env_override:
1149 origin = _("environment under -e");
1150 break;
1151 case o_command:
1152 origin = _("command line");
1153 break;
1154 case o_override:
1155 origin = _("`override' directive");
1156 break;
1157 case o_automatic:
1158 origin = _("automatic");
1159 break;
1160 case o_invalid:
1161 default:
1162 abort ();
1164 fputs ("# ", stdout);
1165 fputs (origin, stdout);
1166 if (v->fileinfo.filenm)
1167 printf (_(" (from `%s', line %lu)"),
1168 v->fileinfo.filenm, v->fileinfo.lineno);
1169 putchar ('\n');
1170 fputs (prefix, stdout);
1172 /* Is this a `define'? */
1173 if (v->recursive && strchr (v->value, '\n') != 0)
1174 printf ("define %s\n%s\nendef\n", v->name, v->value);
1175 else
1177 register char *p;
1179 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1181 /* Check if the value is just whitespace. */
1182 p = next_token (v->value);
1183 if (p != v->value && *p == '\0')
1184 /* All whitespace. */
1185 printf ("$(subst ,,%s)", v->value);
1186 else if (v->recursive)
1187 fputs (v->value, stdout);
1188 else
1189 /* Double up dollar signs. */
1190 for (p = v->value; *p != '\0'; ++p)
1192 if (*p == '$')
1193 putchar ('$');
1194 putchar (*p);
1196 putchar ('\n');
1201 /* Print all the variables in SET. PREFIX is printed before
1202 the actual variable definitions (everything else is comments). */
1204 void
1205 print_variable_set (set, prefix)
1206 register struct variable_set *set;
1207 char *prefix;
1209 hash_map_arg (&set->table, print_variable, prefix);
1211 fputs (_("# variable set hash-table stats:\n"), stdout);
1212 fputs ("# ", stdout);
1213 hash_print_stats (&set->table, stdout);
1214 putc ('\n', stdout);
1217 /* Print the data base of variables. */
1219 void
1220 print_variable_data_base ()
1222 puts (_("\n# Variables\n"));
1224 print_variable_set (&global_variable_set, "");
1228 /* Print all the local variables of FILE. */
1230 void
1231 print_file_variables (file)
1232 struct file *file;
1234 if (file->variables != 0)
1235 print_variable_set (file->variables->set, "# ");
1238 #ifdef WINDOWS32
1239 void
1240 sync_Path_environment(void)
1242 char* path = allocated_variable_expand("$(Path)");
1243 static char* environ_path = NULL;
1245 if (!path)
1246 return;
1249 * If done this before, don't leak memory unnecessarily.
1250 * Free the previous entry before allocating new one.
1252 if (environ_path)
1253 free(environ_path);
1256 * Create something WINDOWS32 world can grok
1258 convert_Path_to_windows32(path, ';');
1259 environ_path = concat("Path", "=", path);
1260 putenv(environ_path);
1261 free(path);
1263 #endif