VirtualBox

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

Last change on this file since 3387 was 3140, checked in by bird, 6 years ago

kmk: Merged in changes from GNU make 4.2.1 (2e55f5e4abdc0e38c1d64be703b446695e70b3b6 / https://git.savannah.gnu.org/git/make.git).

  • Property svn:eol-style set to native
File size: 22.5 KB
Line 
1/* Definitions for using variables in GNU Make.
2Copyright (C) 1988-2016 Free Software Foundation, Inc.
3This file is part of GNU Make.
4
5GNU Make is free software; you can redistribute it and/or modify it under the
6terms of the GNU General Public License as published by the Free Software
7Foundation; either version 3 of the License, or (at your option) any later
8version.
9
10GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this program. If not, see <http://www.gnu.org/licenses/>. */
16
17#include "hash.h"
18#ifdef CONFIG_WITH_COMPILER
19# include "kmk_cc_exec.h"
20#endif
21
22/* Codes in a variable definition saying where the definition came from.
23 Increasing numeric values signify less-overridable definitions. */
24enum variable_origin
25 {
26 o_default, /* Variable from the default set. */
27 o_env, /* Variable from environment. */
28 o_file, /* Variable given in a makefile. */
29 o_env_override, /* Variable from environment, if -e. */
30 o_command, /* Variable given by user. */
31 o_override, /* Variable from an 'override' directive. */
32#ifdef CONFIG_WITH_LOCAL_VARIABLES
33 o_local, /* Variable from an 'local' directive. */
34#endif
35 o_automatic, /* Automatic variable -- cannot be set. */
36 o_invalid /* Core dump time. */
37 };
38
39enum variable_flavor
40 {
41 f_bogus, /* Bogus (error) */
42 f_simple, /* Simple definition (:= or ::=) */
43 f_recursive, /* Recursive definition (=) */
44 f_append, /* Appending definition (+=) */
45#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
46 f_prepend, /* Prepending definition (>=) */
47#endif
48 f_conditional, /* Conditional definition (?=) */
49 f_shell /* Shell assignment (!=) */
50 };
51
52/* Structure that represents one variable definition.
53 Each bucket of the hash table is a chain of these,
54 chained through 'next'. */
55
56#define EXP_COUNT_BITS 15 /* This gets all the bitfields into 32 bits */
57#define EXP_COUNT_MAX ((1<<EXP_COUNT_BITS)-1)
58#ifdef CONFIG_WITH_VALUE_LENGTH
59# define VAR_ALIGN_VALUE_ALLOC(len) ( ((len) + (unsigned int)15) & ~(unsigned int)15 )
60#endif
61
62struct variable
63 {
64#ifndef CONFIG_WITH_STRCACHE2
65 char *name; /* Variable name. */
66#else
67 const char *name; /* Variable name (in varaible_strcache). */
68#endif
69 char *value; /* Variable value. */
70 floc fileinfo; /* Where the variable was defined. */
71 int length; /* strlen (name) */
72#ifdef CONFIG_WITH_VALUE_LENGTH
73 unsigned int value_length; /* The length of the value. */
74 unsigned int value_alloc_len; /* The amount of memory we've actually allocated. */
75 /* FIXME: make lengths unsigned! */
76#endif
77 unsigned int recursive:1; /* Gets recursively re-evaluated. */
78 unsigned int append:1; /* Nonzero if an appending target-specific
79 variable. */
80 unsigned int conditional:1; /* Nonzero if set with a ?=. */
81 unsigned int per_target:1; /* Nonzero if a target-specific variable. */
82 unsigned int special:1; /* Nonzero if this is a special variable. */
83 unsigned int exportable:1; /* Nonzero if the variable _could_ be
84 exported. */
85 unsigned int expanding:1; /* Nonzero if currently being expanded. */
86 unsigned int private_var:1; /* Nonzero avoids inheritance of this
87 target-specific variable. */
88 unsigned int exp_count:EXP_COUNT_BITS;
89 /* If >1, allow this many self-referential
90 expansions. */
91#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
92 unsigned int rdonly_val:1; /* VALUE is read only (strcache/const). */
93#endif
94#ifdef KMK
95 unsigned int alias:1; /* Nonzero if alias. VALUE points to the real variable. */
96 unsigned int aliased:1; /* Nonzero if aliased. Cannot be undefined. */
97#endif
98 enum variable_flavor
99 flavor ENUM_BITFIELD (3); /* Variable flavor. */
100 enum variable_origin
101#ifdef CONFIG_WITH_LOCAL_VARIABLES
102 origin ENUM_BITFIELD (4); /* Variable origin. */
103#else
104 origin ENUM_BITFIELD (3); /* Variable origin. */
105#endif
106 enum variable_export
107 {
108 v_export, /* Export this variable. */
109 v_noexport, /* Don't export this variable. */
110 v_ifset, /* Export it if it has a non-default value. */
111 v_default /* Decide in target_environment. */
112 } export ENUM_BITFIELD (2);
113#ifdef CONFIG_WITH_COMPILER
114 int recursive_without_dollar : 2; /* 0 if undetermined, 1 if value has no '$' chars, -1 if it has. */
115#endif
116#ifdef CONFIG_WITH_MAKE_STATS
117 unsigned int changes; /* Variable modification count. */
118 unsigned int reallocs; /* Realloc on value count. */
119 unsigned int references; /* Lookup count. */
120 unsigned long long cTicksEvalVal; /* Number of ticks spend in cEvalVal. */
121#endif
122#if defined (CONFIG_WITH_COMPILER) || defined (CONFIG_WITH_MAKE_STATS)
123 unsigned int evalval_count; /* Times used with $(evalval ) or $(evalctx ) since last change. */
124 unsigned int expand_count; /* Times expanded since last change (not to be confused with exp_count). */
125#endif
126#ifdef CONFIG_WITH_COMPILER
127 struct kmk_cc_evalprog *evalprog; /* Pointer to evalval/evalctx "program". */
128 struct kmk_cc_expandprog *expandprog; /* Pointer to variable expand "program". */
129#endif
130 };
131
132/* Update statistics and invalidates optimizations when a variable changes. */
133#ifdef CONFIG_WITH_COMPILER
134# define VARIABLE_CHANGED(v) \
135 do { \
136 MAKE_STATS_2((v)->changes++); \
137 if ((v)->evalprog || (v)->expandprog) kmk_cc_variable_changed(v); \
138 (v)->expand_count = 0; \
139 (v)->evalval_count = 0; \
140 (v)->recursive_without_dollar = 0; \
141 } while (0)
142#else
143# define VARIABLE_CHANGED(v) MAKE_STATS_2((v)->changes++)
144#endif
145
146/* Macro that avoids a lot of CONFIG_WITH_COMPILER checks when
147 accessing recursive_without_dollar. */
148#ifdef CONFIG_WITH_COMPILER
149# define IS_VARIABLE_RECURSIVE_WITHOUT_DOLLAR(v) ((v)->recursive_without_dollar > 0)
150#else
151# define IS_VARIABLE_RECURSIVE_WITHOUT_DOLLAR(v) 0
152#endif
153
154
155
156/* Structure that represents a variable set. */
157
158struct variable_set
159 {
160 struct hash_table table; /* Hash table of variables. */
161 };
162
163/* Structure that represents a list of variable sets. */
164
165struct variable_set_list
166 {
167 struct variable_set_list *next; /* Link in the chain. */
168 struct variable_set *set; /* Variable set. */
169 int next_is_parent; /* True if next is a parent target. */
170 };
171
172/* Structure used for pattern-specific variables. */
173
174struct pattern_var
175 {
176 struct pattern_var *next;
177 const char *suffix;
178 const char *target;
179 unsigned int len;
180 struct variable variable;
181 };
182
183extern char *variable_buffer;
184extern struct variable_set_list *current_variable_set_list;
185extern struct variable *default_goal_var;
186extern struct variable shell_var;
187
188#ifdef KMK
189extern struct variable_set global_variable_set;
190extern struct variable_set_list global_setlist;
191extern unsigned int variable_buffer_length;
192# define VARIABLE_BUFFER_ZONE 5
193#endif
194
195/* expand.c */
196#ifndef KMK
197char *
198variable_buffer_output (char *ptr, const char *string, unsigned int length);
199#else /* KMK */
200# include <k/kDefs.h>
201/* Subroutine of variable_expand and friends:
202 The text to add is LENGTH chars starting at STRING to the variable_buffer.
203 The text is added to the buffer at PTR, and the updated pointer into
204 the buffer is returned as the value. Thus, the value returned by
205 each call to variable_buffer_output should be the first argument to
206 the following call. */
207
208K_INLINE char *variable_buffer_output (char *ptr, const char *string, unsigned int length)
209{
210 register unsigned int newlen = length + (ptr - variable_buffer);
211
212 if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
213 {
214 unsigned int offset = ptr - variable_buffer;
215 variable_buffer_length = variable_buffer_length <= 1024
216 ? 2048 : variable_buffer_length * 4;
217 if (variable_buffer_length < newlen + 100)
218 variable_buffer_length = (newlen + 100 + 1023) & ~1023U;
219 variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
220 ptr = variable_buffer + offset;
221 }
222
223# ifndef _MSC_VER
224 switch (length)
225 {
226 case 4: ptr[3] = string[3]; /* fall thru */
227 case 3: ptr[2] = string[2]; /* fall thru */
228 case 2: ptr[1] = string[1]; /* fall thru */
229 case 1: ptr[0] = string[0]; /* fall thru */
230 case 0:
231 break;
232 default:
233 memcpy (ptr, string, length);
234 break;
235 }
236# else
237 memcpy (ptr, string, length);
238# endif
239 return ptr + length;
240}
241#endif /* KMK */
242
243char *variable_expand (const char *line);
244char *variable_expand_for_file (const char *line, struct file *file);
245#if defined (CONFIG_WITH_VALUE_LENGTH) || defined (CONFIG_WITH_COMMANDS_FUNC)
246char *variable_expand_for_file_2 (char *o, const char *line, unsigned int lenght,
247 struct file *file, unsigned int *value_lenp);
248#endif
249char *allocated_variable_expand_for_file (const char *line, struct file *file);
250#ifndef CONFIG_WITH_VALUE_LENGTH
251#define allocated_variable_expand(line) \
252 allocated_variable_expand_for_file (line, (struct file *) 0)
253#else /* CONFIG_WITH_VALUE_LENGTH */
254# define allocated_variable_expand(line) \
255 allocated_variable_expand_2 (line, -1, NULL)
256char *allocated_variable_expand_2 (const char *line, unsigned int length, unsigned int *value_lenp);
257char *allocated_variable_expand_3 (const char *line, unsigned int length,
258 unsigned int *value_lenp, unsigned int *buffer_lengthp);
259void recycle_variable_buffer (char *buffer, unsigned int length);
260#endif /* CONFIG_WITH_VALUE_LENGTH */
261char *expand_argument (const char *str, const char *end);
262#ifndef CONFIG_WITH_VALUE_LENGTH
263char *
264variable_expand_string (char *line, const char *string, long length);
265#else /* CONFIG_WITH_VALUE_LENGTH */
266# include <k/kDefs.h>
267char *
268variable_expand_string_2 (char *line, const char *string, long length, char **eol);
269K_INLINE char *variable_expand_string (char *line, const char *string, long length)
270{
271 char *ignored;
272 return variable_expand_string_2 (line, string, length, &ignored);
273}
274#endif /* CONFIG_WITH_VALUE_LENGTH */
275void install_variable_buffer (char **bufp, unsigned int *lenp);
276void restore_variable_buffer (char *buf, unsigned int len);
277char *install_variable_buffer_with_hint (char **bufp, unsigned int *lenp, unsigned int size_hint); /* bird */
278char *ensure_variable_buffer_space (char *ptr, unsigned int size); /* bird */
279#ifdef CONFIG_WITH_VALUE_LENGTH
280void append_expanded_string_to_variable (struct variable *v, const char *value,
281 unsigned int value_len, int append);
282#endif
283
284/* function.c */
285#ifndef CONFIG_WITH_VALUE_LENGTH
286int handle_function (char **op, const char **stringp);
287#else
288int handle_function (char **op, const char **stringp, const char *nameend, const char *eol);
289#endif
290#ifdef CONFIG_WITH_COMPILER
291typedef char *(*make_function_ptr_t) (char *, char **, const char *);
292make_function_ptr_t lookup_function_for_compiler (const char *name, unsigned int len,
293 unsigned char *minargsp, unsigned char *maxargsp,
294 char *expargsp, const char **funcnamep);
295#endif
296int pattern_matches (const char *pattern, const char *percent, const char *str);
297char *subst_expand (char *o, const char *text, const char *subst,
298 const char *replace, unsigned int slen, unsigned int rlen,
299 int by_word);
300char *patsubst_expand_pat (char *o, const char *text, const char *pattern,
301 const char *replace, const char *pattern_percent,
302 const char *replace_percent);
303char *patsubst_expand (char *o, const char *text, char *pattern, char *replace);
304char *func_shell_base (char *o, char **argv, int trim_newlines);
305void shell_completed (int exit_code, int exit_sig);
306
307#ifdef CONFIG_WITH_COMMANDS_FUNC /* for append.c */
308char *func_commands (char *o, char **argv, const char *funcname);
309#endif
310
311#if defined (CONFIG_WITH_VALUE_LENGTH)
312/* Avoid calling handle_function for every variable, do the
313 basic checks in variable_expand_string_2. */
314extern char func_char_map[256];
315# define MAX_FUNCTION_LENGTH 12
316# define MIN_FUNCTION_LENGTH 2
317K_INLINE const char *may_be_function_name (const char *name, const char *eos)
318{
319 unsigned char ch;
320 unsigned int len = name - eos;
321
322 /* Minimum length is MIN + whitespace. Check this directly.
323 ASSUMES: MIN_FUNCTION_LENGTH == 2 */
324
325 if (MY_PREDICT_TRUE(len < MIN_FUNCTION_LENGTH + 1
326 || !func_char_map[(int)(name[0])]
327 || !func_char_map[(int)(name[1])]))
328 return 0;
329 if (MY_PREDICT_TRUE(!func_char_map[ch = name[2]]))
330 return ISSPACE (ch) ? name + 2 : 0;
331
332 name += 3;
333 if (len > MAX_FUNCTION_LENGTH)
334 len = MAX_FUNCTION_LENGTH - 3;
335 else if (len == 3)
336 len -= 3;
337 if (!len)
338 return 0;
339
340 /* Loop over the remaining possiblities. */
341
342 while (func_char_map[ch = *name])
343 {
344 if (!len--)
345 return 0;
346 name++;
347 }
348 if (ch == '\0' || ISBLANK (ch))
349 return name;
350 return 0;
351}
352#endif /* CONFIG_WITH_VALUE_LENGTH */
353
354/* expand.c */
355#ifndef CONFIG_WITH_VALUE_LENGTH
356char *recursively_expand_for_file (struct variable *v, struct file *file);
357#define recursively_expand(v) recursively_expand_for_file (v, NULL)
358#else
359char *recursively_expand_for_file (struct variable *v, struct file *file,
360 unsigned int *value_lenp);
361# define recursively_expand(v) recursively_expand_for_file (v, NULL, NULL)
362#endif /* CONFIG_WITH_VALUE_LENGTH */
363#ifdef CONFIG_WITH_COMPILER
364char *reference_recursive_variable (char *o, struct variable *v);
365#endif
366
367/* variable.c */
368struct variable_set_list *create_new_variable_set (void);
369void free_variable_set (struct variable_set_list *);
370struct variable_set_list *push_new_variable_scope (void);
371void pop_variable_scope (void);
372void define_automatic_variables (void);
373void initialize_file_variables (struct file *file, int reading);
374void print_file_variables (const struct file *file);
375void print_target_variables (const struct file *file);
376void merge_variable_set_lists (struct variable_set_list **to_list,
377 struct variable_set_list *from_list);
378#ifdef KMK
379void print_variable_set (struct variable_set *set, const char *prefix, int pauto);
380#endif
381
382#ifndef CONFIG_WITH_VALUE_LENGTH
383struct variable *do_variable_definition (const floc *flocp,
384 const char *name, const char *value,
385 enum variable_origin origin,
386 enum variable_flavor flavor,
387 int target_var);
388#else /* CONFIG_WITH_VALUE_LENGTH */
389# define do_variable_definition(flocp, varname, value, origin, flavor, target_var) \
390 do_variable_definition_2 ((flocp), (varname), (value), ~0U, 0, NULL, \
391 (origin), (flavor), (target_var))
392struct variable *do_variable_definition_2 (const floc *flocp,
393 const char *varname,
394 const char *value,
395 unsigned int value_len,
396 int simple_value, char *free_value,
397 enum variable_origin origin,
398 enum variable_flavor flavor,
399 int target_var);
400#endif /* CONFIG_WITH_VALUE_LENGTH */
401char *parse_variable_definition (const char *line,
402 struct variable *v);
403struct variable *assign_variable_definition (struct variable *v, const char *line IF_WITH_VALUE_LENGTH_PARAM(char *eos));
404struct variable *try_variable_definition (const floc *flocp, const char *line
405 IF_WITH_VALUE_LENGTH_PARAM(char *eos),
406 enum variable_origin origin,
407 int target_var);
408void init_hash_global_variable_set (void);
409void hash_init_function_table (void);
410void define_new_function(const floc *flocp, const char *name,
411 unsigned int min, unsigned int max, unsigned int flags,
412 gmk_func_ptr func);
413struct variable *lookup_variable (const char *name, unsigned int length);
414struct variable *lookup_variable_in_set (const char *name, unsigned int length,
415 const struct variable_set *set);
416#ifdef CONFIG_WITH_STRCACHE2
417struct variable *lookup_variable_strcached (const char *name);
418#endif
419
420#ifdef CONFIG_WITH_VALUE_LENGTH
421void append_string_to_variable (struct variable *v, const char *value,
422 unsigned int value_len, int append);
423struct variable * do_variable_definition_append (const floc *flocp, struct variable *v,
424 const char *value, unsigned int value_len,
425 int simple_value, enum variable_origin origin,
426 int append);
427
428struct variable *define_variable_in_set (const char *name, unsigned int length,
429 const char *value,
430 unsigned int value_length,
431 int duplicate_value,
432 enum variable_origin origin,
433 int recursive,
434 struct variable_set *set,
435 const floc *flocp);
436
437/* Define a variable in the current variable set. */
438
439#define define_variable(n,l,v,o,r) \
440 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),\
441 current_variable_set_list->set,NILF)
442
443#define define_variable_vl(n,l,v,vl,dv,o,r) \
444 define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),\
445 current_variable_set_list->set,NILF)
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),~0U,1,(o),(r),\
451 current_variable_set_list->set,NILF)
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),~0U,1,(o),(r),\
457 current_variable_set_list->set,(f))
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),~0U,1,(o),(r),NULL,(f))
463
464#define define_variable_vl_global(n,l,v,vl,dv,o,r,f) \
465 define_variable_in_set((n),(l),(v),(vl),(dv),(o),(r),NULL,(f))
466
467/* Define a variable in FILE's variable set. */
468
469#define define_variable_for_file(n,l,v,o,r,f) \
470 define_variable_in_set((n),(l),(v),~0U,1,(o),(r),(f)->variables->set,NILF)
471
472#else /* !CONFIG_WITH_VALUE_LENGTH */
473
474struct variable *define_variable_in_set (const char *name, unsigned int length,
475 const char *value,
476 enum variable_origin origin,
477 int recursive,
478 struct variable_set *set,
479 const floc *flocp);
480
481/* Define a variable in the current variable set. */
482
483#define define_variable(n,l,v,o,r) \
484 define_variable_in_set((n),(l),(v),(o),(r),\
485 current_variable_set_list->set,NILF) /* force merge conflict */
486
487/* Define a variable with a constant name in the current variable set. */
488
489#define define_variable_cname(n,v,o,r) \
490 define_variable_in_set((n),(sizeof (n) - 1),(v),(o),(r),\
491 current_variable_set_list->set,NILF) /* force merge conflict */
492
493/* Define a variable with a location in the current variable set. */
494
495#define define_variable_loc(n,l,v,o,r,f) \
496 define_variable_in_set((n),(l),(v),(o),(r),\
497 current_variable_set_list->set,(f)) /* force merge conflict */
498
499/* Define a variable with a location in the global variable set. */
500
501#define define_variable_global(n,l,v,o,r,f) \
502 define_variable_in_set((n),(l),(v),(o),(r),NULL,(f)) /* force merge conflict */
503
504/* Define a variable in FILE's variable set. */
505
506#define define_variable_for_file(n,l,v,o,r,f) \
507 define_variable_in_set((n),(l),(v),(o),(r),(f)->variables->set,NILF) /* force merge conflict */
508
509#endif /* !CONFIG_WITH_VALUE_LENGTH */
510
511void undefine_variable_in_set (const char *name, unsigned int length,
512 enum variable_origin origin,
513 struct variable_set *set);
514
515/* Remove variable from the current variable set. */
516
517#define undefine_variable_global(n,l,o) \
518 undefine_variable_in_set((n),(l),(o),NULL)
519
520#ifdef KMK
521struct variable *
522define_variable_alias_in_set (const char *name, unsigned int length,
523 struct variable *target, enum variable_origin origin,
524 struct variable_set *set, const floc *flocp);
525#endif
526
527/* Warn that NAME is an undefined variable. */
528
529#define warn_undefined(n,l) do{\
530 if (warn_undefined_variables_flag) \
531 error (reading_file, (l), \
532 _("warning: undefined variable '%.*s'"), \
533 (int)(l), (n)); \
534 }while(0)
535
536char **target_environment (struct file *file);
537
538struct pattern_var *create_pattern_var (const char *target,
539 const char *suffix);
540
541extern int export_all_variables;
542#ifdef CONFIG_WITH_STRCACHE2
543extern struct strcache2 variable_strcache;
544#endif
545
546#ifdef KMK
547# define MAKELEVEL_NAME "KMK_LEVEL"
548#else
549#define MAKELEVEL_NAME "MAKELEVEL"
550#endif
551#define MAKELEVEL_LENGTH (CSTRLEN (MAKELEVEL_NAME))
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use