remake  4.3+dbg-1.5
variable.h
Go to the documentation of this file.
1 /* Definitions for using variables in GNU Make.
2 Copyright (C) 1988-2020 Free Software Foundation, Inc.
3 This file is part of GNU Make.
4 
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
8 version.
9 
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
16 
17 #ifndef VARIABLE_H
18 #define VARIABLE_H
19 
20 #include "gnuremake.h"
21 #include "hash.h"
22 
23 /* Codes in a variable definition saying where the definition came from.
24  Increasing numeric values signify less-overridable definitions. */
26  {
27  o_default, /* Variable from the default set. */
28  o_env, /* Variable from environment. */
29  o_file, /* Variable given in a makefile. */
30  o_env_override, /* Variable from environment, if -e. */
31  o_command, /* Variable given by user. */
32  o_override, /* Variable from an 'override' directive. */
33  o_automatic, /* Automatic variable -- cannot be set. */
34  o_debugger, /* Set inside debugger. */
35  o_invalid /* Core dump time. */
36  };
37 
39  {
40  f_bogus, /* Bogus (error) */
41  f_simple, /* Simple definition (:= or ::=) */
42  f_recursive, /* Recursive definition (=) */
43  f_append, /* Appending definition (+=) */
44  f_conditional, /* Conditional definition (?=) */
45  f_shell, /* Shell assignment (!=) */
46  f_append_value /* Append unexpanded value */
47  };
48 
49 /* Structure that represents one variable definition.
50  Each bucket of the hash table is a chain of these,
51  chained through 'next'. */
52 
53 #define EXP_COUNT_BITS 15 /* This gets all the bitfields into 32 bits */
54 #define EXP_COUNT_MAX ((1<<EXP_COUNT_BITS)-1)
55 
56 struct variable
57  {
58  char *name; /* Variable name. */
59  char *value; /* Variable value. */
60  gmk_floc fileinfo; /* Where the variable was defined. */
61  unsigned int length; /* strlen (name) */
62  unsigned int recursive:1; /* Gets recursively re-evaluated. */
63  unsigned int append:1; /* Nonzero if an appending target-specific
64  variable. */
65  unsigned int conditional:1; /* Nonzero if set with a ?=. */
66  unsigned int per_target:1; /* Nonzero if a target-specific variable. */
67  unsigned int special:1; /* Nonzero if this is a special variable. */
68  unsigned int exportable:1; /* Nonzero if the variable _could_ be
69  exported. */
70  unsigned int expanding:1; /* Nonzero if currently being expanded. */
71  unsigned int private_var:1; /* Nonzero avoids inheritance of this
72  target-specific variable. */
73  unsigned int exp_count:EXP_COUNT_BITS;
74  /* If >1, allow this many self-referential
75  expansions. */
76  enum variable_flavor
77  flavor ENUM_BITFIELD (4); /* Variable flavor. */
78  enum variable_origin
79  origin ENUM_BITFIELD (4); /* Variable origin. */
81  {
82  v_export, /* Export this variable. */
83  v_noexport, /* Don't export this variable. */
84  v_ifset, /* Export it if it has a non-default value. */
85  v_default /* Decide in target_environment. */
86  } export ENUM_BITFIELD (2);
87  };
88 
89 /* Structure that represents a variable set. */
90 
92  {
93  struct hash_table table; /* Hash table of variables. */
94  };
95 
96 /* Structure that represents a list of variable sets. */
97 
99  {
100  struct variable_set_list *next; /* Link in the chain. */
101  struct variable_set *set; /* Variable set. */
102  int next_is_parent; /* True if next is a parent target. */
103  };
104 
105 /* Structure used for pattern-specific variables. */
106 
108  {
109  struct pattern_var *next;
110  const char *suffix;
111  const char *target;
112  size_t len;
114  };
115 
116 extern char *variable_buffer;
118 extern struct variable *default_goal_var;
119 extern struct variable shell_var;
120 
121 /* expand.c */
122 #ifndef SIZE_MAX
123 # define SIZE_MAX ((size_t)~(size_t)0)
124 #endif
125 
126 char *variable_buffer_output (char *ptr, const char *string, size_t length);
127 char *variable_expand (const char *line);
128 char *variable_expand_for_file (const char *line, struct file *file);
129 char *allocated_variable_expand_for_file (const char *line, struct file *file);
130 #define allocated_variable_expand(line) \
131  allocated_variable_expand_for_file (line, (struct file *) 0)
132 char *expand_argument (const char *str, const char *end);
133 char *variable_expand_string (char *line, const char *string, size_t length);
134 void install_variable_buffer (char **bufp, size_t *lenp);
135 void restore_variable_buffer (char *buf, size_t len);
136 
137 /* function.c */
138 int handle_function (char **op, const char **stringp);
139 int pattern_matches (const char *pattern, const char *percent, const char *str);
140 char *subst_expand (char *o, const char *text, const char *subst,
141  const char *replace, size_t slen, size_t rlen,
142  int by_word);
143 char *patsubst_expand_pat (char *o, const char *text, const char *pattern,
144  const char *replace, const char *pattern_percent,
145  const char *replace_percent);
146 char *patsubst_expand (char *o, const char *text, char *pattern, char *replace);
147 char *func_shell_base (char *o, char **argv, int trim_newlines);
148 void shell_completed (int exit_code, int exit_sig);
149 
150 /* expand.c */
151 char *recursively_expand_for_file (struct variable *v, struct file *file);
152 #define recursively_expand(v) recursively_expand_for_file (v, NULL)
153 
154 /* variable.c */
156 
160 const char *origin2str(variable_origin_t origin);
161 
162 void free_variable_set (struct variable_set_list *);
163 
168 
173 void pop_variable_scope (void);
174 
177 void define_automatic_variables (void);
178 
188 void initialize_file_variables (struct file *file, int reading);
189 
194 extern void print_file_variables(const file_t *p_target, bool b_hash_stats);
195 
198 extern void print_variable_data_base (void);
199 
201 extern void print_variable_info (const void *item, void *arg);
202 
203 void print_target_variables (const struct file *file);
204 
209 void print_variable_set (struct variable_set *set, const char *prefix, int pauto);
210 
211 void merge_variable_set_lists (struct variable_set_list **to_list,
212  struct variable_set_list *from_list);
213 struct variable *do_variable_definition (const gmk_floc *flocp,
214  const char *name, const char *value,
215  enum variable_origin origin,
216  enum variable_flavor flavor,
217  int target_var);
218 char *parse_variable_definition (const char *line,
219  struct variable *v);
220 struct variable *assign_variable_definition (struct variable *v, const char *line);
221 struct variable *try_variable_definition (const gmk_floc *flocp, const char *line,
222  enum variable_origin origin,
223  int target_var);
225 void hash_init_function_table (void);
226 void define_new_function(const gmk_floc *flocp, const char *name,
227  unsigned int min, unsigned int max, unsigned int flags,
228  gmk_func_ptr func);
229 struct variable *lookup_variable (const char *name, size_t length);
230 struct variable *lookup_variable_in_set (const char *name, size_t length,
231  const struct variable_set *set);
232 
233 struct variable *define_variable_in_set (const char *name, size_t length,
234  const char *value,
235  enum variable_origin origin,
236  int recursive,
237  struct variable_set *set,
238  const gmk_floc *flocp);
239 
240 /* Define a variable in the current variable set. */
241 
242 #define define_variable(n,l,v,o,r) \
243  define_variable_in_set((n),(l),(v),(o),(r),\
244  current_variable_set_list->set,NILF)
245 
246 /* Define a variable with a constant name in the current variable set. */
247 
248 #define define_variable_cname(n,v,o,r) \
249  define_variable_in_set((n),(sizeof (n) - 1),(v),(o),(r),\
250  current_variable_set_list->set,NILF)
251 
252 /* Define a variable with a location in the current variable set. */
253 
254 #define define_variable_loc(n,l,v,o,r,f) \
255  define_variable_in_set((n),(l),(v),(o),(r),\
256  current_variable_set_list->set,(f))
257 
258 /* Define a variable with a location in the global variable set. */
259 
260 #define define_variable_global(n,l,v,o,r,f) \
261  define_variable_in_set((n),(l),(v),(o),(r),NULL,(f))
262 
263 /* Define a variable in FILE's variable set. */
264 
265 #define define_variable_for_file(n,l,v,o,r,f) \
266  define_variable_in_set((n),(l),(v),(o),(r),(f)->variables->set,NILF)
267 
268 void undefine_variable_in_set (const char *name, size_t length,
269  enum variable_origin origin,
270  struct variable_set *set);
271 
272 /* Remove variable from the current variable set. */
273 
274 #define undefine_variable_global(n,l,o) \
275  undefine_variable_in_set((n),(l),(o),NULL)
276 
277 /* Warn that NAME is an undefined variable. */
278 
279 #define warn_undefined(n,l) do{\
280  if (warn_undefined_variables_flag) \
281  error (reading_file, (l), \
282  _("warning: undefined variable '%.*s'"), \
283  (int)(l), (n)); \
284  }while(0)
285 
286 char **target_environment (struct file *file);
287 
288 struct pattern_var *create_pattern_var (const char *target,
289  const char *suffix);
290 
291 extern int export_all_variables;
292 
293 #define MAKELEVEL_NAME "MAKELEVEL"
294 #define MAKELEVEL_LENGTH (CSTRLEN (MAKELEVEL_NAME))
295 
296 #endif /*VARIABLE_H*/
variable_flavor
Definition: variable.h:38
int export_all_variables
struct variable * assign_variable_definition(struct variable *v, const char *line)
variable_origin
Definition: variable.h:25
void hash_init_function_table(void)
char * variable_expand_string(char *line, const char *string, size_t length)
Definition: variable.h:82
Definition: variable.h:91
char * value
Definition: variable.h:59
char * expand_argument(const char *str, const char *end)
void pop_variable_scope(void)
int next_is_parent
Definition: variable.h:102
unsigned int exp_count
Definition: variable.h:73
Definition: variable.h:30
struct variable shell_var
Definition: globals.h:92
const char * suffix
Definition: variable.h:110
int handle_function(char **op, const char **stringp)
char *(* gmk_func_ptr)(const char *nm, unsigned int argc, char **argv)
Definition: gnuremake.h:30
Definition: variable.h:85
unsigned int per_target
Definition: variable.h:66
struct pattern_var * next
Definition: variable.h:109
void define_new_function(const gmk_floc *flocp, const char *name, unsigned int min, unsigned int max, unsigned int flags, gmk_func_ptr func)
Definition: variable.h:32
const char * target
Definition: variable.h:111
char * variable_buffer_output(char *ptr, const char *string, size_t length)
char ** target_environment(struct file *file)
int pattern_matches(const char *pattern, const char *percent, const char *str)
void print_variable_info(const void *item, void *arg)
Definition: variable.h:98
void define_automatic_variables(void)
void restore_variable_buffer(char *buf, size_t len)
Definition: variable.h:107
struct variable_set_list * next
Definition: variable.h:100
Definition: variable.h:33
Definition: variable.h:28
struct variable * try_variable_definition(const gmk_floc *flocp, const char *line, enum variable_origin origin, int target_var)
unsigned int length
Definition: variable.h:61
Definition: variable.h:44
unsigned int recursive
Definition: variable.h:62
#define EXP_COUNT_BITS
Definition: variable.h:53
unsigned int append
Definition: variable.h:63
gmk_floc fileinfo
Definition: variable.h:60
char * allocated_variable_expand_for_file(const char *line, struct file *file)
struct variable * define_variable_in_set(const char *name, size_t length, const char *value, enum variable_origin origin, int recursive, struct variable_set *set, const gmk_floc *flocp)
void print_target_variables(const struct file *file)
void undefine_variable_in_set(const char *name, size_t length, enum variable_origin origin, struct variable_set *set)
Definition: variable.h:27
void print_variable_set(struct variable_set *set, const char *prefix, int pauto)
enum variable_origin variable_origin_t
Definition: types.h:79
void print_file_variables(const file_t *p_target, bool b_hash_stats)
Definition: variable.h:84
char * recursively_expand_for_file(struct variable *v, struct file *file)
variable_export
Definition: variable.h:80
struct variable * lookup_variable(const char *name, size_t length)
struct variable * do_variable_definition(const gmk_floc *flocp, const char *name, const char *value, enum variable_origin origin, enum variable_flavor flavor, int target_var)
unsigned int private_var
Definition: variable.h:71
void install_variable_buffer(char **bufp, size_t *lenp)
struct variable_set_list * current_variable_set_list
unsigned int exportable
Definition: variable.h:68
Definition: variable.h:42
Definition: variable.h:43
Definition: variable.h:31
struct variable_set_list * push_new_variable_scope(void)
void initialize_file_variables(struct file *file, int reading)
struct variable * lookup_variable_in_set(const char *name, size_t length, const struct variable_set *set)
void print_variable_data_base(void)
Definition: variable.h:29
char * parse_variable_definition(const char *line, struct variable *v)
const char * origin2str(variable_origin_t origin)
Definition: variable.h:46
struct pattern_var * create_pattern_var(const char *target, const char *suffix)
enum variable_flavor flavor ENUM_BITFIELD(4)
struct variable * default_goal_var
Definition: gnuremake.h:23
char * variable_buffer
char * patsubst_expand(char *o, const char *text, char *pattern, char *replace)
unsigned int expanding
Definition: variable.h:70
char * subst_expand(char *o, const char *text, const char *subst, const char *replace, size_t slen, size_t rlen, int by_word)
Definition: variable.h:34
char * variable_expand_for_file(const char *line, struct file *file)
struct variable_set_list * create_new_variable_set(void)
unsigned int conditional
Definition: variable.h:65
unsigned int special
Definition: variable.h:67
void free_variable_set(struct variable_set_list *)
Definition: variable.h:40
Definition: variable.h:83
Definition: variable.h:45
char * patsubst_expand_pat(char *o, const char *text, const char *pattern, const char *replace, const char *pattern_percent, const char *replace_percent)
char * variable_expand(const char *line)
char * func_shell_base(char *o, char **argv, int trim_newlines)
Definition: variable.h:35
Definition: variable.h:41
void init_hash_global_variable_set(void)
void merge_variable_set_lists(struct variable_set_list **to_list, struct variable_set_list *from_list)
Definition: hash.h:41
char * name
Definition: variable.h:58
Definition: variable.h:56
Definition: filedef.h:34
size_t len
Definition: variable.h:112
void shell_completed(int exit_code, int exit_sig)