VirtualBox

source: kBuild/vendor/gnumake/2007-05-23/variable.c@ 1989

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

Load /home/bird/src/Gnu/make/2007-05-23 into vendor/gnumake/current.

  • Property svn:eol-style set to native
File size: 43.9 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 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 2, or (at your option) any later version.
10
11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16GNU Make; see the file COPYING. If not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
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 *
258handle_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 ? handle_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
807 /* On MSDOS we do use SHELL from environment, since it isn't a standard
808 environment variable on MSDOS, so whoever sets it, does that on purpose.
809 On OS/2 we do not use SHELL from environment but we have already handled
810 that problem above. */
811#if !defined(__MSDOS__) && !defined(__EMX__)
812 /* Don't let SHELL come from the environment. */
813 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
814 {
815 free (v->value);
816 v->origin = o_file;
817 v->value = xstrdup (default_shell);
818 }
819#endif
820
821 /* Make sure MAKEFILES gets exported if it is set. */
822 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
823 v->export = v_ifset;
824
825 /* Define the magic D and F variables in terms of
826 the automatic variables they are variations of. */
827
828#ifdef VMS
829 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
830 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
831 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
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#else
837 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
838 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
839 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
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#endif
845 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
846 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
847 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
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}
853
854
855int export_all_variables;
856
857/* Create a new environment for FILE's commands.
858 If FILE is nil, this is for the `shell' function.
859 The child's MAKELEVEL variable is incremented. */
860
861char **
862target_environment (struct file *file)
863{
864 struct variable_set_list *set_list;
865 register struct variable_set_list *s;
866 struct hash_table table;
867 struct variable **v_slot;
868 struct variable **v_end;
869 struct variable makelevel_key;
870 char **result_0;
871 char **result;
872
873 if (file == 0)
874 set_list = current_variable_set_list;
875 else
876 set_list = file->variables;
877
878 hash_init (&table, VARIABLE_BUCKETS,
879 variable_hash_1, variable_hash_2, variable_hash_cmp);
880
881 /* Run through all the variable sets in the list,
882 accumulating variables in TABLE. */
883 for (s = set_list; s != 0; s = s->next)
884 {
885 struct variable_set *set = s->set;
886 v_slot = (struct variable **) set->table.ht_vec;
887 v_end = v_slot + set->table.ht_size;
888 for ( ; v_slot < v_end; v_slot++)
889 if (! HASH_VACANT (*v_slot))
890 {
891 struct variable **new_slot;
892 struct variable *v = *v_slot;
893
894 /* If this is a per-target variable and it hasn't been touched
895 already then look up the global version and take its export
896 value. */
897 if (v->per_target && v->export == v_default)
898 {
899 struct variable *gv;
900
901 gv = lookup_variable_in_set (v->name, strlen(v->name),
902 &global_variable_set);
903 if (gv)
904 v->export = gv->export;
905 }
906
907 switch (v->export)
908 {
909 case v_default:
910 if (v->origin == o_default || v->origin == o_automatic)
911 /* Only export default variables by explicit request. */
912 continue;
913
914 /* The variable doesn't have a name that can be exported. */
915 if (! v->exportable)
916 continue;
917
918 if (! export_all_variables
919 && v->origin != o_command
920 && v->origin != o_env && v->origin != o_env_override)
921 continue;
922 break;
923
924 case v_export:
925 break;
926
927 case v_noexport:
928 /* If this is the SHELL variable and it's not exported, then
929 add the value from our original environment. */
930 if (streq (v->name, "SHELL"))
931 {
932 extern struct variable shell_var;
933 v = &shell_var;
934 break;
935 }
936 continue;
937
938 case v_ifset:
939 if (v->origin == o_default)
940 continue;
941 break;
942 }
943
944 new_slot = (struct variable **) hash_find_slot (&table, v);
945 if (HASH_VACANT (*new_slot))
946 hash_insert_at (&table, v, new_slot);
947 }
948 }
949
950 makelevel_key.name = MAKELEVEL_NAME;
951 makelevel_key.length = MAKELEVEL_LENGTH;
952 hash_delete (&table, &makelevel_key);
953
954 result = result_0 = xmalloc ((table.ht_fill + 2) * sizeof (char *));
955
956 v_slot = (struct variable **) table.ht_vec;
957 v_end = v_slot + table.ht_size;
958 for ( ; v_slot < v_end; v_slot++)
959 if (! HASH_VACANT (*v_slot))
960 {
961 struct variable *v = *v_slot;
962
963 /* If V is recursively expanded and didn't come from the environment,
964 expand its value. If it came from the environment, it should
965 go back into the environment unchanged. */
966 if (v->recursive
967 && v->origin != o_env && v->origin != o_env_override)
968 {
969 char *value = recursively_expand_for_file (v, file);
970#ifdef WINDOWS32
971 if (strcmp(v->name, "Path") == 0 ||
972 strcmp(v->name, "PATH") == 0)
973 convert_Path_to_windows32(value, ';');
974#endif
975 *result++ = xstrdup (concat (v->name, "=", value));
976 free (value);
977 }
978 else
979 {
980#ifdef WINDOWS32
981 if (strcmp(v->name, "Path") == 0 ||
982 strcmp(v->name, "PATH") == 0)
983 convert_Path_to_windows32(v->value, ';');
984#endif
985 *result++ = xstrdup (concat (v->name, "=", v->value));
986 }
987 }
988
989 *result = xmalloc (100);
990 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
991 *++result = 0;
992
993 hash_free (&table, 0);
994
995 return result_0;
996}
997
998
999/* Given a variable, a value, and a flavor, define the variable.
1000 See the try_variable_definition() function for details on the parameters. */
1001
1002struct variable *
1003do_variable_definition (const struct floc *flocp, const char *varname,
1004 const char *value, enum variable_origin origin,
1005 enum variable_flavor flavor, int target_var)
1006{
1007 const char *p;
1008 char *alloc_value = NULL;
1009 struct variable *v;
1010 int append = 0;
1011 int conditional = 0;
1012
1013 /* Calculate the variable's new value in VALUE. */
1014
1015 switch (flavor)
1016 {
1017 default:
1018 case f_bogus:
1019 /* Should not be possible. */
1020 abort ();
1021 case f_simple:
1022 /* A simple variable definition "var := value". Expand the value.
1023 We have to allocate memory since otherwise it'll clobber the
1024 variable buffer, and we may still need that if we're looking at a
1025 target-specific variable. */
1026 p = alloc_value = allocated_variable_expand (value);
1027 break;
1028 case f_conditional:
1029 /* A conditional variable definition "var ?= value".
1030 The value is set IFF the variable is not defined yet. */
1031 v = lookup_variable (varname, strlen (varname));
1032 if (v)
1033 return v;
1034
1035 conditional = 1;
1036 flavor = f_recursive;
1037 /* FALLTHROUGH */
1038 case f_recursive:
1039 /* A recursive variable definition "var = value".
1040 The value is used verbatim. */
1041 p = value;
1042 break;
1043 case f_append:
1044 {
1045 /* If we have += but we're in a target variable context, we want to
1046 append only with other variables in the context of this target. */
1047 if (target_var)
1048 {
1049 append = 1;
1050 v = lookup_variable_in_set (varname, strlen (varname),
1051 current_variable_set_list->set);
1052
1053 /* Don't append from the global set if a previous non-appending
1054 target-specific variable definition exists. */
1055 if (v && !v->append)
1056 append = 0;
1057 }
1058 else
1059 v = lookup_variable (varname, strlen (varname));
1060
1061 if (v == 0)
1062 {
1063 /* There was no old value.
1064 This becomes a normal recursive definition. */
1065 p = value;
1066 flavor = f_recursive;
1067 }
1068 else
1069 {
1070 /* Paste the old and new values together in VALUE. */
1071
1072 unsigned int oldlen, vallen;
1073 const char *val;
1074 char *tp;
1075
1076 val = value;
1077 if (v->recursive)
1078 /* The previous definition of the variable was recursive.
1079 The new value is the unexpanded old and new values. */
1080 flavor = f_recursive;
1081 else
1082 /* The previous definition of the variable was simple.
1083 The new value comes from the old value, which was expanded
1084 when it was set; and from the expanded new value. Allocate
1085 memory for the expansion as we may still need the rest of the
1086 buffer if we're looking at a target-specific variable. */
1087 val = alloc_value = allocated_variable_expand (val);
1088
1089 oldlen = strlen (v->value);
1090 vallen = strlen (val);
1091 tp = alloca (oldlen + 1 + vallen + 1);
1092 memcpy (tp, v->value, oldlen);
1093 tp[oldlen] = ' ';
1094 memcpy (&tp[oldlen + 1], val, vallen + 1);
1095 p = tp;
1096 }
1097 }
1098 }
1099
1100#ifdef __MSDOS__
1101 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1102 non-Unix systems don't conform to this default configuration (in
1103 fact, most of them don't even have `/bin'). On the other hand,
1104 $SHELL in the environment, if set, points to the real pathname of
1105 the shell.
1106 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1107 the Makefile override $SHELL from the environment. But first, we
1108 look for the basename of the shell in the directory where SHELL=
1109 points, and along the $PATH; if it is found in any of these places,
1110 we define $SHELL to be the actual pathname of the shell. Thus, if
1111 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1112 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1113 defining SHELL to be "d:/unix/bash.exe". */
1114 if ((origin == o_file || origin == o_override)
1115 && strcmp (varname, "SHELL") == 0)
1116 {
1117 PATH_VAR (shellpath);
1118 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1119
1120 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1121 if (__dosexec_find_on_path (p, NULL, shellpath))
1122 {
1123 char *tp;
1124
1125 for (tp = shellpath; *tp; tp++)
1126 if (*tp == '\\')
1127 *tp = '/';
1128
1129 v = define_variable_loc (varname, strlen (varname),
1130 shellpath, origin, flavor == f_recursive,
1131 flocp);
1132 }
1133 else
1134 {
1135 const char *shellbase, *bslash;
1136 struct variable *pathv = lookup_variable ("PATH", 4);
1137 char *path_string;
1138 char *fake_env[2];
1139 size_t pathlen = 0;
1140
1141 shellbase = strrchr (p, '/');
1142 bslash = strrchr (p, '\\');
1143 if (!shellbase || bslash > shellbase)
1144 shellbase = bslash;
1145 if (!shellbase && p[1] == ':')
1146 shellbase = p + 1;
1147 if (shellbase)
1148 shellbase++;
1149 else
1150 shellbase = p;
1151
1152 /* Search for the basename of the shell (with standard
1153 executable extensions) along the $PATH. */
1154 if (pathv)
1155 pathlen = strlen (pathv->value);
1156 path_string = xmalloc (5 + pathlen + 2 + 1);
1157 /* On MSDOS, current directory is considered as part of $PATH. */
1158 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1159 fake_env[0] = path_string;
1160 fake_env[1] = 0;
1161 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1162 {
1163 char *tp;
1164
1165 for (tp = shellpath; *tp; tp++)
1166 if (*tp == '\\')
1167 *tp = '/';
1168
1169 v = define_variable_loc (varname, strlen (varname),
1170 shellpath, origin,
1171 flavor == f_recursive, flocp);
1172 }
1173 else
1174 v = lookup_variable (varname, strlen (varname));
1175
1176 free (path_string);
1177 }
1178 }
1179 else
1180#endif /* __MSDOS__ */
1181#ifdef WINDOWS32
1182 if ((origin == o_file || origin == o_override || origin == o_command)
1183 && streq (varname, "SHELL"))
1184 {
1185 extern char *default_shell;
1186
1187 /* Call shell locator function. If it returns TRUE, then
1188 set no_default_sh_exe to indicate sh was found and
1189 set new value for SHELL variable. */
1190
1191 if (find_and_set_default_shell (p))
1192 {
1193 v = define_variable_in_set (varname, strlen (varname), default_shell,
1194 origin, flavor == f_recursive,
1195 (target_var
1196 ? current_variable_set_list->set
1197 : NULL),
1198 flocp);
1199 no_default_sh_exe = 0;
1200 }
1201 else
1202 v = lookup_variable (varname, strlen (varname));
1203 }
1204 else
1205#endif
1206
1207 /* If we are defining variables inside an $(eval ...), we might have a
1208 different variable context pushed, not the global context (maybe we're
1209 inside a $(call ...) or something. Since this function is only ever
1210 invoked in places where we want to define globally visible variables,
1211 make sure we define this variable in the global set. */
1212
1213 v = define_variable_in_set (varname, strlen (varname), p,
1214 origin, flavor == f_recursive,
1215 (target_var
1216 ? current_variable_set_list->set : NULL),
1217 flocp);
1218 v->append = append;
1219 v->conditional = conditional;
1220
1221 if (alloc_value)
1222 free (alloc_value);
1223
1224 return v;
1225}
1226
1227
1228/* Try to interpret LINE (a null-terminated string) as a variable definition.
1229
1230 ORIGIN may be o_file, o_override, o_env, o_env_override,
1231 or o_command specifying that the variable definition comes
1232 from a makefile, an override directive, the environment with
1233 or without the -e switch, or the command line.
1234
1235 See the comments for parse_variable_definition().
1236
1237 If LINE was recognized as a variable definition, a pointer to its `struct
1238 variable' is returned. If LINE is not a variable definition, NULL is
1239 returned. */
1240
1241struct variable *
1242parse_variable_definition (struct variable *v, char *line)
1243{
1244 register int c;
1245 register char *p = line;
1246 register char *beg;
1247 register char *end;
1248 enum variable_flavor flavor = f_bogus;
1249 char *name;
1250
1251 while (1)
1252 {
1253 c = *p++;
1254 if (c == '\0' || c == '#')
1255 return 0;
1256 if (c == '=')
1257 {
1258 end = p - 1;
1259 flavor = f_recursive;
1260 break;
1261 }
1262 else if (c == ':')
1263 if (*p == '=')
1264 {
1265 end = p++ - 1;
1266 flavor = f_simple;
1267 break;
1268 }
1269 else
1270 /* A colon other than := is a rule line, not a variable defn. */
1271 return 0;
1272 else if (c == '+' && *p == '=')
1273 {
1274 end = p++ - 1;
1275 flavor = f_append;
1276 break;
1277 }
1278 else if (c == '?' && *p == '=')
1279 {
1280 end = p++ - 1;
1281 flavor = f_conditional;
1282 break;
1283 }
1284 else if (c == '$')
1285 {
1286 /* This might begin a variable expansion reference. Make sure we
1287 don't misrecognize chars inside the reference as =, := or +=. */
1288 char closeparen;
1289 int count;
1290 c = *p++;
1291 if (c == '(')
1292 closeparen = ')';
1293 else if (c == '{')
1294 closeparen = '}';
1295 else
1296 continue; /* Nope. */
1297
1298 /* P now points past the opening paren or brace.
1299 Count parens or braces until it is matched. */
1300 count = 0;
1301 for (; *p != '\0'; ++p)
1302 {
1303 if (*p == c)
1304 ++count;
1305 else if (*p == closeparen && --count < 0)
1306 {
1307 ++p;
1308 break;
1309 }
1310 }
1311 }
1312 }
1313 v->flavor = flavor;
1314
1315 beg = next_token (line);
1316 while (end > beg && isblank ((unsigned char)end[-1]))
1317 --end;
1318 p = next_token (p);
1319 v->value = p;
1320
1321 /* Expand the name, so "$(foo)bar = baz" works. */
1322 name = alloca (end - beg + 1);
1323 memcpy (name, beg, end - beg);
1324 name[end - beg] = '\0';
1325 v->name = allocated_variable_expand (name);
1326
1327 if (v->name[0] == '\0')
1328 fatal (&v->fileinfo, _("empty variable name"));
1329
1330 return v;
1331}
1332
1333
1334/* Try to interpret LINE (a null-terminated string) as a variable definition.
1335
1336 ORIGIN may be o_file, o_override, o_env, o_env_override,
1337 or o_command specifying that the variable definition comes
1338 from a makefile, an override directive, the environment with
1339 or without the -e switch, or the command line.
1340
1341 See the comments for parse_variable_definition().
1342
1343 If LINE was recognized as a variable definition, a pointer to its `struct
1344 variable' is returned. If LINE is not a variable definition, NULL is
1345 returned. */
1346
1347struct variable *
1348try_variable_definition (const struct floc *flocp, char *line,
1349 enum variable_origin origin, int target_var)
1350{
1351 struct variable v;
1352 struct variable *vp;
1353
1354 if (flocp != 0)
1355 v.fileinfo = *flocp;
1356 else
1357 v.fileinfo.filenm = 0;
1358
1359 if (!parse_variable_definition (&v, line))
1360 return 0;
1361
1362 vp = do_variable_definition (flocp, v.name, v.value,
1363 origin, v.flavor, target_var);
1364
1365 free (v.name);
1366
1367 return vp;
1368}
1369
1370
1371/* Print information for variable V, prefixing it with PREFIX. */
1372
1373static void
1374print_variable (const void *item, void *arg)
1375{
1376 const struct variable *v = item;
1377 const char *prefix = arg;
1378 const char *origin;
1379
1380 switch (v->origin)
1381 {
1382 case o_default:
1383 origin = _("default");
1384 break;
1385 case o_env:
1386 origin = _("environment");
1387 break;
1388 case o_file:
1389 origin = _("makefile");
1390 break;
1391 case o_env_override:
1392 origin = _("environment under -e");
1393 break;
1394 case o_command:
1395 origin = _("command line");
1396 break;
1397 case o_override:
1398 origin = _("`override' directive");
1399 break;
1400 case o_automatic:
1401 origin = _("automatic");
1402 break;
1403 case o_invalid:
1404 default:
1405 abort ();
1406 }
1407 fputs ("# ", stdout);
1408 fputs (origin, stdout);
1409 if (v->fileinfo.filenm)
1410 printf (_(" (from `%s', line %lu)"),
1411 v->fileinfo.filenm, v->fileinfo.lineno);
1412 putchar ('\n');
1413 fputs (prefix, stdout);
1414
1415 /* Is this a `define'? */
1416 if (v->recursive && strchr (v->value, '\n') != 0)
1417 printf ("define %s\n%s\nendef\n", v->name, v->value);
1418 else
1419 {
1420 register char *p;
1421
1422 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1423
1424 /* Check if the value is just whitespace. */
1425 p = next_token (v->value);
1426 if (p != v->value && *p == '\0')
1427 /* All whitespace. */
1428 printf ("$(subst ,,%s)", v->value);
1429 else if (v->recursive)
1430 fputs (v->value, stdout);
1431 else
1432 /* Double up dollar signs. */
1433 for (p = v->value; *p != '\0'; ++p)
1434 {
1435 if (*p == '$')
1436 putchar ('$');
1437 putchar (*p);
1438 }
1439 putchar ('\n');
1440 }
1441}
1442
1443
1444/* Print all the variables in SET. PREFIX is printed before
1445 the actual variable definitions (everything else is comments). */
1446
1447void
1448print_variable_set (struct variable_set *set, char *prefix)
1449{
1450 hash_map_arg (&set->table, print_variable, prefix);
1451
1452 fputs (_("# variable set hash-table stats:\n"), stdout);
1453 fputs ("# ", stdout);
1454 hash_print_stats (&set->table, stdout);
1455 putc ('\n', stdout);
1456}
1457
1458/* Print the data base of variables. */
1459
1460void
1461print_variable_data_base (void)
1462{
1463 puts (_("\n# Variables\n"));
1464
1465 print_variable_set (&global_variable_set, "");
1466
1467 puts (_("\n# Pattern-specific Variable Values"));
1468
1469 {
1470 struct pattern_var *p;
1471 int rules = 0;
1472
1473 for (p = pattern_vars; p != 0; p = p->next)
1474 {
1475 ++rules;
1476 printf ("\n%s :\n", p->target);
1477 print_variable (&p->variable, "# ");
1478 }
1479
1480 if (rules == 0)
1481 puts (_("\n# No pattern-specific variable values."));
1482 else
1483 printf (_("\n# %u pattern-specific variable values"), rules);
1484 }
1485}
1486
1487
1488/* Print all the local variables of FILE. */
1489
1490void
1491print_file_variables (const struct file *file)
1492{
1493 if (file->variables != 0)
1494 print_variable_set (file->variables->set, "# ");
1495}
1496
1497#ifdef WINDOWS32
1498void
1499sync_Path_environment (void)
1500{
1501 char *path = allocated_variable_expand ("$(PATH)");
1502 static char *environ_path = NULL;
1503
1504 if (!path)
1505 return;
1506
1507 /*
1508 * If done this before, don't leak memory unnecessarily.
1509 * Free the previous entry before allocating new one.
1510 */
1511 if (environ_path)
1512 free (environ_path);
1513
1514 /*
1515 * Create something WINDOWS32 world can grok
1516 */
1517 convert_Path_to_windows32 (path, ';');
1518 environ_path = xstrdup (concat ("PATH", "=", path));
1519 putenv (environ_path);
1520 free (path);
1521}
1522#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