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, 2008, 2009,
4 2010 Free Software Foundation, Inc.
5 This file is part of GNU Make.
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
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.
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/>. */
21 /* Codes in a variable definition saying where the definition came from.
22 Increasing numeric values signify less-overridable definitions. */
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. */
37 f_bogus
, /* Bogus (error) */
38 f_simple
, /* Simple definition (:=) */
39 f_recursive
, /* Recursive definition (=) */
40 f_append
, /* Appending definition (+=) */
41 f_conditional
, /* Conditional definition (?=) */
42 f_shell
/* Shell assignment (!=) */
45 /* Structure that represents one variable definition.
46 Each bucket of the hash table is a chain of these,
47 chained through `next'. */
49 #define EXP_COUNT_BITS 15 /* This gets all the bitfields into 32 bits */
50 #define EXP_COUNT_MAX ((1<<EXP_COUNT_BITS)-1)
54 char *name
; /* Variable name. */
55 int length
; /* strlen (name) */
56 char *value
; /* Variable value. */
57 struct floc fileinfo
; /* Where the variable was defined. */
58 unsigned int recursive
:1; /* Gets recursively re-evaluated. */
59 unsigned int append
:1; /* Nonzero if an appending target-specific
61 unsigned int conditional
:1; /* Nonzero if set with a ?=. */
62 unsigned int per_target
:1; /* Nonzero if a target-specific variable. */
63 unsigned int special
:1; /* Nonzero if this is a special variable. */
64 unsigned int exportable
:1; /* Nonzero if the variable _could_ be
66 unsigned int expanding
:1; /* Nonzero if currently being expanded. */
67 unsigned int private_var
:1; /* Nonzero avoids inheritance of this
68 target-specific variable. */
69 unsigned int exp_count
:EXP_COUNT_BITS
;
70 /* If >1, allow this many self-referential
73 flavor
ENUM_BITFIELD (3); /* Variable flavor. */
75 origin
ENUM_BITFIELD (3); /* Variable origin. */
78 v_export
, /* Export this variable. */
79 v_noexport
, /* Don't export this variable. */
80 v_ifset
, /* Export it if it has a non-default value. */
81 v_default
/* Decide in target_environment. */
82 } export
ENUM_BITFIELD (2);
85 /* Structure that represents a variable set. */
89 struct hash_table table
; /* Hash table of variables. */
92 /* Structure that represents a list of variable sets. */
94 struct variable_set_list
96 struct variable_set_list
*next
; /* Link in the chain. */
97 struct variable_set
*set
; /* Variable set. */
98 int next_is_parent
; /* True if next is a parent target. */
101 /* Structure used for pattern-specific variables. */
105 struct pattern_var
*next
;
109 struct variable variable
;
112 extern char *variable_buffer
;
113 extern struct variable_set_list
*current_variable_set_list
;
114 extern struct variable
*default_goal_var
;
117 char *variable_buffer_output (char *ptr
, const char *string
, unsigned int length
);
118 char *variable_expand (const char *line
);
119 char *variable_expand_for_file (const char *line
, struct file
*file
);
120 char *allocated_variable_expand_for_file (const char *line
, struct file
*file
);
121 #define allocated_variable_expand(line) \
122 allocated_variable_expand_for_file (line, (struct file *) 0)
123 char *expand_argument (const char *str
, const char *end
);
124 char *variable_expand_string (char *line
, const char *string
, long length
);
125 void install_variable_buffer (char **bufp
, unsigned int *lenp
);
126 void restore_variable_buffer (char *buf
, unsigned int len
);
129 int handle_function (char **op
, const char **stringp
);
130 int pattern_matches (const char *pattern
, const char *percent
, const char *str
);
131 char *subst_expand (char *o
, const char *text
, const char *subst
,
132 const char *replace
, unsigned int slen
, unsigned int rlen
,
134 char *patsubst_expand_pat (char *o
, const char *text
, const char *pattern
,
135 const char *replace
, const char *pattern_percent
,
136 const char *replace_percent
);
137 char *patsubst_expand (char *o
, const char *text
, char *pattern
, char *replace
);
138 char *func_shell_base (char *o
, char **argv
, int trim_newlines
);
142 char *recursively_expand_for_file (struct variable
*v
, struct file
*file
);
143 #define recursively_expand(v) recursively_expand_for_file (v, NULL)
146 struct variable_set_list
*create_new_variable_set (void);
147 void free_variable_set (struct variable_set_list
*);
148 struct variable_set_list
*push_new_variable_scope (void);
149 void pop_variable_scope (void);
150 void define_automatic_variables (void);
151 void initialize_file_variables (struct file
*file
, int reading
);
152 void print_file_variables (const struct file
*file
);
153 void print_file_variables (const struct file
*file
);
154 void print_target_variables (const struct file
*file
);
155 void merge_variable_set_lists (struct variable_set_list
**to_list
,
156 struct variable_set_list
*from_list
);
157 struct variable
*do_variable_definition (const struct floc
*flocp
,
158 const char *name
, const char *value
,
159 enum variable_origin origin
,
160 enum variable_flavor flavor
,
162 char *parse_variable_definition (const char *line
,
163 enum variable_flavor
*flavor
);
164 struct variable
*assign_variable_definition (struct variable
*v
, char *line
);
165 struct variable
*try_variable_definition (const struct floc
*flocp
, char *line
,
166 enum variable_origin origin
,
168 void init_hash_global_variable_set (void);
169 void hash_init_function_table (void);
170 struct variable
*lookup_variable (const char *name
, unsigned int length
);
171 struct variable
*lookup_variable_in_set (const char *name
, unsigned int length
,
172 const struct variable_set
*set
);
174 struct variable
*define_variable_in_set (const char *name
, unsigned int length
,
176 enum variable_origin origin
,
178 struct variable_set
*set
,
179 const struct floc
*flocp
);
181 /* Define a variable in the current variable set. */
183 #define define_variable(n,l,v,o,r) \
184 define_variable_in_set((n),(l),(v),(o),(r),\
185 current_variable_set_list->set,NILF)
187 /* Define a variable with a constant name in the current variable set. */
189 #define define_variable_cname(n,v,o,r) \
190 define_variable_in_set((n),(sizeof (n) - 1),(v),(o),(r),\
191 current_variable_set_list->set,NILF)
193 /* Define a variable with a location in the current variable set. */
195 #define define_variable_loc(n,l,v,o,r,f) \
196 define_variable_in_set((n),(l),(v),(o),(r),\
197 current_variable_set_list->set,(f))
199 /* Define a variable with a location in the global variable set. */
201 #define define_variable_global(n,l,v,o,r,f) \
202 define_variable_in_set((n),(l),(v),(o),(r),NULL,(f))
204 /* Define a variable in FILE's variable set. */
206 #define define_variable_for_file(n,l,v,o,r,f) \
207 define_variable_in_set((n),(l),(v),(o),(r),(f)->variables->set,NILF)
209 void undefine_variable_in_set (const char *name
, unsigned int length
,
210 enum variable_origin origin
,
211 struct variable_set
*set
);
213 /* Remove variable from the current variable set. */
215 #define undefine_variable_global(n,l,o) \
216 undefine_variable_in_set((n),(l),(o),NULL)
218 /* Warn that NAME is an undefined variable. */
220 #define warn_undefined(n,l) do{\
221 if (warn_undefined_variables_flag) \
222 error (reading_file, \
223 _("warning: undefined variable `%.*s'"), \
227 char **target_environment (struct file
*file
);
229 struct pattern_var
*create_pattern_var (const char *target
,
232 extern int export_all_variables
;
234 #define MAKELEVEL_NAME "MAKELEVEL"
235 #define MAKELEVEL_LENGTH (sizeof (MAKELEVEL_NAME) - 1)