kmk: strlcpy build fix for windows.
[kbuild-mirror.git] / src / kmk / variable.h
blobcc06de169f260f0dad1fc5ff3666e96b147cd8a6
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, 2007 Free Software
4 Foundation, Inc.
5 This file is part of GNU Make.
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 3 of the License, or (at your option) any later
10 version.
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "hash.h"
21 /* Codes in a variable definition saying where the definition came from.
22 Increasing numeric values signify less-overridable definitions. */
23 enum variable_origin
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 #ifdef CONFIG_WITH_LOCAL_VARIABLES
32 o_local, /* Variable from an 'local' directive. */
33 #endif
34 o_automatic, /* Automatic variable -- cannot be set. */
35 o_invalid /* Core dump time. */
38 enum variable_flavor
40 f_bogus, /* Bogus (error) */
41 f_simple, /* Simple definition (:=) */
42 f_recursive, /* Recursive definition (=) */
43 f_append, /* Appending definition (+=) */
44 #ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
45 f_prepend, /* Prepending definition (>=) */
46 #endif
47 f_conditional /* Conditional definition (?=) */
50 /* Structure that represents one variable definition.
51 Each bucket of the hash table is a chain of these,
52 chained through `next'. */
54 #define EXP_COUNT_BITS 15 /* This gets all the bitfields into 32 bits */
55 #define EXP_COUNT_MAX ((1<<EXP_COUNT_BITS)-1)
56 #ifdef CONFIG_WITH_VALUE_LENGTH
57 #define VAR_ALIGN_VALUE_ALLOC(len) ( ((len) + (unsigned int)15) & ~(unsigned int)15 )
58 #endif
60 struct variable
62 #ifndef CONFIG_WITH_STRCACHE2
63 char *name; /* Variable name. */
64 #else
65 const char *name; /* Variable name (in varaible_strcache). */
66 #endif
67 int length; /* strlen (name) */
68 #ifdef CONFIG_WITH_VALUE_LENGTH
69 unsigned int value_length; /* The length of the value. */
70 unsigned int value_alloc_len; /* The amount of memory we've actually allocated. */
71 /* FIXME: make lengths unsigned! */
72 #endif
73 char *value; /* Variable value. */
74 struct floc fileinfo; /* Where the variable was defined. */
75 unsigned int recursive:1; /* Gets recursively re-evaluated. */
76 unsigned int append:1; /* Nonzero if an appending target-specific
77 variable. */
78 unsigned int conditional:1; /* Nonzero if set with a ?=. */
79 unsigned int per_target:1; /* Nonzero if a target-specific variable. */
80 unsigned int special:1; /* Nonzero if this is a special variable. */
81 unsigned int exportable:1; /* Nonzero if the variable _could_ be
82 exported. */
83 unsigned int expanding:1; /* Nonzero if currently being expanded. */
84 unsigned int exp_count:EXP_COUNT_BITS;
85 /* If >1, allow this many self-referential
86 expansions. */
87 #ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
88 unsigned int rdonly_val:1; /* VALUE is read only (strcache/const). */
89 #endif
90 enum variable_flavor
91 flavor ENUM_BITFIELD (3); /* Variable flavor. */
92 enum variable_origin
93 #ifdef CONFIG_WITH_LOCAL_VARIABLES
94 origin ENUM_BITFIELD (4); /* Variable origin. */
95 #else
96 origin ENUM_BITFIELD (3); /* Variable origin. */
97 #endif
98 enum variable_export
100 v_export, /* Export this variable. */
101 v_noexport, /* Don't export this variable. */
102 v_ifset, /* Export it if it has a non-default value. */
103 v_default /* Decide in target_environment. */
104 } export ENUM_BITFIELD (2);
105 #ifdef CONFIG_WITH_MAKE_STATS
106 unsigned int changes;
107 unsigned int reallocs;
108 #endif
111 /* Structure that represents a variable set. */
113 struct variable_set
115 struct hash_table table; /* Hash table of variables. */
118 /* Structure that represents a list of variable sets. */
120 struct variable_set_list
122 struct variable_set_list *next; /* Link in the chain. */
123 struct variable_set *set; /* Variable set. */
126 /* Structure used for pattern-specific variables. */
128 struct pattern_var
130 struct pattern_var *next;
131 const char *suffix;
132 const char *target;
133 unsigned int len;
134 struct variable variable;
137 extern char *variable_buffer;
138 extern struct variable_set_list *current_variable_set_list;
139 #ifdef KMK
140 extern unsigned int variable_buffer_length;
141 #define VARIABLE_BUFFER_ZONE 5
142 #endif
144 /* expand.c */
145 #ifndef KMK
146 char *variable_buffer_output (char *ptr, const char *string, unsigned int length);
147 #else /* KMK */
148 /* Subroutine of variable_expand and friends:
149 The text to add is LENGTH chars starting at STRING to the variable_buffer.
150 The text is added to the buffer at PTR, and the updated pointer into
151 the buffer is returned as the value. Thus, the value returned by
152 each call to variable_buffer_output should be the first argument to
153 the following call. */
155 __inline static char *
156 variable_buffer_output (char *ptr, const char *string, unsigned int length)
158 register unsigned int newlen = length + (ptr - variable_buffer);
160 if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
162 unsigned int offset = ptr - variable_buffer;
163 variable_buffer_length = variable_buffer_length <= 1024
164 ? 2048 : variable_buffer_length * 4;
165 if (variable_buffer_length < newlen + 100)
166 variable_buffer_length = (newlen + 100 + 1023) & ~1023U;
167 variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
168 ptr = variable_buffer + offset;
171 # ifndef _MSC_VER
172 switch (length)
174 case 4: ptr[3] = string[3];
175 case 3: ptr[2] = string[2];
176 case 2: ptr[1] = string[1];
177 case 1: ptr[0] = string[0];
178 case 0:
179 break;
180 default:
181 memcpy (ptr, string, length);
182 break;
184 # else
185 memcpy (ptr, string, length);
186 # endif
187 return ptr + length;
190 #endif /* KMK */
191 char *variable_expand (const char *line);
192 char *variable_expand_for_file (const char *line, struct file *file);
193 #if defined (CONFIG_WITH_VALUE_LENGTH) || defined (CONFIG_WITH_COMMANDS_FUNC)
194 char *variable_expand_for_file_2 (char *o, const char *line, unsigned int lenght,
195 struct file *file, unsigned int *value_lenp);
196 #endif
197 char *allocated_variable_expand_for_file (const char *line, struct file *file);
198 #ifndef CONFIG_WITH_VALUE_LENGTH
199 #define allocated_variable_expand(line) \
200 allocated_variable_expand_for_file (line, (struct file *) 0)
201 #else /* CONFIG_WITH_VALUE_LENGTH */
202 # define allocated_variable_expand(line) \
203 allocated_variable_expand_2 (line, -1, NULL)
204 char *allocated_variable_expand_2 (const char *line, unsigned int length, unsigned int *value_lenp);
205 char *allocated_variable_expand_3 (const char *line, unsigned int length,
206 unsigned int *value_lenp, unsigned int *buffer_lengthp);
207 void recycle_variable_buffer (char *buffer, unsigned int length);
208 #endif /* CONFIG_WITH_VALUE_LENGTH */
209 char *expand_argument (const char *str, const char *end);
210 #ifndef CONFIG_WITH_VALUE_LENGTH
211 char *variable_expand_string (char *line, const char *string, long length);
212 #else /* CONFIG_WITH_VALUE_LENGTH */
213 char *variable_expand_string_2 (char *line, const char *string, long length, char **eol);
214 __inline static char *
215 variable_expand_string (char *line, const char *string, long length)
217 char *ignored;
218 return variable_expand_string_2 (line, string, length, &ignored);
220 #endif /* CONFIG_WITH_VALUE_LENGTH */
221 void install_variable_buffer (char **bufp, unsigned int *lenp);
222 void restore_variable_buffer (char *buf, unsigned int len);
223 #ifdef CONFIG_WITH_VALUE_LENGTH
224 void append_expanded_string_to_variable (struct variable *v, const char *value,
225 unsigned int value_len, int append);
226 #endif
228 /* function.c */
229 #ifndef CONFIG_WITH_VALUE_LENGTH
230 int handle_function (char **op, const char **stringp);
231 #else
232 int handle_function (char **op, const char **stringp, const char *nameend, const char *eol);
233 #endif
234 int pattern_matches (const char *pattern, const char *percent, const char *str);
235 char *subst_expand (char *o, const char *text, const char *subst,
236 const char *replace, unsigned int slen, unsigned int rlen,
237 int by_word);
238 char *patsubst_expand_pat (char *o, const char *text, const char *pattern,
239 const char *replace, const char *pattern_percent,
240 const char *replace_percent);
241 char *patsubst_expand (char *o, const char *text, char *pattern, char *replace);
242 #ifdef CONFIG_WITH_COMMANDS_FUNC
243 char *func_commands (char *o, char **argv, const char *funcname);
244 #endif
245 #if defined (CONFIG_WITH_VALUE_LENGTH)
246 /* Avoid calling handle_function for every variable, do the
247 basic checks in variable_expand_string_2. */
248 extern char func_char_map[256];
249 # define MAX_FUNCTION_LENGTH 12
250 # define MIN_FUNCTION_LENGTH 2
251 MY_INLINE const char *
252 may_be_function_name (const char *name, const char *eos)
254 unsigned char ch;
255 unsigned int len = name - eos;
257 /* Minimum length is MIN + whitespace. Check this directly.
258 ASSUMES: MIN_FUNCTION_LENGTH == 2 */
260 if (MY_PREDICT_TRUE(len < MIN_FUNCTION_LENGTH + 1
261 || !func_char_map[(int)(name[0])]
262 || !func_char_map[(int)(name[1])]))
263 return 0;
264 if (MY_PREDICT_TRUE(!func_char_map[ch = name[2]]))
265 return isspace (ch) ? name + 2 : 0;
267 name += 3;
268 if (len > MAX_FUNCTION_LENGTH)
269 len = MAX_FUNCTION_LENGTH - 3;
270 else if (len == 3)
271 len -= 3;
272 if (!len)
273 return 0;
275 /* Loop over the remaining possiblities. */
277 while (func_char_map[ch = *name])
279 if (!len--)
280 return 0;
281 name++;
283 if (ch == '\0' || isblank (ch))
284 return name;
285 return 0;
287 #endif /* CONFIG_WITH_VALUE_LENGTH */
289 /* expand.c */
290 #ifndef CONFIG_WITH_VALUE_LENGTH
291 char *recursively_expand_for_file (struct variable *v, struct file *file);
292 #define recursively_expand(v) recursively_expand_for_file (v, NULL)
293 #else
294 char *recursively_expand_for_file (struct variable *v, struct file *file,
295 unsigned int *value_lenp);
296 #define recursively_expand(v) recursively_expand_for_file (v, NULL, NULL)
297 #endif
299 /* variable.c */
300 struct variable_set_list *create_new_variable_set (void);
301 void free_variable_set (struct variable_set_list *);
302 struct variable_set_list *push_new_variable_scope (void);
303 void pop_variable_scope (void);
304 void define_automatic_variables (void);
305 void initialize_file_variables (struct file *file, int reading);
306 void print_file_variables (const struct file *file);
307 void print_variable_set (struct variable_set *set, char *prefix);
308 void merge_variable_set_lists (struct variable_set_list **to_list,
309 struct variable_set_list *from_list);
310 #ifndef CONFIG_WITH_VALUE_LENGTH
311 struct variable *do_variable_definition (const struct floc *flocp,
312 const char *name, const char *value,
313 enum variable_origin origin,
314 enum variable_flavor flavor,
315 int target_var);
316 struct variable *parse_variable_definition (struct variable *v, char *line);
317 struct variable *try_variable_definition (const struct floc *flocp, char *line,
318 enum variable_origin origin,
319 int target_var);
320 #else /* CONFIG_WITH_VALUE_LENGTH */
321 # define do_variable_definition(flocp, varname, value, origin, flavor, target_var) \
322 do_variable_definition_2 ((flocp), (varname), (value), ~0U, 0, NULL, \
323 (origin), (flavor), (target_var))
325 struct variable *do_variable_definition_2 (const struct floc *flocp,
326 const char *varname,
327 const char *value,
328 unsigned int value_len,
329 int simple_value, char *free_value,
330 enum variable_origin origin,
331 enum variable_flavor flavor,
332 int target_var);
333 struct variable *parse_variable_definition (struct variable *v, char *line,
334 char *eos);
335 struct variable *try_variable_definition (const struct floc *flocp, char *line,
336 char *eos,
337 enum variable_origin origin,
338 int target_var);
339 #endif /* CONFIG_WITH_VALUE_LENGTH */
340 void init_hash_global_variable_set (void);
341 void hash_init_function_table (void);
342 struct variable *lookup_variable (const char *name, unsigned int length);
343 struct variable *lookup_variable_in_set (const char *name, unsigned int length,
344 const struct variable_set *set);
346 #ifdef CONFIG_WITH_VALUE_LENGTH
347 void append_string_to_variable (struct variable *v, const char *value,
348 unsigned int value_len, int append);
350 struct variable *define_variable_in_set (const char *name, unsigned int length,
351 const char *value,
352 unsigned int value_length,
353 int duplicate_value,
354 enum variable_origin origin,
355 int recursive,
356 struct variable_set *set,
357 const struct floc *flocp);
359 /* Define a variable in the current variable set. */
361 #define define_variable(n,l,v,o,r) \
362 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),\
363 current_variable_set_list->set,NILF)
365 #define define_variable_vl(n,l,v,vl,dv,o,r) \
366 define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),\
367 current_variable_set_list->set,NILF)
369 /* Define a variable with a location in the current variable set. */
371 #define define_variable_loc(n,l,v,o,r,f) \
372 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),\
373 current_variable_set_list->set,(f))
375 /* Define a variable with a location in the global variable set. */
377 #define define_variable_global(n,l,v,o,r,f) \
378 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),NULL,(f))
380 #define define_variable_vl_global(n,l,v,vl,dv,o,r,f) \
381 define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),NULL,(f))
383 /* Define a variable in FILE's variable set. */
385 #define define_variable_for_file(n,l,v,o,r,f) \
386 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),(f)->variables->set,NILF)
388 #else /* !CONFIG_WITH_VALUE_LENGTH */
390 struct variable *define_variable_in_set (const char *name, unsigned int length,
391 const char *value,
392 enum variable_origin origin,
393 int recursive,
394 struct variable_set *set,
395 const struct floc *flocp);
397 /* Define a variable in the current variable set. */
399 #define define_variable(n,l,v,o,r) \
400 define_variable_in_set((n),(l),(v),(o),(r),\
401 current_variable_set_list->set,NILF) /* force merge conflict */
403 /* Define a variable with a location in the current variable set. */
405 #define define_variable_loc(n,l,v,o,r,f) \
406 define_variable_in_set((n),(l),(v),(o),(r),\
407 current_variable_set_list->set,(f)) /* force merge conflict */
409 /* Define a variable with a location in the global variable set. */
411 #define define_variable_global(n,l,v,o,r,f) \
412 define_variable_in_set((n),(l),(v),(o),(r),NULL,(f)) /* force merge conflict */
414 /* Define a variable in FILE's variable set. */
416 #define define_variable_for_file(n,l,v,o,r,f) \
417 define_variable_in_set((n),(l),(v),(o),(r),(f)->variables->set,NILF) /* force merge conflict */
419 #endif /* !CONFIG_WITH_VALUE_LENGTH */
421 /* Warn that NAME is an undefined variable. */
423 #define warn_undefined(n,l) do{\
424 if (warn_undefined_variables_flag) \
425 error (reading_file, \
426 _("warning: undefined variable `%.*s'"), \
427 (int)(l), (n)); \
428 }while(0)
430 char **target_environment (struct file *file);
432 struct pattern_var *create_pattern_var (const char *target,
433 const char *suffix);
435 extern int export_all_variables;
436 #ifdef CONFIG_WITH_STRCACHE2
437 extern struct strcache2 variable_strcache;
438 #endif
440 #ifdef KMK
441 # define MAKELEVEL_NAME "KMK_LEVEL"
442 #else
443 #define MAKELEVEL_NAME "MAKELEVEL"
444 #endif
445 #define MAKELEVEL_LENGTH (sizeof (MAKELEVEL_NAME) - 1)