1 | /* Definitions for using variables in GNU Make.
|
---|
2 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
|
---|
3 | 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
|
---|
4 | Foundation, Inc.
|
---|
5 | This file is part of GNU Make.
|
---|
6 |
|
---|
7 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
8 | terms of the GNU General Public License as published by the Free Software
|
---|
9 | Foundation; either version 2, or (at your option) any later version.
|
---|
10 |
|
---|
11 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
13 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License along with
|
---|
16 | GNU Make; see the file COPYING. If not, write to the Free Software
|
---|
17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
|
---|
18 |
|
---|
19 | #include "hash.h"
|
---|
20 |
|
---|
21 | /* Codes in a variable definition saying where the definition came from.
|
---|
22 | Increasing numeric values signify less-overridable definitions. */
|
---|
23 | enum variable_origin
|
---|
24 | {
|
---|
25 | o_default, /* Variable from the default set. */
|
---|
26 | o_env, /* Variable from environment. */
|
---|
27 | o_file, /* Variable given in a makefile. */
|
---|
28 | o_env_override, /* Variable from environment, if -e. */
|
---|
29 | o_command, /* Variable given by user. */
|
---|
30 | o_override, /* Variable from an `override' directive. */
|
---|
31 | o_automatic, /* Automatic variable -- cannot be set. */
|
---|
32 | o_invalid /* Core dump time. */
|
---|
33 | };
|
---|
34 |
|
---|
35 | enum variable_flavor
|
---|
36 | {
|
---|
37 | f_bogus, /* Bogus (error) */
|
---|
38 | f_simple, /* Simple definition (:=) */
|
---|
39 | f_recursive, /* Recursive definition (=) */
|
---|
40 | f_append, /* Appending definition (+=) */
|
---|
41 | f_conditional /* Conditional definition (?=) */
|
---|
42 | };
|
---|
43 |
|
---|
44 | /* Structure that represents one variable definition.
|
---|
45 | Each bucket of the hash table is a chain of these,
|
---|
46 | chained through `next'. */
|
---|
47 |
|
---|
48 | #define EXP_COUNT_BITS 15 /* This gets all the bitfields into 32 bits */
|
---|
49 | #define EXP_COUNT_MAX ((1<<EXP_COUNT_BITS)-1)
|
---|
50 |
|
---|
51 | struct variable
|
---|
52 | {
|
---|
53 | char *name; /* Variable name. */
|
---|
54 | int length; /* strlen (name) */
|
---|
55 | #ifdef VARIABLE_HASH /* bird */
|
---|
56 | int hash1; /* the primary hash */
|
---|
57 | int hash2; /* the secondary hash */
|
---|
58 | #endif
|
---|
59 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
60 | int value_length; /* The length of the value, usually unused. */
|
---|
61 | int value_alloc_len; /* The amount of memory we've actually allocated. */
|
---|
62 | /* FIXME: make lengths unsigned! */
|
---|
63 | #endif
|
---|
64 | char *value; /* Variable value. */
|
---|
65 | struct floc fileinfo; /* Where the variable was defined. */
|
---|
66 | unsigned int recursive:1; /* Gets recursively re-evaluated. */
|
---|
67 | unsigned int append:1; /* Nonzero if an appending target-specific
|
---|
68 | variable. */
|
---|
69 | unsigned int conditional:1; /* Nonzero if set with a ?=. */
|
---|
70 | unsigned int per_target:1; /* Nonzero if a target-specific variable. */
|
---|
71 | unsigned int special:1; /* Nonzero if this is a special variable. */
|
---|
72 | unsigned int exportable:1; /* Nonzero if the variable _could_ be
|
---|
73 | exported. */
|
---|
74 | unsigned int expanding:1; /* Nonzero if currently being expanded. */
|
---|
75 | unsigned int exp_count:EXP_COUNT_BITS;
|
---|
76 | /* If >1, allow this many self-referential
|
---|
77 | expansions. */
|
---|
78 | enum variable_flavor
|
---|
79 | flavor ENUM_BITFIELD (3); /* Variable flavor. */
|
---|
80 | enum variable_origin
|
---|
81 | origin ENUM_BITFIELD (3); /* Variable origin. */
|
---|
82 | enum variable_export
|
---|
83 | {
|
---|
84 | v_export, /* Export this variable. */
|
---|
85 | v_noexport, /* Don't export this variable. */
|
---|
86 | v_ifset, /* Export it if it has a non-default value. */
|
---|
87 | v_default /* Decide in target_environment. */
|
---|
88 | } export ENUM_BITFIELD (2);
|
---|
89 | };
|
---|
90 |
|
---|
91 | /* Structure that represents a variable set. */
|
---|
92 |
|
---|
93 | struct variable_set
|
---|
94 | {
|
---|
95 | struct hash_table table; /* Hash table of variables. */
|
---|
96 | };
|
---|
97 |
|
---|
98 | /* Structure that represents a list of variable sets. */
|
---|
99 |
|
---|
100 | struct variable_set_list
|
---|
101 | {
|
---|
102 | struct variable_set_list *next; /* Link in the chain. */
|
---|
103 | struct variable_set *set; /* Variable set. */
|
---|
104 | };
|
---|
105 |
|
---|
106 | /* Structure used for pattern-specific variables. */
|
---|
107 |
|
---|
108 | struct pattern_var
|
---|
109 | {
|
---|
110 | struct pattern_var *next;
|
---|
111 | char *target;
|
---|
112 | unsigned int len;
|
---|
113 | char *suffix;
|
---|
114 | struct variable variable;
|
---|
115 | };
|
---|
116 |
|
---|
117 | extern char *variable_buffer;
|
---|
118 | extern struct variable_set_list *current_variable_set_list;
|
---|
119 |
|
---|
120 | /* expand.c */
|
---|
121 | extern char *variable_buffer_output PARAMS ((char *ptr, char *string, unsigned int length));
|
---|
122 | extern char *variable_expand PARAMS ((char *line));
|
---|
123 | extern char *variable_expand_for_file PARAMS ((char *line, struct file *file));
|
---|
124 | extern char *allocated_variable_expand_for_file PARAMS ((char *line, struct file *file));
|
---|
125 | #define allocated_variable_expand(line) \
|
---|
126 | allocated_variable_expand_for_file (line, (struct file *) 0)
|
---|
127 | extern char *expand_argument PARAMS ((const char *str, const char *end));
|
---|
128 | extern char *variable_expand_string PARAMS ((char *line, char *string,
|
---|
129 | long length));
|
---|
130 | extern void install_variable_buffer PARAMS ((char **bufp, unsigned int *lenp));
|
---|
131 | extern void restore_variable_buffer PARAMS ((char *buf, unsigned int len));
|
---|
132 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
133 | extern void append_expanded_string_to_variable PARAMS ((struct variable *v, char *value));
|
---|
134 | #endif
|
---|
135 |
|
---|
136 | /* function.c */
|
---|
137 | extern int handle_function PARAMS ((char **op, char **stringp));
|
---|
138 | extern int pattern_matches PARAMS ((char *pattern, char *percent, char *str));
|
---|
139 | extern char *subst_expand PARAMS ((char *o, char *text, char *subst, char *replace,
|
---|
140 | unsigned int slen, unsigned int rlen, int by_word));
|
---|
141 | extern char *patsubst_expand PARAMS ((char *o, char *text, char *pattern, char *replace,
|
---|
142 | char *pattern_percent, char *replace_percent));
|
---|
143 |
|
---|
144 | /* expand.c */
|
---|
145 | extern char *recursively_expand_for_file PARAMS ((struct variable *v,
|
---|
146 | struct file *file));
|
---|
147 | #define recursively_expand(v) recursively_expand_for_file (v, NULL)
|
---|
148 |
|
---|
149 | /* variable.c */
|
---|
150 | extern struct variable_set_list *create_new_variable_set PARAMS ((void));
|
---|
151 | extern void free_variable_set PARAMS ((struct variable_set_list *));
|
---|
152 | extern struct variable_set_list *push_new_variable_scope PARAMS ((void));
|
---|
153 | extern void pop_variable_scope PARAMS ((void));
|
---|
154 | extern void define_automatic_variables PARAMS ((void));
|
---|
155 | extern void initialize_file_variables PARAMS ((struct file *file, int read));
|
---|
156 | extern void print_file_variables PARAMS ((struct file *file));
|
---|
157 | extern void print_variable_set PARAMS ((struct variable_set *set, char *prefix));
|
---|
158 | extern void merge_variable_set_lists PARAMS ((struct variable_set_list **to_list, struct variable_set_list *from_list));
|
---|
159 | extern struct variable *do_variable_definition PARAMS ((const struct floc *flocp, const char *name, char *value, enum variable_origin origin, enum variable_flavor flavor, int target_var));
|
---|
160 | extern struct variable *parse_variable_definition PARAMS ((struct variable *v, char *line));
|
---|
161 | extern struct variable *try_variable_definition PARAMS ((const struct floc *flocp, char *line, enum variable_origin origin, int target_var));
|
---|
162 | extern void init_hash_global_variable_set PARAMS ((void));
|
---|
163 | extern void hash_init_function_table PARAMS ((void));
|
---|
164 | extern struct variable *lookup_variable PARAMS ((const char *name, unsigned int length));
|
---|
165 | extern struct variable *lookup_variable_in_set PARAMS ((const char *name,
|
---|
166 | unsigned int length,
|
---|
167 | const struct variable_set *set));
|
---|
168 |
|
---|
169 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
170 |
|
---|
171 | extern struct variable *define_variable_in_set
|
---|
172 | PARAMS ((const char *name, unsigned int length, char *value,
|
---|
173 | unsigned int value_length, int duplicate_value,
|
---|
174 | enum variable_origin origin, int recursive,
|
---|
175 | struct variable_set *set, const struct floc *flocp));
|
---|
176 |
|
---|
177 | /* Define a variable in the current variable set. */
|
---|
178 |
|
---|
179 | #define define_variable(n,l,v,o,r) \
|
---|
180 | define_variable_in_set((n),(l),(v), ~0U,1,(o),(r),\
|
---|
181 | current_variable_set_list->set,NILF)
|
---|
182 |
|
---|
183 | #define define_variable_vl(n,l,v,vl,dv,o,r) \
|
---|
184 | define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),\
|
---|
185 | current_variable_set_list->set,NILF)
|
---|
186 |
|
---|
187 | /* Define a variable with a location in the current variable set. */
|
---|
188 |
|
---|
189 | #define define_variable_loc(n,l,v,o,r,f) \
|
---|
190 | define_variable_in_set((n),(l),(v),~0U,1,(o),(r),\
|
---|
191 | current_variable_set_list->set,(f))
|
---|
192 |
|
---|
193 | /* Define a variable with a location in the global variable set. */
|
---|
194 |
|
---|
195 | #define define_variable_global(n,l,v,o,r,f) \
|
---|
196 | define_variable_in_set((n),(l),(v),~0U,1,(o),(r),NULL,(f))
|
---|
197 |
|
---|
198 | #define define_variable_vl_global(n,l,v,vl,dv,o,r,f) \
|
---|
199 | define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),NULL,(f))
|
---|
200 |
|
---|
201 | /* Define a variable in FILE's variable set. */
|
---|
202 |
|
---|
203 | #define define_variable_for_file(n,l,v,o,r,f) \
|
---|
204 | define_variable_in_set((n),(l),(v),~0U,1,(o),(r),(f)->variables->set,NILF)
|
---|
205 |
|
---|
206 | #else
|
---|
207 |
|
---|
208 | extern struct variable *define_variable_in_set
|
---|
209 | PARAMS ((const char *name, unsigned int length, char *value,
|
---|
210 | enum variable_origin origin, int recursive,
|
---|
211 | struct variable_set *set, const struct floc *flocp));
|
---|
212 |
|
---|
213 | /* Define a variable in the current variable set. */
|
---|
214 |
|
---|
215 | #define define_variable(n,l,v,o,r) \
|
---|
216 | define_variable_in_set((n),(l),(v),(o),(r),\
|
---|
217 | current_variable_set_list->set,NILF)
|
---|
218 |
|
---|
219 | /* Define a variable with a location in the current variable set. */
|
---|
220 |
|
---|
221 | #define define_variable_loc(n,l,v,o,r,f) \
|
---|
222 | define_variable_in_set((n),(l),(v),(o),(r),\
|
---|
223 | current_variable_set_list->set,(f))
|
---|
224 |
|
---|
225 | /* Define a variable with a location in the global variable set. */
|
---|
226 |
|
---|
227 | #define define_variable_global(n,l,v,o,r,f) \
|
---|
228 | define_variable_in_set((n),(l),(v),(o),(r),NULL,(f))
|
---|
229 |
|
---|
230 | /* Define a variable in FILE's variable set. */
|
---|
231 |
|
---|
232 | #define define_variable_for_file(n,l,v,o,r,f) \
|
---|
233 | define_variable_in_set((n),(l),(v),(o),(r),(f)->variables->set,NILF)
|
---|
234 |
|
---|
235 | #endif
|
---|
236 |
|
---|
237 | /* Warn that NAME is an undefined variable. */
|
---|
238 |
|
---|
239 | #define warn_undefined(n,l) do{\
|
---|
240 | if (warn_undefined_variables_flag) \
|
---|
241 | error (reading_file, \
|
---|
242 | _("warning: undefined variable `%.*s'"), \
|
---|
243 | (int)(l), (n)); \
|
---|
244 | }while(0)
|
---|
245 |
|
---|
246 | extern char **target_environment PARAMS ((struct file *file));
|
---|
247 |
|
---|
248 | extern struct pattern_var *create_pattern_var PARAMS ((char *target, char *suffix));
|
---|
249 |
|
---|
250 | extern int export_all_variables;
|
---|
251 |
|
---|
252 | #define MAKELEVEL_NAME "MAKELEVEL"
|
---|
253 | #define MAKELEVEL_LENGTH (sizeof (MAKELEVEL_NAME) - 1)
|
---|