Debian Codegraph @

src/variable.c

raw symbols SCIP
1 /* Internals of variables for GNU Make.
2 Copyright (C) 1988-2020 Free Software Foundation, Inc.
3 This file is part of GNU Make.
4
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
8 version.
9
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
16
17 #include "makeint.h"
18
19 #include <assert.h>
20
21 #include "filedef.h"
22 #include "dep.h"
23 #include "job.h"
24 #include "commands.h"
25 #include "variable.h"
26 #include "rule.h"
27 #ifdef WINDOWS32
28 #include "pathstuff.h"
29 #endif
30 #include "hash.h"
31 #include "main.h"
32
33 /* Incremented every time we add or remove a global variable. */
34 static unsigned long variable_changenum;
35
36 /* Chain of all pattern-specific variables. */
37
38 static struct pattern_var *pattern_vars;
39
40 /* Pointer to the last struct in the pack of a specific size, from 1 to 255.*/
41
42 static struct pattern_var *last_pattern_vars[256];
43
44 /*!
45 Return a string describing origin.
46 */
47 const char *
48 origin2str(variable_origin_t origin)
49 {
50 switch (origin)
51 {
52 case o_default:
53 return _("default");
54 break;
55 case o_env:
56 return _("environment");
57 break;
58 case o_file:
59 return _("makefile");
60 break;
61 case o_env_override:
62 return _("environment under -e");
63 break;
64 case o_command:
65 return _("command line");
66 break;
67 case o_override:
68 return _("`override' directive");
69 break;
70 case o_automatic:
71 return _("automatic");
72 break;
73 case o_debugger:
74 return _("debugger");
75 break;
76 case o_invalid:
77 default:
78 return _("invalid");
79 }
80 }
81
82 /* Create a new pattern-specific variable struct. The new variable is
83 inserted into the PATTERN_VARS list in the shortest patterns first
84 order to support the shortest stem matching (the variables are
85 matched in the reverse order so the ones with the longest pattern
86 will be considered first). Variables with the same pattern length
87 are inserted in the definition order. */
88
89 struct pattern_var *
90 create_pattern_var (const char *target, const char *suffix)
91 {
92 size_t len = strlen (target);
93 struct pattern_var *p = xcalloc (sizeof (struct pattern_var));
94
95 if (pattern_vars != 0)
96 {
97 if (len < 256 && last_pattern_vars[len] != 0)
98 {
99 p->next = last_pattern_vars[len]->next;
100 last_pattern_vars[len]->next = p;
101 }
102 else
103 {
104 /* Find the position where we can insert this variable. */
105 struct pattern_var **v;
106
107 for (v = &pattern_vars; ; v = &(*v)->next)
108 {
109 /* Insert at the end of the pack so that patterns with the
110 same length appear in the order they were defined .*/
111
112 if (*v == 0 || (*v)->len > len)
113 {
114 p->next = *v;
115 *v = p;
116 break;
117 }
118 }
119 }
120 }
121 else
122 {
123 pattern_vars = p;
124 p->next = 0;
125 }
126
127 p->target = target;
128 p->len = len;
129 p->suffix = suffix + 1;
130
131 if (len < 256)
132 last_pattern_vars[len] = p;
133
134 return p;
135 }
136
137 /* Look up a target in the pattern-specific variable list. */
138
139 static struct pattern_var *
140 lookup_pattern_var (struct pattern_var *start, const char *target)
141 {
142 struct pattern_var *p;
143 size_t targlen = strlen (target);
144
145 for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
146 {
147 const char *stem;
148 size_t stemlen;
149
150 if (p->len > targlen)
151 /* It can't possibly match. */
152 continue;
153
154 /* From the lengths of the filename and the pattern parts,
155 find the stem: the part of the filename that matches the %. */
156 stem = target + (p->suffix - p->target - 1);
157 stemlen = targlen - p->len + 1;
158
159 /* Compare the text in the pattern before the stem, if any. */
160 if (stem > target && !strneq (p->target, target, stem - target))
161 continue;
162
163 /* Compare the text in the pattern after the stem, if any.
164 We could test simply using streq, but this way we compare the
165 first two characters immediately. This saves time in the very
166 common case where the first character matches because it is a
167 period. */
168 if (*p->suffix == stem[stemlen]
169 && (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
170 break;
171 }
172
173 return p;
174 }
175
176 /* Hash table of all global variable definitions. */
177
178 static unsigned long
179 variable_hash_1 (const void *keyv)
180 {
181 struct variable const *key = (struct variable const *) keyv;
182 return_STRING_N_HASH_1 (key->name, key->length);
183 }
184
185 static unsigned long
186 variable_hash_2 (const void *keyv)
187 {
188 struct variable const *key = (struct variable const *) keyv;
189 return_STRING_N_HASH_2 (key->name, key->length);
190 }
191
192 static int
193 variable_hash_cmp (const void *xv, const void *yv)
194 {
195 struct variable const *x = (struct variable const *) xv;
196 struct variable const *y = (struct variable const *) yv;
197 int result = x->length - y->length;
198 if (result)
199 return result;
200 return_STRING_N_COMPARE (x->name, y->name, x->length);
201 }
202
203 #ifndef VARIABLE_BUCKETS
204 #define VARIABLE_BUCKETS 523
205 #endif
206 #ifndef PERFILE_VARIABLE_BUCKETS
207 #define PERFILE_VARIABLE_BUCKETS 23
208 #endif
209 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS
210 #define SMALL_SCOPE_VARIABLE_BUCKETS 13
211 #endif
212
213 static struct variable_set global_variable_set;
214 static struct variable_set_list global_setlist
215 = { 0, &global_variable_set, 0 };
216 struct variable_set_list *current_variable_set_list = &global_setlist;
217
218 /* Implement variables. */
219
220 void
221 init_hash_global_variable_set (void)
222 {
223 hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
224 variable_hash_1, variable_hash_2, variable_hash_cmp);
225 }
226
227 /* Define variable named NAME with value VALUE in SET. VALUE is copied.
228 LENGTH is the length of NAME, which does not need to be null-terminated.
229 ORIGIN specifies the origin of the variable (makefile, command line
230 or environment).
231 If RECURSIVE is nonzero a flag is set in the variable saying
232 that it should be recursively re-expanded. */
233
234 struct variable *
235 define_variable_in_set (const char *name, size_t length,
236 const char *value, enum variable_origin origin,
237 int recursive, struct variable_set *set,
238 const gmk_floc *flocp)
239 {
240 struct variable *v;
241 struct variable **var_slot;
242 struct variable var_key;
243
244 if (set == NULL)
245 set = &global_variable_set;
246
247 var_key.name = (char *) name;
248 var_key.length = (unsigned int) length;
249 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
250 v = *var_slot;
251
252 #ifdef VMS
253 /* VMS does not populate envp[] with DCL symbols and logical names which
254 historically are mapped to environent variables.
255 If the variable is not yet defined, then we need to check if getenv()
256 can find it. Do not do this for origin == o_env to avoid infinte
257 recursion */
258 if (HASH_VACANT (v) && (origin != o_env))
259 {
260 struct variable * vms_variable;
261 char * vname = alloca (length + 1);
262 char * vvalue;
263
264 strncpy (vname, name, length);
265 vvalue = getenv(vname);
266
267 /* Values starting with '$' are probably foreign commands.
268 We want to treat them as Shell aliases and not look them up here */
269 if ((vvalue != NULL) && (vvalue[0] != '$'))
270 {
271 vms_variable = lookup_variable(name, length);
272 /* Refresh the slot */
273 var_slot = (struct variable **) hash_find_slot (&set->table,
274 &var_key);
275 v = *var_slot;
276 }
277 }
278 #endif
279
280 if (env_overrides && origin == o_env)
281 origin = o_env_override;
282
283 if (! HASH_VACANT (v))
284 {
285 if (env_overrides && v->origin == o_env)
286 /* V came from in the environment. Since it was defined
287 before the switches were parsed, it wasn't affected by -e. */
288 v->origin = o_env_override;
289
290 /* A variable of this name is already defined.
291 If the old definition is from a stronger source
292 than this one, don't redefine it. */
293 if ((int) origin >= (int) v->origin)
294 {
295 free (v->value);
296 v->value = xstrdup (value);
297 if (flocp != 0)
298 v->fileinfo = *flocp;
299 else
300 v->fileinfo.filenm = 0;
301 v->origin = origin;
302 v->recursive = recursive;
303 }
304 return v;
305 }
306
307 /* Create a new variable definition and add it to the hash table. */
308
309 v = xcalloc (sizeof (struct variable));
310 v->name = xstrndup (name, length);
311 v->length = (unsigned int) length;
312 hash_insert_at (&set->table, v, var_slot);
313 if (set == &global_variable_set)
314 ++variable_changenum;
315
316 v->value = xstrdup (value);
317 if (flocp != 0)
318 v->fileinfo = *flocp;
319 v->origin = origin;
320 v->recursive = recursive;
321
322 v->export = v_default;
323 v->exportable = 1;
324 if (*name != '_' && (*name < 'A' || *name > 'Z')
325 && (*name < 'a' || *name > 'z'))
326 v->exportable = 0;
327 else
328 {
329 for (++name; *name != '\0'; ++name)
330 if (*name != '_' && (*name < 'a' || *name > 'z')
331 && (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
332 break;
333
334 if (*name != '\0')
335 v->exportable = 0;
336 }
337
338 return v;
339 }
340
341
342 /* Undefine variable named NAME in SET. LENGTH is the length of NAME, which
343 does not need to be null-terminated. ORIGIN specifies the origin of the
344 variable (makefile, command line or environment). */
345
346 static void
347 free_variable_name_and_value (const void *item)
348 {
349 struct variable *v = (struct variable *) item;
350 free (v->name);
351 free (v->value);
352 }
353
354 void
355 free_variable_set (struct variable_set_list *list)
356 {
357 hash_map (&list->set->table, free_variable_name_and_value);
358 hash_free (&list->set->table, 1);
359 free (list->set);
360 free (list);
361 }
362
363 void
364 undefine_variable_in_set (const char *name, size_t length,
365 enum variable_origin origin,
366 struct variable_set *set)
367 {
368 struct variable *v;
369 struct variable **var_slot;
370 struct variable var_key;
371
372 if (set == NULL)
373 set = &global_variable_set;
374
375 var_key.name = (char *) name;
376 var_key.length = (unsigned int) length;
377 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
378
379 if (env_overrides && origin == o_env)
380 origin = o_env_override;
381
382 v = *var_slot;
383 if (! HASH_VACANT (v))
384 {
385 if (env_overrides && v->origin == o_env)
386 /* V came from in the environment. Since it was defined
387 before the switches were parsed, it wasn't affected by -e. */
388 v->origin = o_env_override;
389
390 /* Undefine only if this undefinition is from an equal or stronger
391 source than the variable definition. */
392 if ((int) origin >= (int) v->origin)
393 {
394 hash_delete_at (&set->table, var_slot);
395 free_variable_name_and_value (v);
396 free (v);
397 if (set == &global_variable_set)
398 ++variable_changenum;
399 }
400 }
401 }
402
403 /* If the variable passed in is "special", handle its special nature.
404 Currently there are two such variables, both used for introspection:
405 .VARIABLES expands to a list of all the variables defined in this instance
406 of make.
407 .TARGETS expands to a list of all the targets defined in this
408 instance of make.
409 Returns the variable reference passed in. */
410
411 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
412
413 static struct variable *
414 lookup_special_var (struct variable *var)
415 {
416 static unsigned long last_changenum = 0;
417
418
419 /* This one actually turns out to be very hard, due to the way the parser
420 records targets. The way it works is that target information is collected
421 internally until make knows the target is completely specified. It unitl
422 it sees that some new construct (a new target or variable) is defined that
423 it knows the previous one is done. In short, this means that if you do
424 this:
425
426 all:
427
428 TARGS := $(.TARGETS)
429
430 then $(TARGS) won't contain "all", because it's not until after the
431 variable is created that the previous target is completed.
432
433 Changing this would be a major pain. I think a less complex way to do it
434 would be to pre-define the target files as soon as the first line is
435 parsed, then come back and do the rest of the definition as now. That
436 would allow $(.TARGETS) to be correct without a major change to the way
437 the parser works.
438
439 if (streq (var->name, ".TARGETS"))
440 var->value = build_target_list (var->value);
441 else
442 */
443
444 if (variable_changenum != last_changenum && streq (var->name, ".VARIABLES"))
445 {
446 size_t max = EXPANSION_INCREMENT (strlen (var->value));
447 size_t len;
448 char *p;
449 struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
450 struct variable **end = &vp[global_variable_set.table.ht_size];
451
452 /* Make sure we have at least MAX bytes in the allocated buffer. */
453 var->value = xrealloc (var->value, max);
454
455 /* Walk through the hash of variables, constructing a list of names. */
456 p = var->value;
457 len = 0;
458 for (; vp < end; ++vp)
459 if (!HASH_VACANT (*vp))
460 {
461 struct variable *v = *vp;
462 int l = v->length;
463
464 len += l + 1;
465 if (len > max)
466 {
467 size_t off = p - var->value;
468
469 max += EXPANSION_INCREMENT (l + 1);
470 var->value = xrealloc (var->value, max);
471 p = &var->value[off];
472 }
473
474 memcpy (p, v->name, l);
475 p += l;
476 *(p++) = ' ';
477 }
478 *(p-1) = '\0';
479
480 /* Remember the current variable change number. */
481 last_changenum = variable_changenum;
482 }
483
484 return var;
485 }
486
487
488 /* Lookup a variable whose name is a string starting at NAME
489 and with LENGTH chars. NAME need not be null-terminated.
490 Returns address of the 'struct variable' containing all info
491 on the variable, or nil if no such variable is defined. */
492
493 struct variable *
494 lookup_variable (const char *name, size_t length)
495 {
496 const struct variable_set_list *setlist;
497 struct variable var_key;
498 int is_parent = 0;
499
500 var_key.name = (char *) name;
501 var_key.length = (unsigned int) length;
502
503 for (setlist = current_variable_set_list;
504 setlist != 0; setlist = setlist->next)
505 {
506 const struct variable_set *set = setlist->set;
507 struct variable *v;
508
509 v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
510 if (v && (!is_parent || !v->private_var))
511 return v->special ? lookup_special_var (v) : v;
512
513 is_parent |= setlist->next_is_parent;
514 }
515
516 #ifdef VMS
517 /* VMS does not populate envp[] with DCL symbols and logical names which
518 historically are mapped to enviroment varables and returned by getenv() */
519 {
520 char *vname = alloca (length + 1);
521 char *value;
522 strncpy (vname, name, length);
523 vname[length] = 0;
524 value = getenv (vname);
525 if (value != 0)
526 {
527 char *sptr;
528 int scnt;
529
530 sptr = value;
531 scnt = 0;
532
533 while ((sptr = strchr (sptr, '$')))
534 {
535 scnt++;
536 sptr++;
537 }
538
539 if (scnt > 0)
540 {
541 char *nvalue;
542 char *nptr;
543
544 nvalue = alloca (strlen (value) + scnt + 1);
545 sptr = value;
546 nptr = nvalue;
547
548 while (*sptr)
549 {
550 if (*sptr == '$')
551 {
552 *nptr++ = '$';
553 *nptr++ = '$';
554 }
555 else
556 {
557 *nptr++ = *sptr;
558 }
559 sptr++;
560 }
561
562 *nptr = '\0';
563 return define_variable (vname, length, nvalue, o_env, 1);
564
565 }
566
567 return define_variable (vname, length, value, o_env, 1);
568 }
569 }
570 #endif /* VMS */
571
572 return 0;
573 }
574
575 /* Lookup a variable whose name is a string starting at NAME
576 and with LENGTH chars in set SET. NAME need not be null-terminated.
577 Returns address of the 'struct variable' containing all info
578 on the variable, or nil if no such variable is defined. */
579
580 struct variable *
581 lookup_variable_in_set (const char *name, size_t length,
582 const struct variable_set *set)
583 {
584 struct variable var_key;
585
586 var_key.name = (char *) name;
587 var_key.length = (unsigned int) length;
588
589 return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
590 }
591
592 /* Initialize FILE's variable set list. If FILE already has a variable set
593 list, the topmost variable set is left intact, but the the rest of the
594 chain is replaced with FILE->parent's setlist. If FILE is a double-colon
595 rule, then we will use the "root" double-colon target's variable set as the
596 parent of FILE's variable set.
597
598 If we're READING a makefile, don't do the pattern variable search now,
599 since the pattern variable might not have been defined yet. */
600
601 void
602 initialize_file_variables (struct file *file, int reading)
603 {
604 struct variable_set_list *l = file->variables;
605
606 if (l == 0)
607 {
608 l = (struct variable_set_list *)
609 xmalloc (sizeof (struct variable_set_list));
610 l->set = xmalloc (sizeof (struct variable_set));
611 hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
612 variable_hash_1, variable_hash_2, variable_hash_cmp);
613 file->variables = l;
614 }
615
616 /* If this is a double-colon, then our "parent" is the "root" target for
617 this double-colon rule. Since that rule has the same name, parent,
618 etc. we can just use its variables as the "next" for ours. */
619
620 if (file->double_colon && file->double_colon != file)
621 {
622 initialize_file_variables (file->double_colon, reading);
623 l->next = file->double_colon->variables;
624 l->next_is_parent = 0;
625 return;
626 }
627
628 if (file->parent == 0)
629 l->next = &global_setlist;
630 else
631 {
632 initialize_file_variables (file->parent, reading);
633 l->next = file->parent->variables;
634 }
635 l->next_is_parent = 1;
636
637 /* If we're not reading makefiles and we haven't looked yet, see if
638 we can find pattern variables for this target. */
639
640 if (!reading && !file->pat_searched)
641 {
642 struct pattern_var *p;
643
644 p = lookup_pattern_var (0, file->name);
645 if (p != 0)
646 {
647 struct variable_set_list *global = current_variable_set_list;
648
649 /* We found at least one. Set up a new variable set to accumulate
650 all the pattern variables that match this target. */
651
652 file->pat_variables = create_new_variable_set ();
653 current_variable_set_list = file->pat_variables;
654
655 do
656 {
657 /* We found one, so insert it into the set. */
658
659 struct variable *v;
660
661 if (p->variable.flavor == f_simple)
662 {
663 v = define_variable_loc (
664 p->variable.name, strlen (p->variable.name),
665 p->variable.value, p->variable.origin,
666 0, &p->variable.fileinfo);
667
668 v->flavor = f_simple;
669 }
670 else
671 {
672 v = do_variable_definition (
673 &p->variable.fileinfo, p->variable.name,
674 p->variable.value, p->variable.origin,
675 p->variable.flavor, 1);
676 }
677
678 /* Also mark it as a per-target and copy export status. */
679 v->per_target = p->variable.per_target;
680 v->export = p->variable.export;
681 v->private_var = p->variable.private_var;
682 }
683 while ((p = lookup_pattern_var (p, file->name)) != 0);
684
685 current_variable_set_list = global;
686 }
687 file->pat_searched = 1;
688 }
689
690 /* If we have a pattern variable match, set it up. */
691
692 if (file->pat_variables != 0)
693 {
694 file->pat_variables->next = l->next;
695 file->pat_variables->next_is_parent = l->next_is_parent;
696 l->next = file->pat_variables;
697 l->next_is_parent = 0;
698 }
699 }
700
701 /* Pop the top set off the current variable set list,
702 and free all its storage. */
703
704 struct variable_set_list *
705 create_new_variable_set (void)
706 {
707 struct variable_set_list *setlist;
708 struct variable_set *set;
709
710 set = xmalloc (sizeof (struct variable_set));
711 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
712 variable_hash_1, variable_hash_2, variable_hash_cmp);
713
714 setlist = (struct variable_set_list *)
715 xmalloc (sizeof (struct variable_set_list));
716 setlist->set = set;
717 setlist->next = current_variable_set_list;
718 setlist->next_is_parent = 0;
719
720 return setlist;
721 }
722
723 /* Create a new variable set and push it on the current setlist.
724 If we're pushing a global scope (that is, the current scope is the global
725 scope) then we need to "push" it the other way: file variable sets point
726 directly to the global_setlist so we need to replace that with the new one.
727 */
728
729 struct variable_set_list *
730 push_new_variable_scope (void)
731 {
732 current_variable_set_list = create_new_variable_set ();
733 if (current_variable_set_list->next == &global_setlist)
734 {
735 /* It was the global, so instead of new -> &global we want to replace
736 &global with the new one and have &global -> new, with current still
737 pointing to &global */
738 struct variable_set *set = current_variable_set_list->set;
739 current_variable_set_list->set = global_setlist.set;
740 global_setlist.set = set;
741 current_variable_set_list->next = global_setlist.next;
742 global_setlist.next = current_variable_set_list;
743 current_variable_set_list = &global_setlist;
744 }
745 return (current_variable_set_list);
746 }
747
748 void
749 pop_variable_scope (void)
750 {
751 struct variable_set_list *setlist;
752 struct variable_set *set;
753
754 /* Can't call this if there's no scope to pop! */
755 assert (current_variable_set_list->next != NULL);
756
757 if (current_variable_set_list != &global_setlist)
758 {
759 /* We're not pointing to the global setlist, so pop this one. */
760 setlist = current_variable_set_list;
761 set = setlist->set;
762 current_variable_set_list = setlist->next;
763 }
764 else
765 {
766 /* This set is the one in the global_setlist, but there is another global
767 set beyond that. We want to copy that set to global_setlist, then
768 delete what used to be in global_setlist. */
769 setlist = global_setlist.next;
770 set = global_setlist.set;
771 global_setlist.set = setlist->set;
772 global_setlist.next = setlist->next;
773 global_setlist.next_is_parent = setlist->next_is_parent;
774 }
775
776 /* Free the one we no longer need. */
777 free (setlist);
778 hash_map (&set->table, free_variable_name_and_value);
779 hash_free (&set->table, 1);
780 free (set);
781 }
782
783 /* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
784
785 static void
786 merge_variable_sets (struct variable_set *to_set,
787 struct variable_set *from_set)
788 {
789 struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
790 struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
791
792 int inc = to_set == &global_variable_set ? 1 : 0;
793
794 for ( ; from_var_slot < from_var_end; from_var_slot++)
795 if (! HASH_VACANT (*from_var_slot))
796 {
797 struct variable *from_var = *from_var_slot;
798 struct variable **to_var_slot
799 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
800 if (HASH_VACANT (*to_var_slot))
801 {
802 hash_insert_at (&to_set->table, from_var, to_var_slot);
803 variable_changenum += inc;
804 }
805 else
806 {
807 /* GKM FIXME: delete in from_set->table */
808 free (from_var->value);
809 free (from_var);
810 }
811 }
812 }
813
814 /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
815
816 void
817 merge_variable_set_lists (struct variable_set_list **setlist0,
818 struct variable_set_list *setlist1)
819 {
820 struct variable_set_list *to = *setlist0;
821 struct variable_set_list *last0 = 0;
822
823 /* If there's nothing to merge, stop now. */
824 if (!setlist1 || setlist1 == &global_setlist)
825 return;
826
827 if (to)
828 {
829 /* These loops rely on the fact that all setlists terminate with the
830 global setlist (before NULL). If not, arguably we SHOULD die. */
831
832 /* Make sure that setlist1 is not already a subset of setlist0. */
833 while (to != &global_setlist)
834 {
835 if (to == setlist1)
836 return;
837 to = to->next;
838 }
839
840 to = *setlist0;
841 while (setlist1 != &global_setlist && to != &global_setlist)
842 {
843 struct variable_set_list *from = setlist1;
844 setlist1 = setlist1->next;
845
846 merge_variable_sets (to->set, from->set);
847
848 last0 = to;
849 to = to->next;
850 }
851 }
852
853 if (setlist1 != &global_setlist)
854 {
855 if (last0 == 0)
856 *setlist0 = setlist1;
857 else
858 last0->next = setlist1;
859 }
860 }
861
862 /* Define the automatic variables, and record the addresses
863 of their structures so we can change their values quickly. */
864
865 void
866 define_automatic_variables (void)
867 {
868 struct variable *v;
869 char buf[200];
870
871 sprintf (buf, "%u", makelevel);
872 define_variable_cname (MAKELEVEL_NAME, buf, o_env, 0);
873
874 sprintf (buf, "%d", (int32_t)makeparent_pid);
875 define_variable_cname (MAKEPARENT_PID_NAME, buf, o_env, 0);
876
877 if (makeparent_target) {
878 sprintf (buf, "%s", makeparent_target);
879 define_variable_cname (MAKEPARENT_TARGET_NAME, buf, o_env, 0);
880 }
881
882 sprintf (buf, "%s%s%s",
883 version_string,
884 (remote_description == 0 || remote_description[0] == '\0')
885 ? "" : "-",
886 (remote_description == 0 || remote_description[0] == '\0')
887 ? "" : remote_description);
888 define_variable_cname ("MAKE_VERSION", buf, o_default, 0);
889 define_variable_cname ("MAKE_HOST", make_host, o_default, 0);
890
891 #ifdef __MSDOS__
892 /* Allow to specify a special shell just for Make,
893 and use $COMSPEC as the default $SHELL when appropriate. */
894 {
895 static char shell_str[] = "SHELL";
896 const int shlen = sizeof (shell_str) - 1;
897 struct variable *mshp = lookup_variable ("MAKESHELL", 9);
898 struct variable *comp = lookup_variable ("COMSPEC", 7);
899
900 /* $(MAKESHELL) overrides $(SHELL) even if -e is in effect. */
901 if (mshp)
902 (void) define_variable (shell_str, shlen,
903 mshp->value, o_env_override, 0);
904 else if (comp)
905 {
906 /* $(COMSPEC) shouldn't override $(SHELL). */
907 struct variable *shp = lookup_variable (shell_str, shlen);
908
909 if (!shp)
910 (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
911 }
912 }
913 #elif defined(__EMX__)
914 {
915 static char shell_str[] = "SHELL";
916 const int shlen = sizeof (shell_str) - 1;
917 struct variable *shell = lookup_variable (shell_str, shlen);
918 struct variable *replace = lookup_variable ("MAKESHELL", 9);
919
920 /* if $MAKESHELL is defined in the environment assume o_env_override */
921 if (replace && *replace->value && replace->origin == o_env)
922 replace->origin = o_env_override;
923
924 /* if $MAKESHELL is not defined use $SHELL but only if the variable
925 did not come from the environment */
926 if (!replace || !*replace->value)
927 if (shell && *shell->value && (shell->origin == o_env
928 || shell->origin == o_env_override))
929 {
930 /* overwrite whatever we got from the environment */
931 free (shell->value);
932 shell->value = xstrdup (default_shell);
933 shell->origin = o_default;
934 }
935
936 /* Some people do not like cmd to be used as the default
937 if $SHELL is not defined in the Makefile.
938 With -DNO_CMD_DEFAULT you can turn off this behaviour */
939 # ifndef NO_CMD_DEFAULT
940 /* otherwise use $COMSPEC */
941 if (!replace || !*replace->value)
942 replace = lookup_variable ("COMSPEC", 7);
943
944 /* otherwise use $OS2_SHELL */
945 if (!replace || !*replace->value)
946 replace = lookup_variable ("OS2_SHELL", 9);
947 # else
948 # warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
949 # endif
950
951 if (replace && *replace->value)
952 /* overwrite $SHELL */
953 (void) define_variable (shell_str, shlen, replace->value,
954 replace->origin, 0);
955 else
956 /* provide a definition if there is none */
957 (void) define_variable (shell_str, shlen, default_shell,
958 o_default, 0);
959 }
960
961 #endif
962
963 /* This won't override any definition, but it will provide one if there
964 isn't one there. */
965 v = define_variable_cname ("SHELL", default_shell, o_default, 0);
966 #ifdef __MSDOS__
967 v->export = v_export; /* Export always SHELL. */
968 #endif
969
970 /* On MSDOS we do use SHELL from environment, since it isn't a standard
971 environment variable on MSDOS, so whoever sets it, does that on purpose.
972 On OS/2 we do not use SHELL from environment but we have already handled
973 that problem above. */
974 #if !defined(__MSDOS__) && !defined(__EMX__)
975 /* Don't let SHELL come from the environment. */
976 if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
977 {
978 free (v->value);
979 v->origin = o_file;
980 v->value = xstrdup (default_shell);
981 }
982 #endif
983
984 /* Make sure MAKEFILES gets exported if it is set. */
985 v = define_variable_cname ("MAKEFILES", "", o_default, 0);
986 v->export = v_ifset;
987
988 /* Define the magic D and F variables in terms of
989 the automatic variables they are variations of. */
990
991 #if defined(__MSDOS__) || defined(WINDOWS32)
992 /* For consistency, remove the trailing backslash as well as slash. */
993 define_variable_cname ("@D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $@)))",
994 o_automatic, 1);
995 define_variable_cname ("%D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $%)))",
996 o_automatic, 1);
997 define_variable_cname ("*D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $*)))",
998 o_automatic, 1);
999 define_variable_cname ("<D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $<)))",
1000 o_automatic, 1);
1001 define_variable_cname ("?D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $?)))",
1002 o_automatic, 1);
1003 define_variable_cname ("^D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $^)))",
1004 o_automatic, 1);
1005 define_variable_cname ("+D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $+)))",
1006 o_automatic, 1);
1007 #else /* not __MSDOS__, not WINDOWS32 */
1008 define_variable_cname ("@D", "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
1009 define_variable_cname ("%D", "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
1010 define_variable_cname ("*D", "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
1011 define_variable_cname ("<D", "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
1012 define_variable_cname ("?D", "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
1013 define_variable_cname ("^D", "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
1014 define_variable_cname ("+D", "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
1015 #endif
1016 define_variable_cname ("@F", "$(notdir $@)", o_automatic, 1);
1017 define_variable_cname ("%F", "$(notdir $%)", o_automatic, 1);
1018 define_variable_cname ("*F", "$(notdir $*)", o_automatic, 1);
1019 define_variable_cname ("<F", "$(notdir $<)", o_automatic, 1);
1020 define_variable_cname ("?F", "$(notdir $?)", o_automatic, 1);
1021 define_variable_cname ("^F", "$(notdir $^)", o_automatic, 1);
1022 define_variable_cname ("+F", "$(notdir $+)", o_automatic, 1);
1023 }
1024
1025 int export_all_variables;
1026
1027 /* Create a new environment for FILE's commands.
1028 If FILE is nil, this is for the 'shell' function.
1029 The child's MAKELEVEL variable is incremented. */
1030
1031 char **
1032 target_environment (struct file *file)
1033 {
1034 struct variable_set_list *set_list;
1035 struct variable_set_list *s;
1036 struct hash_table table;
1037 struct variable **v_slot;
1038 struct variable **v_end;
1039 struct variable makelevel_key;
1040 struct variable makeparent_key;
1041 char **result_0;
1042 char **result;
1043
1044 if (file == 0)
1045 set_list = current_variable_set_list;
1046 else
1047 set_list = file->variables;
1048
1049 hash_init (&table, VARIABLE_BUCKETS,
1050 variable_hash_1, variable_hash_2, variable_hash_cmp);
1051
1052 /* Run through all the variable sets in the list,
1053 accumulating variables in TABLE. */
1054 for (s = set_list; s != 0; s = s->next)
1055 {
1056 struct variable_set *set = s->set;
1057 v_slot = (struct variable **) set->table.ht_vec;
1058 v_end = v_slot + set->table.ht_size;
1059 for ( ; v_slot < v_end; v_slot++)
1060 if (! HASH_VACANT (*v_slot))
1061 {
1062 struct variable **new_slot;
1063 struct variable *v = *v_slot;
1064
1065 /* If this is a per-target variable and it hasn't been touched
1066 already then look up the global version and take its export
1067 value. */
1068 if (v->per_target && v->export == v_default)
1069 {
1070 struct variable *gv;
1071
1072 gv = lookup_variable_in_set (v->name, strlen (v->name),
1073 &global_variable_set);
1074 if (gv)
1075 v->export = gv->export;
1076 }
1077
1078 switch (v->export)
1079 {
1080 case v_default:
1081 if (v->origin == o_default || v->origin == o_automatic)
1082 /* Only export default variables by explicit request. */
1083 continue;
1084
1085 /* The variable doesn't have a name that can be exported. */
1086 if (! v->exportable)
1087 continue;
1088
1089 if (! export_all_variables
1090 && v->origin != o_command
1091 && v->origin != o_env && v->origin != o_env_override)
1092 continue;
1093 break;
1094
1095 case v_export:
1096 break;
1097
1098 case v_noexport:
1099 {
1100 /* If this is the SHELL variable and it's not exported,
1101 then add the value from our original environment, if
1102 the original environment defined a value for SHELL. */
1103 if (streq (v->name, "SHELL") && shell_var.value)
1104 {
1105 v = &shell_var;
1106 break;
1107 }
1108 continue;
1109 }
1110
1111 case v_ifset:
1112 if (v->origin == o_default)
1113 continue;
1114 break;
1115 }
1116
1117 new_slot = (struct variable **) hash_find_slot (&table, v);
1118 if (HASH_VACANT (*new_slot))
1119 hash_insert_at (&table, v, new_slot);
1120 }
1121 }
1122
1123 makelevel_key.name = (char *)MAKELEVEL_NAME;
1124 makelevel_key.length = MAKELEVEL_LENGTH;
1125 hash_delete (&table, &makelevel_key);
1126
1127 makeparent_key.name = (char *)MAKEPARENT_PID_NAME;
1128 makeparent_key.length = MAKEPARENT_PID_LENGTH;
1129 hash_delete (&table, &makeparent_key);
1130
1131 makeparent_key.name = (char *)MAKEPARENT_TARGET_NAME;
1132 makeparent_key.length = MAKEPARENT_TARGET_LENGTH;
1133 hash_delete (&table, &makeparent_key);
1134
1135 /* Include 3 extra for MAKELEVEL and MAKEPARENT PID and TARGET and 1 more for the NULL terminator */
1136 result = result_0 = xmalloc ((table.ht_fill + 4) * sizeof (char *));
1137
1138 v_slot = (struct variable **) table.ht_vec;
1139 v_end = v_slot + table.ht_size;
1140 for ( ; v_slot < v_end; v_slot++)
1141 if (! HASH_VACANT (*v_slot))
1142 {
1143 struct variable *v = *v_slot;
1144
1145 /* If V is recursively expanded and didn't come from the environment,
1146 expand its value. If it came from the environment, it should
1147 go back into the environment unchanged. */
1148 if (v->recursive
1149 && v->origin != o_env && v->origin != o_env_override)
1150 {
1151 char *value = recursively_expand_for_file (v, file);
1152 #ifdef WINDOWS32
1153 if (strcmp (v->name, "Path") == 0 ||
1154 strcmp (v->name, "PATH") == 0)
1155 convert_Path_to_windows32 (value, ';');
1156 #endif
1157 *result++ = xstrdup (concat (3, v->name, "=", value));
1158 free (value);
1159 }
1160 else
1161 {
1162 #ifdef WINDOWS32
1163 if (strcmp (v->name, "Path") == 0 ||
1164 strcmp (v->name, "PATH") == 0)
1165 convert_Path_to_windows32 (v->value, ';');
1166 #endif
1167 *result++ = xstrdup (concat (3, v->name, "=", v->value));
1168 }
1169 }
1170
1171 *result = xmalloc (100);
1172 sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
1173
1174 *++result = xmalloc (100);
1175 sprintf (*result, "%s=%d", MAKEPARENT_PID_NAME, getpid());
1176
1177 *++result = xmalloc (MAKEPARENT_TARGET_LENGTH + 2 + strlen(file->name));
1178 sprintf(*result, "%s=%s", MAKEPARENT_TARGET_NAME, file->name);
1179 *++result = 0;
1180 hash_free (&table, 0);
1181
1182 return result_0;
1183 }
1184
1185 static struct variable *
1186 set_special_var (struct variable *var)
1187 {
1188 if (streq (var->name, RECIPEPREFIX_NAME))
1189 {
1190 /* The user is resetting the command introduction prefix. This has to
1191 happen immediately, so that subsequent rules are interpreted
1192 properly. */
1193 cmd_prefix = var->value[0]=='\0' ? RECIPEPREFIX_DEFAULT : var->value[0];
1194 }
1195
1196 return var;
1197 }
1198
1199 /* Given a string, shell-execute it and return a malloc'ed string of the
1200 * result. This removes only ONE newline (if any) at the end, for maximum
1201 * compatibility with the *BSD makes. If it fails, returns NULL. */
1202
1203 static char *
1204 shell_result (const char *p)
1205 {
1206 char *buf;
1207 size_t len;
1208 char *args[2];
1209 char *result;
1210
1211 install_variable_buffer (&buf, &len);
1212
1213 args[0] = (char *) p;
1214 args[1] = NULL;
1215 variable_buffer_output (func_shell_base (variable_buffer, args, 0), "\0", 1);
1216 result = strdup (variable_buffer);
1217
1218 restore_variable_buffer (buf, len);
1219 return result;
1220 }
1221
1222 /* Given a variable, a value, and a flavor, define the variable.
1223 See the try_variable_definition() function for details on the parameters. */
1224
1225 struct variable *
1226 do_variable_definition (const gmk_floc *flocp, const char *varname,
1227 const char *value, enum variable_origin origin,
1228 enum variable_flavor flavor, int target_var)
1229 {
1230 const char *p;
1231 char *alloc_value = NULL;
1232 struct variable *v;
1233 int append = 0;
1234 int conditional = 0;
1235
1236 /* Calculate the variable's new value in VALUE. */
1237
1238 switch (flavor)
1239 {
1240 default:
1241 case f_bogus:
1242 /* Should not be possible. */
1243 abort ();
1244 case f_simple:
1245 /* A simple variable definition "var := value". Expand the value.
1246 We have to allocate memory since otherwise it'll clobber the
1247 variable buffer, and we may still need that if we're looking at a
1248 target-specific variable. */
1249 p = alloc_value = allocated_variable_expand (value);
1250 break;
1251 case f_shell:
1252 {
1253 /* A shell definition "var != value". Expand value, pass it to
1254 the shell, and store the result in recursively-expanded var. */
1255 char *q = allocated_variable_expand (value);
1256 p = alloc_value = shell_result (q);
1257 free (q);
1258 flavor = f_recursive;
1259 break;
1260 }
1261 case f_conditional:
1262 /* A conditional variable definition "var ?= value".
1263 The value is set IFF the variable is not defined yet. */
1264 v = lookup_variable (varname, strlen (varname));
1265 if (v)
1266 goto done;
1267
1268 conditional = 1;
1269 flavor = f_recursive;
1270 /* FALLTHROUGH */
1271 case f_recursive:
1272 /* A recursive variable definition "var = value".
1273 The value is used verbatim. */
1274 p = value;
1275 break;
1276 case f_append:
1277 case f_append_value:
1278 {
1279 /* If we have += but we're in a target variable context, we want to
1280 append only with other variables in the context of this target. */
1281 if (target_var)
1282 {
1283 append = 1;
1284 v = lookup_variable_in_set (varname, strlen (varname),
1285 current_variable_set_list->set);
1286
1287 /* Don't append from the global set if a previous non-appending
1288 target-specific variable definition exists. */
1289 if (v && !v->append)
1290 append = 0;
1291 }
1292 else
1293 v = lookup_variable (varname, strlen (varname));
1294
1295 if (v == 0)
1296 {
1297 /* There was no old value.
1298 This becomes a normal recursive definition. */
1299 p = value;
1300 flavor = f_recursive;
1301 }
1302 else
1303 {
1304 /* Paste the old and new values together in VALUE. */
1305
1306 size_t oldlen, vallen;
1307 const char *val;
1308 char *tp = NULL;
1309
1310 val = value;
1311 if (v->recursive)
1312 /* The previous definition of the variable was recursive.
1313 The new value is the unexpanded old and new values. */
1314 flavor = f_recursive;
1315 else if (flavor != f_append_value)
1316 /* The previous definition of the variable was simple.
1317 The new value comes from the old value, which was expanded
1318 when it was set; and from the expanded new value. Allocate
1319 memory for the expansion as we may still need the rest of the
1320 buffer if we're looking at a target-specific variable. */
1321 val = tp = allocated_variable_expand (val);
1322
1323 /* If the new value is empty, nothing to do. */
1324 vallen = strlen (val);
1325 if (!vallen)
1326 {
1327 alloc_value = tp;
1328 goto done;
1329 }
1330
1331 oldlen = strlen (v->value);
1332 p = alloc_value = xmalloc (oldlen + 1 + vallen + 1);
1333
1334 if (oldlen)
1335 {
1336 memcpy (alloc_value, v->value, oldlen);
1337 alloc_value[oldlen] = ' ';
1338 ++oldlen;
1339 }
1340
1341 memcpy (&alloc_value[oldlen], val, vallen + 1);
1342
1343 free (tp);
1344 }
1345 break;
1346 }
1347 }
1348
1349 #ifdef __MSDOS__
1350 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
1351 non-Unix systems don't conform to this default configuration (in
1352 fact, most of them don't even have '/bin'). On the other hand,
1353 $SHELL in the environment, if set, points to the real pathname of
1354 the shell.
1355 Therefore, we generally won't let lines like "SHELL=/bin/sh" from
1356 the Makefile override $SHELL from the environment. But first, we
1357 look for the basename of the shell in the directory where SHELL=
1358 points, and along the $PATH; if it is found in any of these places,
1359 we define $SHELL to be the actual pathname of the shell. Thus, if
1360 you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
1361 your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
1362 defining SHELL to be "d:/unix/bash.exe". */
1363 if ((origin == o_file || origin == o_override)
1364 && strcmp (varname, "SHELL") == 0)
1365 {
1366 PATH_VAR (shellpath);
1367 extern char * __dosexec_find_on_path (const char *, char *[], char *);
1368
1369 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */
1370 if (__dosexec_find_on_path (p, NULL, shellpath))
1371 {
1372 char *tp;
1373
1374 for (tp = shellpath; *tp; tp++)
1375 if (*tp == '\\')
1376 *tp = '/';
1377
1378 v = define_variable_loc (varname, strlen (varname),
1379 shellpath, origin, flavor == f_recursive,
1380 flocp);
1381 }
1382 else
1383 {
1384 const char *shellbase, *bslash;
1385 struct variable *pathv = lookup_variable ("PATH", 4);
1386 char *path_string;
1387 char *fake_env[2];
1388 size_t pathlen = 0;
1389
1390 shellbase = strrchr (p, '/');
1391 bslash = strrchr (p, '\\');
1392 if (!shellbase || bslash > shellbase)
1393 shellbase = bslash;
1394 if (!shellbase && p[1] == ':')
1395 shellbase = p + 1;
1396 if (shellbase)
1397 shellbase++;
1398 else
1399 shellbase = p;
1400
1401 /* Search for the basename of the shell (with standard
1402 executable extensions) along the $PATH. */
1403 if (pathv)
1404 pathlen = strlen (pathv->value);
1405 path_string = xmalloc (5 + pathlen + 2 + 1);
1406 /* On MSDOS, current directory is considered as part of $PATH. */
1407 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
1408 fake_env[0] = path_string;
1409 fake_env[1] = 0;
1410 if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
1411 {
1412 char *tp;
1413
1414 for (tp = shellpath; *tp; tp++)
1415 if (*tp == '\\')
1416 *tp = '/';
1417
1418 v = define_variable_loc (varname, strlen (varname),
1419 shellpath, origin,
1420 flavor == f_recursive, flocp);
1421 }
1422 else
1423 v = lookup_variable (varname, strlen (varname));
1424
1425 free (path_string);
1426 }
1427 }
1428 else
1429 #endif /* __MSDOS__ */
1430 #ifdef WINDOWS32
1431 if ((origin == o_file || origin == o_override || origin == o_command)
1432 && streq (varname, "SHELL"))
1433 {
1434 extern const char *default_shell;
1435
1436 /* Call shell locator function. If it returns TRUE, then
1437 set no_default_sh_exe to indicate sh was found and
1438 set new value for SHELL variable. */
1439
1440 if (find_and_set_default_shell (p))
1441 {
1442 v = define_variable_in_set (varname, strlen (varname), default_shell,
1443 origin, flavor == f_recursive,
1444 (target_var
1445 ? current_variable_set_list->set
1446 : NULL),
1447 flocp);
1448 no_default_sh_exe = 0;
1449 }
1450 else
1451 {
1452 char *tp = alloc_value;
1453
1454 alloc_value = allocated_variable_expand (p);
1455
1456 if (find_and_set_default_shell (alloc_value))
1457 {
1458 v = define_variable_in_set (varname, strlen (varname), p,
1459 origin, flavor == f_recursive,
1460 (target_var
1461 ? current_variable_set_list->set
1462 : NULL),
1463 flocp);
1464 no_default_sh_exe = 0;
1465 }
1466 else
1467 v = lookup_variable (varname, strlen (varname));
1468
1469 free (tp);
1470 }
1471 }
1472 else
1473 v = NULL;
1474
1475 /* If not $SHELL, or if $SHELL points to a program we didn't find,
1476 just process this variable "as usual". */
1477 if (!v)
1478 #endif
1479
1480 /* If we are defining variables inside an $(eval ...), we might have a
1481 different variable context pushed, not the global context (maybe we're
1482 inside a $(call ...) or something. Since this function is only ever
1483 invoked in places where we want to define globally visible variables,
1484 make sure we define this variable in the global set. */
1485
1486 v = define_variable_in_set (varname, strlen (varname), p,
1487 origin, flavor == f_recursive,
1488 (target_var
1489 ? current_variable_set_list->set : NULL),
1490 flocp);
1491 v->append = append;
1492 v->conditional = conditional;
1493
1494 done:
1495 free (alloc_value);
1496 return v->special ? set_special_var (v) : v;
1497 }
1498
1499 /* Parse P (a null-terminated string) as a variable definition.
1500
1501 If it is not a variable definition, return NULL and the contents of *VAR
1502 are undefined, except NAME is set to the first non-space character or NIL.
1503
1504 If it is a variable definition, return a pointer to the char after the
1505 assignment token and set the following fields (only) of *VAR:
1506 name : name of the variable (ALWAYS SET) (NOT NUL-TERMINATED!)
1507 length : length of the variable name
1508 value : value of the variable (nul-terminated)
1509 flavor : flavor of the variable
1510 Other values in *VAR are unchanged.
1511 */
1512
1513 char *
1514 parse_variable_definition (const char *p, struct variable *var)
1515 {
1516 int wspace = 0;
1517 const char *e = NULL;
1518
1519 NEXT_TOKEN (p);
1520 var->name = (char *)p;
1521 var->length = 0;
1522
1523 while (1)
1524 {
1525 int c = *p++;
1526
1527 /* If we find a comment or EOS, it's not a variable definition. */
1528 if (STOP_SET (c, MAP_COMMENT|MAP_NUL))
1529 return NULL;
1530
1531 if (c == '$')
1532 {
1533 /* This begins a variable expansion reference. Make sure we don't
1534 treat chars inside the reference as assignment tokens. */
1535 char closeparen;
1536 unsigned int count;
1537
1538 c = *p++;
1539 if (c == '(')
1540 closeparen = ')';
1541 else if (c == '{')
1542 closeparen = '}';
1543 else if (c == '\0')
1544 return NULL;
1545 else
1546 /* '$$' or '$X'. Either way, nothing special to do here. */
1547 continue;
1548
1549 /* P now points past the opening paren or brace.
1550 Count parens or braces until it is matched. */
1551 for (count = 1; *p != '\0'; ++p)
1552 {
1553 if (*p == closeparen && --count == 0)
1554 {
1555 ++p;
1556 break;
1557 }
1558 if (*p == c)
1559 ++count;
1560 }
1561 continue;
1562 }
1563
1564 /* If we find whitespace skip it, and remember we found it. */
1565 if (ISBLANK (c))
1566 {
1567 wspace = 1;
1568 e = p - 1;
1569 NEXT_TOKEN (p);
1570 c = *p;
1571 if (c == '\0')
1572 return NULL;
1573 ++p;
1574 }
1575
1576
1577 if (c == '=')
1578 {
1579 var->flavor = f_recursive;
1580 if (! e)
1581 e = p - 1;
1582 break;
1583 }
1584
1585 /* Match assignment variants (:=, +=, ?=, !=) */
1586 if (*p == '=')
1587 {
1588 switch (c)
1589 {
1590 case ':':
1591 var->flavor = f_simple;
1592 break;
1593 case '+':
1594 var->flavor = f_append;
1595 break;
1596 case '?':
1597 var->flavor = f_conditional;
1598 break;
1599 case '!':
1600 var->flavor = f_shell;
1601 break;
1602 default:
1603 /* If we skipped whitespace, non-assignments means no var. */
1604 if (wspace)
1605 return NULL;
1606
1607 /* Might be assignment, or might be $= or #=. Check. */
1608 continue;
1609 }
1610 if (! e)
1611 e = p - 1;
1612 ++p;
1613 break;
1614 }
1615
1616 /* Check for POSIX ::= syntax */
1617 if (c == ':')
1618 {
1619 /* A colon other than :=/::= is not a variable defn. */
1620 if (*p != ':' || p[1] != '=')
1621 return NULL;
1622
1623 /* POSIX allows ::= to be the same as GNU make's := */
1624 var->flavor = f_simple;
1625 if (! e)
1626 e = p - 1;
1627 p += 2;
1628 break;
1629 }
1630
1631 /* If we skipped whitespace, non-assignments means no var. */
1632 if (wspace)
1633 return NULL;
1634 }
1635
1636 var->length = (unsigned int) (e - var->name);
1637 var->value = next_token (p);
1638 return (char *)p;
1639 }
1640
1641 /* Try to interpret LINE (a null-terminated string) as a variable definition.
1642
1643 If LINE was recognized as a variable definition, a pointer to its 'struct
1644 variable' is returned. If LINE is not a variable definition, NULL is
1645 returned. */
1646
1647 struct variable *
1648 assign_variable_definition (struct variable *v, const char *line)
1649 {
1650 char *name;
1651
1652 if (!parse_variable_definition (line, v))
1653 return NULL;
1654
1655 /* Expand the name, so "$(foo)bar = baz" works. */
1656 name = alloca (v->length + 1);
1657 memcpy (name, v->name, v->length);
1658 name[v->length] = '\0';
1659 v->name = allocated_variable_expand (name);
1660
1661 if (v->name[0] == '\0')
1662 O (fatal, &v->fileinfo, _("empty variable name"));
1663
1664 return v;
1665 }
1666
1667 /* Try to interpret LINE (a null-terminated string) as a variable definition.
1668
1669 ORIGIN may be o_file, o_override, o_env, o_env_override,
1670 or o_command specifying that the variable definition comes
1671 from a makefile, an override directive, the environment with
1672 or without the -e switch, or the command line.
1673
1674 See the comments for assign_variable_definition().
1675
1676 If LINE was recognized as a variable definition, a pointer to its 'struct
1677 variable' is returned. If LINE is not a variable definition, NULL is
1678 returned. */
1679
1680 struct variable *
1681 try_variable_definition (const gmk_floc *flocp, const char *line,
1682 enum variable_origin origin, int target_var)
1683 {
1684 struct variable v;
1685 struct variable *vp;
1686
1687 if (flocp != 0)
1688 v.fileinfo = *flocp;
1689 else
1690 v.fileinfo.filenm = 0;
1691
1692 if (!assign_variable_definition (&v, line))
1693 return 0;
1694
1695 vp = do_variable_definition (flocp, v.name, v.value,
1696 origin, v.flavor, target_var);
1697
1698 free (v.name);
1699
1700 return vp;
1701 }
1702
1703 /* Print information for variable V, prefixing it with PREFIX. */
1704
1705 static void
1706 print_variable (const void *item, void *arg)
1707 {
1708 const struct variable *v = item;
1709 const char *prefix = arg;
1710 const char *origin;
1711
1712 switch (v->origin)
1713 {
1714 case o_automatic:
1715 origin = _("automatic");
1716 break;
1717 case o_default:
1718 origin = _("default");
1719 break;
1720 case o_env:
1721 origin = _("environment");
1722 break;
1723 case o_file:
1724 origin = _("makefile");
1725 break;
1726 case o_env_override:
1727 origin = _("environment under -e");
1728 break;
1729 case o_command:
1730 origin = _("command line");
1731 break;
1732 case o_override:
1733 origin = _("'override' directive");
1734 break;
1735 case o_debugger:
1736 origin = _("debugger");
1737 break;
1738 case o_invalid:
1739 default:
1740 abort ();
1741 }
1742 fputs ("# ", stdout);
1743 fputs (origin, stdout);
1744 if (v->private_var)
1745 fputs (" private", stdout);
1746 if (v->fileinfo.filenm)
1747 printf (_(" (from '%s', line %lu)"),
1748 v->fileinfo.filenm, v->fileinfo.lineno + v->fileinfo.offset);
1749 putchar ('\n');
1750 fputs (prefix, stdout);
1751
1752 /* Is this a 'define'? */
1753 if (v->recursive && strchr (v->value, '\n') != 0)
1754 printf ("define %s\n%s\nendef\n", v->name, v->value);
1755 else
1756 {
1757 char *p;
1758
1759 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1760
1761 /* Check if the value is just whitespace. */
1762 p = next_token (v->value);
1763 if (p != v->value && *p == '\0')
1764 /* All whitespace. */
1765 printf ("$(subst ,,%s)", v->value);
1766 else if (v->recursive)
1767 fputs (v->value, stdout);
1768 else
1769 /* Double up dollar signs. */
1770 for (p = v->value; *p != '\0'; ++p)
1771 {
1772 if (*p == '$')
1773 putchar ('$');
1774 putchar (*p);
1775 }
1776 putchar ('\n');
1777 }
1778 }
1779
1780
1781 /** Print information for variable V, prefixing it with PREFIX. */
1782 void
1783 print_variable_info (const void *item, void *arg)
1784 {
1785 const variable_t *v = (variable_t *) item;
1786 const char *prefix = (char *) arg;
1787 const char *origin;
1788
1789 if (o_invalid == v->origin) abort ();
1790 origin = origin2str(v->origin);
1791
1792 if (prefix) {
1793 fputs ("# ", stdout);
1794 fputs (origin, stdout);
1795 if (v->fileinfo.filenm)
1796 printf (_(" (from `%s', line %lu)"),
1797 v->fileinfo.filenm, v->fileinfo.lineno);
1798 putchar ('\n');
1799 fputs (prefix, stdout);
1800 }
1801
1802 /* Is this a `define'? */
1803 if (v->recursive && strchr (v->value, '\n') != 0)
1804 printf ("define %s\n%s\nendef\n", v->name, v->value);
1805 else
1806 {
1807 char *p;
1808
1809 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
1810
1811 /* Check if the value is just whitespace. */
1812 p = next_token (v->value);
1813 if (p != v->value && *p == '\0')
1814 /* All whitespace. */
1815 printf ("$(subst ,,%s)", v->value);
1816 else if (v->recursive)
1817 fputs (v->value, stdout);
1818 else
1819 /* Double up dollar signs. */
1820 for (p = v->value; *p != '\0'; ++p)
1821 {
1822 if (*p == '$')
1823 putchar ('$');
1824 putchar (*p);
1825 }
1826 putchar ('\n');
1827 }
1828 }
1829
1830 static void
1831 print_auto_variable (const void *item, void *arg)
1832 {
1833 const struct variable *v = item;
1834
1835 if (v->origin == o_automatic)
1836 print_variable (item, arg);
1837 }
1838
1839
1840 static void
1841 print_noauto_variable (const void *item, void *arg)
1842 {
1843 const struct variable *v = item;
1844
1845 if (v->origin != o_automatic)
1846 print_variable (item, arg);
1847 }
1848
1849
1850 /* Print all the variables in SET. PREFIX is printed before
1851 the actual variable definitions (everything else is comments). */
1852
1853 void
1854 print_variable_set (struct variable_set *set, const char *prefix, int pauto)
1855 {
1856 hash_map_arg (&set->table, (pauto ? print_auto_variable : print_variable),
1857 (void *)prefix);
1858
1859 fputs (_("# variable set hash-table stats:\n"), stdout);
1860 fputs ("# ", stdout);
1861 hash_print_stats (&set->table, stdout);
1862 putc ('\n', stdout);
1863 }
1864
1865 /* Print the data base of variables. */
1866
1867 void
1868 print_variable_data_base (void)
1869 {
1870 puts (_("\n# Variables\n"));
1871
1872 print_variable_set (&global_variable_set, "", 0);
1873
1874 puts (_("\n# Pattern-specific Variable Values"));
1875
1876 {
1877 struct pattern_var *p;
1878 unsigned int rules = 0;
1879
1880 for (p = pattern_vars; p != 0; p = p->next)
1881 {
1882 ++rules;
1883 printf ("\n%s :\n", p->target);
1884 print_variable (&p->variable, (void *)"# ");
1885 }
1886
1887 if (rules == 0)
1888 puts (_("\n# No pattern-specific variable values."));
1889 else
1890 printf (_("\n# %u pattern-specific variable values"), rules);
1891 }
1892 }
1893
1894
1895 /* Print all the local variables of FILE. */
1896
1897 void
1898 print_file_variables (const file_t *p_target, bool b_hash_stats)
1899 {
1900 if (p_target->variables != 0)
1901 print_variable_set (p_target->variables->set, "# ", b_hash_stats);
1902 }
1903
1904 void
1905 print_target_variables (const struct file *file)
1906 {
1907 if (file->variables != 0)
1908 {
1909 size_t l = strlen (file->name);
1910 char *t = alloca (l + 3);
1911
1912 strcpy (t, file->name);
1913 t[l] = ':';
1914 t[l+1] = ' ';
1915 t[l+2] = '\0';
1916
1917 hash_map_arg (&file->variables->set->table, print_noauto_variable, t);
1918 }
1919 }
1920
1921 #ifdef WINDOWS32
1922 void
1923 sync_Path_environment (void)
1924 {
1925 char *path = allocated_variable_expand ("$(PATH)");
1926 static char *environ_path = NULL;
1927
1928 if (!path)
1929 return;
1930
1931 /* If done this before, free the previous entry before allocating new one. */
1932 free (environ_path);
1933
1934 /* Create something WINDOWS32 world can grok. */
1935 convert_Path_to_windows32 (path, ';');
1936 environ_path = xstrdup (concat (3, "PATH", "=", path));
1937 putenv (environ_path);
1938 free (path);
1939 }
1940 #endif