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
;
163 /* Lookup a variable whose name is a string starting at NAME
164 and with LENGTH chars. NAME need not be null-terminated.
165 Returns address of the `struct variable' containing all info
166 on the variable, or nil if no such variable is defined. */
169 lookup_variable (name
, length
)
173 const struct variable_set_list
*setlist
;
174 struct variable var_key
;
176 var_key
.name
= (char *) name
;
177 var_key
.length
= length
;
179 for (setlist
= current_variable_set_list
;
180 setlist
!= 0; setlist
= setlist
->next
)
182 const struct variable_set
*set
= setlist
->set
;
185 v
= (struct variable
*) hash_find_item ((struct hash_table
*) &set
->table
, &var_key
);
191 /* since we don't read envp[] on startup, try to get the
192 variable via getenv() here. */
194 char *vname
= alloca (length
+ 1);
196 strncpy (vname
, name
, length
);
198 value
= getenv (vname
);
207 while ((sptr
= strchr (sptr
, '$')))
218 nvalue
= alloca (strlen (value
) + scnt
+ 1);
237 return define_variable (vname
, length
, nvalue
, o_env
, 1);
241 return define_variable (vname
, length
, value
, o_env
, 1);
249 /* Lookup a variable whose name is a string starting at NAME
250 and with LENGTH chars in set SET. NAME need not be null-terminated.
251 Returns address of the `struct variable' containing all info
252 on the variable, or nil if no such variable is defined. */
255 lookup_variable_in_set (name
, length
, set
)
258 const struct variable_set
*set
;
260 struct variable var_key
;
262 var_key
.name
= (char *) name
;
263 var_key
.length
= length
;
265 return (struct variable
*) hash_find_item ((struct hash_table
*) &set
->table
, &var_key
);
268 /* Initialize FILE's variable set list. If FILE already has a variable set
269 list, the topmost variable set is left intact, but the the rest of the
270 chain is replaced with FILE->parent's setlist. If we're READing a
271 makefile, don't do the pattern variable search now, since the pattern
272 variable might not have been defined yet. */
275 initialize_file_variables (file
, reading
)
279 register struct variable_set_list
*l
= file
->variables
;
283 l
= (struct variable_set_list
*)
284 xmalloc (sizeof (struct variable_set_list
));
285 l
->set
= (struct variable_set
*) xmalloc (sizeof (struct variable_set
));
286 hash_init (&l
->set
->table
, PERFILE_VARIABLE_BUCKETS
,
287 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
291 if (file
->parent
== 0)
292 l
->next
= &global_setlist
;
295 initialize_file_variables (file
->parent
, reading
);
296 l
->next
= file
->parent
->variables
;
299 /* If we're not reading makefiles and we haven't looked yet, see if
300 we can find a pattern variable. */
302 if (!reading
&& !file
->pat_searched
)
304 struct pattern_var
*p
= lookup_pattern_var (file
->name
);
306 file
->pat_searched
= 1;
309 /* If we found one, insert it between the current target's
310 variables and the next set, whatever it is. */
311 file
->pat_variables
= (struct variable_set_list
*)
312 xmalloc (sizeof (struct variable_set_list
));
313 file
->pat_variables
->set
= p
->vars
->set
;
317 /* If we have a pattern variable match, set it up. */
319 if (file
->pat_variables
!= 0)
321 file
->pat_variables
->next
= l
->next
;
322 l
->next
= file
->pat_variables
;
326 /* Pop the top set off the current variable set list,
327 and free all its storage. */
330 free_variable_name_and_value (item
)
333 struct variable
*v
= (struct variable
*) item
;
339 pop_variable_scope ()
341 struct variable_set_list
*setlist
= current_variable_set_list
;
342 struct variable_set
*set
= setlist
->set
;
344 current_variable_set_list
= setlist
->next
;
345 free ((char *) setlist
);
347 hash_map (&set
->table
, free_variable_name_and_value
);
348 hash_free (&set
->table
, 1);
353 struct variable_set_list
*
354 create_new_variable_set ()
356 register struct variable_set_list
*setlist
;
357 register struct variable_set
*set
;
359 set
= (struct variable_set
*) xmalloc (sizeof (struct variable_set
));
360 hash_init (&set
->table
, SMALL_SCOPE_VARIABLE_BUCKETS
,
361 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
363 setlist
= (struct variable_set_list
*)
364 xmalloc (sizeof (struct variable_set_list
));
366 setlist
->next
= current_variable_set_list
;
371 /* Create a new variable set and push it on the current setlist. */
373 struct variable_set_list
*
374 push_new_variable_scope ()
376 return (current_variable_set_list
= create_new_variable_set());
379 /* Merge SET1 into SET0, freeing unused storage in SET1. */
382 merge_variable_sets (to_set
, from_set
)
383 struct variable_set
*to_set
, *from_set
;
385 struct variable
**from_var_slot
= (struct variable
**) from_set
->table
.ht_vec
;
386 struct variable
**from_var_end
= from_var_slot
+ from_set
->table
.ht_size
;
388 for ( ; from_var_slot
< from_var_end
; from_var_slot
++)
389 if (! HASH_VACANT (*from_var_slot
))
391 struct variable
*from_var
= *from_var_slot
;
392 struct variable
**to_var_slot
393 = (struct variable
**) hash_find_slot (&to_set
->table
, *from_var_slot
);
394 if (HASH_VACANT (*to_var_slot
))
395 hash_insert_at (&to_set
->table
, from_var
, to_var_slot
);
398 /* GKM FIXME: delete in from_set->table */
399 free (from_var
->value
);
405 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
408 merge_variable_set_lists (setlist0
, setlist1
)
409 struct variable_set_list
**setlist0
, *setlist1
;
411 register struct variable_set_list
*list0
= *setlist0
;
412 struct variable_set_list
*last0
= 0;
414 while (setlist1
!= 0 && list0
!= 0)
416 struct variable_set_list
*next
= setlist1
;
417 setlist1
= setlist1
->next
;
419 merge_variable_sets (list0
->set
, next
->set
);
428 *setlist0
= setlist1
;
430 last0
->next
= setlist1
;
434 /* Define the automatic variables, and record the addresses
435 of their structures so we can change their values quickly. */
438 define_automatic_variables ()
441 extern char* default_shell
;
443 extern char default_shell
[];
445 register struct variable
*v
;
448 sprintf (buf
, "%u", makelevel
);
449 (void) define_variable (MAKELEVEL_NAME
, MAKELEVEL_LENGTH
, buf
, o_env
, 0);
451 sprintf (buf
, "%s%s%s",
453 (remote_description
== 0 || remote_description
[0] == '\0')
455 (remote_description
== 0 || remote_description
[0] == '\0')
456 ? "" : remote_description
);
457 (void) define_variable ("MAKE_VERSION", 12, buf
, o_default
, 0);
460 /* Allow to specify a special shell just for Make,
461 and use $COMSPEC as the default $SHELL when appropriate. */
463 static char shell_str
[] = "SHELL";
464 const int shlen
= sizeof (shell_str
) - 1;
465 struct variable
*mshp
= lookup_variable ("MAKESHELL", 9);
466 struct variable
*comp
= lookup_variable ("COMSPEC", 7);
468 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
470 (void) define_variable (shell_str
, shlen
,
471 mshp
->value
, o_env_override
, 0);
474 /* $COMSPEC shouldn't override $SHELL. */
475 struct variable
*shp
= lookup_variable (shell_str
, shlen
);
478 (void) define_variable (shell_str
, shlen
, comp
->value
, o_env
, 0);
483 /* This won't override any definition, but it
484 will provide one if there isn't one there. */
485 v
= define_variable ("SHELL", 5, default_shell
, o_default
, 0);
486 v
->export
= v_export
; /* Always export SHELL. */
488 /* On MSDOS we do use SHELL from environment, since
489 it isn't a standard environment variable on MSDOS,
490 so whoever sets it, does that on purpose. */
492 /* Don't let SHELL come from the environment. */
493 if (*v
->value
== '\0' || v
->origin
== o_env
|| v
->origin
== o_env_override
)
497 v
->value
= xstrdup (default_shell
);
501 /* Make sure MAKEFILES gets exported if it is set. */
502 v
= define_variable ("MAKEFILES", 9, "", o_default
, 0);
505 /* Define the magic D and F variables in terms of
506 the automatic variables they are variations of. */
509 define_variable ("@D", 2, "$(dir $@)", o_automatic
, 1);
510 define_variable ("%D", 2, "$(dir $%)", o_automatic
, 1);
511 define_variable ("*D", 2, "$(dir $*)", o_automatic
, 1);
512 define_variable ("<D", 2, "$(dir $<)", o_automatic
, 1);
513 define_variable ("?D", 2, "$(dir $?)", o_automatic
, 1);
514 define_variable ("^D", 2, "$(dir $^)", o_automatic
, 1);
515 define_variable ("+D", 2, "$(dir $+)", o_automatic
, 1);
517 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic
, 1);
518 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic
, 1);
519 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic
, 1);
520 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic
, 1);
521 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic
, 1);
522 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic
, 1);
523 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic
, 1);
525 define_variable ("@F", 2, "$(notdir $@)", o_automatic
, 1);
526 define_variable ("%F", 2, "$(notdir $%)", o_automatic
, 1);
527 define_variable ("*F", 2, "$(notdir $*)", o_automatic
, 1);
528 define_variable ("<F", 2, "$(notdir $<)", o_automatic
, 1);
529 define_variable ("?F", 2, "$(notdir $?)", o_automatic
, 1);
530 define_variable ("^F", 2, "$(notdir $^)", o_automatic
, 1);
531 define_variable ("+F", 2, "$(notdir $+)", o_automatic
, 1);
534 int export_all_variables
;
536 /* Create a new environment for FILE's commands.
537 If FILE is nil, this is for the `shell' function.
538 The child's MAKELEVEL variable is incremented. */
541 target_environment (file
)
544 struct variable_set_list
*set_list
;
545 register struct variable_set_list
*s
;
546 struct hash_table table
;
547 struct variable
**v_slot
;
548 struct variable
**v_end
;
549 struct variable makelevel_key
;
554 set_list
= current_variable_set_list
;
556 set_list
= file
->variables
;
558 hash_init (&table
, VARIABLE_BUCKETS
,
559 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
561 /* Run through all the variable sets in the list,
562 accumulating variables in TABLE. */
563 for (s
= set_list
; s
!= 0; s
= s
->next
)
565 struct variable_set
*set
= s
->set
;
566 v_slot
= (struct variable
**) set
->table
.ht_vec
;
567 v_end
= v_slot
+ set
->table
.ht_size
;
568 for ( ; v_slot
< v_end
; v_slot
++)
569 if (! HASH_VACANT (*v_slot
))
571 struct variable
**new_slot
;
572 struct variable
*v
= *v_slot
;
575 /* If this is a per-target variable and it hasn't been touched
576 already then look up the global version and take its export
578 if (v
->per_target
&& v
->export
== v_default
)
582 gv
= lookup_variable_in_set (v
->name
, strlen(v
->name
),
583 &global_variable_set
);
585 v
->export
= gv
->export
;
591 if (v
->origin
== o_default
|| v
->origin
== o_automatic
)
592 /* Only export default variables by explicit request. */
595 if (! export_all_variables
596 && v
->origin
!= o_command
597 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
600 if (*p
!= '_' && (*p
< 'A' || *p
> 'Z')
601 && (*p
< 'a' || *p
> 'z'))
603 for (++p
; *p
!= '\0'; ++p
)
604 if (*p
!= '_' && (*p
< 'a' || *p
> 'z')
605 && (*p
< 'A' || *p
> 'Z') && (*p
< '0' || *p
> '9'))
618 if (v
->origin
== o_default
)
623 new_slot
= (struct variable
**) hash_find_slot (&table
, v
);
624 if (HASH_VACANT (*new_slot
))
625 hash_insert_at (&table
, v
, new_slot
);
629 makelevel_key
.name
= MAKELEVEL_NAME
;
630 makelevel_key
.length
= MAKELEVEL_LENGTH
;
631 hash_delete (&table
, &makelevel_key
);
633 result
= result_0
= (char **) xmalloc ((table
.ht_fill
+ 2) * sizeof (char *));
635 v_slot
= (struct variable
**) table
.ht_vec
;
636 v_end
= v_slot
+ table
.ht_size
;
637 for ( ; v_slot
< v_end
; v_slot
++)
638 if (! HASH_VACANT (*v_slot
))
640 struct variable
*v
= *v_slot
;
642 /* If V is recursively expanded and didn't come from the environment,
643 expand its value. If it came from the environment, it should
644 go back into the environment unchanged. */
646 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
648 char *value
= recursively_expand_for_file (v
, file
);
650 if (strcmp(v
->name
, "Path") == 0 ||
651 strcmp(v
->name
, "PATH") == 0)
652 convert_Path_to_windows32(value
, ';');
654 *result
++ = concat (v
->name
, "=", value
);
660 if (strcmp(v
->name
, "Path") == 0 ||
661 strcmp(v
->name
, "PATH") == 0)
662 convert_Path_to_windows32(v
->value
, ';');
664 *result
++ = concat (v
->name
, "=", v
->value
);
668 *result
= (char *) xmalloc (100);
669 (void) sprintf (*result
, "%s=%u", MAKELEVEL_NAME
, makelevel
+ 1);
672 hash_free (&table
, 0);
677 /* Given a variable, a value, and a flavor, define the variable.
678 See the try_variable_definition() function for details on the parameters. */
681 do_variable_definition (flocp
, varname
, value
, origin
, flavor
, target_var
)
682 const struct floc
*flocp
;
685 enum variable_origin origin
;
686 enum variable_flavor flavor
;
689 char *p
, *alloc_value
= NULL
;
693 /* Calculate the variable's new value in VALUE. */
699 /* Should not be possible. */
702 /* A simple variable definition "var := value". Expand the value.
703 We have to allocate memory since otherwise it'll clobber the
704 variable buffer, and we may still need that if we're looking at a
705 target-specific variable. */
706 p
= alloc_value
= allocated_variable_expand (value
);
709 /* A conditional variable definition "var ?= value".
710 The value is set IFF the variable is not defined yet. */
711 v
= lookup_variable (varname
, strlen (varname
));
715 flavor
= f_recursive
;
718 /* A recursive variable definition "var = value".
719 The value is used verbatim. */
724 /* If we have += but we're in a target variable context, we want to
725 append only with other variables in the context of this target. */
729 v
= lookup_variable_in_set (varname
, strlen (varname
),
730 current_variable_set_list
->set
);
733 v
= lookup_variable (varname
, strlen (varname
));
737 /* There was no old value.
738 This becomes a normal recursive definition. */
740 flavor
= f_recursive
;
744 /* Paste the old and new values together in VALUE. */
746 unsigned int oldlen
, newlen
;
750 /* The previous definition of the variable was recursive.
751 The new value is the unexpanded old and new values. */
752 flavor
= f_recursive
;
754 /* The previous definition of the variable was simple.
755 The new value comes from the old value, which was expanded
756 when it was set; and from the expanded new value. Allocate
757 memory for the expansion as we may still need the rest of the
758 buffer if we're looking at a target-specific variable. */
759 p
= alloc_value
= allocated_variable_expand (p
);
761 oldlen
= strlen (v
->value
);
763 p
= (char *) alloca (oldlen
+ 1 + newlen
+ 1);
764 bcopy (v
->value
, p
, oldlen
);
766 bcopy (value
, &p
[oldlen
+ 1], newlen
+ 1);
772 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
773 non-Unix systems don't conform to this default configuration (in
774 fact, most of them don't even have `/bin'). On the other hand,
775 $SHELL in the environment, if set, points to the real pathname of
777 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
778 the Makefile override $SHELL from the environment. But first, we
779 look for the basename of the shell in the directory where SHELL=
780 points, and along the $PATH; if it is found in any of these places,
781 we define $SHELL to be the actual pathname of the shell. Thus, if
782 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
783 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
784 defining SHELL to be "d:/unix/bash.exe". */
785 if ((origin
== o_file
|| origin
== o_override
)
786 && strcmp (varname
, "SHELL") == 0)
788 char shellpath
[PATH_MAX
];
789 extern char * __dosexec_find_on_path (const char *, char *[], char *);
791 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
792 if (__dosexec_find_on_path (p
, (char **)0, shellpath
))
796 for (p
= shellpath
; *p
; p
++)
801 v
= define_variable_loc (varname
, strlen (varname
),
802 shellpath
, origin
, flavor
== f_recursive
,
807 char *shellbase
, *bslash
;
808 struct variable
*pathv
= lookup_variable ("PATH", 4);
813 shellbase
= strrchr (p
, '/');
814 bslash
= strrchr (p
, '\\');
815 if (!shellbase
|| bslash
> shellbase
)
817 if (!shellbase
&& p
[1] == ':')
824 /* Search for the basename of the shell (with standard
825 executable extensions) along the $PATH. */
827 pathlen
= strlen (pathv
->value
);
828 path_string
= (char *)xmalloc (5 + pathlen
+ 2 + 1);
829 /* On MSDOS, current directory is considered as part of $PATH. */
830 sprintf (path_string
, "PATH=.;%s", pathv
? pathv
->value
: "");
831 fake_env
[0] = path_string
;
832 fake_env
[1] = (char *)0;
833 if (__dosexec_find_on_path (shellbase
, fake_env
, shellpath
))
837 for (p
= shellpath
; *p
; p
++)
842 v
= define_variable_loc (varname
, strlen (varname
),
844 flavor
== f_recursive
, flocp
);
847 v
= lookup_variable (varname
, strlen (varname
));
853 #endif /* __MSDOS__ */
855 if ((origin
== o_file
|| origin
== o_override
) && streq (varname
, "SHELL"))
857 extern char *default_shell
;
859 /* Call shell locator function. If it returns TRUE, then
860 set no_default_sh_exe to indicate sh was found and
861 set new value for SHELL variable. */
863 if (find_and_set_default_shell (p
))
865 v
= define_variable_in_set (varname
, strlen (varname
), default_shell
,
866 origin
, flavor
== f_recursive
,
868 ? current_variable_set_list
->set
871 no_default_sh_exe
= 0;
874 v
= lookup_variable (varname
, strlen (varname
));
879 /* If we are defining variables inside an $(eval ...), we might have a
880 different variable context pushed, not the global context (maybe we're
881 inside a $(call ...) or something. Since this function is only ever
882 invoked in places where we want to define globally visible variables,
883 make sure we define this variable in the global set. */
885 v
= define_variable_in_set (varname
, strlen (varname
), p
,
886 origin
, flavor
== f_recursive
,
888 ? current_variable_set_list
->set
: NULL
),
898 /* Try to interpret LINE (a null-terminated string) as a variable definition.
900 ORIGIN may be o_file, o_override, o_env, o_env_override,
901 or o_command specifying that the variable definition comes
902 from a makefile, an override directive, the environment with
903 or without the -e switch, or the command line.
905 See the comments for parse_variable_definition().
907 If LINE was recognized as a variable definition, a pointer to its `struct
908 variable' is returned. If LINE is not a variable definition, NULL is
912 try_variable_definition (flocp
, line
, origin
, target_var
)
913 const struct floc
*flocp
;
915 enum variable_origin origin
;
919 register char *p
= line
;
922 enum variable_flavor flavor
= f_bogus
;
923 char *name
, *expanded_name
;
929 if (c
== '\0' || c
== '#')
934 flavor
= f_recursive
;
945 /* A colon other than := is a rule line, not a variable defn. */
947 else if (c
== '+' && *p
== '=')
953 else if (c
== '?' && *p
== '=')
956 flavor
= f_conditional
;
961 /* This might begin a variable expansion reference. Make sure we
962 don't misrecognize chars inside the reference as =, := or +=. */
971 continue; /* Nope. */
973 /* P now points past the opening paren or brace.
974 Count parens or braces until it is matched. */
976 for (; *p
!= '\0'; ++p
)
980 else if (*p
== closeparen
&& --count
< 0)
989 beg
= next_token (line
);
990 while (end
> beg
&& isblank ((unsigned char)end
[-1]))
994 /* Expand the name, so "$(foo)bar = baz" works. */
995 name
= (char *) alloca (end
- beg
+ 1);
996 bcopy (beg
, name
, end
- beg
);
997 name
[end
- beg
] = '\0';
998 expanded_name
= allocated_variable_expand (name
);
1000 if (expanded_name
[0] == '\0')
1001 fatal (flocp
, _("empty variable name"));
1003 v
= do_variable_definition (flocp
, expanded_name
, p
,
1004 origin
, flavor
, target_var
);
1006 free (expanded_name
);
1011 /* Print information for variable V, prefixing it with PREFIX. */
1014 print_variable (v
, prefix
)
1015 register struct variable
*v
;
1023 origin
= _("default");
1026 origin
= _("environment");
1029 origin
= _("makefile");
1031 case o_env_override
:
1032 origin
= _("environment under -e");
1035 origin
= _("command line");
1038 origin
= _("`override' directive");
1041 origin
= _("automatic");
1047 fputs ("# ", stdout
);
1048 fputs (origin
, stdout
);
1049 if (v
->fileinfo
.filenm
)
1050 printf (_(" (from `%s', line %lu)"),
1051 v
->fileinfo
.filenm
, v
->fileinfo
.lineno
);
1053 fputs (prefix
, stdout
);
1055 /* Is this a `define'? */
1056 if (v
->recursive
&& strchr (v
->value
, '\n') != 0)
1057 printf ("define %s\n%s\nendef\n", v
->name
, v
->value
);
1062 printf ("%s %s= ", v
->name
, v
->recursive
? v
->append
? "+" : "" : ":");
1064 /* Check if the value is just whitespace. */
1065 p
= next_token (v
->value
);
1066 if (p
!= v
->value
&& *p
== '\0')
1067 /* All whitespace. */
1068 printf ("$(subst ,,%s)", v
->value
);
1069 else if (v
->recursive
)
1070 fputs (v
->value
, stdout
);
1072 /* Double up dollar signs. */
1073 for (p
= v
->value
; *p
!= '\0'; ++p
)
1084 /* Print all the variables in SET. PREFIX is printed before
1085 the actual variable definitions (everything else is comments). */
1088 print_variable_set (set
, prefix
)
1089 register struct variable_set
*set
;
1092 hash_map_arg (&set
->table
, print_variable
, prefix
);
1094 fputs (_("# variable set hash-table stats:\n"), stdout
);
1095 fputs ("# ", stdout
);
1096 hash_print_stats (&set
->table
, stdout
);
1097 putc ('\n', stdout
);
1100 /* Print the data base of variables. */
1103 print_variable_data_base ()
1105 puts (_("\n# Variables\n"));
1107 print_variable_set (&global_variable_set
, "");
1111 /* Print all the local variables of FILE. */
1114 print_file_variables (file
)
1117 if (file
->variables
!= 0)
1118 print_variable_set (file
->variables
->set
, "# ");
1123 sync_Path_environment(void)
1125 char* path
= allocated_variable_expand("$(Path)");
1126 static char* environ_path
= NULL
;
1132 * If done this before, don't leak memory unnecessarily.
1133 * Free the previous entry before allocating new one.
1139 * Create something WINDOWS32 world can grok
1141 convert_Path_to_windows32(path
, ';');
1142 environ_path
= concat("Path", "=", path
);
1143 putenv(environ_path
);