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