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 /* Chain of all pattern-specific variables. */
35 static struct pattern_var
*pattern_vars
;
37 /* Pointer to last struct in the chain, so we can add onto the end. */
39 static struct pattern_var
*last_pattern_var
;
41 /* Create a new pattern-specific variable struct. */
44 create_pattern_var (char *target
, char *suffix
)
46 register struct pattern_var
*p
47 = (struct pattern_var
*) xmalloc (sizeof (struct pattern_var
));
49 if (last_pattern_var
!= 0)
50 last_pattern_var
->next
= p
;
57 p
->len
= strlen (target
);
58 p
->suffix
= suffix
+ 1;
63 /* Look up a target in the pattern-specific variable list. */
65 static struct pattern_var
*
66 lookup_pattern_var (struct pattern_var
*start
, char *target
)
68 struct pattern_var
*p
;
69 unsigned int targlen
= strlen(target
);
71 for (p
= start
? start
->next
: pattern_vars
; p
!= 0; p
= p
->next
)
77 /* It can't possibly match. */
80 /* From the lengths of the filename and the pattern parts,
81 find the stem: the part of the filename that matches the %. */
82 stem
= target
+ (p
->suffix
- p
->target
- 1);
83 stemlen
= targlen
- p
->len
+ 1;
85 /* Compare the text in the pattern before the stem, if any. */
86 if (stem
> target
&& !strneq (p
->target
, target
, stem
- target
))
89 /* Compare the text in the pattern after the stem, if any.
90 We could test simply using streq, but this way we compare the
91 first two characters immediately. This saves time in the very
92 common case where the first character matches because it is a
94 if (*p
->suffix
== stem
[stemlen
]
95 && (*p
->suffix
== '\0' || streq (&p
->suffix
[1], &stem
[stemlen
+1])))
102 /* Hash table of all global variable definitions. */
105 variable_hash_1 (const void *keyv
)
107 struct variable
const *key
= (struct variable
const *) keyv
;
108 return_STRING_N_HASH_1 (key
->name
, key
->length
);
112 variable_hash_2 (const void *keyv
)
114 struct variable
const *key
= (struct variable
const *) keyv
;
115 return_STRING_N_HASH_2 (key
->name
, key
->length
);
119 variable_hash_cmp (const void *xv
, const void *yv
)
121 struct variable
const *x
= (struct variable
const *) xv
;
122 struct variable
const *y
= (struct variable
const *) yv
;
123 int result
= x
->length
- y
->length
;
126 return_STRING_N_COMPARE (x
->name
, y
->name
, x
->length
);
129 #ifndef VARIABLE_BUCKETS
130 #define VARIABLE_BUCKETS 523
132 #ifndef PERFILE_VARIABLE_BUCKETS
133 #define PERFILE_VARIABLE_BUCKETS 23
135 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
136 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
139 static struct variable_set global_variable_set
;
140 static struct variable_set_list global_setlist
141 = { 0, &global_variable_set
};
142 struct variable_set_list
*current_variable_set_list
= &global_setlist
;
144 /* Implement variables. */
147 init_hash_global_variable_set (void)
149 hash_init (&global_variable_set
.table
, VARIABLE_BUCKETS
,
150 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
153 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
154 LENGTH is the length of NAME, which does not need to be null-terminated.
155 ORIGIN specifies the origin of the variable (makefile, command line
157 If RECURSIVE is nonzero a flag is set in the variable saying
158 that it should be recursively re-expanded. */
161 define_variable_in_set (const char *name
, unsigned int length
,
162 char *value
, enum variable_origin origin
,
163 int recursive
, struct variable_set
*set
,
164 const struct floc
*flocp
)
167 struct variable
**var_slot
;
168 struct variable var_key
;
171 set
= &global_variable_set
;
173 var_key
.name
= (char *) name
;
174 var_key
.length
= length
;
175 var_slot
= (struct variable
**) hash_find_slot (&set
->table
, &var_key
);
177 if (env_overrides
&& origin
== o_env
)
178 origin
= o_env_override
;
181 if (! HASH_VACANT (v
))
183 if (env_overrides
&& v
->origin
== o_env
)
184 /* V came from in the environment. Since it was defined
185 before the switches were parsed, it wasn't affected by -e. */
186 v
->origin
= o_env_override
;
188 /* A variable of this name is already defined.
189 If the old definition is from a stronger source
190 than this one, don't redefine it. */
191 if ((int) origin
>= (int) v
->origin
)
195 v
->value
= xstrdup (value
);
197 v
->fileinfo
= *flocp
;
199 v
->fileinfo
.filenm
= 0;
201 v
->recursive
= recursive
;
206 /* Create a new variable definition and add it to the hash table. */
208 v
= (struct variable
*) xmalloc (sizeof (struct variable
));
209 v
->name
= savestring (name
, length
);
211 hash_insert_at (&set
->table
, v
, var_slot
);
212 v
->value
= xstrdup (value
);
214 v
->fileinfo
= *flocp
;
216 v
->fileinfo
.filenm
= 0;
218 v
->recursive
= recursive
;
224 v
->export
= v_default
;
227 if (*name
!= '_' && (*name
< 'A' || *name
> 'Z')
228 && (*name
< 'a' || *name
> 'z'))
232 for (++name
; *name
!= '\0'; ++name
)
233 if (*name
!= '_' && (*name
< 'a' || *name
> 'z')
234 && (*name
< 'A' || *name
> 'Z') && !ISDIGIT(*name
))
244 /* If the variable passed in is "special", handle its special nature.
245 Currently there are two such variables, both used for introspection:
246 .VARIABLES expands to a list of all the variables defined in this instance
248 .TARGETS expands to a list of all the targets defined in this
250 Returns the variable reference passed in. */
252 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
254 static struct variable
*
255 handle_special_var (struct variable
*var
)
257 static unsigned long last_var_count
= 0;
260 /* This one actually turns out to be very hard, due to the way the parser
261 records targets. The way it works is that target information is collected
262 internally until make knows the target is completely specified. It unitl
263 it sees that some new construct (a new target or variable) is defined that
264 it knows the previous one is done. In short, this means that if you do
271 then $(TARGS) won't contain "all", because it's not until after the
272 variable is created that the previous target is completed.
274 Changing this would be a major pain. I think a less complex way to do it
275 would be to pre-define the target files as soon as the first line is
276 parsed, then come back and do the rest of the definition as now. That
277 would allow $(.TARGETS) to be correct without a major change to the way
280 if (streq (var->name, ".TARGETS"))
281 var->value = build_target_list (var->value);
285 if (streq (var
->name
, ".VARIABLES")
286 && global_variable_set
.table
.ht_fill
!= last_var_count
)
288 unsigned long max
= EXPANSION_INCREMENT (strlen (var
->value
));
291 struct variable
**vp
= (struct variable
**) global_variable_set
.table
.ht_vec
;
292 struct variable
**end
= &vp
[global_variable_set
.table
.ht_size
];
294 /* Make sure we have at least MAX bytes in the allocated buffer. */
295 var
->value
= xrealloc (var
->value
, max
);
297 /* Walk through the hash of variables, constructing a list of names. */
300 for (; vp
< end
; ++vp
)
301 if (!HASH_VACANT (*vp
))
303 struct variable
*v
= *vp
;
309 unsigned long off
= p
- var
->value
;
311 max
+= EXPANSION_INCREMENT (l
+ 1);
312 var
->value
= xrealloc (var
->value
, max
);
313 p
= &var
->value
[off
];
316 bcopy (v
->name
, p
, l
);
322 /* Remember how many variables are in our current count. Since we never
323 remove variables from the list, this is a reliable way to know whether
324 the list is up to date or needs to be recomputed. */
326 last_var_count
= global_variable_set
.table
.ht_fill
;
333 /* Lookup a variable whose name is a string starting at NAME
334 and with LENGTH chars. NAME need not be null-terminated.
335 Returns address of the `struct variable' containing all info
336 on the variable, or nil if no such variable is defined. */
339 lookup_variable (const char *name
, unsigned int length
)
341 const struct variable_set_list
*setlist
;
342 struct variable var_key
;
344 var_key
.name
= (char *) name
;
345 var_key
.length
= length
;
347 for (setlist
= current_variable_set_list
;
348 setlist
!= 0; setlist
= setlist
->next
)
350 const struct variable_set
*set
= setlist
->set
;
353 v
= (struct variable
*) hash_find_item ((struct hash_table
*) &set
->table
, &var_key
);
355 return v
->special
? handle_special_var (v
) : v
;
359 /* since we don't read envp[] on startup, try to get the
360 variable via getenv() here. */
362 char *vname
= alloca (length
+ 1);
364 strncpy (vname
, name
, length
);
366 value
= getenv (vname
);
375 while ((sptr
= strchr (sptr
, '$')))
386 nvalue
= alloca (strlen (value
) + scnt
+ 1);
405 return define_variable (vname
, length
, nvalue
, o_env
, 1);
409 return define_variable (vname
, length
, value
, o_env
, 1);
417 /* Lookup a variable whose name is a string starting at NAME
418 and with LENGTH chars in set SET. NAME need not be null-terminated.
419 Returns address of the `struct variable' containing all info
420 on the variable, or nil if no such variable is defined. */
423 lookup_variable_in_set (const char *name
, unsigned int length
,
424 const struct variable_set
*set
)
426 struct variable var_key
;
428 var_key
.name
= (char *) name
;
429 var_key
.length
= length
;
431 return (struct variable
*) hash_find_item ((struct hash_table
*) &set
->table
, &var_key
);
434 /* Initialize FILE's variable set list. If FILE already has a variable set
435 list, the topmost variable set is left intact, but the the rest of the
436 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
437 rule, then we will use the "root" double-colon target's variable set as the
438 parent of FILE's variable set.
440 If we're READing a makefile, don't do the pattern variable search now,
441 since the pattern variable might not have been defined yet. */
444 initialize_file_variables (struct file
*file
, int reading
)
446 register struct variable_set_list
*l
= file
->variables
;
450 l
= (struct variable_set_list
*)
451 xmalloc (sizeof (struct variable_set_list
));
452 l
->set
= (struct variable_set
*) xmalloc (sizeof (struct variable_set
));
453 hash_init (&l
->set
->table
, PERFILE_VARIABLE_BUCKETS
,
454 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
458 /* If this is a double-colon, then our "parent" is the "root" target for
459 this double-colon rule. Since that rule has the same name, parent,
460 etc. we can just use its variables as the "next" for ours. */
462 if (file
->double_colon
&& file
->double_colon
!= file
)
464 initialize_file_variables (file
->double_colon
, reading
);
465 l
->next
= file
->double_colon
->variables
;
469 if (file
->parent
== 0)
470 l
->next
= &global_setlist
;
473 initialize_file_variables (file
->parent
, reading
);
474 l
->next
= file
->parent
->variables
;
477 /* If we're not reading makefiles and we haven't looked yet, see if
478 we can find pattern variables for this target. */
480 if (!reading
&& !file
->pat_searched
)
482 struct pattern_var
*p
;
484 p
= lookup_pattern_var (0, file
->name
);
487 struct variable_set_list
*global
= current_variable_set_list
;
489 /* We found at least one. Set up a new variable set to accumulate
490 all the pattern variables that match this target. */
492 file
->pat_variables
= create_new_variable_set ();
493 current_variable_set_list
= file
->pat_variables
;
497 /* We found one, so insert it into the set. */
501 if (p
->variable
.flavor
== f_simple
)
503 v
= define_variable_loc (
504 p
->variable
.name
, strlen (p
->variable
.name
),
505 p
->variable
.value
, p
->variable
.origin
,
506 0, &p
->variable
.fileinfo
);
508 v
->flavor
= f_simple
;
512 v
= do_variable_definition (
513 &p
->variable
.fileinfo
, p
->variable
.name
,
514 p
->variable
.value
, p
->variable
.origin
,
515 p
->variable
.flavor
, 1);
518 /* Also mark it as a per-target and copy export status. */
519 v
->per_target
= p
->variable
.per_target
;
520 v
->export
= p
->variable
.export
;
522 while ((p
= lookup_pattern_var (p
, file
->name
)) != 0);
524 current_variable_set_list
= global
;
526 file
->pat_searched
= 1;
529 /* If we have a pattern variable match, set it up. */
531 if (file
->pat_variables
!= 0)
533 file
->pat_variables
->next
= l
->next
;
534 l
->next
= file
->pat_variables
;
538 /* Pop the top set off the current variable set list,
539 and free all its storage. */
542 free_variable_name_and_value (const void *item
)
544 struct variable
*v
= (struct variable
*) item
;
550 pop_variable_scope (void)
552 struct variable_set_list
*setlist
= current_variable_set_list
;
553 struct variable_set
*set
= setlist
->set
;
555 current_variable_set_list
= setlist
->next
;
556 free ((char *) setlist
);
558 hash_map (&set
->table
, free_variable_name_and_value
);
559 hash_free (&set
->table
, 1);
564 struct variable_set_list
*
565 create_new_variable_set (void)
567 register struct variable_set_list
*setlist
;
568 register struct variable_set
*set
;
570 set
= (struct variable_set
*) xmalloc (sizeof (struct variable_set
));
571 hash_init (&set
->table
, SMALL_SCOPE_VARIABLE_BUCKETS
,
572 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
574 setlist
= (struct variable_set_list
*)
575 xmalloc (sizeof (struct variable_set_list
));
577 setlist
->next
= current_variable_set_list
;
582 /* Create a new variable set and push it on the current setlist. */
584 struct variable_set_list
*
585 push_new_variable_scope (void)
587 return (current_variable_set_list
= create_new_variable_set());
590 /* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
593 merge_variable_sets (struct variable_set
*to_set
,
594 struct variable_set
*from_set
)
596 struct variable
**from_var_slot
= (struct variable
**) from_set
->table
.ht_vec
;
597 struct variable
**from_var_end
= from_var_slot
+ from_set
->table
.ht_size
;
599 for ( ; from_var_slot
< from_var_end
; from_var_slot
++)
600 if (! HASH_VACANT (*from_var_slot
))
602 struct variable
*from_var
= *from_var_slot
;
603 struct variable
**to_var_slot
604 = (struct variable
**) hash_find_slot (&to_set
->table
, *from_var_slot
);
605 if (HASH_VACANT (*to_var_slot
))
606 hash_insert_at (&to_set
->table
, from_var
, to_var_slot
);
609 /* GKM FIXME: delete in from_set->table */
610 free (from_var
->value
);
616 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
619 merge_variable_set_lists (struct variable_set_list
**setlist0
,
620 struct variable_set_list
*setlist1
)
622 register struct variable_set_list
*list0
= *setlist0
;
623 struct variable_set_list
*last0
= 0;
625 while (setlist1
!= 0 && list0
!= 0)
627 struct variable_set_list
*next
= setlist1
;
628 setlist1
= setlist1
->next
;
630 merge_variable_sets (list0
->set
, next
->set
);
639 *setlist0
= setlist1
;
641 last0
->next
= setlist1
;
645 /* Define the automatic variables, and record the addresses
646 of their structures so we can change their values quickly. */
649 define_automatic_variables (void)
651 #if defined(WINDOWS32) || defined(__EMX__)
652 extern char* default_shell
;
654 extern char default_shell
[];
656 register struct variable
*v
;
659 sprintf (buf
, "%u", makelevel
);
660 (void) define_variable (MAKELEVEL_NAME
, MAKELEVEL_LENGTH
, buf
, o_env
, 0);
662 sprintf (buf
, "%s%s%s",
664 (remote_description
== 0 || remote_description
[0] == '\0')
666 (remote_description
== 0 || remote_description
[0] == '\0')
667 ? "" : remote_description
);
668 (void) define_variable ("MAKE_VERSION", 12, buf
, o_default
, 0);
671 /* Allow to specify a special shell just for Make,
672 and use $COMSPEC as the default $SHELL when appropriate. */
674 static char shell_str
[] = "SHELL";
675 const int shlen
= sizeof (shell_str
) - 1;
676 struct variable
*mshp
= lookup_variable ("MAKESHELL", 9);
677 struct variable
*comp
= lookup_variable ("COMSPEC", 7);
679 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
681 (void) define_variable (shell_str
, shlen
,
682 mshp
->value
, o_env_override
, 0);
685 /* $COMSPEC shouldn't override $SHELL. */
686 struct variable
*shp
= lookup_variable (shell_str
, shlen
);
689 (void) define_variable (shell_str
, shlen
, comp
->value
, o_env
, 0);
692 #elif defined(__EMX__)
694 static char shell_str
[] = "SHELL";
695 const int shlen
= sizeof (shell_str
) - 1;
696 struct variable
*shell
= lookup_variable (shell_str
, shlen
);
697 struct variable
*replace
= lookup_variable ("MAKESHELL", 9);
699 /* if $MAKESHELL is defined in the environment assume o_env_override */
700 if (replace
&& *replace
->value
&& replace
->origin
== o_env
)
701 replace
->origin
= o_env_override
;
703 /* if $MAKESHELL is not defined use $SHELL but only if the variable
704 did not come from the environment */
705 if (!replace
|| !*replace
->value
)
706 if (shell
&& *shell
->value
&& (shell
->origin
== o_env
707 || shell
->origin
== o_env_override
))
709 /* overwrite whatever we got from the environment */
711 shell
->value
= xstrdup (default_shell
);
712 shell
->origin
= o_default
;
715 /* Some people do not like cmd to be used as the default
716 if $SHELL is not defined in the Makefile.
717 With -DNO_CMD_DEFAULT you can turn off this behaviour */
718 # ifndef NO_CMD_DEFAULT
719 /* otherwise use $COMSPEC */
720 if (!replace
|| !*replace
->value
)
721 replace
= lookup_variable ("COMSPEC", 7);
723 /* otherwise use $OS2_SHELL */
724 if (!replace
|| !*replace
->value
)
725 replace
= lookup_variable ("OS2_SHELL", 9);
727 # warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
730 if (replace
&& *replace
->value
)
731 /* overwrite $SHELL */
732 (void) define_variable (shell_str
, shlen
, replace
->value
,
735 /* provide a definition if there is none */
736 (void) define_variable (shell_str
, shlen
, default_shell
,
742 /* This won't override any definition, but it will provide one if there
744 v
= define_variable ("SHELL", 5, default_shell
, o_default
, 0);
746 /* On MSDOS we do use SHELL from environment, since it isn't a standard
747 environment variable on MSDOS, so whoever sets it, does that on purpose.
748 On OS/2 we do not use SHELL from environment but we have already handled
749 that problem above. */
750 #if !defined(__MSDOS__) && !defined(__EMX__)
751 /* Don't let SHELL come from the environment. */
752 if (*v
->value
== '\0' || v
->origin
== o_env
|| v
->origin
== o_env_override
)
756 v
->value
= xstrdup (default_shell
);
760 /* Make sure MAKEFILES gets exported if it is set. */
761 v
= define_variable ("MAKEFILES", 9, "", o_default
, 0);
764 /* Define the magic D and F variables in terms of
765 the automatic variables they are variations of. */
768 define_variable ("@D", 2, "$(dir $@)", o_automatic
, 1);
769 define_variable ("%D", 2, "$(dir $%)", o_automatic
, 1);
770 define_variable ("*D", 2, "$(dir $*)", o_automatic
, 1);
771 define_variable ("<D", 2, "$(dir $<)", o_automatic
, 1);
772 define_variable ("?D", 2, "$(dir $?)", o_automatic
, 1);
773 define_variable ("^D", 2, "$(dir $^)", o_automatic
, 1);
774 define_variable ("+D", 2, "$(dir $+)", o_automatic
, 1);
776 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic
, 1);
777 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic
, 1);
778 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic
, 1);
779 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic
, 1);
780 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic
, 1);
781 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic
, 1);
782 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic
, 1);
784 define_variable ("@F", 2, "$(notdir $@)", o_automatic
, 1);
785 define_variable ("%F", 2, "$(notdir $%)", o_automatic
, 1);
786 define_variable ("*F", 2, "$(notdir $*)", o_automatic
, 1);
787 define_variable ("<F", 2, "$(notdir $<)", o_automatic
, 1);
788 define_variable ("?F", 2, "$(notdir $?)", o_automatic
, 1);
789 define_variable ("^F", 2, "$(notdir $^)", o_automatic
, 1);
790 define_variable ("+F", 2, "$(notdir $+)", o_automatic
, 1);
793 int export_all_variables
;
795 /* Create a new environment for FILE's commands.
796 If FILE is nil, this is for the `shell' function.
797 The child's MAKELEVEL variable is incremented. */
800 target_environment (struct file
*file
)
802 struct variable_set_list
*set_list
;
803 register struct variable_set_list
*s
;
804 struct hash_table table
;
805 struct variable
**v_slot
;
806 struct variable
**v_end
;
807 struct variable makelevel_key
;
812 /* Set up a fake variable struct for the original SHELL value. */
814 ev
.value
= env_shell
;
817 set_list
= current_variable_set_list
;
819 set_list
= file
->variables
;
821 hash_init (&table
, VARIABLE_BUCKETS
,
822 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
824 /* Run through all the variable sets in the list,
825 accumulating variables in TABLE. */
826 for (s
= set_list
; s
!= 0; s
= s
->next
)
828 struct variable_set
*set
= s
->set
;
829 v_slot
= (struct variable
**) set
->table
.ht_vec
;
830 v_end
= v_slot
+ set
->table
.ht_size
;
831 for ( ; v_slot
< v_end
; v_slot
++)
832 if (! HASH_VACANT (*v_slot
))
834 struct variable
**new_slot
;
835 struct variable
*v
= *v_slot
;
837 /* If this is a per-target variable and it hasn't been touched
838 already then look up the global version and take its export
840 if (v
->per_target
&& v
->export
== v_default
)
844 gv
= lookup_variable_in_set (v
->name
, strlen(v
->name
),
845 &global_variable_set
);
847 v
->export
= gv
->export
;
853 if (v
->origin
== o_default
|| v
->origin
== o_automatic
)
854 /* Only export default variables by explicit request. */
857 /* The variable doesn't have a name that can be exported. */
861 if (! export_all_variables
862 && v
->origin
!= o_command
863 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
871 if (!streq (v
->name
, "SHELL"))
873 /* If this is the SHELL variable and it's not exported, then
874 add the value from our original environment. */
879 if (v
->origin
== o_default
)
884 new_slot
= (struct variable
**) hash_find_slot (&table
, v
);
885 if (HASH_VACANT (*new_slot
))
886 hash_insert_at (&table
, v
, new_slot
);
890 makelevel_key
.name
= MAKELEVEL_NAME
;
891 makelevel_key
.length
= MAKELEVEL_LENGTH
;
892 hash_delete (&table
, &makelevel_key
);
894 result
= result_0
= (char **) xmalloc ((table
.ht_fill
+ 2) * sizeof (char *));
896 v_slot
= (struct variable
**) table
.ht_vec
;
897 v_end
= v_slot
+ table
.ht_size
;
898 for ( ; v_slot
< v_end
; v_slot
++)
899 if (! HASH_VACANT (*v_slot
))
901 struct variable
*v
= *v_slot
;
903 /* If V is recursively expanded and didn't come from the environment,
904 expand its value. If it came from the environment, it should
905 go back into the environment unchanged. */
907 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
909 char *value
= recursively_expand_for_file (v
, file
);
911 if (strcmp(v
->name
, "Path") == 0 ||
912 strcmp(v
->name
, "PATH") == 0)
913 convert_Path_to_windows32(value
, ';');
915 *result
++ = concat (v
->name
, "=", value
);
921 if (strcmp(v
->name
, "Path") == 0 ||
922 strcmp(v
->name
, "PATH") == 0)
923 convert_Path_to_windows32(v
->value
, ';');
925 *result
++ = concat (v
->name
, "=", v
->value
);
929 *result
= (char *) xmalloc (100);
930 (void) sprintf (*result
, "%s=%u", MAKELEVEL_NAME
, makelevel
+ 1);
933 hash_free (&table
, 0);
938 /* Given a variable, a value, and a flavor, define the variable.
939 See the try_variable_definition() function for details on the parameters. */
942 do_variable_definition (const struct floc
*flocp
, const char *varname
,
943 char *value
, enum variable_origin origin
,
944 enum variable_flavor flavor
, int target_var
)
946 char *p
, *alloc_value
= NULL
;
951 /* Calculate the variable's new value in VALUE. */
957 /* Should not be possible. */
960 /* A simple variable definition "var := value". Expand the value.
961 We have to allocate memory since otherwise it'll clobber the
962 variable buffer, and we may still need that if we're looking at a
963 target-specific variable. */
964 p
= alloc_value
= allocated_variable_expand (value
);
967 /* A conditional variable definition "var ?= value".
968 The value is set IFF the variable is not defined yet. */
969 v
= lookup_variable (varname
, strlen (varname
));
974 flavor
= f_recursive
;
977 /* A recursive variable definition "var = value".
978 The value is used verbatim. */
983 /* If we have += but we're in a target variable context, we want to
984 append only with other variables in the context of this target. */
988 v
= lookup_variable_in_set (varname
, strlen (varname
),
989 current_variable_set_list
->set
);
991 /* Don't append from the global set if a previous non-appending
992 target-specific variable definition exists. */
997 v
= lookup_variable (varname
, strlen (varname
));
1001 /* There was no old value.
1002 This becomes a normal recursive definition. */
1004 flavor
= f_recursive
;
1008 /* Paste the old and new values together in VALUE. */
1010 unsigned int oldlen
, vallen
;
1015 /* The previous definition of the variable was recursive.
1016 The new value is the unexpanded old and new values. */
1017 flavor
= f_recursive
;
1019 /* The previous definition of the variable was simple.
1020 The new value comes from the old value, which was expanded
1021 when it was set; and from the expanded new value. Allocate
1022 memory for the expansion as we may still need the rest of the
1023 buffer if we're looking at a target-specific variable. */
1024 val
= alloc_value
= allocated_variable_expand (val
);
1026 oldlen
= strlen (v
->value
);
1027 vallen
= strlen (val
);
1028 p
= (char *) alloca (oldlen
+ 1 + vallen
+ 1);
1029 bcopy (v
->value
, p
, oldlen
);
1031 bcopy (val
, &p
[oldlen
+ 1], vallen
+ 1);
1037 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1038 non-Unix systems don't conform to this default configuration (in
1039 fact, most of them don't even have `/bin'). On the other hand,
1040 $SHELL in the environment, if set, points to the real pathname of
1042 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1043 the Makefile override $SHELL from the environment. But first, we
1044 look for the basename of the shell in the directory where SHELL=
1045 points, and along the $PATH; if it is found in any of these places,
1046 we define $SHELL to be the actual pathname of the shell. Thus, if
1047 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1048 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1049 defining SHELL to be "d:/unix/bash.exe". */
1050 if ((origin
== o_file
|| origin
== o_override
)
1051 && strcmp (varname
, "SHELL") == 0)
1053 char shellpath
[PATH_MAX
];
1054 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1056 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1057 if (__dosexec_find_on_path (p
, (char **)0, shellpath
))
1061 for (p
= shellpath
; *p
; p
++)
1066 v
= define_variable_loc (varname
, strlen (varname
),
1067 shellpath
, origin
, flavor
== f_recursive
,
1072 char *shellbase
, *bslash
;
1073 struct variable
*pathv
= lookup_variable ("PATH", 4);
1078 shellbase
= strrchr (p
, '/');
1079 bslash
= strrchr (p
, '\\');
1080 if (!shellbase
|| bslash
> shellbase
)
1082 if (!shellbase
&& p
[1] == ':')
1089 /* Search for the basename of the shell (with standard
1090 executable extensions) along the $PATH. */
1092 pathlen
= strlen (pathv
->value
);
1093 path_string
= (char *)xmalloc (5 + pathlen
+ 2 + 1);
1094 /* On MSDOS, current directory is considered as part of $PATH. */
1095 sprintf (path_string
, "PATH=.;%s", pathv
? pathv
->value
: "");
1096 fake_env
[0] = path_string
;
1097 fake_env
[1] = (char *)0;
1098 if (__dosexec_find_on_path (shellbase
, fake_env
, shellpath
))
1102 for (p
= shellpath
; *p
; p
++)
1107 v
= define_variable_loc (varname
, strlen (varname
),
1109 flavor
== f_recursive
, flocp
);
1112 v
= lookup_variable (varname
, strlen (varname
));
1118 #endif /* __MSDOS__ */
1120 if ((origin
== o_file
|| origin
== o_override
) && streq (varname
, "SHELL"))
1122 extern char *default_shell
;
1124 /* Call shell locator function. If it returns TRUE, then
1125 set no_default_sh_exe to indicate sh was found and
1126 set new value for SHELL variable. */
1128 if (find_and_set_default_shell (p
))
1130 v
= define_variable_in_set (varname
, strlen (varname
), default_shell
,
1131 origin
, flavor
== f_recursive
,
1133 ? current_variable_set_list
->set
1136 no_default_sh_exe
= 0;
1139 v
= lookup_variable (varname
, strlen (varname
));
1144 /* If we are defining variables inside an $(eval ...), we might have a
1145 different variable context pushed, not the global context (maybe we're
1146 inside a $(call ...) or something. Since this function is only ever
1147 invoked in places where we want to define globally visible variables,
1148 make sure we define this variable in the global set. */
1150 v
= define_variable_in_set (varname
, strlen (varname
), p
,
1151 origin
, flavor
== f_recursive
,
1153 ? current_variable_set_list
->set
: NULL
),
1156 v
->conditional
= conditional
;
1164 /* Try to interpret LINE (a null-terminated string) as a variable definition.
1166 ORIGIN may be o_file, o_override, o_env, o_env_override,
1167 or o_command specifying that the variable definition comes
1168 from a makefile, an override directive, the environment with
1169 or without the -e switch, or the command line.
1171 See the comments for parse_variable_definition().
1173 If LINE was recognized as a variable definition, a pointer to its `struct
1174 variable' is returned. If LINE is not a variable definition, NULL is
1178 parse_variable_definition (struct variable
*v
, char *line
)
1181 register char *p
= line
;
1184 enum variable_flavor flavor
= f_bogus
;
1190 if (c
== '\0' || c
== '#')
1195 flavor
= f_recursive
;
1206 /* A colon other than := is a rule line, not a variable defn. */
1208 else if (c
== '+' && *p
== '=')
1214 else if (c
== '?' && *p
== '=')
1217 flavor
= f_conditional
;
1222 /* This might begin a variable expansion reference. Make sure we
1223 don't misrecognize chars inside the reference as =, := or +=. */
1232 continue; /* Nope. */
1234 /* P now points past the opening paren or brace.
1235 Count parens or braces until it is matched. */
1237 for (; *p
!= '\0'; ++p
)
1241 else if (*p
== closeparen
&& --count
< 0)
1251 beg
= next_token (line
);
1252 while (end
> beg
&& isblank ((unsigned char)end
[-1]))
1257 /* Expand the name, so "$(foo)bar = baz" works. */
1258 name
= (char *) alloca (end
- beg
+ 1);
1259 bcopy (beg
, name
, end
- beg
);
1260 name
[end
- beg
] = '\0';
1261 v
->name
= allocated_variable_expand (name
);
1263 if (v
->name
[0] == '\0')
1264 fatal (&v
->fileinfo
, _("empty variable name"));
1269 /* Try to interpret LINE (a null-terminated string) as a variable definition.
1271 ORIGIN may be o_file, o_override, o_env, o_env_override,
1272 or o_command specifying that the variable definition comes
1273 from a makefile, an override directive, the environment with
1274 or without the -e switch, or the command line.
1276 See the comments for parse_variable_definition().
1278 If LINE was recognized as a variable definition, a pointer to its `struct
1279 variable' is returned. If LINE is not a variable definition, NULL is
1283 try_variable_definition (const struct floc
*flocp
, char *line
,
1284 enum variable_origin origin
, int target_var
)
1287 struct variable
*vp
;
1290 v
.fileinfo
= *flocp
;
1292 v
.fileinfo
.filenm
= 0;
1294 if (!parse_variable_definition (&v
, line
))
1297 vp
= do_variable_definition (flocp
, v
.name
, v
.value
,
1298 origin
, v
.flavor
, target_var
);
1305 /* Print information for variable V, prefixing it with PREFIX. */
1308 print_variable (const void *item
, void *arg
)
1310 const struct variable
*v
= (struct variable
*) item
;
1311 const char *prefix
= (char *) arg
;
1317 origin
= _("default");
1320 origin
= _("environment");
1323 origin
= _("makefile");
1325 case o_env_override
:
1326 origin
= _("environment under -e");
1329 origin
= _("command line");
1332 origin
= _("`override' directive");
1335 origin
= _("automatic");
1341 fputs ("# ", stdout
);
1342 fputs (origin
, stdout
);
1343 if (v
->fileinfo
.filenm
)
1344 printf (_(" (from `%s', line %lu)"),
1345 v
->fileinfo
.filenm
, v
->fileinfo
.lineno
);
1347 fputs (prefix
, stdout
);
1349 /* Is this a `define'? */
1350 if (v
->recursive
&& strchr (v
->value
, '\n') != 0)
1351 printf ("define %s\n%s\nendef\n", v
->name
, v
->value
);
1356 printf ("%s %s= ", v
->name
, v
->recursive
? v
->append
? "+" : "" : ":");
1358 /* Check if the value is just whitespace. */
1359 p
= next_token (v
->value
);
1360 if (p
!= v
->value
&& *p
== '\0')
1361 /* All whitespace. */
1362 printf ("$(subst ,,%s)", v
->value
);
1363 else if (v
->recursive
)
1364 fputs (v
->value
, stdout
);
1366 /* Double up dollar signs. */
1367 for (p
= v
->value
; *p
!= '\0'; ++p
)
1378 /* Print all the variables in SET. PREFIX is printed before
1379 the actual variable definitions (everything else is comments). */
1382 print_variable_set (struct variable_set
*set
, char *prefix
)
1384 hash_map_arg (&set
->table
, print_variable
, prefix
);
1386 fputs (_("# variable set hash-table stats:\n"), stdout
);
1387 fputs ("# ", stdout
);
1388 hash_print_stats (&set
->table
, stdout
);
1389 putc ('\n', stdout
);
1392 /* Print the data base of variables. */
1395 print_variable_data_base (void)
1397 puts (_("\n# Variables\n"));
1399 print_variable_set (&global_variable_set
, "");
1401 puts (_("\n# Pattern-specific Variable Values"));
1404 struct pattern_var
*p
;
1407 for (p
= pattern_vars
; p
!= 0; p
= p
->next
)
1410 printf ("\n%s :\n", p
->target
);
1411 print_variable (&p
->variable
, "# ");
1415 puts (_("\n# No pattern-specific variable values."));
1417 printf (_("\n# %u pattern-specific variable values"), rules
);
1422 /* Print all the local variables of FILE. */
1425 print_file_variables (struct file
*file
)
1427 if (file
->variables
!= 0)
1428 print_variable_set (file
->variables
->set
, "# ");
1433 sync_Path_environment (void)
1435 char *path
= allocated_variable_expand ("$(Path)");
1436 static char *environ_path
= NULL
;
1442 * If done this before, don't leak memory unnecessarily.
1443 * Free the previous entry before allocating new one.
1446 free (environ_path
);
1449 * Create something WINDOWS32 world can grok
1451 convert_Path_to_windows32 (path
, ';');
1452 environ_path
= concat ("Path", "=", path
);
1453 putenv (environ_path
);