*** empty log message ***
[make.git] / variable.c
blob59811a1c61703caee6dbb67c4c8fbb8440ce9fd7
1 /* Internals of variables for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 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
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #include "make.h"
20 #include "commands.h"
21 #include "variable.h"
22 #include "dep.h"
23 #include "file.h"
25 /* Hash table of all global variable definitions. */
27 #ifndef VARIABLE_BUCKETS
28 #define VARIABLE_BUCKETS 523
29 #endif
30 #ifndef PERFILE_VARIABLE_BUCKETS
31 #define PERFILE_VARIABLE_BUCKETS 23
32 #endif
33 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
34 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
35 #endif
36 static struct variable *variable_table[VARIABLE_BUCKETS];
37 static struct variable_set global_variable_set
38 = { variable_table, VARIABLE_BUCKETS };
39 static struct variable_set_list global_setlist
40 = { 0, &global_variable_set };
41 struct variable_set_list *current_variable_set_list = &global_setlist;
43 /* Implement variables. */
45 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
46 LENGTH is the length of NAME, which does not need to be null-terminated.
47 ORIGIN specifies the origin of the variable (makefile, command line
48 or environment).
49 If RECURSIVE is nonzero a flag is set in the variable saying
50 that it should be recursively re-expanded. */
52 static struct variable *
53 define_variable_in_set (name, length, value, origin, recursive, set)
54 char *name;
55 unsigned int length;
56 char *value;
57 enum variable_origin origin;
58 int recursive;
59 struct variable_set *set;
61 register unsigned int i;
62 register unsigned int hashval;
63 register struct variable *v;
65 hashval = 0;
66 for (i = 0; i < length; ++i)
67 HASH (hashval, name[i]);
68 hashval %= set->buckets;
70 for (v = set->table[hashval]; v != 0; v = v->next)
71 if (*v->name == *name
72 && !strncmp (v->name + 1, name + 1, length - 1)
73 && v->name[length] == '\0')
74 break;
76 if (env_overrides && origin == o_env)
77 origin = o_env_override;
79 if (v != 0)
81 if (env_overrides && v->origin == o_env)
82 /* V came from in the environment. Since it was defined
83 before the switches were parsed, it wasn't affected by -e. */
84 v->origin = o_env_override;
86 /* A variable of this name is already defined.
87 If the old definition is from a stronger source
88 than this one, don't redefine it. */
89 if ((int) origin >= (int) v->origin)
91 if (v->value != 0)
92 free (v->value);
93 v->value = savestring (value, strlen (value));
94 v->origin = origin;
95 v->recursive = recursive;
97 return v;
100 /* Create a new variable definition and add it to the hash table. */
102 v = (struct variable *) xmalloc (sizeof (struct variable));
103 v->name = savestring (name, length);
104 v->value = savestring (value, strlen (value));
105 v->origin = origin;
106 v->recursive = recursive;
107 v->expanding = 0;
108 v->export = v_default;
109 v->next = set->table[hashval];
110 set->table[hashval] = v;
111 return v;
114 /* Define a variable in the current variable set. */
116 struct variable *
117 define_variable (name, length, value, origin, recursive)
118 char *name;
119 unsigned int length;
120 char *value;
121 enum variable_origin origin;
122 int recursive;
124 return define_variable_in_set (name, length, value, origin, recursive,
125 current_variable_set_list->set);
128 /* Define a variable in FILE's variable set. */
130 struct variable *
131 define_variable_for_file (name, length, value, origin, recursive, file)
132 char *name;
133 unsigned int length;
134 char *value;
135 enum variable_origin origin;
136 int recursive;
137 struct file *file;
139 return define_variable_in_set (name, length, value, origin, recursive,
140 file->variables->set);
143 /* Lookup a variable whose name is a string starting at NAME
144 and with LENGTH chars. NAME need not be null-terminated.
145 Returns address of the `struct variable' containing all info
146 on the variable, or nil if no such variable is defined. */
148 struct variable *
149 lookup_variable (name, length)
150 char *name;
151 unsigned int length;
153 register struct variable_set_list *setlist;
155 register unsigned int i;
156 register unsigned int rawhash = 0;
158 for (i = 0; i < length; ++i)
159 HASH (rawhash, name[i]);
161 for (setlist = current_variable_set_list;
162 setlist != 0; setlist = setlist->next)
164 register struct variable_set *set = setlist->set;
165 register unsigned int hashval = rawhash % set->buckets;
166 register struct variable *v;
168 for (v = set->table[hashval]; v != 0; v = v->next)
169 if (*v->name == *name
170 && !strncmp (v->name + 1, name + 1, length - 1)
171 && v->name[length] == 0)
172 return v;
175 return 0;
178 /* Initialize FILE's variable set list. If FILE already has a variable set
179 list, the topmost variable set is left intact, but the the rest of the
180 chain is replaced with FILE->parent's setlist. */
182 void
183 initialize_file_variables (file)
184 struct file *file;
186 register struct variable_set_list *l = file->variables;
187 if (l == 0)
189 l = (struct variable_set_list *)
190 xmalloc (sizeof (struct variable_set_list));
191 l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
192 l->set->buckets = PERFILE_VARIABLE_BUCKETS;
193 l->set->table = (struct variable **)
194 xmalloc (l->set->buckets * sizeof (struct variable *));
195 bzero ((char *) l->set->table,
196 l->set->buckets * sizeof (struct variable *));
197 file->variables = l;
200 if (file->parent == 0)
201 l->next = &global_setlist;
202 else
204 if (file->parent->variables == 0)
205 initialize_file_variables (file->parent);
206 l->next = file->parent->variables;
210 /* Pop the top set off the current variable set list,
211 and free all its storage. */
213 void
214 pop_variable_scope ()
216 register struct variable_set_list *setlist = current_variable_set_list;
217 register struct variable_set *set = setlist->set;
218 register unsigned int i;
220 current_variable_set_list = setlist->next;
221 free ((char *) setlist);
223 for (i = 0; i < set->buckets; ++i)
225 register struct variable *next = set->table[i];
226 while (next != 0)
228 register struct variable *v = next;
229 next = v->next;
231 free (v->name);
232 free ((char *) v);
235 free ((char *) set->table);
236 free ((char *) set);
239 /* Create a new variable set and push it on the current setlist. */
241 void
242 push_new_variable_scope ()
244 register struct variable_set_list *setlist;
245 register struct variable_set *set;
247 set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
248 set->buckets = SMALL_SCOPE_VARIABLE_BUCKETS;
249 set->table = (struct variable **)
250 xmalloc (set->buckets * sizeof (struct variable *));
251 bzero ((char *) set->table, set->buckets * sizeof (struct variable *));
253 setlist = (struct variable_set_list *)
254 xmalloc (sizeof (struct variable_set_list));
255 setlist->set = set;
256 setlist->next = current_variable_set_list;
257 current_variable_set_list = setlist;
260 /* Merge SET1 into SET0, freeing unused storage in SET1. */
262 static void
263 merge_variable_sets (set0, set1)
264 struct variable_set *set0, *set1;
266 register unsigned int bucket1;
268 for (bucket1 = 0; bucket1 < set1->buckets; ++bucket1)
270 register struct variable *v1 = set1->table[bucket1];
271 while (v1 != 0)
273 struct variable *next = v1->next;
274 unsigned int bucket0;
275 register struct variable *v0;
277 if (set1->buckets >= set0->buckets)
278 bucket0 = bucket1;
279 else
281 register char *n;
282 bucket0 = 0;
283 for (n = v1->name; *n != '\0'; ++n)
284 HASH (bucket0, *n);
286 bucket0 %= set0->buckets;
288 for (v0 = set0->table[bucket0]; v0 != 0; v0 = v0->next)
289 if (streq (v0->name, v1->name))
290 break;
292 if (v0 == 0)
294 /* There is no variable in SET0 with the same name. */
295 v1->next = set0->table[bucket0];
296 set0->table[bucket0] = v1;
298 else
300 /* The same variable exists in both sets.
301 SET0 takes precedence. */
302 free (v1->value);
303 free ((char *) v1);
306 v1 = next;
311 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
313 void
314 merge_variable_set_lists (setlist0, setlist1)
315 struct variable_set_list **setlist0, *setlist1;
317 register struct variable_set_list *list0 = *setlist0;
318 struct variable_set_list *last0 = 0;
320 while (setlist1 != 0 && list0 != 0)
322 struct variable_set_list *next = setlist1;
323 setlist1 = setlist1->next;
325 merge_variable_sets (list0->set, next->set);
327 free ((char *) next);
329 last0 = list0;
330 list0 = list0->next;
333 if (setlist1 != 0)
335 if (last0 == 0)
336 *setlist0 = setlist1;
337 else
338 last0->next = setlist1;
342 /* Define the automatic variables, and record the addresses
343 of their structures so we can change their values quickly. */
345 void
346 define_automatic_variables ()
348 extern char default_shell[];
349 register struct variable *v;
350 char buf[100];
352 sprintf (buf, "%u", makelevel);
353 (void) define_variable ("MAKELEVEL", 9, buf, o_env, 0);
355 /* This won't override any definition, but it
356 will provide one if there isn't one there. */
357 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
358 v->export = v_export; /* Always export SHELL. */
360 /* Don't let SHELL come from the environment. */
361 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
363 free (v->value);
364 v->origin = o_file;
365 v->value = savestring (default_shell, strlen (default_shell));
368 /* Make sure MAKEFILES gets exported if it is set. */
369 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
370 v->export = v_ifset;
372 /* Define the magic D and F variables in terms of
373 the automatic variables they are variations of. */
375 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
376 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
377 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
378 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
379 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
380 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
381 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
382 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
383 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
384 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
385 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
386 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
389 int export_all_variables;
391 /* Create a new environment for FILE's commands.
392 If FILE is nil, this is for the `shell' function.
393 The child's MAKELEVEL variable is incremented. */
395 char **
396 target_environment (file)
397 struct file *file;
399 struct variable_set_list *set_list;
400 register struct variable_set_list *s;
401 struct variable_bucket
403 struct variable_bucket *next;
404 struct variable *variable;
406 struct variable_bucket **table;
407 unsigned int buckets;
408 register unsigned int i;
409 register unsigned nvariables;
410 char **result;
411 unsigned int mklev_hash;
413 if (file == 0)
414 set_list = current_variable_set_list;
415 else
416 set_list = file->variables;
418 /* Find the lowest number of buckets in any set in the list. */
419 s = set_list;
420 buckets = s->set->buckets;
421 for (s = s->next; s != 0; s = s->next)
422 if (s->set->buckets < buckets)
423 buckets = s->set->buckets;
425 /* Find the hash value of the bucket `MAKELEVEL' will fall into. */
427 char *p = "MAKELEVEL";
428 mklev_hash = 0;
429 while (*p != '\0')
430 HASH (mklev_hash, *p++);
433 /* Temporarily allocate a table with that many buckets. */
434 table = (struct variable_bucket **)
435 alloca (buckets * sizeof (struct variable_bucket *));
436 bzero ((char *) table, buckets * sizeof (struct variable_bucket *));
438 /* Run through all the variable sets in the list,
439 accumulating variables in TABLE. */
440 nvariables = 0;
441 for (s = set_list; s != 0; s = s->next)
443 register struct variable_set *set = s->set;
444 for (i = 0; i < set->buckets; ++i)
446 register struct variable *v;
447 for (v = set->table[i]; v != 0; v = v->next)
449 unsigned int j = i % buckets;
450 register struct variable_bucket *ov;
451 register char *p = v->name;
453 if (i == mklev_hash % set->buckets
454 && streq (v->name, "MAKELEVEL"))
455 /* Don't include MAKELEVEL because it will be
456 added specially at the end. */
457 continue;
459 switch (v->export)
461 case v_default:
462 if (v->origin == o_default || v->origin == o_automatic)
463 /* Only export default variables by explicit request. */
464 continue;
466 if (! export_all_variables
467 && v->origin != o_command
468 && v->origin != o_env && v->origin != o_env_override)
469 continue;
471 if (*p != '_' && (*p < 'A' || *p > 'Z')
472 && (*p < 'a' || *p > 'z'))
473 continue;
474 for (++p; *p != '\0'; ++p)
475 if (*p != '_' && (*p < 'a' || *p > 'z')
476 && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9'))
477 break;
478 if (*p != '\0')
479 continue;
481 case v_export:
482 break;
484 case v_noexport:
485 continue;
487 case v_ifset:
488 if (v->origin == o_default)
489 continue;
490 break;
493 for (ov = table[j]; ov != 0; ov = ov->next)
494 if (streq (v->name, ov->variable->name))
495 break;
496 if (ov == 0)
498 register struct variable_bucket *entry;
499 entry = (struct variable_bucket *)
500 alloca (sizeof (struct variable_bucket));
501 entry->next = table[j];
502 entry->variable = v;
503 table[j] = entry;
504 ++nvariables;
510 result = (char **) xmalloc ((nvariables + 2) * sizeof (char *));
511 nvariables = 0;
512 for (i = 0; i < buckets; ++i)
514 register struct variable_bucket *b;
515 for (b = table[i]; b != 0; b = b->next)
517 register struct variable *v = b->variable;
518 /* If V is recursively expanded and didn't come from the environment,
519 expand its value. If it came from the environment, it should
520 go back into the environment unchanged. */
521 if (v->recursive
522 && v->origin != o_env && v->origin != o_env_override)
524 char *value = recursively_expand (v);
525 result[nvariables++] = concat (v->name, "=", value);
526 free (value);
528 else
529 result[nvariables++] = concat (v->name, "=", v->value);
532 result[nvariables] = (char *) xmalloc (100);
533 (void) sprintf (result[nvariables], "MAKELEVEL=%u", makelevel + 1);
534 result[++nvariables] = 0;
536 return result;
539 /* Try to interpret LINE (a null-terminated string) as a variable definition.
541 ORIGIN may be o_file, o_override, o_env, o_env_override,
542 or o_command specifying that the variable definition comes
543 from a makefile, an override directive, the environment with
544 or without the -e switch, or the command line.
546 A variable definition has the form "name = value" or "name := value".
547 Any whitespace around the "=" or ":=" is removed. The first form
548 defines a variable that is recursively re-evaluated. The second form
549 defines a variable whose value is variable-expanded at the time of
550 definition and then is evaluated only once at the time of expansion.
552 If a variable was defined, a pointer to its `struct variable' is returned.
553 If not, NULL is returned. */
555 struct variable *
556 try_variable_definition (filename, lineno, line, origin)
557 char *filename;
558 unsigned int lineno;
559 char *line;
560 enum variable_origin origin;
562 register int c;
563 register char *p = line;
564 register char *beg;
565 register char *end;
566 enum { bogus, simple, recursive, append } flavor = bogus;
567 char *name, *expanded_name, *value;
568 struct variable *v;
570 while (1)
572 c = *p++;
573 if (c == '\0' || c == '#')
574 return 0;
575 if (c == '=')
577 end = p - 1;
578 flavor = recursive;
579 break;
581 else if (c == ':')
582 if (*p == '=')
584 end = p++ - 1;
585 flavor = simple;
586 break;
588 else
589 /* A colon other than := is a rule line, not a variable defn. */
590 return 0;
591 else if (c == '+' && *p == '=')
593 end = p++ - 1;
594 flavor = append;
595 break;
599 while (isblank (end[-1]))
600 --end;
601 beg = next_token (line);
602 p = next_token (p);
604 /* Expand the name, so "$(foo)bar = baz" works. */
605 name = (char *) alloca (end - beg + 1);
606 bcopy (beg, name, end - beg);
607 name[end - beg] = '\0';
608 expanded_name = allocated_variable_expand (name);
610 if (expanded_name[0] == '\0')
612 if (filename == 0)
613 fatal ("empty variable name");
614 else
615 makefile_fatal (filename, lineno, "empty variable name");
618 /* Calculate the variable's new value in VALUE. */
620 switch (flavor)
622 case bogus:
623 /* Should not be possible. */
624 abort ();
625 break;
626 case simple:
627 /* A simple variable definition "var := value". Expand the value. */
628 value = variable_expand (p);
629 break;
630 case recursive:
631 /* A recursive variable definition "var = value".
632 The value is used verbatim. */
633 value = p;
634 break;
635 case append:
636 /* An appending variable definition "var += value".
637 Extract the old value and append the new one. */
638 v = lookup_variable (expanded_name, strlen (expanded_name));
639 if (v == 0)
641 /* There was no old value.
642 This becomes a normal recursive definition. */
643 value = p;
644 flavor = recursive;
646 else
648 /* Paste the old and new values together in VALUE. */
650 unsigned int oldlen, newlen;
652 if (v->recursive)
653 /* The previous definition of the variable was recursive.
654 The new value comes from the unexpanded old and new values. */
655 flavor = recursive;
656 else
657 /* The previous definition of the variable was simple.
658 The new value comes from the old value, which was expanded
659 when it was set; and from the expanded new value. */
660 p = variable_expand (p);
662 oldlen = strlen (v->value);
663 newlen = strlen (p);
664 value = (char *) alloca (oldlen + 1 + newlen + 1);
665 bcopy (v->value, value, oldlen);
666 value[oldlen] = ' ';
667 bcopy (p, &value[oldlen + 1], newlen + 1);
671 v = define_variable (expanded_name, strlen (expanded_name),
672 value, origin, flavor == recursive);
674 free (expanded_name);
676 return v;
679 /* Print information for variable V, prefixing it with PREFIX. */
681 static void
682 print_variable (v, prefix)
683 register struct variable *v;
684 char *prefix;
686 char *origin;
688 switch (v->origin)
690 case o_default:
691 origin = "default";
692 break;
693 case o_env:
694 origin = "environment";
695 break;
696 case o_file:
697 origin = "makefile";
698 break;
699 case o_env_override:
700 origin = "environment under -e";
701 break;
702 case o_command:
703 origin = "command line";
704 break;
705 case o_override:
706 origin = "`override' directive";
707 break;
708 case o_automatic:
709 origin = "automatic";
710 break;
711 case o_invalid:
712 default:
713 abort ();
714 break;
716 printf ("# %s\n", origin);
718 fputs (prefix, stdout);
720 /* Is this a `define'? */
721 if (v->recursive && index (v->value, '\n') != 0)
722 printf ("define %s\n%s\nendef\n", v->name, v->value);
723 else
725 register char *p;
727 printf ("%s %s= ", v->name, v->recursive ? "" : ":");
729 /* Check if the value is just whitespace. */
730 p = next_token (v->value);
731 if (p != v->value && *p == '\0')
732 /* All whitespace. */
733 printf ("$(subst ,,%s)", v->value);
734 else if (v->recursive)
735 fputs (v->value, stdout);
736 else
737 /* Double up dollar signs. */
738 for (p = v->value; *p != '\0'; ++p)
740 if (*p == '$')
741 putchar ('$');
742 putchar (*p);
744 putchar ('\n');
749 /* Print all the variables in SET. PREFIX is printed before
750 the actual variable definitions (everything else is comments). */
752 static void
753 print_variable_set (set, prefix)
754 register struct variable_set *set;
755 char *prefix;
757 register unsigned int i, nvariables, per_bucket;
758 register struct variable *v;
760 per_bucket = nvariables = 0;
761 for (i = 0; i < set->buckets; ++i)
763 register unsigned int this_bucket = 0;
765 for (v = set->table[i]; v != 0; v = v->next)
767 ++this_bucket;
768 print_variable (v, prefix);
771 nvariables += this_bucket;
772 if (this_bucket > per_bucket)
773 per_bucket = this_bucket;
776 if (nvariables == 0)
777 puts ("# No variables.");
778 else
780 printf ("# %u variables in %u hash buckets.\n",
781 nvariables, set->buckets);
782 #ifndef NO_FLOAT
783 printf ("# average of %.1f variables per bucket, \
784 max %u in one bucket.\n",
785 (double) nvariables / (double) set->buckets,
786 per_bucket);
787 #endif
792 /* Print the data base of variables. */
794 void
795 print_variable_data_base ()
797 puts ("\n# Variables\n");
799 print_variable_set (&global_variable_set, "");
803 /* Print all the local variables of FILE. */
805 void
806 print_file_variables (file)
807 struct file *file;
809 if (file->variables != 0)
810 print_variable_set (file->variables->set, "# ");