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 | const char *suffix;
|
---|
112 | const char *target;
|
---|
113 | unsigned int len;
|
---|
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 | char *variable_buffer_output (char *ptr, const char *string, unsigned int length);
|
---|
122 | char *variable_expand (const char *line);
|
---|
123 | char *variable_expand_for_file (const char *line, struct file *file);
|
---|
124 | char *allocated_variable_expand_for_file (const char *line, struct file *file);
|
---|
125 | #define allocated_variable_expand(line) \
|
---|
126 | allocated_variable_expand_for_file (line, (struct file *) 0)
|
---|
127 | char *expand_argument (const char *str, const char *end);
|
---|
128 | char *variable_expand_string (char *line, const char *string, long length);
|
---|
129 | void install_variable_buffer (char **bufp, unsigned int *lenp);
|
---|
130 | void restore_variable_buffer (char *buf, unsigned int len);
|
---|
131 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
132 | extern void append_expanded_string_to_variable (struct variable *v, const char *value);
|
---|
133 | #endif
|
---|
134 |
|
---|
135 | /* function.c */
|
---|
136 | int handle_function (char **op, const char **stringp);
|
---|
137 | int pattern_matches (const char *pattern, const char *percent, const char *str);
|
---|
138 | char *subst_expand (char *o, const char *text, const char *subst,
|
---|
139 | const char *replace, unsigned int slen, unsigned int rlen,
|
---|
140 | int by_word);
|
---|
141 | char *patsubst_expand_pat (char *o, const char *text, const char *pattern,
|
---|
142 | const char *replace, const char *pattern_percent,
|
---|
143 | const char *replace_percent);
|
---|
144 | char *patsubst_expand (char *o, const char *text, char *pattern, char *replace);
|
---|
145 |
|
---|
146 | /* expand.c */
|
---|
147 | char *recursively_expand_for_file (struct variable *v, struct file *file);
|
---|
148 | #define recursively_expand(v) recursively_expand_for_file (v, NULL)
|
---|
149 |
|
---|
150 | /* variable.c */
|
---|
151 | struct variable_set_list *create_new_variable_set (void);
|
---|
152 | void free_variable_set (struct variable_set_list *);
|
---|
153 | struct variable_set_list *push_new_variable_scope (void);
|
---|
154 | void pop_variable_scope (void);
|
---|
155 | void define_automatic_variables (void);
|
---|
156 | void initialize_file_variables (struct file *file, int reading);
|
---|
157 | void print_file_variables (const struct file *file);
|
---|
158 | void print_variable_set (struct variable_set *set, char *prefix);
|
---|
159 | void merge_variable_set_lists (struct variable_set_list **to_list,
|
---|
160 | struct variable_set_list *from_list);
|
---|
161 | struct variable *do_variable_definition (const struct floc *flocp,
|
---|
162 | const char *name, const char *value,
|
---|
163 | enum variable_origin origin,
|
---|
164 | enum variable_flavor flavor,
|
---|
165 | int target_var);
|
---|
166 | struct variable *parse_variable_definition (struct variable *v, char *line);
|
---|
167 | struct variable *try_variable_definition (const struct floc *flocp, char *line,
|
---|
168 | enum variable_origin origin,
|
---|
169 | int target_var);
|
---|
170 | void init_hash_global_variable_set (void);
|
---|
171 | void hash_init_function_table (void);
|
---|
172 | struct variable *lookup_variable (const char *name, unsigned int length);
|
---|
173 | struct variable *lookup_variable_in_set (const char *name, unsigned int length,
|
---|
174 | const struct variable_set *set);
|
---|
175 |
|
---|
176 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
177 |
|
---|
178 | struct variable *define_variable_in_set (const char *name, unsigned int length,
|
---|
179 | const char *value,
|
---|
180 | unsigned int value_length,
|
---|
181 | int duplicate_value,
|
---|
182 | enum variable_origin origin,
|
---|
183 | int recursive,
|
---|
184 | struct variable_set *set,
|
---|
185 | const struct floc *flocp);
|
---|
186 |
|
---|
187 | /* Define a variable in the current variable set. */
|
---|
188 |
|
---|
189 | #define define_variable(n,l,v,o,r) \
|
---|
190 | define_variable_in_set((n),(l),(v),~0U,1,(o),(r),\
|
---|
191 | current_variable_set_list->set,NILF)
|
---|
192 |
|
---|
193 | #define define_variable_vl(n,l,v,vl,dv,o,r) \
|
---|
194 | define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),\
|
---|
195 | current_variable_set_list->set,NILF)
|
---|
196 |
|
---|
197 | /* Define a variable with a location in the current variable set. */
|
---|
198 |
|
---|
199 | #define define_variable_loc(n,l,v,o,r,f) \
|
---|
200 | define_variable_in_set((n),(l),(v),~0U,1,(o),(r),\
|
---|
201 | current_variable_set_list->set,(f))
|
---|
202 |
|
---|
203 | /* Define a variable with a location in the global variable set. */
|
---|
204 |
|
---|
205 | #define define_variable_global(n,l,v,o,r,f) \
|
---|
206 | define_variable_in_set((n),(l),(v),~0U,1,(o),(r),NULL,(f))
|
---|
207 |
|
---|
208 | #define define_variable_vl_global(n,l,v,vl,dv,o,r,f) \
|
---|
209 | define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),NULL,(f))
|
---|
210 |
|
---|
211 | /* Define a variable in FILE's variable set. */
|
---|
212 |
|
---|
213 | #define define_variable_for_file(n,l,v,o,r,f) \
|
---|
214 | define_variable_in_set((n),(l),(v),~0U,1,(o),(r),(f)->variables->set,NILF)
|
---|
215 |
|
---|
216 | #else /* !CONFIG_WITH_VALUE_LENGTH */
|
---|
217 |
|
---|
218 | struct variable *define_variable_in_set (const char *name, unsigned int length,
|
---|
219 | const char *value,
|
---|
220 | enum variable_origin origin,
|
---|
221 | int recursive,
|
---|
222 | struct variable_set *set,
|
---|
223 | const struct floc *flocp);
|
---|
224 |
|
---|
225 | /* Define a variable in the current variable set. */
|
---|
226 |
|
---|
227 | #define define_variable(n,l,v,o,r) \
|
---|
228 | define_variable_in_set((n),(l),(v),(o),(r),\
|
---|
229 | current_variable_set_list->set,NILF) /* force merge conflict */
|
---|
230 |
|
---|
231 | /* Define a variable with a location in the current variable set. */
|
---|
232 |
|
---|
233 | #define define_variable_loc(n,l,v,o,r,f) \
|
---|
234 | define_variable_in_set((n),(l),(v),(o),(r),\
|
---|
235 | current_variable_set_list->set,(f)) /* force merge conflict */
|
---|
236 |
|
---|
237 | /* Define a variable with a location in the global variable set. */
|
---|
238 |
|
---|
239 | #define define_variable_global(n,l,v,o,r,f) \
|
---|
240 | define_variable_in_set((n),(l),(v),(o),(r),NULL,(f)) /* force merge conflict */
|
---|
241 |
|
---|
242 | /* Define a variable in FILE's variable set. */
|
---|
243 |
|
---|
244 | #define define_variable_for_file(n,l,v,o,r,f) \
|
---|
245 | define_variable_in_set((n),(l),(v),(o),(r),(f)->variables->set,NILF) /* force merge conflict */
|
---|
246 |
|
---|
247 | #endif /* !CONFIG_WITH_VALUE_LENGTH */
|
---|
248 |
|
---|
249 | /* Warn that NAME is an undefined variable. */
|
---|
250 |
|
---|
251 | #define warn_undefined(n,l) do{\
|
---|
252 | if (warn_undefined_variables_flag) \
|
---|
253 | error (reading_file, \
|
---|
254 | _("warning: undefined variable `%.*s'"), \
|
---|
255 | (int)(l), (n)); \
|
---|
256 | }while(0)
|
---|
257 |
|
---|
258 | char **target_environment (struct file *file);
|
---|
259 |
|
---|
260 | struct pattern_var *create_pattern_var (const char *target,
|
---|
261 | const char *suffix);
|
---|
262 |
|
---|
263 | extern int export_all_variables;
|
---|
264 |
|
---|
265 | #define MAKELEVEL_NAME "MAKELEVEL"
|
---|
266 | #define MAKELEVEL_LENGTH (sizeof (MAKELEVEL_NAME) - 1)
|
---|