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