VirtualBox

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

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

String expansion 'compilation' and 'execution' code is mostly done.

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