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 (const void *keyv
)
38 struct variable
const *key
= (struct variable
const *) keyv
;
39 return_STRING_N_HASH_1 (key
->name
, key
->length
);
43 variable_hash_2 (const void *keyv
)
45 struct variable
const *key
= (struct variable
const *) keyv
;
46 return_STRING_N_HASH_2 (key
->name
, key
->length
);
50 variable_hash_cmp (const void *xv
, const void *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 (void)
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 (const char *name
, unsigned int length
,
93 char *value
, enum variable_origin origin
,
94 int recursive
, struct variable_set
*set
,
95 const struct floc
*flocp
)
98 struct variable
**var_slot
;
99 struct variable var_key
;
102 set
= &global_variable_set
;
104 var_key
.name
= (char *) name
;
105 var_key
.length
= length
;
106 var_slot
= (struct variable
**) hash_find_slot (&set
->table
, &var_key
);
108 if (env_overrides
&& origin
== o_env
)
109 origin
= o_env_override
;
112 if (! HASH_VACANT (v
))
114 if (env_overrides
&& v
->origin
== o_env
)
115 /* V came from in the environment. Since it was defined
116 before the switches were parsed, it wasn't affected by -e. */
117 v
->origin
= o_env_override
;
119 /* A variable of this name is already defined.
120 If the old definition is from a stronger source
121 than this one, don't redefine it. */
122 if ((int) origin
>= (int) v
->origin
)
126 v
->value
= xstrdup (value
);
128 v
->fileinfo
= *flocp
;
130 v
->fileinfo
.filenm
= 0;
132 v
->recursive
= recursive
;
137 /* Create a new variable definition and add it to the hash table. */
139 v
= (struct variable
*) xmalloc (sizeof (struct variable
));
140 v
->name
= savestring (name
, length
);
142 hash_insert_at (&set
->table
, v
, var_slot
);
143 v
->value
= xstrdup (value
);
145 v
->fileinfo
= *flocp
;
147 v
->fileinfo
.filenm
= 0;
149 v
->recursive
= recursive
;
154 v
->export
= v_default
;
157 if (*name
!= '_' && (*name
< 'A' || *name
> 'Z')
158 && (*name
< 'a' || *name
> 'z'))
162 for (++name
; *name
!= '\0'; ++name
)
163 if (*name
!= '_' && (*name
< 'a' || *name
> 'z')
164 && (*name
< 'A' || *name
> 'Z') && !ISDIGIT(*name
))
174 /* If the variable passed in is "special", handle its special nature.
175 Currently there are two such variables, both used for introspection:
176 .VARIABLES expands to a list of all the variables defined in this instance
178 .TARGETS expands to a list of all the targets defined in this
180 Returns the variable reference passed in. */
182 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
184 static struct variable
*
185 handle_special_var (struct variable
*var
)
187 static unsigned long last_var_count
= 0;
190 /* This one actually turns out to be very hard, due to the way the parser
191 records targets. The way it works is that target information is collected
192 internally until make knows the target is completely specified. It unitl
193 it sees that some new construct (a new target or variable) is defined that
194 it knows the previous one is done. In short, this means that if you do
201 then $(TARGS) won't contain "all", because it's not until after the
202 variable is created that the previous target is completed.
204 Changing this would be a major pain. I think a less complex way to do it
205 would be to pre-define the target files as soon as the first line is
206 parsed, then come back and do the rest of the definition as now. That
207 would allow $(.TARGETS) to be correct without a major change to the way
210 if (streq (var->name, ".TARGETS"))
211 var->value = build_target_list (var->value);
215 if (streq (var
->name
, ".VARIABLES")
216 && global_variable_set
.table
.ht_fill
!= last_var_count
)
218 unsigned long max
= EXPANSION_INCREMENT (strlen (var
->value
));
221 struct variable
**vp
= (struct variable
**) global_variable_set
.table
.ht_vec
;
222 struct variable
**end
= &vp
[global_variable_set
.table
.ht_size
];
224 /* Make sure we have at least MAX bytes in the allocated buffer. */
225 var
->value
= xrealloc (var
->value
, max
);
227 /* Walk through the hash of variables, constructing a list of names. */
230 for (; vp
< end
; ++vp
)
231 if (!HASH_VACANT (*vp
))
233 struct variable
*v
= *vp
;
239 unsigned long off
= p
- var
->value
;
241 max
+= EXPANSION_INCREMENT (l
+ 1);
242 var
->value
= xrealloc (var
->value
, max
);
243 p
= &var
->value
[off
];
246 bcopy (v
->name
, p
, l
);
252 /* Remember how many variables are in our current count. Since we never
253 remove variables from the list, this is a reliable way to know whether
254 the list is up to date or needs to be recomputed. */
256 last_var_count
= global_variable_set
.table
.ht_fill
;
263 /* Lookup a variable whose name is a string starting at NAME
264 and with LENGTH chars. NAME need not be null-terminated.
265 Returns address of the `struct variable' containing all info
266 on the variable, or nil if no such variable is defined. */
269 lookup_variable (const char *name
, unsigned int length
)
271 const struct variable_set_list
*setlist
;
272 struct variable var_key
;
274 var_key
.name
= (char *) name
;
275 var_key
.length
= length
;
277 for (setlist
= current_variable_set_list
;
278 setlist
!= 0; setlist
= setlist
->next
)
280 const struct variable_set
*set
= setlist
->set
;
283 v
= (struct variable
*) hash_find_item ((struct hash_table
*) &set
->table
, &var_key
);
285 return v
->special
? handle_special_var (v
) : v
;
289 /* since we don't read envp[] on startup, try to get the
290 variable via getenv() here. */
292 char *vname
= alloca (length
+ 1);
294 strncpy (vname
, name
, length
);
296 value
= getenv (vname
);
305 while ((sptr
= strchr (sptr
, '$')))
316 nvalue
= alloca (strlen (value
) + scnt
+ 1);
335 return define_variable (vname
, length
, nvalue
, o_env
, 1);
339 return define_variable (vname
, length
, value
, o_env
, 1);
347 /* Lookup a variable whose name is a string starting at NAME
348 and with LENGTH chars in set SET. NAME need not be null-terminated.
349 Returns address of the `struct variable' containing all info
350 on the variable, or nil if no such variable is defined. */
353 lookup_variable_in_set (const char *name
, unsigned int length
,
354 const struct variable_set
*set
)
356 struct variable var_key
;
358 var_key
.name
= (char *) name
;
359 var_key
.length
= length
;
361 return (struct variable
*) hash_find_item ((struct hash_table
*) &set
->table
, &var_key
);
364 /* Initialize FILE's variable set list. If FILE already has a variable set
365 list, the topmost variable set is left intact, but the the rest of the
366 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
367 rule, then we will use the "root" double-colon target's variable set as the
368 parent of FILE's variable set.
370 If we're READing a makefile, don't do the pattern variable search now,
371 since the pattern variable might not have been defined yet. */
374 initialize_file_variables (struct file
*file
, int reading
)
376 register struct variable_set_list
*l
= file
->variables
;
380 l
= (struct variable_set_list
*)
381 xmalloc (sizeof (struct variable_set_list
));
382 l
->set
= (struct variable_set
*) xmalloc (sizeof (struct variable_set
));
383 hash_init (&l
->set
->table
, PERFILE_VARIABLE_BUCKETS
,
384 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
388 /* If this is a double-colon, then our "parent" is the "root" target for
389 this double-colon rule. Since that rule has the same name, parent,
390 etc. we can just use its variables as the "next" for ours. */
392 if (file
->double_colon
&& file
->double_colon
!= file
)
394 initialize_file_variables (file
->double_colon
, reading
);
395 l
->next
= file
->double_colon
->variables
;
399 if (file
->parent
== 0)
400 l
->next
= &global_setlist
;
403 initialize_file_variables (file
->parent
, reading
);
404 l
->next
= file
->parent
->variables
;
407 /* If we're not reading makefiles and we haven't looked yet, see if
408 we can find a pattern variable. */
410 if (!reading
&& !file
->pat_searched
)
412 struct pattern_var
*p
= lookup_pattern_var (file
->name
);
414 file
->pat_searched
= 1;
417 /* If we found one, insert it between the current target's
418 variables and the next set, whatever it is. */
419 file
->pat_variables
= (struct variable_set_list
*)
420 xmalloc (sizeof (struct variable_set_list
));
421 file
->pat_variables
->set
= p
->vars
->set
;
425 /* If we have a pattern variable match, set it up. */
427 if (file
->pat_variables
!= 0)
429 file
->pat_variables
->next
= l
->next
;
430 l
->next
= file
->pat_variables
;
434 /* Pop the top set off the current variable set list,
435 and free all its storage. */
438 free_variable_name_and_value (const void *item
)
440 struct variable
*v
= (struct variable
*) item
;
446 pop_variable_scope (void)
448 struct variable_set_list
*setlist
= current_variable_set_list
;
449 struct variable_set
*set
= setlist
->set
;
451 current_variable_set_list
= setlist
->next
;
452 free ((char *) setlist
);
454 hash_map (&set
->table
, free_variable_name_and_value
);
455 hash_free (&set
->table
, 1);
460 struct variable_set_list
*
461 create_new_variable_set (void)
463 register struct variable_set_list
*setlist
;
464 register struct variable_set
*set
;
466 set
= (struct variable_set
*) xmalloc (sizeof (struct variable_set
));
467 hash_init (&set
->table
, SMALL_SCOPE_VARIABLE_BUCKETS
,
468 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
470 setlist
= (struct variable_set_list
*)
471 xmalloc (sizeof (struct variable_set_list
));
473 setlist
->next
= current_variable_set_list
;
478 /* Create a new variable set and push it on the current setlist. */
480 struct variable_set_list
*
481 push_new_variable_scope (void)
483 return (current_variable_set_list
= create_new_variable_set());
486 /* Merge SET1 into SET0, freeing unused storage in SET1. */
489 merge_variable_sets (struct variable_set
*to_set
,
490 struct variable_set
*from_set
)
492 struct variable
**from_var_slot
= (struct variable
**) from_set
->table
.ht_vec
;
493 struct variable
**from_var_end
= from_var_slot
+ from_set
->table
.ht_size
;
495 for ( ; from_var_slot
< from_var_end
; from_var_slot
++)
496 if (! HASH_VACANT (*from_var_slot
))
498 struct variable
*from_var
= *from_var_slot
;
499 struct variable
**to_var_slot
500 = (struct variable
**) hash_find_slot (&to_set
->table
, *from_var_slot
);
501 if (HASH_VACANT (*to_var_slot
))
502 hash_insert_at (&to_set
->table
, from_var
, to_var_slot
);
505 /* GKM FIXME: delete in from_set->table */
506 free (from_var
->value
);
512 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
515 merge_variable_set_lists (struct variable_set_list
**setlist0
,
516 struct variable_set_list
*setlist1
)
518 register struct variable_set_list
*list0
= *setlist0
;
519 struct variable_set_list
*last0
= 0;
521 while (setlist1
!= 0 && list0
!= 0)
523 struct variable_set_list
*next
= setlist1
;
524 setlist1
= setlist1
->next
;
526 merge_variable_sets (list0
->set
, next
->set
);
535 *setlist0
= setlist1
;
537 last0
->next
= setlist1
;
541 /* Define the automatic variables, and record the addresses
542 of their structures so we can change their values quickly. */
545 define_automatic_variables (void)
547 #if defined(WINDOWS32) || defined(__EMX__)
548 extern char* default_shell
;
550 extern char default_shell
[];
552 register struct variable
*v
;
555 sprintf (buf
, "%u", makelevel
);
556 (void) define_variable (MAKELEVEL_NAME
, MAKELEVEL_LENGTH
, buf
, o_env
, 0);
558 sprintf (buf
, "%s%s%s",
560 (remote_description
== 0 || remote_description
[0] == '\0')
562 (remote_description
== 0 || remote_description
[0] == '\0')
563 ? "" : remote_description
);
564 (void) define_variable ("MAKE_VERSION", 12, buf
, o_default
, 0);
567 /* Allow to specify a special shell just for Make,
568 and use $COMSPEC as the default $SHELL when appropriate. */
570 static char shell_str
[] = "SHELL";
571 const int shlen
= sizeof (shell_str
) - 1;
572 struct variable
*mshp
= lookup_variable ("MAKESHELL", 9);
573 struct variable
*comp
= lookup_variable ("COMSPEC", 7);
575 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
577 (void) define_variable (shell_str
, shlen
,
578 mshp
->value
, o_env_override
, 0);
581 /* $COMSPEC shouldn't override $SHELL. */
582 struct variable
*shp
= lookup_variable (shell_str
, shlen
);
585 (void) define_variable (shell_str
, shlen
, comp
->value
, o_env
, 0);
588 #elif defined(__EMX__)
590 static char shell_str
[] = "SHELL";
591 const int shlen
= sizeof (shell_str
) - 1;
592 struct variable
*shell
= lookup_variable (shell_str
, shlen
);
593 struct variable
*replace
= lookup_variable ("MAKESHELL", 9);
595 /* if $MAKESHELL is defined in the environment assume o_env_override */
596 if (replace
&& *replace
->value
&& replace
->origin
== o_env
)
597 replace
->origin
= o_env_override
;
599 /* if $MAKESHELL is not defined use $SHELL but only if the variable
600 did not come from the environment */
601 if (!replace
|| !*replace
->value
)
602 if (shell
&& *shell
->value
&& (shell
->origin
== o_env
603 || shell
->origin
== o_env_override
))
605 /* overwrite whatever we got from the environment */
607 shell
->value
= xstrdup (default_shell
);
608 shell
->origin
= o_default
;
611 /* Some people do not like cmd to be used as the default
612 if $SHELL is not defined in the Makefile.
613 With -DNO_CMD_DEFAULT you can turn off this behaviour */
614 # ifndef NO_CMD_DEFAULT
615 /* otherwise use $COMSPEC */
616 if (!replace
|| !*replace
->value
)
617 replace
= lookup_variable ("COMSPEC", 7);
619 /* otherwise use $OS2_SHELL */
620 if (!replace
|| !*replace
->value
)
621 replace
= lookup_variable ("OS2_SHELL", 9);
623 # warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
626 if (replace
&& *replace
->value
)
627 /* overwrite $SHELL */
628 (void) define_variable (shell_str
, shlen
, replace
->value
,
631 /* provide a definition if there is none */
632 (void) define_variable (shell_str
, shlen
, default_shell
,
638 /* This won't override any definition, but it
639 will provide one if there isn't one there. */
640 v
= define_variable ("SHELL", 5, default_shell
, o_default
, 0);
641 v
->export
= v_export
; /* Always export SHELL. */
643 /* On MSDOS we do use SHELL from environment, since
644 it isn't a standard environment variable on MSDOS,
645 so whoever sets it, does that on purpose.
646 On OS/2 we do not use SHELL from environment but
647 we have already handled that problem above. */
648 #if !defined(__MSDOS__) && !defined(__EMX__)
649 /* Don't let SHELL come from the environment. */
650 if (*v
->value
== '\0' || v
->origin
== o_env
|| v
->origin
== o_env_override
)
654 v
->value
= xstrdup (default_shell
);
658 /* Make sure MAKEFILES gets exported if it is set. */
659 v
= define_variable ("MAKEFILES", 9, "", o_default
, 0);
662 /* Define the magic D and F variables in terms of
663 the automatic variables they are variations of. */
666 define_variable ("@D", 2, "$(dir $@)", o_automatic
, 1);
667 define_variable ("%D", 2, "$(dir $%)", o_automatic
, 1);
668 define_variable ("*D", 2, "$(dir $*)", o_automatic
, 1);
669 define_variable ("<D", 2, "$(dir $<)", o_automatic
, 1);
670 define_variable ("?D", 2, "$(dir $?)", o_automatic
, 1);
671 define_variable ("^D", 2, "$(dir $^)", o_automatic
, 1);
672 define_variable ("+D", 2, "$(dir $+)", o_automatic
, 1);
674 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic
, 1);
675 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic
, 1);
676 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic
, 1);
677 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic
, 1);
678 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic
, 1);
679 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic
, 1);
680 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic
, 1);
682 define_variable ("@F", 2, "$(notdir $@)", o_automatic
, 1);
683 define_variable ("%F", 2, "$(notdir $%)", o_automatic
, 1);
684 define_variable ("*F", 2, "$(notdir $*)", o_automatic
, 1);
685 define_variable ("<F", 2, "$(notdir $<)", o_automatic
, 1);
686 define_variable ("?F", 2, "$(notdir $?)", o_automatic
, 1);
687 define_variable ("^F", 2, "$(notdir $^)", o_automatic
, 1);
688 define_variable ("+F", 2, "$(notdir $+)", o_automatic
, 1);
691 int export_all_variables
;
693 /* Create a new environment for FILE's commands.
694 If FILE is nil, this is for the `shell' function.
695 The child's MAKELEVEL variable is incremented. */
698 target_environment (struct file
*file
)
700 struct variable_set_list
*set_list
;
701 register struct variable_set_list
*s
;
702 struct hash_table table
;
703 struct variable
**v_slot
;
704 struct variable
**v_end
;
705 struct variable makelevel_key
;
710 set_list
= current_variable_set_list
;
712 set_list
= file
->variables
;
714 hash_init (&table
, VARIABLE_BUCKETS
,
715 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
717 /* Run through all the variable sets in the list,
718 accumulating variables in TABLE. */
719 for (s
= set_list
; s
!= 0; s
= s
->next
)
721 struct variable_set
*set
= s
->set
;
722 v_slot
= (struct variable
**) set
->table
.ht_vec
;
723 v_end
= v_slot
+ set
->table
.ht_size
;
724 for ( ; v_slot
< v_end
; v_slot
++)
725 if (! HASH_VACANT (*v_slot
))
727 struct variable
**new_slot
;
728 struct variable
*v
= *v_slot
;
730 /* If this is a per-target variable and it hasn't been touched
731 already then look up the global version and take its export
733 if (v
->per_target
&& v
->export
== v_default
)
737 gv
= lookup_variable_in_set (v
->name
, strlen(v
->name
),
738 &global_variable_set
);
740 v
->export
= gv
->export
;
746 if (v
->origin
== o_default
|| v
->origin
== o_automatic
)
747 /* Only export default variables by explicit request. */
750 /* The variable doesn't have a name that can be exported. */
754 if (! export_all_variables
755 && v
->origin
!= o_command
756 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
767 if (v
->origin
== o_default
)
772 new_slot
= (struct variable
**) hash_find_slot (&table
, v
);
773 if (HASH_VACANT (*new_slot
))
774 hash_insert_at (&table
, v
, new_slot
);
778 makelevel_key
.name
= MAKELEVEL_NAME
;
779 makelevel_key
.length
= MAKELEVEL_LENGTH
;
780 hash_delete (&table
, &makelevel_key
);
782 result
= result_0
= (char **) xmalloc ((table
.ht_fill
+ 2) * sizeof (char *));
784 v_slot
= (struct variable
**) table
.ht_vec
;
785 v_end
= v_slot
+ table
.ht_size
;
786 for ( ; v_slot
< v_end
; v_slot
++)
787 if (! HASH_VACANT (*v_slot
))
789 struct variable
*v
= *v_slot
;
791 /* If V is recursively expanded and didn't come from the environment,
792 expand its value. If it came from the environment, it should
793 go back into the environment unchanged. */
795 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
797 char *value
= recursively_expand_for_file (v
, file
);
799 if (strcmp(v
->name
, "Path") == 0 ||
800 strcmp(v
->name
, "PATH") == 0)
801 convert_Path_to_windows32(value
, ';');
803 *result
++ = concat (v
->name
, "=", value
);
809 if (strcmp(v
->name
, "Path") == 0 ||
810 strcmp(v
->name
, "PATH") == 0)
811 convert_Path_to_windows32(v
->value
, ';');
813 *result
++ = concat (v
->name
, "=", v
->value
);
817 *result
= (char *) xmalloc (100);
818 (void) sprintf (*result
, "%s=%u", MAKELEVEL_NAME
, makelevel
+ 1);
821 hash_free (&table
, 0);
826 /* Given a variable, a value, and a flavor, define the variable.
827 See the try_variable_definition() function for details on the parameters. */
830 do_variable_definition (const struct floc
*flocp
, const char *varname
,
831 char *value
, enum variable_origin origin
,
832 enum variable_flavor flavor
, int target_var
)
834 char *p
, *alloc_value
= NULL
;
838 /* Calculate the variable's new value in VALUE. */
844 /* Should not be possible. */
847 /* A simple variable definition "var := value". Expand the value.
848 We have to allocate memory since otherwise it'll clobber the
849 variable buffer, and we may still need that if we're looking at a
850 target-specific variable. */
851 p
= alloc_value
= allocated_variable_expand (value
);
854 /* A conditional variable definition "var ?= value".
855 The value is set IFF the variable is not defined yet. */
856 v
= lookup_variable (varname
, strlen (varname
));
860 flavor
= f_recursive
;
863 /* A recursive variable definition "var = value".
864 The value is used verbatim. */
869 /* If we have += but we're in a target variable context, we want to
870 append only with other variables in the context of this target. */
874 v
= lookup_variable_in_set (varname
, strlen (varname
),
875 current_variable_set_list
->set
);
878 v
= lookup_variable (varname
, strlen (varname
));
882 /* There was no old value.
883 This becomes a normal recursive definition. */
885 flavor
= f_recursive
;
889 /* Paste the old and new values together in VALUE. */
891 unsigned int oldlen
, vallen
;
896 /* The previous definition of the variable was recursive.
897 The new value is the unexpanded old and new values. */
898 flavor
= f_recursive
;
900 /* The previous definition of the variable was simple.
901 The new value comes from the old value, which was expanded
902 when it was set; and from the expanded new value. Allocate
903 memory for the expansion as we may still need the rest of the
904 buffer if we're looking at a target-specific variable. */
905 val
= alloc_value
= allocated_variable_expand (val
);
907 oldlen
= strlen (v
->value
);
908 vallen
= strlen (val
);
909 p
= (char *) alloca (oldlen
+ 1 + vallen
+ 1);
910 bcopy (v
->value
, p
, oldlen
);
912 bcopy (val
, &p
[oldlen
+ 1], vallen
+ 1);
918 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
919 non-Unix systems don't conform to this default configuration (in
920 fact, most of them don't even have `/bin'). On the other hand,
921 $SHELL in the environment, if set, points to the real pathname of
923 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
924 the Makefile override $SHELL from the environment. But first, we
925 look for the basename of the shell in the directory where SHELL=
926 points, and along the $PATH; if it is found in any of these places,
927 we define $SHELL to be the actual pathname of the shell. Thus, if
928 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
929 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
930 defining SHELL to be "d:/unix/bash.exe". */
931 if ((origin
== o_file
|| origin
== o_override
)
932 && strcmp (varname
, "SHELL") == 0)
934 char shellpath
[PATH_MAX
];
935 extern char * __dosexec_find_on_path (const char *, char *[], char *);
937 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
938 if (__dosexec_find_on_path (p
, (char **)0, shellpath
))
942 for (p
= shellpath
; *p
; p
++)
947 v
= define_variable_loc (varname
, strlen (varname
),
948 shellpath
, origin
, flavor
== f_recursive
,
953 char *shellbase
, *bslash
;
954 struct variable
*pathv
= lookup_variable ("PATH", 4);
959 shellbase
= strrchr (p
, '/');
960 bslash
= strrchr (p
, '\\');
961 if (!shellbase
|| bslash
> shellbase
)
963 if (!shellbase
&& p
[1] == ':')
970 /* Search for the basename of the shell (with standard
971 executable extensions) along the $PATH. */
973 pathlen
= strlen (pathv
->value
);
974 path_string
= (char *)xmalloc (5 + pathlen
+ 2 + 1);
975 /* On MSDOS, current directory is considered as part of $PATH. */
976 sprintf (path_string
, "PATH=.;%s", pathv
? pathv
->value
: "");
977 fake_env
[0] = path_string
;
978 fake_env
[1] = (char *)0;
979 if (__dosexec_find_on_path (shellbase
, fake_env
, shellpath
))
983 for (p
= shellpath
; *p
; p
++)
988 v
= define_variable_loc (varname
, strlen (varname
),
990 flavor
== f_recursive
, flocp
);
993 v
= lookup_variable (varname
, strlen (varname
));
999 #endif /* __MSDOS__ */
1001 if ((origin
== o_file
|| origin
== o_override
) && streq (varname
, "SHELL"))
1003 extern char *default_shell
;
1005 /* Call shell locator function. If it returns TRUE, then
1006 set no_default_sh_exe to indicate sh was found and
1007 set new value for SHELL variable. */
1009 if (find_and_set_default_shell (p
))
1011 v
= define_variable_in_set (varname
, strlen (varname
), default_shell
,
1012 origin
, flavor
== f_recursive
,
1014 ? current_variable_set_list
->set
1017 no_default_sh_exe
= 0;
1020 v
= lookup_variable (varname
, strlen (varname
));
1025 /* If we are defining variables inside an $(eval ...), we might have a
1026 different variable context pushed, not the global context (maybe we're
1027 inside a $(call ...) or something. Since this function is only ever
1028 invoked in places where we want to define globally visible variables,
1029 make sure we define this variable in the global set. */
1031 v
= define_variable_in_set (varname
, strlen (varname
), p
,
1032 origin
, flavor
== f_recursive
,
1034 ? current_variable_set_list
->set
: NULL
),
1044 /* Try to interpret LINE (a null-terminated string) as a variable definition.
1046 ORIGIN may be o_file, o_override, o_env, o_env_override,
1047 or o_command specifying that the variable definition comes
1048 from a makefile, an override directive, the environment with
1049 or without the -e switch, or the command line.
1051 See the comments for parse_variable_definition().
1053 If LINE was recognized as a variable definition, a pointer to its `struct
1054 variable' is returned. If LINE is not a variable definition, NULL is
1058 try_variable_definition (const struct floc
*flocp
, char *line
,
1059 enum variable_origin origin
, int target_var
)
1062 register char *p
= line
;
1065 enum variable_flavor flavor
= f_bogus
;
1066 char *name
, *expanded_name
;
1072 if (c
== '\0' || c
== '#')
1077 flavor
= f_recursive
;
1088 /* A colon other than := is a rule line, not a variable defn. */
1090 else if (c
== '+' && *p
== '=')
1096 else if (c
== '?' && *p
== '=')
1099 flavor
= f_conditional
;
1104 /* This might begin a variable expansion reference. Make sure we
1105 don't misrecognize chars inside the reference as =, := or +=. */
1114 continue; /* Nope. */
1116 /* P now points past the opening paren or brace.
1117 Count parens or braces until it is matched. */
1119 for (; *p
!= '\0'; ++p
)
1123 else if (*p
== closeparen
&& --count
< 0)
1132 beg
= next_token (line
);
1133 while (end
> beg
&& isblank ((unsigned char)end
[-1]))
1137 /* Expand the name, so "$(foo)bar = baz" works. */
1138 name
= (char *) alloca (end
- beg
+ 1);
1139 bcopy (beg
, name
, end
- beg
);
1140 name
[end
- beg
] = '\0';
1141 expanded_name
= allocated_variable_expand (name
);
1143 if (expanded_name
[0] == '\0')
1144 fatal (flocp
, _("empty variable name"));
1146 v
= do_variable_definition (flocp
, expanded_name
, p
,
1147 origin
, flavor
, target_var
);
1149 free (expanded_name
);
1154 /* Print information for variable V, prefixing it with PREFIX. */
1157 print_variable (const void *item
, void *arg
)
1159 const struct variable
*v
= (struct variable
*) item
;
1160 const char *prefix
= (char *) arg
;
1166 origin
= _("default");
1169 origin
= _("environment");
1172 origin
= _("makefile");
1174 case o_env_override
:
1175 origin
= _("environment under -e");
1178 origin
= _("command line");
1181 origin
= _("`override' directive");
1184 origin
= _("automatic");
1190 fputs ("# ", stdout
);
1191 fputs (origin
, stdout
);
1192 if (v
->fileinfo
.filenm
)
1193 printf (_(" (from `%s', line %lu)"),
1194 v
->fileinfo
.filenm
, v
->fileinfo
.lineno
);
1196 fputs (prefix
, stdout
);
1198 /* Is this a `define'? */
1199 if (v
->recursive
&& strchr (v
->value
, '\n') != 0)
1200 printf ("define %s\n%s\nendef\n", v
->name
, v
->value
);
1205 printf ("%s %s= ", v
->name
, v
->recursive
? v
->append
? "+" : "" : ":");
1207 /* Check if the value is just whitespace. */
1208 p
= next_token (v
->value
);
1209 if (p
!= v
->value
&& *p
== '\0')
1210 /* All whitespace. */
1211 printf ("$(subst ,,%s)", v
->value
);
1212 else if (v
->recursive
)
1213 fputs (v
->value
, stdout
);
1215 /* Double up dollar signs. */
1216 for (p
= v
->value
; *p
!= '\0'; ++p
)
1227 /* Print all the variables in SET. PREFIX is printed before
1228 the actual variable definitions (everything else is comments). */
1231 print_variable_set (struct variable_set
*set
, char *prefix
)
1233 hash_map_arg (&set
->table
, print_variable
, prefix
);
1235 fputs (_("# variable set hash-table stats:\n"), stdout
);
1236 fputs ("# ", stdout
);
1237 hash_print_stats (&set
->table
, stdout
);
1238 putc ('\n', stdout
);
1241 /* Print the data base of variables. */
1244 print_variable_data_base (void)
1246 puts (_("\n# Variables\n"));
1248 print_variable_set (&global_variable_set
, "");
1252 /* Print all the local variables of FILE. */
1255 print_file_variables (struct file
*file
)
1257 if (file
->variables
!= 0)
1258 print_variable_set (file
->variables
->set
, "# ");
1263 sync_Path_environment (void)
1265 char *path
= allocated_variable_expand ("$(Path)");
1266 static char *environ_path
= NULL
;
1272 * If done this before, don't leak memory unnecessarily.
1273 * Free the previous entry before allocating new one.
1276 free (environ_path
);
1279 * Create something WINDOWS32 world can grok
1281 convert_Path_to_windows32 (path
, ';');
1282 environ_path
= concat ("Path", "=", path
);
1283 putenv (environ_path
);