1 /* Internals of variables for GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 96 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 #include "pathstuff.h"
29 /* Hash table of all global variable definitions. */
31 #ifndef VARIABLE_BUCKETS
32 #define VARIABLE_BUCKETS 523
34 #ifndef PERFILE_VARIABLE_BUCKETS
35 #define PERFILE_VARIABLE_BUCKETS 23
37 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
38 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
40 static struct variable
*variable_table
[VARIABLE_BUCKETS
];
41 static struct variable_set global_variable_set
42 = { variable_table
, VARIABLE_BUCKETS
};
43 static struct variable_set_list global_setlist
44 = { 0, &global_variable_set
};
45 struct variable_set_list
*current_variable_set_list
= &global_setlist
;
47 static struct variable
*define_variable_in_set
PARAMS ((char *name
, unsigned int length
,
48 char *value
, enum variable_origin origin
,
49 int recursive
, struct variable_set
*set
));
52 /* Implement variables. */
54 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
55 LENGTH is the length of NAME, which does not need to be null-terminated.
56 ORIGIN specifies the origin of the variable (makefile, command line
58 If RECURSIVE is nonzero a flag is set in the variable saying
59 that it should be recursively re-expanded. */
61 static struct variable
*
62 define_variable_in_set (name
, length
, value
, origin
, recursive
, set
)
66 enum variable_origin origin
;
68 struct variable_set
*set
;
70 register unsigned int i
;
71 register unsigned int hashval
;
72 register struct variable
*v
;
75 for (i
= 0; i
< length
; ++i
)
76 HASH (hashval
, name
[i
]);
77 hashval
%= set
->buckets
;
79 for (v
= set
->table
[hashval
]; v
!= 0; v
= v
->next
)
81 && !strncmp (v
->name
+ 1, name
+ 1, length
- 1)
82 && v
->name
[length
] == '\0')
85 if (env_overrides
&& origin
== o_env
)
86 origin
= o_env_override
;
90 if (env_overrides
&& v
->origin
== o_env
)
91 /* V came from in the environment. Since it was defined
92 before the switches were parsed, it wasn't affected by -e. */
93 v
->origin
= o_env_override
;
95 /* A variable of this name is already defined.
96 If the old definition is from a stronger source
97 than this one, don't redefine it. */
98 if ((int) origin
>= (int) v
->origin
)
102 v
->value
= savestring (value
, strlen (value
));
104 v
->recursive
= recursive
;
109 /* Create a new variable definition and add it to the hash table. */
111 v
= (struct variable
*) xmalloc (sizeof (struct variable
));
112 v
->name
= savestring (name
, length
);
113 v
->value
= savestring (value
, strlen (value
));
115 v
->recursive
= recursive
;
117 v
->export
= v_default
;
118 v
->next
= set
->table
[hashval
];
119 set
->table
[hashval
] = v
;
123 /* Define a variable in the current variable set. */
126 define_variable (name
, length
, value
, origin
, recursive
)
130 enum variable_origin origin
;
133 return define_variable_in_set (name
, length
, value
, origin
, recursive
,
134 current_variable_set_list
->set
);
137 /* Define a variable in FILE's variable set. */
140 define_variable_for_file (name
, length
, value
, origin
, recursive
, file
)
144 enum variable_origin origin
;
148 return define_variable_in_set (name
, length
, value
, origin
, recursive
,
149 file
->variables
->set
);
152 /* Lookup a variable whose name is a string starting at NAME
153 and with LENGTH chars. NAME need not be null-terminated.
154 Returns address of the `struct variable' containing all info
155 on the variable, or nil if no such variable is defined. */
158 lookup_variable (name
, length
)
162 register struct variable_set_list
*setlist
;
164 register unsigned int i
;
165 register unsigned int rawhash
= 0;
167 for (i
= 0; i
< length
; ++i
)
168 HASH (rawhash
, name
[i
]);
170 for (setlist
= current_variable_set_list
;
171 setlist
!= 0; setlist
= setlist
->next
)
173 register struct variable_set
*set
= setlist
->set
;
174 register unsigned int hashval
= rawhash
% set
->buckets
;
175 register struct variable
*v
;
177 for (v
= set
->table
[hashval
]; v
!= 0; v
= v
->next
)
178 if (*v
->name
== *name
179 && !strncmp (v
->name
+ 1, name
+ 1, length
- 1)
180 && v
->name
[length
] == 0)
187 /* Initialize FILE's variable set list. If FILE already has a variable set
188 list, the topmost variable set is left intact, but the the rest of the
189 chain is replaced with FILE->parent's setlist. */
192 initialize_file_variables (file
)
195 register struct variable_set_list
*l
= file
->variables
;
198 l
= (struct variable_set_list
*)
199 xmalloc (sizeof (struct variable_set_list
));
200 l
->set
= (struct variable_set
*) xmalloc (sizeof (struct variable_set
));
201 l
->set
->buckets
= PERFILE_VARIABLE_BUCKETS
;
202 l
->set
->table
= (struct variable
**)
203 xmalloc (l
->set
->buckets
* sizeof (struct variable
*));
204 bzero ((char *) l
->set
->table
,
205 l
->set
->buckets
* sizeof (struct variable
*));
209 if (file
->parent
== 0)
210 l
->next
= &global_setlist
;
213 if (file
->parent
->variables
== 0)
214 initialize_file_variables (file
->parent
);
215 l
->next
= file
->parent
->variables
;
219 /* Pop the top set off the current variable set list,
220 and free all its storage. */
223 pop_variable_scope ()
225 register struct variable_set_list
*setlist
= current_variable_set_list
;
226 register struct variable_set
*set
= setlist
->set
;
227 register unsigned int i
;
229 current_variable_set_list
= setlist
->next
;
230 free ((char *) setlist
);
232 for (i
= 0; i
< set
->buckets
; ++i
)
234 register struct variable
*next
= set
->table
[i
];
237 register struct variable
*v
= next
;
244 free ((char *) set
->table
);
248 /* Create a new variable set and push it on the current setlist. */
251 push_new_variable_scope ()
253 register struct variable_set_list
*setlist
;
254 register struct variable_set
*set
;
256 set
= (struct variable_set
*) xmalloc (sizeof (struct variable_set
));
257 set
->buckets
= SMALL_SCOPE_VARIABLE_BUCKETS
;
258 set
->table
= (struct variable
**)
259 xmalloc (set
->buckets
* sizeof (struct variable
*));
260 bzero ((char *) set
->table
, set
->buckets
* sizeof (struct variable
*));
262 setlist
= (struct variable_set_list
*)
263 xmalloc (sizeof (struct variable_set_list
));
265 setlist
->next
= current_variable_set_list
;
266 current_variable_set_list
= setlist
;
269 /* Merge SET1 into SET0, freeing unused storage in SET1. */
272 merge_variable_sets (set0
, set1
)
273 struct variable_set
*set0
, *set1
;
275 register unsigned int bucket1
;
277 for (bucket1
= 0; bucket1
< set1
->buckets
; ++bucket1
)
279 register struct variable
*v1
= set1
->table
[bucket1
];
282 struct variable
*next
= v1
->next
;
283 unsigned int bucket0
;
284 register struct variable
*v0
;
286 if (set1
->buckets
>= set0
->buckets
)
292 for (n
= v1
->name
; *n
!= '\0'; ++n
)
295 bucket0
%= set0
->buckets
;
297 for (v0
= set0
->table
[bucket0
]; v0
!= 0; v0
= v0
->next
)
298 if (streq (v0
->name
, v1
->name
))
303 /* There is no variable in SET0 with the same name. */
304 v1
->next
= set0
->table
[bucket0
];
305 set0
->table
[bucket0
] = v1
;
309 /* The same variable exists in both sets.
310 SET0 takes precedence. */
320 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
323 merge_variable_set_lists (setlist0
, setlist1
)
324 struct variable_set_list
**setlist0
, *setlist1
;
326 register struct variable_set_list
*list0
= *setlist0
;
327 struct variable_set_list
*last0
= 0;
329 while (setlist1
!= 0 && list0
!= 0)
331 struct variable_set_list
*next
= setlist1
;
332 setlist1
= setlist1
->next
;
334 merge_variable_sets (list0
->set
, next
->set
);
336 free ((char *) next
);
345 *setlist0
= setlist1
;
347 last0
->next
= setlist1
;
351 /* Define the automatic variables, and record the addresses
352 of their structures so we can change their values quickly. */
355 define_automatic_variables ()
358 extern char* default_shell
;
360 extern char default_shell
[];
362 register struct variable
*v
;
365 sprintf (buf
, "%u", makelevel
);
366 (void) define_variable ("MAKELEVEL", 9, buf
, o_env
, 0);
368 sprintf (buf
, "%s%s%s",
370 (remote_description
== 0 || remote_description
[0] == '\0')
372 (remote_description
== 0 || remote_description
[0] == '\0')
373 ? "" : remote_description
);
374 (void) define_variable ("MAKE_VERSION", 12, buf
, o_default
, 0);
377 /* This won't override any definition, but it
378 will provide one if there isn't one there. */
379 v
= define_variable ("SHELL", 5, default_shell
, o_default
, 0);
380 v
->export
= v_export
; /* Always export SHELL. */
382 /* Don't let SHELL come from the environment. */
383 if (*v
->value
== '\0' || v
->origin
== o_env
|| v
->origin
== o_env_override
)
387 v
->value
= savestring (default_shell
, strlen (default_shell
));
390 /* Make sure MAKEFILES gets exported if it is set. */
391 v
= define_variable ("MAKEFILES", 9, "", o_default
, 0);
394 /* Define the magic D and F variables in terms of
395 the automatic variables they are variations of. */
397 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic
, 1);
398 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic
, 1);
399 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic
, 1);
400 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic
, 1);
401 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic
, 1);
402 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic
, 1);
403 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic
, 1);
404 define_variable ("@F", 2, "$(notdir $@)", o_automatic
, 1);
405 define_variable ("%F", 2, "$(notdir $%)", o_automatic
, 1);
406 define_variable ("*F", 2, "$(notdir $*)", o_automatic
, 1);
407 define_variable ("<F", 2, "$(notdir $<)", o_automatic
, 1);
408 define_variable ("?F", 2, "$(notdir $?)", o_automatic
, 1);
409 define_variable ("^F", 2, "$(notdir $^)", o_automatic
, 1);
410 define_variable ("+F", 2, "$(notdir $+)", o_automatic
, 1);
413 int export_all_variables
;
415 /* Create a new environment for FILE's commands.
416 If FILE is nil, this is for the `shell' function.
417 The child's MAKELEVEL variable is incremented. */
420 target_environment (file
)
423 struct variable_set_list
*set_list
;
424 register struct variable_set_list
*s
;
425 struct variable_bucket
427 struct variable_bucket
*next
;
428 struct variable
*variable
;
430 struct variable_bucket
**table
;
431 unsigned int buckets
;
432 register unsigned int i
;
433 register unsigned nvariables
;
435 unsigned int mklev_hash
;
438 set_list
= current_variable_set_list
;
440 set_list
= file
->variables
;
442 /* Find the lowest number of buckets in any set in the list. */
444 buckets
= s
->set
->buckets
;
445 for (s
= s
->next
; s
!= 0; s
= s
->next
)
446 if (s
->set
->buckets
< buckets
)
447 buckets
= s
->set
->buckets
;
449 /* Find the hash value of the bucket `MAKELEVEL' will fall into. */
451 char *p
= "MAKELEVEL";
454 HASH (mklev_hash
, *p
++);
457 /* Temporarily allocate a table with that many buckets. */
458 table
= (struct variable_bucket
**)
459 alloca (buckets
* sizeof (struct variable_bucket
*));
460 bzero ((char *) table
, buckets
* sizeof (struct variable_bucket
*));
462 /* Run through all the variable sets in the list,
463 accumulating variables in TABLE. */
465 for (s
= set_list
; s
!= 0; s
= s
->next
)
467 register struct variable_set
*set
= s
->set
;
468 for (i
= 0; i
< set
->buckets
; ++i
)
470 register struct variable
*v
;
471 for (v
= set
->table
[i
]; v
!= 0; v
= v
->next
)
473 unsigned int j
= i
% buckets
;
474 register struct variable_bucket
*ov
;
475 register char *p
= v
->name
;
477 if (i
== mklev_hash
% set
->buckets
478 && streq (v
->name
, "MAKELEVEL"))
479 /* Don't include MAKELEVEL because it will be
480 added specially at the end. */
486 if (v
->origin
== o_default
|| v
->origin
== o_automatic
)
487 /* Only export default variables by explicit request. */
490 if (! export_all_variables
491 && v
->origin
!= o_command
492 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
495 if (*p
!= '_' && (*p
< 'A' || *p
> 'Z')
496 && (*p
< 'a' || *p
> 'z'))
498 for (++p
; *p
!= '\0'; ++p
)
499 if (*p
!= '_' && (*p
< 'a' || *p
> 'z')
500 && (*p
< 'A' || *p
> 'Z') && (*p
< '0' || *p
> '9'))
512 if (v
->origin
== o_default
)
517 for (ov
= table
[j
]; ov
!= 0; ov
= ov
->next
)
518 if (streq (v
->name
, ov
->variable
->name
))
522 register struct variable_bucket
*entry
;
523 entry
= (struct variable_bucket
*)
524 alloca (sizeof (struct variable_bucket
));
525 entry
->next
= table
[j
];
534 result
= (char **) xmalloc ((nvariables
+ 2) * sizeof (char *));
536 for (i
= 0; i
< buckets
; ++i
)
538 register struct variable_bucket
*b
;
539 for (b
= table
[i
]; b
!= 0; b
= b
->next
)
541 register struct variable
*v
= b
->variable
;
542 /* If V is recursively expanded and didn't come from the environment,
543 expand its value. If it came from the environment, it should
544 go back into the environment unchanged. */
546 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
548 char *value
= recursively_expand (v
);
550 if (strcmp(v
->name
, "Path") == 0 ||
551 strcmp(v
->name
, "PATH") == 0)
552 convert_Path_to_win32(value
, ';');
554 result
[nvariables
++] = concat (v
->name
, "=", value
);
560 if (strcmp(v
->name
, "Path") == 0 ||
561 strcmp(v
->name
, "PATH") == 0)
562 convert_Path_to_win32(v
->value
, ';');
563 result
[nvariables
++] = concat (v
->name
, "=", v
->value
);
566 result
[nvariables
++] = concat (v
->name
, "=", v
->value
);
570 result
[nvariables
] = (char *) xmalloc (100);
571 (void) sprintf (result
[nvariables
], "MAKELEVEL=%u", makelevel
+ 1);
572 result
[++nvariables
] = 0;
577 /* Try to interpret LINE (a null-terminated string) as a variable definition.
579 ORIGIN may be o_file, o_override, o_env, o_env_override,
580 or o_command specifying that the variable definition comes
581 from a makefile, an override directive, the environment with
582 or without the -e switch, or the command line.
584 A variable definition has the form "name = value" or "name := value".
585 Any whitespace around the "=" or ":=" is removed. The first form
586 defines a variable that is recursively re-evaluated. The second form
587 defines a variable whose value is variable-expanded at the time of
588 definition and then is evaluated only once at the time of expansion.
590 If a variable was defined, a pointer to its `struct variable' is returned.
591 If not, NULL is returned. */
594 try_variable_definition (filename
, lineno
, line
, origin
)
598 enum variable_origin origin
;
601 register char *p
= line
;
604 enum { bogus
, simple
, recursive
, append
} flavor
= bogus
;
605 char *name
, *expanded_name
, *value
;
611 if (c
== '\0' || c
== '#')
627 /* A colon other than := is a rule line, not a variable defn. */
629 else if (c
== '+' && *p
== '=')
637 /* This might begin a variable expansion reference. Make sure we
638 don't misrecognize chars inside the reference as =, := or +=. */
647 continue; /* Nope. */
649 /* P now points past the opening paren or brace.
650 Count parens or braces until it is matched. */
652 for (; *p
!= '\0'; ++p
)
656 else if (*p
== closeparen
&& --count
< 0)
665 beg
= next_token (line
);
666 while (end
> beg
&& isblank (end
[-1]))
670 /* Expand the name, so "$(foo)bar = baz" works. */
671 name
= (char *) alloca (end
- beg
+ 1);
672 bcopy (beg
, name
, end
- beg
);
673 name
[end
- beg
] = '\0';
674 expanded_name
= allocated_variable_expand (name
);
676 if (expanded_name
[0] == '\0')
679 fatal ("empty variable name");
681 makefile_fatal (filename
, lineno
, "empty variable name");
684 /* Calculate the variable's new value in VALUE. */
689 /* Should not be possible. */
693 /* A simple variable definition "var := value". Expand the value. */
694 value
= variable_expand (p
);
697 /* A recursive variable definition "var = value".
698 The value is used verbatim. */
702 /* An appending variable definition "var += value".
703 Extract the old value and append the new one. */
704 v
= lookup_variable (expanded_name
, strlen (expanded_name
));
707 /* There was no old value.
708 This becomes a normal recursive definition. */
714 /* Paste the old and new values together in VALUE. */
716 unsigned int oldlen
, newlen
;
719 /* The previous definition of the variable was recursive.
720 The new value comes from the unexpanded old and new values. */
723 /* The previous definition of the variable was simple.
724 The new value comes from the old value, which was expanded
725 when it was set; and from the expanded new value. */
726 p
= variable_expand (p
);
728 oldlen
= strlen (v
->value
);
730 value
= (char *) alloca (oldlen
+ 1 + newlen
+ 1);
731 bcopy (v
->value
, value
, oldlen
);
733 bcopy (p
, &value
[oldlen
+ 1], newlen
+ 1);
737 v
= define_variable (expanded_name
, strlen (expanded_name
),
738 value
, origin
, flavor
== recursive
);
740 free (expanded_name
);
745 /* Print information for variable V, prefixing it with PREFIX. */
748 print_variable (v
, prefix
)
749 register struct variable
*v
;
760 origin
= "environment";
766 origin
= "environment under -e";
769 origin
= "command line";
772 origin
= "`override' directive";
775 origin
= "automatic";
782 printf ("# %s\n", origin
);
784 fputs (prefix
, stdout
);
786 /* Is this a `define'? */
787 if (v
->recursive
&& index (v
->value
, '\n') != 0)
788 printf ("define %s\n%s\nendef\n", v
->name
, v
->value
);
793 printf ("%s %s= ", v
->name
, v
->recursive
? "" : ":");
795 /* Check if the value is just whitespace. */
796 p
= next_token (v
->value
);
797 if (p
!= v
->value
&& *p
== '\0')
798 /* All whitespace. */
799 printf ("$(subst ,,%s)", v
->value
);
800 else if (v
->recursive
)
801 fputs (v
->value
, stdout
);
803 /* Double up dollar signs. */
804 for (p
= v
->value
; *p
!= '\0'; ++p
)
815 /* Print all the variables in SET. PREFIX is printed before
816 the actual variable definitions (everything else is comments). */
819 print_variable_set (set
, prefix
)
820 register struct variable_set
*set
;
823 register unsigned int i
, nvariables
, per_bucket
;
824 register struct variable
*v
;
826 per_bucket
= nvariables
= 0;
827 for (i
= 0; i
< set
->buckets
; ++i
)
829 register unsigned int this_bucket
= 0;
831 for (v
= set
->table
[i
]; v
!= 0; v
= v
->next
)
834 print_variable (v
, prefix
);
837 nvariables
+= this_bucket
;
838 if (this_bucket
> per_bucket
)
839 per_bucket
= this_bucket
;
843 puts ("# No variables.");
846 printf ("# %u variables in %u hash buckets.\n",
847 nvariables
, set
->buckets
);
849 printf ("# average of %.1f variables per bucket, \
850 max %u in one bucket.\n",
851 (double) nvariables
/ (double) set
->buckets
,
855 int f
= (nvariables
* 1000 + 5) / set
->buckets
;
856 printf ("# average of %d.%d variables per bucket, \
857 max %u in one bucket.\n",
866 /* Print the data base of variables. */
869 print_variable_data_base ()
871 puts ("\n# Variables\n");
873 print_variable_set (&global_variable_set
, "");
877 /* Print all the local variables of FILE. */
880 print_file_variables (file
)
883 if (file
->variables
!= 0)
884 print_variable_set (file
->variables
->set
, "# ");
889 sync_Path_environment(void)
891 char* path
= allocated_variable_expand("$(Path)");
892 static char* environ_path
= NULL
;
898 * If done this before, don't leak memory unnecessarily.
899 * Free the previous entry before allocating new one.
905 * Create something WIN32 world can grok
907 convert_Path_to_win32(path
, ';');
908 environ_path
= concat("Path", "=", path
);
909 putenv(environ_path
);