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