VirtualBox

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

Last change on this file since 2752 was 2752, checked in by bird, 10 years ago

Added another variable statistic under #ifdef CONFIG_WITH_MAKE_STATS and made the makefile check for CONFIG_WITH_MAKE_STATS (instead of only enabling it for debug builds).

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