source: trunk/src/kmk/variable.h@ 1931

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

kmk: recycle more allocate_variable_expand_* results.

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