VirtualBox

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

Last change on this file since 2657 was 2591, checked in by bird, 13 years ago

kmk: Merged in changes from GNU make 3.82. Previous GNU make base version was gnumake-2008-10-28-CVS.

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