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.
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)
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.
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. */
29 #include "pathstuff.h"
33 /* Hash table of all global variable definitions. */
36 variable_hash_1 (keyv
)
39 struct variable
const *key
= (struct variable
const *) keyv
;
40 return_STRING_N_HASH_1 (key
->name
, key
->length
);
44 variable_hash_2 (keyv
)
47 struct variable
const *key
= (struct variable
const *) keyv
;
48 return_STRING_N_HASH_2 (key
->name
, key
->length
);
52 variable_hash_cmp (xv
, yv
)
56 struct variable
const *x
= (struct variable
const *) xv
;
57 struct variable
const *y
= (struct variable
const *) yv
;
58 int result
= x
->length
- y
->length
;
61 return_STRING_N_COMPARE (x
->name
, y
->name
, x
->length
);
64 #ifndef VARIABLE_BUCKETS
65 #define VARIABLE_BUCKETS 523
67 #ifndef PERFILE_VARIABLE_BUCKETS
68 #define PERFILE_VARIABLE_BUCKETS 23
70 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
71 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
74 static struct variable_set global_variable_set
;
75 static struct variable_set_list global_setlist
76 = { 0, &global_variable_set
};
77 struct variable_set_list
*current_variable_set_list
= &global_setlist
;
79 /* Implement variables. */
82 init_hash_global_variable_set ()
84 hash_init (&global_variable_set
.table
, VARIABLE_BUCKETS
,
85 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
88 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
89 LENGTH is the length of NAME, which does not need to be null-terminated.
90 ORIGIN specifies the origin of the variable (makefile, command line
92 If RECURSIVE is nonzero a flag is set in the variable saying
93 that it should be recursively re-expanded. */
96 define_variable_in_set (name
, length
, value
, origin
, recursive
, set
, flocp
)
100 enum variable_origin origin
;
102 struct variable_set
*set
;
103 const struct floc
*flocp
;
106 struct variable
**var_slot
;
107 struct variable var_key
;
110 set
= &global_variable_set
;
112 var_key
.name
= (char *) name
;
113 var_key
.length
= length
;
114 var_slot
= (struct variable
**) hash_find_slot (&set
->table
, &var_key
);
116 if (env_overrides
&& origin
== o_env
)
117 origin
= o_env_override
;
120 if (! HASH_VACANT (v
))
122 if (env_overrides
&& v
->origin
== o_env
)
123 /* V came from in the environment. Since it was defined
124 before the switches were parsed, it wasn't affected by -e. */
125 v
->origin
= o_env_override
;
127 /* A variable of this name is already defined.
128 If the old definition is from a stronger source
129 than this one, don't redefine it. */
130 if ((int) origin
>= (int) v
->origin
)
134 v
->value
= xstrdup (value
);
136 v
->fileinfo
= *flocp
;
138 v
->fileinfo
.filenm
= 0;
140 v
->recursive
= recursive
;
145 /* Create a new variable definition and add it to the hash table. */
147 v
= (struct variable
*) xmalloc (sizeof (struct variable
));
148 v
->name
= savestring (name
, length
);
150 hash_insert_at (&set
->table
, v
, var_slot
);
151 v
->value
= xstrdup (value
);
153 v
->fileinfo
= *flocp
;
155 v
->fileinfo
.filenm
= 0;
157 v
->recursive
= recursive
;
162 v
->export
= v_default
;
165 if (*name
!= '_' && (*name
< 'A' || *name
> 'Z')
166 && (*name
< 'a' || *name
> 'z'))
170 for (++name
; *name
!= '\0'; ++name
)
171 if (*name
!= '_' && (*name
< 'a' || *name
> 'z')
172 && (*name
< 'A' || *name
> 'Z') && !ISDIGIT(*name
))
182 /* If the variable passed in is "special", handle its special nature.
183 Currently there are two such variables, both used for introspection:
184 .VARIABLES expands to a list of all the variables defined in this instance
186 .TARGETS expands to a list of all the targets defined in this
188 Returns the variable reference passed in. */
190 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
192 static struct variable
*
193 handle_special_var (var
)
194 struct variable
*var
;
196 static unsigned long last_var_count
= 0;
199 /* This one actually turns out to be very hard, due to the way the parser
200 records targets. The way it works is that target information is collected
201 internally until make knows the target is completely specified. It unitl
202 it sees that some new construct (a new target or variable) is defined that
203 it knows the previous one is done. In short, this means that if you do
210 then $(TARGS) won't contain "all", because it's not until after the
211 variable is created that the previous target is completed.
213 Changing this would be a major pain. I think a less complex way to do it
214 would be to pre-define the target files as soon as the first line is
215 parsed, then come back and do the rest of the definition as now. That
216 would allow $(.TARGETS) to be correct without a major change to the way
219 if (streq (var->name, ".TARGETS"))
220 var->value = build_target_list (var->value);
224 if (streq (var
->name
, ".VARIABLES")
225 && global_variable_set
.table
.ht_fill
!= last_var_count
)
227 unsigned long max
= EXPANSION_INCREMENT (strlen (var
->value
));
230 struct variable
**vp
= (struct variable
**) global_variable_set
.table
.ht_vec
;
231 struct variable
**end
= &vp
[global_variable_set
.table
.ht_size
];
233 /* Make sure we have at least MAX bytes in the allocated buffer. */
234 var
->value
= xrealloc (var
->value
, max
);
236 /* Walk through the hash of variables, constructing a list of names. */
239 for (; vp
< end
; ++vp
)
240 if (!HASH_VACANT (*vp
))
242 struct variable
*v
= *vp
;
248 unsigned long off
= p
- var
->value
;
250 max
+= EXPANSION_INCREMENT (l
+ 1);
251 var
->value
= xrealloc (var
->value
, max
);
252 p
= &var
->value
[off
];
255 bcopy (v
->name
, p
, l
);
261 /* Remember how many variables are in our current count. Since we never
262 remove variables from the list, this is a reliable way to know whether
263 the list is up to date or needs to be recomputed. */
265 last_var_count
= global_variable_set
.table
.ht_fill
;
272 /* Lookup a variable whose name is a string starting at NAME
273 and with LENGTH chars. NAME need not be null-terminated.
274 Returns address of the `struct variable' containing all info
275 on the variable, or nil if no such variable is defined. */
278 lookup_variable (name
, length
)
282 const struct variable_set_list
*setlist
;
283 struct variable var_key
;
285 var_key
.name
= (char *) name
;
286 var_key
.length
= length
;
288 for (setlist
= current_variable_set_list
;
289 setlist
!= 0; setlist
= setlist
->next
)
291 const struct variable_set
*set
= setlist
->set
;
294 v
= (struct variable
*) hash_find_item ((struct hash_table
*) &set
->table
, &var_key
);
296 return v
->special
? handle_special_var (v
) : v
;
300 /* since we don't read envp[] on startup, try to get the
301 variable via getenv() here. */
303 char *vname
= alloca (length
+ 1);
305 strncpy (vname
, name
, length
);
307 value
= getenv (vname
);
316 while ((sptr
= strchr (sptr
, '$')))
327 nvalue
= alloca (strlen (value
) + scnt
+ 1);
346 return define_variable (vname
, length
, nvalue
, o_env
, 1);
350 return define_variable (vname
, length
, value
, o_env
, 1);
358 /* Lookup a variable whose name is a string starting at NAME
359 and with LENGTH chars in set SET. NAME need not be null-terminated.
360 Returns address of the `struct variable' containing all info
361 on the variable, or nil if no such variable is defined. */
364 lookup_variable_in_set (name
, length
, set
)
367 const struct variable_set
*set
;
369 struct variable var_key
;
371 var_key
.name
= (char *) name
;
372 var_key
.length
= length
;
374 return (struct variable
*) hash_find_item ((struct hash_table
*) &set
->table
, &var_key
);
377 /* Initialize FILE's variable set list. If FILE already has a variable set
378 list, the topmost variable set is left intact, but the the rest of the
379 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
380 rule, then we will use the "root" double-colon target's variable set as the
381 parent of FILE's variable set.
383 If we're READing a makefile, don't do the pattern variable search now,
384 since the pattern variable might not have been defined yet. */
387 initialize_file_variables (file
, reading
)
391 register struct variable_set_list
*l
= file
->variables
;
395 l
= (struct variable_set_list
*)
396 xmalloc (sizeof (struct variable_set_list
));
397 l
->set
= (struct variable_set
*) xmalloc (sizeof (struct variable_set
));
398 hash_init (&l
->set
->table
, PERFILE_VARIABLE_BUCKETS
,
399 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
403 /* If this is a double-colon, then our "parent" is the "root" target for
404 this double-colon rule. Since that rule has the same name, parent,
405 etc. we can just use its variables as the "next" for ours. */
407 if (file
->double_colon
&& file
->double_colon
!= file
)
409 initialize_file_variables (file
->double_colon
, reading
);
410 l
->next
= file
->double_colon
->variables
;
414 if (file
->parent
== 0)
415 l
->next
= &global_setlist
;
418 initialize_file_variables (file
->parent
, reading
);
419 l
->next
= file
->parent
->variables
;
422 /* If we're not reading makefiles and we haven't looked yet, see if
423 we can find a pattern variable. */
425 if (!reading
&& !file
->pat_searched
)
427 struct pattern_var
*p
= lookup_pattern_var (file
->name
);
429 file
->pat_searched
= 1;
432 /* If we found one, insert it between the current target's
433 variables and the next set, whatever it is. */
434 file
->pat_variables
= (struct variable_set_list
*)
435 xmalloc (sizeof (struct variable_set_list
));
436 file
->pat_variables
->set
= p
->vars
->set
;
440 /* If we have a pattern variable match, set it up. */
442 if (file
->pat_variables
!= 0)
444 file
->pat_variables
->next
= l
->next
;
445 l
->next
= file
->pat_variables
;
449 /* Pop the top set off the current variable set list,
450 and free all its storage. */
453 free_variable_name_and_value (item
)
456 struct variable
*v
= (struct variable
*) item
;
462 pop_variable_scope ()
464 struct variable_set_list
*setlist
= current_variable_set_list
;
465 struct variable_set
*set
= setlist
->set
;
467 current_variable_set_list
= setlist
->next
;
468 free ((char *) setlist
);
470 hash_map (&set
->table
, free_variable_name_and_value
);
471 hash_free (&set
->table
, 1);
476 struct variable_set_list
*
477 create_new_variable_set ()
479 register struct variable_set_list
*setlist
;
480 register struct variable_set
*set
;
482 set
= (struct variable_set
*) xmalloc (sizeof (struct variable_set
));
483 hash_init (&set
->table
, SMALL_SCOPE_VARIABLE_BUCKETS
,
484 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
486 setlist
= (struct variable_set_list
*)
487 xmalloc (sizeof (struct variable_set_list
));
489 setlist
->next
= current_variable_set_list
;
494 /* Create a new variable set and push it on the current setlist. */
496 struct variable_set_list
*
497 push_new_variable_scope ()
499 return (current_variable_set_list
= create_new_variable_set());
502 /* Merge SET1 into SET0, freeing unused storage in SET1. */
505 merge_variable_sets (to_set
, from_set
)
506 struct variable_set
*to_set
, *from_set
;
508 struct variable
**from_var_slot
= (struct variable
**) from_set
->table
.ht_vec
;
509 struct variable
**from_var_end
= from_var_slot
+ from_set
->table
.ht_size
;
511 for ( ; from_var_slot
< from_var_end
; from_var_slot
++)
512 if (! HASH_VACANT (*from_var_slot
))
514 struct variable
*from_var
= *from_var_slot
;
515 struct variable
**to_var_slot
516 = (struct variable
**) hash_find_slot (&to_set
->table
, *from_var_slot
);
517 if (HASH_VACANT (*to_var_slot
))
518 hash_insert_at (&to_set
->table
, from_var
, to_var_slot
);
521 /* GKM FIXME: delete in from_set->table */
522 free (from_var
->value
);
528 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
531 merge_variable_set_lists (setlist0
, setlist1
)
532 struct variable_set_list
**setlist0
, *setlist1
;
534 register struct variable_set_list
*list0
= *setlist0
;
535 struct variable_set_list
*last0
= 0;
537 while (setlist1
!= 0 && list0
!= 0)
539 struct variable_set_list
*next
= setlist1
;
540 setlist1
= setlist1
->next
;
542 merge_variable_sets (list0
->set
, next
->set
);
551 *setlist0
= setlist1
;
553 last0
->next
= setlist1
;
557 /* Define the automatic variables, and record the addresses
558 of their structures so we can change their values quickly. */
561 define_automatic_variables ()
564 extern char* default_shell
;
566 extern char default_shell
[];
568 register struct variable
*v
;
571 sprintf (buf
, "%u", makelevel
);
572 (void) define_variable (MAKELEVEL_NAME
, MAKELEVEL_LENGTH
, buf
, o_env
, 0);
574 sprintf (buf
, "%s%s%s",
576 (remote_description
== 0 || remote_description
[0] == '\0')
578 (remote_description
== 0 || remote_description
[0] == '\0')
579 ? "" : remote_description
);
580 (void) define_variable ("MAKE_VERSION", 12, buf
, o_default
, 0);
583 /* Allow to specify a special shell just for Make,
584 and use $COMSPEC as the default $SHELL when appropriate. */
586 static char shell_str
[] = "SHELL";
587 const int shlen
= sizeof (shell_str
) - 1;
588 struct variable
*mshp
= lookup_variable ("MAKESHELL", 9);
589 struct variable
*comp
= lookup_variable ("COMSPEC", 7);
591 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
593 (void) define_variable (shell_str
, shlen
,
594 mshp
->value
, o_env_override
, 0);
597 /* $COMSPEC shouldn't override $SHELL. */
598 struct variable
*shp
= lookup_variable (shell_str
, shlen
);
601 (void) define_variable (shell_str
, shlen
, comp
->value
, o_env
, 0);
606 /* This won't override any definition, but it
607 will provide one if there isn't one there. */
608 v
= define_variable ("SHELL", 5, default_shell
, o_default
, 0);
609 v
->export
= v_export
; /* Always export SHELL. */
611 /* On MSDOS we do use SHELL from environment, since
612 it isn't a standard environment variable on MSDOS,
613 so whoever sets it, does that on purpose. */
615 /* Don't let SHELL come from the environment. */
616 if (*v
->value
== '\0' || v
->origin
== o_env
|| v
->origin
== o_env_override
)
620 v
->value
= xstrdup (default_shell
);
624 /* Make sure MAKEFILES gets exported if it is set. */
625 v
= define_variable ("MAKEFILES", 9, "", o_default
, 0);
628 /* Define the magic D and F variables in terms of
629 the automatic variables they are variations of. */
632 define_variable ("@D", 2, "$(dir $@)", o_automatic
, 1);
633 define_variable ("%D", 2, "$(dir $%)", o_automatic
, 1);
634 define_variable ("*D", 2, "$(dir $*)", o_automatic
, 1);
635 define_variable ("<D", 2, "$(dir $<)", o_automatic
, 1);
636 define_variable ("?D", 2, "$(dir $?)", o_automatic
, 1);
637 define_variable ("^D", 2, "$(dir $^)", o_automatic
, 1);
638 define_variable ("+D", 2, "$(dir $+)", o_automatic
, 1);
640 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic
, 1);
641 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic
, 1);
642 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic
, 1);
643 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic
, 1);
644 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic
, 1);
645 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic
, 1);
646 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic
, 1);
648 define_variable ("@F", 2, "$(notdir $@)", o_automatic
, 1);
649 define_variable ("%F", 2, "$(notdir $%)", o_automatic
, 1);
650 define_variable ("*F", 2, "$(notdir $*)", o_automatic
, 1);
651 define_variable ("<F", 2, "$(notdir $<)", o_automatic
, 1);
652 define_variable ("?F", 2, "$(notdir $?)", o_automatic
, 1);
653 define_variable ("^F", 2, "$(notdir $^)", o_automatic
, 1);
654 define_variable ("+F", 2, "$(notdir $+)", o_automatic
, 1);
657 int export_all_variables
;
659 /* Create a new environment for FILE's commands.
660 If FILE is nil, this is for the `shell' function.
661 The child's MAKELEVEL variable is incremented. */
664 target_environment (file
)
667 struct variable_set_list
*set_list
;
668 register struct variable_set_list
*s
;
669 struct hash_table table
;
670 struct variable
**v_slot
;
671 struct variable
**v_end
;
672 struct variable makelevel_key
;
677 set_list
= current_variable_set_list
;
679 set_list
= file
->variables
;
681 hash_init (&table
, VARIABLE_BUCKETS
,
682 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
684 /* Run through all the variable sets in the list,
685 accumulating variables in TABLE. */
686 for (s
= set_list
; s
!= 0; s
= s
->next
)
688 struct variable_set
*set
= s
->set
;
689 v_slot
= (struct variable
**) set
->table
.ht_vec
;
690 v_end
= v_slot
+ set
->table
.ht_size
;
691 for ( ; v_slot
< v_end
; v_slot
++)
692 if (! HASH_VACANT (*v_slot
))
694 struct variable
**new_slot
;
695 struct variable
*v
= *v_slot
;
697 /* If this is a per-target variable and it hasn't been touched
698 already then look up the global version and take its export
700 if (v
->per_target
&& v
->export
== v_default
)
704 gv
= lookup_variable_in_set (v
->name
, strlen(v
->name
),
705 &global_variable_set
);
707 v
->export
= gv
->export
;
713 if (v
->origin
== o_default
|| v
->origin
== o_automatic
)
714 /* Only export default variables by explicit request. */
717 /* The variable doesn't have a name that can be exported. */
721 if (! export_all_variables
722 && v
->origin
!= o_command
723 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
734 if (v
->origin
== o_default
)
739 new_slot
= (struct variable
**) hash_find_slot (&table
, v
);
740 if (HASH_VACANT (*new_slot
))
741 hash_insert_at (&table
, v
, new_slot
);
745 makelevel_key
.name
= MAKELEVEL_NAME
;
746 makelevel_key
.length
= MAKELEVEL_LENGTH
;
747 hash_delete (&table
, &makelevel_key
);
749 result
= result_0
= (char **) xmalloc ((table
.ht_fill
+ 2) * sizeof (char *));
751 v_slot
= (struct variable
**) table
.ht_vec
;
752 v_end
= v_slot
+ table
.ht_size
;
753 for ( ; v_slot
< v_end
; v_slot
++)
754 if (! HASH_VACANT (*v_slot
))
756 struct variable
*v
= *v_slot
;
758 /* If V is recursively expanded and didn't come from the environment,
759 expand its value. If it came from the environment, it should
760 go back into the environment unchanged. */
762 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
764 char *value
= recursively_expand_for_file (v
, file
);
766 if (strcmp(v
->name
, "Path") == 0 ||
767 strcmp(v
->name
, "PATH") == 0)
768 convert_Path_to_windows32(value
, ';');
770 *result
++ = concat (v
->name
, "=", value
);
776 if (strcmp(v
->name
, "Path") == 0 ||
777 strcmp(v
->name
, "PATH") == 0)
778 convert_Path_to_windows32(v
->value
, ';');
780 *result
++ = concat (v
->name
, "=", v
->value
);
784 *result
= (char *) xmalloc (100);
785 (void) sprintf (*result
, "%s=%u", MAKELEVEL_NAME
, makelevel
+ 1);
788 hash_free (&table
, 0);
793 /* Given a variable, a value, and a flavor, define the variable.
794 See the try_variable_definition() function for details on the parameters. */
797 do_variable_definition (flocp
, varname
, value
, origin
, flavor
, target_var
)
798 const struct floc
*flocp
;
801 enum variable_origin origin
;
802 enum variable_flavor flavor
;
805 char *p
, *alloc_value
= NULL
;
809 /* Calculate the variable's new value in VALUE. */
815 /* Should not be possible. */
818 /* A simple variable definition "var := value". Expand the value.
819 We have to allocate memory since otherwise it'll clobber the
820 variable buffer, and we may still need that if we're looking at a
821 target-specific variable. */
822 p
= alloc_value
= allocated_variable_expand (value
);
825 /* A conditional variable definition "var ?= value".
826 The value is set IFF the variable is not defined yet. */
827 v
= lookup_variable (varname
, strlen (varname
));
831 flavor
= f_recursive
;
834 /* A recursive variable definition "var = value".
835 The value is used verbatim. */
840 /* If we have += but we're in a target variable context, we want to
841 append only with other variables in the context of this target. */
845 v
= lookup_variable_in_set (varname
, strlen (varname
),
846 current_variable_set_list
->set
);
849 v
= lookup_variable (varname
, strlen (varname
));
853 /* There was no old value.
854 This becomes a normal recursive definition. */
856 flavor
= f_recursive
;
860 /* Paste the old and new values together in VALUE. */
862 unsigned int oldlen
, vallen
;
867 /* The previous definition of the variable was recursive.
868 The new value is the unexpanded old and new values. */
869 flavor
= f_recursive
;
871 /* The previous definition of the variable was simple.
872 The new value comes from the old value, which was expanded
873 when it was set; and from the expanded new value. Allocate
874 memory for the expansion as we may still need the rest of the
875 buffer if we're looking at a target-specific variable. */
876 val
= alloc_value
= allocated_variable_expand (val
);
878 oldlen
= strlen (v
->value
);
879 vallen
= strlen (val
);
880 p
= (char *) alloca (oldlen
+ 1 + vallen
+ 1);
881 bcopy (v
->value
, p
, oldlen
);
883 bcopy (val
, &p
[oldlen
+ 1], vallen
+ 1);
889 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
890 non-Unix systems don't conform to this default configuration (in
891 fact, most of them don't even have `/bin'). On the other hand,
892 $SHELL in the environment, if set, points to the real pathname of
894 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
895 the Makefile override $SHELL from the environment. But first, we
896 look for the basename of the shell in the directory where SHELL=
897 points, and along the $PATH; if it is found in any of these places,
898 we define $SHELL to be the actual pathname of the shell. Thus, if
899 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
900 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
901 defining SHELL to be "d:/unix/bash.exe". */
902 if ((origin
== o_file
|| origin
== o_override
)
903 && strcmp (varname
, "SHELL") == 0)
905 char shellpath
[PATH_MAX
];
906 extern char * __dosexec_find_on_path (const char *, char *[], char *);
908 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
909 if (__dosexec_find_on_path (p
, (char **)0, shellpath
))
913 for (p
= shellpath
; *p
; p
++)
918 v
= define_variable_loc (varname
, strlen (varname
),
919 shellpath
, origin
, flavor
== f_recursive
,
924 char *shellbase
, *bslash
;
925 struct variable
*pathv
= lookup_variable ("PATH", 4);
930 shellbase
= strrchr (p
, '/');
931 bslash
= strrchr (p
, '\\');
932 if (!shellbase
|| bslash
> shellbase
)
934 if (!shellbase
&& p
[1] == ':')
941 /* Search for the basename of the shell (with standard
942 executable extensions) along the $PATH. */
944 pathlen
= strlen (pathv
->value
);
945 path_string
= (char *)xmalloc (5 + pathlen
+ 2 + 1);
946 /* On MSDOS, current directory is considered as part of $PATH. */
947 sprintf (path_string
, "PATH=.;%s", pathv
? pathv
->value
: "");
948 fake_env
[0] = path_string
;
949 fake_env
[1] = (char *)0;
950 if (__dosexec_find_on_path (shellbase
, fake_env
, shellpath
))
954 for (p
= shellpath
; *p
; p
++)
959 v
= define_variable_loc (varname
, strlen (varname
),
961 flavor
== f_recursive
, flocp
);
964 v
= lookup_variable (varname
, strlen (varname
));
970 #endif /* __MSDOS__ */
972 if ((origin
== o_file
|| origin
== o_override
) && streq (varname
, "SHELL"))
974 extern char *default_shell
;
976 /* Call shell locator function. If it returns TRUE, then
977 set no_default_sh_exe to indicate sh was found and
978 set new value for SHELL variable. */
980 if (find_and_set_default_shell (p
))
982 v
= define_variable_in_set (varname
, strlen (varname
), default_shell
,
983 origin
, flavor
== f_recursive
,
985 ? current_variable_set_list
->set
988 no_default_sh_exe
= 0;
991 v
= lookup_variable (varname
, strlen (varname
));
996 /* If we are defining variables inside an $(eval ...), we might have a
997 different variable context pushed, not the global context (maybe we're
998 inside a $(call ...) or something. Since this function is only ever
999 invoked in places where we want to define globally visible variables,
1000 make sure we define this variable in the global set. */
1002 v
= define_variable_in_set (varname
, strlen (varname
), p
,
1003 origin
, flavor
== f_recursive
,
1005 ? current_variable_set_list
->set
: NULL
),
1015 /* Try to interpret LINE (a null-terminated string) as a variable definition.
1017 ORIGIN may be o_file, o_override, o_env, o_env_override,
1018 or o_command specifying that the variable definition comes
1019 from a makefile, an override directive, the environment with
1020 or without the -e switch, or the command line.
1022 See the comments for parse_variable_definition().
1024 If LINE was recognized as a variable definition, a pointer to its `struct
1025 variable' is returned. If LINE is not a variable definition, NULL is
1029 try_variable_definition (flocp
, line
, origin
, target_var
)
1030 const struct floc
*flocp
;
1032 enum variable_origin origin
;
1036 register char *p
= line
;
1039 enum variable_flavor flavor
= f_bogus
;
1040 char *name
, *expanded_name
;
1046 if (c
== '\0' || c
== '#')
1051 flavor
= f_recursive
;
1062 /* A colon other than := is a rule line, not a variable defn. */
1064 else if (c
== '+' && *p
== '=')
1070 else if (c
== '?' && *p
== '=')
1073 flavor
= f_conditional
;
1078 /* This might begin a variable expansion reference. Make sure we
1079 don't misrecognize chars inside the reference as =, := or +=. */
1088 continue; /* Nope. */
1090 /* P now points past the opening paren or brace.
1091 Count parens or braces until it is matched. */
1093 for (; *p
!= '\0'; ++p
)
1097 else if (*p
== closeparen
&& --count
< 0)
1106 beg
= next_token (line
);
1107 while (end
> beg
&& isblank ((unsigned char)end
[-1]))
1111 /* Expand the name, so "$(foo)bar = baz" works. */
1112 name
= (char *) alloca (end
- beg
+ 1);
1113 bcopy (beg
, name
, end
- beg
);
1114 name
[end
- beg
] = '\0';
1115 expanded_name
= allocated_variable_expand (name
);
1117 if (expanded_name
[0] == '\0')
1118 fatal (flocp
, _("empty variable name"));
1120 v
= do_variable_definition (flocp
, expanded_name
, p
,
1121 origin
, flavor
, target_var
);
1123 free (expanded_name
);
1128 /* Print information for variable V, prefixing it with PREFIX. */
1131 print_variable (v
, prefix
)
1132 register struct variable
*v
;
1140 origin
= _("default");
1143 origin
= _("environment");
1146 origin
= _("makefile");
1148 case o_env_override
:
1149 origin
= _("environment under -e");
1152 origin
= _("command line");
1155 origin
= _("`override' directive");
1158 origin
= _("automatic");
1164 fputs ("# ", stdout
);
1165 fputs (origin
, stdout
);
1166 if (v
->fileinfo
.filenm
)
1167 printf (_(" (from `%s', line %lu)"),
1168 v
->fileinfo
.filenm
, v
->fileinfo
.lineno
);
1170 fputs (prefix
, stdout
);
1172 /* Is this a `define'? */
1173 if (v
->recursive
&& strchr (v
->value
, '\n') != 0)
1174 printf ("define %s\n%s\nendef\n", v
->name
, v
->value
);
1179 printf ("%s %s= ", v
->name
, v
->recursive
? v
->append
? "+" : "" : ":");
1181 /* Check if the value is just whitespace. */
1182 p
= next_token (v
->value
);
1183 if (p
!= v
->value
&& *p
== '\0')
1184 /* All whitespace. */
1185 printf ("$(subst ,,%s)", v
->value
);
1186 else if (v
->recursive
)
1187 fputs (v
->value
, stdout
);
1189 /* Double up dollar signs. */
1190 for (p
= v
->value
; *p
!= '\0'; ++p
)
1201 /* Print all the variables in SET. PREFIX is printed before
1202 the actual variable definitions (everything else is comments). */
1205 print_variable_set (set
, prefix
)
1206 register struct variable_set
*set
;
1209 hash_map_arg (&set
->table
, print_variable
, prefix
);
1211 fputs (_("# variable set hash-table stats:\n"), stdout
);
1212 fputs ("# ", stdout
);
1213 hash_print_stats (&set
->table
, stdout
);
1214 putc ('\n', stdout
);
1217 /* Print the data base of variables. */
1220 print_variable_data_base ()
1222 puts (_("\n# Variables\n"));
1224 print_variable_set (&global_variable_set
, "");
1228 /* Print all the local variables of FILE. */
1231 print_file_variables (file
)
1234 if (file
->variables
!= 0)
1235 print_variable_set (file
->variables
->set
, "# ");
1240 sync_Path_environment(void)
1242 char* path
= allocated_variable_expand("$(Path)");
1243 static char* environ_path
= NULL
;
1249 * If done this before, don't leak memory unnecessarily.
1250 * Free the previous entry before allocating new one.
1256 * Create something WINDOWS32 world can grok
1258 convert_Path_to_windows32(path
, ';');
1259 environ_path
= concat("Path", "=", path
);
1260 putenv(environ_path
);