VirtualBox

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

Last change on this file since 2717 was 2717, checked in by bird, 11 years ago

kmk: Hacking kBuild-define-*.

  • Property svn:eol-style set to native
File size: 19.0 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)15) & ~(unsigned int)15 )
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 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 private_var:1; /* Nonzero avoids inheritance of this
85 target-specific variable. */
86 unsigned int exp_count:EXP_COUNT_BITS;
87 /* If >1, allow this many self-referential
88 expansions. */
89#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
90 unsigned int rdonly_val:1; /* VALUE is read only (strcache/const). */
91#endif
92 enum variable_flavor
93 flavor ENUM_BITFIELD (3); /* Variable flavor. */
94 enum variable_origin
95#ifdef CONFIG_WITH_LOCAL_VARIABLES
96 origin ENUM_BITFIELD (4); /* Variable origin. */
97#else
98 origin ENUM_BITFIELD (3); /* Variable origin. */
99#endif
100 enum variable_export
101 {
102 v_export, /* Export this variable. */
103 v_noexport, /* Don't export this variable. */
104 v_ifset, /* Export it if it has a non-default value. */
105 v_default /* Decide in target_environment. */
106 } export ENUM_BITFIELD (2);
107#ifdef CONFIG_WITH_MAKE_STATS
108 unsigned int changes;
109 unsigned int reallocs;
110#endif
111 };
112
113/* Structure that represents a variable set. */
114
115struct variable_set
116 {
117 struct hash_table table; /* Hash table of variables. */
118 };
119
120/* Structure that represents a list of variable sets. */
121
122struct variable_set_list
123 {
124 struct variable_set_list *next; /* Link in the chain. */
125 struct variable_set *set; /* Variable set. */
126 int next_is_parent; /* True if next is a parent target. */
127 };
128
129/* Structure used for pattern-specific variables. */
130
131struct pattern_var
132 {
133 struct pattern_var *next;
134 const char *suffix;
135 const char *target;
136 unsigned int len;
137 struct variable variable;
138 };
139
140extern char *variable_buffer;
141extern struct variable_set_list *current_variable_set_list;
142extern struct variable *default_goal_var;
143
144#ifdef KMK
145extern struct variable_set global_variable_set;
146extern struct variable_set_list global_setlist;
147extern unsigned int variable_buffer_length;
148# define VARIABLE_BUFFER_ZONE 5
149#endif
150
151/* expand.c */
152#ifndef KMK
153char *
154variable_buffer_output (char *ptr, const char *string, unsigned int length);
155#else /* KMK */
156/* Subroutine of variable_expand and friends:
157 The text to add is LENGTH chars starting at STRING to the variable_buffer.
158 The text is added to the buffer at PTR, and the updated pointer into
159 the buffer is returned as the value. Thus, the value returned by
160 each call to variable_buffer_output should be the first argument to
161 the following call. */
162
163__inline static char *
164variable_buffer_output (char *ptr, const char *string, unsigned int length)
165{
166 register unsigned int newlen = length + (ptr - variable_buffer);
167
168 if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
169 {
170 unsigned int offset = ptr - variable_buffer;
171 variable_buffer_length = variable_buffer_length <= 1024
172 ? 2048 : variable_buffer_length * 4;
173 if (variable_buffer_length < newlen + 100)
174 variable_buffer_length = (newlen + 100 + 1023) & ~1023U;
175 variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
176 ptr = variable_buffer + offset;
177 }
178
179# ifndef _MSC_VER
180 switch (length)
181 {
182 case 4: ptr[3] = string[3];
183 case 3: ptr[2] = string[2];
184 case 2: ptr[1] = string[1];
185 case 1: ptr[0] = string[0];
186 case 0:
187 break;
188 default:
189 memcpy (ptr, string, length);
190 break;
191 }
192# else
193 memcpy (ptr, string, length);
194# endif
195 return ptr + length;
196}
197
198#endif /* KMK */
199char *variable_expand (const char *line);
200char *variable_expand_for_file (const char *line, struct file *file);
201#if defined (CONFIG_WITH_VALUE_LENGTH) || defined (CONFIG_WITH_COMMANDS_FUNC)
202char *variable_expand_for_file_2 (char *o, const char *line, unsigned int lenght,
203 struct file *file, unsigned int *value_lenp);
204#endif
205char *allocated_variable_expand_for_file (const char *line, struct file *file);
206#ifndef CONFIG_WITH_VALUE_LENGTH
207#define allocated_variable_expand(line) \
208 allocated_variable_expand_for_file (line, (struct file *) 0)
209#else /* CONFIG_WITH_VALUE_LENGTH */
210# define allocated_variable_expand(line) \
211 allocated_variable_expand_2 (line, -1, NULL)
212char *allocated_variable_expand_2 (const char *line, unsigned int length, unsigned int *value_lenp);
213char *allocated_variable_expand_3 (const char *line, unsigned int length,
214 unsigned int *value_lenp, unsigned int *buffer_lengthp);
215void recycle_variable_buffer (char *buffer, unsigned int length);
216#endif /* CONFIG_WITH_VALUE_LENGTH */
217char *expand_argument (const char *str, const char *end);
218#ifndef CONFIG_WITH_VALUE_LENGTH
219char *
220variable_expand_string (char *line, const char *string, long length);
221#else /* CONFIG_WITH_VALUE_LENGTH */
222char *
223variable_expand_string_2 (char *line, const char *string, long length, char **eol);
224__inline static char *
225variable_expand_string (char *line, const char *string, long length)
226{
227 char *ignored;
228 return variable_expand_string_2 (line, string, length, &ignored);
229}
230#endif /* CONFIG_WITH_VALUE_LENGTH */
231void install_variable_buffer (char **bufp, unsigned int *lenp);
232void restore_variable_buffer (char *buf, unsigned int len);
233#ifdef CONFIG_WITH_VALUE_LENGTH
234void append_expanded_string_to_variable (struct variable *v, const char *value,
235 unsigned int value_len, int append);
236#endif
237
238/* function.c */
239#ifndef CONFIG_WITH_VALUE_LENGTH
240int handle_function (char **op, const char **stringp);
241#else
242int handle_function (char **op, const char **stringp, const char *nameend, const char *eol);
243#endif
244int pattern_matches (const char *pattern, const char *percent, const char *str);
245char *subst_expand (char *o, const char *text, const char *subst,
246 const char *replace, unsigned int slen, unsigned int rlen,
247 int by_word);
248char *patsubst_expand_pat (char *o, const char *text, const char *pattern,
249 const char *replace, const char *pattern_percent,
250 const char *replace_percent);
251char *patsubst_expand (char *o, const char *text, char *pattern, char *replace);
252#ifdef CONFIG_WITH_COMMANDS_FUNC
253char *func_commands (char *o, char **argv, const char *funcname);
254#endif
255#if defined (CONFIG_WITH_VALUE_LENGTH)
256/* Avoid calling handle_function for every variable, do the
257 basic checks in variable_expand_string_2. */
258extern char func_char_map[256];
259# define MAX_FUNCTION_LENGTH 12
260# define MIN_FUNCTION_LENGTH 2
261MY_INLINE const char *
262may_be_function_name (const char *name, const char *eos)
263{
264 unsigned char ch;
265 unsigned int len = name - eos;
266
267 /* Minimum length is MIN + whitespace. Check this directly.
268 ASSUMES: MIN_FUNCTION_LENGTH == 2 */
269
270 if (MY_PREDICT_TRUE(len < MIN_FUNCTION_LENGTH + 1
271 || !func_char_map[(int)(name[0])]
272 || !func_char_map[(int)(name[1])]))
273 return 0;
274 if (MY_PREDICT_TRUE(!func_char_map[ch = name[2]]))
275 return isspace (ch) ? name + 2 : 0;
276
277 name += 3;
278 if (len > MAX_FUNCTION_LENGTH)
279 len = MAX_FUNCTION_LENGTH - 3;
280 else if (len == 3)
281 len -= 3;
282 if (!len)
283 return 0;
284
285 /* Loop over the remaining possiblities. */
286
287 while (func_char_map[ch = *name])
288 {
289 if (!len--)
290 return 0;
291 name++;
292 }
293 if (ch == '\0' || isblank (ch))
294 return name;
295 return 0;
296}
297#endif /* CONFIG_WITH_VALUE_LENGTH */
298
299/* expand.c */
300#ifndef CONFIG_WITH_VALUE_LENGTH
301char *recursively_expand_for_file (struct variable *v, struct file *file);
302#define recursively_expand(v) recursively_expand_for_file (v, NULL)
303#else
304char *recursively_expand_for_file (struct variable *v, struct file *file,
305 unsigned int *value_lenp);
306#define recursively_expand(v) recursively_expand_for_file (v, NULL, NULL)
307#endif
308
309/* variable.c */
310struct variable_set_list *create_new_variable_set (void);
311void free_variable_set (struct variable_set_list *);
312struct variable_set_list *push_new_variable_scope (void);
313void pop_variable_scope (void);
314void define_automatic_variables (void);
315void initialize_file_variables (struct file *file, int reading);
316void print_file_variables (const struct file *file);
317void print_variable_set (struct variable_set *set, char *prefix);
318void merge_variable_set_lists (struct variable_set_list **to_list,
319 struct variable_set_list *from_list);
320#ifndef CONFIG_WITH_VALUE_LENGTH
321struct variable *do_variable_definition (const struct floc *flocp,
322 const char *name, const char *value,
323 enum variable_origin origin,
324 enum variable_flavor flavor,
325 int target_var);
326#else /* CONFIG_WITH_VALUE_LENGTH */
327# define do_variable_definition(flocp, varname, value, origin, flavor, target_var) \
328 do_variable_definition_2 ((flocp), (varname), (value), ~0U, 0, NULL, \
329 (origin), (flavor), (target_var))
330struct variable *do_variable_definition_2 (const struct floc *flocp,
331 const char *varname,
332 const char *value,
333 unsigned int value_len,
334 int simple_value, char *free_value,
335 enum variable_origin origin,
336 enum variable_flavor flavor,
337 int target_var);
338#endif /* CONFIG_WITH_VALUE_LENGTH */
339char *parse_variable_definition (const char *line,
340 enum variable_flavor *flavor);
341struct variable *assign_variable_definition (struct variable *v, char *line IF_WITH_VALUE_LENGTH_PARAM(char *eos));
342struct variable *try_variable_definition (const struct floc *flocp, char *line
343 IF_WITH_VALUE_LENGTH_PARAM(char *eos),
344 enum variable_origin origin,
345 int target_var);
346void init_hash_global_variable_set (void);
347void hash_init_function_table (void);
348struct variable *lookup_variable (const char *name, unsigned int length);
349struct variable *lookup_variable_in_set (const char *name, unsigned int length,
350 const struct variable_set *set);
351
352#ifdef CONFIG_WITH_VALUE_LENGTH
353void append_string_to_variable (struct variable *v, const char *value,
354 unsigned int value_len, int append);
355struct variable * do_variable_definition_append (const struct floc *flocp, struct variable *v,
356 const char *value, unsigned int value_len,
357 int simple_value, enum variable_origin origin,
358 int append);
359
360struct variable *define_variable_in_set (const char *name, unsigned int length,
361 const char *value,
362 unsigned int value_length,
363 int duplicate_value,
364 enum variable_origin origin,
365 int recursive,
366 struct variable_set *set,
367 const struct floc *flocp);
368
369/* Define a variable in the current variable set. */
370
371#define define_variable(n,l,v,o,r) \
372 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),\
373 current_variable_set_list->set,NILF)
374
375#define define_variable_vl(n,l,v,vl,dv,o,r) \
376 define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),\
377 current_variable_set_list->set,NILF)
378
379/* Define a variable with a constant name in the current variable set. */
380
381#define define_variable_cname(n,v,o,r) \
382 define_variable_in_set((n),(sizeof (n) - 1),(v),~0U,1,(o),(r),\
383 current_variable_set_list->set,NILF)
384
385/* Define a variable with a location in the current variable set. */
386
387#define define_variable_loc(n,l,v,o,r,f) \
388 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),\
389 current_variable_set_list->set,(f))
390
391/* Define a variable with a location in the global variable set. */
392
393#define define_variable_global(n,l,v,o,r,f) \
394 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),NULL,(f))
395
396#define define_variable_vl_global(n,l,v,vl,dv,o,r,f) \
397 define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),NULL,(f))
398
399/* Define a variable in FILE's variable set. */
400
401#define define_variable_for_file(n,l,v,o,r,f) \
402 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),(f)->variables->set,NILF)
403
404#else /* !CONFIG_WITH_VALUE_LENGTH */
405
406struct variable *define_variable_in_set (const char *name, unsigned int length,
407 const char *value,
408 enum variable_origin origin,
409 int recursive,
410 struct variable_set *set,
411 const struct floc *flocp);
412
413/* Define a variable in the current variable set. */
414
415#define define_variable(n,l,v,o,r) \
416 define_variable_in_set((n),(l),(v),(o),(r),\
417 current_variable_set_list->set,NILF) /* force merge conflict */
418
419/* Define a variable with a constant name in the current variable set. */
420
421#define define_variable_cname(n,v,o,r) \
422 define_variable_in_set((n),(sizeof (n) - 1),(v),(o),(r),\
423 current_variable_set_list->set,NILF) /* force merge conflict */
424
425/* Define a variable with a location in the current variable set. */
426
427#define define_variable_loc(n,l,v,o,r,f) \
428 define_variable_in_set((n),(l),(v),(o),(r),\
429 current_variable_set_list->set,(f)) /* force merge conflict */
430
431/* Define a variable with a location in the global variable set. */
432
433#define define_variable_global(n,l,v,o,r,f) \
434 define_variable_in_set((n),(l),(v),(o),(r),NULL,(f)) /* force merge conflict */
435
436/* Define a variable in FILE's variable set. */
437
438#define define_variable_for_file(n,l,v,o,r,f) \
439 define_variable_in_set((n),(l),(v),(o),(r),(f)->variables->set,NILF) /* force merge conflict */
440
441#endif /* !CONFIG_WITH_VALUE_LENGTH */
442
443void undefine_variable_in_set (const char *name, unsigned int length,
444 enum variable_origin origin,
445 struct variable_set *set);
446
447/* Remove variable from the current variable set. */
448
449#define undefine_variable_global(n,l,o) \
450 undefine_variable_in_set((n),(l),(o),NULL)
451
452/* Warn that NAME is an undefined variable. */
453
454#define warn_undefined(n,l) do{\
455 if (warn_undefined_variables_flag) \
456 error (reading_file, \
457 _("warning: undefined variable `%.*s'"), \
458 (int)(l), (n)); \
459 }while(0)
460
461char **target_environment (struct file *file);
462
463struct pattern_var *create_pattern_var (const char *target,
464 const char *suffix);
465
466extern int export_all_variables;
467#ifdef CONFIG_WITH_STRCACHE2
468extern struct strcache2 variable_strcache;
469#endif
470
471#ifdef KMK
472# define MAKELEVEL_NAME "KMK_LEVEL"
473#else
474#define MAKELEVEL_NAME "MAKELEVEL"
475#endif
476#define MAKELEVEL_LENGTH (sizeof (MAKELEVEL_NAME) - 1)
477
Note: See TracBrowser for help on using the repository browser.

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