-
Notifications
You must be signed in to change notification settings - Fork 0
/
variable.c
1822 lines (1532 loc) · 54.8 KB
/
variable.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Internals of variables for GNU Make.
Copyright (C) 1988-2016 Free Software Foundation, Inc.
This file is part of GNU Make.
GNU Make is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later
version.
GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. */
#include "makeint.h"
#include <assert.h>
#include "filedef.h"
#include "dep.h"
#include "job.h"
#include "commands.h"
#include "variable.h"
#include "rule.h"
#ifdef WINDOWS32
#include "pathstuff.h"
#endif
#include "hash.h"
/* Incremented every time we add or remove a global variable. */
static unsigned long variable_changenum;
/* Chain of all pattern-specific variables. */
static struct pattern_var *pattern_vars;
/* Pointer to the last struct in the pack of a specific size, from 1 to 255.*/
static struct pattern_var *last_pattern_vars[256];
/* Create a new pattern-specific variable struct. The new variable is
inserted into the PATTERN_VARS list in the shortest patterns first
order to support the shortest stem matching (the variables are
matched in the reverse order so the ones with the longest pattern
will be considered first). Variables with the same pattern length
are inserted in the definition order. */
struct pattern_var *
create_pattern_var (const char *target, const char *suffix)
{
register unsigned int len = strlen (target);
register struct pattern_var *p = xmalloc (sizeof (struct pattern_var));
if (pattern_vars != 0)
{
if (len < 256 && last_pattern_vars[len] != 0)
{
p->next = last_pattern_vars[len]->next;
last_pattern_vars[len]->next = p;
}
else
{
/* Find the position where we can insert this variable. */
register struct pattern_var **v;
for (v = &pattern_vars; ; v = &(*v)->next)
{
/* Insert at the end of the pack so that patterns with the
same length appear in the order they were defined .*/
if (*v == 0 || (*v)->len > len)
{
p->next = *v;
*v = p;
break;
}
}
}
}
else
{
pattern_vars = p;
p->next = 0;
}
p->target = target;
p->len = len;
p->suffix = suffix + 1;
if (len < 256)
last_pattern_vars[len] = p;
return p;
}
/* Look up a target in the pattern-specific variable list. */
static struct pattern_var *
lookup_pattern_var (struct pattern_var *start, const char *target)
{
struct pattern_var *p;
unsigned int targlen = strlen (target);
for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
{
const char *stem;
unsigned int stemlen;
if (p->len > targlen)
/* It can't possibly match. */
continue;
/* From the lengths of the filename and the pattern parts,
find the stem: the part of the filename that matches the %. */
stem = target + (p->suffix - p->target - 1);
stemlen = targlen - p->len + 1;
/* Compare the text in the pattern before the stem, if any. */
if (stem > target && !strneq (p->target, target, stem - target))
continue;
/* Compare the text in the pattern after the stem, if any.
We could test simply using streq, but this way we compare the
first two characters immediately. This saves time in the very
common case where the first character matches because it is a
period. */
if (*p->suffix == stem[stemlen]
&& (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
break;
}
return p;
}
/* Hash table of all global variable definitions. */
static unsigned long
variable_hash_1 (const void *keyv)
{
struct variable const *key = (struct variable const *) keyv;
return_STRING_N_HASH_1 (key->name, key->length);
}
static unsigned long
variable_hash_2 (const void *keyv)
{
struct variable const *key = (struct variable const *) keyv;
return_STRING_N_HASH_2 (key->name, key->length);
}
static int
variable_hash_cmp (const void *xv, const void *yv)
{
struct variable const *x = (struct variable const *) xv;
struct variable const *y = (struct variable const *) yv;
int result = x->length - y->length;
if (result)
return result;
return_STRING_N_COMPARE (x->name, y->name, x->length);
}
#ifndef VARIABLE_BUCKETS
#define VARIABLE_BUCKETS 523
#endif
#ifndef PERFILE_VARIABLE_BUCKETS
#define PERFILE_VARIABLE_BUCKETS 23
#endif
#ifndef SMALL_SCOPE_VARIABLE_BUCKETS
#define SMALL_SCOPE_VARIABLE_BUCKETS 13
#endif
static struct variable_set global_variable_set;
static struct variable_set_list global_setlist
= { 0, &global_variable_set, 0 };
struct variable_set_list *current_variable_set_list = &global_setlist;
/* Implement variables. */
void
init_hash_global_variable_set (void)
{
hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
variable_hash_1, variable_hash_2, variable_hash_cmp);
}
/* Define variable named NAME with value VALUE in SET. VALUE is copied.
LENGTH is the length of NAME, which does not need to be null-terminated.
ORIGIN specifies the origin of the variable (makefile, command line
or environment).
If RECURSIVE is nonzero a flag is set in the variable saying
that it should be recursively re-expanded. */
struct variable *
define_variable_in_set (const char *name, unsigned int length,
const char *value, enum variable_origin origin,
int recursive, struct variable_set *set,
const floc *flocp)
{
struct variable *v;
struct variable **var_slot;
struct variable var_key;
if (set == NULL)
set = &global_variable_set;
var_key.name = (char *) name;
var_key.length = length;
var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
v = *var_slot;
#ifdef VMS
/* VMS does not populate envp[] with DCL symbols and logical names which
historically are mapped to environent variables.
If the variable is not yet defined, then we need to check if getenv()
can find it. Do not do this for origin == o_env to avoid infinte
recursion */
if (HASH_VACANT (v) && (origin != o_env))
{
struct variable * vms_variable;
char * vname = alloca (length + 1);
char * vvalue;
strncpy (vname, name, length);
vvalue = getenv(vname);
/* Values starting with '$' are probably foreign commands.
We want to treat them as Shell aliases and not look them up here */
if ((vvalue != NULL) && (vvalue[0] != '$'))
{
vms_variable = lookup_variable(name, length);
/* Refresh the slot */
var_slot = (struct variable **) hash_find_slot (&set->table,
&var_key);
v = *var_slot;
}
}
#endif
if (env_overrides && origin == o_env)
origin = o_env_override;
if (! HASH_VACANT (v))
{
if (env_overrides && v->origin == o_env)
/* V came from in the environment. Since it was defined
before the switches were parsed, it wasn't affected by -e. */
v->origin = o_env_override;
/* A variable of this name is already defined.
If the old definition is from a stronger source
than this one, don't redefine it. */
if ((int) origin >= (int) v->origin)
{
free (v->value);
v->value = xstrdup (value);
if (flocp != 0)
v->fileinfo = *flocp;
else
v->fileinfo.filenm = 0;
v->origin = origin;
v->recursive = recursive;
}
return v;
}
/* Create a new variable definition and add it to the hash table. */
v = xmalloc (sizeof (struct variable));
v->name = xstrndup (name, length);
v->length = length;
hash_insert_at (&set->table, v, var_slot);
if (set == &global_variable_set)
++variable_changenum;
v->value = xstrdup (value);
if (flocp != 0)
v->fileinfo = *flocp;
else
v->fileinfo.filenm = 0;
v->origin = origin;
v->recursive = recursive;
v->special = 0;
v->expanding = 0;
v->exp_count = 0;
v->per_target = 0;
v->append = 0;
v->private_var = 0;
v->export = v_default;
v->exportable = 1;
if (*name != '_' && (*name < 'A' || *name > 'Z')
&& (*name < 'a' || *name > 'z'))
v->exportable = 0;
else
{
for (++name; *name != '\0'; ++name)
if (*name != '_' && (*name < 'a' || *name > 'z')
&& (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
break;
if (*name != '\0')
v->exportable = 0;
}
return v;
}
/* Undefine variable named NAME in SET. LENGTH is the length of NAME, which
does not need to be null-terminated. ORIGIN specifies the origin of the
variable (makefile, command line or environment). */
static void
free_variable_name_and_value (const void *item)
{
struct variable *v = (struct variable *) item;
free (v->name);
free (v->value);
}
void
free_variable_set (struct variable_set_list *list)
{
hash_map (&list->set->table, free_variable_name_and_value);
hash_free (&list->set->table, 1);
free (list->set);
free (list);
}
void
undefine_variable_in_set (const char *name, unsigned int length,
enum variable_origin origin,
struct variable_set *set)
{
struct variable *v;
struct variable **var_slot;
struct variable var_key;
if (set == NULL)
set = &global_variable_set;
var_key.name = (char *) name;
var_key.length = length;
var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
if (env_overrides && origin == o_env)
origin = o_env_override;
v = *var_slot;
if (! HASH_VACANT (v))
{
if (env_overrides && v->origin == o_env)
/* V came from in the environment. Since it was defined
before the switches were parsed, it wasn't affected by -e. */
v->origin = o_env_override;
/* Undefine only if this undefinition is from an equal or stronger
source than the variable definition. */
if ((int) origin >= (int) v->origin)
{
hash_delete_at (&set->table, var_slot);
free_variable_name_and_value (v);
free (v);
if (set == &global_variable_set)
++variable_changenum;
}
}
}
/* If the variable passed in is "special", handle its special nature.
Currently there are two such variables, both used for introspection:
.VARIABLES expands to a list of all the variables defined in this instance
of make.
.TARGETS expands to a list of all the targets defined in this
instance of make.
Returns the variable reference passed in. */
#define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
static struct variable *
lookup_special_var (struct variable *var)
{
static unsigned long last_changenum = 0;
/* This one actually turns out to be very hard, due to the way the parser
records targets. The way it works is that target information is collected
internally until make knows the target is completely specified. It unitl
it sees that some new construct (a new target or variable) is defined that
it knows the previous one is done. In short, this means that if you do
this:
all:
TARGS := $(.TARGETS)
then $(TARGS) won't contain "all", because it's not until after the
variable is created that the previous target is completed.
Changing this would be a major pain. I think a less complex way to do it
would be to pre-define the target files as soon as the first line is
parsed, then come back and do the rest of the definition as now. That
would allow $(.TARGETS) to be correct without a major change to the way
the parser works.
if (streq (var->name, ".TARGETS"))
var->value = build_target_list (var->value);
else
*/
if (variable_changenum != last_changenum && streq (var->name, ".VARIABLES"))
{
unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
unsigned long len;
char *p;
struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
struct variable **end = &vp[global_variable_set.table.ht_size];
/* Make sure we have at least MAX bytes in the allocated buffer. */
var->value = xrealloc (var->value, max);
/* Walk through the hash of variables, constructing a list of names. */
p = var->value;
len = 0;
for (; vp < end; ++vp)
if (!HASH_VACANT (*vp))
{
struct variable *v = *vp;
int l = v->length;
len += l + 1;
if (len > max)
{
unsigned long off = p - var->value;
max += EXPANSION_INCREMENT (l + 1);
var->value = xrealloc (var->value, max);
p = &var->value[off];
}
memcpy (p, v->name, l);
p += l;
*(p++) = ' ';
}
*(p-1) = '\0';
/* Remember the current variable change number. */
last_changenum = variable_changenum;
}
return var;
}
/* Lookup a variable whose name is a string starting at NAME
and with LENGTH chars. NAME need not be null-terminated.
Returns address of the 'struct variable' containing all info
on the variable, or nil if no such variable is defined. */
struct variable *
lookup_variable (const char *name, unsigned int length)
{
const struct variable_set_list *setlist;
struct variable var_key;
int is_parent = 0;
var_key.name = (char *) name;
var_key.length = length;
for (setlist = current_variable_set_list;
setlist != 0; setlist = setlist->next)
{
const struct variable_set *set = setlist->set;
struct variable *v;
v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
if (v && (!is_parent || !v->private_var))
return v->special ? lookup_special_var (v) : v;
is_parent |= setlist->next_is_parent;
}
#ifdef VMS
/* VMS does not populate envp[] with DCL symbols and logical names which
historically are mapped to enviroment varables and returned by getenv() */
{
char *vname = alloca (length + 1);
char *value;
strncpy (vname, name, length);
vname[length] = 0;
value = getenv (vname);
if (value != 0)
{
char *sptr;
int scnt;
sptr = value;
scnt = 0;
while ((sptr = strchr (sptr, '$')))
{
scnt++;
sptr++;
}
if (scnt > 0)
{
char *nvalue;
char *nptr;
nvalue = alloca (strlen (value) + scnt + 1);
sptr = value;
nptr = nvalue;
while (*sptr)
{
if (*sptr == '$')
{
*nptr++ = '$';
*nptr++ = '$';
}
else
{
*nptr++ = *sptr;
}
sptr++;
}
*nptr = '\0';
return define_variable (vname, length, nvalue, o_env, 1);
}
return define_variable (vname, length, value, o_env, 1);
}
}
#endif /* VMS */
return 0;
}
/* Lookup a variable whose name is a string starting at NAME
and with LENGTH chars in set SET. NAME need not be null-terminated.
Returns address of the 'struct variable' containing all info
on the variable, or nil if no such variable is defined. */
struct variable *
lookup_variable_in_set (const char *name, unsigned int length,
const struct variable_set *set)
{
struct variable var_key;
var_key.name = (char *) name;
var_key.length = length;
return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
}
/* Initialize FILE's variable set list. If FILE already has a variable set
list, the topmost variable set is left intact, but the the rest of the
chain is replaced with FILE->parent's setlist. If FILE is a double-colon
rule, then we will use the "root" double-colon target's variable set as the
parent of FILE's variable set.
If we're READING a makefile, don't do the pattern variable search now,
since the pattern variable might not have been defined yet. */
void
initialize_file_variables (struct file *file, int reading)
{
struct variable_set_list *l = file->variables;
if (l == 0)
{
l = (struct variable_set_list *)
xmalloc (sizeof (struct variable_set_list));
l->set = xmalloc (sizeof (struct variable_set));
hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
variable_hash_1, variable_hash_2, variable_hash_cmp);
file->variables = l;
}
/* If this is a double-colon, then our "parent" is the "root" target for
this double-colon rule. Since that rule has the same name, parent,
etc. we can just use its variables as the "next" for ours. */
if (file->double_colon && file->double_colon != file)
{
initialize_file_variables (file->double_colon, reading);
l->next = file->double_colon->variables;
l->next_is_parent = 0;
return;
}
if (file->parent == 0)
l->next = &global_setlist;
else
{
initialize_file_variables (file->parent, reading);
l->next = file->parent->variables;
}
l->next_is_parent = 1;
/* If we're not reading makefiles and we haven't looked yet, see if
we can find pattern variables for this target. */
if (!reading && !file->pat_searched)
{
struct pattern_var *p;
p = lookup_pattern_var (0, file->name);
if (p != 0)
{
struct variable_set_list *global = current_variable_set_list;
/* We found at least one. Set up a new variable set to accumulate
all the pattern variables that match this target. */
file->pat_variables = create_new_variable_set ();
current_variable_set_list = file->pat_variables;
do
{
/* We found one, so insert it into the set. */
struct variable *v;
if (p->variable.flavor == f_simple)
{
v = define_variable_loc (
p->variable.name, strlen (p->variable.name),
p->variable.value, p->variable.origin,
0, &p->variable.fileinfo);
v->flavor = f_simple;
}
else
{
v = do_variable_definition (
&p->variable.fileinfo, p->variable.name,
p->variable.value, p->variable.origin,
p->variable.flavor, 1);
}
/* Also mark it as a per-target and copy export status. */
v->per_target = p->variable.per_target;
v->export = p->variable.export;
v->private_var = p->variable.private_var;
}
while ((p = lookup_pattern_var (p, file->name)) != 0);
current_variable_set_list = global;
}
file->pat_searched = 1;
}
/* If we have a pattern variable match, set it up. */
if (file->pat_variables != 0)
{
file->pat_variables->next = l->next;
file->pat_variables->next_is_parent = l->next_is_parent;
l->next = file->pat_variables;
l->next_is_parent = 0;
}
}
/* Pop the top set off the current variable set list,
and free all its storage. */
struct variable_set_list *
create_new_variable_set (void)
{
register struct variable_set_list *setlist;
register struct variable_set *set;
set = xmalloc (sizeof (struct variable_set));
hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
variable_hash_1, variable_hash_2, variable_hash_cmp);
setlist = (struct variable_set_list *)
xmalloc (sizeof (struct variable_set_list));
setlist->set = set;
setlist->next = current_variable_set_list;
setlist->next_is_parent = 0;
return setlist;
}
/* Create a new variable set and push it on the current setlist.
If we're pushing a global scope (that is, the current scope is the global
scope) then we need to "push" it the other way: file variable sets point
directly to the global_setlist so we need to replace that with the new one.
*/
struct variable_set_list *
push_new_variable_scope (void)
{
current_variable_set_list = create_new_variable_set ();
if (current_variable_set_list->next == &global_setlist)
{
/* It was the global, so instead of new -> &global we want to replace
&global with the new one and have &global -> new, with current still
pointing to &global */
struct variable_set *set = current_variable_set_list->set;
current_variable_set_list->set = global_setlist.set;
global_setlist.set = set;
current_variable_set_list->next = global_setlist.next;
global_setlist.next = current_variable_set_list;
current_variable_set_list = &global_setlist;
}
return (current_variable_set_list);
}
void
pop_variable_scope (void)
{
struct variable_set_list *setlist;
struct variable_set *set;
/* Can't call this if there's no scope to pop! */
assert (current_variable_set_list->next != NULL);
if (current_variable_set_list != &global_setlist)
{
/* We're not pointing to the global setlist, so pop this one. */
setlist = current_variable_set_list;
set = setlist->set;
current_variable_set_list = setlist->next;
}
else
{
/* This set is the one in the global_setlist, but there is another global
set beyond that. We want to copy that set to global_setlist, then
delete what used to be in global_setlist. */
setlist = global_setlist.next;
set = global_setlist.set;
global_setlist.set = setlist->set;
global_setlist.next = setlist->next;
global_setlist.next_is_parent = setlist->next_is_parent;
}
/* Free the one we no longer need. */
free (setlist);
hash_map (&set->table, free_variable_name_and_value);
hash_free (&set->table, 1);
free (set);
}
/* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */
static void
merge_variable_sets (struct variable_set *to_set,
struct variable_set *from_set)
{
struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
int inc = to_set == &global_variable_set ? 1 : 0;
for ( ; from_var_slot < from_var_end; from_var_slot++)
if (! HASH_VACANT (*from_var_slot))
{
struct variable *from_var = *from_var_slot;
struct variable **to_var_slot
= (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
if (HASH_VACANT (*to_var_slot))
{
hash_insert_at (&to_set->table, from_var, to_var_slot);
variable_changenum += inc;
}
else
{
/* GKM FIXME: delete in from_set->table */
free (from_var->value);
free (from_var);
}
}
}
/* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1. */
void
merge_variable_set_lists (struct variable_set_list **setlist0,
struct variable_set_list *setlist1)
{
struct variable_set_list *to = *setlist0;
struct variable_set_list *last0 = 0;
/* If there's nothing to merge, stop now. */
if (!setlist1)
return;
/* This loop relies on the fact that all setlists terminate with the global
setlist (before NULL). If that's not true, arguably we SHOULD die. */
if (to)
while (setlist1 != &global_setlist && to != &global_setlist)
{
struct variable_set_list *from = setlist1;
setlist1 = setlist1->next;
merge_variable_sets (to->set, from->set);
last0 = to;
to = to->next;
}
if (setlist1 != &global_setlist)
{
if (last0 == 0)
*setlist0 = setlist1;
else
last0->next = setlist1;
}
}
/* Define the automatic variables, and record the addresses
of their structures so we can change their values quickly. */
void
define_automatic_variables (void)
{
struct variable *v;
char buf[200];
sprintf (buf, "%u", makelevel);
define_variable_cname (MAKELEVEL_NAME, buf, o_env, 0);
sprintf (buf, "%s%s%s",
version_string,
(remote_description == 0 || remote_description[0] == '\0')
? "" : "-",
(remote_description == 0 || remote_description[0] == '\0')
? "" : remote_description);
define_variable_cname ("MAKE_VERSION", buf, o_default, 0);
define_variable_cname ("MAKE_HOST", make_host, o_default, 0);
#ifdef __MSDOS__
/* Allow to specify a special shell just for Make,
and use $COMSPEC as the default $SHELL when appropriate. */
{
static char shell_str[] = "SHELL";
const int shlen = sizeof (shell_str) - 1;
struct variable *mshp = lookup_variable ("MAKESHELL", 9);
struct variable *comp = lookup_variable ("COMSPEC", 7);
/* $(MAKESHELL) overrides $(SHELL) even if -e is in effect. */
if (mshp)
(void) define_variable (shell_str, shlen,
mshp->value, o_env_override, 0);
else if (comp)
{
/* $(COMSPEC) shouldn't override $(SHELL). */
struct variable *shp = lookup_variable (shell_str, shlen);
if (!shp)
(void) define_variable (shell_str, shlen, comp->value, o_env, 0);
}
}
#elif defined(__EMX__)
{
static char shell_str[] = "SHELL";
const int shlen = sizeof (shell_str) - 1;
struct variable *shell = lookup_variable (shell_str, shlen);
struct variable *replace = lookup_variable ("MAKESHELL", 9);
/* if $MAKESHELL is defined in the environment assume o_env_override */
if (replace && *replace->value && replace->origin == o_env)
replace->origin = o_env_override;
/* if $MAKESHELL is not defined use $SHELL but only if the variable
did not come from the environment */
if (!replace || !*replace->value)
if (shell && *shell->value && (shell->origin == o_env
|| shell->origin == o_env_override))
{
/* overwrite whatever we got from the environment */
free (shell->value);
shell->value = xstrdup (default_shell);
shell->origin = o_default;
}
/* Some people do not like cmd to be used as the default
if $SHELL is not defined in the Makefile.
With -DNO_CMD_DEFAULT you can turn off this behaviour */
# ifndef NO_CMD_DEFAULT
/* otherwise use $COMSPEC */
if (!replace || !*replace->value)
replace = lookup_variable ("COMSPEC", 7);
/* otherwise use $OS2_SHELL */
if (!replace || !*replace->value)
replace = lookup_variable ("OS2_SHELL", 9);
# else
# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
# endif
if (replace && *replace->value)
/* overwrite $SHELL */
(void) define_variable (shell_str, shlen, replace->value,
replace->origin, 0);
else
/* provide a definition if there is none */
(void) define_variable (shell_str, shlen, default_shell,
o_default, 0);
}
#endif
/* This won't override any definition, but it will provide one if there
isn't one there. */
v = define_variable_cname ("SHELL", default_shell, o_default, 0);
#ifdef __MSDOS__
v->export = v_export; /* Export always SHELL. */
#endif
/* On MSDOS we do use SHELL from environment, since it isn't a standard
environment variable on MSDOS, so whoever sets it, does that on purpose.
On OS/2 we do not use SHELL from environment but we have already handled
that problem above. */
#if !defined(__MSDOS__) && !defined(__EMX__)
/* Don't let SHELL come from the environment. */
if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
{
free (v->value);
v->origin = o_file;
v->value = xstrdup (default_shell);
}
#endif
/* Make sure MAKEFILES gets exported if it is set. */
v = define_variable_cname ("MAKEFILES", "", o_default, 0);
v->export = v_ifset;
/* Define the magic D and F variables in terms of
the automatic variables they are variations of. */
#if defined(__MSDOS__) || defined(WINDOWS32)
/* For consistency, remove the trailing backslash as well as slash. */
define_variable_cname ("@D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $@)))",
o_automatic, 1);
define_variable_cname ("%D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $%)))",
o_automatic, 1);
define_variable_cname ("*D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $*)))",
o_automatic, 1);
define_variable_cname ("<D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $<)))",
o_automatic, 1);
define_variable_cname ("?D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $?)))",
o_automatic, 1);
define_variable_cname ("^D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $^)))",
o_automatic, 1);
define_variable_cname ("+D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $+)))",
o_automatic, 1);
#else /* not __MSDOS__, not WINDOWS32 */
define_variable_cname ("@D", "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
define_variable_cname ("%D", "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
define_variable_cname ("*D", "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
define_variable_cname ("<D", "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
define_variable_cname ("?D", "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
define_variable_cname ("^D", "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
define_variable_cname ("+D", "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
#endif
define_variable_cname ("@F", "$(notdir $@)", o_automatic, 1);
define_variable_cname ("%F", "$(notdir $%)", o_automatic, 1);
define_variable_cname ("*F", "$(notdir $*)", o_automatic, 1);
define_variable_cname ("<F", "$(notdir $<)", o_automatic, 1);
define_variable_cname ("?F", "$(notdir $?)", o_automatic, 1);
define_variable_cname ("^F", "$(notdir $^)", o_automatic, 1);
define_variable_cname ("+F", "$(notdir $+)", o_automatic, 1);
}
int export_all_variables;
/* Create a new environment for FILE's commands.
If FILE is nil, this is for the 'shell' function.
The child's MAKELEVEL variable is incremented. */
char **
target_environment (struct file *file)
{
struct variable_set_list *set_list;
register struct variable_set_list *s;
struct hash_table table;
struct variable **v_slot;
struct variable **v_end;
struct variable makelevel_key;
char **result_0;
char **result;
if (file == 0)
set_list = current_variable_set_list;
else
set_list = file->variables;
hash_init (&table, VARIABLE_BUCKETS,
variable_hash_1, variable_hash_2, variable_hash_cmp);
/* Run through all the variable sets in the list,