[2580] | 1 | /* Internals of variables for 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.
|
---|
| 6 |
|
---|
| 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
|
---|
| 10 | version.
|
---|
| 11 |
|
---|
| 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.
|
---|
| 15 |
|
---|
| 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/>. */
|
---|
| 18 |
|
---|
| 19 | #include "make.h"
|
---|
| 20 |
|
---|
| 21 | #include <assert.h>
|
---|
| 22 |
|
---|
| 23 | #include "dep.h"
|
---|
| 24 | #include "filedef.h"
|
---|
| 25 | #include "job.h"
|
---|
| 26 | #include "commands.h"
|
---|
| 27 | #include "variable.h"
|
---|
| 28 | #include "rule.h"
|
---|
| 29 | #ifdef WINDOWS32
|
---|
| 30 | #include "pathstuff.h"
|
---|
| 31 | #endif
|
---|
| 32 | #include "hash.h"
|
---|
| 33 |
|
---|
| 34 | /* Chain of all pattern-specific variables. */
|
---|
| 35 |
|
---|
| 36 | static struct pattern_var *pattern_vars;
|
---|
| 37 |
|
---|
| 38 | /* Pointer to the last struct in the pack of a specific size, from 1 to 255.*/
|
---|
| 39 |
|
---|
| 40 | static struct pattern_var *last_pattern_vars[256];
|
---|
| 41 |
|
---|
| 42 | /* Create a new pattern-specific variable struct. The new variable is
|
---|
| 43 | inserted into the PATTERN_VARS list in the shortest patterns first
|
---|
| 44 | order to support the shortest stem matching (the variables are
|
---|
| 45 | matched in the reverse order so the ones with the longest pattern
|
---|
| 46 | will be considered first). Variables with the same pattern length
|
---|
| 47 | are inserted in the definition order. */
|
---|
| 48 |
|
---|
| 49 | struct pattern_var *
|
---|
| 50 | create_pattern_var (const char *target, const char *suffix)
|
---|
| 51 | {
|
---|
| 52 | register unsigned int len = strlen (target);
|
---|
| 53 | register struct pattern_var *p = xmalloc (sizeof (struct pattern_var));
|
---|
| 54 |
|
---|
| 55 | if (pattern_vars != 0)
|
---|
| 56 | {
|
---|
| 57 | if (len < 256 && last_pattern_vars[len] != 0)
|
---|
| 58 | {
|
---|
| 59 | p->next = last_pattern_vars[len]->next;
|
---|
| 60 | last_pattern_vars[len]->next = p;
|
---|
| 61 | }
|
---|
| 62 | else
|
---|
| 63 | {
|
---|
| 64 | /* Find the position where we can insert this variable. */
|
---|
| 65 | register struct pattern_var **v;
|
---|
| 66 |
|
---|
| 67 | for (v = &pattern_vars; ; v = &(*v)->next)
|
---|
| 68 | {
|
---|
| 69 | /* Insert at the end of the pack so that patterns with the
|
---|
| 70 | same length appear in the order they were defined .*/
|
---|
| 71 |
|
---|
| 72 | if (*v == 0 || (*v)->len > len)
|
---|
| 73 | {
|
---|
| 74 | p->next = *v;
|
---|
| 75 | *v = p;
|
---|
| 76 | break;
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | else
|
---|
| 82 | {
|
---|
| 83 | pattern_vars = p;
|
---|
| 84 | p->next = 0;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | p->target = target;
|
---|
| 88 | p->len = len;
|
---|
| 89 | p->suffix = suffix + 1;
|
---|
| 90 |
|
---|
| 91 | if (len < 256)
|
---|
| 92 | last_pattern_vars[len] = p;
|
---|
| 93 |
|
---|
| 94 | return p;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /* Look up a target in the pattern-specific variable list. */
|
---|
| 98 |
|
---|
| 99 | static struct pattern_var *
|
---|
| 100 | lookup_pattern_var (struct pattern_var *start, const char *target)
|
---|
| 101 | {
|
---|
| 102 | struct pattern_var *p;
|
---|
| 103 | unsigned int targlen = strlen(target);
|
---|
| 104 |
|
---|
| 105 | for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
|
---|
| 106 | {
|
---|
| 107 | const char *stem;
|
---|
| 108 | unsigned int stemlen;
|
---|
| 109 |
|
---|
| 110 | if (p->len > targlen)
|
---|
| 111 | /* It can't possibly match. */
|
---|
| 112 | continue;
|
---|
| 113 |
|
---|
| 114 | /* From the lengths of the filename and the pattern parts,
|
---|
| 115 | find the stem: the part of the filename that matches the %. */
|
---|
| 116 | stem = target + (p->suffix - p->target - 1);
|
---|
| 117 | stemlen = targlen - p->len + 1;
|
---|
| 118 |
|
---|
| 119 | /* Compare the text in the pattern before the stem, if any. */
|
---|
| 120 | if (stem > target && !strneq (p->target, target, stem - target))
|
---|
| 121 | continue;
|
---|
| 122 |
|
---|
| 123 | /* Compare the text in the pattern after the stem, if any.
|
---|
| 124 | We could test simply using streq, but this way we compare the
|
---|
| 125 | first two characters immediately. This saves time in the very
|
---|
| 126 | common case where the first character matches because it is a
|
---|
| 127 | period. */
|
---|
| 128 | if (*p->suffix == stem[stemlen]
|
---|
| 129 | && (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
|
---|
| 130 | break;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | return p;
|
---|
| 134 | }
|
---|
| 135 | |
---|
| 136 |
|
---|
| 137 | /* Hash table of all global variable definitions. */
|
---|
| 138 |
|
---|
| 139 | static unsigned long
|
---|
| 140 | variable_hash_1 (const void *keyv)
|
---|
| 141 | {
|
---|
| 142 | struct variable const *key = (struct variable const *) keyv;
|
---|
| 143 | return_STRING_N_HASH_1 (key->name, key->length);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | static unsigned long
|
---|
| 147 | variable_hash_2 (const void *keyv)
|
---|
| 148 | {
|
---|
| 149 | struct variable const *key = (struct variable const *) keyv;
|
---|
| 150 | return_STRING_N_HASH_2 (key->name, key->length);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | static int
|
---|
| 154 | variable_hash_cmp (const void *xv, const void *yv)
|
---|
| 155 | {
|
---|
| 156 | struct variable const *x = (struct variable const *) xv;
|
---|
| 157 | struct variable const *y = (struct variable const *) yv;
|
---|
| 158 | int result = x->length - y->length;
|
---|
| 159 | if (result)
|
---|
| 160 | return result;
|
---|
| 161 | return_STRING_N_COMPARE (x->name, y->name, x->length);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | #ifndef VARIABLE_BUCKETS
|
---|
| 165 | #define VARIABLE_BUCKETS 523
|
---|
| 166 | #endif
|
---|
| 167 | #ifndef PERFILE_VARIABLE_BUCKETS
|
---|
| 168 | #define PERFILE_VARIABLE_BUCKETS 23
|
---|
| 169 | #endif
|
---|
| 170 | #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
|
---|
| 171 | #define SMALL_SCOPE_VARIABLE_BUCKETS 13
|
---|
| 172 | #endif
|
---|
| 173 |
|
---|
| 174 | static struct variable_set global_variable_set;
|
---|
| 175 | static struct variable_set_list global_setlist
|
---|
| 176 | = { 0, &global_variable_set, 0 };
|
---|
| 177 | struct variable_set_list *current_variable_set_list = &global_setlist;
|
---|
| 178 | |
---|
| 179 |
|
---|
| 180 | /* Implement variables. */
|
---|
| 181 |
|
---|
| 182 | void
|
---|
| 183 | init_hash_global_variable_set (void)
|
---|
| 184 | {
|
---|
| 185 | hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
|
---|
| 186 | variable_hash_1, variable_hash_2, variable_hash_cmp);
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | /* Define variable named NAME with value VALUE in SET. VALUE is copied.
|
---|
| 190 | LENGTH is the length of NAME, which does not need to be null-terminated.
|
---|
| 191 | ORIGIN specifies the origin of the variable (makefile, command line
|
---|
| 192 | or environment).
|
---|
| 193 | If RECURSIVE is nonzero a flag is set in the variable saying
|
---|
| 194 | that it should be recursively re-expanded. */
|
---|
| 195 |
|
---|
| 196 | struct variable *
|
---|
| 197 | define_variable_in_set (const char *name, unsigned int length,
|
---|
| 198 | const char *value, enum variable_origin origin,
|
---|
| 199 | int recursive, struct variable_set *set,
|
---|
| 200 | const struct floc *flocp)
|
---|
| 201 | {
|
---|
| 202 | struct variable *v;
|
---|
| 203 | struct variable **var_slot;
|
---|
| 204 | struct variable var_key;
|
---|
| 205 |
|
---|
| 206 | if (set == NULL)
|
---|
| 207 | set = &global_variable_set;
|
---|
| 208 |
|
---|
| 209 | var_key.name = (char *) name;
|
---|
| 210 | var_key.length = length;
|
---|
| 211 | var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
|
---|
| 212 |
|
---|
| 213 | if (env_overrides && origin == o_env)
|
---|
| 214 | origin = o_env_override;
|
---|
| 215 |
|
---|
| 216 | v = *var_slot;
|
---|
| 217 | if (! HASH_VACANT (v))
|
---|
| 218 | {
|
---|
| 219 | if (env_overrides && v->origin == o_env)
|
---|
| 220 | /* V came from in the environment. Since it was defined
|
---|
| 221 | before the switches were parsed, it wasn't affected by -e. */
|
---|
| 222 | v->origin = o_env_override;
|
---|
| 223 |
|
---|
| 224 | /* A variable of this name is already defined.
|
---|
| 225 | If the old definition is from a stronger source
|
---|
| 226 | than this one, don't redefine it. */
|
---|
| 227 | if ((int) origin >= (int) v->origin)
|
---|
| 228 | {
|
---|
| 229 | if (v->value != 0)
|
---|
| 230 | free (v->value);
|
---|
| 231 | v->value = xstrdup (value);
|
---|
| 232 | if (flocp != 0)
|
---|
| 233 | v->fileinfo = *flocp;
|
---|
| 234 | else
|
---|
| 235 | v->fileinfo.filenm = 0;
|
---|
| 236 | v->origin = origin;
|
---|
| 237 | v->recursive = recursive;
|
---|
| 238 | }
|
---|
| 239 | return v;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | /* Create a new variable definition and add it to the hash table. */
|
---|
| 243 |
|
---|
| 244 | v = xmalloc (sizeof (struct variable));
|
---|
| 245 | v->name = xstrndup (name, length);
|
---|
| 246 | v->length = length;
|
---|
| 247 | hash_insert_at (&set->table, v, var_slot);
|
---|
| 248 | v->value = xstrdup (value);
|
---|
| 249 | if (flocp != 0)
|
---|
| 250 | v->fileinfo = *flocp;
|
---|
| 251 | else
|
---|
| 252 | v->fileinfo.filenm = 0;
|
---|
| 253 | v->origin = origin;
|
---|
| 254 | v->recursive = recursive;
|
---|
| 255 | v->special = 0;
|
---|
| 256 | v->expanding = 0;
|
---|
| 257 | v->exp_count = 0;
|
---|
| 258 | v->per_target = 0;
|
---|
| 259 | v->append = 0;
|
---|
| 260 | v->private_var = 0;
|
---|
| 261 | v->export = v_default;
|
---|
| 262 |
|
---|
| 263 | v->exportable = 1;
|
---|
| 264 | if (*name != '_' && (*name < 'A' || *name > 'Z')
|
---|
| 265 | && (*name < 'a' || *name > 'z'))
|
---|
| 266 | v->exportable = 0;
|
---|
| 267 | else
|
---|
| 268 | {
|
---|
| 269 | for (++name; *name != '\0'; ++name)
|
---|
| 270 | if (*name != '_' && (*name < 'a' || *name > 'z')
|
---|
| 271 | && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
|
---|
| 272 | break;
|
---|
| 273 |
|
---|
| 274 | if (*name != '\0')
|
---|
| 275 | v->exportable = 0;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | return v;
|
---|
| 279 | }
|
---|
| 280 | |
---|
| 281 |
|
---|
| 282 |
|
---|
| 283 | /* Undefine variable named NAME in SET. LENGTH is the length of NAME, which
|
---|
| 284 | does not need to be null-terminated. ORIGIN specifies the origin of the
|
---|
| 285 | variable (makefile, command line or environment). */
|
---|
| 286 |
|
---|
| 287 | static void
|
---|
| 288 | free_variable_name_and_value (const void *item);
|
---|
| 289 |
|
---|
| 290 | void
|
---|
| 291 | undefine_variable_in_set (const char *name, unsigned int length,
|
---|
| 292 | enum variable_origin origin,
|
---|
| 293 | struct variable_set *set)
|
---|
| 294 | {
|
---|
| 295 | struct variable *v;
|
---|
| 296 | struct variable **var_slot;
|
---|
| 297 | struct variable var_key;
|
---|
| 298 |
|
---|
| 299 | if (set == NULL)
|
---|
| 300 | set = &global_variable_set;
|
---|
| 301 |
|
---|
| 302 | var_key.name = (char *) name;
|
---|
| 303 | var_key.length = length;
|
---|
| 304 | var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
|
---|
| 305 |
|
---|
| 306 | if (env_overrides && origin == o_env)
|
---|
| 307 | origin = o_env_override;
|
---|
| 308 |
|
---|
| 309 | v = *var_slot;
|
---|
| 310 | if (! HASH_VACANT (v))
|
---|
| 311 | {
|
---|
| 312 | if (env_overrides && v->origin == o_env)
|
---|
| 313 | /* V came from in the environment. Since it was defined
|
---|
| 314 | before the switches were parsed, it wasn't affected by -e. */
|
---|
| 315 | v->origin = o_env_override;
|
---|
| 316 |
|
---|
| 317 | /* If the definition is from a stronger source than this one, don't
|
---|
| 318 | undefine it. */
|
---|
| 319 | if ((int) origin >= (int) v->origin)
|
---|
| 320 | {
|
---|
| 321 | hash_delete_at (&set->table, var_slot);
|
---|
| 322 | free_variable_name_and_value (v);
|
---|
| 323 | }
|
---|
| 324 | }
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | /* If the variable passed in is "special", handle its special nature.
|
---|
| 328 | Currently there are two such variables, both used for introspection:
|
---|
| 329 | .VARIABLES expands to a list of all the variables defined in this instance
|
---|
| 330 | of make.
|
---|
| 331 | .TARGETS expands to a list of all the targets defined in this
|
---|
| 332 | instance of make.
|
---|
| 333 | Returns the variable reference passed in. */
|
---|
| 334 |
|
---|
| 335 | #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
|
---|
| 336 |
|
---|
| 337 | static struct variable *
|
---|
| 338 | lookup_special_var (struct variable *var)
|
---|
| 339 | {
|
---|
| 340 | static unsigned long last_var_count = 0;
|
---|
| 341 |
|
---|
| 342 |
|
---|
| 343 | /* This one actually turns out to be very hard, due to the way the parser
|
---|
| 344 | records targets. The way it works is that target information is collected
|
---|
| 345 | internally until make knows the target is completely specified. It unitl
|
---|
| 346 | it sees that some new construct (a new target or variable) is defined that
|
---|
| 347 | it knows the previous one is done. In short, this means that if you do
|
---|
| 348 | this:
|
---|
| 349 |
|
---|
| 350 | all:
|
---|
| 351 |
|
---|
| 352 | TARGS := $(.TARGETS)
|
---|
| 353 |
|
---|
| 354 | then $(TARGS) won't contain "all", because it's not until after the
|
---|
| 355 | variable is created that the previous target is completed.
|
---|
| 356 |
|
---|
| 357 | Changing this would be a major pain. I think a less complex way to do it
|
---|
| 358 | would be to pre-define the target files as soon as the first line is
|
---|
| 359 | parsed, then come back and do the rest of the definition as now. That
|
---|
| 360 | would allow $(.TARGETS) to be correct without a major change to the way
|
---|
| 361 | the parser works.
|
---|
| 362 |
|
---|
| 363 | if (streq (var->name, ".TARGETS"))
|
---|
| 364 | var->value = build_target_list (var->value);
|
---|
| 365 | else
|
---|
| 366 | */
|
---|
| 367 |
|
---|
| 368 | if (streq (var->name, ".VARIABLES")
|
---|
| 369 | && global_variable_set.table.ht_fill != last_var_count)
|
---|
| 370 | {
|
---|
| 371 | unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
|
---|
| 372 | unsigned long len;
|
---|
| 373 | char *p;
|
---|
| 374 | struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
|
---|
| 375 | struct variable **end = &vp[global_variable_set.table.ht_size];
|
---|
| 376 |
|
---|
| 377 | /* Make sure we have at least MAX bytes in the allocated buffer. */
|
---|
| 378 | var->value = xrealloc (var->value, max);
|
---|
| 379 |
|
---|
| 380 | /* Walk through the hash of variables, constructing a list of names. */
|
---|
| 381 | p = var->value;
|
---|
| 382 | len = 0;
|
---|
| 383 | for (; vp < end; ++vp)
|
---|
| 384 | if (!HASH_VACANT (*vp))
|
---|
| 385 | {
|
---|
| 386 | struct variable *v = *vp;
|
---|
| 387 | int l = v->length;
|
---|
| 388 |
|
---|
| 389 | len += l + 1;
|
---|
| 390 | if (len > max)
|
---|
| 391 | {
|
---|
| 392 | unsigned long off = p - var->value;
|
---|
| 393 |
|
---|
| 394 | max += EXPANSION_INCREMENT (l + 1);
|
---|
| 395 | var->value = xrealloc (var->value, max);
|
---|
| 396 | p = &var->value[off];
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | memcpy (p, v->name, l);
|
---|
| 400 | p += l;
|
---|
| 401 | *(p++) = ' ';
|
---|
| 402 | }
|
---|
| 403 | *(p-1) = '\0';
|
---|
| 404 |
|
---|
| 405 | /* Remember how many variables are in our current count. Since we never
|
---|
| 406 | remove variables from the list, this is a reliable way to know whether
|
---|
| 407 | the list is up to date or needs to be recomputed. */
|
---|
| 408 |
|
---|
| 409 | last_var_count = global_variable_set.table.ht_fill;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | return var;
|
---|
| 413 | }
|
---|
| 414 |
|
---|
| 415 | |
---|
| 416 |
|
---|
| 417 | /* Lookup a variable whose name is a string starting at NAME
|
---|
| 418 | and with LENGTH chars. NAME need not be null-terminated.
|
---|
| 419 | Returns address of the `struct variable' containing all info
|
---|
| 420 | on the variable, or nil if no such variable is defined. */
|
---|
| 421 |
|
---|
| 422 | struct variable *
|
---|
| 423 | lookup_variable (const char *name, unsigned int length)
|
---|
| 424 | {
|
---|
| 425 | const struct variable_set_list *setlist;
|
---|
| 426 | struct variable var_key;
|
---|
| 427 | int is_parent = 0;
|
---|
| 428 |
|
---|
| 429 | var_key.name = (char *) name;
|
---|
| 430 | var_key.length = length;
|
---|
| 431 |
|
---|
| 432 | for (setlist = current_variable_set_list;
|
---|
| 433 | setlist != 0; setlist = setlist->next)
|
---|
| 434 | {
|
---|
| 435 | const struct variable_set *set = setlist->set;
|
---|
| 436 | struct variable *v;
|
---|
| 437 |
|
---|
| 438 | v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
|
---|
| 439 | if (v && (!is_parent || !v->private_var))
|
---|
| 440 | return v->special ? lookup_special_var (v) : v;
|
---|
| 441 |
|
---|
| 442 | is_parent |= setlist->next_is_parent;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
| 445 | #ifdef VMS
|
---|
| 446 | /* since we don't read envp[] on startup, try to get the
|
---|
| 447 | variable via getenv() here. */
|
---|
| 448 | {
|
---|
| 449 | char *vname = alloca (length + 1);
|
---|
| 450 | char *value;
|
---|
| 451 | strncpy (vname, name, length);
|
---|
| 452 | vname[length] = 0;
|
---|
| 453 | value = getenv (vname);
|
---|
| 454 | if (value != 0)
|
---|
| 455 | {
|
---|
| 456 | char *sptr;
|
---|
| 457 | int scnt;
|
---|
| 458 |
|
---|
| 459 | sptr = value;
|
---|
| 460 | scnt = 0;
|
---|
| 461 |
|
---|
| 462 | while ((sptr = strchr (sptr, '$')))
|
---|
| 463 | {
|
---|
| 464 | scnt++;
|
---|
| 465 | sptr++;
|
---|
| 466 | }
|
---|
| 467 |
|
---|
| 468 | if (scnt > 0)
|
---|
| 469 | {
|
---|
| 470 | char *nvalue;
|
---|
| 471 | char *nptr;
|
---|
| 472 |
|
---|
| 473 | nvalue = alloca (strlen (value) + scnt + 1);
|
---|
| 474 | sptr = value;
|
---|
| 475 | nptr = nvalue;
|
---|
| 476 |
|
---|
| 477 | while (*sptr)
|
---|
| 478 | {
|
---|
| 479 | if (*sptr == '$')
|
---|
| 480 | {
|
---|
| 481 | *nptr++ = '$';
|
---|
| 482 | *nptr++ = '$';
|
---|
| 483 | }
|
---|
| 484 | else
|
---|
| 485 | {
|
---|
| 486 | *nptr++ = *sptr;
|
---|
| 487 | }
|
---|
| 488 | sptr++;
|
---|
| 489 | }
|
---|
| 490 |
|
---|
| 491 | *nptr = '\0';
|
---|
| 492 | return define_variable (vname, length, nvalue, o_env, 1);
|
---|
| 493 |
|
---|
| 494 | }
|
---|
| 495 |
|
---|
| 496 | return define_variable (vname, length, value, o_env, 1);
|
---|
| 497 | }
|
---|
| 498 | }
|
---|
| 499 | #endif /* VMS */
|
---|
| 500 |
|
---|
| 501 | return 0;
|
---|
| 502 | }
|
---|
| 503 | |
---|
| 504 |
|
---|
| 505 | /* Lookup a variable whose name is a string starting at NAME
|
---|
| 506 | and with LENGTH chars in set SET. NAME need not be null-terminated.
|
---|
| 507 | Returns address of the `struct variable' containing all info
|
---|
| 508 | on the variable, or nil if no such variable is defined. */
|
---|
| 509 |
|
---|
| 510 | struct variable *
|
---|
| 511 | lookup_variable_in_set (const char *name, unsigned int length,
|
---|
| 512 | const struct variable_set *set)
|
---|
| 513 | {
|
---|
| 514 | struct variable var_key;
|
---|
| 515 |
|
---|
| 516 | var_key.name = (char *) name;
|
---|
| 517 | var_key.length = length;
|
---|
| 518 |
|
---|
| 519 | return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
|
---|
| 520 | }
|
---|
| 521 | |
---|
| 522 |
|
---|
| 523 | /* Initialize FILE's variable set list. If FILE already has a variable set
|
---|
| 524 | list, the topmost variable set is left intact, but the the rest of the
|
---|
| 525 | chain is replaced with FILE->parent's setlist. If FILE is a double-colon
|
---|
| 526 | rule, then we will use the "root" double-colon target's variable set as the
|
---|
| 527 | parent of FILE's variable set.
|
---|
| 528 |
|
---|
| 529 | If we're READING a makefile, don't do the pattern variable search now,
|
---|
| 530 | since the pattern variable might not have been defined yet. */
|
---|
| 531 |
|
---|
| 532 | void
|
---|
| 533 | initialize_file_variables (struct file *file, int reading)
|
---|
| 534 | {
|
---|
| 535 | struct variable_set_list *l = file->variables;
|
---|
| 536 |
|
---|
| 537 | if (l == 0)
|
---|
| 538 | {
|
---|
| 539 | l = (struct variable_set_list *)
|
---|
| 540 | xmalloc (sizeof (struct variable_set_list));
|
---|
| 541 | l->set = xmalloc (sizeof (struct variable_set));
|
---|
| 542 | hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
|
---|
| 543 | variable_hash_1, variable_hash_2, variable_hash_cmp);
|
---|
| 544 | file->variables = l;
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 | /* If this is a double-colon, then our "parent" is the "root" target for
|
---|
| 548 | this double-colon rule. Since that rule has the same name, parent,
|
---|
| 549 | etc. we can just use its variables as the "next" for ours. */
|
---|
| 550 |
|
---|
| 551 | if (file->double_colon && file->double_colon != file)
|
---|
| 552 | {
|
---|
| 553 | initialize_file_variables (file->double_colon, reading);
|
---|
| 554 | l->next = file->double_colon->variables;
|
---|
| 555 | l->next_is_parent = 0;
|
---|
| 556 | return;
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | if (file->parent == 0)
|
---|
| 560 | l->next = &global_setlist;
|
---|
| 561 | else
|
---|
| 562 | {
|
---|
| 563 | initialize_file_variables (file->parent, reading);
|
---|
| 564 | l->next = file->parent->variables;
|
---|
| 565 | }
|
---|
| 566 | l->next_is_parent = 1;
|
---|
| 567 |
|
---|
| 568 | /* If we're not reading makefiles and we haven't looked yet, see if
|
---|
| 569 | we can find pattern variables for this target. */
|
---|
| 570 |
|
---|
| 571 | if (!reading && !file->pat_searched)
|
---|
| 572 | {
|
---|
| 573 | struct pattern_var *p;
|
---|
| 574 |
|
---|
| 575 | p = lookup_pattern_var (0, file->name);
|
---|
| 576 | if (p != 0)
|
---|
| 577 | {
|
---|
| 578 | struct variable_set_list *global = current_variable_set_list;
|
---|
| 579 |
|
---|
| 580 | /* We found at least one. Set up a new variable set to accumulate
|
---|
| 581 | all the pattern variables that match this target. */
|
---|
| 582 |
|
---|
| 583 | file->pat_variables = create_new_variable_set ();
|
---|
| 584 | current_variable_set_list = file->pat_variables;
|
---|
| 585 |
|
---|
| 586 | do
|
---|
| 587 | {
|
---|
| 588 | /* We found one, so insert it into the set. */
|
---|
| 589 |
|
---|
| 590 | struct variable *v;
|
---|
| 591 |
|
---|
| 592 | if (p->variable.flavor == f_simple)
|
---|
| 593 | {
|
---|
| 594 | v = define_variable_loc (
|
---|
| 595 | p->variable.name, strlen (p->variable.name),
|
---|
| 596 | p->variable.value, p->variable.origin,
|
---|
| 597 | 0, &p->variable.fileinfo);
|
---|
| 598 |
|
---|
| 599 | v->flavor = f_simple;
|
---|
| 600 | }
|
---|
| 601 | else
|
---|
| 602 | {
|
---|
| 603 | v = do_variable_definition (
|
---|
| 604 | &p->variable.fileinfo, p->variable.name,
|
---|
| 605 | p->variable.value, p->variable.origin,
|
---|
| 606 | p->variable.flavor, 1);
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 | /* Also mark it as a per-target and copy export status. */
|
---|
| 610 | v->per_target = p->variable.per_target;
|
---|
| 611 | v->export = p->variable.export;
|
---|
| 612 | v->private_var = p->variable.private_var;
|
---|
| 613 | }
|
---|
| 614 | while ((p = lookup_pattern_var (p, file->name)) != 0);
|
---|
| 615 |
|
---|
| 616 | current_variable_set_list = global;
|
---|
| 617 | }
|
---|
| 618 | file->pat_searched = 1;
|
---|
| 619 | }
|
---|
| 620 |
|
---|
| 621 | /* If we have a pattern variable match, set it up. */
|
---|
| 622 |
|
---|
| 623 | if (file->pat_variables != 0)
|
---|
| 624 | {
|
---|
| 625 | file->pat_variables->next = l->next;
|
---|
| 626 | file->pat_variables->next_is_parent = l->next_is_parent;
|
---|
| 627 | l->next = file->pat_variables;
|
---|
| 628 | l->next_is_parent = 0;
|
---|
| 629 | }
|
---|
| 630 | }
|
---|
| 631 | |
---|
| 632 |
|
---|
| 633 | /* Pop the top set off the current variable set list,
|
---|
| 634 | and free all its storage. */
|
---|
| 635 |
|
---|
| 636 | struct variable_set_list *
|
---|
| 637 | create_new_variable_set (void)
|
---|
| 638 | {
|
---|
| 639 | register struct variable_set_list *setlist;
|
---|
| 640 | register struct variable_set *set;
|
---|
| 641 |
|
---|
| 642 | set = xmalloc (sizeof (struct variable_set));
|
---|
| 643 | hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
|
---|
| 644 | variable_hash_1, variable_hash_2, variable_hash_cmp);
|
---|
| 645 |
|
---|
| 646 | setlist = (struct variable_set_list *)
|
---|
| 647 | xmalloc (sizeof (struct variable_set_list));
|
---|
| 648 | setlist->set = set;
|
---|
| 649 | setlist->next = current_variable_set_list;
|
---|
| 650 | setlist->next_is_parent = 0;
|
---|
| 651 |
|
---|
| 652 | return setlist;
|
---|
| 653 | }
|
---|
| 654 |
|
---|
| 655 | static void
|
---|
| 656 | free_variable_name_and_value (const void *item)
|
---|
| 657 | {
|
---|
| 658 | struct variable *v = (struct variable *) item;
|
---|
| 659 | free (v->name);
|
---|
| 660 | free (v->value);
|
---|
| 661 | }
|
---|
| 662 |
|
---|
| 663 | void
|
---|
| 664 | free_variable_set (struct variable_set_list *list)
|
---|
| 665 | {
|
---|
| 666 | hash_map (&list->set->table, free_variable_name_and_value);
|
---|
| 667 | hash_free (&list->set->table, 1);
|
---|
| 668 | free (list->set);
|
---|
| 669 | free (list);
|
---|
| 670 | }
|
---|
| 671 |
|
---|
| 672 | /* Create a new variable set and push it on the current setlist.
|
---|
| 673 | If we're pushing a global scope (that is, the current scope is the global
|
---|
| 674 | scope) then we need to "push" it the other way: file variable sets point
|
---|
| 675 | directly to the global_setlist so we need to replace that with the new one.
|
---|
| 676 | */
|
---|
| 677 |
|
---|
| 678 | struct variable_set_list *
|
---|
| 679 | push_new_variable_scope (void)
|
---|
| 680 | {
|
---|
| 681 | current_variable_set_list = create_new_variable_set();
|
---|
| 682 | if (current_variable_set_list->next == &global_setlist)
|
---|
| 683 | {
|
---|
| 684 | /* It was the global, so instead of new -> &global we want to replace
|
---|
| 685 | &global with the new one and have &global -> new, with current still
|
---|
| 686 | pointing to &global */
|
---|
| 687 | struct variable_set *set = current_variable_set_list->set;
|
---|
| 688 | current_variable_set_list->set = global_setlist.set;
|
---|
| 689 | global_setlist.set = set;
|
---|
| 690 | current_variable_set_list->next = global_setlist.next;
|
---|
| 691 | global_setlist.next = current_variable_set_list;
|
---|
| 692 | current_variable_set_list = &global_setlist;
|
---|
| 693 | }
|
---|
| 694 | return (current_variable_set_list);
|
---|
| 695 | }
|
---|
| 696 |
|
---|
| 697 | void
|
---|
| 698 | pop_variable_scope (void)
|
---|
| 699 | {
|
---|
| 700 | struct variable_set_list *setlist;
|
---|
| 701 | struct variable_set *set;
|
---|
| 702 |
|
---|
| 703 | /* Can't call this if there's no scope to pop! */
|
---|
| 704 | assert(current_variable_set_list->next != NULL);
|
---|
| 705 |
|
---|
| 706 | if (current_variable_set_list != &global_setlist)
|
---|
| 707 | {
|
---|
| 708 | /* We're not pointing to the global setlist, so pop this one. */
|
---|
| 709 | setlist = current_variable_set_list;
|
---|
| 710 | set = setlist->set;
|
---|
| 711 | current_variable_set_list = setlist->next;
|
---|
| 712 | }
|
---|
| 713 | else
|
---|
| 714 | {
|
---|
| 715 | /* This set is the one in the global_setlist, but there is another global
|
---|
| 716 | set beyond that. We want to copy that set to global_setlist, then
|
---|
| 717 | delete what used to be in global_setlist. */
|
---|
| 718 | setlist = global_setlist.next;
|
---|
| 719 | set = global_setlist.set;
|
---|
| 720 | global_setlist.set = setlist->set;
|
---|
| 721 | global_setlist.next = setlist->next;
|
---|
| 722 | global_setlist.next_is_parent = setlist->next_is_parent;
|
---|
| 723 | }
|
---|
| 724 |
|
---|
| 725 | /* Free the one we no longer need. */
|
---|
| 726 | free (setlist);
|
---|
| 727 | hash_map (&set->table, free_variable_name_and_value);
|
---|
| 728 | hash_free (&set->table, 1);
|
---|
| 729 | free (set);
|
---|
| 730 | }
|
---|
| 731 | |
---|
| 732 |
|
---|
| 733 | /* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
|
---|
| 734 |
|
---|
| 735 | static void
|
---|
| 736 | merge_variable_sets (struct variable_set *to_set,
|
---|
| 737 | struct variable_set *from_set)
|
---|
| 738 | {
|
---|
| 739 | struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
|
---|
| 740 | struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
|
---|
| 741 |
|
---|
| 742 | for ( ; from_var_slot < from_var_end; from_var_slot++)
|
---|
| 743 | if (! HASH_VACANT (*from_var_slot))
|
---|
| 744 | {
|
---|
| 745 | struct variable *from_var = *from_var_slot;
|
---|
| 746 | struct variable **to_var_slot
|
---|
| 747 | = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
|
---|
| 748 | if (HASH_VACANT (*to_var_slot))
|
---|
| 749 | hash_insert_at (&to_set->table, from_var, to_var_slot);
|
---|
| 750 | else
|
---|
| 751 | {
|
---|
| 752 | /* GKM FIXME: delete in from_set->table */
|
---|
| 753 | free (from_var->value);
|
---|
| 754 | free (from_var);
|
---|
| 755 | }
|
---|
| 756 | }
|
---|
| 757 | }
|
---|
| 758 |
|
---|
| 759 | /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
|
---|
| 760 |
|
---|
| 761 | void
|
---|
| 762 | merge_variable_set_lists (struct variable_set_list **setlist0,
|
---|
| 763 | struct variable_set_list *setlist1)
|
---|
| 764 | {
|
---|
| 765 | struct variable_set_list *to = *setlist0;
|
---|
| 766 | struct variable_set_list *last0 = 0;
|
---|
| 767 |
|
---|
| 768 | /* If there's nothing to merge, stop now. */
|
---|
| 769 | if (!setlist1)
|
---|
| 770 | return;
|
---|
| 771 |
|
---|
| 772 | /* This loop relies on the fact that all setlists terminate with the global
|
---|
| 773 | setlist (before NULL). If that's not true, arguably we SHOULD die. */
|
---|
| 774 | if (to)
|
---|
| 775 | while (setlist1 != &global_setlist && to != &global_setlist)
|
---|
| 776 | {
|
---|
| 777 | struct variable_set_list *from = setlist1;
|
---|
| 778 | setlist1 = setlist1->next;
|
---|
| 779 |
|
---|
| 780 | merge_variable_sets (to->set, from->set);
|
---|
| 781 |
|
---|
| 782 | last0 = to;
|
---|
| 783 | to = to->next;
|
---|
| 784 | }
|
---|
| 785 |
|
---|
| 786 | if (setlist1 != &global_setlist)
|
---|
| 787 | {
|
---|
| 788 | if (last0 == 0)
|
---|
| 789 | *setlist0 = setlist1;
|
---|
| 790 | else
|
---|
| 791 | last0->next = setlist1;
|
---|
| 792 | }
|
---|
| 793 | }
|
---|
| 794 | |
---|
| 795 |
|
---|
| 796 | /* Define the automatic variables, and record the addresses
|
---|
| 797 | of their structures so we can change their values quickly. */
|
---|
| 798 |
|
---|
| 799 | void
|
---|
| 800 | define_automatic_variables (void)
|
---|
| 801 | {
|
---|
| 802 | #if defined(WINDOWS32) || defined(__EMX__)
|
---|
| 803 | extern char* default_shell;
|
---|
| 804 | #else
|
---|
| 805 | extern char default_shell[];
|
---|
| 806 | #endif
|
---|
| 807 | register struct variable *v;
|
---|
| 808 | char buf[200];
|
---|
| 809 |
|
---|
| 810 | sprintf (buf, "%u", makelevel);
|
---|
| 811 | define_variable_cname (MAKELEVEL_NAME, buf, o_env, 0);
|
---|
| 812 |
|
---|
| 813 | sprintf (buf, "%s%s%s",
|
---|
| 814 | version_string,
|
---|
| 815 | (remote_description == 0 || remote_description[0] == '\0')
|
---|
| 816 | ? "" : "-",
|
---|
| 817 | (remote_description == 0 || remote_description[0] == '\0')
|
---|
| 818 | ? "" : remote_description);
|
---|
| 819 | define_variable_cname ("MAKE_VERSION", buf, o_default, 0);
|
---|
| 820 |
|
---|
| 821 | #ifdef __MSDOS__
|
---|
| 822 | /* Allow to specify a special shell just for Make,
|
---|
| 823 | and use $COMSPEC as the default $SHELL when appropriate. */
|
---|
| 824 | {
|
---|
| 825 | static char shell_str[] = "SHELL";
|
---|
| 826 | const int shlen = sizeof (shell_str) - 1;
|
---|
| 827 | struct variable *mshp = lookup_variable ("MAKESHELL", 9);
|
---|
| 828 | struct variable *comp = lookup_variable ("COMSPEC", 7);
|
---|
| 829 |
|
---|
| 830 | /* $(MAKESHELL) overrides $(SHELL) even if -e is in effect. */
|
---|
| 831 | if (mshp)
|
---|
| 832 | (void) define_variable (shell_str, shlen,
|
---|
| 833 | mshp->value, o_env_override, 0);
|
---|
| 834 | else if (comp)
|
---|
| 835 | {
|
---|
| 836 | /* $(COMSPEC) shouldn't override $(SHELL). */
|
---|
| 837 | struct variable *shp = lookup_variable (shell_str, shlen);
|
---|
| 838 |
|
---|
| 839 | if (!shp)
|
---|
| 840 | (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
|
---|
| 841 | }
|
---|
| 842 | }
|
---|
| 843 | #elif defined(__EMX__)
|
---|
| 844 | {
|
---|
| 845 | static char shell_str[] = "SHELL";
|
---|
| 846 | const int shlen = sizeof (shell_str) - 1;
|
---|
| 847 | struct variable *shell = lookup_variable (shell_str, shlen);
|
---|
| 848 | struct variable *replace = lookup_variable ("MAKESHELL", 9);
|
---|
| 849 |
|
---|
| 850 | /* if $MAKESHELL is defined in the environment assume o_env_override */
|
---|
| 851 | if (replace && *replace->value && replace->origin == o_env)
|
---|
| 852 | replace->origin = o_env_override;
|
---|
| 853 |
|
---|
| 854 | /* if $MAKESHELL is not defined use $SHELL but only if the variable
|
---|
| 855 | did not come from the environment */
|
---|
| 856 | if (!replace || !*replace->value)
|
---|
| 857 | if (shell && *shell->value && (shell->origin == o_env
|
---|
| 858 | || shell->origin == o_env_override))
|
---|
| 859 | {
|
---|
| 860 | /* overwrite whatever we got from the environment */
|
---|
| 861 | free(shell->value);
|
---|
| 862 | shell->value = xstrdup (default_shell);
|
---|
| 863 | shell->origin = o_default;
|
---|
| 864 | }
|
---|
| 865 |
|
---|
| 866 | /* Some people do not like cmd to be used as the default
|
---|
| 867 | if $SHELL is not defined in the Makefile.
|
---|
| 868 | With -DNO_CMD_DEFAULT you can turn off this behaviour */
|
---|
| 869 | # ifndef NO_CMD_DEFAULT
|
---|
| 870 | /* otherwise use $COMSPEC */
|
---|
| 871 | if (!replace || !*replace->value)
|
---|
| 872 | replace = lookup_variable ("COMSPEC", 7);
|
---|
| 873 |
|
---|
| 874 | /* otherwise use $OS2_SHELL */
|
---|
| 875 | if (!replace || !*replace->value)
|
---|
| 876 | replace = lookup_variable ("OS2_SHELL", 9);
|
---|
| 877 | # else
|
---|
| 878 | # warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
|
---|
| 879 | # endif
|
---|
| 880 |
|
---|
| 881 | if (replace && *replace->value)
|
---|
| 882 | /* overwrite $SHELL */
|
---|
| 883 | (void) define_variable (shell_str, shlen, replace->value,
|
---|
| 884 | replace->origin, 0);
|
---|
| 885 | else
|
---|
| 886 | /* provide a definition if there is none */
|
---|
| 887 | (void) define_variable (shell_str, shlen, default_shell,
|
---|
| 888 | o_default, 0);
|
---|
| 889 | }
|
---|
| 890 |
|
---|
| 891 | #endif
|
---|
| 892 |
|
---|
| 893 | /* This won't override any definition, but it will provide one if there
|
---|
| 894 | isn't one there. */
|
---|
| 895 | v = define_variable_cname ("SHELL", default_shell, o_default, 0);
|
---|
| 896 | #ifdef __MSDOS__
|
---|
| 897 | v->export = v_export; /* Export always SHELL. */
|
---|
| 898 | #endif
|
---|
| 899 |
|
---|
| 900 | /* On MSDOS we do use SHELL from environment, since it isn't a standard
|
---|
| 901 | environment variable on MSDOS, so whoever sets it, does that on purpose.
|
---|
| 902 | On OS/2 we do not use SHELL from environment but we have already handled
|
---|
| 903 | that problem above. */
|
---|
| 904 | #if !defined(__MSDOS__) && !defined(__EMX__)
|
---|
| 905 | /* Don't let SHELL come from the environment. */
|
---|
| 906 | if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
|
---|
| 907 | {
|
---|
| 908 | free (v->value);
|
---|
| 909 | v->origin = o_file;
|
---|
| 910 | v->value = xstrdup (default_shell);
|
---|
| 911 | }
|
---|
| 912 | #endif
|
---|
| 913 |
|
---|
| 914 | /* Make sure MAKEFILES gets exported if it is set. */
|
---|
| 915 | v = define_variable_cname ("MAKEFILES", "", o_default, 0);
|
---|
| 916 | v->export = v_ifset;
|
---|
| 917 |
|
---|
| 918 | /* Define the magic D and F variables in terms of
|
---|
| 919 | the automatic variables they are variations of. */
|
---|
| 920 |
|
---|
| 921 | #ifdef VMS
|
---|
| 922 | define_variable_cname ("@D", "$(dir $@)", o_automatic, 1);
|
---|
| 923 | define_variable_cname ("%D", "$(dir $%)", o_automatic, 1);
|
---|
| 924 | define_variable_cname ("*D", "$(dir $*)", o_automatic, 1);
|
---|
| 925 | define_variable_cname ("<D", "$(dir $<)", o_automatic, 1);
|
---|
| 926 | define_variable_cname ("?D", "$(dir $?)", o_automatic, 1);
|
---|
| 927 | define_variable_cname ("^D", "$(dir $^)", o_automatic, 1);
|
---|
| 928 | define_variable_cname ("+D", "$(dir $+)", o_automatic, 1);
|
---|
| 929 | #else
|
---|
| 930 | define_variable_cname ("@D", "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
|
---|
| 931 | define_variable_cname ("%D", "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
|
---|
| 932 | define_variable_cname ("*D", "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
|
---|
| 933 | define_variable_cname ("<D", "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
|
---|
| 934 | define_variable_cname ("?D", "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
|
---|
| 935 | define_variable_cname ("^D", "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
|
---|
| 936 | define_variable_cname ("+D", "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
|
---|
| 937 | #endif
|
---|
| 938 | define_variable_cname ("@F", "$(notdir $@)", o_automatic, 1);
|
---|
| 939 | define_variable_cname ("%F", "$(notdir $%)", o_automatic, 1);
|
---|
| 940 | define_variable_cname ("*F", "$(notdir $*)", o_automatic, 1);
|
---|
| 941 | define_variable_cname ("<F", "$(notdir $<)", o_automatic, 1);
|
---|
| 942 | define_variable_cname ("?F", "$(notdir $?)", o_automatic, 1);
|
---|
| 943 | define_variable_cname ("^F", "$(notdir $^)", o_automatic, 1);
|
---|
| 944 | define_variable_cname ("+F", "$(notdir $+)", o_automatic, 1);
|
---|
| 945 | }
|
---|
| 946 | |
---|
| 947 |
|
---|
| 948 | int export_all_variables;
|
---|
| 949 |
|
---|
| 950 | /* Create a new environment for FILE's commands.
|
---|
| 951 | If FILE is nil, this is for the `shell' function.
|
---|
| 952 | The child's MAKELEVEL variable is incremented. */
|
---|
| 953 |
|
---|
| 954 | char **
|
---|
| 955 | target_environment (struct file *file)
|
---|
| 956 | {
|
---|
| 957 | struct variable_set_list *set_list;
|
---|
| 958 | register struct variable_set_list *s;
|
---|
| 959 | struct hash_table table;
|
---|
| 960 | struct variable **v_slot;
|
---|
| 961 | struct variable **v_end;
|
---|
| 962 | struct variable makelevel_key;
|
---|
| 963 | char **result_0;
|
---|
| 964 | char **result;
|
---|
| 965 |
|
---|
| 966 | if (file == 0)
|
---|
| 967 | set_list = current_variable_set_list;
|
---|
| 968 | else
|
---|
| 969 | set_list = file->variables;
|
---|
| 970 |
|
---|
| 971 | hash_init (&table, VARIABLE_BUCKETS,
|
---|
| 972 | variable_hash_1, variable_hash_2, variable_hash_cmp);
|
---|
| 973 |
|
---|
| 974 | /* Run through all the variable sets in the list,
|
---|
| 975 | accumulating variables in TABLE. */
|
---|
| 976 | for (s = set_list; s != 0; s = s->next)
|
---|
| 977 | {
|
---|
| 978 | struct variable_set *set = s->set;
|
---|
| 979 | v_slot = (struct variable **) set->table.ht_vec;
|
---|
| 980 | v_end = v_slot + set->table.ht_size;
|
---|
| 981 | for ( ; v_slot < v_end; v_slot++)
|
---|
| 982 | if (! HASH_VACANT (*v_slot))
|
---|
| 983 | {
|
---|
| 984 | struct variable **new_slot;
|
---|
| 985 | struct variable *v = *v_slot;
|
---|
| 986 |
|
---|
| 987 | /* If this is a per-target variable and it hasn't been touched
|
---|
| 988 | already then look up the global version and take its export
|
---|
| 989 | value. */
|
---|
| 990 | if (v->per_target && v->export == v_default)
|
---|
| 991 | {
|
---|
| 992 | struct variable *gv;
|
---|
| 993 |
|
---|
| 994 | gv = lookup_variable_in_set (v->name, strlen(v->name),
|
---|
| 995 | &global_variable_set);
|
---|
| 996 | if (gv)
|
---|
| 997 | v->export = gv->export;
|
---|
| 998 | }
|
---|
| 999 |
|
---|
| 1000 | switch (v->export)
|
---|
| 1001 | {
|
---|
| 1002 | case v_default:
|
---|
| 1003 | if (v->origin == o_default || v->origin == o_automatic)
|
---|
| 1004 | /* Only export default variables by explicit request. */
|
---|
| 1005 | continue;
|
---|
| 1006 |
|
---|
| 1007 | /* The variable doesn't have a name that can be exported. */
|
---|
| 1008 | if (! v->exportable)
|
---|
| 1009 | continue;
|
---|
| 1010 |
|
---|
| 1011 | if (! export_all_variables
|
---|
| 1012 | && v->origin != o_command
|
---|
| 1013 | && v->origin != o_env && v->origin != o_env_override)
|
---|
| 1014 | continue;
|
---|
| 1015 | break;
|
---|
| 1016 |
|
---|
| 1017 | case v_export:
|
---|
| 1018 | break;
|
---|
| 1019 |
|
---|
| 1020 | case v_noexport:
|
---|
| 1021 | {
|
---|
| 1022 | /* If this is the SHELL variable and it's not exported,
|
---|
| 1023 | then add the value from our original environment, if
|
---|
| 1024 | the original environment defined a value for SHELL. */
|
---|
| 1025 | extern struct variable shell_var;
|
---|
| 1026 | if (streq (v->name, "SHELL") && shell_var.value)
|
---|
| 1027 | {
|
---|
| 1028 | v = &shell_var;
|
---|
| 1029 | break;
|
---|
| 1030 | }
|
---|
| 1031 | continue;
|
---|
| 1032 | }
|
---|
| 1033 |
|
---|
| 1034 | case v_ifset:
|
---|
| 1035 | if (v->origin == o_default)
|
---|
| 1036 | continue;
|
---|
| 1037 | break;
|
---|
| 1038 | }
|
---|
| 1039 |
|
---|
| 1040 | new_slot = (struct variable **) hash_find_slot (&table, v);
|
---|
| 1041 | if (HASH_VACANT (*new_slot))
|
---|
| 1042 | hash_insert_at (&table, v, new_slot);
|
---|
| 1043 | }
|
---|
| 1044 | }
|
---|
| 1045 |
|
---|
| 1046 | makelevel_key.name = MAKELEVEL_NAME;
|
---|
| 1047 | makelevel_key.length = MAKELEVEL_LENGTH;
|
---|
| 1048 | hash_delete (&table, &makelevel_key);
|
---|
| 1049 |
|
---|
| 1050 | result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
|
---|
| 1051 |
|
---|
| 1052 | v_slot = (struct variable **) table.ht_vec;
|
---|
| 1053 | v_end = v_slot + table.ht_size;
|
---|
| 1054 | for ( ; v_slot < v_end; v_slot++)
|
---|
| 1055 | if (! HASH_VACANT (*v_slot))
|
---|
| 1056 | {
|
---|
| 1057 | struct variable *v = *v_slot;
|
---|
| 1058 |
|
---|
| 1059 | /* If V is recursively expanded and didn't come from the environment,
|
---|
| 1060 | expand its value. If it came from the environment, it should
|
---|
| 1061 | go back into the environment unchanged. */
|
---|
| 1062 | if (v->recursive
|
---|
| 1063 | && v->origin != o_env && v->origin != o_env_override)
|
---|
| 1064 | {
|
---|
| 1065 | char *value = recursively_expand_for_file (v, file);
|
---|
| 1066 | #ifdef WINDOWS32
|
---|
| 1067 | if (strcmp(v->name, "Path") == 0 ||
|
---|
| 1068 | strcmp(v->name, "PATH") == 0)
|
---|
| 1069 | convert_Path_to_windows32(value, ';');
|
---|
| 1070 | #endif
|
---|
| 1071 | *result++ = xstrdup (concat (3, v->name, "=", value));
|
---|
| 1072 | free (value);
|
---|
| 1073 | }
|
---|
| 1074 | else
|
---|
| 1075 | {
|
---|
| 1076 | #ifdef WINDOWS32
|
---|
| 1077 | if (strcmp(v->name, "Path") == 0 ||
|
---|
| 1078 | strcmp(v->name, "PATH") == 0)
|
---|
| 1079 | convert_Path_to_windows32(v->value, ';');
|
---|
| 1080 | #endif
|
---|
| 1081 | *result++ = xstrdup (concat (3, v->name, "=", v->value));
|
---|
| 1082 | }
|
---|
| 1083 | }
|
---|
| 1084 |
|
---|
| 1085 | *result = xmalloc (100);
|
---|
| 1086 | sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
|
---|
| 1087 | *++result = 0;
|
---|
| 1088 |
|
---|
| 1089 | hash_free (&table, 0);
|
---|
| 1090 |
|
---|
| 1091 | return result_0;
|
---|
| 1092 | }
|
---|
| 1093 | |
---|
| 1094 |
|
---|
| 1095 | static struct variable *
|
---|
| 1096 | set_special_var (struct variable *var)
|
---|
| 1097 | {
|
---|
| 1098 | if (streq (var->name, RECIPEPREFIX_NAME))
|
---|
| 1099 | {
|
---|
| 1100 | /* The user is resetting the command introduction prefix. This has to
|
---|
| 1101 | happen immediately, so that subsequent rules are interpreted
|
---|
| 1102 | properly. */
|
---|
| 1103 | cmd_prefix = var->value[0]=='\0' ? RECIPEPREFIX_DEFAULT : var->value[0];
|
---|
| 1104 | }
|
---|
| 1105 |
|
---|
| 1106 | return var;
|
---|
| 1107 | }
|
---|
| 1108 | |
---|
| 1109 |
|
---|
| 1110 | /* Given a variable, a value, and a flavor, define the variable.
|
---|
| 1111 | See the try_variable_definition() function for details on the parameters. */
|
---|
| 1112 |
|
---|
| 1113 | struct variable *
|
---|
| 1114 | do_variable_definition (const struct floc *flocp, const char *varname,
|
---|
| 1115 | const char *value, enum variable_origin origin,
|
---|
| 1116 | enum variable_flavor flavor, int target_var)
|
---|
| 1117 | {
|
---|
| 1118 | const char *p;
|
---|
| 1119 | char *alloc_value = NULL;
|
---|
| 1120 | struct variable *v;
|
---|
| 1121 | int append = 0;
|
---|
| 1122 | int conditional = 0;
|
---|
| 1123 |
|
---|
| 1124 | /* Calculate the variable's new value in VALUE. */
|
---|
| 1125 |
|
---|
| 1126 | switch (flavor)
|
---|
| 1127 | {
|
---|
| 1128 | default:
|
---|
| 1129 | case f_bogus:
|
---|
| 1130 | /* Should not be possible. */
|
---|
| 1131 | abort ();
|
---|
| 1132 | case f_simple:
|
---|
| 1133 | /* A simple variable definition "var := value". Expand the value.
|
---|
| 1134 | We have to allocate memory since otherwise it'll clobber the
|
---|
| 1135 | variable buffer, and we may still need that if we're looking at a
|
---|
| 1136 | target-specific variable. */
|
---|
| 1137 | p = alloc_value = allocated_variable_expand (value);
|
---|
| 1138 | break;
|
---|
| 1139 | case f_conditional:
|
---|
| 1140 | /* A conditional variable definition "var ?= value".
|
---|
| 1141 | The value is set IFF the variable is not defined yet. */
|
---|
| 1142 | v = lookup_variable (varname, strlen (varname));
|
---|
| 1143 | if (v)
|
---|
| 1144 | return v->special ? set_special_var (v) : v;
|
---|
| 1145 |
|
---|
| 1146 | conditional = 1;
|
---|
| 1147 | flavor = f_recursive;
|
---|
| 1148 | /* FALLTHROUGH */
|
---|
| 1149 | case f_recursive:
|
---|
| 1150 | /* A recursive variable definition "var = value".
|
---|
| 1151 | The value is used verbatim. */
|
---|
| 1152 | p = value;
|
---|
| 1153 | break;
|
---|
| 1154 | case f_append:
|
---|
| 1155 | {
|
---|
| 1156 | /* If we have += but we're in a target variable context, we want to
|
---|
| 1157 | append only with other variables in the context of this target. */
|
---|
| 1158 | if (target_var)
|
---|
| 1159 | {
|
---|
| 1160 | append = 1;
|
---|
| 1161 | v = lookup_variable_in_set (varname, strlen (varname),
|
---|
| 1162 | current_variable_set_list->set);
|
---|
| 1163 |
|
---|
| 1164 | /* Don't append from the global set if a previous non-appending
|
---|
| 1165 | target-specific variable definition exists. */
|
---|
| 1166 | if (v && !v->append)
|
---|
| 1167 | append = 0;
|
---|
| 1168 | }
|
---|
| 1169 | else
|
---|
| 1170 | v = lookup_variable (varname, strlen (varname));
|
---|
| 1171 |
|
---|
| 1172 | if (v == 0)
|
---|
| 1173 | {
|
---|
| 1174 | /* There was no old value.
|
---|
| 1175 | This becomes a normal recursive definition. */
|
---|
| 1176 | p = value;
|
---|
| 1177 | flavor = f_recursive;
|
---|
| 1178 | }
|
---|
| 1179 | else
|
---|
| 1180 | {
|
---|
| 1181 | /* Paste the old and new values together in VALUE. */
|
---|
| 1182 |
|
---|
| 1183 | unsigned int oldlen, vallen;
|
---|
| 1184 | const char *val;
|
---|
| 1185 | char *tp = NULL;
|
---|
| 1186 |
|
---|
| 1187 | val = value;
|
---|
| 1188 | if (v->recursive)
|
---|
| 1189 | /* The previous definition of the variable was recursive.
|
---|
| 1190 | The new value is the unexpanded old and new values. */
|
---|
| 1191 | flavor = f_recursive;
|
---|
| 1192 | else
|
---|
| 1193 | /* The previous definition of the variable was simple.
|
---|
| 1194 | The new value comes from the old value, which was expanded
|
---|
| 1195 | when it was set; and from the expanded new value. Allocate
|
---|
| 1196 | memory for the expansion as we may still need the rest of the
|
---|
| 1197 | buffer if we're looking at a target-specific variable. */
|
---|
| 1198 | val = tp = allocated_variable_expand (val);
|
---|
| 1199 |
|
---|
| 1200 | oldlen = strlen (v->value);
|
---|
| 1201 | vallen = strlen (val);
|
---|
| 1202 | p = alloc_value = xmalloc (oldlen + 1 + vallen + 1);
|
---|
| 1203 | memcpy (alloc_value, v->value, oldlen);
|
---|
| 1204 | alloc_value[oldlen] = ' ';
|
---|
| 1205 | memcpy (&alloc_value[oldlen + 1], val, vallen + 1);
|
---|
| 1206 |
|
---|
| 1207 | if (tp)
|
---|
| 1208 | free (tp);
|
---|
| 1209 | }
|
---|
| 1210 | }
|
---|
| 1211 | }
|
---|
| 1212 |
|
---|
| 1213 | #ifdef __MSDOS__
|
---|
| 1214 | /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
|
---|
| 1215 | non-Unix systems don't conform to this default configuration (in
|
---|
| 1216 | fact, most of them don't even have `/bin'). On the other hand,
|
---|
| 1217 | $SHELL in the environment, if set, points to the real pathname of
|
---|
| 1218 | the shell.
|
---|
| 1219 | Therefore, we generally won't let lines like "SHELL=/bin/sh" from
|
---|
| 1220 | the Makefile override $SHELL from the environment. But first, we
|
---|
| 1221 | look for the basename of the shell in the directory where SHELL=
|
---|
| 1222 | points, and along the $PATH; if it is found in any of these places,
|
---|
| 1223 | we define $SHELL to be the actual pathname of the shell. Thus, if
|
---|
| 1224 | you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
|
---|
| 1225 | your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
|
---|
| 1226 | defining SHELL to be "d:/unix/bash.exe". */
|
---|
| 1227 | if ((origin == o_file || origin == o_override)
|
---|
| 1228 | && strcmp (varname, "SHELL") == 0)
|
---|
| 1229 | {
|
---|
| 1230 | PATH_VAR (shellpath);
|
---|
| 1231 | extern char * __dosexec_find_on_path (const char *, char *[], char *);
|
---|
| 1232 |
|
---|
| 1233 | /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
|
---|
| 1234 | if (__dosexec_find_on_path (p, NULL, shellpath))
|
---|
| 1235 | {
|
---|
| 1236 | char *tp;
|
---|
| 1237 |
|
---|
| 1238 | for (tp = shellpath; *tp; tp++)
|
---|
| 1239 | if (*tp == '\\')
|
---|
| 1240 | *tp = '/';
|
---|
| 1241 |
|
---|
| 1242 | v = define_variable_loc (varname, strlen (varname),
|
---|
| 1243 | shellpath, origin, flavor == f_recursive,
|
---|
| 1244 | flocp);
|
---|
| 1245 | }
|
---|
| 1246 | else
|
---|
| 1247 | {
|
---|
| 1248 | const char *shellbase, *bslash;
|
---|
| 1249 | struct variable *pathv = lookup_variable ("PATH", 4);
|
---|
| 1250 | char *path_string;
|
---|
| 1251 | char *fake_env[2];
|
---|
| 1252 | size_t pathlen = 0;
|
---|
| 1253 |
|
---|
| 1254 | shellbase = strrchr (p, '/');
|
---|
| 1255 | bslash = strrchr (p, '\\');
|
---|
| 1256 | if (!shellbase || bslash > shellbase)
|
---|
| 1257 | shellbase = bslash;
|
---|
| 1258 | if (!shellbase && p[1] == ':')
|
---|
| 1259 | shellbase = p + 1;
|
---|
| 1260 | if (shellbase)
|
---|
| 1261 | shellbase++;
|
---|
| 1262 | else
|
---|
| 1263 | shellbase = p;
|
---|
| 1264 |
|
---|
| 1265 | /* Search for the basename of the shell (with standard
|
---|
| 1266 | executable extensions) along the $PATH. */
|
---|
| 1267 | if (pathv)
|
---|
| 1268 | pathlen = strlen (pathv->value);
|
---|
| 1269 | path_string = xmalloc (5 + pathlen + 2 + 1);
|
---|
| 1270 | /* On MSDOS, current directory is considered as part of $PATH. */
|
---|
| 1271 | sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
|
---|
| 1272 | fake_env[0] = path_string;
|
---|
| 1273 | fake_env[1] = 0;
|
---|
| 1274 | if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
|
---|
| 1275 | {
|
---|
| 1276 | char *tp;
|
---|
| 1277 |
|
---|
| 1278 | for (tp = shellpath; *tp; tp++)
|
---|
| 1279 | if (*tp == '\\')
|
---|
| 1280 | *tp = '/';
|
---|
| 1281 |
|
---|
| 1282 | v = define_variable_loc (varname, strlen (varname),
|
---|
| 1283 | shellpath, origin,
|
---|
| 1284 | flavor == f_recursive, flocp);
|
---|
| 1285 | }
|
---|
| 1286 | else
|
---|
| 1287 | v = lookup_variable (varname, strlen (varname));
|
---|
| 1288 |
|
---|
| 1289 | free (path_string);
|
---|
| 1290 | }
|
---|
| 1291 | }
|
---|
| 1292 | else
|
---|
| 1293 | #endif /* __MSDOS__ */
|
---|
| 1294 | #ifdef WINDOWS32
|
---|
| 1295 | if ((origin == o_file || origin == o_override || origin == o_command)
|
---|
| 1296 | && streq (varname, "SHELL"))
|
---|
| 1297 | {
|
---|
| 1298 | extern char *default_shell;
|
---|
| 1299 |
|
---|
| 1300 | /* Call shell locator function. If it returns TRUE, then
|
---|
| 1301 | set no_default_sh_exe to indicate sh was found and
|
---|
| 1302 | set new value for SHELL variable. */
|
---|
| 1303 |
|
---|
| 1304 | if (find_and_set_default_shell (p))
|
---|
| 1305 | {
|
---|
| 1306 | v = define_variable_in_set (varname, strlen (varname), default_shell,
|
---|
| 1307 | origin, flavor == f_recursive,
|
---|
| 1308 | (target_var
|
---|
| 1309 | ? current_variable_set_list->set
|
---|
| 1310 | : NULL),
|
---|
| 1311 | flocp);
|
---|
| 1312 | no_default_sh_exe = 0;
|
---|
| 1313 | }
|
---|
| 1314 | else
|
---|
| 1315 | {
|
---|
| 1316 | char *tp = alloc_value;
|
---|
| 1317 |
|
---|
| 1318 | alloc_value = allocated_variable_expand (p);
|
---|
| 1319 |
|
---|
| 1320 | if (find_and_set_default_shell (alloc_value))
|
---|
| 1321 | {
|
---|
| 1322 | v = define_variable_in_set (varname, strlen (varname), p,
|
---|
| 1323 | origin, flavor == f_recursive,
|
---|
| 1324 | (target_var
|
---|
| 1325 | ? current_variable_set_list->set
|
---|
| 1326 | : NULL),
|
---|
| 1327 | flocp);
|
---|
| 1328 | no_default_sh_exe = 0;
|
---|
| 1329 | }
|
---|
| 1330 | else
|
---|
| 1331 | v = lookup_variable (varname, strlen (varname));
|
---|
| 1332 |
|
---|
| 1333 | if (tp)
|
---|
| 1334 | free (tp);
|
---|
| 1335 | }
|
---|
| 1336 | }
|
---|
| 1337 | else
|
---|
| 1338 | #endif
|
---|
| 1339 |
|
---|
| 1340 | /* If we are defining variables inside an $(eval ...), we might have a
|
---|
| 1341 | different variable context pushed, not the global context (maybe we're
|
---|
| 1342 | inside a $(call ...) or something. Since this function is only ever
|
---|
| 1343 | invoked in places where we want to define globally visible variables,
|
---|
| 1344 | make sure we define this variable in the global set. */
|
---|
| 1345 |
|
---|
| 1346 | v = define_variable_in_set (varname, strlen (varname), p,
|
---|
| 1347 | origin, flavor == f_recursive,
|
---|
| 1348 | (target_var
|
---|
| 1349 | ? current_variable_set_list->set : NULL),
|
---|
| 1350 | flocp);
|
---|
| 1351 | v->append = append;
|
---|
| 1352 | v->conditional = conditional;
|
---|
| 1353 |
|
---|
| 1354 | if (alloc_value)
|
---|
| 1355 | free (alloc_value);
|
---|
| 1356 |
|
---|
| 1357 | return v->special ? set_special_var (v) : v;
|
---|
| 1358 | }
|
---|
| 1359 | |
---|
| 1360 |
|
---|
| 1361 | /* Parse P (a null-terminated string) as a variable definition.
|
---|
| 1362 |
|
---|
| 1363 | If it is not a variable definition, return NULL.
|
---|
| 1364 |
|
---|
| 1365 | If it is a variable definition, return a pointer to the char after the
|
---|
| 1366 | assignment token and set *FLAVOR to the type of variable assignment. */
|
---|
| 1367 |
|
---|
| 1368 | char *
|
---|
| 1369 | parse_variable_definition (const char *p, enum variable_flavor *flavor)
|
---|
| 1370 | {
|
---|
| 1371 | int wspace = 0;
|
---|
| 1372 |
|
---|
| 1373 | p = next_token (p);
|
---|
| 1374 |
|
---|
| 1375 | while (1)
|
---|
| 1376 | {
|
---|
| 1377 | int c = *p++;
|
---|
| 1378 |
|
---|
| 1379 | /* If we find a comment or EOS, it's not a variable definition. */
|
---|
| 1380 | if (c == '\0' || c == '#')
|
---|
| 1381 | return NULL;
|
---|
| 1382 |
|
---|
| 1383 | if (c == '$')
|
---|
| 1384 | {
|
---|
| 1385 | /* This begins a variable expansion reference. Make sure we don't
|
---|
| 1386 | treat chars inside the reference as assignment tokens. */
|
---|
| 1387 | char closeparen;
|
---|
| 1388 | int count;
|
---|
| 1389 | c = *p++;
|
---|
| 1390 | if (c == '(')
|
---|
| 1391 | closeparen = ')';
|
---|
| 1392 | else if (c == '{')
|
---|
| 1393 | closeparen = '}';
|
---|
| 1394 | else
|
---|
| 1395 | /* '$$' or '$X'. Either way, nothing special to do here. */
|
---|
| 1396 | continue;
|
---|
| 1397 |
|
---|
| 1398 | /* P now points past the opening paren or brace.
|
---|
| 1399 | Count parens or braces until it is matched. */
|
---|
| 1400 | count = 0;
|
---|
| 1401 | for (; *p != '\0'; ++p)
|
---|
| 1402 | {
|
---|
| 1403 | if (*p == c)
|
---|
| 1404 | ++count;
|
---|
| 1405 | else if (*p == closeparen && --count < 0)
|
---|
| 1406 | {
|
---|
| 1407 | ++p;
|
---|
| 1408 | break;
|
---|
| 1409 | }
|
---|
| 1410 | }
|
---|
| 1411 | continue;
|
---|
| 1412 | }
|
---|
| 1413 |
|
---|
| 1414 | /* If we find whitespace skip it, and remember we found it. */
|
---|
| 1415 | if (isblank ((unsigned char)c))
|
---|
| 1416 | {
|
---|
| 1417 | wspace = 1;
|
---|
| 1418 | p = next_token (p);
|
---|
| 1419 | c = *p;
|
---|
| 1420 | if (c == '\0')
|
---|
| 1421 | return NULL;
|
---|
| 1422 | ++p;
|
---|
| 1423 | }
|
---|
| 1424 |
|
---|
| 1425 |
|
---|
| 1426 | if (c == '=')
|
---|
| 1427 | {
|
---|
| 1428 | *flavor = f_recursive;
|
---|
| 1429 | return (char *)p;
|
---|
| 1430 | }
|
---|
| 1431 |
|
---|
| 1432 | /* Match assignment variants (:=, +=, ?=) */
|
---|
| 1433 | if (*p == '=')
|
---|
| 1434 | {
|
---|
| 1435 | switch (c)
|
---|
| 1436 | {
|
---|
| 1437 | case ':':
|
---|
| 1438 | *flavor = f_simple;
|
---|
| 1439 | break;
|
---|
| 1440 | case '+':
|
---|
| 1441 | *flavor = f_append;
|
---|
| 1442 | break;
|
---|
| 1443 | case '?':
|
---|
| 1444 | *flavor = f_conditional;
|
---|
| 1445 | break;
|
---|
| 1446 | default:
|
---|
| 1447 | /* If we skipped whitespace, non-assignments means no var. */
|
---|
| 1448 | if (wspace)
|
---|
| 1449 | return NULL;
|
---|
| 1450 |
|
---|
| 1451 | /* Might be assignment, or might be $= or #=. Check. */
|
---|
| 1452 | continue;
|
---|
| 1453 | }
|
---|
| 1454 | return (char *)++p;
|
---|
| 1455 | }
|
---|
| 1456 | else if (c == ':')
|
---|
| 1457 | /* A colon other than := is a rule line, not a variable defn. */
|
---|
| 1458 | return NULL;
|
---|
| 1459 |
|
---|
| 1460 | /* If we skipped whitespace, non-assignments means no var. */
|
---|
| 1461 | if (wspace)
|
---|
| 1462 | return NULL;
|
---|
| 1463 | }
|
---|
| 1464 |
|
---|
| 1465 | return (char *)p;
|
---|
| 1466 | }
|
---|
| 1467 | |
---|
| 1468 |
|
---|
| 1469 | /* Try to interpret LINE (a null-terminated string) as a variable definition.
|
---|
| 1470 |
|
---|
| 1471 | If LINE was recognized as a variable definition, a pointer to its `struct
|
---|
| 1472 | variable' is returned. If LINE is not a variable definition, NULL is
|
---|
| 1473 | returned. */
|
---|
| 1474 |
|
---|
| 1475 | struct variable *
|
---|
| 1476 | assign_variable_definition (struct variable *v, char *line)
|
---|
| 1477 | {
|
---|
| 1478 | char *beg;
|
---|
| 1479 | char *end;
|
---|
| 1480 | enum variable_flavor flavor;
|
---|
| 1481 | char *name;
|
---|
| 1482 |
|
---|
| 1483 | beg = next_token (line);
|
---|
| 1484 | line = parse_variable_definition (beg, &flavor);
|
---|
| 1485 | if (!line)
|
---|
| 1486 | return NULL;
|
---|
| 1487 |
|
---|
| 1488 | end = line - (flavor == f_recursive ? 1 : 2);
|
---|
| 1489 | while (end > beg && isblank ((unsigned char)end[-1]))
|
---|
| 1490 | --end;
|
---|
| 1491 | line = next_token (line);
|
---|
| 1492 | v->value = line;
|
---|
| 1493 | v->flavor = flavor;
|
---|
| 1494 |
|
---|
| 1495 | /* Expand the name, so "$(foo)bar = baz" works. */
|
---|
| 1496 | name = alloca (end - beg + 1);
|
---|
| 1497 | memcpy (name, beg, end - beg);
|
---|
| 1498 | name[end - beg] = '\0';
|
---|
| 1499 | v->name = allocated_variable_expand (name);
|
---|
| 1500 |
|
---|
| 1501 | if (v->name[0] == '\0')
|
---|
| 1502 | fatal (&v->fileinfo, _("empty variable name"));
|
---|
| 1503 |
|
---|
| 1504 | return v;
|
---|
| 1505 | }
|
---|
| 1506 | |
---|
| 1507 |
|
---|
| 1508 | /* Try to interpret LINE (a null-terminated string) as a variable definition.
|
---|
| 1509 |
|
---|
| 1510 | ORIGIN may be o_file, o_override, o_env, o_env_override,
|
---|
| 1511 | or o_command specifying that the variable definition comes
|
---|
| 1512 | from a makefile, an override directive, the environment with
|
---|
| 1513 | or without the -e switch, or the command line.
|
---|
| 1514 |
|
---|
| 1515 | See the comments for assign_variable_definition().
|
---|
| 1516 |
|
---|
| 1517 | If LINE was recognized as a variable definition, a pointer to its `struct
|
---|
| 1518 | variable' is returned. If LINE is not a variable definition, NULL is
|
---|
| 1519 | returned. */
|
---|
| 1520 |
|
---|
| 1521 | struct variable *
|
---|
| 1522 | try_variable_definition (const struct floc *flocp, char *line,
|
---|
| 1523 | enum variable_origin origin, int target_var)
|
---|
| 1524 | {
|
---|
| 1525 | struct variable v;
|
---|
| 1526 | struct variable *vp;
|
---|
| 1527 |
|
---|
| 1528 | if (flocp != 0)
|
---|
| 1529 | v.fileinfo = *flocp;
|
---|
| 1530 | else
|
---|
| 1531 | v.fileinfo.filenm = 0;
|
---|
| 1532 |
|
---|
| 1533 | if (!assign_variable_definition (&v, line))
|
---|
| 1534 | return 0;
|
---|
| 1535 |
|
---|
| 1536 | vp = do_variable_definition (flocp, v.name, v.value,
|
---|
| 1537 | origin, v.flavor, target_var);
|
---|
| 1538 |
|
---|
| 1539 | free (v.name);
|
---|
| 1540 |
|
---|
| 1541 | return vp;
|
---|
| 1542 | }
|
---|
| 1543 | |
---|
| 1544 |
|
---|
| 1545 | /* Print information for variable V, prefixing it with PREFIX. */
|
---|
| 1546 |
|
---|
| 1547 | static void
|
---|
| 1548 | print_variable (const void *item, void *arg)
|
---|
| 1549 | {
|
---|
| 1550 | const struct variable *v = item;
|
---|
| 1551 | const char *prefix = arg;
|
---|
| 1552 | const char *origin;
|
---|
| 1553 |
|
---|
| 1554 | switch (v->origin)
|
---|
| 1555 | {
|
---|
| 1556 | case o_default:
|
---|
| 1557 | origin = _("default");
|
---|
| 1558 | break;
|
---|
| 1559 | case o_env:
|
---|
| 1560 | origin = _("environment");
|
---|
| 1561 | break;
|
---|
| 1562 | case o_file:
|
---|
| 1563 | origin = _("makefile");
|
---|
| 1564 | break;
|
---|
| 1565 | case o_env_override:
|
---|
| 1566 | origin = _("environment under -e");
|
---|
| 1567 | break;
|
---|
| 1568 | case o_command:
|
---|
| 1569 | origin = _("command line");
|
---|
| 1570 | break;
|
---|
| 1571 | case o_override:
|
---|
| 1572 | origin = _("`override' directive");
|
---|
| 1573 | break;
|
---|
| 1574 | case o_automatic:
|
---|
| 1575 | origin = _("automatic");
|
---|
| 1576 | break;
|
---|
| 1577 | case o_invalid:
|
---|
| 1578 | default:
|
---|
| 1579 | abort ();
|
---|
| 1580 | }
|
---|
| 1581 | fputs ("# ", stdout);
|
---|
| 1582 | fputs (origin, stdout);
|
---|
| 1583 | if (v->private_var)
|
---|
| 1584 | fputs (" private", stdout);
|
---|
| 1585 | if (v->fileinfo.filenm)
|
---|
| 1586 | printf (_(" (from `%s', line %lu)"),
|
---|
| 1587 | v->fileinfo.filenm, v->fileinfo.lineno);
|
---|
| 1588 | putchar ('\n');
|
---|
| 1589 | fputs (prefix, stdout);
|
---|
| 1590 |
|
---|
| 1591 | /* Is this a `define'? */
|
---|
| 1592 | if (v->recursive && strchr (v->value, '\n') != 0)
|
---|
| 1593 | printf ("define %s\n%s\nendef\n", v->name, v->value);
|
---|
| 1594 | else
|
---|
| 1595 | {
|
---|
| 1596 | char *p;
|
---|
| 1597 |
|
---|
| 1598 | printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
|
---|
| 1599 |
|
---|
| 1600 | /* Check if the value is just whitespace. */
|
---|
| 1601 | p = next_token (v->value);
|
---|
| 1602 | if (p != v->value && *p == '\0')
|
---|
| 1603 | /* All whitespace. */
|
---|
| 1604 | printf ("$(subst ,,%s)", v->value);
|
---|
| 1605 | else if (v->recursive)
|
---|
| 1606 | fputs (v->value, stdout);
|
---|
| 1607 | else
|
---|
| 1608 | /* Double up dollar signs. */
|
---|
| 1609 | for (p = v->value; *p != '\0'; ++p)
|
---|
| 1610 | {
|
---|
| 1611 | if (*p == '$')
|
---|
| 1612 | putchar ('$');
|
---|
| 1613 | putchar (*p);
|
---|
| 1614 | }
|
---|
| 1615 | putchar ('\n');
|
---|
| 1616 | }
|
---|
| 1617 | }
|
---|
| 1618 |
|
---|
| 1619 |
|
---|
| 1620 | /* Print all the variables in SET. PREFIX is printed before
|
---|
| 1621 | the actual variable definitions (everything else is comments). */
|
---|
| 1622 |
|
---|
| 1623 | void
|
---|
| 1624 | print_variable_set (struct variable_set *set, char *prefix)
|
---|
| 1625 | {
|
---|
| 1626 | hash_map_arg (&set->table, print_variable, prefix);
|
---|
| 1627 |
|
---|
| 1628 | fputs (_("# variable set hash-table stats:\n"), stdout);
|
---|
| 1629 | fputs ("# ", stdout);
|
---|
| 1630 | hash_print_stats (&set->table, stdout);
|
---|
| 1631 | putc ('\n', stdout);
|
---|
| 1632 | }
|
---|
| 1633 |
|
---|
| 1634 | /* Print the data base of variables. */
|
---|
| 1635 |
|
---|
| 1636 | void
|
---|
| 1637 | print_variable_data_base (void)
|
---|
| 1638 | {
|
---|
| 1639 | puts (_("\n# Variables\n"));
|
---|
| 1640 |
|
---|
| 1641 | print_variable_set (&global_variable_set, "");
|
---|
| 1642 |
|
---|
| 1643 | puts (_("\n# Pattern-specific Variable Values"));
|
---|
| 1644 |
|
---|
| 1645 | {
|
---|
| 1646 | struct pattern_var *p;
|
---|
| 1647 | int rules = 0;
|
---|
| 1648 |
|
---|
| 1649 | for (p = pattern_vars; p != 0; p = p->next)
|
---|
| 1650 | {
|
---|
| 1651 | ++rules;
|
---|
| 1652 | printf ("\n%s :\n", p->target);
|
---|
| 1653 | print_variable (&p->variable, "# ");
|
---|
| 1654 | }
|
---|
| 1655 |
|
---|
| 1656 | if (rules == 0)
|
---|
| 1657 | puts (_("\n# No pattern-specific variable values."));
|
---|
| 1658 | else
|
---|
| 1659 | printf (_("\n# %u pattern-specific variable values"), rules);
|
---|
| 1660 | }
|
---|
| 1661 | }
|
---|
| 1662 |
|
---|
| 1663 |
|
---|
| 1664 | /* Print all the local variables of FILE. */
|
---|
| 1665 |
|
---|
| 1666 | void
|
---|
| 1667 | print_file_variables (const struct file *file)
|
---|
| 1668 | {
|
---|
| 1669 | if (file->variables != 0)
|
---|
| 1670 | print_variable_set (file->variables->set, "# ");
|
---|
| 1671 | }
|
---|
| 1672 |
|
---|
| 1673 | #ifdef WINDOWS32
|
---|
| 1674 | void
|
---|
| 1675 | sync_Path_environment (void)
|
---|
| 1676 | {
|
---|
| 1677 | char *path = allocated_variable_expand ("$(PATH)");
|
---|
| 1678 | static char *environ_path = NULL;
|
---|
| 1679 |
|
---|
| 1680 | if (!path)
|
---|
| 1681 | return;
|
---|
| 1682 |
|
---|
| 1683 | /*
|
---|
| 1684 | * If done this before, don't leak memory unnecessarily.
|
---|
| 1685 | * Free the previous entry before allocating new one.
|
---|
| 1686 | */
|
---|
| 1687 | if (environ_path)
|
---|
| 1688 | free (environ_path);
|
---|
| 1689 |
|
---|
| 1690 | /*
|
---|
| 1691 | * Create something WINDOWS32 world can grok
|
---|
| 1692 | */
|
---|
| 1693 | convert_Path_to_windows32 (path, ';');
|
---|
| 1694 | environ_path = xstrdup (concat (3, "PATH", "=", path));
|
---|
| 1695 | putenv (environ_path);
|
---|
| 1696 | free (path);
|
---|
| 1697 | }
|
---|
| 1698 | #endif
|
---|