-
Notifications
You must be signed in to change notification settings - Fork 5
/
parse.c
1707 lines (1541 loc) · 44 KB
/
parse.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
/* $OpenBSD: parse.c,v 1.134 2021/03/06 08:31:42 espie Exp $ */
/* $NetBSD: parse.c,v 1.29 1997/03/10 21:20:04 christos Exp $ */
/*
* Copyright (c) 1999 Marc Espie.
*
* Extensive code changes for the OpenBSD project.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
* PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1988, 1989, 1990, 1993
* The Regents of the University of California. All rights reserved.
* Copyright (c) 1989 by Berkeley Softworks
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Adam de Boor.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <assert.h>
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ohash.h>
#include "config.h"
#include "defines.h"
#include "dir.h"
#include "direxpand.h"
#include "job.h"
#include "buf.h"
#include "for.h"
#include "lowparse.h"
#include "arch.h"
#include "cond.h"
#include "suff.h"
#include "parse.h"
#include "var.h"
#include "targ.h"
#include "error.h"
#include "str.h"
#include "main.h"
#include "gnode.h"
#include "memory.h"
#include "extern.h"
#include "lst.h"
#include "parsevar.h"
#include "stats.h"
#include "garray.h"
#include "node_int.h"
#include "nodehashconsts.h"
/* gsources and gtargets should be local to some functions, but they're
* set as persistent arrays for performance reasons.
*/
static struct growableArray gsources, gtargets;
static struct ohash htargets;
static bool htargets_setup = false;
#define SOURCES_SIZE 128
#define TARGETS_SIZE 32
static LIST theUserIncPath;/* list of directories for "..." includes */
static LIST theSysIncPath; /* list of directories for <...> includes */
Lst systemIncludePath = &theSysIncPath;
Lst userIncludePath = &theUserIncPath;
static GNode *mainNode; /* The main target to create. This is the
* first target on the first dependency
* line in the first makefile */
/*-
* specType contains the special TYPE of the current target. It is
* SPECIAL_NONE if the target is unspecial. If it *is* special, however,
* the children are linked as children of the parent but not vice versa.
* This variable is set in ParseDoDependency
*/
static unsigned int specType;
static int waiting;
/*
* Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
* seen, then set to each successive source on the line.
*/
static GNode *predecessor;
static void ParseLinkSrc(GNode *, GNode *);
static int ParseDoOp(GNode **, unsigned int);
static void ParseDoSpecial(GNode *, unsigned int);
static int ParseAddDep(GNode *, GNode *);
static void ParseDoSrc(struct growableArray *, struct growableArray *, int,
const char *, const char *);
static int ParseFindMain(void *, void *);
static void ParseClearPath(void *);
static void add_target_node(const char *, const char *);
static void add_target_nodes(const char *, const char *);
static void apply_op(struct growableArray *, unsigned int, GNode *);
static void ParseDoDependency(const char *);
static void ParseAddCmd(void *, void *);
static void ParseHasCommands(void *);
static bool handle_poison(const char *);
static bool handle_for_loop(Buffer, const char *);
static bool handle_undef(const char *);
#define ParseReadLoopLine(linebuf) Parse_ReadUnparsedLine(linebuf, "for loop")
static bool handle_bsd_command(Buffer, Buffer, const char *);
static bool register_target(GNode *, struct ohash *);
static char *strip_comments(Buffer, const char *);
static char *resolve_include_filename(const char *, const char *, bool);
static void handle_include_file(const char *, const char *, bool, bool);
static bool lookup_bsd_include(const char *);
static void lookup_sysv_style_include(const char *, const char *, bool);
static void lookup_sysv_include(const char *, const char *);
static void lookup_conditional_include(const char *, const char *);
static bool parse_as_special_line(Buffer, Buffer, const char *);
static unsigned int parse_operator(const char **);
static const char *parse_do_targets(Lst, unsigned int *, const char *);
static void parse_target_line(struct growableArray *, const char *,
const char *, bool *);
static void finish_commands(struct growableArray *);
static void parse_commands(struct growableArray *, const char *);
static void create_special_nodes(void);
static bool found_delimiter(const char *);
static unsigned int handle_special_targets(Lst);
static void dump_targets(void);
static void dedup_targets(struct growableArray *);
static void build_target_group(struct growableArray *, struct ohash *t);
static void reset_target_hash(void);
#define P(k) k, sizeof(k), K_##k
static struct {
const char *keyword;
size_t sz;
uint32_t hv;
unsigned int special;
unsigned int special_op;
} specials[] = {
{ P(NODE_EXEC), SPECIAL_DEPRECATED, 0 },
{ P(NODE_IGNORE), SPECIAL_IGNORE, OP_IGNORE },
{ P(NODE_INCLUDES), SPECIAL_DEPRECATED, 0 },
{ P(NODE_INVISIBLE), SPECIAL_DEPRECATED, 0 },
{ P(NODE_JOIN), SPECIAL_DEPRECATED, 0 },
{ P(NODE_LIBS), SPECIAL_DEPRECATED, 0 },
{ P(NODE_MADE), SPECIAL_DEPRECATED, 0 },
{ P(NODE_MAIN), SPECIAL_MAIN, 0 },
{ P(NODE_MAKE), SPECIAL_MAKE, OP_MAKE },
{ P(NODE_MAKEFLAGS), SPECIAL_MFLAGS, 0 },
{ P(NODE_MFLAGS), SPECIAL_MFLAGS, 0 },
{ P(NODE_NOTMAIN), SPECIAL_NOTMAIN, OP_NOTMAIN },
{ P(NODE_NOTPARALLEL), SPECIAL_NOTPARALLEL, 0 },
{ P(NODE_NO_PARALLEL), SPECIAL_NOTPARALLEL, 0 },
{ P(NODE_NULL), SPECIAL_DEPRECATED, 0 },
{ P(NODE_OPTIONAL), SPECIAL_OPTIONAL, OP_OPTIONAL },
{ P(NODE_ORDER), SPECIAL_ORDER, 0 },
{ P(NODE_PARALLEL), SPECIAL_PARALLEL, 0 },
{ P(NODE_PATH), SPECIAL_PATH, 0 },
{ P(NODE_PHONY), SPECIAL_PHONY, OP_PHONY },
{ P(NODE_PRECIOUS), SPECIAL_PRECIOUS, OP_PRECIOUS },
{ P(NODE_RECURSIVE), SPECIAL_MAKE, OP_MAKE },
{ P(NODE_SILENT), SPECIAL_SILENT, OP_SILENT },
{ P(NODE_SINGLESHELL), SPECIAL_NOTHING, 0 },
{ P(NODE_SUFFIXES), SPECIAL_SUFFIXES, 0 },
{ P(NODE_USE), SPECIAL_USE, OP_USE },
{ P(NODE_WAIT), SPECIAL_WAIT, 0 },
{ P(NODE_CHEAP), SPECIAL_CHEAP, OP_CHEAP },
{ P(NODE_EXPENSIVE), SPECIAL_EXPENSIVE, OP_EXPENSIVE },
{ P(NODE_POSIX), SPECIAL_NOTHING, 0 },
{ P(NODE_SCCS_GET), SPECIAL_NOTHING, 0 },
};
#undef P
static void
create_special_nodes()
{
unsigned int i;
for (i = 0; i < sizeof(specials)/sizeof(specials[0]); i++) {
(void)Targ_mk_special_node(specials[i].keyword,
specials[i].sz, specials[i].hv,
OP_ZERO, specials[i].special, specials[i].special_op);
}
}
/*-
*---------------------------------------------------------------------
* ParseLinkSrc --
* Link the parent node to its new child. Used by
* ParseDoDependency. If the specType isn't 'Not', the parent
* isn't linked as a parent of the child.
*
* Side Effects:
* New elements are added to the parents list of cgn and the
* children list of cgn. the children_left field of pgn is updated
* to reflect the additional child.
*---------------------------------------------------------------------
*/
static void
ParseLinkSrc(GNode *pgn, GNode *cgn)
{
if (Lst_AddNew(&pgn->children, cgn)) {
if (specType == SPECIAL_NONE)
Lst_AtEnd(&cgn->parents, pgn);
pgn->children_left++;
}
}
static char *
operator_string(int op)
{
/* XXX we don't bother freeing this, it's used for a fatal error
* anyways
*/
char *result = emalloc(5);
char *t = result;
if (op & OP_DEPENDS) {
*t++ = ':';
}
if (op & OP_FORCE) {
*t++ = '!';
}
if (op & OP_DOUBLEDEP) {
*t++ = ':';
*t++ = ':';
}
*t = 0;
return result;
}
/*-
*---------------------------------------------------------------------
* ParseDoOp --
* Apply the parsed operator to the given target node. Used in a
* Array_Find call by ParseDoDependency once all targets have
* been found and their operator parsed. If the previous and new
* operators are incompatible, a major error is taken, and the find
* stops early
*
* Side Effects:
* The node gets the right dependency operator.
* Cohorts may be created for double dep.
*---------------------------------------------------------------------
*/
static int
ParseDoOp(GNode **gnp, unsigned int op)
{
GNode *gn = *gnp;
assert(op == (op & OP_OPMASK));
/* if the node didn't already appear on the left hand side (no known
* dependency operator), we don't need to do much. */
if (!OP_NOP(gn->type)) {
/*
* If the dependency mask of the operator and the node don't
* match and the node has actually had an operator applied to
* it before, and the operator actually has some dependency
* information in it, complain. */
if (op != (gn->type & OP_OPMASK)) {
Parse_Error(PARSE_FATAL,
"Inconsistent dependency operator for target %s\n"
"\t(was %s%s, now %s%s)",
gn->name, gn->name, operator_string(gn->type),
gn->name, operator_string(op));
return 0;
}
if (op == OP_DOUBLEDEP) {
/* If the node was the object of a :: operator, we need
* to create a new instance of it for the children and
* commands on this dependency line. The new instance
* is placed on the 'cohorts' list of the initial one
* (note the initial one is not on its own cohorts
* list) and the new instance is linked to all parents
* of the initial instance. */
GNode *cohort;
LstNode ln;
cohort = Targ_NewGN(gn->name);
/* Duplicate links to parents so graph traversal is
* simple. Perhaps some type bits should be
* duplicated?
*
* Make the cohort invisible as well to avoid
* duplicating it into other variables. True, parents
* of this target won't tend to do anything with their
* local variables, but better safe than sorry. */
for (ln = Lst_First(&gn->parents); ln != NULL;
ln = Lst_Adv(ln))
ParseLinkSrc(Lst_Datum(ln), cohort);
cohort->type = OP_DOUBLEDEP|OP_INVISIBLE;
Lst_AtEnd(&gn->cohorts, cohort);
/* Replace the node in the targets list with the new
* copy */
*gnp = cohort;
gn = cohort;
}
}
/* Preserve possible special flags already applied to the operator */
gn->type |= op;
return 1;
}
static void
ParseDoSpecial(GNode *gn, unsigned int special_op)
{
gn->type |= special_op;
}
/*-
*---------------------------------------------------------------------
* ParseAddDep --
* Check if the pair of GNodes given needs to be synchronized.
* This has to be when two nodes are on different sides of a
* .WAIT directive.
*
* Results:
* Returns 0 if the two targets need to be ordered, 1 otherwise.
* If it returns 0, the search can stop.
*
* Side Effects:
* A dependency can be added between the two nodes.
*
*---------------------------------------------------------------------
*/
static int
ParseAddDep(GNode *p, GNode *s)
{
if (p->order < s->order) {
/* XXX: This can cause cycles but finding them is hard
* and debugging output will show the problem. */
Lst_AtEnd(&s->predecessors, p);
Lst_AtEnd(&p->successors, s);
return 1;
} else
return 0;
}
static void
apply_op(struct growableArray *targets, unsigned int op, GNode *gn)
{
if (op)
gn->type |= op;
else
Array_ForEach(targets, ParseLinkSrc, gn);
}
/*-
*---------------------------------------------------------------------
* ParseDoSrc --
* Given the name of a source, figure out if it is an attribute
* and apply it to the targets if it is. Else decide if there is
* some attribute which should be applied *to* the source because
* of some special target and apply it if so. Otherwise, make the
* source be a child of the targets in the list 'targets'
*
* Side Effects:
* Operator bits may be added to the list of targets or to the source.
* The targets may have a new source added to their lists of children.
*---------------------------------------------------------------------
*/
static void
ParseDoSrc(
struct growableArray *targets,
struct growableArray *sources,
int tOp, /* operator (if any) from special targets */
const char *src, /* name of the source to handle */
const char *esrc)
{
GNode *gn = Targ_FindNodei(src, esrc, TARG_CREATE);
if (gn->special == SPECIAL_DEPRECATED) {
Parse_Error(PARSE_FATAL, "Deprecated keyword found %s\n",
gn->name);
return;
}
if (gn->special_op) {
Array_ForEach(targets, ParseDoSpecial, gn->special_op);
return;
}
if (gn->special == SPECIAL_WAIT) {
waiting++;
return;
}
switch (specType) {
case SPECIAL_MAIN:
/*
* If we have noted the existence of a .MAIN, it means we need
* to add the sources of said target to the list of things
* to create. Note that this will only be invoked if the user
* didn't specify a target on the command line. This is to
* allow #ifmake's to succeed, or something...
*/
Lst_AtEnd(create, gn->name);
/*
* Add the name to the .TARGETS variable as well, so the user
* can employ that, if desired.
*/
Var_Append(".TARGETS", gn->name);
return;
case SPECIAL_ORDER:
/*
* Create proper predecessor/successor links between the
* previous source and the current one.
*/
if (predecessor != NULL) {
Lst_AtEnd(&predecessor->successors, gn);
Lst_AtEnd(&gn->predecessors, predecessor);
}
predecessor = gn;
break;
default:
/*
* In the case of a source that was the object of a :: operator,
* the attribute is applied to all of its instances (as kept in
* the 'cohorts' list of the node) or all the cohorts are linked
* to all the targets.
*/
apply_op(targets, tOp, gn);
if ((gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
LstNode ln;
for (ln=Lst_First(&gn->cohorts); ln != NULL;
ln = Lst_Adv(ln)){
apply_op(targets, tOp, Lst_Datum(ln));
}
}
break;
}
gn->order = waiting;
Array_AtEnd(sources, gn);
if (waiting)
Array_Find(sources, ParseAddDep, gn);
}
/*-
*-----------------------------------------------------------------------
* ParseFindMain --
* Find a real target in the list and set it to be the main one.
* Called by ParseDoDependency when a main target hasn't been found
* yet.
*
* Results:
* 1 if main not found yet, 0 if it is.
*
* Side Effects:
* mainNode is changed and.
*-----------------------------------------------------------------------
*/
static int
ParseFindMain(void *gnp, void *dummy UNUSED)
{
GNode *gn = gnp;
if ((gn->type & OP_NOTARGET) == 0 && gn->special == SPECIAL_NONE) {
mainNode = gn;
return 0;
} else {
return 1;
}
}
/*-
*-----------------------------------------------------------------------
* ParseClearPath --
* Reinit path to an empty path
*-----------------------------------------------------------------------
*/
static void
ParseClearPath(void *p)
{
Lst path = p;
Lst_Destroy(path, Dir_Destroy);
Lst_Init(path);
}
static void
add_target_node(const char *line, const char *end)
{
GNode *gn;
gn = Suff_ParseAsTransform(line, end);
if (gn == NULL) {
gn = Targ_FindNodei(line, end, TARG_CREATE);
gn->type &= ~OP_DUMMY;
}
Array_AtEnd(>argets, gn);
}
static void
add_target_nodes(const char *line, const char *end)
{
if (Dir_HasWildcardsi(line, end)) {
/*
* Targets are to be sought only in the current directory,
* so create an empty path for the thing. Note we need to
* use Dir_Destroy in the destruction of the path as the
* Dir module could have added a directory to the path...
*/
char *targName;
LIST emptyPath;
LIST curTargs;
Lst_Init(&emptyPath);
Lst_Init(&curTargs);
Dir_Expandi(line, end, &emptyPath, &curTargs);
Lst_Destroy(&emptyPath, Dir_Destroy);
while ((targName = Lst_DeQueue(&curTargs)) != NULL) {
add_target_node(targName, targName + strlen(targName));
}
Lst_Destroy(&curTargs, NOFREE);
} else {
add_target_node(line, end);
}
}
/* special target line check: a proper delimiter is a ':' or '!', but
* we don't want to end a target on such a character if there is a better
* match later on.
* By "better" I mean one that is followed by whitespace. This allows the
* user to have targets like:
* fie::fi:fo: fum
* where "fie::fi:fo" is the target. In real life this is used for perl5
* library man pages where "::" separates an object from its class. Ie:
* "File::Spec::Unix".
* This behaviour is also consistent with other versions of make.
*/
static bool
found_delimiter(const char *s)
{
if (*s == '!' || *s == ':') {
const char *p = s + 1;
if (*s == ':' && *p == ':')
p++;
/* Found the best match already. */
if (ISSPACE(*p) || *p == '\0')
return true;
do {
p += strcspn(p, "!:");
if (*p == '\0')
break;
/* always prefer : to ! if not space separated */
if (*p == ':' && *s == '!')
return false;
p++;
} while (*p != '\0' && !ISSPACE(*p));
/* No better match later on... */
if (*p == '\0')
return true;
}
return false;
}
static const char *
parse_do_targets(Lst paths, unsigned int *op, const char *line)
{
const char *cp;
do {
for (cp = line; *cp && !ISSPACE(*cp) && *cp != '(';) {
if (*cp == '$')
/* Must be a dynamic source (would have been
* expanded otherwise), so call the Var module
* to parse the puppy so we can safely advance
* beyond it...There should be no errors in
* this, as they would have been discovered in
* the initial Var_Subst and we wouldn't be
* here. */
Var_ParseSkip(&cp, NULL);
else {
if (found_delimiter(cp))
break;
cp++;
}
}
if (*cp == '(') {
LIST temp;
Lst_Init(&temp);
/* Archives must be handled specially to make sure the
* OP_ARCHV flag is set in their 'type' field, for one
* thing, and because things like "archive(file1.o
* file2.o file3.o)" are permissible.
* Arch_ParseArchive will set 'line' to be the first
* non-blank after the archive-spec. It creates/finds
* nodes for the members and places them on the given
* list, returning true if all went well and false if
* there was an error in the specification. On error,
* line should remain untouched. */
if (!Arch_ParseArchive(&line, &temp, NULL)) {
Parse_Error(PARSE_FATAL,
"Error in archive specification: \"%s\"",
line);
return NULL;
} else {
AppendList2Array(&temp, >argets);
Lst_Destroy(&temp, NOFREE);
cp = line;
continue;
}
}
if (*cp == '\0') {
/* Ending a dependency line without an operator is a
* Bozo no-no */
/* Deeper check for cvs conflicts */
if (gtargets.n > 0 &&
(strcmp(gtargets.a[0]->name, "<<<<<<<") == 0 ||
strcmp(gtargets.a[0]->name, ">>>>>>>") == 0)) {
Parse_Error(PARSE_FATAL,
"Need an operator (likely from a cvs update conflict)");
} else {
Parse_Error(PARSE_FATAL,
"Need an operator in '%s'", line);
}
return NULL;
}
/*
* Have word in line. Get or create its nodes and stick it at
* the end of the targets list
*/
if (*line != '\0')
add_target_nodes(line, cp);
while (ISSPACE(*cp))
cp++;
line = cp;
} while (*line != '!' && *line != ':' && *line);
*op = handle_special_targets(paths);
return cp;
}
static void
dump_targets()
{
size_t i;
for (i = 0; i < gtargets.n; i++)
fprintf(stderr, "%s", gtargets.a[i]->name);
fprintf(stderr, "\n");
}
static unsigned int
handle_special_targets(Lst paths)
{
size_t i;
int seen_path = 0;
int seen_special = 0;
int seen_normal = 0;
int type;
for (i = 0; i < gtargets.n; i++) {
type = gtargets.a[i]->special;
if (type == SPECIAL_DEPRECATED) {
Parse_Error(PARSE_FATAL,
"Deprecated keyword found %s\n",
gtargets.a[i]->name);
specType = SPECIAL_ERROR;
return 0;
}
if (type == SPECIAL_PATH) {
seen_path++;
Lst_AtEnd(paths, find_suffix_path(gtargets.a[i]));
} else if (type != 0)
seen_special++;
else
seen_normal++;
}
if ((seen_path != 0) + (seen_special != 0) + (seen_normal != 0) > 1) {
Parse_Error(PARSE_FATAL, "Wrong mix of special targets");
dump_targets();
specType = SPECIAL_ERROR;
return 0;
}
if (seen_normal != 0) {
specType = SPECIAL_NONE;
return 0;
} else if (seen_path != 0) {
specType = SPECIAL_PATH;
return 0;
} else if (seen_special == 0) {
specType = SPECIAL_NONE;
return 0;
} else if (seen_special != 1) {
Parse_Error(PARSE_FATAL,
"Mixing special targets is not allowed");
dump_targets();
return 0;
} else if (seen_special == 1) {
specType = gtargets.a[0]->special;
switch (specType) {
case SPECIAL_MAIN:
if (!Lst_IsEmpty(create)) {
specType = SPECIAL_NONE;
}
break;
case SPECIAL_NOTPARALLEL:
set_notparallel();
break;
case SPECIAL_ORDER:
predecessor = NULL;
break;
default:
break;
}
return gtargets.a[0]->special_op;
} else {
/* we're allowed to have 0 target */
specType = SPECIAL_NONE;
return 0;
}
}
static unsigned int
parse_operator(const char **pos)
{
const char *cp = *pos;
unsigned int op = OP_ERROR;
if (*cp == '!') {
op = OP_FORCE;
} else if (*cp == ':') {
if (cp[1] == ':') {
op = OP_DOUBLEDEP;
cp++;
} else {
op = OP_DEPENDS;
}
} else {
Parse_Error(PARSE_FATAL, "Missing dependency operator");
return OP_ERROR;
}
cp++; /* Advance beyond operator */
/* Get to the first source */
while (ISSPACE(*cp))
cp++;
*pos = cp;
return op;
}
/*-
*---------------------------------------------------------------------
* ParseDoDependency --
* Parse the dependency line in line.
*
* Side Effects:
* The nodes of the sources are linked as children to the nodes of the
* targets. Some nodes may be created.
*
* We parse a dependency line by first extracting words from the line and
* finding nodes in the list of all targets with that name. This is done
* until a character is encountered which is an operator character. Currently
* these are only ! and :. At this point the operator is parsed and the
* pointer into the line advanced until the first source is encountered.
* The parsed operator is applied to each node in the 'targets' list,
* which is where the nodes found for the targets are kept, by means of
* the ParseDoOp function.
* The sources are read in much the same way as the targets were except
* that now they are expanded using the wildcarding scheme of the C-Shell
* and all instances of the resulting words in the list of all targets
* are found. Each of the resulting nodes is then linked to each of the
* targets as one of its children.
* Certain targets are handled specially. These are the ones detailed
* by the specType variable.
* The storing of transformation rules is also taken care of here.
* A target is recognized as a transformation rule by calling
* Suff_IsTransform. If it is a transformation rule, its node is gotten
* from the suffix module via Suff_AddTransform rather than the standard
* Targ_FindNode in the target module.
*---------------------------------------------------------------------
*/
static void
ParseDoDependency(const char *line) /* the line to parse */
{
const char *cp; /* our current position */
unsigned int op; /* the operator on the line */
LIST paths; /* List of search paths to alter when parsing
* a list of .PATH targets */
unsigned int tOp; /* operator from special target */
waiting = 0;
Lst_Init(&paths);
Array_Reset(&gsources);
cp = parse_do_targets(&paths, &tOp, line);
assert(specType == SPECIAL_PATH || Lst_IsEmpty(&paths));
if (cp == NULL || specType == SPECIAL_ERROR) {
/* invalidate targets for further processing */
Array_Reset(>argets);
return;
}
op = parse_operator(&cp);
if (op == OP_ERROR) {
/* invalidate targets for further processing */
Array_Reset(>argets);
return;
}
Array_FindP(>argets, ParseDoOp, op);
dedup_targets(>argets);
line = cp;
/* Several special targets have specific semantics with no source:
* .SUFFIXES clears out all old suffixes
* .PRECIOUS/.IGNORE/.SILENT
* apply to all target
* .PATH clears out all search paths. */
if (!*line) {
switch (specType) {
case SPECIAL_SUFFIXES:
Suff_DisableAllSuffixes();
break;
case SPECIAL_PRECIOUS:
allPrecious = true;
break;
case SPECIAL_IGNORE:
ignoreErrors = true;
break;
case SPECIAL_SILENT:
beSilent = true;
break;
case SPECIAL_PATH:
Lst_Every(&paths, ParseClearPath);
break;
default:
break;
}
} else if (specType == SPECIAL_MFLAGS) {
Main_ParseArgLine(line);
return;
} else if (specType == SPECIAL_NOTPARALLEL) {
return;
}
/* NOW GO FOR THE SOURCES */
if (specType == SPECIAL_SUFFIXES || specType == SPECIAL_PATH ||
specType == SPECIAL_NOTHING) {
while (*line) {
/* Some special targets take a list of space-separated
* words. For each word,
*
* if .SUFFIXES, add it to the list of suffixes maintained
* by suff.c.
*
* if .PATHS, add it as a directory on the main search path.
*
* if .LIBS/.INCLUDE/.NULL... this has been deprecated,
* ignore
*/
while (*cp && !ISSPACE(*cp))
cp++;
switch (specType) {
case SPECIAL_SUFFIXES:
Suff_AddSuffixi(line, cp);
break;
case SPECIAL_PATH:
{
LstNode ln;
for (ln = Lst_First(&paths); ln != NULL;
ln = Lst_Adv(ln))
Dir_AddDiri(Lst_Datum(ln), line, cp);
break;
}
default:
break;
}
if (*cp != '\0')
cp++;
while (ISSPACE(*cp))
cp++;
line = cp;
}
} else {
while (*line) {
/*
* The targets take real sources, so we must beware of
* archive specifications (i.e. things with left
* parentheses in them) and handle them accordingly.
*/
while (*cp && !ISSPACE(*cp)) {
if (*cp == '(' && cp > line && cp[-1] != '$') {
/*
* Only stop for a left parenthesis if
* it isn't at the start of a word
* (that'll be for variable changes
* later) and isn't preceded by a
* dollar sign (a dynamic source).
*/
break;
} else {
cp++;
}
}
if (*cp == '(') {
GNode *gn;
LIST sources; /* list of archive source
* names after expansion */
Lst_Init(&sources);
if (!Arch_ParseArchive(&line, &sources, NULL)) {
Parse_Error(PARSE_FATAL,
"Error in source archive spec \"%s\"",
line);
return;
}
while ((gn = Lst_DeQueue(&sources)) != NULL)
ParseDoSrc(>argets, &gsources, tOp,
gn->name, NULL);
cp = line;
} else {
const char *endSrc = cp;
ParseDoSrc(>argets, &gsources, tOp, line,
endSrc);
if (*cp)
cp++;
}
while (ISSPACE(*cp))
cp++;
line = cp;
}
}
/* If we're not in the SPEC_PATH case the list is empty, but who
* cares ? This is cheap.
*/
Lst_Destroy(&paths, NOFREE);
if (mainNode == NULL) {
/* If we have yet to decide on a main target to make, in the
* absence of any user input, we want the first target on
* the first dependency line that is actually a real target
* (i.e. isn't a .USE or .EXEC rule) to be made. */