Break out of an infinite loop if we're not making progress.
[make.git] / variable.c
blob8576d6a8c45fd601c81dc2be4a19bf95dff93f6b
1 /* Internals of variables for GNU Make.
2 Copyright (C) 1988-2012 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
8 version.
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
17 #include "make.h"
19 #include <assert.h>
21 #include "dep.h"
22 #include "filedef.h"
23 #include "job.h"
24 #include "commands.h"
25 #include "variable.h"
26 #include "rule.h"
27 #ifdef WINDOWS32
28 #include "pathstuff.h"
29 #endif
30 #include "hash.h"
32 /* Chain of all pattern-specific variables. */
34 static struct pattern_var *pattern_vars;
36 /* Pointer to the last struct in the pack of a specific size, from 1 to 255.*/
38 static struct pattern_var *last_pattern_vars[256];
40 /* Create a new pattern-specific variable struct. The new variable is
41 inserted into the PATTERN_VARS list in the shortest patterns first
42 order to support the shortest stem matching (the variables are
43 matched in the reverse order so the ones with the longest pattern
44 will be considered first). Variables with the same pattern length
45 are inserted in the definition order. */
47 struct pattern_var *
48 create_pattern_var (const char *target, const char *suffix)
50 register unsigned int len = strlen (target);
51 register struct pattern_var *p = xmalloc (sizeof (struct pattern_var));
53 if (pattern_vars != 0)
55 if (len < 256 && last_pattern_vars[len] != 0)
57 p->next = last_pattern_vars[len]->next;
58 last_pattern_vars[len]->next = p;
60 else
62 /* Find the position where we can insert this variable. */
63 register struct pattern_var **v;
65 for (v = &pattern_vars; ; v = &(*v)->next)
67 /* Insert at the end of the pack so that patterns with the
68 same length appear in the order they were defined .*/
70 if (*v == 0 || (*v)->len > len)
72 p->next = *v;
73 *v = p;
74 break;
79 else
81 pattern_vars = p;
82 p->next = 0;
85 p->target = target;
86 p->len = len;
87 p->suffix = suffix + 1;
89 if (len < 256)
90 last_pattern_vars[len] = p;
92 return p;
95 /* Look up a target in the pattern-specific variable list. */
97 static struct pattern_var *
98 lookup_pattern_var (struct pattern_var *start, const char *target)
100 struct pattern_var *p;
101 unsigned int targlen = strlen(target);
103 for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
105 const char *stem;
106 unsigned int stemlen;
108 if (p->len > targlen)
109 /* It can't possibly match. */
110 continue;
112 /* From the lengths of the filename and the pattern parts,
113 find the stem: the part of the filename that matches the %. */
114 stem = target + (p->suffix - p->target - 1);
115 stemlen = targlen - p->len + 1;
117 /* Compare the text in the pattern before the stem, if any. */
118 if (stem > target && !strneq (p->target, target, stem - target))
119 continue;
121 /* Compare the text in the pattern after the stem, if any.
122 We could test simply using streq, but this way we compare the
123 first two characters immediately. This saves time in the very
124 common case where the first character matches because it is a
125 period. */
126 if (*p->suffix == stem[stemlen]
127 && (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
128 break;
131 return p;
134 /* Hash table of all global variable definitions. */
136 static unsigned long
137 variable_hash_1 (const void *keyv)
139 struct variable const *key = (struct variable const *) keyv;
140 return_STRING_N_HASH_1 (key->name, key->length);
143 static unsigned long
144 variable_hash_2 (const void *keyv)
146 struct variable const *key = (struct variable const *) keyv;
147 return_STRING_N_HASH_2 (key->name, key->length);
150 static int
151 variable_hash_cmp (const void *xv, const void *yv)
153 struct variable const *x = (struct variable const *) xv;
154 struct variable const *y = (struct variable const *) yv;
155 int result = x->length - y->length;
156 if (result)
157 return result;
158 return_STRING_N_COMPARE (x->name, y->name, x->length);
161 #ifndef VARIABLE_BUCKETS
162 #define VARIABLE_BUCKETS 523
163 #endif
164 #ifndef PERFILE_VARIABLE_BUCKETS
165 #define PERFILE_VARIABLE_BUCKETS 23
166 #endif
167 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
168 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
169 #endif
171 static struct variable_set global_variable_set;
172 static struct variable_set_list global_setlist
173 = { 0, &global_variable_set, 0 };
174 struct variable_set_list *current_variable_set_list = &global_setlist;
176 /* Implement variables. */
178 void
179 init_hash_global_variable_set (void)
181 hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
182 variable_hash_1, variable_hash_2, variable_hash_cmp);
185 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
186 LENGTH is the length of NAME, which does not need to be null-terminated.
187 ORIGIN specifies the origin of the variable (makefile, command line
188 or environment).
189 If RECURSIVE is nonzero a flag is set in the variable saying
190 that it should be recursively re-expanded. */
192 struct variable *
193 define_variable_in_set (const char *name, unsigned int length,
194 const char *value, enum variable_origin origin,
195 int recursive, struct variable_set *set,
196 const struct floc *flocp)
198 struct variable *v;
199 struct variable **var_slot;
200 struct variable var_key;
202 if (set == NULL)
203 set = &global_variable_set;
205 var_key.name = (char *) name;
206 var_key.length = length;
207 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
209 if (env_overrides && origin == o_env)
210 origin = o_env_override;
212 v = *var_slot;
213 if (! HASH_VACANT (v))
215 if (env_overrides && v->origin == o_env)
216 /* V came from in the environment. Since it was defined
217 before the switches were parsed, it wasn't affected by -e. */
218 v->origin = o_env_override;
220 /* A variable of this name is already defined.
221 If the old definition is from a stronger source
222 than this one, don't redefine it. */
223 if ((int) origin >= (int) v->origin)
225 if (v->value != 0)
226 free (v->value);
227 v->value = xstrdup (value);
228 if (flocp != 0)
229 v->fileinfo = *flocp;
230 else
231 v->fileinfo.filenm = 0;
232 v->origin = origin;
233 v->recursive = recursive;
235 return v;
238 /* Create a new variable definition and add it to the hash table. */
240 v = xmalloc (sizeof (struct variable));
241 v->name = xstrndup (name, length);
242 v->length = length;
243 hash_insert_at (&set->table, v, var_slot);
244 v->value = xstrdup (value);
245 if (flocp != 0)
246 v->fileinfo = *flocp;
247 else
248 v->fileinfo.filenm = 0;
249 v->origin = origin;
250 v->recursive = recursive;
251 v->special = 0;
252 v->expanding = 0;
253 v->exp_count = 0;
254 v->per_target = 0;
255 v->append = 0;
256 v->private_var = 0;
257 v->export = v_default;
259 v->exportable = 1;
260 if (*name != '_' && (*name < 'A' || *name > 'Z')
261 && (*name < 'a' || *name > 'z'))
262 v->exportable = 0;
263 else
265 for (++name; *name != '\0'; ++name)
266 if (*name != '_' && (*name < 'a' || *name > 'z')
267 && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
268 break;
270 if (*name != '\0')
271 v->exportable = 0;
274 return v;
278 /* Undefine variable named NAME in SET. LENGTH is the length of NAME, which
279 does not need to be null-terminated. ORIGIN specifies the origin of the
280 variable (makefile, command line or environment). */
282 static void
283 free_variable_name_and_value (const void *item);
285 void
286 undefine_variable_in_set (const char *name, unsigned int length,
287 enum variable_origin origin,
288 struct variable_set *set)
290 struct variable *v;
291 struct variable **var_slot;
292 struct variable var_key;
294 if (set == NULL)
295 set = &global_variable_set;
297 var_key.name = (char *) name;
298 var_key.length = length;
299 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
301 if (env_overrides && origin == o_env)
302 origin = o_env_override;
304 v = *var_slot;
305 if (! HASH_VACANT (v))
307 if (env_overrides && v->origin == o_env)
308 /* V came from in the environment. Since it was defined
309 before the switches were parsed, it wasn't affected by -e. */
310 v->origin = o_env_override;
312 /* If the definition is from a stronger source than this one, don't
313 undefine it. */
314 if ((int) origin >= (int) v->origin)
316 hash_delete_at (&set->table, var_slot);
317 free_variable_name_and_value (v);
322 /* If the variable passed in is "special", handle its special nature.
323 Currently there are two such variables, both used for introspection:
324 .VARIABLES expands to a list of all the variables defined in this instance
325 of make.
326 .TARGETS expands to a list of all the targets defined in this
327 instance of make.
328 Returns the variable reference passed in. */
330 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
332 static struct variable *
333 lookup_special_var (struct variable *var)
335 static unsigned long last_var_count = 0;
338 /* This one actually turns out to be very hard, due to the way the parser
339 records targets. The way it works is that target information is collected
340 internally until make knows the target is completely specified. It unitl
341 it sees that some new construct (a new target or variable) is defined that
342 it knows the previous one is done. In short, this means that if you do
343 this:
345 all:
347 TARGS := $(.TARGETS)
349 then $(TARGS) won't contain "all", because it's not until after the
350 variable is created that the previous target is completed.
352 Changing this would be a major pain. I think a less complex way to do it
353 would be to pre-define the target files as soon as the first line is
354 parsed, then come back and do the rest of the definition as now. That
355 would allow $(.TARGETS) to be correct without a major change to the way
356 the parser works.
358 if (streq (var->name, ".TARGETS"))
359 var->value = build_target_list (var->value);
360 else
363 if (streq (var->name, ".VARIABLES")
364 && global_variable_set.table.ht_fill != last_var_count)
366 unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
367 unsigned long len;
368 char *p;
369 struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
370 struct variable **end = &vp[global_variable_set.table.ht_size];
372 /* Make sure we have at least MAX bytes in the allocated buffer. */
373 var->value = xrealloc (var->value, max);
375 /* Walk through the hash of variables, constructing a list of names. */
376 p = var->value;
377 len = 0;
378 for (; vp < end; ++vp)
379 if (!HASH_VACANT (*vp))
381 struct variable *v = *vp;
382 int l = v->length;
384 len += l + 1;
385 if (len > max)
387 unsigned long off = p - var->value;
389 max += EXPANSION_INCREMENT (l + 1);
390 var->value = xrealloc (var->value, max);
391 p = &var->value[off];
394 memcpy (p, v->name, l);
395 p += l;
396 *(p++) = ' ';
398 *(p-1) = '\0';
400 /* Remember how many variables are in our current count. Since we never
401 remove variables from the list, this is a reliable way to know whether
402 the list is up to date or needs to be recomputed. */
404 last_var_count = global_variable_set.table.ht_fill;
407 return var;
411 /* Lookup a variable whose name is a string starting at NAME
412 and with LENGTH chars. NAME need not be null-terminated.
413 Returns address of the 'struct variable' containing all info
414 on the variable, or nil if no such variable is defined. */
416 struct variable *
417 lookup_variable (const char *name, unsigned int length)
419 const struct variable_set_list *setlist;
420 struct variable var_key;
421 int is_parent = 0;
423 var_key.name = (char *) name;
424 var_key.length = length;
426 for (setlist = current_variable_set_list;
427 setlist != 0; setlist = setlist->next)
429 const struct variable_set *set = setlist->set;
430 struct variable *v;
432 v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
433 if (v && (!is_parent || !v->private_var))
434 return v->special ? lookup_special_var (v) : v;
436 is_parent |= setlist->next_is_parent;
439 #ifdef VMS
440 /* since we don't read envp[] on startup, try to get the
441 variable via getenv() here. */
443 char *vname = alloca (length + 1);
444 char *value;
445 strncpy (vname, name, length);
446 vname[length] = 0;
447 value = getenv (vname);
448 if (value != 0)
450 char *sptr;
451 int scnt;
453 sptr = value;
454 scnt = 0;
456 while ((sptr = strchr (sptr, '$')))
458 scnt++;
459 sptr++;
462 if (scnt > 0)
464 char *nvalue;
465 char *nptr;
467 nvalue = alloca (strlen (value) + scnt + 1);
468 sptr = value;
469 nptr = nvalue;
471 while (*sptr)
473 if (*sptr == '$')
475 *nptr++ = '$';
476 *nptr++ = '$';
478 else
480 *nptr++ = *sptr;
482 sptr++;
485 *nptr = '\0';
486 return define_variable (vname, length, nvalue, o_env, 1);
490 return define_variable (vname, length, value, o_env, 1);
493 #endif /* VMS */
495 return 0;
498 /* Lookup a variable whose name is a string starting at NAME
499 and with LENGTH chars in set SET. NAME need not be null-terminated.
500 Returns address of the 'struct variable' containing all info
501 on the variable, or nil if no such variable is defined. */
503 struct variable *
504 lookup_variable_in_set (const char *name, unsigned int length,
505 const struct variable_set *set)
507 struct variable var_key;
509 var_key.name = (char *) name;
510 var_key.length = length;
512 return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
515 /* Initialize FILE's variable set list. If FILE already has a variable set
516 list, the topmost variable set is left intact, but the the rest of the
517 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
518 rule, then we will use the "root" double-colon target's variable set as the
519 parent of FILE's variable set.
521 If we're READING a makefile, don't do the pattern variable search now,
522 since the pattern variable might not have been defined yet. */
524 void
525 initialize_file_variables (struct file *file, int reading)
527 struct variable_set_list *l = file->variables;
529 if (l == 0)
531 l = (struct variable_set_list *)
532 xmalloc (sizeof (struct variable_set_list));
533 l->set = xmalloc (sizeof (struct variable_set));
534 hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
535 variable_hash_1, variable_hash_2, variable_hash_cmp);
536 file->variables = l;
539 /* If this is a double-colon, then our "parent" is the "root" target for
540 this double-colon rule. Since that rule has the same name, parent,
541 etc. we can just use its variables as the "next" for ours. */
543 if (file->double_colon && file->double_colon != file)
545 initialize_file_variables (file->double_colon, reading);
546 l->next = file->double_colon->variables;
547 l->next_is_parent = 0;
548 return;
551 if (file->parent == 0)
552 l->next = &global_setlist;
553 else
555 initialize_file_variables (file->parent, reading);
556 l->next = file->parent->variables;
558 l->next_is_parent = 1;
560 /* If we're not reading makefiles and we haven't looked yet, see if
561 we can find pattern variables for this target. */
563 if (!reading && !file->pat_searched)
565 struct pattern_var *p;
567 p = lookup_pattern_var (0, file->name);
568 if (p != 0)
570 struct variable_set_list *global = current_variable_set_list;
572 /* We found at least one. Set up a new variable set to accumulate
573 all the pattern variables that match this target. */
575 file->pat_variables = create_new_variable_set ();
576 current_variable_set_list = file->pat_variables;
580 /* We found one, so insert it into the set. */
582 struct variable *v;
584 if (p->variable.flavor == f_simple)
586 v = define_variable_loc (
587 p->variable.name, strlen (p->variable.name),
588 p->variable.value, p->variable.origin,
589 0, &p->variable.fileinfo);
591 v->flavor = f_simple;
593 else
595 v = do_variable_definition (
596 &p->variable.fileinfo, p->variable.name,
597 p->variable.value, p->variable.origin,
598 p->variable.flavor, 1);
601 /* Also mark it as a per-target and copy export status. */
602 v->per_target = p->variable.per_target;
603 v->export = p->variable.export;
604 v->private_var = p->variable.private_var;
606 while ((p = lookup_pattern_var (p, file->name)) != 0);
608 current_variable_set_list = global;
610 file->pat_searched = 1;
613 /* If we have a pattern variable match, set it up. */
615 if (file->pat_variables != 0)
617 file->pat_variables->next = l->next;
618 file->pat_variables->next_is_parent = l->next_is_parent;
619 l->next = file->pat_variables;
620 l->next_is_parent = 0;
624 /* Pop the top set off the current variable set list,
625 and free all its storage. */
627 struct variable_set_list *
628 create_new_variable_set (void)
630 register struct variable_set_list *setlist;
631 register struct variable_set *set;
633 set = xmalloc (sizeof (struct variable_set));
634 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
635 variable_hash_1, variable_hash_2, variable_hash_cmp);
637 setlist = (struct variable_set_list *)
638 xmalloc (sizeof (struct variable_set_list));
639 setlist->set = set;
640 setlist->next = current_variable_set_list;
641 setlist->next_is_parent = 0;
643 return setlist;
646 static void
647 free_variable_name_and_value (const void *item)
649 struct variable *v = (struct variable *) item;
650 free (v->name);
651 free (v->value);
654 void
655 free_variable_set (struct variable_set_list *list)
657 hash_map (&list->set->table, free_variable_name_and_value);
658 hash_free (&list->set->table, 1);
659 free (list->set);
660 free (list);
663 /* Create a new variable set and push it on the current setlist.
664 If we're pushing a global scope (that is, the current scope is the global
665 scope) then we need to "push" it the other way: file variable sets point
666 directly to the global_setlist so we need to replace that with the new one.
669 struct variable_set_list *
670 push_new_variable_scope (void)
672 current_variable_set_list = create_new_variable_set();
673 if (current_variable_set_list->next == &global_setlist)
675 /* It was the global, so instead of new -> &global we want to replace
676 &global with the new one and have &global -> new, with current still
677 pointing to &global */
678 struct variable_set *set = current_variable_set_list->set;
679 current_variable_set_list->set = global_setlist.set;
680 global_setlist.set = set;
681 current_variable_set_list->next = global_setlist.next;
682 global_setlist.next = current_variable_set_list;
683 current_variable_set_list = &global_setlist;
685 return (current_variable_set_list);
688 void
689 pop_variable_scope (void)
691 struct variable_set_list *setlist;
692 struct variable_set *set;
694 /* Can't call this if there's no scope to pop! */
695 assert(current_variable_set_list->next != NULL);
697 if (current_variable_set_list != &global_setlist)
699 /* We're not pointing to the global setlist, so pop this one. */
700 setlist = current_variable_set_list;
701 set = setlist->set;
702 current_variable_set_list = setlist->next;
704 else
706 /* This set is the one in the global_setlist, but there is another global
707 set beyond that. We want to copy that set to global_setlist, then
708 delete what used to be in global_setlist. */
709 setlist = global_setlist.next;
710 set = global_setlist.set;
711 global_setlist.set = setlist->set;
712 global_setlist.next = setlist->next;
713 global_setlist.next_is_parent = setlist->next_is_parent;
716 /* Free the one we no longer need. */
717 free (setlist);
718 hash_map (&set->table, free_variable_name_and_value);
719 hash_free (&set->table, 1);
720 free (set);
723 /* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
725 static void
726 merge_variable_sets (struct variable_set *to_set,
727 struct variable_set *from_set)
729 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
730 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
732 for ( ; from_var_slot < from_var_end; from_var_slot++)
733 if (! HASH_VACANT (*from_var_slot))
735 struct variable *from_var = *from_var_slot;
736 struct variable **to_var_slot
737 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
738 if (HASH_VACANT (*to_var_slot))
739 hash_insert_at (&to_set->table, from_var, to_var_slot);
740 else
742 /* GKM FIXME: delete in from_set->table */
743 free (from_var->value);
744 free (from_var);
749 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
751 void
752 merge_variable_set_lists (struct variable_set_list **setlist0,
753 struct variable_set_list *setlist1)
755 struct variable_set_list *to = *setlist0;
756 struct variable_set_list *last0 = 0;
758 /* If there's nothing to merge, stop now. */
759 if (!setlist1)
760 return;
762 /* This loop relies on the fact that all setlists terminate with the global
763 setlist (before NULL). If that's not true, arguably we SHOULD die. */
764 if (to)
765 while (setlist1 != &global_setlist && to != &global_setlist)
767 struct variable_set_list *from = setlist1;
768 setlist1 = setlist1->next;
770 merge_variable_sets (to->set, from->set);
772 last0 = to;
773 to = to->next;
776 if (setlist1 != &global_setlist)
778 if (last0 == 0)
779 *setlist0 = setlist1;
780 else
781 last0->next = setlist1;
785 /* Define the automatic variables, and record the addresses
786 of their structures so we can change their values quickly. */
788 void
789 define_automatic_variables (void)
791 #if defined(WINDOWS32) || defined(__EMX__)
792 extern char* default_shell;
793 #else
794 extern char default_shell[];
795 #endif
796 register struct variable *v;
797 char buf[200];
799 sprintf (buf, "%u", makelevel);
800 define_variable_cname (MAKELEVEL_NAME, buf, o_env, 0);
802 sprintf (buf, "%s%s%s",
803 version_string,
804 (remote_description == 0 || remote_description[0] == '\0')
805 ? "" : "-",
806 (remote_description == 0 || remote_description[0] == '\0')
807 ? "" : remote_description);
808 define_variable_cname ("MAKE_VERSION", buf, o_default, 0);
810 #ifdef __MSDOS__
811 /* Allow to specify a special shell just for Make,
812 and use $COMSPEC as the default $SHELL when appropriate. */
814 static char shell_str[] = "SHELL";
815 const int shlen = sizeof (shell_str) - 1;
816 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
817 struct variable *comp = lookup_variable ("COMSPEC", 7);
819 /* $(MAKESHELL) overrides $(SHELL) even if -e is in effect. */
820 if (mshp)
821 (void) define_variable (shell_str, shlen,
822 mshp->value, o_env_override, 0);
823 else if (comp)
825 /* $(COMSPEC) shouldn't override $(SHELL). */
826 struct variable *shp = lookup_variable (shell_str, shlen);
828 if (!shp)
829 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
832 #elif defined(__EMX__)
834 static char shell_str[] = "SHELL";
835 const int shlen = sizeof (shell_str) - 1;
836 struct variable *shell = lookup_variable (shell_str, shlen);
837 struct variable *replace = lookup_variable ("MAKESHELL", 9);
839 /* if $MAKESHELL is defined in the environment assume o_env_override */
840 if (replace && *replace->value && replace->origin == o_env)
841 replace->origin = o_env_override;
843 /* if $MAKESHELL is not defined use $SHELL but only if the variable
844 did not come from the environment */
845 if (!replace || !*replace->value)
846 if (shell && *shell->value && (shell->origin == o_env
847 || shell->origin == o_env_override))
849 /* overwrite whatever we got from the environment */
850 free(shell->value);
851 shell->value = xstrdup (default_shell);
852 shell->origin = o_default;
855 /* Some people do not like cmd to be used as the default
856 if $SHELL is not defined in the Makefile.
857 With -DNO_CMD_DEFAULT you can turn off this behaviour */
858 # ifndef NO_CMD_DEFAULT
859 /* otherwise use $COMSPEC */
860 if (!replace || !*replace->value)
861 replace = lookup_variable ("COMSPEC", 7);
863 /* otherwise use $OS2_SHELL */
864 if (!replace || !*replace->value)
865 replace = lookup_variable ("OS2_SHELL", 9);
866 # else
867 # warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
868 # endif
870 if (replace && *replace->value)
871 /* overwrite $SHELL */
872 (void) define_variable (shell_str, shlen, replace->value,
873 replace->origin, 0);
874 else
875 /* provide a definition if there is none */
876 (void) define_variable (shell_str, shlen, default_shell,
877 o_default, 0);
880 #endif
882 /* This won't override any definition, but it will provide one if there
883 isn't one there. */
884 v = define_variable_cname ("SHELL", default_shell, o_default, 0);
885 #ifdef __MSDOS__
886 v->export = v_export; /* Export always SHELL. */
887 #endif
889 /* On MSDOS we do use SHELL from environment, since it isn't a standard
890 environment variable on MSDOS, so whoever sets it, does that on purpose.
891 On OS/2 we do not use SHELL from environment but we have already handled
892 that problem above. */
893 #if !defined(__MSDOS__) && !defined(__EMX__)
894 /* Don't let SHELL come from the environment. */
895 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
897 free (v->value);
898 v->origin = o_file;
899 v->value = xstrdup (default_shell);
901 #endif
903 /* Make sure MAKEFILES gets exported if it is set. */
904 v = define_variable_cname ("MAKEFILES", "", o_default, 0);
905 v->export = v_ifset;
907 /* Define the magic D and F variables in terms of
908 the automatic variables they are variations of. */
910 #ifdef VMS
911 define_variable_cname ("@D", "$(dir $@)", o_automatic, 1);
912 define_variable_cname ("%D", "$(dir $%)", o_automatic, 1);
913 define_variable_cname ("*D", "$(dir $*)", o_automatic, 1);
914 define_variable_cname ("<D", "$(dir $<)", o_automatic, 1);
915 define_variable_cname ("?D", "$(dir $?)", o_automatic, 1);
916 define_variable_cname ("^D", "$(dir $^)", o_automatic, 1);
917 define_variable_cname ("+D", "$(dir $+)", o_automatic, 1);
918 #elif defined(__MSDOS__) || defined(WINDOWS32)
919 /* For consistency, remove the trailing backslash as well as slash. */
920 define_variable_cname ("@D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $@)))",
921 o_automatic, 1);
922 define_variable_cname ("%D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $%)))",
923 o_automatic, 1);
924 define_variable_cname ("*D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $*)))",
925 o_automatic, 1);
926 define_variable_cname ("<D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $<)))",
927 o_automatic, 1);
928 define_variable_cname ("?D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $?)))",
929 o_automatic, 1);
930 define_variable_cname ("^D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $^)))",
931 o_automatic, 1);
932 define_variable_cname ("+D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $+)))",
933 o_automatic, 1);
934 #else /* not __MSDOS__, not WINDOWS32 */
935 define_variable_cname ("@D", "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
936 define_variable_cname ("%D", "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
937 define_variable_cname ("*D", "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
938 define_variable_cname ("<D", "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
939 define_variable_cname ("?D", "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
940 define_variable_cname ("^D", "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
941 define_variable_cname ("+D", "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
942 #endif
943 define_variable_cname ("@F", "$(notdir $@)", o_automatic, 1);
944 define_variable_cname ("%F", "$(notdir $%)", o_automatic, 1);
945 define_variable_cname ("*F", "$(notdir $*)", o_automatic, 1);
946 define_variable_cname ("<F", "$(notdir $<)", o_automatic, 1);
947 define_variable_cname ("?F", "$(notdir $?)", o_automatic, 1);
948 define_variable_cname ("^F", "$(notdir $^)", o_automatic, 1);
949 define_variable_cname ("+F", "$(notdir $+)", o_automatic, 1);
952 int export_all_variables;
954 /* Create a new environment for FILE's commands.
955 If FILE is nil, this is for the 'shell' function.
956 The child's MAKELEVEL variable is incremented. */
958 char **
959 target_environment (struct file *file)
961 struct variable_set_list *set_list;
962 register struct variable_set_list *s;
963 struct hash_table table;
964 struct variable **v_slot;
965 struct variable **v_end;
966 struct variable makelevel_key;
967 char **result_0;
968 char **result;
970 if (file == 0)
971 set_list = current_variable_set_list;
972 else
973 set_list = file->variables;
975 hash_init (&table, VARIABLE_BUCKETS,
976 variable_hash_1, variable_hash_2, variable_hash_cmp);
978 /* Run through all the variable sets in the list,
979 accumulating variables in TABLE. */
980 for (s = set_list; s != 0; s = s->next)
982 struct variable_set *set = s->set;
983 v_slot = (struct variable **) set->table.ht_vec;
984 v_end = v_slot + set->table.ht_size;
985 for ( ; v_slot < v_end; v_slot++)
986 if (! HASH_VACANT (*v_slot))
988 struct variable **new_slot;
989 struct variable *v = *v_slot;
991 /* If this is a per-target variable and it hasn't been touched
992 already then look up the global version and take its export
993 value. */
994 if (v->per_target && v->export == v_default)
996 struct variable *gv;
998 gv = lookup_variable_in_set (v->name, strlen(v->name),
999 &global_variable_set);
1000 if (gv)
1001 v->export = gv->export;
1004 switch (v->export)
1006 case v_default:
1007 if (v->origin == o_default || v->origin == o_automatic)
1008 /* Only export default variables by explicit request. */
1009 continue;
1011 /* The variable doesn't have a name that can be exported. */
1012 if (! v->exportable)
1013 continue;
1015 if (! export_all_variables
1016 && v->origin != o_command
1017 && v->origin != o_env && v->origin != o_env_override)
1018 continue;
1019 break;
1021 case v_export:
1022 break;
1024 case v_noexport:
1026 /* If this is the SHELL variable and it's not exported,
1027 then add the value from our original environment, if
1028 the original environment defined a value for SHELL. */
1029 extern struct variable shell_var;
1030 if (streq (v->name, "SHELL") && shell_var.value)
1032 v = &shell_var;
1033 break;
1035 continue;
1038 case v_ifset:
1039 if (v->origin == o_default)
1040 continue;
1041 break;
1044 new_slot = (struct variable **) hash_find_slot (&table, v);
1045 if (HASH_VACANT (*new_slot))
1046 hash_insert_at (&table, v, new_slot);
1050 makelevel_key.name = MAKELEVEL_NAME;
1051 makelevel_key.length = MAKELEVEL_LENGTH;
1052 hash_delete (&table, &makelevel_key);
1054 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
1056 v_slot = (struct variable **) table.ht_vec;
1057 v_end = v_slot + table.ht_size;
1058 for ( ; v_slot < v_end; v_slot++)
1059 if (! HASH_VACANT (*v_slot))
1061 struct variable *v = *v_slot;
1063 /* If V is recursively expanded and didn't come from the environment,
1064 expand its value. If it came from the environment, it should
1065 go back into the environment unchanged. */
1066 if (v->recursive
1067 && v->origin != o_env && v->origin != o_env_override)
1069 char *value = recursively_expand_for_file (v, file);
1070 #ifdef WINDOWS32
1071 if (strcmp(v->name, "Path") == 0 ||
1072 strcmp(v->name, "PATH") == 0)
1073 convert_Path_to_windows32(value, ';');
1074 #endif
1075 *result++ = xstrdup (concat (3, v->name, "=", value));
1076 free (value);
1078 else
1080 #ifdef WINDOWS32
1081 if (strcmp(v->name, "Path") == 0 ||
1082 strcmp(v->name, "PATH") == 0)
1083 convert_Path_to_windows32(v->value, ';');
1084 #endif
1085 *result++ = xstrdup (concat (3, v->name, "=", v->value));
1089 *result = xmalloc (100);
1090 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
1091 *++result = 0;
1093 hash_free (&table, 0);
1095 return result_0;
1098 static struct variable *
1099 set_special_var (struct variable *var)
1101 if (streq (var->name, RECIPEPREFIX_NAME))
1103 /* The user is resetting the command introduction prefix. This has to
1104 happen immediately, so that subsequent rules are interpreted
1105 properly. */
1106 cmd_prefix = var->value[0]=='\0' ? RECIPEPREFIX_DEFAULT : var->value[0];
1109 return var;
1112 /* Given a string, shell-execute it and return a malloc'ed string of the
1113 * result. This removes only ONE newline (if any) at the end, for maximum
1114 * compatibility with the *BSD makes. If it fails, returns NULL. */
1116 char *
1117 shell_result (const char *p)
1119 char *buf;
1120 unsigned int len;
1121 char *args[2];
1122 char *result;
1124 install_variable_buffer (&buf, &len);
1126 args[0] = (char *) p;
1127 args[1] = NULL;
1128 variable_buffer_output (func_shell_base (variable_buffer, args, 0), "\0", 1);
1129 result = strdup (variable_buffer);
1131 restore_variable_buffer (buf, len);
1132 return result;
1135 /* Given a variable, a value, and a flavor, define the variable.
1136 See the try_variable_definition() function for details on the parameters. */
1138 struct variable *
1139 do_variable_definition (const struct floc *flocp, const char *varname,
1140 const char *value, enum variable_origin origin,
1141 enum variable_flavor flavor, int target_var)
1143 const char *p;
1144 char *alloc_value = NULL;
1145 struct variable *v;
1146 int append = 0;
1147 int conditional = 0;
1149 /* Calculate the variable's new value in VALUE. */
1151 switch (flavor)
1153 default:
1154 case f_bogus:
1155 /* Should not be possible. */
1156 abort ();
1157 case f_simple:
1158 /* A simple variable definition "var := value". Expand the value.
1159 We have to allocate memory since otherwise it'll clobber the
1160 variable buffer, and we may still need that if we're looking at a
1161 target-specific variable. */
1162 p = alloc_value = allocated_variable_expand (value);
1163 break;
1164 case f_shell:
1166 /* A shell definition "var != value". Expand value, pass it to
1167 the shell, and store the result in recursively-expanded var. */
1168 char *q = allocated_variable_expand (value);
1169 p = alloc_value = shell_result (q);
1170 free (q);
1171 flavor = f_recursive;
1172 break;
1174 case f_conditional:
1175 /* A conditional variable definition "var ?= value".
1176 The value is set IFF the variable is not defined yet. */
1177 v = lookup_variable (varname, strlen (varname));
1178 if (v)
1179 return v->special ? set_special_var (v) : v;
1181 conditional = 1;
1182 flavor = f_recursive;
1183 /* FALLTHROUGH */
1184 case f_recursive:
1185 /* A recursive variable definition "var = value".
1186 The value is used verbatim. */
1187 p = value;
1188 break;
1189 case f_append:
1191 /* If we have += but we're in a target variable context, we want to
1192 append only with other variables in the context of this target. */
1193 if (target_var)
1195 append = 1;
1196 v = lookup_variable_in_set (varname, strlen (varname),
1197 current_variable_set_list->set);
1199 /* Don't append from the global set if a previous non-appending
1200 target-specific variable definition exists. */
1201 if (v && !v->append)
1202 append = 0;
1204 else
1205 v = lookup_variable (varname, strlen (varname));
1207 if (v == 0)
1209 /* There was no old value.
1210 This becomes a normal recursive definition. */
1211 p = value;
1212 flavor = f_recursive;
1214 else
1216 /* Paste the old and new values together in VALUE. */
1218 unsigned int oldlen, vallen;
1219 const char *val;
1220 char *tp = NULL;
1222 val = value;
1223 if (v->recursive)
1224 /* The previous definition of the variable was recursive.
1225 The new value is the unexpanded old and new values. */
1226 flavor = f_recursive;
1227 else
1228 /* The previous definition of the variable was simple.
1229 The new value comes from the old value, which was expanded
1230 when it was set; and from the expanded new value. Allocate
1231 memory for the expansion as we may still need the rest of the
1232 buffer if we're looking at a target-specific variable. */
1233 val = tp = allocated_variable_expand (val);
1235 oldlen = strlen (v->value);
1236 vallen = strlen (val);
1237 p = alloc_value = xmalloc (oldlen + 1 + vallen + 1);
1238 memcpy (alloc_value, v->value, oldlen);
1239 alloc_value[oldlen] = ' ';
1240 memcpy (&alloc_value[oldlen + 1], val, vallen + 1);
1242 if (tp)
1243 free (tp);
1248 #ifdef __MSDOS__
1249 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1250 non-Unix systems don't conform to this default configuration (in
1251 fact, most of them don't even have '/bin'). On the other hand,
1252 $SHELL in the environment, if set, points to the real pathname of
1253 the shell.
1254 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1255 the Makefile override $SHELL from the environment. But first, we
1256 look for the basename of the shell in the directory where SHELL=
1257 points, and along the $PATH; if it is found in any of these places,
1258 we define $SHELL to be the actual pathname of the shell. Thus, if
1259 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1260 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1261 defining SHELL to be "d:/unix/bash.exe". */
1262 if ((origin == o_file || origin == o_override)
1263 && strcmp (varname, "SHELL") == 0)
1265 PATH_VAR (shellpath);
1266 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1268 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1269 if (__dosexec_find_on_path (p, NULL, shellpath))
1271 char *tp;
1273 for (tp = shellpath; *tp; tp++)
1274 if (*tp == '\\')
1275 *tp = '/';
1277 v = define_variable_loc (varname, strlen (varname),
1278 shellpath, origin, flavor == f_recursive,
1279 flocp);
1281 else
1283 const char *shellbase, *bslash;
1284 struct variable *pathv = lookup_variable ("PATH", 4);
1285 char *path_string;
1286 char *fake_env[2];
1287 size_t pathlen = 0;
1289 shellbase = strrchr (p, '/');
1290 bslash = strrchr (p, '\\');
1291 if (!shellbase || bslash > shellbase)
1292 shellbase = bslash;
1293 if (!shellbase && p[1] == ':')
1294 shellbase = p + 1;
1295 if (shellbase)
1296 shellbase++;
1297 else
1298 shellbase = p;
1300 /* Search for the basename of the shell (with standard
1301 executable extensions) along the $PATH. */
1302 if (pathv)
1303 pathlen = strlen (pathv->value);
1304 path_string = xmalloc (5 + pathlen + 2 + 1);
1305 /* On MSDOS, current directory is considered as part of $PATH. */
1306 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1307 fake_env[0] = path_string;
1308 fake_env[1] = 0;
1309 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1311 char *tp;
1313 for (tp = shellpath; *tp; tp++)
1314 if (*tp == '\\')
1315 *tp = '/';
1317 v = define_variable_loc (varname, strlen (varname),
1318 shellpath, origin,
1319 flavor == f_recursive, flocp);
1321 else
1322 v = lookup_variable (varname, strlen (varname));
1324 free (path_string);
1327 else
1328 #endif /* __MSDOS__ */
1329 #ifdef WINDOWS32
1330 if ((origin == o_file || origin == o_override || origin == o_command)
1331 && streq (varname, "SHELL"))
1333 extern char *default_shell;
1335 /* Call shell locator function. If it returns TRUE, then
1336 set no_default_sh_exe to indicate sh was found and
1337 set new value for SHELL variable. */
1339 if (find_and_set_default_shell (p))
1341 v = define_variable_in_set (varname, strlen (varname), default_shell,
1342 origin, flavor == f_recursive,
1343 (target_var
1344 ? current_variable_set_list->set
1345 : NULL),
1346 flocp);
1347 no_default_sh_exe = 0;
1349 else
1351 char *tp = alloc_value;
1353 alloc_value = allocated_variable_expand (p);
1355 if (find_and_set_default_shell (alloc_value))
1357 v = define_variable_in_set (varname, strlen (varname), p,
1358 origin, flavor == f_recursive,
1359 (target_var
1360 ? current_variable_set_list->set
1361 : NULL),
1362 flocp);
1363 no_default_sh_exe = 0;
1365 else
1366 v = lookup_variable (varname, strlen (varname));
1368 if (tp)
1369 free (tp);
1372 else
1373 #endif
1375 /* If we are defining variables inside an $(eval ...), we might have a
1376 different variable context pushed, not the global context (maybe we're
1377 inside a $(call ...) or something. Since this function is only ever
1378 invoked in places where we want to define globally visible variables,
1379 make sure we define this variable in the global set. */
1381 v = define_variable_in_set (varname, strlen (varname), p,
1382 origin, flavor == f_recursive,
1383 (target_var
1384 ? current_variable_set_list->set : NULL),
1385 flocp);
1386 v->append = append;
1387 v->conditional = conditional;
1389 if (alloc_value)
1390 free (alloc_value);
1392 return v->special ? set_special_var (v) : v;
1395 /* Parse P (a null-terminated string) as a variable definition.
1397 If it is not a variable definition, return NULL and the contents of *VAR
1398 are undefined, except NAME is set to the first non-space character or NIL.
1400 If it is a variable definition, return a pointer to the char after the
1401 assignment token and set the following fields (only) of *VAR:
1402 name : name of the variable (ALWAYS SET) (NOT NUL-TERMINATED!)
1403 length : length of the variable name
1404 value : value of the variable (nul-terminated)
1405 flavor : flavor of the variable
1406 Other values in *VAR are unchanged.
1409 char *
1410 parse_variable_definition (const char *p, struct variable *var)
1412 int wspace = 0;
1413 const char *e = NULL;
1415 p = next_token (p);
1416 var->name = (char *)p;
1417 var->length = 0;
1419 while (1)
1421 int c = *p++;
1423 /* If we find a comment or EOS, it's not a variable definition. */
1424 if (c == '\0' || c == '#')
1425 return NULL;
1427 if (c == '$')
1429 /* This begins a variable expansion reference. Make sure we don't
1430 treat chars inside the reference as assignment tokens. */
1431 char closeparen;
1432 int count;
1433 c = *p++;
1434 if (c == '(')
1435 closeparen = ')';
1436 else if (c == '{')
1437 closeparen = '}';
1438 else
1439 /* '$$' or '$X'. Either way, nothing special to do here. */
1440 continue;
1442 /* P now points past the opening paren or brace.
1443 Count parens or braces until it is matched. */
1444 count = 0;
1445 for (; *p != '\0'; ++p)
1447 if (*p == c)
1448 ++count;
1449 else if (*p == closeparen && --count < 0)
1451 ++p;
1452 break;
1455 continue;
1458 /* If we find whitespace skip it, and remember we found it. */
1459 if (isblank ((unsigned char)c))
1461 wspace = 1;
1462 e = p - 1;
1463 p = next_token (p);
1464 c = *p;
1465 if (c == '\0')
1466 return NULL;
1467 ++p;
1471 if (c == '=')
1473 var->flavor = f_recursive;
1474 if (! e)
1475 e = p - 1;
1476 break;
1479 /* Match assignment variants (:=, +=, ?=, !=) */
1480 if (*p == '=')
1482 switch (c)
1484 case ':':
1485 var->flavor = f_simple;
1486 break;
1487 case '+':
1488 var->flavor = f_append;
1489 break;
1490 case '?':
1491 var->flavor = f_conditional;
1492 break;
1493 case '!':
1494 var->flavor = f_shell;
1495 break;
1496 default:
1497 /* If we skipped whitespace, non-assignments means no var. */
1498 if (wspace)
1499 return NULL;
1501 /* Might be assignment, or might be $= or #=. Check. */
1502 continue;
1504 if (! e)
1505 e = p - 1;
1506 ++p;
1507 break;
1510 /* Check for POSIX ::= syntax */
1511 if (c == ':')
1513 /* A colon other than :=/::= is not a variable defn. */
1514 if (*p != ':' || p[1] != '=')
1515 return NULL;
1517 /* POSIX allows ::= to be the same as GNU make's := */
1518 var->flavor = f_simple;
1519 if (! e)
1520 e = p - 1;
1521 p += 2;
1522 break;
1525 /* If we skipped whitespace, non-assignments means no var. */
1526 if (wspace)
1527 return NULL;
1530 var->length = e - var->name;
1531 var->value = next_token (p);
1532 return (char *)p;
1535 /* Try to interpret LINE (a null-terminated string) as a variable definition.
1537 If LINE was recognized as a variable definition, a pointer to its 'struct
1538 variable' is returned. If LINE is not a variable definition, NULL is
1539 returned. */
1541 struct variable *
1542 assign_variable_definition (struct variable *v, char *line)
1544 char *name;
1546 if (!parse_variable_definition (line, v))
1547 return NULL;
1549 /* Expand the name, so "$(foo)bar = baz" works. */
1550 name = alloca (v->length + 1);
1551 memcpy (name, v->name, v->length);
1552 name[v->length] = '\0';
1553 v->name = allocated_variable_expand (name);
1555 if (v->name[0] == '\0')
1556 fatal (&v->fileinfo, _("empty variable name"));
1558 return v;
1561 /* Try to interpret LINE (a null-terminated string) as a variable definition.
1563 ORIGIN may be o_file, o_override, o_env, o_env_override,
1564 or o_command specifying that the variable definition comes
1565 from a makefile, an override directive, the environment with
1566 or without the -e switch, or the command line.
1568 See the comments for assign_variable_definition().
1570 If LINE was recognized as a variable definition, a pointer to its 'struct
1571 variable' is returned. If LINE is not a variable definition, NULL is
1572 returned. */
1574 struct variable *
1575 try_variable_definition (const struct floc *flocp, char *line,
1576 enum variable_origin origin, int target_var)
1578 struct variable v;
1579 struct variable *vp;
1581 if (flocp != 0)
1582 v.fileinfo = *flocp;
1583 else
1584 v.fileinfo.filenm = 0;
1586 if (!assign_variable_definition (&v, line))
1587 return 0;
1589 vp = do_variable_definition (flocp, v.name, v.value,
1590 origin, v.flavor, target_var);
1592 free (v.name);
1594 return vp;
1597 /* Print information for variable V, prefixing it with PREFIX. */
1599 static void
1600 print_variable (const void *item, void *arg)
1602 const struct variable *v = item;
1603 const char *prefix = arg;
1604 const char *origin;
1606 switch (v->origin)
1608 case o_automatic:
1609 origin = _("automatic");
1610 break;
1611 case o_default:
1612 origin = _("default");
1613 break;
1614 case o_env:
1615 origin = _("environment");
1616 break;
1617 case o_file:
1618 origin = _("makefile");
1619 break;
1620 case o_env_override:
1621 origin = _("environment under -e");
1622 break;
1623 case o_command:
1624 origin = _("command line");
1625 break;
1626 case o_override:
1627 origin = _("'override' directive");
1628 break;
1629 case o_invalid:
1630 default:
1631 abort ();
1633 fputs ("# ", stdout);
1634 fputs (origin, stdout);
1635 if (v->private_var)
1636 fputs (" private", stdout);
1637 if (v->fileinfo.filenm)
1638 printf (_(" (from '%s', line %lu)"),
1639 v->fileinfo.filenm, v->fileinfo.lineno);
1640 putchar ('\n');
1641 fputs (prefix, stdout);
1643 /* Is this a 'define'? */
1644 if (v->recursive && strchr (v->value, '\n') != 0)
1645 printf ("define %s\n%s\nendef\n", v->name, v->value);
1646 else
1648 char *p;
1650 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1652 /* Check if the value is just whitespace. */
1653 p = next_token (v->value);
1654 if (p != v->value && *p == '\0')
1655 /* All whitespace. */
1656 printf ("$(subst ,,%s)", v->value);
1657 else if (v->recursive)
1658 fputs (v->value, stdout);
1659 else
1660 /* Double up dollar signs. */
1661 for (p = v->value; *p != '\0'; ++p)
1663 if (*p == '$')
1664 putchar ('$');
1665 putchar (*p);
1667 putchar ('\n');
1672 static void
1673 print_auto_variable (const void *item, void *arg)
1675 const struct variable *v = item;
1677 if (v->origin == o_automatic)
1678 print_variable (item, arg);
1682 static void
1683 print_noauto_variable (const void *item, void *arg)
1685 const struct variable *v = item;
1687 if (v->origin != o_automatic)
1688 print_variable (item, arg);
1692 /* Print all the variables in SET. PREFIX is printed before
1693 the actual variable definitions (everything else is comments). */
1695 void
1696 print_variable_set (struct variable_set *set, char *prefix, int pauto)
1698 hash_map_arg (&set->table, (pauto ? print_auto_variable : print_variable),
1699 prefix);
1701 fputs (_("# variable set hash-table stats:\n"), stdout);
1702 fputs ("# ", stdout);
1703 hash_print_stats (&set->table, stdout);
1704 putc ('\n', stdout);
1707 /* Print the data base of variables. */
1709 void
1710 print_variable_data_base (void)
1712 puts (_("\n# Variables\n"));
1714 print_variable_set (&global_variable_set, "", 0);
1716 puts (_("\n# Pattern-specific Variable Values"));
1719 struct pattern_var *p;
1720 int rules = 0;
1722 for (p = pattern_vars; p != 0; p = p->next)
1724 ++rules;
1725 printf ("\n%s :\n", p->target);
1726 print_variable (&p->variable, "# ");
1729 if (rules == 0)
1730 puts (_("\n# No pattern-specific variable values."));
1731 else
1732 printf (_("\n# %u pattern-specific variable values"), rules);
1737 /* Print all the local variables of FILE. */
1739 void
1740 print_file_variables (const struct file *file)
1742 if (file->variables != 0)
1743 print_variable_set (file->variables->set, "# ", 1);
1746 void
1747 print_target_variables (const struct file *file)
1749 if (file->variables != 0)
1751 int l = strlen (file->name);
1752 char *t = alloca (l + 3);
1754 strcpy (t, file->name);
1755 t[l] = ':';
1756 t[l+1] = ' ';
1757 t[l+2] = '\0';
1759 hash_map_arg (&file->variables->set->table, print_noauto_variable, t);
1763 #ifdef WINDOWS32
1764 void
1765 sync_Path_environment (void)
1767 char *path = allocated_variable_expand ("$(PATH)");
1768 static char *environ_path = NULL;
1770 if (!path)
1771 return;
1774 * If done this before, don't leak memory unnecessarily.
1775 * Free the previous entry before allocating new one.
1777 if (environ_path)
1778 free (environ_path);
1781 * Create something WINDOWS32 world can grok
1783 convert_Path_to_windows32 (path, ';');
1784 environ_path = xstrdup (concat (3, "PATH", "=", path));
1785 putenv (environ_path);
1786 free (path);
1788 #endif