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