VirtualBox

source: kBuild/branches/GNU/src/gmake/variable.c@ 54

Last change on this file since 54 was 54, checked in by (none), 21 years ago

This commit was manufactured by cvs2svn to create branch 'GNU'.

  • Property svn:eol-style set to native
File size: 40.7 KB
Line 
1/* Internals of variables for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997,
32002 Free Software Foundation, Inc.
4This file is part of GNU Make.
5
6GNU Make is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Make is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Make; see the file COPYING. If not, write to
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
20
21#include "make.h"
22#include "dep.h"
23#include "filedef.h"
24#include "job.h"
25#include "commands.h"
26#include "variable.h"
27#include "rule.h"
28#ifdef WINDOWS32
29#include "pathstuff.h"
30#endif
31#include "hash.h"
32
33/* Chain of all pattern-specific variables. */
34
35static struct pattern_var *pattern_vars;
36
37/* Pointer to last struct in the chain, so we can add onto the end. */
38
39static struct pattern_var *last_pattern_var;
40
41/* Create a new pattern-specific variable struct. */
42
43struct pattern_var *
44create_pattern_var (char *target, char *suffix)
45{
46 register struct pattern_var *p
47 = (struct pattern_var *) 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, 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 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 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 = (struct variable *) 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 bcopy (v->name, p, 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 register 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 = (struct variable_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 /* We found one, so insert it into the set. */
503 do_variable_definition (&p->variable.fileinfo, p->variable.name,
504 p->variable.value, p->variable.origin,
505 p->variable.flavor, 1);
506 while ((p = lookup_pattern_var (p, file->name)) != 0);
507
508 current_variable_set_list = global;
509 }
510 file->pat_searched = 1;
511 }
512
513 /* If we have a pattern variable match, set it up. */
514
515 if (file->pat_variables != 0)
516 {
517 file->pat_variables->next = l->next;
518 l->next = file->pat_variables;
519 }
520}
521
522
523/* Pop the top set off the current variable set list,
524 and free all its storage. */
525
526static void
527free_variable_name_and_value (const void *item)
528{
529 struct variable *v = (struct variable *) item;
530 free (v->name);
531 free (v->value);
532}
533
534void
535pop_variable_scope (void)
536{
537 struct variable_set_list *setlist = current_variable_set_list;
538 struct variable_set *set = setlist->set;
539
540 current_variable_set_list = setlist->next;
541 free ((char *) setlist);
542
543 hash_map (&set->table, free_variable_name_and_value);
544 hash_free (&set->table, 1);
545
546 free ((char *) set);
547}
548
549struct variable_set_list *
550create_new_variable_set (void)
551{
552 register struct variable_set_list *setlist;
553 register struct variable_set *set;
554
555 set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
556 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
557 variable_hash_1, variable_hash_2, variable_hash_cmp);
558
559 setlist = (struct variable_set_list *)
560 xmalloc (sizeof (struct variable_set_list));
561 setlist->set = set;
562 setlist->next = current_variable_set_list;
563
564 return setlist;
565}
566
567/* Create a new variable set and push it on the current setlist. */
568
569struct variable_set_list *
570push_new_variable_scope (void)
571{
572 return (current_variable_set_list = create_new_variable_set());
573}
574
575
576/* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
577
578static void
579merge_variable_sets (struct variable_set *to_set,
580 struct variable_set *from_set)
581{
582 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
583 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
584
585 for ( ; from_var_slot < from_var_end; from_var_slot++)
586 if (! HASH_VACANT (*from_var_slot))
587 {
588 struct variable *from_var = *from_var_slot;
589 struct variable **to_var_slot
590 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
591 if (HASH_VACANT (*to_var_slot))
592 hash_insert_at (&to_set->table, from_var, to_var_slot);
593 else
594 {
595 /* GKM FIXME: delete in from_set->table */
596 free (from_var->value);
597 free (from_var);
598 }
599 }
600}
601
602/* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
603
604void
605merge_variable_set_lists (struct variable_set_list **setlist0,
606 struct variable_set_list *setlist1)
607{
608 register struct variable_set_list *list0 = *setlist0;
609 struct variable_set_list *last0 = 0;
610
611 while (setlist1 != 0 && list0 != 0)
612 {
613 struct variable_set_list *next = setlist1;
614 setlist1 = setlist1->next;
615
616 merge_variable_sets (list0->set, next->set);
617
618 last0 = list0;
619 list0 = list0->next;
620 }
621
622 if (setlist1 != 0)
623 {
624 if (last0 == 0)
625 *setlist0 = setlist1;
626 else
627 last0->next = setlist1;
628 }
629}
630
631
632/* Define the automatic variables, and record the addresses
633 of their structures so we can change their values quickly. */
634
635void
636define_automatic_variables (void)
637{
638#if defined(WINDOWS32) || defined(__EMX__)
639 extern char* default_shell;
640#else
641 extern char default_shell[];
642#endif
643 register struct variable *v;
644 char buf[200];
645
646 sprintf (buf, "%u", makelevel);
647 (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
648
649 sprintf (buf, "%s%s%s",
650 version_string,
651 (remote_description == 0 || remote_description[0] == '\0')
652 ? "" : "-",
653 (remote_description == 0 || remote_description[0] == '\0')
654 ? "" : remote_description);
655 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
656
657#ifdef __MSDOS__
658 /* Allow to specify a special shell just for Make,
659 and use $COMSPEC as the default $SHELL when appropriate. */
660 {
661 static char shell_str[] = "SHELL";
662 const int shlen = sizeof (shell_str) - 1;
663 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
664 struct variable *comp = lookup_variable ("COMSPEC", 7);
665
666 /* Make $MAKESHELL override $SHELL even if -e is in effect. */
667 if (mshp)
668 (void) define_variable (shell_str, shlen,
669 mshp->value, o_env_override, 0);
670 else if (comp)
671 {
672 /* $COMSPEC shouldn't override $SHELL. */
673 struct variable *shp = lookup_variable (shell_str, shlen);
674
675 if (!shp)
676 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
677 }
678 }
679#elif defined(__EMX__)
680 {
681 static char shell_str[] = "SHELL";
682 const int shlen = sizeof (shell_str) - 1;
683 struct variable *shell = lookup_variable (shell_str, shlen);
684 struct variable *replace = lookup_variable ("MAKESHELL", 9);
685
686 /* if $MAKESHELL is defined in the environment assume o_env_override */
687 if (replace && *replace->value && replace->origin == o_env)
688 replace->origin = o_env_override;
689
690 /* if $MAKESHELL is not defined use $SHELL but only if the variable
691 did not come from the environment */
692 if (!replace || !*replace->value)
693 if (shell && *shell->value && (shell->origin == o_env
694 || shell->origin == o_env_override))
695 {
696 /* overwrite whatever we got from the environment */
697 free(shell->value);
698 shell->value = xstrdup (default_shell);
699 shell->origin = o_default;
700 }
701
702 /* Some people do not like cmd to be used as the default
703 if $SHELL is not defined in the Makefile.
704 With -DNO_CMD_DEFAULT you can turn off this behaviour */
705# ifndef NO_CMD_DEFAULT
706 /* otherwise use $COMSPEC */
707 if (!replace || !*replace->value)
708 replace = lookup_variable ("COMSPEC", 7);
709
710 /* otherwise use $OS2_SHELL */
711 if (!replace || !*replace->value)
712 replace = lookup_variable ("OS2_SHELL", 9);
713# else
714# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
715# endif
716
717 if (replace && *replace->value)
718 /* overwrite $SHELL */
719 (void) define_variable (shell_str, shlen, replace->value,
720 replace->origin, 0);
721 else
722 /* provide a definition if there is none */
723 (void) define_variable (shell_str, shlen, default_shell,
724 o_default, 0);
725 }
726
727#endif
728
729 /* This won't override any definition, but it
730 will provide one if there isn't one there. */
731 v = define_variable ("SHELL", 5, default_shell, o_default, 0);
732 v->export = v_export; /* Always export SHELL. */
733
734 /* On MSDOS we do use SHELL from environment, since
735 it isn't a standard environment variable on MSDOS,
736 so whoever sets it, does that on purpose.
737 On OS/2 we do not use SHELL from environment but
738 we have already handled that problem above. */
739#if !defined(__MSDOS__) && !defined(__EMX__)
740 /* Don't let SHELL come from the environment. */
741 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
742 {
743 free (v->value);
744 v->origin = o_file;
745 v->value = xstrdup (default_shell);
746 }
747#endif
748
749 /* Make sure MAKEFILES gets exported if it is set. */
750 v = define_variable ("MAKEFILES", 9, "", o_default, 0);
751 v->export = v_ifset;
752
753 /* Define the magic D and F variables in terms of
754 the automatic variables they are variations of. */
755
756#ifdef VMS
757 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
758 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
759 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
760 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
761 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
762 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
763 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
764#else
765 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
766 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
767 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
768 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
769 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
770 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
771 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
772#endif
773 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
774 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
775 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
776 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
777 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
778 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
779 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
780}
781
782
783int export_all_variables;
784
785/* Create a new environment for FILE's commands.
786 If FILE is nil, this is for the `shell' function.
787 The child's MAKELEVEL variable is incremented. */
788
789char **
790target_environment (struct file *file)
791{
792 struct variable_set_list *set_list;
793 register struct variable_set_list *s;
794 struct hash_table table;
795 struct variable **v_slot;
796 struct variable **v_end;
797 struct variable makelevel_key;
798 char **result_0;
799 char **result;
800
801 if (file == 0)
802 set_list = current_variable_set_list;
803 else
804 set_list = file->variables;
805
806 hash_init (&table, VARIABLE_BUCKETS,
807 variable_hash_1, variable_hash_2, variable_hash_cmp);
808
809 /* Run through all the variable sets in the list,
810 accumulating variables in TABLE. */
811 for (s = set_list; s != 0; s = s->next)
812 {
813 struct variable_set *set = s->set;
814 v_slot = (struct variable **) set->table.ht_vec;
815 v_end = v_slot + set->table.ht_size;
816 for ( ; v_slot < v_end; v_slot++)
817 if (! HASH_VACANT (*v_slot))
818 {
819 struct variable **new_slot;
820 struct variable *v = *v_slot;
821
822 /* If this is a per-target variable and it hasn't been touched
823 already then look up the global version and take its export
824 value. */
825 if (v->per_target && v->export == v_default)
826 {
827 struct variable *gv;
828
829 gv = lookup_variable_in_set (v->name, strlen(v->name),
830 &global_variable_set);
831 if (gv)
832 v->export = gv->export;
833 }
834
835 switch (v->export)
836 {
837 case v_default:
838 if (v->origin == o_default || v->origin == o_automatic)
839 /* Only export default variables by explicit request. */
840 continue;
841
842 /* The variable doesn't have a name that can be exported. */
843 if (! v->exportable)
844 continue;
845
846 if (! export_all_variables
847 && v->origin != o_command
848 && v->origin != o_env && v->origin != o_env_override)
849 continue;
850 break;
851
852 case v_export:
853 break;
854
855 case v_noexport:
856 continue;
857
858 case v_ifset:
859 if (v->origin == o_default)
860 continue;
861 break;
862 }
863
864 new_slot = (struct variable **) hash_find_slot (&table, v);
865 if (HASH_VACANT (*new_slot))
866 hash_insert_at (&table, v, new_slot);
867 }
868 }
869
870 makelevel_key.name = MAKELEVEL_NAME;
871 makelevel_key.length = MAKELEVEL_LENGTH;
872 hash_delete (&table, &makelevel_key);
873
874 result = result_0 = (char **) xmalloc ((table.ht_fill + 2) * sizeof (char *));
875
876 v_slot = (struct variable **) table.ht_vec;
877 v_end = v_slot + table.ht_size;
878 for ( ; v_slot < v_end; v_slot++)
879 if (! HASH_VACANT (*v_slot))
880 {
881 struct variable *v = *v_slot;
882
883 /* If V is recursively expanded and didn't come from the environment,
884 expand its value. If it came from the environment, it should
885 go back into the environment unchanged. */
886 if (v->recursive
887 && v->origin != o_env && v->origin != o_env_override)
888 {
889 char *value = recursively_expand_for_file (v, file);
890#ifdef WINDOWS32
891 if (strcmp(v->name, "Path") == 0 ||
892 strcmp(v->name, "PATH") == 0)
893 convert_Path_to_windows32(value, ';');
894#endif
895 *result++ = concat (v->name, "=", value);
896 free (value);
897 }
898 else
899 {
900#ifdef WINDOWS32
901 if (strcmp(v->name, "Path") == 0 ||
902 strcmp(v->name, "PATH") == 0)
903 convert_Path_to_windows32(v->value, ';');
904#endif
905 *result++ = concat (v->name, "=", v->value);
906 }
907 }
908
909 *result = (char *) xmalloc (100);
910 (void) sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
911 *++result = 0;
912
913 hash_free (&table, 0);
914
915 return result_0;
916}
917
918
919/* Given a variable, a value, and a flavor, define the variable.
920 See the try_variable_definition() function for details on the parameters. */
921
922struct variable *
923do_variable_definition (const struct floc *flocp, const char *varname,
924 char *value, enum variable_origin origin,
925 enum variable_flavor flavor, int target_var)
926{
927 char *p, *alloc_value = NULL;
928 struct variable *v;
929 int append = 0;
930 int conditional = 0;
931
932 /* Calculate the variable's new value in VALUE. */
933
934 switch (flavor)
935 {
936 default:
937 case f_bogus:
938 /* Should not be possible. */
939 abort ();
940 case f_simple:
941 /* A simple variable definition "var := value". Expand the value.
942 We have to allocate memory since otherwise it'll clobber the
943 variable buffer, and we may still need that if we're looking at a
944 target-specific variable. */
945 p = alloc_value = allocated_variable_expand (value);
946 break;
947 case f_conditional:
948 /* A conditional variable definition "var ?= value".
949 The value is set IFF the variable is not defined yet. */
950 v = lookup_variable (varname, strlen (varname));
951 if (v)
952 return v;
953
954 conditional = 1;
955 flavor = f_recursive;
956 /* FALLTHROUGH */
957 case f_recursive:
958 /* A recursive variable definition "var = value".
959 The value is used verbatim. */
960 p = value;
961 break;
962 case f_append:
963 {
964 /* If we have += but we're in a target variable context, we want to
965 append only with other variables in the context of this target. */
966 if (target_var)
967 {
968 append = 1;
969 v = lookup_variable_in_set (varname, strlen (varname),
970 current_variable_set_list->set);
971 }
972 else
973 v = lookup_variable (varname, strlen (varname));
974
975 if (v == 0)
976 {
977 /* There was no old value.
978 This becomes a normal recursive definition. */
979 p = value;
980 flavor = f_recursive;
981 }
982 else
983 {
984 /* Paste the old and new values together in VALUE. */
985
986 unsigned int oldlen, vallen;
987 char *val;
988
989 val = value;
990 if (v->recursive)
991 /* The previous definition of the variable was recursive.
992 The new value is the unexpanded old and new values. */
993 flavor = f_recursive;
994 else
995 /* The previous definition of the variable was simple.
996 The new value comes from the old value, which was expanded
997 when it was set; and from the expanded new value. Allocate
998 memory for the expansion as we may still need the rest of the
999 buffer if we're looking at a target-specific variable. */
1000 val = alloc_value = allocated_variable_expand (val);
1001
1002 oldlen = strlen (v->value);
1003 vallen = strlen (val);
1004 p = (char *) alloca (oldlen + 1 + vallen + 1);
1005 bcopy (v->value, p, oldlen);
1006 p[oldlen] = ' ';
1007 bcopy (val, &p[oldlen + 1], vallen + 1);
1008 }
1009 }
1010 }
1011
1012#ifdef __MSDOS__
1013 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1014 non-Unix systems don't conform to this default configuration (in
1015 fact, most of them don't even have `/bin'). On the other hand,
1016 $SHELL in the environment, if set, points to the real pathname of
1017 the shell.
1018 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1019 the Makefile override $SHELL from the environment. But first, we
1020 look for the basename of the shell in the directory where SHELL=
1021 points, and along the $PATH; if it is found in any of these places,
1022 we define $SHELL to be the actual pathname of the shell. Thus, if
1023 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1024 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1025 defining SHELL to be "d:/unix/bash.exe". */
1026 if ((origin == o_file || origin == o_override)
1027 && strcmp (varname, "SHELL") == 0)
1028 {
1029 char shellpath[PATH_MAX];
1030 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1031
1032 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1033 if (__dosexec_find_on_path (p, (char **)0, shellpath))
1034 {
1035 char *p;
1036
1037 for (p = shellpath; *p; p++)
1038 {
1039 if (*p == '\\')
1040 *p = '/';
1041 }
1042 v = define_variable_loc (varname, strlen (varname),
1043 shellpath, origin, flavor == f_recursive,
1044 flocp);
1045 }
1046 else
1047 {
1048 char *shellbase, *bslash;
1049 struct variable *pathv = lookup_variable ("PATH", 4);
1050 char *path_string;
1051 char *fake_env[2];
1052 size_t pathlen = 0;
1053
1054 shellbase = strrchr (p, '/');
1055 bslash = strrchr (p, '\\');
1056 if (!shellbase || bslash > shellbase)
1057 shellbase = bslash;
1058 if (!shellbase && p[1] == ':')
1059 shellbase = p + 1;
1060 if (shellbase)
1061 shellbase++;
1062 else
1063 shellbase = p;
1064
1065 /* Search for the basename of the shell (with standard
1066 executable extensions) along the $PATH. */
1067 if (pathv)
1068 pathlen = strlen (pathv->value);
1069 path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
1070 /* On MSDOS, current directory is considered as part of $PATH. */
1071 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1072 fake_env[0] = path_string;
1073 fake_env[1] = (char *)0;
1074 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1075 {
1076 char *p;
1077
1078 for (p = shellpath; *p; p++)
1079 {
1080 if (*p == '\\')
1081 *p = '/';
1082 }
1083 v = define_variable_loc (varname, strlen (varname),
1084 shellpath, origin,
1085 flavor == f_recursive, flocp);
1086 }
1087 else
1088 v = lookup_variable (varname, strlen (varname));
1089
1090 free (path_string);
1091 }
1092 }
1093 else
1094#endif /* __MSDOS__ */
1095#ifdef WINDOWS32
1096 if ((origin == o_file || origin == o_override) && streq (varname, "SHELL"))
1097 {
1098 extern char *default_shell;
1099
1100 /* Call shell locator function. If it returns TRUE, then
1101 set no_default_sh_exe to indicate sh was found and
1102 set new value for SHELL variable. */
1103
1104 if (find_and_set_default_shell (p))
1105 {
1106 v = define_variable_in_set (varname, strlen (varname), default_shell,
1107 origin, flavor == f_recursive,
1108 (target_var
1109 ? current_variable_set_list->set
1110 : NULL),
1111 flocp);
1112 no_default_sh_exe = 0;
1113 }
1114 else
1115 v = lookup_variable (varname, strlen (varname));
1116 }
1117 else
1118#endif
1119
1120 /* If we are defining variables inside an $(eval ...), we might have a
1121 different variable context pushed, not the global context (maybe we're
1122 inside a $(call ...) or something. Since this function is only ever
1123 invoked in places where we want to define globally visible variables,
1124 make sure we define this variable in the global set. */
1125
1126 v = define_variable_in_set (varname, strlen (varname), p,
1127 origin, flavor == f_recursive,
1128 (target_var
1129 ? current_variable_set_list->set : NULL),
1130 flocp);
1131 v->append = append;
1132 v->conditional = conditional;
1133
1134 if (alloc_value)
1135 free (alloc_value);
1136
1137 return v;
1138}
1139
1140
1141/* Try to interpret LINE (a null-terminated string) as a variable definition.
1142
1143 ORIGIN may be o_file, o_override, o_env, o_env_override,
1144 or o_command specifying that the variable definition comes
1145 from a makefile, an override directive, the environment with
1146 or without the -e switch, or the command line.
1147
1148 See the comments for parse_variable_definition().
1149
1150 If LINE was recognized as a variable definition, a pointer to its `struct
1151 variable' is returned. If LINE is not a variable definition, NULL is
1152 returned. */
1153
1154struct variable *
1155parse_variable_definition (struct variable *v, char *line)
1156{
1157 register int c;
1158 register char *p = line;
1159 register char *beg;
1160 register char *end;
1161 enum variable_flavor flavor = f_bogus;
1162 char *name;
1163
1164 while (1)
1165 {
1166 c = *p++;
1167 if (c == '\0' || c == '#')
1168 return 0;
1169 if (c == '=')
1170 {
1171 end = p - 1;
1172 flavor = f_recursive;
1173 break;
1174 }
1175 else if (c == ':')
1176 if (*p == '=')
1177 {
1178 end = p++ - 1;
1179 flavor = f_simple;
1180 break;
1181 }
1182 else
1183 /* A colon other than := is a rule line, not a variable defn. */
1184 return 0;
1185 else if (c == '+' && *p == '=')
1186 {
1187 end = p++ - 1;
1188 flavor = f_append;
1189 break;
1190 }
1191 else if (c == '?' && *p == '=')
1192 {
1193 end = p++ - 1;
1194 flavor = f_conditional;
1195 break;
1196 }
1197 else if (c == '$')
1198 {
1199 /* This might begin a variable expansion reference. Make sure we
1200 don't misrecognize chars inside the reference as =, := or +=. */
1201 char closeparen;
1202 int count;
1203 c = *p++;
1204 if (c == '(')
1205 closeparen = ')';
1206 else if (c == '{')
1207 closeparen = '}';
1208 else
1209 continue; /* Nope. */
1210
1211 /* P now points past the opening paren or brace.
1212 Count parens or braces until it is matched. */
1213 count = 0;
1214 for (; *p != '\0'; ++p)
1215 {
1216 if (*p == c)
1217 ++count;
1218 else if (*p == closeparen && --count < 0)
1219 {
1220 ++p;
1221 break;
1222 }
1223 }
1224 }
1225 }
1226 v->flavor = flavor;
1227
1228 beg = next_token (line);
1229 while (end > beg && isblank ((unsigned char)end[-1]))
1230 --end;
1231 p = next_token (p);
1232 v->value = p;
1233
1234 /* Expand the name, so "$(foo)bar = baz" works. */
1235 name = (char *) alloca (end - beg + 1);
1236 bcopy (beg, name, end - beg);
1237 name[end - beg] = '\0';
1238 v->name = allocated_variable_expand (name);
1239
1240 if (v->name[0] == '\0')
1241 fatal (&v->fileinfo, _("empty variable name"));
1242
1243 return v;
1244}
1245
1246
1247/* Try to interpret LINE (a null-terminated string) as a variable definition.
1248
1249 ORIGIN may be o_file, o_override, o_env, o_env_override,
1250 or o_command specifying that the variable definition comes
1251 from a makefile, an override directive, the environment with
1252 or without the -e switch, or the command line.
1253
1254 See the comments for parse_variable_definition().
1255
1256 If LINE was recognized as a variable definition, a pointer to its `struct
1257 variable' is returned. If LINE is not a variable definition, NULL is
1258 returned. */
1259
1260struct variable *
1261try_variable_definition (const struct floc *flocp, char *line,
1262 enum variable_origin origin, int target_var)
1263{
1264 struct variable v;
1265 struct variable *vp;
1266
1267 if (flocp != 0)
1268 v.fileinfo = *flocp;
1269 else
1270 v.fileinfo.filenm = 0;
1271
1272 if (!parse_variable_definition (&v, line))
1273 return 0;
1274
1275 vp = do_variable_definition (flocp, v.name, v.value,
1276 origin, v.flavor, target_var);
1277
1278 free (v.name);
1279
1280 return vp;
1281}
1282
1283
1284/* Print information for variable V, prefixing it with PREFIX. */
1285
1286static void
1287print_variable (const void *item, void *arg)
1288{
1289 const struct variable *v = (struct variable *) item;
1290 const char *prefix = (char *) arg;
1291 const char *origin;
1292
1293 switch (v->origin)
1294 {
1295 case o_default:
1296 origin = _("default");
1297 break;
1298 case o_env:
1299 origin = _("environment");
1300 break;
1301 case o_file:
1302 origin = _("makefile");
1303 break;
1304 case o_env_override:
1305 origin = _("environment under -e");
1306 break;
1307 case o_command:
1308 origin = _("command line");
1309 break;
1310 case o_override:
1311 origin = _("`override' directive");
1312 break;
1313 case o_automatic:
1314 origin = _("automatic");
1315 break;
1316 case o_invalid:
1317 default:
1318 abort ();
1319 }
1320 fputs ("# ", stdout);
1321 fputs (origin, stdout);
1322 if (v->fileinfo.filenm)
1323 printf (_(" (from `%s', line %lu)"),
1324 v->fileinfo.filenm, v->fileinfo.lineno);
1325 putchar ('\n');
1326 fputs (prefix, stdout);
1327
1328 /* Is this a `define'? */
1329 if (v->recursive && strchr (v->value, '\n') != 0)
1330 printf ("define %s\n%s\nendef\n", v->name, v->value);
1331 else
1332 {
1333 register char *p;
1334
1335 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1336
1337 /* Check if the value is just whitespace. */
1338 p = next_token (v->value);
1339 if (p != v->value && *p == '\0')
1340 /* All whitespace. */
1341 printf ("$(subst ,,%s)", v->value);
1342 else if (v->recursive)
1343 fputs (v->value, stdout);
1344 else
1345 /* Double up dollar signs. */
1346 for (p = v->value; *p != '\0'; ++p)
1347 {
1348 if (*p == '$')
1349 putchar ('$');
1350 putchar (*p);
1351 }
1352 putchar ('\n');
1353 }
1354}
1355
1356
1357/* Print all the variables in SET. PREFIX is printed before
1358 the actual variable definitions (everything else is comments). */
1359
1360void
1361print_variable_set (struct variable_set *set, char *prefix)
1362{
1363 hash_map_arg (&set->table, print_variable, prefix);
1364
1365 fputs (_("# variable set hash-table stats:\n"), stdout);
1366 fputs ("# ", stdout);
1367 hash_print_stats (&set->table, stdout);
1368 putc ('\n', stdout);
1369}
1370
1371/* Print the data base of variables. */
1372
1373void
1374print_variable_data_base (void)
1375{
1376 puts (_("\n# Variables\n"));
1377
1378 print_variable_set (&global_variable_set, "");
1379
1380 puts (_("\n# Pattern-specific Variable Values"));
1381
1382 {
1383 struct pattern_var *p;
1384 int rules = 0;
1385
1386 for (p = pattern_vars; p != 0; p = p->next)
1387 {
1388 ++rules;
1389 printf ("\n%s :\n", p->target);
1390 print_variable (&p->variable, "# ");
1391 }
1392
1393 if (rules == 0)
1394 puts (_("\n# No pattern-specific variable values."));
1395 else
1396 printf (_("\n# %u pattern-specific variable values"), rules);
1397 }
1398}
1399
1400
1401/* Print all the local variables of FILE. */
1402
1403void
1404print_file_variables (struct file *file)
1405{
1406 if (file->variables != 0)
1407 print_variable_set (file->variables->set, "# ");
1408}
1409
1410#ifdef WINDOWS32
1411void
1412sync_Path_environment (void)
1413{
1414 char *path = allocated_variable_expand ("$(Path)");
1415 static char *environ_path = NULL;
1416
1417 if (!path)
1418 return;
1419
1420 /*
1421 * If done this before, don't leak memory unnecessarily.
1422 * Free the previous entry before allocating new one.
1423 */
1424 if (environ_path)
1425 free (environ_path);
1426
1427 /*
1428 * Create something WINDOWS32 world can grok
1429 */
1430 convert_Path_to_windows32 (path, ';');
1431 environ_path = concat ("Path", "=", path);
1432 putenv (environ_path);
1433 free (path);
1434}
1435#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