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)
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. */
25 /* Hash table of all global variable definitions. */
27 #ifndef VARIABLE_BUCKETS
28 #define VARIABLE_BUCKETS 523
30 #ifndef PERFILE_VARIABLE_BUCKETS
31 #define PERFILE_VARIABLE_BUCKETS 23
33 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
34 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
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
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
)
57 enum variable_origin origin
;
59 struct variable_set
*set
;
61 register unsigned int i
;
62 register unsigned int hashval
;
63 register struct variable
*v
;
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
)
72 && !strncmp (v
->name
+ 1, name
+ 1, length
- 1)
73 && v
->name
[length
] == '\0')
76 if (env_overrides
&& origin
== o_env
)
77 origin
= o_env_override
;
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
)
93 v
->value
= savestring (value
, strlen (value
));
95 v
->recursive
= recursive
;
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
));
106 v
->recursive
= recursive
;
108 v
->export
= v_default
;
109 v
->next
= set
->table
[hashval
];
110 set
->table
[hashval
] = v
;
114 /* Define a variable in the current variable set. */
117 define_variable (name
, length
, value
, origin
, recursive
)
121 enum variable_origin origin
;
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. */
131 define_variable_for_file (name
, length
, value
, origin
, recursive
, file
)
135 enum variable_origin origin
;
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. */
149 lookup_variable (name
, 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)
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. */
183 initialize_file_variables (file
)
186 register struct variable_set_list
*l
= file
->variables
;
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
*));
200 if (file
->parent
== 0)
201 l
->next
= &global_setlist
;
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. */
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
];
228 register struct variable
*v
= next
;
235 free ((char *) set
->table
);
239 /* Create a new variable set and push it on the current setlist. */
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
));
256 setlist
->next
= current_variable_set_list
;
257 current_variable_set_list
= setlist
;
260 /* Merge SET1 into SET0, freeing unused storage in SET1. */
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
];
273 struct variable
*next
= v1
->next
;
274 unsigned int bucket0
;
275 register struct variable
*v0
;
277 if (set1
->buckets
>= set0
->buckets
)
283 for (n
= v1
->name
; *n
!= '\0'; ++n
)
286 bucket0
%= set0
->buckets
;
288 for (v0
= set0
->table
[bucket0
]; v0
!= 0; v0
= v0
->next
)
289 if (streq (v0
->name
, v1
->name
))
294 /* There is no variable in SET0 with the same name. */
295 v1
->next
= set0
->table
[bucket0
];
296 set0
->table
[bucket0
] = v1
;
300 /* The same variable exists in both sets.
301 SET0 takes precedence. */
311 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
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
);
336 *setlist0
= setlist1
;
338 last0
->next
= setlist1
;
342 /* Define the automatic variables, and record the addresses
343 of their structures so we can change their values quickly. */
346 define_automatic_variables ()
348 extern char default_shell
[];
349 register struct variable
*v
;
352 sprintf (buf
, "%u", makelevel
);
353 (void) define_variable ("MAKELEVEL", 9, buf
, o_env
, 0);
355 sprintf (buf
, "%s%s%s",
357 (remote_description
== 0 || remote_description
[0] == '\0')
359 (remote_description
== 0 || remote_description
[0] == '\0')
360 ? "" : remote_description
);
361 (void) define_variable ("MAKE_VERSION", 12, buf
, o_default
, 0);
364 /* This won't override any definition, but it
365 will provide one if there isn't one there. */
366 v
= define_variable ("SHELL", 5, default_shell
, o_default
, 0);
367 v
->export
= v_export
; /* Always export SHELL. */
369 /* Don't let SHELL come from the environment. */
370 if (*v
->value
== '\0' || v
->origin
== o_env
|| v
->origin
== o_env_override
)
374 v
->value
= savestring (default_shell
, strlen (default_shell
));
377 /* Make sure MAKEFILES gets exported if it is set. */
378 v
= define_variable ("MAKEFILES", 9, "", o_default
, 0);
381 /* Define the magic D and F variables in terms of
382 the automatic variables they are variations of. */
384 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic
, 1);
385 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic
, 1);
386 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic
, 1);
387 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic
, 1);
388 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic
, 1);
389 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic
, 1);
390 define_variable ("@F", 2, "$(notdir $@)", o_automatic
, 1);
391 define_variable ("%F", 2, "$(notdir $%)", o_automatic
, 1);
392 define_variable ("*F", 2, "$(notdir $*)", o_automatic
, 1);
393 define_variable ("<F", 2, "$(notdir $<)", o_automatic
, 1);
394 define_variable ("?F", 2, "$(notdir $?)", o_automatic
, 1);
395 define_variable ("^F", 2, "$(notdir $^)", o_automatic
, 1);
398 int export_all_variables
;
400 /* Create a new environment for FILE's commands.
401 If FILE is nil, this is for the `shell' function.
402 The child's MAKELEVEL variable is incremented. */
405 target_environment (file
)
408 struct variable_set_list
*set_list
;
409 register struct variable_set_list
*s
;
410 struct variable_bucket
412 struct variable_bucket
*next
;
413 struct variable
*variable
;
415 struct variable_bucket
**table
;
416 unsigned int buckets
;
417 register unsigned int i
;
418 register unsigned nvariables
;
420 unsigned int mklev_hash
;
423 set_list
= current_variable_set_list
;
425 set_list
= file
->variables
;
427 /* Find the lowest number of buckets in any set in the list. */
429 buckets
= s
->set
->buckets
;
430 for (s
= s
->next
; s
!= 0; s
= s
->next
)
431 if (s
->set
->buckets
< buckets
)
432 buckets
= s
->set
->buckets
;
434 /* Find the hash value of the bucket `MAKELEVEL' will fall into. */
436 char *p
= "MAKELEVEL";
439 HASH (mklev_hash
, *p
++);
442 /* Temporarily allocate a table with that many buckets. */
443 table
= (struct variable_bucket
**)
444 alloca (buckets
* sizeof (struct variable_bucket
*));
445 bzero ((char *) table
, buckets
* sizeof (struct variable_bucket
*));
447 /* Run through all the variable sets in the list,
448 accumulating variables in TABLE. */
450 for (s
= set_list
; s
!= 0; s
= s
->next
)
452 register struct variable_set
*set
= s
->set
;
453 for (i
= 0; i
< set
->buckets
; ++i
)
455 register struct variable
*v
;
456 for (v
= set
->table
[i
]; v
!= 0; v
= v
->next
)
458 unsigned int j
= i
% buckets
;
459 register struct variable_bucket
*ov
;
460 register char *p
= v
->name
;
462 if (i
== mklev_hash
% set
->buckets
463 && streq (v
->name
, "MAKELEVEL"))
464 /* Don't include MAKELEVEL because it will be
465 added specially at the end. */
471 if (v
->origin
== o_default
|| v
->origin
== o_automatic
)
472 /* Only export default variables by explicit request. */
475 if (! export_all_variables
476 && v
->origin
!= o_command
477 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
480 if (*p
!= '_' && (*p
< 'A' || *p
> 'Z')
481 && (*p
< 'a' || *p
> 'z'))
483 for (++p
; *p
!= '\0'; ++p
)
484 if (*p
!= '_' && (*p
< 'a' || *p
> 'z')
485 && (*p
< 'A' || *p
> 'Z') && (*p
< '0' || *p
> '9'))
497 if (v
->origin
== o_default
)
502 for (ov
= table
[j
]; ov
!= 0; ov
= ov
->next
)
503 if (streq (v
->name
, ov
->variable
->name
))
507 register struct variable_bucket
*entry
;
508 entry
= (struct variable_bucket
*)
509 alloca (sizeof (struct variable_bucket
));
510 entry
->next
= table
[j
];
519 result
= (char **) xmalloc ((nvariables
+ 2) * sizeof (char *));
521 for (i
= 0; i
< buckets
; ++i
)
523 register struct variable_bucket
*b
;
524 for (b
= table
[i
]; b
!= 0; b
= b
->next
)
526 register struct variable
*v
= b
->variable
;
527 /* If V is recursively expanded and didn't come from the environment,
528 expand its value. If it came from the environment, it should
529 go back into the environment unchanged. */
531 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
533 char *value
= recursively_expand (v
);
534 result
[nvariables
++] = concat (v
->name
, "=", value
);
538 result
[nvariables
++] = concat (v
->name
, "=", v
->value
);
541 result
[nvariables
] = (char *) xmalloc (100);
542 (void) sprintf (result
[nvariables
], "MAKELEVEL=%u", makelevel
+ 1);
543 result
[++nvariables
] = 0;
548 /* Try to interpret LINE (a null-terminated string) as a variable definition.
550 ORIGIN may be o_file, o_override, o_env, o_env_override,
551 or o_command specifying that the variable definition comes
552 from a makefile, an override directive, the environment with
553 or without the -e switch, or the command line.
555 A variable definition has the form "name = value" or "name := value".
556 Any whitespace around the "=" or ":=" is removed. The first form
557 defines a variable that is recursively re-evaluated. The second form
558 defines a variable whose value is variable-expanded at the time of
559 definition and then is evaluated only once at the time of expansion.
561 If a variable was defined, a pointer to its `struct variable' is returned.
562 If not, NULL is returned. */
565 try_variable_definition (filename
, lineno
, line
, origin
)
569 enum variable_origin origin
;
572 register char *p
= line
;
575 enum { bogus
, simple
, recursive
, append
} flavor
= bogus
;
576 char *name
, *expanded_name
, *value
;
582 if (c
== '\0' || c
== '#')
598 /* A colon other than := is a rule line, not a variable defn. */
600 else if (c
== '+' && *p
== '=')
608 beg
= next_token (line
);
609 while (end
> beg
&& isblank (end
[-1]))
613 /* Expand the name, so "$(foo)bar = baz" works. */
614 name
= (char *) alloca (end
- beg
+ 1);
615 bcopy (beg
, name
, end
- beg
);
616 name
[end
- beg
] = '\0';
617 expanded_name
= allocated_variable_expand (name
);
619 if (expanded_name
[0] == '\0')
622 fatal ("empty variable name");
624 makefile_fatal (filename
, lineno
, "empty variable name");
627 /* Calculate the variable's new value in VALUE. */
632 /* Should not be possible. */
636 /* A simple variable definition "var := value". Expand the value. */
637 value
= variable_expand (p
);
640 /* A recursive variable definition "var = value".
641 The value is used verbatim. */
645 /* An appending variable definition "var += value".
646 Extract the old value and append the new one. */
647 v
= lookup_variable (expanded_name
, strlen (expanded_name
));
650 /* There was no old value.
651 This becomes a normal recursive definition. */
657 /* Paste the old and new values together in VALUE. */
659 unsigned int oldlen
, newlen
;
662 /* The previous definition of the variable was recursive.
663 The new value comes from the unexpanded old and new values. */
666 /* The previous definition of the variable was simple.
667 The new value comes from the old value, which was expanded
668 when it was set; and from the expanded new value. */
669 p
= variable_expand (p
);
671 oldlen
= strlen (v
->value
);
673 value
= (char *) alloca (oldlen
+ 1 + newlen
+ 1);
674 bcopy (v
->value
, value
, oldlen
);
676 bcopy (p
, &value
[oldlen
+ 1], newlen
+ 1);
680 v
= define_variable (expanded_name
, strlen (expanded_name
),
681 value
, origin
, flavor
== recursive
);
683 free (expanded_name
);
688 /* Print information for variable V, prefixing it with PREFIX. */
691 print_variable (v
, prefix
)
692 register struct variable
*v
;
703 origin
= "environment";
709 origin
= "environment under -e";
712 origin
= "command line";
715 origin
= "`override' directive";
718 origin
= "automatic";
725 printf ("# %s\n", origin
);
727 fputs (prefix
, stdout
);
729 /* Is this a `define'? */
730 if (v
->recursive
&& index (v
->value
, '\n') != 0)
731 printf ("define %s\n%s\nendef\n", v
->name
, v
->value
);
736 printf ("%s %s= ", v
->name
, v
->recursive
? "" : ":");
738 /* Check if the value is just whitespace. */
739 p
= next_token (v
->value
);
740 if (p
!= v
->value
&& *p
== '\0')
741 /* All whitespace. */
742 printf ("$(subst ,,%s)", v
->value
);
743 else if (v
->recursive
)
744 fputs (v
->value
, stdout
);
746 /* Double up dollar signs. */
747 for (p
= v
->value
; *p
!= '\0'; ++p
)
758 /* Print all the variables in SET. PREFIX is printed before
759 the actual variable definitions (everything else is comments). */
762 print_variable_set (set
, prefix
)
763 register struct variable_set
*set
;
766 register unsigned int i
, nvariables
, per_bucket
;
767 register struct variable
*v
;
769 per_bucket
= nvariables
= 0;
770 for (i
= 0; i
< set
->buckets
; ++i
)
772 register unsigned int this_bucket
= 0;
774 for (v
= set
->table
[i
]; v
!= 0; v
= v
->next
)
777 print_variable (v
, prefix
);
780 nvariables
+= this_bucket
;
781 if (this_bucket
> per_bucket
)
782 per_bucket
= this_bucket
;
786 puts ("# No variables.");
789 printf ("# %u variables in %u hash buckets.\n",
790 nvariables
, set
->buckets
);
792 printf ("# average of %.1f variables per bucket, \
793 max %u in one bucket.\n",
794 (double) nvariables
/ (double) set
->buckets
,
801 /* Print the data base of variables. */
804 print_variable_data_base ()
806 puts ("\n# Variables\n");
808 print_variable_set (&global_variable_set
, "");
812 /* Print all the local variables of FILE. */
815 print_file_variables (file
)
818 if (file
->variables
!= 0)
819 print_variable_set (file
->variables
->set
, "# ");