VirtualBox

source: kBuild/vendor/gnumake/current/variable.c@ 1989

Last change on this file since 1989 was 1989, checked in by bird, 16 years ago

Load gnumake-2008-10-28-CVS into vendor/gnumake/current.

  • Property svn:eol-style set to native
File size: 45.0 KB
Line 
1/* Internals of variables for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
4Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 3 of the License, or (at your option) any later
10version.
11
12GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License along with
17this program. If not, see <http://www.gnu.org/licenses/>. */
18
19#include "make.h"
20
21#include <assert.h>
22
23#include "dep.h"
24#include "filedef.h"
25#include "job.h"
26#include "commands.h"
27#include "variable.h"
28#include "rule.h"
29#ifdef WINDOWS32
30#include "pathstuff.h"
31#endif
32#include "hash.h"
33
34/* Chain of all pattern-specific variables. */
35
36static struct pattern_var *pattern_vars;
37
38/* Pointer to last struct in the chain, so we can add onto the end. */
39
40static struct pattern_var *last_pattern_var;
41
42/* Create a new pattern-specific variable struct. */
43
44struct pattern_var *
45create_pattern_var (const char *target, const char *suffix)
46{
47 register struct pattern_var *p = xmalloc (sizeof (struct pattern_var));
48
49 if (last_pattern_var != 0)
50 last_pattern_var->next = p;
51 else
52 pattern_vars = p;
53 last_pattern_var = p;
54 p->next = 0;
55
56 p->target = target;
57 p->len = strlen (target);
58 p->suffix = suffix + 1;
59
60 return p;
61}
62
63/* Look up a target in the pattern-specific variable list. */
64
65static struct pattern_var *
66lookup_pattern_var (struct pattern_var *start, const char *target)
67{
68 struct pattern_var *p;
69 unsigned int targlen = strlen(target);
70
71 for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
72 {
73 const char *stem;
74 unsigned int stemlen;
75
76 if (p->len > targlen)
77 /* It can't possibly match. */
78 continue;
79
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;
84
85 /* Compare the text in the pattern before the stem, if any. */
86 if (stem > target && !strneq (p->target, target, stem - target))
87 continue;
88
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
93 period. */
94 if (*p->suffix == stem[stemlen]
95 && (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
96 break;
97 }
98
99 return p;
100}
101
102
103/* Hash table of all global variable definitions. */
104
105static unsigned long
106variable_hash_1 (const void *keyv)
107{
108 struct variable const *key = (struct variable const *) keyv;
109 return_STRING_N_HASH_1 (key->name, key->length);
110}
111
112static unsigned long
113variable_hash_2 (const void *keyv)
114{
115 struct variable const *key = (struct variable const *) keyv;
116 return_STRING_N_HASH_2 (key->name, key->length);
117}
118
119static int
120variable_hash_cmp (const void *xv, const void *yv)
121{
122 struct variable const *x = (struct variable const *) xv;
123 struct variable const *y = (struct variable const *) yv;
124 int result = x->length - y->length;
125 if (result)
126 return result;
127 return_STRING_N_COMPARE (x->name, y->name, x->length);
128}
129
130#ifndef VARIABLE_BUCKETS
131#define VARIABLE_BUCKETS 523
132#endif
133#ifndef PERFILE_VARIABLE_BUCKETS
134#define PERFILE_VARIABLE_BUCKETS 23
135#endif
136#ifndef SMALL_SCOPE_VARIABLE_BUCKETS
137#define SMALL_SCOPE_VARIABLE_BUCKETS 13
138#endif
139
140static struct variable_set global_variable_set;
141static struct variable_set_list global_setlist
142 = { 0, &global_variable_set };
143struct variable_set_list *current_variable_set_list = &global_setlist;
144
145
146/* Implement variables. */
147
148void
149init_hash_global_variable_set (void)
150{
151 hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
152 variable_hash_1, variable_hash_2, variable_hash_cmp);
153}
154
155/* Define variable named NAME with value VALUE in SET. VALUE is copied.
156 LENGTH is the length of NAME, which does not need to be null-terminated.
157 ORIGIN specifies the origin of the variable (makefile, command line
158 or environment).
159 If RECURSIVE is nonzero a flag is set in the variable saying
160 that it should be recursively re-expanded. */
161
162struct variable *
163define_variable_in_set (const char *name, unsigned int length,
164 const char *value, enum variable_origin origin,
165 int recursive, struct variable_set *set,
166 const struct floc *flocp)
167{
168 struct variable *v;
169 struct variable **var_slot;
170 struct variable var_key;
171
172 if (set == NULL)
173 set = &global_variable_set;
174
175 var_key.name = (char *) name;
176 var_key.length = length;
177 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
178
179 if (env_overrides && origin == o_env)
180 origin = o_env_override;
181
182 v = *var_slot;
183 if (! HASH_VACANT (v))
184 {
185 if (env_overrides && v->origin == o_env)
186 /* V came from in the environment. Since it was defined
187 before the switches were parsed, it wasn't affected by -e. */
188 v->origin = o_env_override;
189
190 /* A variable of this name is already defined.
191 If the old definition is from a stronger source
192 than this one, don't redefine it. */
193 if ((int) origin >= (int) v->origin)
194 {
195 if (v->value != 0)
196 free (v->value);
197 v->value = xstrdup (value);
198 if (flocp != 0)
199 v->fileinfo = *flocp;
200 else
201 v->fileinfo.filenm = 0;
202 v->origin = origin;
203 v->recursive = recursive;
204 }
205 return v;
206 }
207
208 /* Create a new variable definition and add it to the hash table. */
209
210 v = xmalloc (sizeof (struct variable));
211 v->name = savestring (name, length);
212 v->length = length;
213 hash_insert_at (&set->table, v, var_slot);
214 v->value = xstrdup (value);
215 if (flocp != 0)
216 v->fileinfo = *flocp;
217 else
218 v->fileinfo.filenm = 0;
219 v->origin = origin;
220 v->recursive = recursive;
221 v->special = 0;
222 v->expanding = 0;
223 v->exp_count = 0;
224 v->per_target = 0;
225 v->append = 0;
226 v->export = v_default;
227
228 v->exportable = 1;
229 if (*name != '_' && (*name < 'A' || *name > 'Z')
230 && (*name < 'a' || *name > 'z'))
231 v->exportable = 0;
232 else
233 {
234 for (++name; *name != '\0'; ++name)
235 if (*name != '_' && (*name < 'a' || *name > 'z')
236 && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
237 break;
238
239 if (*name != '\0')
240 v->exportable = 0;
241 }
242
243 return v;
244}
245
246
247/* If the variable passed in is "special", handle its special nature.
248 Currently there are two such variables, both used for introspection:
249 .VARIABLES expands to a list of all the variables defined in this instance
250 of make.
251 .TARGETS expands to a list of all the targets defined in this
252 instance of make.
253 Returns the variable reference passed in. */
254
255#define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
256
257static struct variable *
258lookup_special_var (struct variable *var)
259{
260 static unsigned long last_var_count = 0;
261
262
263 /* This one actually turns out to be very hard, due to the way the parser
264 records targets. The way it works is that target information is collected
265 internally until make knows the target is completely specified. It unitl
266 it sees that some new construct (a new target or variable) is defined that
267 it knows the previous one is done. In short, this means that if you do
268 this:
269
270 all:
271
272 TARGS := $(.TARGETS)
273
274 then $(TARGS) won't contain "all", because it's not until after the
275 variable is created that the previous target is completed.
276
277 Changing this would be a major pain. I think a less complex way to do it
278 would be to pre-define the target files as soon as the first line is
279 parsed, then come back and do the rest of the definition as now. That
280 would allow $(.TARGETS) to be correct without a major change to the way
281 the parser works.
282
283 if (streq (var->name, ".TARGETS"))
284 var->value = build_target_list (var->value);
285 else
286 */
287
288 if (streq (var->name, ".VARIABLES")
289 && global_variable_set.table.ht_fill != last_var_count)
290 {
291 unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
292 unsigned long len;
293 char *p;
294 struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
295 struct variable **end = &vp[global_variable_set.table.ht_size];
296
297 /* Make sure we have at least MAX bytes in the allocated buffer. */
298 var->value = xrealloc (var->value, max);
299
300 /* Walk through the hash of variables, constructing a list of names. */
301 p = var->value;
302 len = 0;
303 for (; vp < end; ++vp)
304 if (!HASH_VACANT (*vp))
305 {
306 struct variable *v = *vp;
307 int l = v->length;
308
309 len += l + 1;
310 if (len > max)
311 {
312 unsigned long off = p - var->value;
313
314 max += EXPANSION_INCREMENT (l + 1);
315 var->value = xrealloc (var->value, max);
316 p = &var->value[off];
317 }
318
319 memcpy (p, v->name, l);
320 p += l;
321 *(p++) = ' ';
322 }
323 *(p-1) = '\0';
324
325 /* Remember how many variables are in our current count. Since we never
326 remove variables from the list, this is a reliable way to know whether
327 the list is up to date or needs to be recomputed. */
328
329 last_var_count = global_variable_set.table.ht_fill;
330 }
331
332 return var;
333}
334
335
336
337/* Lookup a variable whose name is a string starting at NAME
338 and with LENGTH chars. NAME need not be null-terminated.
339 Returns address of the `struct variable' containing all info
340 on the variable, or nil if no such variable is defined. */
341
342struct variable *
343lookup_variable (const char *name, unsigned int length)
344{
345 const struct variable_set_list *setlist;
346 struct variable var_key;
347
348 var_key.name = (char *) name;
349 var_key.length = length;
350
351 for (setlist = current_variable_set_list;
352 setlist != 0; setlist = setlist->next)
353 {
354 const struct variable_set *set = setlist->set;
355 struct variable *v;
356
357 v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
358 if (v)
359 return v->special ? lookup_special_var (v) : v;
360 }
361
362#ifdef VMS
363 /* since we don't read envp[] on startup, try to get the
364 variable via getenv() here. */
365 {
366 char *vname = alloca (length + 1);
367 char *value;
368 strncpy (vname, name, length);
369 vname[length] = 0;
370 value = getenv (vname);
371 if (value != 0)
372 {
373 char *sptr;
374 int scnt;
375
376 sptr = value;
377 scnt = 0;
378
379 while ((sptr = strchr (sptr, '$')))
380 {
381 scnt++;
382 sptr++;
383 }
384
385 if (scnt > 0)
386 {
387 char *nvalue;
388 char *nptr;
389
390 nvalue = alloca (strlen (value) + scnt + 1);
391 sptr = value;
392 nptr = nvalue;
393
394 while (*sptr)
395 {
396 if (*sptr == '$')
397 {
398 *nptr++ = '$';
399 *nptr++ = '$';
400 }
401 else
402 {
403 *nptr++ = *sptr;
404 }
405 sptr++;
406 }
407
408 *nptr = '\0';
409 return define_variable (vname, length, nvalue, o_env, 1);
410
411 }
412
413 return define_variable (vname, length, value, o_env, 1);
414 }
415 }
416#endif /* VMS */
417
418 return 0;
419}
420
421
422/* Lookup a variable whose name is a string starting at NAME
423 and with LENGTH chars in set SET. NAME need not be null-terminated.
424 Returns address of the `struct variable' containing all info
425 on the variable, or nil if no such variable is defined. */
426
427struct variable *
428lookup_variable_in_set (const char *name, unsigned int length,
429 const struct variable_set *set)
430{
431 struct variable var_key;
432
433 var_key.name = (char *) name;
434 var_key.length = length;
435
436 return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
437}
438
439
440/* Initialize FILE's variable set list. If FILE already has a variable set
441 list, the topmost variable set is left intact, but the the rest of the
442 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
443 rule, then we will use the "root" double-colon target's variable set as the
444 parent of FILE's variable set.
445
446 If we're READING a makefile, don't do the pattern variable search now,
447 since the pattern variable might not have been defined yet. */
448
449void
450initialize_file_variables (struct file *file, int reading)
451{
452 struct variable_set_list *l = file->variables;
453
454 if (l == 0)
455 {
456 l = (struct variable_set_list *)
457 xmalloc (sizeof (struct variable_set_list));
458 l->set = xmalloc (sizeof (struct variable_set));
459 hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
460 variable_hash_1, variable_hash_2, variable_hash_cmp);
461 file->variables = l;
462 }
463
464 /* If this is a double-colon, then our "parent" is the "root" target for
465 this double-colon rule. Since that rule has the same name, parent,
466 etc. we can just use its variables as the "next" for ours. */
467
468 if (file->double_colon && file->double_colon != file)
469 {
470 initialize_file_variables (file->double_colon, reading);
471 l->next = file->double_colon->variables;
472 return;
473 }
474
475 if (file->parent == 0)
476 l->next = &global_setlist;
477 else
478 {
479 initialize_file_variables (file->parent, reading);
480 l->next = file->parent->variables;
481 }
482
483 /* If we're not reading makefiles and we haven't looked yet, see if
484 we can find pattern variables for this target. */
485
486 if (!reading && !file->pat_searched)
487 {
488 struct pattern_var *p;
489
490 p = lookup_pattern_var (0, file->name);
491 if (p != 0)
492 {
493 struct variable_set_list *global = current_variable_set_list;
494
495 /* We found at least one. Set up a new variable set to accumulate
496 all the pattern variables that match this target. */
497
498 file->pat_variables = create_new_variable_set ();
499 current_variable_set_list = file->pat_variables;
500
501 do
502 {
503 /* We found one, so insert it into the set. */
504
505 struct variable *v;
506
507 if (p->variable.flavor == f_simple)
508 {
509 v = define_variable_loc (
510 p->variable.name, strlen (p->variable.name),
511 p->variable.value, p->variable.origin,
512 0, &p->variable.fileinfo);
513
514 v->flavor = f_simple;
515 }
516 else
517 {
518 v = do_variable_definition (
519 &p->variable.fileinfo, p->variable.name,
520 p->variable.value, p->variable.origin,
521 p->variable.flavor, 1);
522 }
523
524 /* Also mark it as a per-target and copy export status. */
525 v->per_target = p->variable.per_target;
526 v->export = p->variable.export;
527 }
528 while ((p = lookup_pattern_var (p, file->name)) != 0);
529
530 current_variable_set_list = global;
531 }
532 file->pat_searched = 1;
533 }
534
535 /* If we have a pattern variable match, set it up. */
536
537 if (file->pat_variables != 0)
538 {
539 file->pat_variables->next = l->next;
540 l->next = file->pat_variables;
541 }
542}
543
544
545/* Pop the top set off the current variable set list,
546 and free all its storage. */
547
548struct variable_set_list *
549create_new_variable_set (void)
550{
551 register struct variable_set_list *setlist;
552 register struct variable_set *set;
553
554 set = xmalloc (sizeof (struct variable_set));
555 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
556 variable_hash_1, variable_hash_2, variable_hash_cmp);
557
558 setlist = (struct variable_set_list *)
559 xmalloc (sizeof (struct variable_set_list));
560 setlist->set = set;
561 setlist->next = current_variable_set_list;
562
563 return setlist;
564}
565
566static void
567free_variable_name_and_value (const void *item)
568{
569 struct variable *v = (struct variable *) item;
570 free (v->name);
571 free (v->value);
572}
573
574void
575free_variable_set (struct variable_set_list *list)
576{
577 hash_map (&list->set->table, free_variable_name_and_value);
578 hash_free (&list->set->table, 1);
579 free (list->set);
580 free (list);
581}
582
583/* Create a new variable set and push it on the current setlist.
584 If we're pushing a global scope (that is, the current scope is the global
585 scope) then we need to "push" it the other way: file variable sets point
586 directly to the global_setlist so we need to replace that with the new one.
587 */
588
589struct variable_set_list *
590push_new_variable_scope (void)
591{
592 current_variable_set_list = create_new_variable_set();
593 if (current_variable_set_list->next == &global_setlist)
594 {
595 /* It was the global, so instead of new -> &global we want to replace
596 &global with the new one and have &global -> new, with current still
597 pointing to &global */
598 struct variable_set *set = current_variable_set_list->set;
599 current_variable_set_list->set = global_setlist.set;
600 global_setlist.set = set;
601 current_variable_set_list->next = global_setlist.next;
602 global_setlist.next = current_variable_set_list;
603 current_variable_set_list = &global_setlist;
604 }
605 return (current_variable_set_list);
606}
607
608void
609pop_variable_scope (void)
610{
611 struct variable_set_list *setlist;
612 struct variable_set *set;
613
614 /* Can't call this if there's no scope to pop! */
615 assert(current_variable_set_list->next != NULL);
616
617 if (current_variable_set_list != &global_setlist)
618 {
619 /* We're not pointing to the global setlist, so pop this one. */
620 setlist = current_variable_set_list;
621 set = setlist->set;
622 current_variable_set_list = setlist->next;
623 }
624 else
625 {
626 /* This set is the one in the global_setlist, but there is another global
627 set beyond that. We want to copy that set to global_setlist, then
628 delete what used to be in global_setlist. */
629 setlist = global_setlist.next;
630 set = global_setlist.set;
631 global_setlist.set = setlist->set;
632 global_setlist.next = setlist->next;
633 }
634
635 /* Free the one we no longer need. */
636 free (setlist);
637 hash_map (&set->table, free_variable_name_and_value);
638 hash_free (&set->table, 1);
639 free (set);
640}
641
642
643/* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
644
645static void
646merge_variable_sets (struct variable_set *to_set,
647 struct variable_set *from_set)
648{
649 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
650 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
651
652 for ( ; from_var_slot < from_var_end; from_var_slot++)
653 if (! HASH_VACANT (*from_var_slot))
654 {
655 struct variable *from_var = *from_var_slot;
656 struct variable **to_var_slot
657 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
658 if (HASH_VACANT (*to_var_slot))
659 hash_insert_at (&to_set->table, from_var, to_var_slot);
660 else
661 {
662 /* GKM FIXME: delete in from_set->table */
663 free (from_var->value);
664 free (from_var);
665 }
666 }
667}
668
669/* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
670
671void
672merge_variable_set_lists (struct variable_set_list **setlist0,
673 struct variable_set_list *setlist1)
674{
675 struct variable_set_list *to = *setlist0;
676 struct variable_set_list *last0 = 0;
677
678 /* If there's nothing to merge, stop now. */
679 if (!setlist1)
680 return;
681
682 /* This loop relies on the fact that all setlists terminate with the global
683 setlist (before NULL). If that's not true, arguably we SHOULD die. */
684 if (to)
685 while (setlist1 != &global_setlist && to != &global_setlist)
686 {
687 struct variable_set_list *from = setlist1;
688 setlist1 = setlist1->next;
689
690 merge_variable_sets (to->set, from->set);
691
692 last0 = to;
693 to = to->next;
694 }
695
696 if (setlist1 != &global_setlist)
697 {
698 if (last0 == 0)
699 *setlist0 = setlist1;
700 else
701 last0->next = setlist1;
702 }
703}
704
705
706/* Define the automatic variables, and record the addresses
707 of their structures so we can change their values quickly. */
708
709void
710define_automatic_variables (void)
711{
712#if defined(WINDOWS32) || defined(__EMX__)
713 extern char* default_shell;
714#else
715 extern char default_shell[];
716#endif
717 register struct variable *v;
718 char buf[200];
719
720 sprintf (buf, "%u", makelevel);
721 (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
722
723 sprintf (buf, "%s%s%s",
724 version_string,
725 (remote_description == 0 || remote_description[0] == '\0')
726 ? "" : "-",
727 (remote_description == 0 || remote_description[0] == '\0')
728 ? "" : remote_description);
729 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
730
731#ifdef __MSDOS__
732 /* Allow to specify a special shell just for Make,
733 and use $COMSPEC as the default $SHELL when appropriate. */
734 {
735 static char shell_str[] = "SHELL";
736 const int shlen = sizeof (shell_str) - 1;
737 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
738 struct variable *comp = lookup_variable ("COMSPEC", 7);
739
740 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
741 if (mshp)
742 (void) define_variable (shell_str, shlen,
743 mshp->value, o_env_override, 0);
744 else if (comp)
745 {
746 /* $COMSPEC shouldn't override $SHELL. */
747 struct variable *shp = lookup_variable (shell_str, shlen);
748
749 if (!shp)
750 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
751 }
752 }
753#elif defined(__EMX__)
754 {
755 static char shell_str[] = "SHELL";
756 const int shlen = sizeof (shell_str) - 1;
757 struct variable *shell = lookup_variable (shell_str, shlen);
758 struct variable *replace = lookup_variable ("MAKESHELL", 9);
759
760 /* if $MAKESHELL is defined in the environment assume o_env_override */
761 if (replace && *replace->value && replace->origin == o_env)
762 replace->origin = o_env_override;
763
764 /* if $MAKESHELL is not defined use $SHELL but only if the variable
765 did not come from the environment */
766 if (!replace || !*replace->value)
767 if (shell && *shell->value && (shell->origin == o_env
768 || shell->origin == o_env_override))
769 {
770 /* overwrite whatever we got from the environment */
771 free(shell->value);
772 shell->value = xstrdup (default_shell);
773 shell->origin = o_default;
774 }
775
776 /* Some people do not like cmd to be used as the default
777 if $SHELL is not defined in the Makefile.
778 With -DNO_CMD_DEFAULT you can turn off this behaviour */
779# ifndef NO_CMD_DEFAULT
780 /* otherwise use $COMSPEC */
781 if (!replace || !*replace->value)
782 replace = lookup_variable ("COMSPEC", 7);
783
784 /* otherwise use $OS2_SHELL */
785 if (!replace || !*replace->value)
786 replace = lookup_variable ("OS2_SHELL", 9);
787# else
788# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
789# endif
790
791 if (replace && *replace->value)
792 /* overwrite $SHELL */
793 (void) define_variable (shell_str, shlen, replace->value,
794 replace->origin, 0);
795 else
796 /* provide a definition if there is none */
797 (void) define_variable (shell_str, shlen, default_shell,
798 o_default, 0);
799 }
800
801#endif
802
803 /* This won't override any definition, but it will provide one if there
804 isn't one there. */
805 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
806#ifdef __MSDOS__
807 v->export = v_export; /* Export always SHELL. */
808#endif
809
810 /* On MSDOS we do use SHELL from environment, since it isn't a standard
811 environment variable on MSDOS, so whoever sets it, does that on purpose.
812 On OS/2 we do not use SHELL from environment but we have already handled
813 that problem above. */
814#if !defined(__MSDOS__) && !defined(__EMX__)
815 /* Don't let SHELL come from the environment. */
816 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
817 {
818 free (v->value);
819 v->origin = o_file;
820 v->value = xstrdup (default_shell);
821 }
822#endif
823
824 /* Make sure MAKEFILES gets exported if it is set. */
825 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
826 v->export = v_ifset;
827
828 /* Define the magic D and F variables in terms of
829 the automatic variables they are variations of. */
830
831#ifdef VMS
832 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
833 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
834 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
835 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
836 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
837 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
838 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
839#else
840 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
841 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
842 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
843 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
844 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
845 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
846 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
847#endif
848 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
849 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
850 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
851 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
852 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
853 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
854 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
855}
856
857
858int export_all_variables;
859
860/* Create a new environment for FILE's commands.
861 If FILE is nil, this is for the `shell' function.
862 The child's MAKELEVEL variable is incremented. */
863
864char **
865target_environment (struct file *file)
866{
867 struct variable_set_list *set_list;
868 register struct variable_set_list *s;
869 struct hash_table table;
870 struct variable **v_slot;
871 struct variable **v_end;
872 struct variable makelevel_key;
873 char **result_0;
874 char **result;
875
876 if (file == 0)
877 set_list = current_variable_set_list;
878 else
879 set_list = file->variables;
880
881 hash_init (&table, VARIABLE_BUCKETS,
882 variable_hash_1, variable_hash_2, variable_hash_cmp);
883
884 /* Run through all the variable sets in the list,
885 accumulating variables in TABLE. */
886 for (s = set_list; s != 0; s = s->next)
887 {
888 struct variable_set *set = s->set;
889 v_slot = (struct variable **) set->table.ht_vec;
890 v_end = v_slot + set->table.ht_size;
891 for ( ; v_slot < v_end; v_slot++)
892 if (! HASH_VACANT (*v_slot))
893 {
894 struct variable **new_slot;
895 struct variable *v = *v_slot;
896
897 /* If this is a per-target variable and it hasn't been touched
898 already then look up the global version and take its export
899 value. */
900 if (v->per_target && v->export == v_default)
901 {
902 struct variable *gv;
903
904 gv = lookup_variable_in_set (v->name, strlen(v->name),
905 &global_variable_set);
906 if (gv)
907 v->export = gv->export;
908 }
909
910 switch (v->export)
911 {
912 case v_default:
913 if (v->origin == o_default || v->origin == o_automatic)
914 /* Only export default variables by explicit request. */
915 continue;
916
917 /* The variable doesn't have a name that can be exported. */
918 if (! v->exportable)
919 continue;
920
921 if (! export_all_variables
922 && v->origin != o_command
923 && v->origin != o_env && v->origin != o_env_override)
924 continue;
925 break;
926
927 case v_export:
928 break;
929
930 case v_noexport:
931 {
932 /* If this is the SHELL variable and it's not exported,
933 then add the value from our original environment, if
934 the original environment defined a value for SHELL. */
935 extern struct variable shell_var;
936 if (streq (v->name, "SHELL") && shell_var.value)
937 {
938 v = &shell_var;
939 break;
940 }
941 continue;
942 }
943
944 case v_ifset:
945 if (v->origin == o_default)
946 continue;
947 break;
948 }
949
950 new_slot = (struct variable **) hash_find_slot (&table, v);
951 if (HASH_VACANT (*new_slot))
952 hash_insert_at (&table, v, new_slot);
953 }
954 }
955
956 makelevel_key.name = MAKELEVEL_NAME;
957 makelevel_key.length = MAKELEVEL_LENGTH;
958 hash_delete (&table, &makelevel_key);
959
960 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
961
962 v_slot = (struct variable **) table.ht_vec;
963 v_end = v_slot + table.ht_size;
964 for ( ; v_slot < v_end; v_slot++)
965 if (! HASH_VACANT (*v_slot))
966 {
967 struct variable *v = *v_slot;
968
969 /* If V is recursively expanded and didn't come from the environment,
970 expand its value. If it came from the environment, it should
971 go back into the environment unchanged. */
972 if (v->recursive
973 && v->origin != o_env && v->origin != o_env_override)
974 {
975 char *value = recursively_expand_for_file (v, file);
976#ifdef WINDOWS32
977 if (strcmp(v->name, "Path") == 0 ||
978 strcmp(v->name, "PATH") == 0)
979 convert_Path_to_windows32(value, ';');
980#endif
981 *result++ = xstrdup (concat (v->name, "=", value));
982 free (value);
983 }
984 else
985 {
986#ifdef WINDOWS32
987 if (strcmp(v->name, "Path") == 0 ||
988 strcmp(v->name, "PATH") == 0)
989 convert_Path_to_windows32(v->value, ';');
990#endif
991 *result++ = xstrdup (concat (v->name, "=", v->value));
992 }
993 }
994
995 *result = xmalloc (100);
996 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
997 *++result = 0;
998
999 hash_free (&table, 0);
1000
1001 return result_0;
1002}
1003
1004
1005static struct variable *
1006set_special_var (struct variable *var)
1007{
1008 if (streq (var->name, RECIPEPREFIX_NAME))
1009 {
1010 /* The user is resetting the command introduction prefix. This has to
1011 happen immediately, so that subsequent rules are interpreted
1012 properly. */
1013 cmd_prefix = var->value[0]=='\0' ? RECIPEPREFIX_DEFAULT : var->value[0];
1014 }
1015
1016 return var;
1017}
1018
1019
1020/* Given a variable, a value, and a flavor, define the variable.
1021 See the try_variable_definition() function for details on the parameters. */
1022
1023struct variable *
1024do_variable_definition (const struct floc *flocp, const char *varname,
1025 const char *value, enum variable_origin origin,
1026 enum variable_flavor flavor, int target_var)
1027{
1028 const char *p;
1029 char *alloc_value = NULL;
1030 struct variable *v;
1031 int append = 0;
1032 int conditional = 0;
1033
1034 /* Calculate the variable's new value in VALUE. */
1035
1036 switch (flavor)
1037 {
1038 default:
1039 case f_bogus:
1040 /* Should not be possible. */
1041 abort ();
1042 case f_simple:
1043 /* A simple variable definition "var := value". Expand the value.
1044 We have to allocate memory since otherwise it'll clobber the
1045 variable buffer, and we may still need that if we're looking at a
1046 target-specific variable. */
1047 p = alloc_value = allocated_variable_expand (value);
1048 break;
1049 case f_conditional:
1050 /* A conditional variable definition "var ?= value".
1051 The value is set IFF the variable is not defined yet. */
1052 v = lookup_variable (varname, strlen (varname));
1053 if (v)
1054 return v->special ? set_special_var (v) : v;
1055
1056 conditional = 1;
1057 flavor = f_recursive;
1058 /* FALLTHROUGH */
1059 case f_recursive:
1060 /* A recursive variable definition "var = value".
1061 The value is used verbatim. */
1062 p = value;
1063 break;
1064 case f_append:
1065 {
1066 /* If we have += but we're in a target variable context, we want to
1067 append only with other variables in the context of this target. */
1068 if (target_var)
1069 {
1070 append = 1;
1071 v = lookup_variable_in_set (varname, strlen (varname),
1072 current_variable_set_list->set);
1073
1074 /* Don't append from the global set if a previous non-appending
1075 target-specific variable definition exists. */
1076 if (v && !v->append)
1077 append = 0;
1078 }
1079 else
1080 v = lookup_variable (varname, strlen (varname));
1081
1082 if (v == 0)
1083 {
1084 /* There was no old value.
1085 This becomes a normal recursive definition. */
1086 p = value;
1087 flavor = f_recursive;
1088 }
1089 else
1090 {
1091 /* Paste the old and new values together in VALUE. */
1092
1093 unsigned int oldlen, vallen;
1094 const char *val;
1095 char *tp;
1096
1097 val = value;
1098 if (v->recursive)
1099 /* The previous definition of the variable was recursive.
1100 The new value is the unexpanded old and new values. */
1101 flavor = f_recursive;
1102 else
1103 /* The previous definition of the variable was simple.
1104 The new value comes from the old value, which was expanded
1105 when it was set; and from the expanded new value. Allocate
1106 memory for the expansion as we may still need the rest of the
1107 buffer if we're looking at a target-specific variable. */
1108 val = alloc_value = allocated_variable_expand (val);
1109
1110 oldlen = strlen (v->value);
1111 vallen = strlen (val);
1112 tp = alloca (oldlen + 1 + vallen + 1);
1113 memcpy (tp, v->value, oldlen);
1114 tp[oldlen] = ' ';
1115 memcpy (&tp[oldlen + 1], val, vallen + 1);
1116 p = tp;
1117 }
1118 }
1119 }
1120
1121#ifdef __MSDOS__
1122 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1123 non-Unix systems don't conform to this default configuration (in
1124 fact, most of them don't even have `/bin'). On the other hand,
1125 $SHELL in the environment, if set, points to the real pathname of
1126 the shell.
1127 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1128 the Makefile override $SHELL from the environment. But first, we
1129 look for the basename of the shell in the directory where SHELL=
1130 points, and along the $PATH; if it is found in any of these places,
1131 we define $SHELL to be the actual pathname of the shell. Thus, if
1132 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1133 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1134 defining SHELL to be "d:/unix/bash.exe". */
1135 if ((origin == o_file || origin == o_override)
1136 && strcmp (varname, "SHELL") == 0)
1137 {
1138 PATH_VAR (shellpath);
1139 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1140
1141 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1142 if (__dosexec_find_on_path (p, NULL, shellpath))
1143 {
1144 char *tp;
1145
1146 for (tp = shellpath; *tp; tp++)
1147 if (*tp == '\\')
1148 *tp = '/';
1149
1150 v = define_variable_loc (varname, strlen (varname),
1151 shellpath, origin, flavor == f_recursive,
1152 flocp);
1153 }
1154 else
1155 {
1156 const char *shellbase, *bslash;
1157 struct variable *pathv = lookup_variable ("PATH", 4);
1158 char *path_string;
1159 char *fake_env[2];
1160 size_t pathlen = 0;
1161
1162 shellbase = strrchr (p, '/');
1163 bslash = strrchr (p, '\\');
1164 if (!shellbase || bslash > shellbase)
1165 shellbase = bslash;
1166 if (!shellbase && p[1] == ':')
1167 shellbase = p + 1;
1168 if (shellbase)
1169 shellbase++;
1170 else
1171 shellbase = p;
1172
1173 /* Search for the basename of the shell (with standard
1174 executable extensions) along the $PATH. */
1175 if (pathv)
1176 pathlen = strlen (pathv->value);
1177 path_string = xmalloc (5 + pathlen + 2 + 1);
1178 /* On MSDOS, current directory is considered as part of $PATH. */
1179 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1180 fake_env[0] = path_string;
1181 fake_env[1] = 0;
1182 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1183 {
1184 char *tp;
1185
1186 for (tp = shellpath; *tp; tp++)
1187 if (*tp == '\\')
1188 *tp = '/';
1189
1190 v = define_variable_loc (varname, strlen (varname),
1191 shellpath, origin,
1192 flavor == f_recursive, flocp);
1193 }
1194 else
1195 v = lookup_variable (varname, strlen (varname));
1196
1197 free (path_string);
1198 }
1199 }
1200 else
1201#endif /* __MSDOS__ */
1202#ifdef WINDOWS32
1203 if ((origin == o_file || origin == o_override || origin == o_command)
1204 && streq (varname, "SHELL"))
1205 {
1206 extern char *default_shell;
1207
1208 /* Call shell locator function. If it returns TRUE, then
1209 set no_default_sh_exe to indicate sh was found and
1210 set new value for SHELL variable. */
1211
1212 if (find_and_set_default_shell (p))
1213 {
1214 v = define_variable_in_set (varname, strlen (varname), default_shell,
1215 origin, flavor == f_recursive,
1216 (target_var
1217 ? current_variable_set_list->set
1218 : NULL),
1219 flocp);
1220 no_default_sh_exe = 0;
1221 }
1222 else
1223 {
1224 if (alloc_value)
1225 free (alloc_value);
1226
1227 alloc_value = allocated_variable_expand (p);
1228 if (find_and_set_default_shell (alloc_value))
1229 {
1230 v = define_variable_in_set (varname, strlen (varname), p,
1231 origin, flavor == f_recursive,
1232 (target_var
1233 ? current_variable_set_list->set
1234 : NULL),
1235 flocp);
1236 no_default_sh_exe = 0;
1237 }
1238 else
1239 v = lookup_variable (varname, strlen (varname));
1240 }
1241 }
1242 else
1243#endif
1244
1245 /* If we are defining variables inside an $(eval ...), we might have a
1246 different variable context pushed, not the global context (maybe we're
1247 inside a $(call ...) or something. Since this function is only ever
1248 invoked in places where we want to define globally visible variables,
1249 make sure we define this variable in the global set. */
1250
1251 v = define_variable_in_set (varname, strlen (varname), p,
1252 origin, flavor == f_recursive,
1253 (target_var
1254 ? current_variable_set_list->set : NULL),
1255 flocp);
1256 v->append = append;
1257 v->conditional = conditional;
1258
1259 if (alloc_value)
1260 free (alloc_value);
1261
1262 return v->special ? set_special_var (v) : v;
1263}
1264
1265
1266/* Try to interpret LINE (a null-terminated string) as a variable definition.
1267
1268 ORIGIN may be o_file, o_override, o_env, o_env_override,
1269 or o_command specifying that the variable definition comes
1270 from a makefile, an override directive, the environment with
1271 or without the -e switch, or the command line.
1272
1273 See the comments for parse_variable_definition().
1274
1275 If LINE was recognized as a variable definition, a pointer to its `struct
1276 variable' is returned. If LINE is not a variable definition, NULL is
1277 returned. */
1278
1279struct variable *
1280parse_variable_definition (struct variable *v, char *line)
1281{
1282 register int c;
1283 register char *p = line;
1284 register char *beg;
1285 register char *end;
1286 enum variable_flavor flavor = f_bogus;
1287 char *name;
1288
1289 while (1)
1290 {
1291 c = *p++;
1292 if (c == '\0' || c == '#')
1293 return 0;
1294 if (c == '=')
1295 {
1296 end = p - 1;
1297 flavor = f_recursive;
1298 break;
1299 }
1300 else if (c == ':')
1301 if (*p == '=')
1302 {
1303 end = p++ - 1;
1304 flavor = f_simple;
1305 break;
1306 }
1307 else
1308 /* A colon other than := is a rule line, not a variable defn. */
1309 return 0;
1310 else if (c == '+' && *p == '=')
1311 {
1312 end = p++ - 1;
1313 flavor = f_append;
1314 break;
1315 }
1316 else if (c == '?' && *p == '=')
1317 {
1318 end = p++ - 1;
1319 flavor = f_conditional;
1320 break;
1321 }
1322 else if (c == '$')
1323 {
1324 /* This might begin a variable expansion reference. Make sure we
1325 don't misrecognize chars inside the reference as =, := or +=. */
1326 char closeparen;
1327 int count;
1328 c = *p++;
1329 if (c == '(')
1330 closeparen = ')';
1331 else if (c == '{')
1332 closeparen = '}';
1333 else
1334 continue; /* Nope. */
1335
1336 /* P now points past the opening paren or brace.
1337 Count parens or braces until it is matched. */
1338 count = 0;
1339 for (; *p != '\0'; ++p)
1340 {
1341 if (*p == c)
1342 ++count;
1343 else if (*p == closeparen && --count < 0)
1344 {
1345 ++p;
1346 break;
1347 }
1348 }
1349 }
1350 }
1351 v->flavor = flavor;
1352
1353 beg = next_token (line);
1354 while (end > beg && isblank ((unsigned char)end[-1]))
1355 --end;
1356 p = next_token (p);
1357 v->value = p;
1358
1359 /* Expand the name, so "$(foo)bar = baz" works. */
1360 name = alloca (end - beg + 1);
1361 memcpy (name, beg, end - beg);
1362 name[end - beg] = '\0';
1363 v->name = allocated_variable_expand (name);
1364
1365 if (v->name[0] == '\0')
1366 fatal (&v->fileinfo, _("empty variable name"));
1367
1368 return v;
1369}
1370
1371
1372/* Try to interpret LINE (a null-terminated string) as a variable definition.
1373
1374 ORIGIN may be o_file, o_override, o_env, o_env_override,
1375 or o_command specifying that the variable definition comes
1376 from a makefile, an override directive, the environment with
1377 or without the -e switch, or the command line.
1378
1379 See the comments for parse_variable_definition().
1380
1381 If LINE was recognized as a variable definition, a pointer to its `struct
1382 variable' is returned. If LINE is not a variable definition, NULL is
1383 returned. */
1384
1385struct variable *
1386try_variable_definition (const struct floc *flocp, char *line,
1387 enum variable_origin origin, int target_var)
1388{
1389 struct variable v;
1390 struct variable *vp;
1391
1392 if (flocp != 0)
1393 v.fileinfo = *flocp;
1394 else
1395 v.fileinfo.filenm = 0;
1396
1397 if (!parse_variable_definition (&v, line))
1398 return 0;
1399
1400 vp = do_variable_definition (flocp, v.name, v.value,
1401 origin, v.flavor, target_var);
1402
1403 free (v.name);
1404
1405 return vp;
1406}
1407
1408
1409/* Print information for variable V, prefixing it with PREFIX. */
1410
1411static void
1412print_variable (const void *item, void *arg)
1413{
1414 const struct variable *v = item;
1415 const char *prefix = arg;
1416 const char *origin;
1417
1418 switch (v->origin)
1419 {
1420 case o_default:
1421 origin = _("default");
1422 break;
1423 case o_env:
1424 origin = _("environment");
1425 break;
1426 case o_file:
1427 origin = _("makefile");
1428 break;
1429 case o_env_override:
1430 origin = _("environment under -e");
1431 break;
1432 case o_command:
1433 origin = _("command line");
1434 break;
1435 case o_override:
1436 origin = _("`override' directive");
1437 break;
1438 case o_automatic:
1439 origin = _("automatic");
1440 break;
1441 case o_invalid:
1442 default:
1443 abort ();
1444 }
1445 fputs ("# ", stdout);
1446 fputs (origin, stdout);
1447 if (v->fileinfo.filenm)
1448 printf (_(" (from `%s', line %lu)"),
1449 v->fileinfo.filenm, v->fileinfo.lineno);
1450 putchar ('\n');
1451 fputs (prefix, stdout);
1452
1453 /* Is this a `define'? */
1454 if (v->recursive && strchr (v->value, '\n') != 0)
1455 printf ("define %s\n%s\nendef\n", v->name, v->value);
1456 else
1457 {
1458 register char *p;
1459
1460 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1461
1462 /* Check if the value is just whitespace. */
1463 p = next_token (v->value);
1464 if (p != v->value && *p == '\0')
1465 /* All whitespace. */
1466 printf ("$(subst ,,%s)", v->value);
1467 else if (v->recursive)
1468 fputs (v->value, stdout);
1469 else
1470 /* Double up dollar signs. */
1471 for (p = v->value; *p != '\0'; ++p)
1472 {
1473 if (*p == '$')
1474 putchar ('$');
1475 putchar (*p);
1476 }
1477 putchar ('\n');
1478 }
1479}
1480
1481
1482/* Print all the variables in SET. PREFIX is printed before
1483 the actual variable definitions (everything else is comments). */
1484
1485void
1486print_variable_set (struct variable_set *set, char *prefix)
1487{
1488 hash_map_arg (&set->table, print_variable, prefix);
1489
1490 fputs (_("# variable set hash-table stats:\n"), stdout);
1491 fputs ("# ", stdout);
1492 hash_print_stats (&set->table, stdout);
1493 putc ('\n', stdout);
1494}
1495
1496/* Print the data base of variables. */
1497
1498void
1499print_variable_data_base (void)
1500{
1501 puts (_("\n# Variables\n"));
1502
1503 print_variable_set (&global_variable_set, "");
1504
1505 puts (_("\n# Pattern-specific Variable Values"));
1506
1507 {
1508 struct pattern_var *p;
1509 int rules = 0;
1510
1511 for (p = pattern_vars; p != 0; p = p->next)
1512 {
1513 ++rules;
1514 printf ("\n%s :\n", p->target);
1515 print_variable (&p->variable, "# ");
1516 }
1517
1518 if (rules == 0)
1519 puts (_("\n# No pattern-specific variable values."));
1520 else
1521 printf (_("\n# %u pattern-specific variable values"), rules);
1522 }
1523}
1524
1525
1526/* Print all the local variables of FILE. */
1527
1528void
1529print_file_variables (const struct file *file)
1530{
1531 if (file->variables != 0)
1532 print_variable_set (file->variables->set, "# ");
1533}
1534
1535#ifdef WINDOWS32
1536void
1537sync_Path_environment (void)
1538{
1539 char *path = allocated_variable_expand ("$(PATH)");
1540 static char *environ_path = NULL;
1541
1542 if (!path)
1543 return;
1544
1545 /*
1546 * If done this before, don't leak memory unnecessarily.
1547 * Free the previous entry before allocating new one.
1548 */
1549 if (environ_path)
1550 free (environ_path);
1551
1552 /*
1553 * Create something WINDOWS32 world can grok
1554 */
1555 convert_Path_to_windows32 (path, ';');
1556 environ_path = xstrdup (concat ("PATH", "=", path));
1557 putenv (environ_path);
1558 free (path);
1559}
1560#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette