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