VirtualBox

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

Last change on this file since 1956 was 1946, checked in by bird, 17 years ago

kmk: Caught a variable::value update that I'd missed when implementing CONFIG_WITH_VALUE_LENGTH.

  • Property svn:eol-style set to native
File size: 17.3 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 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 2, or (at your option) any later version.
10
11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16GNU Make; see the file COPYING. If not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
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
57struct variable
58 {
59#ifndef CONFIG_WITH_STRCACHE2
60 char *name; /* Variable name. */
61#else
62 const char *name; /* Variable name (in varaible_strcache). */
63#endif
64 int length; /* strlen (name) */
65#ifdef CONFIG_WITH_VALUE_LENGTH
66 int value_length; /* The length of the value, usually unused. */
67 int value_alloc_len; /* The amount of memory we've actually allocated. */
68 /* FIXME: make lengths unsigned! */
69#endif
70 char *value; /* Variable value. */
71 struct floc fileinfo; /* Where the variable was defined. */
72 unsigned int recursive:1; /* Gets recursively re-evaluated. */
73 unsigned int append:1; /* Nonzero if an appending target-specific
74 variable. */
75 unsigned int conditional:1; /* Nonzero if set with a ?=. */
76 unsigned int per_target:1; /* Nonzero if a target-specific variable. */
77 unsigned int special:1; /* Nonzero if this is a special variable. */
78 unsigned int exportable:1; /* Nonzero if the variable _could_ be
79 exported. */
80 unsigned int expanding:1; /* Nonzero if currently being expanded. */
81 unsigned int exp_count:EXP_COUNT_BITS;
82 /* If >1, allow this many self-referential
83 expansions. */
84#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
85 unsigned int rdonly_val:1; /* VALUE is read only (strcache/const). */
86#endif
87 enum variable_flavor
88 flavor ENUM_BITFIELD (3); /* Variable flavor. */
89 enum variable_origin
90#ifdef CONFIG_WITH_LOCAL_VARIABLES
91 origin ENUM_BITFIELD (4); /* Variable origin. */
92#else
93 origin ENUM_BITFIELD (3); /* Variable origin. */
94#endif
95 enum variable_export
96 {
97 v_export, /* Export this variable. */
98 v_noexport, /* Don't export this variable. */
99 v_ifset, /* Export it if it has a non-default value. */
100 v_default /* Decide in target_environment. */
101 } export ENUM_BITFIELD (2);
102 };
103
104/* Structure that represents a variable set. */
105
106struct variable_set
107 {
108 struct hash_table table; /* Hash table of variables. */
109 };
110
111/* Structure that represents a list of variable sets. */
112
113struct variable_set_list
114 {
115 struct variable_set_list *next; /* Link in the chain. */
116 struct variable_set *set; /* Variable set. */
117 };
118
119/* Structure used for pattern-specific variables. */
120
121struct pattern_var
122 {
123 struct pattern_var *next;
124 const char *suffix;
125 const char *target;
126 unsigned int len;
127 struct variable variable;
128 };
129
130extern char *variable_buffer;
131extern struct variable_set_list *current_variable_set_list;
132#ifdef KMK
133extern unsigned int variable_buffer_length;
134#define VARIABLE_BUFFER_ZONE 5
135#endif
136
137/* expand.c */
138#ifndef KMK
139char *variable_buffer_output (char *ptr, const char *string, unsigned int length);
140#else /* KMK */
141/* Subroutine of variable_expand and friends:
142 The text to add is LENGTH chars starting at STRING to the variable_buffer.
143 The text is added to the buffer at PTR, and the updated pointer into
144 the buffer is returned as the value. Thus, the value returned by
145 each call to variable_buffer_output should be the first argument to
146 the following call. */
147
148__inline static char *
149variable_buffer_output (char *ptr, const char *string, unsigned int length)
150{
151 register unsigned int newlen = length + (ptr - variable_buffer);
152
153 if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
154 {
155 unsigned int offset = ptr - variable_buffer;
156 variable_buffer_length = variable_buffer_length <= 1024
157 ? 2048 : variable_buffer_length * 4;
158 if (variable_buffer_length < newlen + 100)
159 variable_buffer_length = (newlen + 100 + 1023) & ~1023U;
160 variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
161 ptr = variable_buffer + offset;
162 }
163
164# ifndef _MSC_VER
165 switch (length)
166 {
167 case 4: ptr[3] = string[3];
168 case 3: ptr[2] = string[2];
169 case 2: ptr[1] = string[1];
170 case 1: ptr[0] = string[0];
171 case 0:
172 break;
173 default:
174 memcpy (ptr, string, length);
175 break;
176 }
177# else
178 memcpy (ptr, string, length);
179# endif
180 return ptr + length;
181}
182
183#endif /* KMK */
184char *variable_expand (const char *line);
185char *variable_expand_for_file (const char *line, struct file *file);
186#ifdef CONFIG_WITH_COMMANDS_FUNC
187char *variable_expand_for_file_2 (char *o, const char *line, unsigned int lenght,
188 struct file *file, unsigned int *value_lenp);
189#endif
190char *allocated_variable_expand_for_file (const char *line, struct file *file);
191#ifndef CONFIG_WITH_VALUE_LENGTH
192#define allocated_variable_expand(line) \
193 allocated_variable_expand_for_file (line, (struct file *) 0)
194#else /* CONFIG_WITH_VALUE_LENGTH */
195# define allocated_variable_expand(line) \
196 allocated_variable_expand_2 (line, -1, NULL)
197char *allocated_variable_expand_2 (const char *line, unsigned int length, unsigned int *value_lenp);
198char *allocated_variable_expand_3 (const char *line, unsigned int length,
199 unsigned int *value_lenp, unsigned int *buffer_lengthp);
200void recycle_variable_buffer (char *buffer, unsigned int length);
201#endif /* CONFIG_WITH_VALUE_LENGTH */
202char *expand_argument (const char *str, const char *end);
203#ifndef CONFIG_WITH_VALUE_LENGTH
204char *variable_expand_string (char *line, const char *string, long length);
205#else /* CONFIG_WITH_VALUE_LENGTH */
206char *variable_expand_string_2 (char *line, const char *string, long length, char **eol);
207__inline static char *
208variable_expand_string (char *line, const char *string, long length)
209{
210 char *ignored;
211 return variable_expand_string_2 (line, string, length, &ignored);
212}
213#endif /* CONFIG_WITH_VALUE_LENGTH */
214void install_variable_buffer (char **bufp, unsigned int *lenp);
215void restore_variable_buffer (char *buf, unsigned int len);
216#ifdef CONFIG_WITH_VALUE_LENGTH
217void append_expanded_string_to_variable (struct variable *v, const char *value,
218 unsigned int value_len, int append);
219#endif
220
221/* function.c */
222#ifndef CONFIG_WITH_VALUE_LENGTH
223int handle_function (char **op, const char **stringp);
224#else
225int handle_function (char **op, const char **stringp, const char *nameend, const char *eol);
226#endif
227int pattern_matches (const char *pattern, const char *percent, const char *str);
228char *subst_expand (char *o, const char *text, const char *subst,
229 const char *replace, unsigned int slen, unsigned int rlen,
230 int by_word);
231char *patsubst_expand_pat (char *o, const char *text, const char *pattern,
232 const char *replace, const char *pattern_percent,
233 const char *replace_percent);
234char *patsubst_expand (char *o, const char *text, char *pattern, char *replace);
235#ifdef CONFIG_WITH_COMMANDS_FUNC
236char *func_commands (char *o, char **argv, const char *funcname);
237#endif
238#if defined (CONFIG_WITH_VALUE_LENGTH)
239/* Avoid calling handle_function for every variable, do the
240 basic checks in variable_expand_string_2. */
241extern char func_char_map[256];
242# define MAX_FUNCTION_LENGTH 12
243# define MIN_FUNCTION_LENGTH 2
244MY_INLINE const char *
245may_be_function_name (const char *name, const char *eos)
246{
247 unsigned char ch;
248 unsigned int len = name - eos;
249
250 /* Minimum length is MIN + whitespace. Check this directly.
251 ASSUMES: MIN_FUNCTION_LENGTH == 2 */
252
253 if (MY_PREDICT_TRUE(len < MIN_FUNCTION_LENGTH + 1
254 || !func_char_map[(int)(name[0])]
255 || !func_char_map[(int)(name[1])]))
256 return 0;
257 if (MY_PREDICT_TRUE(!func_char_map[ch = name[2]]))
258 return isspace (ch) ? name + 2 : 0;
259
260 name += 3;
261 if (len > MAX_FUNCTION_LENGTH)
262 len = MAX_FUNCTION_LENGTH - 3;
263 else if (len == 3)
264 len -= 3;
265 if (!len)
266 return 0;
267
268 /* Loop over the remaining possiblities. */
269
270 while (func_char_map[ch = *name])
271 {
272 if (!len--)
273 return 0;
274 name++;
275 }
276 if (ch == '\0' || isblank (ch))
277 return name;
278 return 0;
279}
280#endif /* CONFIG_WITH_VALUE_LENGTH */
281
282/* expand.c */
283#ifndef CONFIG_WITH_VALUE_LENGTH
284char *recursively_expand_for_file (struct variable *v, struct file *file);
285#define recursively_expand(v) recursively_expand_for_file (v, NULL)
286#else
287char *recursively_expand_for_file (struct variable *v, struct file *file,
288 unsigned int *value_lenp);
289#define recursively_expand(v) recursively_expand_for_file (v, NULL, NULL)
290#endif
291
292/* variable.c */
293struct variable_set_list *create_new_variable_set (void);
294void free_variable_set (struct variable_set_list *);
295struct variable_set_list *push_new_variable_scope (void);
296void pop_variable_scope (void);
297void define_automatic_variables (void);
298void initialize_file_variables (struct file *file, int reading);
299void print_file_variables (const struct file *file);
300void print_variable_set (struct variable_set *set, char *prefix);
301void merge_variable_set_lists (struct variable_set_list **to_list,
302 struct variable_set_list *from_list);
303#ifndef CONFIG_WITH_VALUE_LENGTH
304struct variable *do_variable_definition (const struct floc *flocp,
305 const char *name, const char *value,
306 enum variable_origin origin,
307 enum variable_flavor flavor,
308 int target_var);
309struct variable *parse_variable_definition (struct variable *v, char *line);
310struct variable *try_variable_definition (const struct floc *flocp, char *line,
311 enum variable_origin origin,
312 int target_var);
313#else /* CONFIG_WITH_VALUE_LENGTH */
314# define do_variable_definition(flocp, varname, value, origin, flavor, target_var) \
315 do_variable_definition_2 ((flocp), (varname), (value), ~0U, 0, NULL, \
316 (origin), (flavor), (target_var))
317
318struct variable *do_variable_definition_2 (const struct floc *flocp,
319 const char *varname,
320 const char *value,
321 unsigned int value_len,
322 int simple_value, char *free_value,
323 enum variable_origin origin,
324 enum variable_flavor flavor,
325 int target_var);
326struct variable *parse_variable_definition (struct variable *v, char *line,
327 char *eos);
328struct variable *try_variable_definition (const struct floc *flocp, char *line,
329 char *eos,
330 enum variable_origin origin,
331 int target_var);
332#endif /* CONFIG_WITH_VALUE_LENGTH */
333void init_hash_global_variable_set (void);
334void hash_init_function_table (void);
335struct variable *lookup_variable (const char *name, unsigned int length);
336struct variable *lookup_variable_in_set (const char *name, unsigned int length,
337 const struct variable_set *set);
338
339#ifdef CONFIG_WITH_VALUE_LENGTH
340void append_string_to_variable (struct variable *v, const char *value,
341 unsigned int value_len, int append);
342
343struct variable *define_variable_in_set (const char *name, unsigned int length,
344 const char *value,
345 unsigned int value_length,
346 int duplicate_value,
347 enum variable_origin origin,
348 int recursive,
349 struct variable_set *set,
350 const struct floc *flocp);
351
352/* Define a variable in the current variable set. */
353
354#define define_variable(n,l,v,o,r) \
355 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),\
356 current_variable_set_list->set,NILF)
357
358#define define_variable_vl(n,l,v,vl,dv,o,r) \
359 define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),\
360 current_variable_set_list->set,NILF)
361
362/* Define a variable with a location in the current variable set. */
363
364#define define_variable_loc(n,l,v,o,r,f) \
365 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),\
366 current_variable_set_list->set,(f))
367
368/* Define a variable with a location in the global variable set. */
369
370#define define_variable_global(n,l,v,o,r,f) \
371 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),NULL,(f))
372
373#define define_variable_vl_global(n,l,v,vl,dv,o,r,f) \
374 define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),NULL,(f))
375
376/* Define a variable in FILE's variable set. */
377
378#define define_variable_for_file(n,l,v,o,r,f) \
379 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),(f)->variables->set,NILF)
380
381#else /* !CONFIG_WITH_VALUE_LENGTH */
382
383struct variable *define_variable_in_set (const char *name, unsigned int length,
384 const char *value,
385 enum variable_origin origin,
386 int recursive,
387 struct variable_set *set,
388 const struct floc *flocp);
389
390/* Define a variable in the current variable set. */
391
392#define define_variable(n,l,v,o,r) \
393 define_variable_in_set((n),(l),(v),(o),(r),\
394 current_variable_set_list->set,NILF) /* force merge conflict */
395
396/* Define a variable with a location in the current variable set. */
397
398#define define_variable_loc(n,l,v,o,r,f) \
399 define_variable_in_set((n),(l),(v),(o),(r),\
400 current_variable_set_list->set,(f)) /* force merge conflict */
401
402/* Define a variable with a location in the global variable set. */
403
404#define define_variable_global(n,l,v,o,r,f) \
405 define_variable_in_set((n),(l),(v),(o),(r),NULL,(f)) /* force merge conflict */
406
407/* Define a variable in FILE's variable set. */
408
409#define define_variable_for_file(n,l,v,o,r,f) \
410 define_variable_in_set((n),(l),(v),(o),(r),(f)->variables->set,NILF) /* force merge conflict */
411
412#endif /* !CONFIG_WITH_VALUE_LENGTH */
413
414/* Warn that NAME is an undefined variable. */
415
416#define warn_undefined(n,l) do{\
417 if (warn_undefined_variables_flag) \
418 error (reading_file, \
419 _("warning: undefined variable `%.*s'"), \
420 (int)(l), (n)); \
421 }while(0)
422
423char **target_environment (struct file *file);
424
425struct pattern_var *create_pattern_var (const char *target,
426 const char *suffix);
427
428extern int export_all_variables;
429#ifdef CONFIG_WITH_STRCACHE2
430extern struct strcache2 variable_strcache;
431#endif
432
433#ifdef KMK
434# define MAKELEVEL_NAME "KMK_LEVEL"
435#else
436#define MAKELEVEL_NAME "MAKELEVEL"
437#endif
438#define MAKELEVEL_LENGTH (sizeof (MAKELEVEL_NAME) - 1)
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