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. */
26 /* Hash table of all global variable definitions. */
28 #ifndef VARIABLE_BUCKETS
29 #define VARIABLE_BUCKETS 523
31 #ifndef PERFILE_VARIABLE_BUCKETS
32 #define PERFILE_VARIABLE_BUCKETS 23
34 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
35 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
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
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
)
63 enum variable_origin origin
;
65 struct variable_set
*set
;
67 register unsigned int i
;
68 register unsigned int hashval
;
69 register struct variable
*v
;
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
)
78 && !strncmp (v
->name
+ 1, name
+ 1, length
- 1)
79 && v
->name
[length
] == '\0')
82 if (env_overrides
&& origin
== o_env
)
83 origin
= o_env_override
;
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
)
99 v
->value
= savestring (value
, strlen (value
));
101 v
->recursive
= recursive
;
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
));
112 v
->recursive
= recursive
;
114 v
->export
= v_default
;
115 v
->next
= set
->table
[hashval
];
116 set
->table
[hashval
] = v
;
120 /* Define a variable in the current variable set. */
123 define_variable (name
, length
, value
, origin
, recursive
)
127 enum variable_origin origin
;
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. */
137 define_variable_for_file (name
, length
, value
, origin
, recursive
, file
)
141 enum variable_origin origin
;
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. */
155 lookup_variable (name
, 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)
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. */
189 initialize_file_variables (file
)
192 register struct variable_set_list
*l
= file
->variables
;
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
*));
206 if (file
->parent
== 0)
207 l
->next
= &global_setlist
;
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. */
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
];
234 register struct variable
*v
= next
;
241 free ((char *) set
->table
);
245 /* Create a new variable set and push it on the current setlist. */
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
));
262 setlist
->next
= current_variable_set_list
;
263 current_variable_set_list
= setlist
;
266 /* Merge SET1 into SET0, freeing unused storage in SET1. */
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
];
279 struct variable
*next
= v1
->next
;
280 unsigned int bucket0
;
281 register struct variable
*v0
;
283 if (set1
->buckets
>= set0
->buckets
)
289 for (n
= v1
->name
; *n
!= '\0'; ++n
)
292 bucket0
%= set0
->buckets
;
294 for (v0
= set0
->table
[bucket0
]; v0
!= 0; v0
= v0
->next
)
295 if (streq (v0
->name
, v1
->name
))
300 /* There is no variable in SET0 with the same name. */
301 v1
->next
= set0
->table
[bucket0
];
302 set0
->table
[bucket0
] = v1
;
306 /* The same variable exists in both sets.
307 SET0 takes precedence. */
317 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
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
);
342 *setlist0
= setlist1
;
344 last0
->next
= setlist1
;
348 /* Define the automatic variables, and record the addresses
349 of their structures so we can change their values quickly. */
352 define_automatic_variables ()
354 extern char default_shell
[];
355 register struct variable
*v
;
358 sprintf (buf
, "%u", makelevel
);
359 (void) define_variable ("MAKELEVEL", 9, buf
, o_env
, 0);
361 sprintf (buf
, "%s%s%s",
363 (remote_description
== 0 || remote_description
[0] == '\0')
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
)
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);
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. */
413 target_environment (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
;
428 unsigned int mklev_hash
;
431 set_list
= current_variable_set_list
;
433 set_list
= file
->variables
;
435 /* Find the lowest number of buckets in any set in the 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";
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. */
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. */
479 if (v
->origin
== o_default
|| v
->origin
== o_automatic
)
480 /* Only export default variables by explicit request. */
483 if (! export_all_variables
484 && v
->origin
!= o_command
485 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
488 if (*p
!= '_' && (*p
< 'A' || *p
> 'Z')
489 && (*p
< 'a' || *p
> 'z'))
491 for (++p
; *p
!= '\0'; ++p
)
492 if (*p
!= '_' && (*p
< 'a' || *p
> 'z')
493 && (*p
< 'A' || *p
> 'Z') && (*p
< '0' || *p
> '9'))
505 if (v
->origin
== o_default
)
510 for (ov
= table
[j
]; ov
!= 0; ov
= ov
->next
)
511 if (streq (v
->name
, ov
->variable
->name
))
515 register struct variable_bucket
*entry
;
516 entry
= (struct variable_bucket
*)
517 alloca (sizeof (struct variable_bucket
));
518 entry
->next
= table
[j
];
527 result
= (char **) xmalloc ((nvariables
+ 2) * sizeof (char *));
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. */
539 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
541 char *value
= recursively_expand (v
);
542 result
[nvariables
++] = concat (v
->name
, "=", value
);
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;
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. */
573 try_variable_definition (filename
, lineno
, line
, origin
)
577 enum variable_origin origin
;
580 register char *p
= line
;
583 enum { bogus
, simple
, recursive
, append
} flavor
= bogus
;
584 char *name
, *expanded_name
, *value
;
590 if (c
== '\0' || c
== '#')
606 /* A colon other than := is a rule line, not a variable defn. */
608 else if (c
== '+' && *p
== '=')
616 beg
= next_token (line
);
617 while (end
> beg
&& isblank (end
[-1]))
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')
630 fatal ("empty variable name");
632 makefile_fatal (filename
, lineno
, "empty variable name");
635 /* Calculate the variable's new value in VALUE. */
640 /* Should not be possible. */
644 /* A simple variable definition "var := value". Expand the value. */
645 value
= variable_expand (p
);
648 /* A recursive variable definition "var = value".
649 The value is used verbatim. */
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
));
658 /* There was no old value.
659 This becomes a normal recursive definition. */
665 /* Paste the old and new values together in VALUE. */
667 unsigned int oldlen
, newlen
;
670 /* The previous definition of the variable was recursive.
671 The new value comes from the unexpanded old and new values. */
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
);
681 value
= (char *) alloca (oldlen
+ 1 + newlen
+ 1);
682 bcopy (v
->value
, 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
);
696 /* Print information for variable V, prefixing it with PREFIX. */
699 print_variable (v
, prefix
)
700 register struct variable
*v
;
711 origin
= "environment";
717 origin
= "environment under -e";
720 origin
= "command line";
723 origin
= "`override' directive";
726 origin
= "automatic";
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
);
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
);
754 /* Double up dollar signs. */
755 for (p
= v
->value
; *p
!= '\0'; ++p
)
766 /* Print all the variables in SET. PREFIX is printed before
767 the actual variable definitions (everything else is comments). */
770 print_variable_set (set
, prefix
)
771 register struct variable_set
*set
;
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
)
785 print_variable (v
, prefix
);
788 nvariables
+= this_bucket
;
789 if (this_bucket
> per_bucket
)
790 per_bucket
= this_bucket
;
794 puts ("# No variables.");
797 printf ("# %u variables in %u hash buckets.\n",
798 nvariables
, set
->buckets
);
800 printf ("# average of %.1f variables per bucket, \
801 max %u in one bucket.\n",
802 (double) nvariables
/ (double) set
->buckets
,
809 /* Print the data base of variables. */
812 print_variable_data_base ()
814 puts ("\n# Variables\n");
816 print_variable_set (&global_variable_set
, "");
820 /* Print all the local variables of FILE. */
823 print_file_variables (file
)
826 if (file
->variables
!= 0)
827 print_variable_set (file
->variables
->set
, "# ");