VirtualBox

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

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

Some more variable stats I added the other day (not in release builds).

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