Sun May 12 19:19:43 1996 Aaron Digulla <digulla@fh-konstanz.de>
[make.git] / variable.c
blob12cfcc49393820780bd83921964eea7c788526ec
1 /* Internals of variables for GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94 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 "dep.h"
21 #include "filedef.h"
22 #include "job.h"
23 #include "commands.h"
24 #include "variable.h"
26 /* Hash table of all global variable definitions. */
28 #ifndef VARIABLE_BUCKETS
29 #define VARIABLE_BUCKETS 523
30 #endif
31 #ifndef PERFILE_VARIABLE_BUCKETS
32 #define PERFILE_VARIABLE_BUCKETS 23
33 #endif
34 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
35 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
36 #endif
37 static struct variable *variable_table[VARIABLE_BUCKETS];
38 static struct variable_set global_variable_set
39 = { variable_table, VARIABLE_BUCKETS };
40 static struct variable_set_list global_setlist
41 = { 0, &global_variable_set };
42 struct variable_set_list *current_variable_set_list = &global_setlist;
44 static struct variable *define_variable_in_set PARAMS ((char *name, unsigned int length,
45 char *value, enum variable_origin origin,
46 int recursive, struct variable_set *set));
49 /* Implement variables. */
51 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
52 LENGTH is the length of NAME, which does not need to be null-terminated.
53 ORIGIN specifies the origin of the variable (makefile, command line
54 or environment).
55 If RECURSIVE is nonzero a flag is set in the variable saying
56 that it should be recursively re-expanded. */
58 static struct variable *
59 define_variable_in_set (name, length, value, origin, recursive, set)
60 char *name;
61 unsigned int length;
62 char *value;
63 enum variable_origin origin;
64 int recursive;
65 struct variable_set *set;
67 register unsigned int i;
68 register unsigned int hashval;
69 register struct variable *v;
71 hashval = 0;
72 for (i = 0; i < length; ++i)
73 HASH (hashval, name[i]);
74 hashval %= set->buckets;
76 for (v = set->table[hashval]; v != 0; v = v->next)
77 if (*v->name == *name
78 && !strncmp (v->name + 1, name + 1, length - 1)
79 && v->name[length] == '\0')
80 break;
82 if (env_overrides && origin == o_env)
83 origin = o_env_override;
85 if (v != 0)
87 if (env_overrides && v->origin == o_env)
88 /* V came from in the environment. Since it was defined
89 before the switches were parsed, it wasn't affected by -e. */
90 v->origin = o_env_override;
92 /* A variable of this name is already defined.
93 If the old definition is from a stronger source
94 than this one, don't redefine it. */
95 if ((int) origin >= (int) v->origin)
97 if (v->value != 0)
98 free (v->value);
99 v->value = savestring (value, strlen (value));
100 v->origin = origin;
101 v->recursive = recursive;
103 return v;
106 /* Create a new variable definition and add it to the hash table. */
108 v = (struct variable *) xmalloc (sizeof (struct variable));
109 v->name = savestring (name, length);
110 v->value = savestring (value, strlen (value));
111 v->origin = origin;
112 v->recursive = recursive;
113 v->expanding = 0;
114 v->export = v_default;
115 v->next = set->table[hashval];
116 set->table[hashval] = v;
117 return v;
120 /* Define a variable in the current variable set. */
122 struct variable *
123 define_variable (name, length, value, origin, recursive)
124 char *name;
125 unsigned int length;
126 char *value;
127 enum variable_origin origin;
128 int recursive;
130 return define_variable_in_set (name, length, value, origin, recursive,
131 current_variable_set_list->set);
134 /* Define a variable in FILE's variable set. */
136 struct variable *
137 define_variable_for_file (name, length, value, origin, recursive, file)
138 char *name;
139 unsigned int length;
140 char *value;
141 enum variable_origin origin;
142 int recursive;
143 struct file *file;
145 return define_variable_in_set (name, length, value, origin, recursive,
146 file->variables->set);
149 /* Lookup a variable whose name is a string starting at NAME
150 and with LENGTH chars. NAME need not be null-terminated.
151 Returns address of the `struct variable' containing all info
152 on the variable, or nil if no such variable is defined. */
154 struct variable *
155 lookup_variable (name, length)
156 char *name;
157 unsigned int length;
159 register struct variable_set_list *setlist;
161 register unsigned int i;
162 register unsigned int rawhash = 0;
164 for (i = 0; i < length; ++i)
165 HASH (rawhash, name[i]);
167 for (setlist = current_variable_set_list;
168 setlist != 0; setlist = setlist->next)
170 register struct variable_set *set = setlist->set;
171 register unsigned int hashval = rawhash % set->buckets;
172 register struct variable *v;
174 for (v = set->table[hashval]; v != 0; v = v->next)
175 if (*v->name == *name
176 && !strncmp (v->name + 1, name + 1, length - 1)
177 && v->name[length] == 0)
178 return v;
181 return 0;
184 /* Initialize FILE's variable set list. If FILE already has a variable set
185 list, the topmost variable set is left intact, but the the rest of the
186 chain is replaced with FILE->parent's setlist. */
188 void
189 initialize_file_variables (file)
190 struct file *file;
192 register struct variable_set_list *l = file->variables;
193 if (l == 0)
195 l = (struct variable_set_list *)
196 xmalloc (sizeof (struct variable_set_list));
197 l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
198 l->set->buckets = PERFILE_VARIABLE_BUCKETS;
199 l->set->table = (struct variable **)
200 xmalloc (l->set->buckets * sizeof (struct variable *));
201 bzero ((char *) l->set->table,
202 l->set->buckets * sizeof (struct variable *));
203 file->variables = l;
206 if (file->parent == 0)
207 l->next = &global_setlist;
208 else
210 if (file->parent->variables == 0)
211 initialize_file_variables (file->parent);
212 l->next = file->parent->variables;
216 /* Pop the top set off the current variable set list,
217 and free all its storage. */
219 void
220 pop_variable_scope ()
222 register struct variable_set_list *setlist = current_variable_set_list;
223 register struct variable_set *set = setlist->set;
224 register unsigned int i;
226 current_variable_set_list = setlist->next;
227 free ((char *) setlist);
229 for (i = 0; i < set->buckets; ++i)
231 register struct variable *next = set->table[i];
232 while (next != 0)
234 register struct variable *v = next;
235 next = v->next;
237 free (v->name);
238 free ((char *) v);
241 free ((char *) set->table);
242 free ((char *) set);
245 /* Create a new variable set and push it on the current setlist. */
247 void
248 push_new_variable_scope ()
250 register struct variable_set_list *setlist;
251 register struct variable_set *set;
253 set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
254 set->buckets = SMALL_SCOPE_VARIABLE_BUCKETS;
255 set->table = (struct variable **)
256 xmalloc (set->buckets * sizeof (struct variable *));
257 bzero ((char *) set->table, set->buckets * sizeof (struct variable *));
259 setlist = (struct variable_set_list *)
260 xmalloc (sizeof (struct variable_set_list));
261 setlist->set = set;
262 setlist->next = current_variable_set_list;
263 current_variable_set_list = setlist;
266 /* Merge SET1 into SET0, freeing unused storage in SET1. */
268 static void
269 merge_variable_sets (set0, set1)
270 struct variable_set *set0, *set1;
272 register unsigned int bucket1;
274 for (bucket1 = 0; bucket1 < set1->buckets; ++bucket1)
276 register struct variable *v1 = set1->table[bucket1];
277 while (v1 != 0)
279 struct variable *next = v1->next;
280 unsigned int bucket0;
281 register struct variable *v0;
283 if (set1->buckets >= set0->buckets)
284 bucket0 = bucket1;
285 else
287 register char *n;
288 bucket0 = 0;
289 for (n = v1->name; *n != '\0'; ++n)
290 HASH (bucket0, *n);
292 bucket0 %= set0->buckets;
294 for (v0 = set0->table[bucket0]; v0 != 0; v0 = v0->next)
295 if (streq (v0->name, v1->name))
296 break;
298 if (v0 == 0)
300 /* There is no variable in SET0 with the same name. */
301 v1->next = set0->table[bucket0];
302 set0->table[bucket0] = v1;
304 else
306 /* The same variable exists in both sets.
307 SET0 takes precedence. */
308 free (v1->value);
309 free ((char *) v1);
312 v1 = next;
317 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
319 void
320 merge_variable_set_lists (setlist0, setlist1)
321 struct variable_set_list **setlist0, *setlist1;
323 register struct variable_set_list *list0 = *setlist0;
324 struct variable_set_list *last0 = 0;
326 while (setlist1 != 0 && list0 != 0)
328 struct variable_set_list *next = setlist1;
329 setlist1 = setlist1->next;
331 merge_variable_sets (list0->set, next->set);
333 free ((char *) next);
335 last0 = list0;
336 list0 = list0->next;
339 if (setlist1 != 0)
341 if (last0 == 0)
342 *setlist0 = setlist1;
343 else
344 last0->next = setlist1;
348 /* Define the automatic variables, and record the addresses
349 of their structures so we can change their values quickly. */
351 void
352 define_automatic_variables ()
354 extern char default_shell[];
355 register struct variable *v;
356 char buf[200];
358 sprintf (buf, "%u", makelevel);
359 (void) define_variable ("MAKELEVEL", 9, buf, o_env, 0);
361 sprintf (buf, "%s%s%s",
362 version_string,
363 (remote_description == 0 || remote_description[0] == '\0')
364 ? "" : "-",
365 (remote_description == 0 || remote_description[0] == '\0')
366 ? "" : remote_description);
367 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
370 /* This won't override any definition, but it
371 will provide one if there isn't one there. */
372 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
373 v->export = v_export; /* Always export SHELL. */
375 /* Don't let SHELL come from the environment. */
376 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
378 free (v->value);
379 v->origin = o_file;
380 v->value = savestring (default_shell, strlen (default_shell));
383 /* Make sure MAKEFILES gets exported if it is set. */
384 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
385 v->export = v_ifset;
387 /* Define the magic D and F variables in terms of
388 the automatic variables they are variations of. */
390 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
391 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
392 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
393 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
394 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
395 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
396 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
397 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
398 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
399 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
400 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
401 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
402 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
403 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
406 int export_all_variables;
408 /* Create a new environment for FILE's commands.
409 If FILE is nil, this is for the `shell' function.
410 The child's MAKELEVEL variable is incremented. */
412 char **
413 target_environment (file)
414 struct file *file;
416 struct variable_set_list *set_list;
417 register struct variable_set_list *s;
418 struct variable_bucket
420 struct variable_bucket *next;
421 struct variable *variable;
423 struct variable_bucket **table;
424 unsigned int buckets;
425 register unsigned int i;
426 register unsigned nvariables;
427 char **result;
428 unsigned int mklev_hash;
430 if (file == 0)
431 set_list = current_variable_set_list;
432 else
433 set_list = file->variables;
435 /* Find the lowest number of buckets in any set in the list. */
436 s = set_list;
437 buckets = s->set->buckets;
438 for (s = s->next; s != 0; s = s->next)
439 if (s->set->buckets < buckets)
440 buckets = s->set->buckets;
442 /* Find the hash value of the bucket `MAKELEVEL' will fall into. */
444 char *p = "MAKELEVEL";
445 mklev_hash = 0;
446 while (*p != '\0')
447 HASH (mklev_hash, *p++);
450 /* Temporarily allocate a table with that many buckets. */
451 table = (struct variable_bucket **)
452 alloca (buckets * sizeof (struct variable_bucket *));
453 bzero ((char *) table, buckets * sizeof (struct variable_bucket *));
455 /* Run through all the variable sets in the list,
456 accumulating variables in TABLE. */
457 nvariables = 0;
458 for (s = set_list; s != 0; s = s->next)
460 register struct variable_set *set = s->set;
461 for (i = 0; i < set->buckets; ++i)
463 register struct variable *v;
464 for (v = set->table[i]; v != 0; v = v->next)
466 unsigned int j = i % buckets;
467 register struct variable_bucket *ov;
468 register char *p = v->name;
470 if (i == mklev_hash % set->buckets
471 && streq (v->name, "MAKELEVEL"))
472 /* Don't include MAKELEVEL because it will be
473 added specially at the end. */
474 continue;
476 switch (v->export)
478 case v_default:
479 if (v->origin == o_default || v->origin == o_automatic)
480 /* Only export default variables by explicit request. */
481 continue;
483 if (! export_all_variables
484 && v->origin != o_command
485 && v->origin != o_env && v->origin != o_env_override)
486 continue;
488 if (*p != '_' && (*p < 'A' || *p > 'Z')
489 && (*p < 'a' || *p > 'z'))
490 continue;
491 for (++p; *p != '\0'; ++p)
492 if (*p != '_' && (*p < 'a' || *p > 'z')
493 && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9'))
494 break;
495 if (*p != '\0')
496 continue;
498 case v_export:
499 break;
501 case v_noexport:
502 continue;
504 case v_ifset:
505 if (v->origin == o_default)
506 continue;
507 break;
510 for (ov = table[j]; ov != 0; ov = ov->next)
511 if (streq (v->name, ov->variable->name))
512 break;
513 if (ov == 0)
515 register struct variable_bucket *entry;
516 entry = (struct variable_bucket *)
517 alloca (sizeof (struct variable_bucket));
518 entry->next = table[j];
519 entry->variable = v;
520 table[j] = entry;
521 ++nvariables;
527 result = (char **) xmalloc ((nvariables + 2) * sizeof (char *));
528 nvariables = 0;
529 for (i = 0; i < buckets; ++i)
531 register struct variable_bucket *b;
532 for (b = table[i]; b != 0; b = b->next)
534 register struct variable *v = b->variable;
535 /* If V is recursively expanded and didn't come from the environment,
536 expand its value. If it came from the environment, it should
537 go back into the environment unchanged. */
538 if (v->recursive
539 && v->origin != o_env && v->origin != o_env_override)
541 char *value = recursively_expand (v);
542 result[nvariables++] = concat (v->name, "=", value);
543 free (value);
545 else
546 result[nvariables++] = concat (v->name, "=", v->value);
549 result[nvariables] = (char *) xmalloc (100);
550 (void) sprintf (result[nvariables], "MAKELEVEL=%u", makelevel + 1);
551 result[++nvariables] = 0;
553 return result;
556 /* Try to interpret LINE (a null-terminated string) as a variable definition.
558 ORIGIN may be o_file, o_override, o_env, o_env_override,
559 or o_command specifying that the variable definition comes
560 from a makefile, an override directive, the environment with
561 or without the -e switch, or the command line.
563 A variable definition has the form "name = value" or "name := value".
564 Any whitespace around the "=" or ":=" is removed. The first form
565 defines a variable that is recursively re-evaluated. The second form
566 defines a variable whose value is variable-expanded at the time of
567 definition and then is evaluated only once at the time of expansion.
569 If a variable was defined, a pointer to its `struct variable' is returned.
570 If not, NULL is returned. */
572 struct variable *
573 try_variable_definition (filename, lineno, line, origin)
574 char *filename;
575 unsigned int lineno;
576 char *line;
577 enum variable_origin origin;
579 register int c;
580 register char *p = line;
581 register char *beg;
582 register char *end;
583 enum { bogus, simple, recursive, append } flavor = bogus;
584 char *name, *expanded_name, *value;
585 struct variable *v;
587 while (1)
589 c = *p++;
590 if (c == '\0' || c == '#')
591 return 0;
592 if (c == '=')
594 end = p - 1;
595 flavor = recursive;
596 break;
598 else if (c == ':')
599 if (*p == '=')
601 end = p++ - 1;
602 flavor = simple;
603 break;
605 else
606 /* A colon other than := is a rule line, not a variable defn. */
607 return 0;
608 else if (c == '+' && *p == '=')
610 end = p++ - 1;
611 flavor = append;
612 break;
616 beg = next_token (line);
617 while (end > beg && isblank (end[-1]))
618 --end;
619 p = next_token (p);
621 /* Expand the name, so "$(foo)bar = baz" works. */
622 name = (char *) alloca (end - beg + 1);
623 bcopy (beg, name, end - beg);
624 name[end - beg] = '\0';
625 expanded_name = allocated_variable_expand (name);
627 if (expanded_name[0] == '\0')
629 if (filename == 0)
630 fatal ("empty variable name");
631 else
632 makefile_fatal (filename, lineno, "empty variable name");
635 /* Calculate the variable's new value in VALUE. */
637 switch (flavor)
639 case bogus:
640 /* Should not be possible. */
641 abort ();
642 return 0;
643 case simple:
644 /* A simple variable definition "var := value". Expand the value. */
645 value = variable_expand (p);
646 break;
647 case recursive:
648 /* A recursive variable definition "var = value".
649 The value is used verbatim. */
650 value = p;
651 break;
652 case append:
653 /* An appending variable definition "var += value".
654 Extract the old value and append the new one. */
655 v = lookup_variable (expanded_name, strlen (expanded_name));
656 if (v == 0)
658 /* There was no old value.
659 This becomes a normal recursive definition. */
660 value = p;
661 flavor = recursive;
663 else
665 /* Paste the old and new values together in VALUE. */
667 unsigned int oldlen, newlen;
669 if (v->recursive)
670 /* The previous definition of the variable was recursive.
671 The new value comes from the unexpanded old and new values. */
672 flavor = recursive;
673 else
674 /* The previous definition of the variable was simple.
675 The new value comes from the old value, which was expanded
676 when it was set; and from the expanded new value. */
677 p = variable_expand (p);
679 oldlen = strlen (v->value);
680 newlen = strlen (p);
681 value = (char *) alloca (oldlen + 1 + newlen + 1);
682 bcopy (v->value, value, oldlen);
683 value[oldlen] = ' ';
684 bcopy (p, &value[oldlen + 1], newlen + 1);
688 v = define_variable (expanded_name, strlen (expanded_name),
689 value, origin, flavor == recursive);
691 free (expanded_name);
693 return v;
696 /* Print information for variable V, prefixing it with PREFIX. */
698 static void
699 print_variable (v, prefix)
700 register struct variable *v;
701 char *prefix;
703 char *origin;
705 switch (v->origin)
707 case o_default:
708 origin = "default";
709 break;
710 case o_env:
711 origin = "environment";
712 break;
713 case o_file:
714 origin = "makefile";
715 break;
716 case o_env_override:
717 origin = "environment under -e";
718 break;
719 case o_command:
720 origin = "command line";
721 break;
722 case o_override:
723 origin = "`override' directive";
724 break;
725 case o_automatic:
726 origin = "automatic";
727 break;
728 case o_invalid:
729 default:
730 abort ();
731 break;
733 printf ("# %s\n", origin);
735 fputs (prefix, stdout);
737 /* Is this a `define'? */
738 if (v->recursive && index (v->value, '\n') != 0)
739 printf ("define %s\n%s\nendef\n", v->name, v->value);
740 else
742 register char *p;
744 printf ("%s %s= ", v->name, v->recursive ? "" : ":");
746 /* Check if the value is just whitespace. */
747 p = next_token (v->value);
748 if (p != v->value && *p == '\0')
749 /* All whitespace. */
750 printf ("$(subst ,,%s)", v->value);
751 else if (v->recursive)
752 fputs (v->value, stdout);
753 else
754 /* Double up dollar signs. */
755 for (p = v->value; *p != '\0'; ++p)
757 if (*p == '$')
758 putchar ('$');
759 putchar (*p);
761 putchar ('\n');
766 /* Print all the variables in SET. PREFIX is printed before
767 the actual variable definitions (everything else is comments). */
769 static void
770 print_variable_set (set, prefix)
771 register struct variable_set *set;
772 char *prefix;
774 register unsigned int i, nvariables, per_bucket;
775 register struct variable *v;
777 per_bucket = nvariables = 0;
778 for (i = 0; i < set->buckets; ++i)
780 register unsigned int this_bucket = 0;
782 for (v = set->table[i]; v != 0; v = v->next)
784 ++this_bucket;
785 print_variable (v, prefix);
788 nvariables += this_bucket;
789 if (this_bucket > per_bucket)
790 per_bucket = this_bucket;
793 if (nvariables == 0)
794 puts ("# No variables.");
795 else
797 printf ("# %u variables in %u hash buckets.\n",
798 nvariables, set->buckets);
799 #ifndef NO_FLOAT
800 printf ("# average of %.1f variables per bucket, \
801 max %u in one bucket.\n",
802 (double) nvariables / (double) set->buckets,
803 per_bucket);
804 #else
806 int f = (nvariables * 1000 + 5) / set->buckets;
807 printf ("# average of %d.%d variables per bucket, \
808 max %u in one bucket.\n",
809 f/10, f%10,
810 per_bucket);
812 #endif
817 /* Print the data base of variables. */
819 void
820 print_variable_data_base ()
822 puts ("\n# Variables\n");
824 print_variable_set (&global_variable_set, "");
828 /* Print all the local variables of FILE. */
830 void
831 print_file_variables (file)
832 struct file *file;
834 if (file->variables != 0)
835 print_variable_set (file->variables->set, "# ");