Imported Upstream version 4.3
[platform/upstream/make.git] / src / variable.h
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 #include "hash.h"
18
19 /* Codes in a variable definition saying where the definition came from.
20    Increasing numeric values signify less-overridable definitions.  */
21 enum variable_origin
22   {
23     o_default,          /* Variable from the default set.  */
24     o_env,              /* Variable from environment.  */
25     o_file,             /* Variable given in a makefile.  */
26     o_env_override,     /* Variable from environment, if -e.  */
27     o_command,          /* Variable given by user.  */
28     o_override,         /* Variable from an 'override' directive.  */
29     o_automatic,        /* Automatic variable -- cannot be set.  */
30     o_invalid           /* Core dump time.  */
31   };
32
33 enum variable_flavor
34   {
35     f_bogus,            /* Bogus (error) */
36     f_simple,           /* Simple definition (:= or ::=) */
37     f_recursive,        /* Recursive definition (=) */
38     f_append,           /* Appending definition (+=) */
39     f_conditional,      /* Conditional definition (?=) */
40     f_shell,            /* Shell assignment (!=) */
41     f_append_value      /* Append unexpanded value */
42   };
43
44 /* Structure that represents one variable definition.
45    Each bucket of the hash table is a chain of these,
46    chained through 'next'.  */
47
48 #define EXP_COUNT_BITS  15      /* This gets all the bitfields into 32 bits */
49 #define EXP_COUNT_MAX   ((1<<EXP_COUNT_BITS)-1)
50
51 struct variable
52   {
53     char *name;                 /* Variable name.  */
54     char *value;                /* Variable value.  */
55     floc fileinfo;              /* Where the variable was defined.  */
56     unsigned int length;        /* strlen (name) */
57     unsigned int recursive:1;   /* Gets recursively re-evaluated.  */
58     unsigned int append:1;      /* Nonzero if an appending target-specific
59                                    variable.  */
60     unsigned int conditional:1; /* Nonzero if set with a ?=. */
61     unsigned int per_target:1;  /* Nonzero if a target-specific variable.  */
62     unsigned int special:1;     /* Nonzero if this is a special variable.  */
63     unsigned int exportable:1;  /* Nonzero if the variable _could_ be
64                                    exported.  */
65     unsigned int expanding:1;   /* Nonzero if currently being expanded.  */
66     unsigned int private_var:1; /* Nonzero avoids inheritance of this
67                                    target-specific variable.  */
68     unsigned int exp_count:EXP_COUNT_BITS;
69                                 /* If >1, allow this many self-referential
70                                    expansions.  */
71     enum variable_flavor
72       flavor ENUM_BITFIELD (3); /* Variable flavor.  */
73     enum variable_origin
74       origin ENUM_BITFIELD (3); /* Variable origin.  */
75     enum variable_export
76       {
77         v_export,               /* Export this variable.  */
78         v_noexport,             /* Don't export this variable.  */
79         v_ifset,                /* Export it if it has a non-default value.  */
80         v_default               /* Decide in target_environment.  */
81       } export ENUM_BITFIELD (2);
82   };
83
84 /* Structure that represents a variable set.  */
85
86 struct variable_set
87   {
88     struct hash_table table;    /* Hash table of variables.  */
89   };
90
91 /* Structure that represents a list of variable sets.  */
92
93 struct variable_set_list
94   {
95     struct variable_set_list *next;     /* Link in the chain.  */
96     struct variable_set *set;           /* Variable set.  */
97     int next_is_parent;                 /* True if next is a parent target.  */
98   };
99
100 /* Structure used for pattern-specific variables.  */
101
102 struct pattern_var
103   {
104     struct pattern_var *next;
105     const char *suffix;
106     const char *target;
107     size_t len;
108     struct variable variable;
109   };
110
111 extern char *variable_buffer;
112 extern struct variable_set_list *current_variable_set_list;
113 extern struct variable *default_goal_var;
114 extern struct variable shell_var;
115
116 /* expand.c */
117 #ifndef SIZE_MAX
118 # define SIZE_MAX ((size_t)~(size_t)0)
119 #endif
120
121 char *variable_buffer_output (char *ptr, const char *string, size_t length);
122 char *variable_expand (const char *line);
123 char *variable_expand_for_file (const char *line, struct file *file);
124 char *allocated_variable_expand_for_file (const char *line, struct file *file);
125 #define allocated_variable_expand(line) \
126   allocated_variable_expand_for_file (line, (struct file *) 0)
127 char *expand_argument (const char *str, const char *end);
128 char *variable_expand_string (char *line, const char *string, size_t length);
129 void install_variable_buffer (char **bufp, size_t *lenp);
130 void restore_variable_buffer (char *buf, size_t len);
131
132 /* function.c */
133 int handle_function (char **op, const char **stringp);
134 int pattern_matches (const char *pattern, const char *percent, const char *str);
135 char *subst_expand (char *o, const char *text, const char *subst,
136                     const char *replace, size_t slen, size_t rlen,
137                     int by_word);
138 char *patsubst_expand_pat (char *o, const char *text, const char *pattern,
139                            const char *replace, const char *pattern_percent,
140                            const char *replace_percent);
141 char *patsubst_expand (char *o, const char *text, char *pattern, char *replace);
142 char *func_shell_base (char *o, char **argv, int trim_newlines);
143 void shell_completed (int exit_code, int exit_sig);
144
145 /* expand.c */
146 char *recursively_expand_for_file (struct variable *v, struct file *file);
147 #define recursively_expand(v)   recursively_expand_for_file (v, NULL)
148
149 /* variable.c */
150 struct variable_set_list *create_new_variable_set (void);
151 void free_variable_set (struct variable_set_list *);
152 struct variable_set_list *push_new_variable_scope (void);
153 void pop_variable_scope (void);
154 void define_automatic_variables (void);
155 void initialize_file_variables (struct file *file, int reading);
156 void print_file_variables (const struct file *file);
157 void print_target_variables (const struct file *file);
158 void merge_variable_set_lists (struct variable_set_list **to_list,
159                                struct variable_set_list *from_list);
160 struct variable *do_variable_definition (const floc *flocp,
161                                          const char *name, const char *value,
162                                          enum variable_origin origin,
163                                          enum variable_flavor flavor,
164                                          int target_var);
165 char *parse_variable_definition (const char *line,
166                                  struct variable *v);
167 struct variable *assign_variable_definition (struct variable *v, const char *line);
168 struct variable *try_variable_definition (const floc *flocp, const char *line,
169                                           enum variable_origin origin,
170                                           int target_var);
171 void init_hash_global_variable_set (void);
172 void hash_init_function_table (void);
173 void define_new_function(const floc *flocp, const char *name,
174                          unsigned int min, unsigned int max, unsigned int flags,
175                          gmk_func_ptr func);
176 struct variable *lookup_variable (const char *name, size_t length);
177 struct variable *lookup_variable_in_set (const char *name, size_t length,
178                                          const struct variable_set *set);
179
180 struct variable *define_variable_in_set (const char *name, size_t length,
181                                          const char *value,
182                                          enum variable_origin origin,
183                                          int recursive,
184                                          struct variable_set *set,
185                                          const floc *flocp);
186
187 /* Define a variable in the current variable set.  */
188
189 #define define_variable(n,l,v,o,r) \
190           define_variable_in_set((n),(l),(v),(o),(r),\
191                                  current_variable_set_list->set,NILF)
192
193 /* Define a variable with a constant name in the current variable set.  */
194
195 #define define_variable_cname(n,v,o,r) \
196           define_variable_in_set((n),(sizeof (n) - 1),(v),(o),(r),\
197                                  current_variable_set_list->set,NILF)
198
199 /* Define a variable with a location in the current variable set.  */
200
201 #define define_variable_loc(n,l,v,o,r,f) \
202           define_variable_in_set((n),(l),(v),(o),(r),\
203                                  current_variable_set_list->set,(f))
204
205 /* Define a variable with a location in the global variable set.  */
206
207 #define define_variable_global(n,l,v,o,r,f) \
208           define_variable_in_set((n),(l),(v),(o),(r),NULL,(f))
209
210 /* Define a variable in FILE's variable set.  */
211
212 #define define_variable_for_file(n,l,v,o,r,f) \
213           define_variable_in_set((n),(l),(v),(o),(r),(f)->variables->set,NILF)
214
215 void undefine_variable_in_set (const char *name, size_t length,
216                                enum variable_origin origin,
217                                struct variable_set *set);
218
219 /* Remove variable from the current variable set. */
220
221 #define undefine_variable_global(n,l,o) \
222           undefine_variable_in_set((n),(l),(o),NULL)
223
224 /* Warn that NAME is an undefined variable.  */
225
226 #define warn_undefined(n,l) do{\
227                               if (warn_undefined_variables_flag)        \
228                                 error (reading_file, (l),               \
229                                        _("warning: undefined variable '%.*s'"), \
230                                        (int)(l), (n));                  \
231                               }while(0)
232
233 char **target_environment (struct file *file);
234
235 struct pattern_var *create_pattern_var (const char *target,
236                                         const char *suffix);
237
238 extern int export_all_variables;
239
240 #define MAKELEVEL_NAME "MAKELEVEL"
241 #define MAKELEVEL_LENGTH (CSTRLEN (MAKELEVEL_NAME))