1 /* Internals of variables for GNU Make.
2 Copyright (C) 1988-2012 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "pathstuff.h"
32 /* Chain of all pattern-specific variables. */
34 static struct pattern_var
*pattern_vars
;
36 /* Pointer to the last struct in the pack of a specific size, from 1 to 255.*/
38 static struct pattern_var
*last_pattern_vars
[256];
40 /* Create a new pattern-specific variable struct. The new variable is
41 inserted into the PATTERN_VARS list in the shortest patterns first
42 order to support the shortest stem matching (the variables are
43 matched in the reverse order so the ones with the longest pattern
44 will be considered first). Variables with the same pattern length
45 are inserted in the definition order. */
48 create_pattern_var (const char *target
, const char *suffix
)
50 register unsigned int len
= strlen (target
);
51 register struct pattern_var
*p
= xmalloc (sizeof (struct pattern_var
));
53 if (pattern_vars
!= 0)
55 if (len
< 256 && last_pattern_vars
[len
] != 0)
57 p
->next
= last_pattern_vars
[len
]->next
;
58 last_pattern_vars
[len
]->next
= p
;
62 /* Find the position where we can insert this variable. */
63 register struct pattern_var
**v
;
65 for (v
= &pattern_vars
; ; v
= &(*v
)->next
)
67 /* Insert at the end of the pack so that patterns with the
68 same length appear in the order they were defined .*/
70 if (*v
== 0 || (*v
)->len
> len
)
87 p
->suffix
= suffix
+ 1;
90 last_pattern_vars
[len
] = p
;
95 /* Look up a target in the pattern-specific variable list. */
97 static struct pattern_var
*
98 lookup_pattern_var (struct pattern_var
*start
, const char *target
)
100 struct pattern_var
*p
;
101 unsigned int targlen
= strlen(target
);
103 for (p
= start
? start
->next
: pattern_vars
; p
!= 0; p
= p
->next
)
106 unsigned int stemlen
;
108 if (p
->len
> targlen
)
109 /* It can't possibly match. */
112 /* From the lengths of the filename and the pattern parts,
113 find the stem: the part of the filename that matches the %. */
114 stem
= target
+ (p
->suffix
- p
->target
- 1);
115 stemlen
= targlen
- p
->len
+ 1;
117 /* Compare the text in the pattern before the stem, if any. */
118 if (stem
> target
&& !strneq (p
->target
, target
, stem
- target
))
121 /* Compare the text in the pattern after the stem, if any.
122 We could test simply using streq, but this way we compare the
123 first two characters immediately. This saves time in the very
124 common case where the first character matches because it is a
126 if (*p
->suffix
== stem
[stemlen
]
127 && (*p
->suffix
== '\0' || streq (&p
->suffix
[1], &stem
[stemlen
+1])))
134 /* Hash table of all global variable definitions. */
137 variable_hash_1 (const void *keyv
)
139 struct variable
const *key
= (struct variable
const *) keyv
;
140 return_STRING_N_HASH_1 (key
->name
, key
->length
);
144 variable_hash_2 (const void *keyv
)
146 struct variable
const *key
= (struct variable
const *) keyv
;
147 return_STRING_N_HASH_2 (key
->name
, key
->length
);
151 variable_hash_cmp (const void *xv
, const void *yv
)
153 struct variable
const *x
= (struct variable
const *) xv
;
154 struct variable
const *y
= (struct variable
const *) yv
;
155 int result
= x
->length
- y
->length
;
158 return_STRING_N_COMPARE (x
->name
, y
->name
, x
->length
);
161 #ifndef VARIABLE_BUCKETS
162 #define VARIABLE_BUCKETS 523
164 #ifndef PERFILE_VARIABLE_BUCKETS
165 #define PERFILE_VARIABLE_BUCKETS 23
167 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
168 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
171 static struct variable_set global_variable_set
;
172 static struct variable_set_list global_setlist
173 = { 0, &global_variable_set
, 0 };
174 struct variable_set_list
*current_variable_set_list
= &global_setlist
;
176 /* Implement variables. */
179 init_hash_global_variable_set (void)
181 hash_init (&global_variable_set
.table
, VARIABLE_BUCKETS
,
182 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
185 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
186 LENGTH is the length of NAME, which does not need to be null-terminated.
187 ORIGIN specifies the origin of the variable (makefile, command line
189 If RECURSIVE is nonzero a flag is set in the variable saying
190 that it should be recursively re-expanded. */
193 define_variable_in_set (const char *name
, unsigned int length
,
194 const char *value
, enum variable_origin origin
,
195 int recursive
, struct variable_set
*set
,
196 const struct floc
*flocp
)
199 struct variable
**var_slot
;
200 struct variable var_key
;
203 set
= &global_variable_set
;
205 var_key
.name
= (char *) name
;
206 var_key
.length
= length
;
207 var_slot
= (struct variable
**) hash_find_slot (&set
->table
, &var_key
);
209 if (env_overrides
&& origin
== o_env
)
210 origin
= o_env_override
;
213 if (! HASH_VACANT (v
))
215 if (env_overrides
&& v
->origin
== o_env
)
216 /* V came from in the environment. Since it was defined
217 before the switches were parsed, it wasn't affected by -e. */
218 v
->origin
= o_env_override
;
220 /* A variable of this name is already defined.
221 If the old definition is from a stronger source
222 than this one, don't redefine it. */
223 if ((int) origin
>= (int) v
->origin
)
227 v
->value
= xstrdup (value
);
229 v
->fileinfo
= *flocp
;
231 v
->fileinfo
.filenm
= 0;
233 v
->recursive
= recursive
;
238 /* Create a new variable definition and add it to the hash table. */
240 v
= xmalloc (sizeof (struct variable
));
241 v
->name
= xstrndup (name
, length
);
243 hash_insert_at (&set
->table
, v
, var_slot
);
244 v
->value
= xstrdup (value
);
246 v
->fileinfo
= *flocp
;
248 v
->fileinfo
.filenm
= 0;
250 v
->recursive
= recursive
;
257 v
->export
= v_default
;
260 if (*name
!= '_' && (*name
< 'A' || *name
> 'Z')
261 && (*name
< 'a' || *name
> 'z'))
265 for (++name
; *name
!= '\0'; ++name
)
266 if (*name
!= '_' && (*name
< 'a' || *name
> 'z')
267 && (*name
< 'A' || *name
> 'Z') && !ISDIGIT(*name
))
278 /* Undefine variable named NAME in SET. LENGTH is the length of NAME, which
279 does not need to be null-terminated. ORIGIN specifies the origin of the
280 variable (makefile, command line or environment). */
283 free_variable_name_and_value (const void *item
);
286 undefine_variable_in_set (const char *name
, unsigned int length
,
287 enum variable_origin origin
,
288 struct variable_set
*set
)
291 struct variable
**var_slot
;
292 struct variable var_key
;
295 set
= &global_variable_set
;
297 var_key
.name
= (char *) name
;
298 var_key
.length
= length
;
299 var_slot
= (struct variable
**) hash_find_slot (&set
->table
, &var_key
);
301 if (env_overrides
&& origin
== o_env
)
302 origin
= o_env_override
;
305 if (! HASH_VACANT (v
))
307 if (env_overrides
&& v
->origin
== o_env
)
308 /* V came from in the environment. Since it was defined
309 before the switches were parsed, it wasn't affected by -e. */
310 v
->origin
= o_env_override
;
312 /* If the definition is from a stronger source than this one, don't
314 if ((int) origin
>= (int) v
->origin
)
316 hash_delete_at (&set
->table
, var_slot
);
317 free_variable_name_and_value (v
);
322 /* If the variable passed in is "special", handle its special nature.
323 Currently there are two such variables, both used for introspection:
324 .VARIABLES expands to a list of all the variables defined in this instance
326 .TARGETS expands to a list of all the targets defined in this
328 Returns the variable reference passed in. */
330 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
332 static struct variable
*
333 lookup_special_var (struct variable
*var
)
335 static unsigned long last_var_count
= 0;
338 /* This one actually turns out to be very hard, due to the way the parser
339 records targets. The way it works is that target information is collected
340 internally until make knows the target is completely specified. It unitl
341 it sees that some new construct (a new target or variable) is defined that
342 it knows the previous one is done. In short, this means that if you do
349 then $(TARGS) won't contain "all", because it's not until after the
350 variable is created that the previous target is completed.
352 Changing this would be a major pain. I think a less complex way to do it
353 would be to pre-define the target files as soon as the first line is
354 parsed, then come back and do the rest of the definition as now. That
355 would allow $(.TARGETS) to be correct without a major change to the way
358 if (streq (var->name, ".TARGETS"))
359 var->value = build_target_list (var->value);
363 if (streq (var
->name
, ".VARIABLES")
364 && global_variable_set
.table
.ht_fill
!= last_var_count
)
366 unsigned long max
= EXPANSION_INCREMENT (strlen (var
->value
));
369 struct variable
**vp
= (struct variable
**) global_variable_set
.table
.ht_vec
;
370 struct variable
**end
= &vp
[global_variable_set
.table
.ht_size
];
372 /* Make sure we have at least MAX bytes in the allocated buffer. */
373 var
->value
= xrealloc (var
->value
, max
);
375 /* Walk through the hash of variables, constructing a list of names. */
378 for (; vp
< end
; ++vp
)
379 if (!HASH_VACANT (*vp
))
381 struct variable
*v
= *vp
;
387 unsigned long off
= p
- var
->value
;
389 max
+= EXPANSION_INCREMENT (l
+ 1);
390 var
->value
= xrealloc (var
->value
, max
);
391 p
= &var
->value
[off
];
394 memcpy (p
, v
->name
, l
);
400 /* Remember how many variables are in our current count. Since we never
401 remove variables from the list, this is a reliable way to know whether
402 the list is up to date or needs to be recomputed. */
404 last_var_count
= global_variable_set
.table
.ht_fill
;
411 /* Lookup a variable whose name is a string starting at NAME
412 and with LENGTH chars. NAME need not be null-terminated.
413 Returns address of the 'struct variable' containing all info
414 on the variable, or nil if no such variable is defined. */
417 lookup_variable (const char *name
, unsigned int length
)
419 const struct variable_set_list
*setlist
;
420 struct variable var_key
;
423 var_key
.name
= (char *) name
;
424 var_key
.length
= length
;
426 for (setlist
= current_variable_set_list
;
427 setlist
!= 0; setlist
= setlist
->next
)
429 const struct variable_set
*set
= setlist
->set
;
432 v
= (struct variable
*) hash_find_item ((struct hash_table
*) &set
->table
, &var_key
);
433 if (v
&& (!is_parent
|| !v
->private_var
))
434 return v
->special
? lookup_special_var (v
) : v
;
436 is_parent
|= setlist
->next_is_parent
;
440 /* since we don't read envp[] on startup, try to get the
441 variable via getenv() here. */
443 char *vname
= alloca (length
+ 1);
445 strncpy (vname
, name
, length
);
447 value
= getenv (vname
);
456 while ((sptr
= strchr (sptr
, '$')))
467 nvalue
= alloca (strlen (value
) + scnt
+ 1);
486 return define_variable (vname
, length
, nvalue
, o_env
, 1);
490 return define_variable (vname
, length
, value
, o_env
, 1);
498 /* Lookup a variable whose name is a string starting at NAME
499 and with LENGTH chars in set SET. NAME need not be null-terminated.
500 Returns address of the 'struct variable' containing all info
501 on the variable, or nil if no such variable is defined. */
504 lookup_variable_in_set (const char *name
, unsigned int length
,
505 const struct variable_set
*set
)
507 struct variable var_key
;
509 var_key
.name
= (char *) name
;
510 var_key
.length
= length
;
512 return (struct variable
*) hash_find_item ((struct hash_table
*) &set
->table
, &var_key
);
515 /* Initialize FILE's variable set list. If FILE already has a variable set
516 list, the topmost variable set is left intact, but the the rest of the
517 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
518 rule, then we will use the "root" double-colon target's variable set as the
519 parent of FILE's variable set.
521 If we're READING a makefile, don't do the pattern variable search now,
522 since the pattern variable might not have been defined yet. */
525 initialize_file_variables (struct file
*file
, int reading
)
527 struct variable_set_list
*l
= file
->variables
;
531 l
= (struct variable_set_list
*)
532 xmalloc (sizeof (struct variable_set_list
));
533 l
->set
= xmalloc (sizeof (struct variable_set
));
534 hash_init (&l
->set
->table
, PERFILE_VARIABLE_BUCKETS
,
535 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
539 /* If this is a double-colon, then our "parent" is the "root" target for
540 this double-colon rule. Since that rule has the same name, parent,
541 etc. we can just use its variables as the "next" for ours. */
543 if (file
->double_colon
&& file
->double_colon
!= file
)
545 initialize_file_variables (file
->double_colon
, reading
);
546 l
->next
= file
->double_colon
->variables
;
547 l
->next_is_parent
= 0;
551 if (file
->parent
== 0)
552 l
->next
= &global_setlist
;
555 initialize_file_variables (file
->parent
, reading
);
556 l
->next
= file
->parent
->variables
;
558 l
->next_is_parent
= 1;
560 /* If we're not reading makefiles and we haven't looked yet, see if
561 we can find pattern variables for this target. */
563 if (!reading
&& !file
->pat_searched
)
565 struct pattern_var
*p
;
567 p
= lookup_pattern_var (0, file
->name
);
570 struct variable_set_list
*global
= current_variable_set_list
;
572 /* We found at least one. Set up a new variable set to accumulate
573 all the pattern variables that match this target. */
575 file
->pat_variables
= create_new_variable_set ();
576 current_variable_set_list
= file
->pat_variables
;
580 /* We found one, so insert it into the set. */
584 if (p
->variable
.flavor
== f_simple
)
586 v
= define_variable_loc (
587 p
->variable
.name
, strlen (p
->variable
.name
),
588 p
->variable
.value
, p
->variable
.origin
,
589 0, &p
->variable
.fileinfo
);
591 v
->flavor
= f_simple
;
595 v
= do_variable_definition (
596 &p
->variable
.fileinfo
, p
->variable
.name
,
597 p
->variable
.value
, p
->variable
.origin
,
598 p
->variable
.flavor
, 1);
601 /* Also mark it as a per-target and copy export status. */
602 v
->per_target
= p
->variable
.per_target
;
603 v
->export
= p
->variable
.export
;
604 v
->private_var
= p
->variable
.private_var
;
606 while ((p
= lookup_pattern_var (p
, file
->name
)) != 0);
608 current_variable_set_list
= global
;
610 file
->pat_searched
= 1;
613 /* If we have a pattern variable match, set it up. */
615 if (file
->pat_variables
!= 0)
617 file
->pat_variables
->next
= l
->next
;
618 file
->pat_variables
->next_is_parent
= l
->next_is_parent
;
619 l
->next
= file
->pat_variables
;
620 l
->next_is_parent
= 0;
624 /* Pop the top set off the current variable set list,
625 and free all its storage. */
627 struct variable_set_list
*
628 create_new_variable_set (void)
630 register struct variable_set_list
*setlist
;
631 register struct variable_set
*set
;
633 set
= xmalloc (sizeof (struct variable_set
));
634 hash_init (&set
->table
, SMALL_SCOPE_VARIABLE_BUCKETS
,
635 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
637 setlist
= (struct variable_set_list
*)
638 xmalloc (sizeof (struct variable_set_list
));
640 setlist
->next
= current_variable_set_list
;
641 setlist
->next_is_parent
= 0;
647 free_variable_name_and_value (const void *item
)
649 struct variable
*v
= (struct variable
*) item
;
655 free_variable_set (struct variable_set_list
*list
)
657 hash_map (&list
->set
->table
, free_variable_name_and_value
);
658 hash_free (&list
->set
->table
, 1);
663 /* Create a new variable set and push it on the current setlist.
664 If we're pushing a global scope (that is, the current scope is the global
665 scope) then we need to "push" it the other way: file variable sets point
666 directly to the global_setlist so we need to replace that with the new one.
669 struct variable_set_list
*
670 push_new_variable_scope (void)
672 current_variable_set_list
= create_new_variable_set();
673 if (current_variable_set_list
->next
== &global_setlist
)
675 /* It was the global, so instead of new -> &global we want to replace
676 &global with the new one and have &global -> new, with current still
677 pointing to &global */
678 struct variable_set
*set
= current_variable_set_list
->set
;
679 current_variable_set_list
->set
= global_setlist
.set
;
680 global_setlist
.set
= set
;
681 current_variable_set_list
->next
= global_setlist
.next
;
682 global_setlist
.next
= current_variable_set_list
;
683 current_variable_set_list
= &global_setlist
;
685 return (current_variable_set_list
);
689 pop_variable_scope (void)
691 struct variable_set_list
*setlist
;
692 struct variable_set
*set
;
694 /* Can't call this if there's no scope to pop! */
695 assert(current_variable_set_list
->next
!= NULL
);
697 if (current_variable_set_list
!= &global_setlist
)
699 /* We're not pointing to the global setlist, so pop this one. */
700 setlist
= current_variable_set_list
;
702 current_variable_set_list
= setlist
->next
;
706 /* This set is the one in the global_setlist, but there is another global
707 set beyond that. We want to copy that set to global_setlist, then
708 delete what used to be in global_setlist. */
709 setlist
= global_setlist
.next
;
710 set
= global_setlist
.set
;
711 global_setlist
.set
= setlist
->set
;
712 global_setlist
.next
= setlist
->next
;
713 global_setlist
.next_is_parent
= setlist
->next_is_parent
;
716 /* Free the one we no longer need. */
718 hash_map (&set
->table
, free_variable_name_and_value
);
719 hash_free (&set
->table
, 1);
723 /* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
726 merge_variable_sets (struct variable_set
*to_set
,
727 struct variable_set
*from_set
)
729 struct variable
**from_var_slot
= (struct variable
**) from_set
->table
.ht_vec
;
730 struct variable
**from_var_end
= from_var_slot
+ from_set
->table
.ht_size
;
732 for ( ; from_var_slot
< from_var_end
; from_var_slot
++)
733 if (! HASH_VACANT (*from_var_slot
))
735 struct variable
*from_var
= *from_var_slot
;
736 struct variable
**to_var_slot
737 = (struct variable
**) hash_find_slot (&to_set
->table
, *from_var_slot
);
738 if (HASH_VACANT (*to_var_slot
))
739 hash_insert_at (&to_set
->table
, from_var
, to_var_slot
);
742 /* GKM FIXME: delete in from_set->table */
743 free (from_var
->value
);
749 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
752 merge_variable_set_lists (struct variable_set_list
**setlist0
,
753 struct variable_set_list
*setlist1
)
755 struct variable_set_list
*to
= *setlist0
;
756 struct variable_set_list
*last0
= 0;
758 /* If there's nothing to merge, stop now. */
762 /* This loop relies on the fact that all setlists terminate with the global
763 setlist (before NULL). If that's not true, arguably we SHOULD die. */
765 while (setlist1
!= &global_setlist
&& to
!= &global_setlist
)
767 struct variable_set_list
*from
= setlist1
;
768 setlist1
= setlist1
->next
;
770 merge_variable_sets (to
->set
, from
->set
);
776 if (setlist1
!= &global_setlist
)
779 *setlist0
= setlist1
;
781 last0
->next
= setlist1
;
785 /* Define the automatic variables, and record the addresses
786 of their structures so we can change their values quickly. */
789 define_automatic_variables (void)
791 #if defined(WINDOWS32) || defined(__EMX__)
792 extern char* default_shell
;
794 extern char default_shell
[];
796 register struct variable
*v
;
799 sprintf (buf
, "%u", makelevel
);
800 define_variable_cname (MAKELEVEL_NAME
, buf
, o_env
, 0);
802 sprintf (buf
, "%s%s%s",
804 (remote_description
== 0 || remote_description
[0] == '\0')
806 (remote_description
== 0 || remote_description
[0] == '\0')
807 ? "" : remote_description
);
808 define_variable_cname ("MAKE_VERSION", buf
, o_default
, 0);
811 /* Allow to specify a special shell just for Make,
812 and use $COMSPEC as the default $SHELL when appropriate. */
814 static char shell_str
[] = "SHELL";
815 const int shlen
= sizeof (shell_str
) - 1;
816 struct variable
*mshp
= lookup_variable ("MAKESHELL", 9);
817 struct variable
*comp
= lookup_variable ("COMSPEC", 7);
819 /* $(MAKESHELL) overrides $(SHELL) even if -e is in effect. */
821 (void) define_variable (shell_str
, shlen
,
822 mshp
->value
, o_env_override
, 0);
825 /* $(COMSPEC) shouldn't override $(SHELL). */
826 struct variable
*shp
= lookup_variable (shell_str
, shlen
);
829 (void) define_variable (shell_str
, shlen
, comp
->value
, o_env
, 0);
832 #elif defined(__EMX__)
834 static char shell_str
[] = "SHELL";
835 const int shlen
= sizeof (shell_str
) - 1;
836 struct variable
*shell
= lookup_variable (shell_str
, shlen
);
837 struct variable
*replace
= lookup_variable ("MAKESHELL", 9);
839 /* if $MAKESHELL is defined in the environment assume o_env_override */
840 if (replace
&& *replace
->value
&& replace
->origin
== o_env
)
841 replace
->origin
= o_env_override
;
843 /* if $MAKESHELL is not defined use $SHELL but only if the variable
844 did not come from the environment */
845 if (!replace
|| !*replace
->value
)
846 if (shell
&& *shell
->value
&& (shell
->origin
== o_env
847 || shell
->origin
== o_env_override
))
849 /* overwrite whatever we got from the environment */
851 shell
->value
= xstrdup (default_shell
);
852 shell
->origin
= o_default
;
855 /* Some people do not like cmd to be used as the default
856 if $SHELL is not defined in the Makefile.
857 With -DNO_CMD_DEFAULT you can turn off this behaviour */
858 # ifndef NO_CMD_DEFAULT
859 /* otherwise use $COMSPEC */
860 if (!replace
|| !*replace
->value
)
861 replace
= lookup_variable ("COMSPEC", 7);
863 /* otherwise use $OS2_SHELL */
864 if (!replace
|| !*replace
->value
)
865 replace
= lookup_variable ("OS2_SHELL", 9);
867 # warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
870 if (replace
&& *replace
->value
)
871 /* overwrite $SHELL */
872 (void) define_variable (shell_str
, shlen
, replace
->value
,
875 /* provide a definition if there is none */
876 (void) define_variable (shell_str
, shlen
, default_shell
,
882 /* This won't override any definition, but it will provide one if there
884 v
= define_variable_cname ("SHELL", default_shell
, o_default
, 0);
886 v
->export
= v_export
; /* Export always SHELL. */
889 /* On MSDOS we do use SHELL from environment, since it isn't a standard
890 environment variable on MSDOS, so whoever sets it, does that on purpose.
891 On OS/2 we do not use SHELL from environment but we have already handled
892 that problem above. */
893 #if !defined(__MSDOS__) && !defined(__EMX__)
894 /* Don't let SHELL come from the environment. */
895 if (*v
->value
== '\0' || v
->origin
== o_env
|| v
->origin
== o_env_override
)
899 v
->value
= xstrdup (default_shell
);
903 /* Make sure MAKEFILES gets exported if it is set. */
904 v
= define_variable_cname ("MAKEFILES", "", o_default
, 0);
907 /* Define the magic D and F variables in terms of
908 the automatic variables they are variations of. */
911 define_variable_cname ("@D", "$(dir $@)", o_automatic
, 1);
912 define_variable_cname ("%D", "$(dir $%)", o_automatic
, 1);
913 define_variable_cname ("*D", "$(dir $*)", o_automatic
, 1);
914 define_variable_cname ("<D", "$(dir $<)", o_automatic
, 1);
915 define_variable_cname ("?D", "$(dir $?)", o_automatic
, 1);
916 define_variable_cname ("^D", "$(dir $^)", o_automatic
, 1);
917 define_variable_cname ("+D", "$(dir $+)", o_automatic
, 1);
918 #elif defined(__MSDOS__) || defined(WINDOWS32)
919 /* For consistency, remove the trailing backslash as well as slash. */
920 define_variable_cname ("@D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $@)))",
922 define_variable_cname ("%D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $%)))",
924 define_variable_cname ("*D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $*)))",
926 define_variable_cname ("<D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $<)))",
928 define_variable_cname ("?D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $?)))",
930 define_variable_cname ("^D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $^)))",
932 define_variable_cname ("+D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $+)))",
934 #else /* not __MSDOS__, not WINDOWS32 */
935 define_variable_cname ("@D", "$(patsubst %/,%,$(dir $@))", o_automatic
, 1);
936 define_variable_cname ("%D", "$(patsubst %/,%,$(dir $%))", o_automatic
, 1);
937 define_variable_cname ("*D", "$(patsubst %/,%,$(dir $*))", o_automatic
, 1);
938 define_variable_cname ("<D", "$(patsubst %/,%,$(dir $<))", o_automatic
, 1);
939 define_variable_cname ("?D", "$(patsubst %/,%,$(dir $?))", o_automatic
, 1);
940 define_variable_cname ("^D", "$(patsubst %/,%,$(dir $^))", o_automatic
, 1);
941 define_variable_cname ("+D", "$(patsubst %/,%,$(dir $+))", o_automatic
, 1);
943 define_variable_cname ("@F", "$(notdir $@)", o_automatic
, 1);
944 define_variable_cname ("%F", "$(notdir $%)", o_automatic
, 1);
945 define_variable_cname ("*F", "$(notdir $*)", o_automatic
, 1);
946 define_variable_cname ("<F", "$(notdir $<)", o_automatic
, 1);
947 define_variable_cname ("?F", "$(notdir $?)", o_automatic
, 1);
948 define_variable_cname ("^F", "$(notdir $^)", o_automatic
, 1);
949 define_variable_cname ("+F", "$(notdir $+)", o_automatic
, 1);
952 int export_all_variables
;
954 /* Create a new environment for FILE's commands.
955 If FILE is nil, this is for the 'shell' function.
956 The child's MAKELEVEL variable is incremented. */
959 target_environment (struct file
*file
)
961 struct variable_set_list
*set_list
;
962 register struct variable_set_list
*s
;
963 struct hash_table table
;
964 struct variable
**v_slot
;
965 struct variable
**v_end
;
966 struct variable makelevel_key
;
971 set_list
= current_variable_set_list
;
973 set_list
= file
->variables
;
975 hash_init (&table
, VARIABLE_BUCKETS
,
976 variable_hash_1
, variable_hash_2
, variable_hash_cmp
);
978 /* Run through all the variable sets in the list,
979 accumulating variables in TABLE. */
980 for (s
= set_list
; s
!= 0; s
= s
->next
)
982 struct variable_set
*set
= s
->set
;
983 v_slot
= (struct variable
**) set
->table
.ht_vec
;
984 v_end
= v_slot
+ set
->table
.ht_size
;
985 for ( ; v_slot
< v_end
; v_slot
++)
986 if (! HASH_VACANT (*v_slot
))
988 struct variable
**new_slot
;
989 struct variable
*v
= *v_slot
;
991 /* If this is a per-target variable and it hasn't been touched
992 already then look up the global version and take its export
994 if (v
->per_target
&& v
->export
== v_default
)
998 gv
= lookup_variable_in_set (v
->name
, strlen(v
->name
),
999 &global_variable_set
);
1001 v
->export
= gv
->export
;
1007 if (v
->origin
== o_default
|| v
->origin
== o_automatic
)
1008 /* Only export default variables by explicit request. */
1011 /* The variable doesn't have a name that can be exported. */
1012 if (! v
->exportable
)
1015 if (! export_all_variables
1016 && v
->origin
!= o_command
1017 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
1026 /* If this is the SHELL variable and it's not exported,
1027 then add the value from our original environment, if
1028 the original environment defined a value for SHELL. */
1029 extern struct variable shell_var
;
1030 if (streq (v
->name
, "SHELL") && shell_var
.value
)
1039 if (v
->origin
== o_default
)
1044 new_slot
= (struct variable
**) hash_find_slot (&table
, v
);
1045 if (HASH_VACANT (*new_slot
))
1046 hash_insert_at (&table
, v
, new_slot
);
1050 makelevel_key
.name
= MAKELEVEL_NAME
;
1051 makelevel_key
.length
= MAKELEVEL_LENGTH
;
1052 hash_delete (&table
, &makelevel_key
);
1054 result
= result_0
= xmalloc ((table
.ht_fill
+ 2) * sizeof (char *));
1056 v_slot
= (struct variable
**) table
.ht_vec
;
1057 v_end
= v_slot
+ table
.ht_size
;
1058 for ( ; v_slot
< v_end
; v_slot
++)
1059 if (! HASH_VACANT (*v_slot
))
1061 struct variable
*v
= *v_slot
;
1063 /* If V is recursively expanded and didn't come from the environment,
1064 expand its value. If it came from the environment, it should
1065 go back into the environment unchanged. */
1067 && v
->origin
!= o_env
&& v
->origin
!= o_env_override
)
1069 char *value
= recursively_expand_for_file (v
, file
);
1071 if (strcmp(v
->name
, "Path") == 0 ||
1072 strcmp(v
->name
, "PATH") == 0)
1073 convert_Path_to_windows32(value
, ';');
1075 *result
++ = xstrdup (concat (3, v
->name
, "=", value
));
1081 if (strcmp(v
->name
, "Path") == 0 ||
1082 strcmp(v
->name
, "PATH") == 0)
1083 convert_Path_to_windows32(v
->value
, ';');
1085 *result
++ = xstrdup (concat (3, v
->name
, "=", v
->value
));
1089 *result
= xmalloc (100);
1090 sprintf (*result
, "%s=%u", MAKELEVEL_NAME
, makelevel
+ 1);
1093 hash_free (&table
, 0);
1098 static struct variable
*
1099 set_special_var (struct variable
*var
)
1101 if (streq (var
->name
, RECIPEPREFIX_NAME
))
1103 /* The user is resetting the command introduction prefix. This has to
1104 happen immediately, so that subsequent rules are interpreted
1106 cmd_prefix
= var
->value
[0]=='\0' ? RECIPEPREFIX_DEFAULT
: var
->value
[0];
1112 /* Given a string, shell-execute it and return a malloc'ed string of the
1113 * result. This removes only ONE newline (if any) at the end, for maximum
1114 * compatibility with the *BSD makes. If it fails, returns NULL. */
1117 shell_result (const char *p
)
1124 install_variable_buffer (&buf
, &len
);
1126 args
[0] = (char *) p
;
1128 variable_buffer_output (func_shell_base (variable_buffer
, args
, 0), "\0", 1);
1129 result
= strdup (variable_buffer
);
1131 restore_variable_buffer (buf
, len
);
1135 /* Given a variable, a value, and a flavor, define the variable.
1136 See the try_variable_definition() function for details on the parameters. */
1139 do_variable_definition (const struct floc
*flocp
, const char *varname
,
1140 const char *value
, enum variable_origin origin
,
1141 enum variable_flavor flavor
, int target_var
)
1144 char *alloc_value
= NULL
;
1147 int conditional
= 0;
1149 /* Calculate the variable's new value in VALUE. */
1155 /* Should not be possible. */
1158 /* A simple variable definition "var := value". Expand the value.
1159 We have to allocate memory since otherwise it'll clobber the
1160 variable buffer, and we may still need that if we're looking at a
1161 target-specific variable. */
1162 p
= alloc_value
= allocated_variable_expand (value
);
1166 /* A shell definition "var != value". Expand value, pass it to
1167 the shell, and store the result in recursively-expanded var. */
1168 char *q
= allocated_variable_expand (value
);
1169 p
= alloc_value
= shell_result (q
);
1171 flavor
= f_recursive
;
1175 /* A conditional variable definition "var ?= value".
1176 The value is set IFF the variable is not defined yet. */
1177 v
= lookup_variable (varname
, strlen (varname
));
1179 return v
->special
? set_special_var (v
) : v
;
1182 flavor
= f_recursive
;
1185 /* A recursive variable definition "var = value".
1186 The value is used verbatim. */
1191 /* If we have += but we're in a target variable context, we want to
1192 append only with other variables in the context of this target. */
1196 v
= lookup_variable_in_set (varname
, strlen (varname
),
1197 current_variable_set_list
->set
);
1199 /* Don't append from the global set if a previous non-appending
1200 target-specific variable definition exists. */
1201 if (v
&& !v
->append
)
1205 v
= lookup_variable (varname
, strlen (varname
));
1209 /* There was no old value.
1210 This becomes a normal recursive definition. */
1212 flavor
= f_recursive
;
1216 /* Paste the old and new values together in VALUE. */
1218 unsigned int oldlen
, vallen
;
1224 /* The previous definition of the variable was recursive.
1225 The new value is the unexpanded old and new values. */
1226 flavor
= f_recursive
;
1228 /* The previous definition of the variable was simple.
1229 The new value comes from the old value, which was expanded
1230 when it was set; and from the expanded new value. Allocate
1231 memory for the expansion as we may still need the rest of the
1232 buffer if we're looking at a target-specific variable. */
1233 val
= tp
= allocated_variable_expand (val
);
1235 oldlen
= strlen (v
->value
);
1236 vallen
= strlen (val
);
1237 p
= alloc_value
= xmalloc (oldlen
+ 1 + vallen
+ 1);
1238 memcpy (alloc_value
, v
->value
, oldlen
);
1239 alloc_value
[oldlen
] = ' ';
1240 memcpy (&alloc_value
[oldlen
+ 1], val
, vallen
+ 1);
1249 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1250 non-Unix systems don't conform to this default configuration (in
1251 fact, most of them don't even have '/bin'). On the other hand,
1252 $SHELL in the environment, if set, points to the real pathname of
1254 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1255 the Makefile override $SHELL from the environment. But first, we
1256 look for the basename of the shell in the directory where SHELL=
1257 points, and along the $PATH; if it is found in any of these places,
1258 we define $SHELL to be the actual pathname of the shell. Thus, if
1259 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1260 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1261 defining SHELL to be "d:/unix/bash.exe". */
1262 if ((origin
== o_file
|| origin
== o_override
)
1263 && strcmp (varname
, "SHELL") == 0)
1265 PATH_VAR (shellpath
);
1266 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1268 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1269 if (__dosexec_find_on_path (p
, NULL
, shellpath
))
1273 for (tp
= shellpath
; *tp
; tp
++)
1277 v
= define_variable_loc (varname
, strlen (varname
),
1278 shellpath
, origin
, flavor
== f_recursive
,
1283 const char *shellbase
, *bslash
;
1284 struct variable
*pathv
= lookup_variable ("PATH", 4);
1289 shellbase
= strrchr (p
, '/');
1290 bslash
= strrchr (p
, '\\');
1291 if (!shellbase
|| bslash
> shellbase
)
1293 if (!shellbase
&& p
[1] == ':')
1300 /* Search for the basename of the shell (with standard
1301 executable extensions) along the $PATH. */
1303 pathlen
= strlen (pathv
->value
);
1304 path_string
= xmalloc (5 + pathlen
+ 2 + 1);
1305 /* On MSDOS, current directory is considered as part of $PATH. */
1306 sprintf (path_string
, "PATH=.;%s", pathv
? pathv
->value
: "");
1307 fake_env
[0] = path_string
;
1309 if (__dosexec_find_on_path (shellbase
, fake_env
, shellpath
))
1313 for (tp
= shellpath
; *tp
; tp
++)
1317 v
= define_variable_loc (varname
, strlen (varname
),
1319 flavor
== f_recursive
, flocp
);
1322 v
= lookup_variable (varname
, strlen (varname
));
1328 #endif /* __MSDOS__ */
1330 if ((origin
== o_file
|| origin
== o_override
|| origin
== o_command
)
1331 && streq (varname
, "SHELL"))
1333 extern char *default_shell
;
1335 /* Call shell locator function. If it returns TRUE, then
1336 set no_default_sh_exe to indicate sh was found and
1337 set new value for SHELL variable. */
1339 if (find_and_set_default_shell (p
))
1341 v
= define_variable_in_set (varname
, strlen (varname
), default_shell
,
1342 origin
, flavor
== f_recursive
,
1344 ? current_variable_set_list
->set
1347 no_default_sh_exe
= 0;
1351 char *tp
= alloc_value
;
1353 alloc_value
= allocated_variable_expand (p
);
1355 if (find_and_set_default_shell (alloc_value
))
1357 v
= define_variable_in_set (varname
, strlen (varname
), p
,
1358 origin
, flavor
== f_recursive
,
1360 ? current_variable_set_list
->set
1363 no_default_sh_exe
= 0;
1366 v
= lookup_variable (varname
, strlen (varname
));
1375 /* If we are defining variables inside an $(eval ...), we might have a
1376 different variable context pushed, not the global context (maybe we're
1377 inside a $(call ...) or something. Since this function is only ever
1378 invoked in places where we want to define globally visible variables,
1379 make sure we define this variable in the global set. */
1381 v
= define_variable_in_set (varname
, strlen (varname
), p
,
1382 origin
, flavor
== f_recursive
,
1384 ? current_variable_set_list
->set
: NULL
),
1387 v
->conditional
= conditional
;
1392 return v
->special
? set_special_var (v
) : v
;
1395 /* Parse P (a null-terminated string) as a variable definition.
1397 If it is not a variable definition, return NULL and the contents of *VAR
1398 are undefined, except NAME is set to the first non-space character or NIL.
1400 If it is a variable definition, return a pointer to the char after the
1401 assignment token and set the following fields (only) of *VAR:
1402 name : name of the variable (ALWAYS SET) (NOT NUL-TERMINATED!)
1403 length : length of the variable name
1404 value : value of the variable (nul-terminated)
1405 flavor : flavor of the variable
1406 Other values in *VAR are unchanged.
1410 parse_variable_definition (const char *p
, struct variable
*var
)
1413 const char *e
= NULL
;
1416 var
->name
= (char *)p
;
1423 /* If we find a comment or EOS, it's not a variable definition. */
1424 if (c
== '\0' || c
== '#')
1429 /* This begins a variable expansion reference. Make sure we don't
1430 treat chars inside the reference as assignment tokens. */
1439 /* '$$' or '$X'. Either way, nothing special to do here. */
1442 /* P now points past the opening paren or brace.
1443 Count parens or braces until it is matched. */
1445 for (; *p
!= '\0'; ++p
)
1449 else if (*p
== closeparen
&& --count
< 0)
1458 /* If we find whitespace skip it, and remember we found it. */
1459 if (isblank ((unsigned char)c
))
1473 var
->flavor
= f_recursive
;
1479 /* Match assignment variants (:=, +=, ?=, !=) */
1485 var
->flavor
= f_simple
;
1488 var
->flavor
= f_append
;
1491 var
->flavor
= f_conditional
;
1494 var
->flavor
= f_shell
;
1497 /* If we skipped whitespace, non-assignments means no var. */
1501 /* Might be assignment, or might be $= or #=. Check. */
1510 /* Check for POSIX ::= syntax */
1513 /* A colon other than :=/::= is not a variable defn. */
1514 if (*p
!= ':' || p
[1] != '=')
1517 /* POSIX allows ::= to be the same as GNU make's := */
1518 var
->flavor
= f_simple
;
1525 /* If we skipped whitespace, non-assignments means no var. */
1530 var
->length
= e
- var
->name
;
1531 var
->value
= next_token (p
);
1535 /* Try to interpret LINE (a null-terminated string) as a variable definition.
1537 If LINE was recognized as a variable definition, a pointer to its 'struct
1538 variable' is returned. If LINE is not a variable definition, NULL is
1542 assign_variable_definition (struct variable
*v
, char *line
)
1546 if (!parse_variable_definition (line
, v
))
1549 /* Expand the name, so "$(foo)bar = baz" works. */
1550 name
= alloca (v
->length
+ 1);
1551 memcpy (name
, v
->name
, v
->length
);
1552 name
[v
->length
] = '\0';
1553 v
->name
= allocated_variable_expand (name
);
1555 if (v
->name
[0] == '\0')
1556 fatal (&v
->fileinfo
, _("empty variable name"));
1561 /* Try to interpret LINE (a null-terminated string) as a variable definition.
1563 ORIGIN may be o_file, o_override, o_env, o_env_override,
1564 or o_command specifying that the variable definition comes
1565 from a makefile, an override directive, the environment with
1566 or without the -e switch, or the command line.
1568 See the comments for assign_variable_definition().
1570 If LINE was recognized as a variable definition, a pointer to its 'struct
1571 variable' is returned. If LINE is not a variable definition, NULL is
1575 try_variable_definition (const struct floc
*flocp
, char *line
,
1576 enum variable_origin origin
, int target_var
)
1579 struct variable
*vp
;
1582 v
.fileinfo
= *flocp
;
1584 v
.fileinfo
.filenm
= 0;
1586 if (!assign_variable_definition (&v
, line
))
1589 vp
= do_variable_definition (flocp
, v
.name
, v
.value
,
1590 origin
, v
.flavor
, target_var
);
1597 /* Print information for variable V, prefixing it with PREFIX. */
1600 print_variable (const void *item
, void *arg
)
1602 const struct variable
*v
= item
;
1603 const char *prefix
= arg
;
1609 origin
= _("automatic");
1612 origin
= _("default");
1615 origin
= _("environment");
1618 origin
= _("makefile");
1620 case o_env_override
:
1621 origin
= _("environment under -e");
1624 origin
= _("command line");
1627 origin
= _("'override' directive");
1633 fputs ("# ", stdout
);
1634 fputs (origin
, stdout
);
1636 fputs (" private", stdout
);
1637 if (v
->fileinfo
.filenm
)
1638 printf (_(" (from '%s', line %lu)"),
1639 v
->fileinfo
.filenm
, v
->fileinfo
.lineno
);
1641 fputs (prefix
, stdout
);
1643 /* Is this a 'define'? */
1644 if (v
->recursive
&& strchr (v
->value
, '\n') != 0)
1645 printf ("define %s\n%s\nendef\n", v
->name
, v
->value
);
1650 printf ("%s %s= ", v
->name
, v
->recursive
? v
->append
? "+" : "" : ":");
1652 /* Check if the value is just whitespace. */
1653 p
= next_token (v
->value
);
1654 if (p
!= v
->value
&& *p
== '\0')
1655 /* All whitespace. */
1656 printf ("$(subst ,,%s)", v
->value
);
1657 else if (v
->recursive
)
1658 fputs (v
->value
, stdout
);
1660 /* Double up dollar signs. */
1661 for (p
= v
->value
; *p
!= '\0'; ++p
)
1673 print_auto_variable (const void *item
, void *arg
)
1675 const struct variable
*v
= item
;
1677 if (v
->origin
== o_automatic
)
1678 print_variable (item
, arg
);
1683 print_noauto_variable (const void *item
, void *arg
)
1685 const struct variable
*v
= item
;
1687 if (v
->origin
!= o_automatic
)
1688 print_variable (item
, arg
);
1692 /* Print all the variables in SET. PREFIX is printed before
1693 the actual variable definitions (everything else is comments). */
1696 print_variable_set (struct variable_set
*set
, char *prefix
, int pauto
)
1698 hash_map_arg (&set
->table
, (pauto
? print_auto_variable
: print_variable
),
1701 fputs (_("# variable set hash-table stats:\n"), stdout
);
1702 fputs ("# ", stdout
);
1703 hash_print_stats (&set
->table
, stdout
);
1704 putc ('\n', stdout
);
1707 /* Print the data base of variables. */
1710 print_variable_data_base (void)
1712 puts (_("\n# Variables\n"));
1714 print_variable_set (&global_variable_set
, "", 0);
1716 puts (_("\n# Pattern-specific Variable Values"));
1719 struct pattern_var
*p
;
1722 for (p
= pattern_vars
; p
!= 0; p
= p
->next
)
1725 printf ("\n%s :\n", p
->target
);
1726 print_variable (&p
->variable
, "# ");
1730 puts (_("\n# No pattern-specific variable values."));
1732 printf (_("\n# %u pattern-specific variable values"), rules
);
1737 /* Print all the local variables of FILE. */
1740 print_file_variables (const struct file
*file
)
1742 if (file
->variables
!= 0)
1743 print_variable_set (file
->variables
->set
, "# ", 1);
1747 print_target_variables (const struct file
*file
)
1749 if (file
->variables
!= 0)
1751 int l
= strlen (file
->name
);
1752 char *t
= alloca (l
+ 3);
1754 strcpy (t
, file
->name
);
1759 hash_map_arg (&file
->variables
->set
->table
, print_noauto_variable
, t
);
1765 sync_Path_environment (void)
1767 char *path
= allocated_variable_expand ("$(PATH)");
1768 static char *environ_path
= NULL
;
1774 * If done this before, don't leak memory unnecessarily.
1775 * Free the previous entry before allocating new one.
1778 free (environ_path
);
1781 * Create something WINDOWS32 world can grok
1783 convert_Path_to_windows32 (path
, ';');
1784 environ_path
= xstrdup (concat (3, "PATH", "=", path
));
1785 putenv (environ_path
);