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 (void const *keyv
)
38 struct variable
const *key
= (struct variable
const *) keyv
;
39 return_STRING_N_HASH_1 (key
->name
, key
->length
);
43 variable_hash_2 (void const *keyv
)
45 struct variable
const *key
= (struct variable
const *) keyv
;
46 return_STRING_N_HASH_2 (key
->name
, key
->length
);
50 variable_hash_cmp (void const *xv
, void const *yv
)
52 struct variable
const *x
= (struct variable
const *) xv
;
53 struct variable
const *y
= (struct variable
const *) yv
;
54 int result
= x
->length
- y
->length
;
57 return_STRING_N_COMPARE (x
->name
, y
->name
, x
->length
);
60 #ifndef VARIABLE_BUCKETS
61 #define VARIABLE_BUCKETS 523
63 #ifndef PERFILE_VARIABLE_BUCKETS
64 #define PERFILE_VARIABLE_BUCKETS 23
66 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
67 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
70 static struct variable_set global_variable_set
;
71 static struct variable_set_list global_setlist
72 = { 0, &global_variable_set
};
73 struct variable_set_list
*current_variable_set_list
= &global_setlist
;
75 /* Implement variables. */
78 init_hash_global_variable_set ()
80 hash_init (&global_variable_set
.table
, VARIABLE_BUCKETS
,
81 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
84 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
85 LENGTH is the length of NAME, which does not need to be null-terminated.
86 ORIGIN specifies the origin of the variable (makefile, command line
88 If RECURSIVE is nonzero a flag is set in the variable saying
89 that it should be recursively re-expanded. */
92 define_variable_in_set (name
, length
, value
, origin
, recursive
, set
, flocp
)
96 enum variable_origin origin
;
98 struct variable_set
*set
;
99 const struct floc
*flocp
;
102 struct variable
**var_slot
;
103 struct variable var_key
;
106 set
= &global_variable_set
;
108 var_key
.name
= (char *) name
;
109 var_key
.length
= length
;
110 var_slot
= (struct variable
**) hash_find_slot (&set
->table
, &var_key
);
112 if (env_overrides
&& origin
== o_env
)
113 origin
= o_env_override
;
116 if (! HASH_VACANT (v
))
118 if (env_overrides
&& v
->origin
== o_env
)
119 /* V came from in the environment. Since it was defined
120 before the switches were parsed, it wasn't affected by -e. */
121 v
->origin
= o_env_override
;
123 /* A variable of this name is already defined.
124 If the old definition is from a stronger source
125 than this one, don't redefine it. */
126 if ((int) origin
>= (int) v
->origin
)
130 v
->value
= xstrdup (value
);
132 v
->fileinfo
= *flocp
;
134 v
->fileinfo
.filenm
= 0;
136 v
->recursive
= recursive
;
141 /* Create a new variable definition and add it to the hash table. */
143 v
= (struct variable
*) xmalloc (sizeof (struct variable
));
144 v
->name
= savestring (name
, length
);
146 hash_insert_at (&set
->table
, v
, var_slot
);
147 v
->value
= xstrdup (value
);
149 v
->fileinfo
= *flocp
;
151 v
->fileinfo
.filenm
= 0;
153 v
->recursive
= recursive
;
158 v
->export
= v_default
;
161 if (*name
!= '_' && (*name
< 'A' || *name
> 'Z')
162 && (*name
< 'a' || *name
> 'z'))
166 for (++name
; *name
!= '\0'; ++name
)
167 if (*name
!= '_' && (*name
< 'a' || *name
> 'z')
168 && (*name
< 'A' || *name
> 'Z') && !ISDIGIT(*name
))
178 /* If the variable passed in is "special", handle its special nature.
179 Currently there are two such variables, both used for introspection:
180 .VARIABLES expands to a list of all the variables defined in this instance
182 .TARGETS expands to a list of all the targets defined in this
184 Returns the variable reference passed in. */
186 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
188 static struct variable
*
189 handle_special_var (var
)
190 struct variable
*var
;
192 static unsigned long last_var_count
= 0;
195 /* This one actually turns out to be very hard, due to the way the parser
196 records targets. The way it works is that target information is collected
197 internally until make knows the target is completely specified. It unitl
198 it sees that some new construct (a new target or variable) is defined that
199 it knows the previous one is done. In short, this means that if you do
206 then $(TARGS) won't contain "all", because it's not until after the
207 variable is created that the previous target is completed.
209 Changing this would be a major pain. I think a less complex way to do it
210 would be to pre-define the target files as soon as the first line is
211 parsed, then come back and do the rest of the definition as now. That
212 would allow $(.TARGETS) to be correct without a major change to the way
215 if (streq (var->name, ".TARGETS"))
216 var->value = build_target_list (var->value);
220 if (streq (var
->name
, ".VARIABLES")
221 && global_variable_set
.table
.ht_fill
!= last_var_count
)
223 unsigned long max
= EXPANSION_INCREMENT (strlen (var
->value
));
226 struct variable
**vp
= (struct variable
**) global_variable_set
.table
.ht_vec
;
227 struct variable
**end
= &vp
[global_variable_set
.table
.ht_size
];
229 /* Make sure we have at least MAX bytes in the allocated buffer. */
230 var
->value
= xrealloc (var
->value
, max
);
232 /* Walk through the hash of variables, constructing a list of names. */
235 for (; vp
< end
; ++vp
)
236 if (!HASH_VACANT (*vp
))
238 struct variable
*v
= *vp
;
244 unsigned long off
= p
- var
->value
;
246 max
+= EXPANSION_INCREMENT (l
+ 1);
247 var
->value
= xrealloc (var
->value
, max
);
248 p
= &var
->value
[off
];
251 bcopy (v
->name
, p
, l
);
257 /* Remember how many variables are in our current count. Since we never
258 remove variables from the list, this is a reliable way to know whether
259 the list is up to date or needs to be recomputed. */
261 last_var_count
= global_variable_set
.table
.ht_fill
;
268 /* Lookup a variable whose name is a string starting at NAME
269 and with LENGTH chars. NAME need not be null-terminated.
270 Returns address of the `struct variable' containing all info
271 on the variable, or nil if no such variable is defined. */
274 lookup_variable (name
, length
)
278 const struct variable_set_list
*setlist
;
279 struct variable var_key
;
281 var_key
.name
= (char *) name
;
282 var_key
.length
= length
;
284 for (setlist
= current_variable_set_list
;
285 setlist
!= 0; setlist
= setlist
->next
)
287 const struct variable_set
*set
= setlist
->set
;
290 v
= (struct variable
*) hash_find_item ((struct hash_table
*) &set
->table
, &var_key
);
292 return v
->special
? handle_special_var (v
) : v
;
296 /* since we don't read envp[] on startup, try to get the
297 variable via getenv() here. */
299 char *vname
= alloca (length
+ 1);
301 strncpy (vname
, name
, length
);
303 value
= getenv (vname
);
312 while ((sptr
= strchr (sptr
, '$')))
323 nvalue
= alloca (strlen (value
) + scnt
+ 1);
342 return define_variable (vname
, length
, nvalue
, o_env
, 1);
346 return define_variable (vname
, length
, value
, o_env
, 1);
354 /* Lookup a variable whose name is a string starting at NAME
355 and with LENGTH chars in set SET. NAME need not be null-terminated.
356 Returns address of the `struct variable' containing all info
357 on the variable, or nil if no such variable is defined. */
360 lookup_variable_in_set (name
, length
, set
)
363 const struct variable_set
*set
;
365 struct variable var_key
;
367 var_key
.name
= (char *) name
;
368 var_key
.length
= length
;
370 return (struct variable
*) hash_find_item ((struct hash_table
*) &set
->table
, &var_key
);
373 /* Initialize FILE's variable set list. If FILE already has a variable set
374 list, the topmost variable set is left intact, but the the rest of the
375 chain is replaced with FILE->parent's setlist. If we're READing a
376 makefile, don't do the pattern variable search now, since the pattern
377 variable might not have been defined yet. */
380 initialize_file_variables (file
, reading
)
384 register struct variable_set_list
*l
= file
->variables
;
388 l
= (struct variable_set_list
*)
389 xmalloc (sizeof (struct variable_set_list
));
390 l
->set
= (struct variable_set
*) xmalloc (sizeof (struct variable_set
));
391 hash_init (&l
->set
->table
, PERFILE_VARIABLE_BUCKETS
,
392 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
396 if (file
->parent
== 0)
397 l
->next
= &global_setlist
;
400 initialize_file_variables (file
->parent
, reading
);
401 l
->next
= file
->parent
->variables
;
404 /* If we're not reading makefiles and we haven't looked yet, see if
405 we can find a pattern variable. */
407 if (!reading
&& !file
->pat_searched
)
409 struct pattern_var
*p
= lookup_pattern_var (file
->name
);
411 file
->pat_searched
= 1;
414 /* If we found one, insert it between the current target's
415 variables and the next set, whatever it is. */
416 file
->pat_variables
= (struct variable_set_list
*)
417 xmalloc (sizeof (struct variable_set_list
));
418 file
->pat_variables
->set
= p
->vars
->set
;
422 /* If we have a pattern variable match, set it up. */
424 if (file
->pat_variables
!= 0)
426 file
->pat_variables
->next
= l
->next
;
427 l
->next
= file
->pat_variables
;
431 /* Pop the top set off the current variable set list,
432 and free all its storage. */
435 free_variable_name_and_value (item
)
438 struct variable
*v
= (struct variable
*) item
;
444 pop_variable_scope ()
446 struct variable_set_list
*setlist
= current_variable_set_list
;
447 struct variable_set
*set
= setlist
->set
;
449 current_variable_set_list
= setlist
->next
;
450 free ((char *) setlist
);
452 hash_map (&set
->table
, free_variable_name_and_value
);
453 hash_free (&set
->table
, 1);
458 struct variable_set_list
*
459 create_new_variable_set ()
461 register struct variable_set_list
*setlist
;
462 register struct variable_set
*set
;
464 set
= (struct variable_set
*) xmalloc (sizeof (struct variable_set
));
465 hash_init (&set
->table
, SMALL_SCOPE_VARIABLE_BUCKETS
,
466 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
468 setlist
= (struct variable_set_list
*)
469 xmalloc (sizeof (struct variable_set_list
));
471 setlist
->next
= current_variable_set_list
;
476 /* Create a new variable set and push it on the current setlist. */
478 struct variable_set_list
*
479 push_new_variable_scope ()
481 return (current_variable_set_list
= create_new_variable_set());
484 /* Merge SET1 into SET0, freeing unused storage in SET1. */
487 merge_variable_sets (to_set
, from_set
)
488 struct variable_set
*to_set
, *from_set
;
490 struct variable
**from_var_slot
= (struct variable
**) from_set
->table
.ht_vec
;
491 struct variable
**from_var_end
= from_var_slot
+ from_set
->table
.ht_size
;
493 for ( ; from_var_slot
< from_var_end
; from_var_slot
++)
494 if (! HASH_VACANT (*from_var_slot
))
496 struct variable
*from_var
= *from_var_slot
;
497 struct variable
**to_var_slot
498 = (struct variable
**) hash_find_slot (&to_set
->table
, *from_var_slot
);
499 if (HASH_VACANT (*to_var_slot
))
500 hash_insert_at (&to_set
->table
, from_var
, to_var_slot
);
503 /* GKM FIXME: delete in from_set->table */
504 free (from_var
->value
);
510 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
513 merge_variable_set_lists (setlist0
, setlist1
)
514 struct variable_set_list
**setlist0
, *setlist1
;
516 register struct variable_set_list
*list0
= *setlist0
;
517 struct variable_set_list
*last0
= 0;
519 while (setlist1
!= 0 && list0
!= 0)
521 struct variable_set_list
*next
= setlist1
;
522 setlist1
= setlist1
->next
;
524 merge_variable_sets (list0
->set
, next
->set
);
533 *setlist0
= setlist1
;
535 last0
->next
= setlist1
;
539 /* Define the automatic variables, and record the addresses
540 of their structures so we can change their values quickly. */
543 define_automatic_variables ()
546 extern char* default_shell
;
548 extern char default_shell
[];
550 register struct variable
*v
;
553 sprintf (buf
, "%u", makelevel
);
554 (void) define_variable (MAKELEVEL_NAME
, MAKELEVEL_LENGTH
, buf
, o_env
, 0);
556 sprintf (buf
, "%s%s%s",
558 (remote_description
== 0 || remote_description
[0] == '\0')
560 (remote_description
== 0 || remote_description
[0] == '\0')
561 ? "" : remote_description
);
562 (void) define_variable ("MAKE_VERSION", 12, buf
, o_default
, 0);
565 /* Allow to specify a special shell just for Make,
566 and use $COMSPEC as the default $SHELL when appropriate. */
568 static char shell_str
[] = "SHELL";
569 const int shlen
= sizeof (shell_str
) - 1;
570 struct variable
*mshp
= lookup_variable ("MAKESHELL", 9);
571 struct variable
*comp
= lookup_variable ("COMSPEC", 7);
573 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
575 (void) define_variable (shell_str
, shlen
,
576 mshp
->value
, o_env_override
, 0);
579 /* $COMSPEC shouldn't override $SHELL. */
580 struct variable
*shp
= lookup_variable (shell_str
, shlen
);
583 (void) define_variable (shell_str
, shlen
, comp
->value
, o_env
, 0);
588 /* This won't override any definition, but it
589 will provide one if there isn't one there. */
590 v
= define_variable ("SHELL", 5, default_shell
, o_default
, 0);
591 v
->export
= v_export
; /* Always export SHELL. */
593 /* On MSDOS we do use SHELL from environment, since
594 it isn't a standard environment variable on MSDOS,
595 so whoever sets it, does that on purpose. */
597 /* Don't let SHELL come from the environment. */
598 if (*v
->value
== '\0' || v
->origin
== o_env
|| v
->origin
== o_env_override
)
602 v
->value
= xstrdup (default_shell
);
606 /* Make sure MAKEFILES gets exported if it is set. */
607 v
= define_variable ("MAKEFILES", 9, "", o_default
, 0);
610 /* Define the magic D and F variables in terms of
611 the automatic variables they are variations of. */
614 define_variable ("@D", 2, "$(dir $@)", o_automatic
, 1);
615 define_variable ("%D", 2, "$(dir $%)", o_automatic
, 1);
616 define_variable ("*D", 2, "$(dir $*)", o_automatic
, 1);
617 define_variable ("<D", 2, "$(dir $<)", o_automatic
, 1);
618 define_variable ("?D", 2, "$(dir $?)", o_automatic
, 1);
619 define_variable ("^D", 2, "$(dir $^)", o_automatic
, 1);
620 define_variable ("+D", 2, "$(dir $+)", o_automatic
, 1);
622 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic
, 1);
623 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic
, 1);
624 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic
, 1);
625 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic
, 1);
626 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic
, 1);
627 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic
, 1);
628 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic
, 1);
630 define_variable ("@F", 2, "$(notdir $@)", o_automatic
, 1);
631 define_variable ("%F", 2, "$(notdir $%)", o_automatic
, 1);
632 define_variable ("*F", 2, "$(notdir $*)", o_automatic
, 1);
633 define_variable ("<F", 2, "$(notdir $<)", o_automatic
, 1);
634 define_variable ("?F", 2, "$(notdir $?)", o_automatic
, 1);
635 define_variable ("^F", 2, "$(notdir $^)", o_automatic
, 1);
636 define_variable ("+F", 2, "$(notdir $+)", o_automatic
, 1);
639 int export_all_variables
;
641 /* Create a new environment for FILE's commands.
642 If FILE is nil, this is for the `shell' function.
643 The child's MAKELEVEL variable is incremented. */
646 target_environment (file
)
649 struct variable_set_list
*set_list
;
650 register struct variable_set_list
*s
;
651 struct hash_table table
;
652 struct variable
**v_slot
;
653 struct variable
**v_end
;
654 struct variable makelevel_key
;
659 set_list
= current_variable_set_list
;
661 set_list
= file
->variables
;
663 hash_init (&table
, VARIABLE_BUCKETS
,
664 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
666 /* Run through all the variable sets in the list,
667 accumulating variables in TABLE. */
668 for (s
= set_list
; s
!= 0; s
= s
->next
)
670 struct variable_set
*set
= s
->set
;
671 v_slot
= (struct variable
**) set
->table
.ht_vec
;
672 v_end
= v_slot
+ set
->table
.ht_size
;
673 for ( ; v_slot
< v_end
; v_slot
++)
674 if (! HASH_VACANT (*v_slot
))
676 struct variable
**new_slot
;
677 struct variable
*v
= *v_slot
;
679 /* If this is a per-target variable and it hasn't been touched
680 already then look up the global version and take its export
682 if (v
->per_target
&& v
->export
== v_default
)
686 gv
= lookup_variable_in_set (v
->name
, strlen(v
->name
),
687 &global_variable_set
);
689 v
->export
= gv
->export
;
695 if (v
->origin
== o_default
|| v
->origin
== o_automatic
)
696 /* Only export default variables by explicit request. */
699 /* The variable doesn't have a name that can be exported. */
703 if (! export_all_variables
704 && v
->origin
!= o_command
705 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
716 if (v
->origin
== o_default
)
721 new_slot
= (struct variable
**) hash_find_slot (&table
, v
);
722 if (HASH_VACANT (*new_slot
))
723 hash_insert_at (&table
, v
, new_slot
);
727 makelevel_key
.name
= MAKELEVEL_NAME
;
728 makelevel_key
.length
= MAKELEVEL_LENGTH
;
729 hash_delete (&table
, &makelevel_key
);
731 result
= result_0
= (char **) xmalloc ((table
.ht_fill
+ 2) * sizeof (char *));
733 v_slot
= (struct variable
**) table
.ht_vec
;
734 v_end
= v_slot
+ table
.ht_size
;
735 for ( ; v_slot
< v_end
; v_slot
++)
736 if (! HASH_VACANT (*v_slot
))
738 struct variable
*v
= *v_slot
;
740 /* If V is recursively expanded and didn't come from the environment,
741 expand its value. If it came from the environment, it should
742 go back into the environment unchanged. */
744 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
746 char *value
= recursively_expand_for_file (v
, file
);
748 if (strcmp(v
->name
, "Path") == 0 ||
749 strcmp(v
->name
, "PATH") == 0)
750 convert_Path_to_windows32(value
, ';');
752 *result
++ = concat (v
->name
, "=", value
);
758 if (strcmp(v
->name
, "Path") == 0 ||
759 strcmp(v
->name
, "PATH") == 0)
760 convert_Path_to_windows32(v
->value
, ';');
762 *result
++ = concat (v
->name
, "=", v
->value
);
766 *result
= (char *) xmalloc (100);
767 (void) sprintf (*result
, "%s=%u", MAKELEVEL_NAME
, makelevel
+ 1);
770 hash_free (&table
, 0);
775 /* Given a variable, a value, and a flavor, define the variable.
776 See the try_variable_definition() function for details on the parameters. */
779 do_variable_definition (flocp
, varname
, value
, origin
, flavor
, target_var
)
780 const struct floc
*flocp
;
783 enum variable_origin origin
;
784 enum variable_flavor flavor
;
787 char *p
, *alloc_value
= NULL
;
791 /* Calculate the variable's new value in VALUE. */
797 /* Should not be possible. */
800 /* A simple variable definition "var := value". Expand the value.
801 We have to allocate memory since otherwise it'll clobber the
802 variable buffer, and we may still need that if we're looking at a
803 target-specific variable. */
804 p
= alloc_value
= allocated_variable_expand (value
);
807 /* A conditional variable definition "var ?= value".
808 The value is set IFF the variable is not defined yet. */
809 v
= lookup_variable (varname
, strlen (varname
));
813 flavor
= f_recursive
;
816 /* A recursive variable definition "var = value".
817 The value is used verbatim. */
822 /* If we have += but we're in a target variable context, we want to
823 append only with other variables in the context of this target. */
827 v
= lookup_variable_in_set (varname
, strlen (varname
),
828 current_variable_set_list
->set
);
831 v
= lookup_variable (varname
, strlen (varname
));
835 /* There was no old value.
836 This becomes a normal recursive definition. */
838 flavor
= f_recursive
;
842 /* Paste the old and new values together in VALUE. */
844 unsigned int oldlen
, vallen
;
849 /* The previous definition of the variable was recursive.
850 The new value is the unexpanded old and new values. */
851 flavor
= f_recursive
;
853 /* The previous definition of the variable was simple.
854 The new value comes from the old value, which was expanded
855 when it was set; and from the expanded new value. Allocate
856 memory for the expansion as we may still need the rest of the
857 buffer if we're looking at a target-specific variable. */
858 val
= alloc_value
= allocated_variable_expand (val
);
860 oldlen
= strlen (v
->value
);
861 vallen
= strlen (val
);
862 p
= (char *) alloca (oldlen
+ 1 + vallen
+ 1);
863 bcopy (v
->value
, p
, oldlen
);
865 bcopy (val
, &p
[oldlen
+ 1], vallen
+ 1);
871 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
872 non-Unix systems don't conform to this default configuration (in
873 fact, most of them don't even have `/bin'). On the other hand,
874 $SHELL in the environment, if set, points to the real pathname of
876 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
877 the Makefile override $SHELL from the environment. But first, we
878 look for the basename of the shell in the directory where SHELL=
879 points, and along the $PATH; if it is found in any of these places,
880 we define $SHELL to be the actual pathname of the shell. Thus, if
881 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
882 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
883 defining SHELL to be "d:/unix/bash.exe". */
884 if ((origin
== o_file
|| origin
== o_override
)
885 && strcmp (varname
, "SHELL") == 0)
887 char shellpath
[PATH_MAX
];
888 extern char * __dosexec_find_on_path (const char *, char *[], char *);
890 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
891 if (__dosexec_find_on_path (p
, (char **)0, shellpath
))
895 for (p
= shellpath
; *p
; p
++)
900 v
= define_variable_loc (varname
, strlen (varname
),
901 shellpath
, origin
, flavor
== f_recursive
,
906 char *shellbase
, *bslash
;
907 struct variable
*pathv
= lookup_variable ("PATH", 4);
912 shellbase
= strrchr (p
, '/');
913 bslash
= strrchr (p
, '\\');
914 if (!shellbase
|| bslash
> shellbase
)
916 if (!shellbase
&& p
[1] == ':')
923 /* Search for the basename of the shell (with standard
924 executable extensions) along the $PATH. */
926 pathlen
= strlen (pathv
->value
);
927 path_string
= (char *)xmalloc (5 + pathlen
+ 2 + 1);
928 /* On MSDOS, current directory is considered as part of $PATH. */
929 sprintf (path_string
, "PATH=.;%s", pathv
? pathv
->value
: "");
930 fake_env
[0] = path_string
;
931 fake_env
[1] = (char *)0;
932 if (__dosexec_find_on_path (shellbase
, fake_env
, shellpath
))
936 for (p
= shellpath
; *p
; p
++)
941 v
= define_variable_loc (varname
, strlen (varname
),
943 flavor
== f_recursive
, flocp
);
946 v
= lookup_variable (varname
, strlen (varname
));
952 #endif /* __MSDOS__ */
954 if ((origin
== o_file
|| origin
== o_override
) && streq (varname
, "SHELL"))
956 extern char *default_shell
;
958 /* Call shell locator function. If it returns TRUE, then
959 set no_default_sh_exe to indicate sh was found and
960 set new value for SHELL variable. */
962 if (find_and_set_default_shell (p
))
964 v
= define_variable_in_set (varname
, strlen (varname
), default_shell
,
965 origin
, flavor
== f_recursive
,
967 ? current_variable_set_list
->set
970 no_default_sh_exe
= 0;
973 v
= lookup_variable (varname
, strlen (varname
));
978 /* If we are defining variables inside an $(eval ...), we might have a
979 different variable context pushed, not the global context (maybe we're
980 inside a $(call ...) or something. Since this function is only ever
981 invoked in places where we want to define globally visible variables,
982 make sure we define this variable in the global set. */
984 v
= define_variable_in_set (varname
, strlen (varname
), p
,
985 origin
, flavor
== f_recursive
,
987 ? current_variable_set_list
->set
: NULL
),
997 /* Try to interpret LINE (a null-terminated string) as a variable definition.
999 ORIGIN may be o_file, o_override, o_env, o_env_override,
1000 or o_command specifying that the variable definition comes
1001 from a makefile, an override directive, the environment with
1002 or without the -e switch, or the command line.
1004 See the comments for parse_variable_definition().
1006 If LINE was recognized as a variable definition, a pointer to its `struct
1007 variable' is returned. If LINE is not a variable definition, NULL is
1011 try_variable_definition (flocp
, line
, origin
, target_var
)
1012 const struct floc
*flocp
;
1014 enum variable_origin origin
;
1018 register char *p
= line
;
1021 enum variable_flavor flavor
= f_bogus
;
1022 char *name
, *expanded_name
;
1028 if (c
== '\0' || c
== '#')
1033 flavor
= f_recursive
;
1044 /* A colon other than := is a rule line, not a variable defn. */
1046 else if (c
== '+' && *p
== '=')
1052 else if (c
== '?' && *p
== '=')
1055 flavor
= f_conditional
;
1060 /* This might begin a variable expansion reference. Make sure we
1061 don't misrecognize chars inside the reference as =, := or +=. */
1070 continue; /* Nope. */
1072 /* P now points past the opening paren or brace.
1073 Count parens or braces until it is matched. */
1075 for (; *p
!= '\0'; ++p
)
1079 else if (*p
== closeparen
&& --count
< 0)
1088 beg
= next_token (line
);
1089 while (end
> beg
&& isblank ((unsigned char)end
[-1]))
1093 /* Expand the name, so "$(foo)bar = baz" works. */
1094 name
= (char *) alloca (end
- beg
+ 1);
1095 bcopy (beg
, name
, end
- beg
);
1096 name
[end
- beg
] = '\0';
1097 expanded_name
= allocated_variable_expand (name
);
1099 if (expanded_name
[0] == '\0')
1100 fatal (flocp
, _("empty variable name"));
1102 v
= do_variable_definition (flocp
, expanded_name
, p
,
1103 origin
, flavor
, target_var
);
1105 free (expanded_name
);
1110 /* Print information for variable V, prefixing it with PREFIX. */
1113 print_variable (v
, prefix
)
1114 register struct variable
*v
;
1122 origin
= _("default");
1125 origin
= _("environment");
1128 origin
= _("makefile");
1130 case o_env_override
:
1131 origin
= _("environment under -e");
1134 origin
= _("command line");
1137 origin
= _("`override' directive");
1140 origin
= _("automatic");
1146 fputs ("# ", stdout
);
1147 fputs (origin
, stdout
);
1148 if (v
->fileinfo
.filenm
)
1149 printf (_(" (from `%s', line %lu)"),
1150 v
->fileinfo
.filenm
, v
->fileinfo
.lineno
);
1152 fputs (prefix
, stdout
);
1154 /* Is this a `define'? */
1155 if (v
->recursive
&& strchr (v
->value
, '\n') != 0)
1156 printf ("define %s\n%s\nendef\n", v
->name
, v
->value
);
1161 printf ("%s %s= ", v
->name
, v
->recursive
? v
->append
? "+" : "" : ":");
1163 /* Check if the value is just whitespace. */
1164 p
= next_token (v
->value
);
1165 if (p
!= v
->value
&& *p
== '\0')
1166 /* All whitespace. */
1167 printf ("$(subst ,,%s)", v
->value
);
1168 else if (v
->recursive
)
1169 fputs (v
->value
, stdout
);
1171 /* Double up dollar signs. */
1172 for (p
= v
->value
; *p
!= '\0'; ++p
)
1183 /* Print all the variables in SET. PREFIX is printed before
1184 the actual variable definitions (everything else is comments). */
1187 print_variable_set (set
, prefix
)
1188 register struct variable_set
*set
;
1191 hash_map_arg (&set
->table
, print_variable
, prefix
);
1193 fputs (_("# variable set hash-table stats:\n"), stdout
);
1194 fputs ("# ", stdout
);
1195 hash_print_stats (&set
->table
, stdout
);
1196 putc ('\n', stdout
);
1199 /* Print the data base of variables. */
1202 print_variable_data_base ()
1204 puts (_("\n# Variables\n"));
1206 print_variable_set (&global_variable_set
, "");
1210 /* Print all the local variables of FILE. */
1213 print_file_variables (file
)
1216 if (file
->variables
!= 0)
1217 print_variable_set (file
->variables
->set
, "# ");
1222 sync_Path_environment(void)
1224 char* path
= allocated_variable_expand("$(Path)");
1225 static char* environ_path
= NULL
;
1231 * If done this before, don't leak memory unnecessarily.
1232 * Free the previous entry before allocating new one.
1238 * Create something WINDOWS32 world can grok
1240 convert_Path_to_windows32(path
, ';');
1241 environ_path
= concat("Path", "=", path
);
1242 putenv(environ_path
);