VirtualBox

source: kBuild/trunk/src/kmk/variable.h@ 1997

Last change on this file since 1997 was 1997, checked in by bird, 16 years ago

variaiable::value_alloc_len -> unsigned int.

  • Property svn:eol-style set to native
File size: 17.5 KB
Line 
1/* Definitions for using variables in GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
4Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 3 of the License, or (at your option) any later
10version.
11
12GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License along with
17this program. If not, see <http://www.gnu.org/licenses/>. */
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. */
23enum 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#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. */
36 };
37
38enum variable_flavor
39 {
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 (?=) */
48 };
49
50/* Structure that represents one variable definition.
51 Each bucket of the hash table is a chain of these,
52 chained through `next'. */
53
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)31) & ~(unsigned int)31 )
58#endif
59
60struct variable
61 {
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 int value_length; /* The length of the value, usually unused. */
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
99 {
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 };
106
107/* Structure that represents a variable set. */
108
109struct variable_set
110 {
111 struct hash_table table; /* Hash table of variables. */
112 };
113
114/* Structure that represents a list of variable sets. */
115
116struct variable_set_list
117 {
118 struct variable_set_list *next; /* Link in the chain. */
119 struct variable_set *set; /* Variable set. */
120 };
121
122/* Structure used for pattern-specific variables. */
123
124struct pattern_var
125 {
126 struct pattern_var *next;
127 const char *suffix;
128 const char *target;
129 unsigned int len;
130 struct variable variable;
131 };
132
133extern char *variable_buffer;
134extern struct variable_set_list *current_variable_set_list;
135#ifdef KMK
136extern unsigned int variable_buffer_length;
137#define VARIABLE_BUFFER_ZONE 5
138#endif
139
140/* expand.c */
141#ifndef KMK
142char *variable_buffer_output (char *ptr, const char *string, unsigned int length);
143#else /* KMK */
144/* Subroutine of variable_expand and friends:
145 The text to add is LENGTH chars starting at STRING to the variable_buffer.
146 The text is added to the buffer at PTR, and the updated pointer into
147 the buffer is returned as the value. Thus, the value returned by
148 each call to variable_buffer_output should be the first argument to
149 the following call. */
150
151__inline static char *
152variable_buffer_output (char *ptr, const char *string, unsigned int length)
153{
154 register unsigned int newlen = length + (ptr - variable_buffer);
155
156 if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
157 {
158 unsigned int offset = ptr - variable_buffer;
159 variable_buffer_length = variable_buffer_length <= 1024
160 ? 2048 : variable_buffer_length * 4;
161 if (variable_buffer_length < newlen + 100)
162 variable_buffer_length = (newlen + 100 + 1023) & ~1023U;
163 variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
164 ptr = variable_buffer + offset;
165 }
166
167# ifndef _MSC_VER
168 switch (length)
169 {
170 case 4: ptr[3] = string[3];
171 case 3: ptr[2] = string[2];
172 case 2: ptr[1] = string[1];
173 case 1: ptr[0] = string[0];
174 case 0:
175 break;
176 default:
177 memcpy (ptr, string, length);
178 break;
179 }
180# else
181 memcpy (ptr, string, length);
182# endif
183 return ptr + length;
184}
185
186#endif /* KMK */
187char *variable_expand (const char *line);
188char *variable_expand_for_file (const char *line, struct file *file);
189#if defined (CONFIG_WITH_VALUE_LENGTH) || defined (CONFIG_WITH_COMMANDS_FUNC)
190char *variable_expand_for_file_2 (char *o, const char *line, unsigned int lenght,
191 struct file *file, unsigned int *value_lenp);
192#endif
193char *allocated_variable_expand_for_file (const char *line, struct file *file);
194#ifndef CONFIG_WITH_VALUE_LENGTH
195#define allocated_variable_expand(line) \
196 allocated_variable_expand_for_file (line, (struct file *) 0)
197#else /* CONFIG_WITH_VALUE_LENGTH */
198# define allocated_variable_expand(line) \
199 allocated_variable_expand_2 (line, -1, NULL)
200char *allocated_variable_expand_2 (const char *line, unsigned int length, unsigned int *value_lenp);
201char *allocated_variable_expand_3 (const char *line, unsigned int length,
202 unsigned int *value_lenp, unsigned int *buffer_lengthp);
203void recycle_variable_buffer (char *buffer, unsigned int length);
204#endif /* CONFIG_WITH_VALUE_LENGTH */
205char *expand_argument (const char *str, const char *end);
206#ifndef CONFIG_WITH_VALUE_LENGTH
207char *variable_expand_string (char *line, const char *string, long length);
208#else /* CONFIG_WITH_VALUE_LENGTH */
209char *variable_expand_string_2 (char *line, const char *string, long length, char **eol);
210__inline static char *
211variable_expand_string (char *line, const char *string, long length)
212{
213 char *ignored;
214 return variable_expand_string_2 (line, string, length, &ignored);
215}
216#endif /* CONFIG_WITH_VALUE_LENGTH */
217void install_variable_buffer (char **bufp, unsigned int *lenp);
218void restore_variable_buffer (char *buf, unsigned int len);
219#ifdef CONFIG_WITH_VALUE_LENGTH
220void append_expanded_string_to_variable (struct variable *v, const char *value,
221 unsigned int value_len, int append);
222#endif
223
224/* function.c */
225#ifndef CONFIG_WITH_VALUE_LENGTH
226int handle_function (char **op, const char **stringp);
227#else
228int handle_function (char **op, const char **stringp, const char *nameend, const char *eol);
229#endif
230int pattern_matches (const char *pattern, const char *percent, const char *str);
231char *subst_expand (char *o, const char *text, const char *subst,
232 const char *replace, unsigned int slen, unsigned int rlen,
233 int by_word);
234char *patsubst_expand_pat (char *o, const char *text, const char *pattern,
235 const char *replace, const char *pattern_percent,
236 const char *replace_percent);
237char *patsubst_expand (char *o, const char *text, char *pattern, char *replace);
238#ifdef CONFIG_WITH_COMMANDS_FUNC
239char *func_commands (char *o, char **argv, const char *funcname);
240#endif
241#if defined (CONFIG_WITH_VALUE_LENGTH)
242/* Avoid calling handle_function for every variable, do the
243 basic checks in variable_expand_string_2. */
244extern char func_char_map[256];
245# define MAX_FUNCTION_LENGTH 12
246# define MIN_FUNCTION_LENGTH 2
247MY_INLINE const char *
248may_be_function_name (const char *name, const char *eos)
249{
250 unsigned char ch;
251 unsigned int len = name - eos;
252
253 /* Minimum length is MIN + whitespace. Check this directly.
254 ASSUMES: MIN_FUNCTION_LENGTH == 2 */
255
256 if (MY_PREDICT_TRUE(len < MIN_FUNCTION_LENGTH + 1
257 || !func_char_map[(int)(name[0])]
258 || !func_char_map[(int)(name[1])]))
259 return 0;
260 if (MY_PREDICT_TRUE(!func_char_map[ch = name[2]]))
261 return isspace (ch) ? name + 2 : 0;
262
263 name += 3;
264 if (len > MAX_FUNCTION_LENGTH)
265 len = MAX_FUNCTION_LENGTH - 3;
266 else if (len == 3)
267 len -= 3;
268 if (!len)
269 return 0;
270
271 /* Loop over the remaining possiblities. */
272
273 while (func_char_map[ch = *name])
274 {
275 if (!len--)
276 return 0;
277 name++;
278 }
279 if (ch == '\0' || isblank (ch))
280 return name;
281 return 0;
282}
283#endif /* CONFIG_WITH_VALUE_LENGTH */
284
285/* expand.c */
286#ifndef CONFIG_WITH_VALUE_LENGTH
287char *recursively_expand_for_file (struct variable *v, struct file *file);
288#define recursively_expand(v) recursively_expand_for_file (v, NULL)
289#else
290char *recursively_expand_for_file (struct variable *v, struct file *file,
291 unsigned int *value_lenp);
292#define recursively_expand(v) recursively_expand_for_file (v, NULL, NULL)
293#endif
294
295/* variable.c */
296struct variable_set_list *create_new_variable_set (void);
297void free_variable_set (struct variable_set_list *);
298struct variable_set_list *push_new_variable_scope (void);
299void pop_variable_scope (void);
300void define_automatic_variables (void);
301void initialize_file_variables (struct file *file, int reading);
302void print_file_variables (const struct file *file);
303void print_variable_set (struct variable_set *set, char *prefix);
304void merge_variable_set_lists (struct variable_set_list **to_list,
305 struct variable_set_list *from_list);
306#ifndef CONFIG_WITH_VALUE_LENGTH
307struct variable *do_variable_definition (const struct floc *flocp,
308 const char *name, const char *value,
309 enum variable_origin origin,
310 enum variable_flavor flavor,
311 int target_var);
312struct variable *parse_variable_definition (struct variable *v, char *line);
313struct variable *try_variable_definition (const struct floc *flocp, char *line,
314 enum variable_origin origin,
315 int target_var);
316#else /* CONFIG_WITH_VALUE_LENGTH */
317# define do_variable_definition(flocp, varname, value, origin, flavor, target_var) \
318 do_variable_definition_2 ((flocp), (varname), (value), ~0U, 0, NULL, \
319 (origin), (flavor), (target_var))
320
321struct variable *do_variable_definition_2 (const struct floc *flocp,
322 const char *varname,
323 const char *value,
324 unsigned int value_len,
325 int simple_value, char *free_value,
326 enum variable_origin origin,
327 enum variable_flavor flavor,
328 int target_var);
329struct variable *parse_variable_definition (struct variable *v, char *line,
330 char *eos);
331struct variable *try_variable_definition (const struct floc *flocp, char *line,
332 char *eos,
333 enum variable_origin origin,
334 int target_var);
335#endif /* CONFIG_WITH_VALUE_LENGTH */
336void init_hash_global_variable_set (void);
337void hash_init_function_table (void);
338struct variable *lookup_variable (const char *name, unsigned int length);
339struct variable *lookup_variable_in_set (const char *name, unsigned int length,
340 const struct variable_set *set);
341
342#ifdef CONFIG_WITH_VALUE_LENGTH
343void append_string_to_variable (struct variable *v, const char *value,
344 unsigned int value_len, int append);
345
346struct variable *define_variable_in_set (const char *name, unsigned int length,
347 const char *value,
348 unsigned int value_length,
349 int duplicate_value,
350 enum variable_origin origin,
351 int recursive,
352 struct variable_set *set,
353 const struct floc *flocp);
354
355/* Define a variable in the current variable set. */
356
357#define define_variable(n,l,v,o,r) \
358 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),\
359 current_variable_set_list->set,NILF)
360
361#define define_variable_vl(n,l,v,vl,dv,o,r) \
362 define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),\
363 current_variable_set_list->set,NILF)
364
365/* Define a variable with a location in the current variable set. */
366
367#define define_variable_loc(n,l,v,o,r,f) \
368 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),\
369 current_variable_set_list->set,(f))
370
371/* Define a variable with a location in the global variable set. */
372
373#define define_variable_global(n,l,v,o,r,f) \
374 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),NULL,(f))
375
376#define define_variable_vl_global(n,l,v,vl,dv,o,r,f) \
377 define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),NULL,(f))
378
379/* Define a variable in FILE's variable set. */
380
381#define define_variable_for_file(n,l,v,o,r,f) \
382 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),(f)->variables->set,NILF)
383
384#else /* !CONFIG_WITH_VALUE_LENGTH */
385
386struct variable *define_variable_in_set (const char *name, unsigned int length,
387 const char *value,
388 enum variable_origin origin,
389 int recursive,
390 struct variable_set *set,
391 const struct floc *flocp);
392
393/* Define a variable in the current variable set. */
394
395#define define_variable(n,l,v,o,r) \
396 define_variable_in_set((n),(l),(v),(o),(r),\
397 current_variable_set_list->set,NILF) /* force merge conflict */
398
399/* Define a variable with a location in the current variable set. */
400
401#define define_variable_loc(n,l,v,o,r,f) \
402 define_variable_in_set((n),(l),(v),(o),(r),\
403 current_variable_set_list->set,(f)) /* force merge conflict */
404
405/* Define a variable with a location in the global variable set. */
406
407#define define_variable_global(n,l,v,o,r,f) \
408 define_variable_in_set((n),(l),(v),(o),(r),NULL,(f)) /* force merge conflict */
409
410/* Define a variable in FILE's variable set. */
411
412#define define_variable_for_file(n,l,v,o,r,f) \
413 define_variable_in_set((n),(l),(v),(o),(r),(f)->variables->set,NILF) /* force merge conflict */
414
415#endif /* !CONFIG_WITH_VALUE_LENGTH */
416
417/* Warn that NAME is an undefined variable. */
418
419#define warn_undefined(n,l) do{\
420 if (warn_undefined_variables_flag) \
421 error (reading_file, \
422 _("warning: undefined variable `%.*s'"), \
423 (int)(l), (n)); \
424 }while(0)
425
426char **target_environment (struct file *file);
427
428struct pattern_var *create_pattern_var (const char *target,
429 const char *suffix);
430
431extern int export_all_variables;
432#ifdef CONFIG_WITH_STRCACHE2
433extern struct strcache2 variable_strcache;
434#endif
435
436#ifdef KMK
437# define MAKELEVEL_NAME "KMK_LEVEL"
438#else
439#define MAKELEVEL_NAME "MAKELEVEL"
440#endif
441#define MAKELEVEL_LENGTH (sizeof (MAKELEVEL_NAME) - 1)
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette