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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
|
#ifdef CONFIG_WITH_INCLUDEDEP
/* $Id: incdep.c 2591 2012-06-17 20:45:31Z bird $ */
/** @file
* incdep - Simple dependency files.
*/
/*
* Copyright (c) 2007-2010 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
*
* This file is part of kBuild.
*
* kBuild 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.
*
* kBuild 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 kBuild. If not, see <http://www.gnu.org/licenses/>
*
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#ifdef __OS2__
# define INCL_BASE
# define INCL_ERRORS
#endif
#include "make.h"
#if !defined(WINDOWS32) && !defined(__OS2__)
# define HAVE_PTHREAD
#endif
#include <assert.h>
#include <glob.h>
#include "dep.h"
#include "filedef.h"
#include "job.h"
#include "commands.h"
#include "variable.h"
#include "rule.h"
#include "debug.h"
#include "strcache2.h"
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#else
# include <sys/file.h>
#endif
#ifdef WINDOWS32
# include <io.h>
# include <process.h>
# include <Windows.h>
# define PARSE_IN_WORKER
#endif
#ifdef __OS2__
# include <os2.h>
# include <sys/fmutex.h>
#endif
#ifdef HAVE_PTHREAD
# include <pthread.h>
#endif
#ifdef __APPLE__
# include <malloc/malloc.h>
# define PARSE_IN_WORKER
#endif
#if defined(__gnu_linux__) || defined(__linux__)
# define PARSE_IN_WORKER
#endif
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
struct incdep_variable_in_set
{
struct incdep_variable_in_set *next;
/* the parameters */
struct strcache2_entry *name_entry; /* dep strcache - WRONG */
const char *value; /* xmalloc'ed */
unsigned int value_length;
int duplicate_value; /* 0 */
enum variable_origin origin;
int recursive;
struct variable_set *set;
const struct floc *flocp; /* NILF */
};
struct incdep_variable_def
{
struct incdep_variable_def *next;
/* the parameters */
const struct floc *flocp; /* NILF */
struct strcache2_entry *name_entry; /* dep strcache - WRONG */
char *value; /* xmalloc'ed, free it */
unsigned int value_length;
enum variable_origin origin;
enum variable_flavor flavor;
int target_var;
};
struct incdep_recorded_file
{
struct incdep_recorded_file *next;
/* the parameters */
struct strcache2_entry *filename_entry; /* dep strcache; converted to a nameseq record. */
struct dep *deps; /* All the names are dep strcache entries. */
const struct floc *flocp; /* NILF */
};
/* per dep file structure. */
struct incdep
{
struct incdep *next;
char *file_base;
char *file_end;
int worker_tid;
#ifdef PARSE_IN_WORKER
unsigned int err_line_no;
const char *err_msg;
struct incdep_variable_in_set *recorded_variables_in_set_head;
struct incdep_variable_in_set *recorded_variables_in_set_tail;
struct incdep_variable_def *recorded_variable_defs_head;
struct incdep_variable_def *recorded_variable_defs_tail;
struct incdep_recorded_file *recorded_file_head;
struct incdep_recorded_file *recorded_file_tail;
#endif
char name[1];
};
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/* mutex protecting the globals and an associated condition/event. */
#ifdef HAVE_PTHREAD
static pthread_mutex_t incdep_mtx;
static pthread_cond_t incdep_cond_todo;
static pthread_cond_t incdep_cond_done;
#elif defined (WINDOWS32)
static CRITICAL_SECTION incdep_mtx;
static HANDLE incdep_hev_todo;
static HANDLE incdep_hev_done;
static int volatile incdep_hev_todo_waiters;
static int volatile incdep_hev_done_waiters;
#elif defined (__OS2__)
static _fmutex incdep_mtx;
static HEV incdep_hev_todo;
static HEV incdep_hev_done;
static int volatile incdep_hev_todo_waiters;
static int volatile incdep_hev_done_waiters;
#endif
/* flag indicating whether the threads, lock and event/condvars has
been initialized or not. */
static int incdep_initialized;
/* the list of files that needs reading. */
static struct incdep * volatile incdep_head_todo;
static struct incdep * volatile incdep_tail_todo;
/* the number of files that are currently being read. */
static int volatile incdep_num_reading;
/* the list of files that have been read. */
static struct incdep * volatile incdep_head_done;
static struct incdep * volatile incdep_tail_done;
/* The handles to the worker threads. */
#ifdef HAVE_PTHREAD
# define INCDEP_MAX_THREADS 1
static pthread_t incdep_threads[INCDEP_MAX_THREADS];
#elif defined (WINDOWS32)
# define INCDEP_MAX_THREADS 2
static HANDLE incdep_threads[INCDEP_MAX_THREADS];
#elif defined (__OS2__)
# define INCDEP_MAX_THREADS 2
static TID incdep_threads[INCDEP_MAX_THREADS];
#endif
static struct alloccache incdep_rec_caches[INCDEP_MAX_THREADS];
static struct alloccache incdep_dep_caches[INCDEP_MAX_THREADS];
static struct strcache2 incdep_dep_strcaches[INCDEP_MAX_THREADS];
static struct strcache2 incdep_var_strcaches[INCDEP_MAX_THREADS];
static unsigned incdep_num_threads;
/* flag indicating whether the worker threads should terminate or not. */
static int volatile incdep_terminate;
#ifdef __APPLE__
/* malloc zone for the incdep threads. */
static malloc_zone_t *incdep_zone;
#endif
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static void incdep_flush_it (struct floc *);
static void eval_include_dep_file (struct incdep *, struct floc *);
static void incdep_commit_recorded_file (const char *filename, struct dep *deps,
const struct floc *flocp);
/* xmalloc wrapper.
For working around multithreaded performance problems found on Darwin,
Linux (glibc), and possibly other systems. */
static void *
incdep_xmalloc (struct incdep *cur, size_t size)
{
void *ptr;
#ifdef __APPLE__
if (cur && cur->worker_tid != -1)
{
ptr = malloc_zone_malloc (incdep_zone, size);
if (!ptr)
fatal (NILF, _("virtual memory exhausted"));
}
else
ptr = xmalloc (size);
#else
ptr = xmalloc (size);
#endif
(void)cur;
return ptr;
}
#if 0
/* memset(malloc(sz),'\0',sz) wrapper. */
static void *
incdep_xcalloc (struct incdep *cur, size_t size)
{
void *ptr;
#ifdef __APPLE__
if (cur && cur->worker_tid != -1)
ptr = malloc_zone_calloc (incdep_zone, size, 1);
else
ptr = calloc (size, 1);
#else
ptr = calloc (size, 1);
#endif
if (!ptr)
fatal (NILF, _("virtual memory exhausted"));
(void)cur;
return ptr;
}
#endif /* unused */
/* free wrapper */
static void
incdep_xfree (struct incdep *cur, void *ptr)
{
/* free() *must* work for the allocation hacks above because
of free_dep_chain. */
free (ptr);
(void)cur;
}
/* alloc a dep structure. These are allocated in bunches to save time. */
struct dep *
incdep_alloc_dep (struct incdep *cur)
{
struct alloccache *cache;
if (cur->worker_tid != -1)
cache = &incdep_dep_caches[cur->worker_tid];
else
cache = &dep_cache;
return alloccache_calloc (cache);
}
/* allocate a record. */
static void *
incdep_alloc_rec (struct incdep *cur)
{
return alloccache_alloc (&incdep_rec_caches[cur->worker_tid]);
}
/* free a record. */
static void
incdep_free_rec (struct incdep *cur, void *rec)
{
/*alloccache_free (&incdep_rec_caches[cur->worker_tid], rec); - doesn't work of course. */
}
/* grow a cache. */
static void *
incdep_cache_allocator (void *thrd, unsigned int size)
{
(void)thrd;
#ifdef __APPLE__
return malloc_zone_malloc (incdep_zone, size);
#else
return xmalloc (size);
#endif
}
/* term a cache. */
static void
incdep_cache_deallocator (void *thrd, void *ptr, unsigned int size)
{
(void)thrd;
(void)size;
free (ptr);
}
/* acquires the lock */
void
incdep_lock(void)
{
#if defined (HAVE_PTHREAD) && !defined (CONFIG_WITHOUT_THREADS)
pthread_mutex_lock (&incdep_mtx);
#elif defined (WINDOWS32)
EnterCriticalSection (&incdep_mtx);
#elif defined (__OS2__)
_fmutex_request (&incdep_mtx, 0);
#endif
}
/* releases the lock */
void
incdep_unlock(void)
{
#if defined (HAVE_PTHREAD) && !defined (CONFIG_WITHOUT_THREADS)
pthread_mutex_unlock (&incdep_mtx);
#elif defined(WINDOWS32)
LeaveCriticalSection (&incdep_mtx);
#elif defined(__OS2__)
_fmutex_release (&incdep_mtx);
#endif
}
/* signals the main thread that there is stuff todo. caller owns the lock. */
static void
incdep_signal_done (void)
{
#if defined (HAVE_PTHREAD) && !defined (CONFIG_WITHOUT_THREADS)
pthread_cond_broadcast (&incdep_cond_done);
#elif defined (WINDOWS32)
if (incdep_hev_done_waiters)
SetEvent (incdep_hev_done);
#elif defined (__OS2__)
if (incdep_hev_done_waiters)
DosPostEventSem (incdep_hev_done);
#endif
}
/* waits for a reader to finish reading. caller owns the lock. */
static void
incdep_wait_done (void)
{
#if defined (HAVE_PTHREAD) && !defined (CONFIG_WITHOUT_THREADS)
pthread_cond_wait (&incdep_cond_done, &incdep_mtx);
#elif defined (WINDOWS32)
ResetEvent (incdep_hev_done);
incdep_hev_done_waiters++;
incdep_unlock ();
WaitForSingleObject (incdep_hev_done, INFINITE);
incdep_lock ();
incdep_hev_done_waiters--;
#elif defined (__OS2__)
ULONG ulIgnore;
DosResetEventSem (incdep_hev_done, &ulIgnore);
incdep_hev_done_waiters++;
incdep_unlock ();
DosWaitEventSem (incdep_hev_done, SEM_INDEFINITE_WAIT);
incdep_lock ();
incdep_hev_done_waiters--;
#endif
}
/* signals the worker threads. caller owns the lock. */
static void
incdep_signal_todo (void)
{
#if defined (HAVE_PTHREAD) && !defined (CONFIG_WITHOUT_THREADS)
pthread_cond_broadcast (&incdep_cond_todo);
#elif defined(WINDOWS32)
if (incdep_hev_todo_waiters)
SetEvent (incdep_hev_todo);
#elif defined(__OS2__)
if (incdep_hev_todo_waiters)
DosPostEventSem (incdep_hev_todo);
#endif
}
/* waits for stuff to arrive in the todo list. caller owns the lock. */
static void
incdep_wait_todo (void)
{
#if defined (HAVE_PTHREAD) && !defined (CONFIG_WITHOUT_THREADS)
pthread_cond_wait (&incdep_cond_todo, &incdep_mtx);
#elif defined (WINDOWS32)
ResetEvent (incdep_hev_todo);
incdep_hev_todo_waiters++;
incdep_unlock ();
WaitForSingleObject (incdep_hev_todo, INFINITE);
incdep_lock ();
incdep_hev_todo_waiters--;
#elif defined (__OS2__)
ULONG ulIgnore;
DosResetEventSem (incdep_hev_todo, &ulIgnore);
incdep_hev_todo_waiters++;
incdep_unlock ();
DosWaitEventSem (incdep_hev_todo, SEM_INDEFINITE_WAIT);
incdep_lock ();
incdep_hev_todo_waiters--;
#endif
}
/* Reads a dep file into memory. */
static int
incdep_read_file (struct incdep *cur, struct floc *f)
{
int fd;
struct stat st;
errno = 0;
#ifdef O_BINARY
fd = open (cur->name, O_RDONLY | O_BINARY, 0);
#else
fd = open (cur->name, O_RDONLY, 0);
#endif
if (fd < 0)
{
/* ignore non-existing dependency files. */
int err = errno;
if (err == ENOENT || stat (cur->name, &st) != 0)
return 1;
error (f, "%s: %s", cur->name, strerror (err));
return -1;
}
if (!fstat (fd, &st))
{
cur->file_base = incdep_xmalloc (cur, st.st_size + 1);
if (read (fd, cur->file_base, st.st_size) == st.st_size)
{
close (fd);
cur->file_end = cur->file_base + st.st_size;
cur->file_base[st.st_size] = '\0';
return 0;
}
/* bail out */
error (f, "%s: read: %s", cur->name, strerror (errno));
incdep_xfree (cur, cur->file_base);
}
else
error (f, "%s: fstat: %s", cur->name, strerror (errno));
close (fd);
cur->file_base = cur->file_end = NULL;
return -1;
}
/* Free the incdep structure. */
static void
incdep_freeit (struct incdep *cur)
{
#ifdef PARSE_IN_WORKER
assert (!cur->recorded_variables_in_set_head);
assert (!cur->recorded_variable_defs_head);
assert (!cur->recorded_file_head);
#endif
incdep_xfree (cur, cur->file_base);
cur->next = NULL;
free (cur);
}
/* A worker thread. */
void
incdep_worker (int thrd)
{
incdep_lock ();
while (!incdep_terminate)
{
/* get job from the todo list. */
struct incdep *cur = incdep_head_todo;
if (!cur)
{
incdep_wait_todo ();
continue;
}
if (cur->next)
incdep_head_todo = cur->next;
else
incdep_head_todo = incdep_tail_todo = NULL;
incdep_num_reading++;
/* read the file. */
incdep_unlock ();
cur->worker_tid = thrd;
incdep_read_file (cur, NILF);
#ifdef PARSE_IN_WORKER
eval_include_dep_file (cur, NILF);
#endif
cur->worker_tid = -1;
incdep_lock ();
/* insert finished job into the done list. */
incdep_num_reading--;
cur->next = NULL;
if (incdep_tail_done)
incdep_tail_done->next = cur;
else
incdep_head_done = cur;
incdep_tail_done = cur;
incdep_signal_done ();
}
incdep_unlock ();
}
/* Thread library specific thread functions wrapping incdep_wroker. */
#ifdef HAVE_PTHREAD
static void *
incdep_worker_pthread (void *thrd)
{
incdep_worker ((size_t)thrd);
return NULL;
}
#elif defined (WINDOWS32)
static unsigned __stdcall
incdep_worker_windows (void *thrd)
{
incdep_worker ((size_t)thrd);
return 0;
}
#elif defined (__OS2__)
static void
incdep_worker_os2 (void *thrd)
{
incdep_worker ((size_t)thrd);
}
#endif
/* Checks if threads are enabled or not.
This is a special hack so that is possible to disable the threads when in a
debian fakeroot environment. Thus, in addition to the KMK_THREADS_DISABLED
and KMK_THREADS_ENABLED environment variable check we also check for signs
of fakeroot. */
static int
incdep_are_threads_enabled (void)
{
#if defined (CONFIG_WITHOUT_THREADS)
return 0;
#endif
/* Generic overrides. */
if (getenv ("KMK_THREADS_DISABLED"))
{
message (1, "Threads disabled (environment)");
return 0;
}
if (getenv ("KMK_THREADS_ENABLED"))
return 1;
#if defined (__gnu_linux__) || defined (__linux__)
/* Try detect fakeroot. */
if (getenv ("FAKEROOTKEY")
|| getenv ("FAKEROOTUID")
|| getenv ("FAKEROOTGID")
|| getenv ("FAKEROOTEUID")
|| getenv ("FAKEROOTEGID")
|| getenv ("FAKEROOTSUID")
|| getenv ("FAKEROOTSGID")
|| getenv ("FAKEROOTFUID")
|| getenv ("FAKEROOTFGID")
|| getenv ("FAKEROOTDONTTRYCHOWN")
|| getenv ("FAKEROOT_FD_BASE")
|| getenv ("FAKEROOT_DB_SEARCH_PATHS"))
{
message (1, "Threads disabled (fakeroot)");
return 0;
}
/* LD_PRELOAD could indicate undetected debian fakeroot or some
other ingenius library which cannot deal correctly with threads. */
if (getenv ("LD_PRELOAD"))
{
message (1, "Threads disabled (LD_PRELOAD)");
return 0;
}
#elif defined(__APPLE__) \
|| defined(__sun__) || defined(__SunOS__) || defined(__sun) || defined(__SunOS) \
|| defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) \
|| defined(__HAIKU__)
/* No broken preload libraries known to be in common use on these platforms... */
#elif defined(_MSC_VER) || defined(_WIN32) || defined(__OS2__)
/* No preload mess to care about. */
#else
# error "Add your self to the appropriate case above and send a patch to bird."
#endif
return 1;
}
/* Creates the the worker threads. */
static void
incdep_init (struct floc *f)
{
unsigned i;
#if defined (HAVE_PTHREAD) && !defined (CONFIG_WITHOUT_THREADS)
int rc;
pthread_attr_t attr;
#elif defined (WINDOWS32)
unsigned tid;
uintptr_t hThread;
#elif defined (__OS2__)
int rc;
int tid;
#endif
(void)f;
/* heap hacks */
#ifdef __APPLE__
incdep_zone = malloc_create_zone (0, 0);
if (!incdep_zone)
incdep_zone = malloc_default_zone ();
#endif
/* create the mutex and two condition variables / event objects. */
#if defined (HAVE_PTHREAD) && !defined (CONFIG_WITHOUT_THREADS)
rc = pthread_mutex_init (&incdep_mtx, NULL);
if (rc)
fatal (f, _("pthread_mutex_init failed: err=%d"), rc);
rc = pthread_cond_init (&incdep_cond_todo, NULL);
if (rc)
fatal (f, _("pthread_cond_init failed: err=%d"), rc);
rc = pthread_cond_init (&incdep_cond_done, NULL);
if (rc)
fatal (f, _("pthread_cond_init failed: err=%d"), rc);
#elif defined (WINDOWS32)
InitializeCriticalSection (&incdep_mtx);
incdep_hev_todo = CreateEvent (NULL, TRUE /*bManualReset*/, FALSE /*bInitialState*/, NULL);
if (!incdep_hev_todo)
fatal (f, _("CreateEvent failed: err=%d"), GetLastError());
incdep_hev_done = CreateEvent (NULL, TRUE /*bManualReset*/, FALSE /*bInitialState*/, NULL);
if (!incdep_hev_done)
fatal (f, _("CreateEvent failed: err=%d"), GetLastError());
incdep_hev_todo_waiters = 0;
incdep_hev_done_waiters = 0;
#elif defined (__OS2__)
_fmutex_create (&incdep_mtx, 0);
rc = DosCreateEventSem (NULL, &incdep_hev_todo, 0, FALSE);
if (rc)
fatal (f, _("DosCreateEventSem failed: rc=%d"), rc);
rc = DosCreateEventSem (NULL, &incdep_hev_done, 0, FALSE);
if (rc)
fatal (f, _("DosCreateEventSem failed: rc=%d"), rc);
incdep_hev_todo_waiters = 0;
incdep_hev_done_waiters = 0;
#endif
/* create the worker threads and associated per thread data. */
incdep_terminate = 0;
if (incdep_are_threads_enabled())
{
incdep_num_threads = sizeof (incdep_threads) / sizeof (incdep_threads[0]);
if (incdep_num_threads + 1 > job_slots)
incdep_num_threads = job_slots <= 1 ? 1 : job_slots - 1;
for (i = 0; i < incdep_num_threads; i++)
{
/* init caches */
unsigned rec_size = sizeof (struct incdep_variable_in_set);
if (rec_size < sizeof (struct incdep_variable_def))
rec_size = sizeof (struct incdep_variable_def);
if (rec_size < sizeof (struct incdep_recorded_file))
rec_size = sizeof (struct incdep_recorded_file);
alloccache_init (&incdep_rec_caches[i], rec_size, "incdep rec",
incdep_cache_allocator, (void *)(size_t)i);
alloccache_init (&incdep_dep_caches[i], sizeof(struct dep), "incdep dep",
incdep_cache_allocator, (void *)(size_t)i);
strcache2_init (&incdep_dep_strcaches[i],
"incdep dep", /* name */
65536, /* hash size */
0, /* default segment size*/
#ifdef HAVE_CASE_INSENSITIVE_FS
1, /* case insensitive */
#else
0, /* case insensitive */
#endif
0); /* thread safe */
strcache2_init (&incdep_var_strcaches[i],
"incdep var", /* name */
32768, /* hash size */
0, /* default segment size*/
0, /* case insensitive */
0); /* thread safe */
/* create the thread. */
#if defined (HAVE_PTHREAD) && !defined (CONFIG_WITHOUT_THREADS)
rc = pthread_attr_init (&attr);
if (rc)
fatal (f, _("pthread_attr_init failed: err=%d"), rc);
/*rc = pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE); */
rc = pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
if (rc)
fatal (f, _("pthread_attr_setdetachstate failed: err=%d"), rc);
rc = pthread_create(&incdep_threads[i], &attr,
incdep_worker_pthread, (void *)(size_t)i);
if (rc)
fatal (f, _("pthread_mutex_init failed: err=%d"), rc);
pthread_attr_destroy (&attr);
#elif defined (WINDOWS32)
tid = 0;
hThread = _beginthreadex (NULL, 128*1024, incdep_worker_windows,
(void *)i, 0, &tid);
if (hThread == 0 || hThread == ~(uintptr_t)0)
fatal (f, _("_beginthreadex failed: err=%d"), errno);
incdep_threads[i] = (HANDLE)hThread;
#elif defined (__OS2__)
tid = _beginthread (incdep_worker_os2, NULL, 128*1024, (void *)i);
if (tid <= 0)
fatal (f, _("_beginthread failed: err=%d"), errno);
incdep_threads[i] = tid;
#endif
}
}
else
incdep_num_threads = 0;
incdep_initialized = 1;
}
/* Flushes outstanding work and terminates the worker threads.
This is called from snap_deps(). */
void
incdep_flush_and_term (void)
{
unsigned i;
if (!incdep_initialized)
return;
/* flush any out standing work */
incdep_flush_it (NILF);
/* tell the threads to terminate */
incdep_lock ();
incdep_terminate = 1;
incdep_signal_todo ();
incdep_unlock ();
/* wait for the threads to quit */
for (i = 0; i < incdep_num_threads; i++)
{
/* more later? */
/* terminate or join up the allocation caches. */
alloccache_term (&incdep_rec_caches[i], incdep_cache_deallocator, (void *)(size_t)i);
alloccache_join (&dep_cache, &incdep_dep_caches[i]);
strcache2_term (&incdep_dep_strcaches[i]);
strcache2_term (&incdep_var_strcaches[i]);
}
incdep_num_threads = 0;
/* destroy the lock and condition variables / event objects. */
/* later */
incdep_initialized = 0;
}
#ifdef PARSE_IN_WORKER
/* Flushes a strcache entry returning the actual string cache entry.
The input is freed! */
static const char *
incdep_flush_strcache_entry (struct strcache2_entry *entry)
{
if (!entry->user)
entry->user = (void *) strcache2_add_hashed_file (&file_strcache,
(const char *)(entry + 1),
entry->length, entry->hash);
return (const char *)entry->user;
}
/* Flushes the recorded instructions. */
static void
incdep_flush_recorded_instructions (struct incdep *cur)
{
struct incdep_variable_in_set *rec_vis;
struct incdep_variable_def *rec_vd;
struct incdep_recorded_file *rec_f;
/* define_variable_in_set */
rec_vis = cur->recorded_variables_in_set_head;
cur->recorded_variables_in_set_head = cur->recorded_variables_in_set_tail = NULL;
if (rec_vis)
do
{
void *free_me = rec_vis;
unsigned int name_length = rec_vis->name_entry->length;
define_variable_in_set (incdep_flush_strcache_entry (rec_vis->name_entry),
name_length,
rec_vis->value,
rec_vis->value_length,
rec_vis->duplicate_value,
rec_vis->origin,
rec_vis->recursive,
rec_vis->set,
rec_vis->flocp);
rec_vis = rec_vis->next;
incdep_free_rec (cur, free_me);
}
while (rec_vis);
/* do_variable_definition */
rec_vd = cur->recorded_variable_defs_head;
cur->recorded_variable_defs_head = cur->recorded_variable_defs_tail = NULL;
if (rec_vd)
do
{
void *free_me = rec_vd;
do_variable_definition_2 (rec_vd->flocp,
incdep_flush_strcache_entry (rec_vd->name_entry),
rec_vd->value,
rec_vd->value_length,
0,
rec_vd->value,
rec_vd->origin,
rec_vd->flavor,
rec_vd->target_var);
rec_vd = rec_vd->next;
incdep_free_rec (cur, free_me);
}
while (rec_vd);
/* record_files */
rec_f = cur->recorded_file_head;
cur->recorded_file_head = cur->recorded_file_tail = NULL;
if (rec_f)
do
{
void *free_me = rec_f;
struct dep *dep;
for (dep = rec_f->deps; dep; dep = dep->next)
dep->name = incdep_flush_strcache_entry ((struct strcache2_entry *)dep->name);
incdep_commit_recorded_file (incdep_flush_strcache_entry (rec_f->filename_entry),
rec_f->deps,
rec_f->flocp);
rec_f = rec_f->next;
incdep_free_rec (cur, free_me);
}
while (rec_f);
}
#endif /* PARSE_IN_WORKER */
/* Record / issue a warning about a misformed dep file. */
static void
incdep_warn (struct incdep *cur, unsigned int line_no, const char *msg)
{
if (cur->worker_tid == -1)
error (NILF, "%s(%d): %s", cur->name, line_no, msg);
#ifdef PARSE_IN_WORKER
else
{
cur->err_line_no = line_no;
cur->err_msg = msg;
}
#endif
}
/* Dependency or file strcache allocation / recording. */
static const char *
incdep_dep_strcache (struct incdep *cur, const char *str, int len)
{
const char *ret;
if (cur->worker_tid == -1)
{
/* Make sure the string is terminated before we hand it to
strcache_add_len so it does have to make a temporary copy
of it on the stack. */
char ch = str[len];
((char *)str)[len] = '\0';
ret = strcache_add_len (str, len);
((char *)str)[len] = ch;
}
else
{
/* Add it out the strcache of the thread. */
ret = strcache2_add (&incdep_dep_strcaches[cur->worker_tid], str, len);
ret = (const char *)strcache2_get_entry(&incdep_dep_strcaches[cur->worker_tid], ret);
}
return ret;
}
/* Variable name allocation / recording. */
static const char *
incdep_var_strcache (struct incdep *cur, const char *str, int len)
{
const char *ret;
if (cur->worker_tid == -1)
{
/* XXX: we're leaking this memory now! This will be fixed later. */
ret = xmalloc (len + 1);
memcpy ((char *)ret, str, len);
((char *)ret)[len] = '\0';
}
else
{
/* Add it out the strcache of the thread. */
ret = strcache2_add (&incdep_var_strcaches[cur->worker_tid], str, len);
ret = (const char *)strcache2_get_entry(&incdep_var_strcaches[cur->worker_tid], ret);
}
return ret;
}
/* Record / perform a variable definition in a set.
The NAME is in the string cache.
The VALUE is on the heap.
The DUPLICATE_VALUE is always 0. */
static void
incdep_record_variable_in_set (struct incdep *cur,
const char *name, unsigned int name_length,
const char *value,
unsigned int value_length,
int duplicate_value,
enum variable_origin origin,
int recursive,
struct variable_set *set,
const struct floc *flocp)
{
assert (!duplicate_value);
if (cur->worker_tid == -1)
define_variable_in_set (name, name_length, value, value_length,
duplicate_value, origin, recursive, set, flocp);
#ifdef PARSE_IN_WORKER
else
{
struct incdep_variable_in_set *rec =
(struct incdep_variable_in_set *)incdep_alloc_rec (cur);
rec->name_entry = (struct strcache2_entry *)name;
rec->value = value;
rec->value_length = value_length;
rec->duplicate_value = duplicate_value;
rec->origin = origin;
rec->recursive = recursive;
rec->set = set;
rec->flocp = flocp;
rec->next = NULL;
if (cur->recorded_variables_in_set_tail)
cur->recorded_variables_in_set_tail->next = rec;
else
cur->recorded_variables_in_set_head = rec;
cur->recorded_variables_in_set_tail = rec;
}
#endif
}
/* Record / perform a variable definition. The VALUE should be disposed of. */
static void
incdep_record_variable_def (struct incdep *cur,
const struct floc *flocp,
const char *name,
unsigned int name_length,
char *value,
unsigned int value_length,
enum variable_origin origin,
enum variable_flavor flavor,
int target_var)
{
if (cur->worker_tid == -1)
do_variable_definition_2 (flocp, name, value, value_length, 0, value,
origin, flavor, target_var);
#ifdef PARSE_IN_WORKER
else
{
struct incdep_variable_def *rec =
(struct incdep_variable_def *)incdep_alloc_rec (cur);
rec->flocp = flocp;
rec->name_entry = (struct strcache2_entry *)name;
rec->value = value;
rec->value_length = value_length;
rec->origin = origin;
rec->flavor = flavor;
rec->target_var = target_var;
rec->next = NULL;
if (cur->recorded_variable_defs_tail)
cur->recorded_variable_defs_tail->next = rec;
else
cur->recorded_variable_defs_head = rec;
cur->recorded_variable_defs_tail = rec;
}
#else
(void)name_length;
#endif
}
/* Similar to record_files in read.c, only much much simpler. */
static void
incdep_commit_recorded_file (const char *filename, struct dep *deps,
const struct floc *flocp)
{
struct file *f;
/* Perform some validations. */
if (filename[0] == '.'
&& ( streq(filename, ".POSIX")
|| streq(filename, ".EXPORT_ALL_VARIABLES")
|| streq(filename, ".INTERMEDIATE")
|| streq(filename, ".LOW_RESOLUTION_TIME")
|| streq(filename, ".NOTPARALLEL")
|| streq(filename, ".ONESHELL")
|| streq(filename, ".PHONY")
|| streq(filename, ".PRECIOUS")
|| streq(filename, ".SECONDARY")
|| streq(filename, ".SECONDTARGETEXPANSION")
|| streq(filename, ".SILENT")
|| streq(filename, ".SHELLFLAGS")
|| streq(filename, ".SUFFIXES")
)
)
{
error (flocp, _("reserved filename '%s' used in dependency file, ignored"), filename);
return;
}
/* Lookup or create an entry in the database. */
f = enter_file (filename);
if (f->double_colon)
{
error (flocp, _("dependency file '%s' has a double colon entry already, ignoring"), filename);
return;
}
f->is_target = 1;
/* Append dependencies. */
deps = enter_prereqs (deps, NULL);
if (deps)
{
struct dep *last = f->deps;
if (!last)
f->deps = deps;
else
{
while (last->next)
last = last->next;
last->next = deps;
}
}
}
/* Record a file.*/
static void
incdep_record_file (struct incdep *cur,
const char *filename,
struct dep *deps,
const struct floc *flocp)
{
if (cur->worker_tid == -1)
incdep_commit_recorded_file (filename, deps, flocp);
#ifdef PARSE_IN_WORKER
else
{
struct incdep_recorded_file *rec =
(struct incdep_recorded_file *) incdep_alloc_rec (cur);
rec->filename_entry = (struct strcache2_entry *)filename;
rec->deps = deps;
rec->flocp = flocp;
rec->next = NULL;
if (cur->recorded_file_tail)
cur->recorded_file_tail->next = rec;
else
cur->recorded_file_head = rec;
cur->recorded_file_tail = rec;
}
#endif
}
/* no nonsense dependency file including.
Because nobody wants bogus dependency files to break their incremental
builds with hard to comprehend error messages, this function does not
use the normal eval routine but does all the parsing itself. This isn't,
as much work as it sounds, because the necessary feature set is very
limited.
eval_include_dep_file groks:
define var
endef
var [|:|?|>]= value [\]
[\]
file: [deps] [\]
*/
static void
eval_include_dep_file (struct incdep *curdep, struct floc *f)
{
unsigned line_no = 1;
const char *file_end = curdep->file_end;
const char *cur = curdep->file_base;
const char *endp;
/* if no file data, just return immediately. */
if (!cur)
return;
/* now parse the file. */
while (cur < file_end)
{
/* skip empty lines */
while (cur < file_end && isspace ((unsigned char)*cur) && *cur != '\n')
++cur;
if (cur >= file_end)
break;
if (*cur == '#')
{
cur = memchr (cur, '\n', file_end - cur);
if (!cur)
break;
}
if (*cur == '\\')
{
unsigned eol_len = (file_end - cur > 1 && cur[1] == '\n') ? 2
: (file_end - cur > 2 && cur[1] == '\r' && cur[2] == '\n') ? 3
: (file_end - cur == 1) ? 1 : 0;
if (eol_len)
{
cur += eol_len;
line_no++;
continue;
}
}
if (*cur == '\n')
{
cur++;
line_no++;
continue;
}
/* define var
...
endef */
if (strneq (cur, "define ", 7))
{
const char *var;
unsigned var_len;
const char *value_start;
const char *value_end;
char *value;
unsigned value_len;
int found_endef = 0;
/* extract the variable name. */
cur += 7;
while (isblank ((unsigned char)*cur))
++cur;
value_start = endp = memchr (cur, '\n', file_end - cur);
if (!endp)
endp = cur;
while (endp > cur && isspace ((unsigned char)endp[-1]))
--endp;
var_len = endp - cur;
if (!var_len)
{
incdep_warn (curdep, line_no, "bogus define statement.");
break;
}
var = incdep_var_strcache (curdep, cur, var_len);
/* find the end of the variable. */
cur = value_end = value_start = value_start + 1;
++line_no;
while (cur < file_end)
{
/* check for endef, don't bother with skipping leading spaces. */
if ( file_end - cur >= 5
&& strneq (cur, "endef", 5))
{
endp = cur + 5;
while (endp < file_end && isspace ((unsigned char)*endp) && *endp != '\n')
endp++;
if (endp >= file_end || *endp == '\n')
{
found_endef = 1;
cur = endp >= file_end ? file_end : endp + 1;
break;
}
}
/* skip a line ahead. */
cur = value_end = memchr (cur, '\n', file_end - cur);
if (cur != NULL)
++cur;
else
cur = value_end = file_end;
++line_no;
}
if (!found_endef)
{
incdep_warn (curdep, line_no, "missing endef, dropping the rest of the file.");
break;
}
value_len = value_end - value_start;
if (memchr (value_start, '\0', value_len))
{
incdep_warn (curdep, line_no, "'\\0' in define, dropping the rest of the file.");
break;
}
/* make a copy of the value, converting \r\n to \n, and define it. */
value = incdep_xmalloc (curdep, value_len + 1);
endp = memchr (value_start, '\r', value_len);
if (endp)
{
const char *src = value_start;
char *dst = value;
for (;;)
{
size_t len = endp - src;
memcpy (dst, src, len);
dst += len;
src = endp;
if (src + 1 < file_end && src[1] == '\n')
src++; /* skip the '\r' */
if (src >= value_end)
break;
endp = memchr (endp + 1, '\r', src - value_end);
if (!endp)
endp = value_end;
}
value_len = dst - value;
}
else
memcpy (value, value_start, value_len);
value [value_len] = '\0';
incdep_record_variable_in_set (curdep,
var, var_len, value, value_len,
0 /* don't duplicate */, o_file,
0 /* defines are recursive but this is faster */,
NULL /* global set */, f);
}
/* file: deps
OR
variable [:]= value */
else
{
const char *colonp;
const char *equalp;
/* Look for a colon and an equal sign, optimize for colon.
Only one file is support and the colon / equal must be on
the same line. */
colonp = memchr (cur, ':', file_end - cur);
#ifdef HAVE_DOS_PATHS
while ( colonp
&& colonp + 1 < file_end
&& (colonp[1] == '/' || colonp[1] == '\\')
&& colonp > cur
&& isalpha ((unsigned char)colonp[-1])
&& ( colonp == cur + 1
|| strchr (" \t(", colonp[-2]) != 0))
colonp = memchr (colonp + 1, ':', file_end - (colonp + 1));
#endif
endp = NULL;
if ( !colonp
|| (endp = memchr (cur, '\n', colonp - cur)))
{
colonp = NULL;
equalp = memchr (cur, '=', (endp ? endp : file_end) - cur);
if ( !equalp
|| (!endp && memchr (cur, '\n', equalp - cur)))
{
incdep_warn (curdep, line_no, "no colon.");
break;
}
}
else
equalp = memchr (cur, '=', (colonp + 2 <= file_end
? colonp + 2 : file_end) - cur);
if (equalp)
{
/* An assignment of some sort. */
const char *var;
unsigned var_len;
const char *value_start;
const char *value_end;
char *value;
unsigned value_len;
unsigned multi_line = 0;
enum variable_flavor flavor;
/* figure the flavor first. */
flavor = f_recursive;
if (equalp > cur)
{
if (equalp[-1] == ':')
flavor = f_simple;
else if (equalp[-1] == '?')
flavor = f_conditional;
else if (equalp[-1] == '+')
flavor = f_append;
else if (equalp[-1] == '>')
flavor = f_prepend;
}
/* extract the variable name. */
endp = flavor == f_recursive ? equalp : equalp - 1;
while (endp > cur && isblank ((unsigned char)endp[-1]))
--endp;
var_len = endp - cur;
if (!var_len)
{
incdep_warn (curdep, line_no, "empty variable. (includedep)");
break;
}
if ( memchr (cur, '$', var_len)
|| memchr (cur, ' ', var_len)
|| memchr (cur, '\t', var_len))
{
incdep_warn (curdep, line_no, "fancy variable name. (includedep)");
break;
}
var = incdep_var_strcache (curdep, cur, var_len);
/* find the start of the value. */
cur = equalp + 1;
while (cur < file_end && isblank ((unsigned char)*cur))
cur++;
value_start = cur;
/* find the end of the value / line (this isn't 101% correct). */
value_end = cur;
while (cur < file_end)
{
endp = value_end = memchr (cur, '\n', file_end - cur);
if (!value_end)
value_end = file_end;
if (value_end - 1 >= cur && value_end[-1] == '\r')
--value_end;
if (value_end - 1 < cur || value_end[-1] != '\\')
{
cur = endp ? endp + 1 : file_end;
break;
}
--value_end;
if (value_end - 1 >= cur && value_end[-1] == '\\')
{
incdep_warn (curdep, line_no, "fancy escaping! (includedep)");
cur = NULL;
break;
}
if (!endp)
{
cur = file_end;
break;
}
cur = endp + 1;
++multi_line;
++line_no;
}
if (!cur)
break;
++line_no;
/* make a copy of the value, converting \r\n to \n, and define it. */
value_len = value_end - value_start;
value = incdep_xmalloc (curdep, value_len + 1);
if (!multi_line)
memcpy (value, value_start, value_len);
else
{
/* unescape it */
const char *src = value_start;
char *dst = value;
while (src < value_end)
{
const char *nextp;
endp = memchr (src, '\n', value_end - src);
if (!endp)
nextp = endp = value_end;
else
nextp = endp + 1;
if (endp > src && endp[-1] == '\r')
--endp;
if (endp > src && endp[-1] == '\\')
--endp;
if (src != value_start)
*dst++ = ' ';
memcpy (dst, src, endp - src);
dst += endp - src;
src = nextp;
}
value_len = dst - value;
}
value [value_len] = '\0';
/* do the definition */
if (flavor == f_recursive
|| ( flavor == f_simple
&& !memchr (value, '$', value_len)))
incdep_record_variable_in_set (curdep,
var, var_len, value, value_len,
0 /* don't duplicate */, o_file,
flavor == f_recursive /* recursive */,
NULL /* global set */, f);
else
incdep_record_variable_def (curdep,
f, var, var_len, value, value_len,
o_file, flavor, 0 /* not target var */);
}
else
{
/* file: dependencies */
const char *filename;
struct dep *deps = 0;
struct dep **nextdep = &deps;
struct dep *dep;
/* extract the filename, ASSUME a single one. */
endp = colonp;
while (endp > cur && isblank ((unsigned char)endp[-1]))
--endp;
if (cur == endp)
{
incdep_warn (curdep, line_no, "empty filename.");
break;
}
if ( memchr (cur, '$', endp - cur)
|| memchr (cur, ' ', endp - cur)
|| memchr (cur, '\t', endp - cur))
{
incdep_warn (curdep, line_no, "multiple / fancy file name. (includedep)");
break;
}
filename = incdep_dep_strcache (curdep, cur, endp - cur);
/* parse any dependencies. */
cur = colonp + 1;
while (cur < file_end)
{
/* skip blanks and count lines. */
while (cur < file_end && isspace ((unsigned char)*cur) && *cur != '\n')
++cur;
if (cur >= file_end)
break;
if (*cur == '\n')
{
cur++;
line_no++;
break;
}
/* continuation + eol? */
if (*cur == '\\')
{
unsigned eol_len = (file_end - cur > 1 && cur[1] == '\n') ? 2
: (file_end - cur > 2 && cur[1] == '\r' && cur[2] == '\n') ? 3
: (file_end - cur == 1) ? 1 : 0;
if (eol_len)
{
cur += eol_len;
line_no++;
continue;
}
}
/* find the end of the filename */
endp = cur;
while (endp < file_end && !isspace ((unsigned char)*endp))
++endp;
/* add it to the list. */
*nextdep = dep = incdep_alloc_dep (curdep);
dep->name = incdep_dep_strcache (curdep, cur, endp - cur);
dep->includedep = 1;
nextdep = &dep->next;
cur = endp;
}
/* enter the file with its dependencies. */
incdep_record_file (curdep, filename, deps, f);
}
}
}
/* free the file data */
incdep_xfree (curdep, curdep->file_base);
curdep->file_base = curdep->file_end = NULL;
}
/* Flushes the incdep todo and done lists. */
static void
incdep_flush_it (struct floc *f)
{
incdep_lock ();
for (;;)
{
struct incdep *cur = incdep_head_done;
/* if the done list is empty, grab a todo list entry. */
if (!cur && incdep_head_todo)
{
cur = incdep_head_todo;
if (cur->next)
incdep_head_todo = cur->next;
else
incdep_head_todo = incdep_tail_todo = NULL;
incdep_unlock ();
incdep_read_file (cur, f);
eval_include_dep_file (cur, f);
incdep_freeit (cur);
incdep_lock ();
continue;
}
/* if the todo list and done list are empty we're either done
or will have to wait for the thread(s) to finish. */
if (!cur && !incdep_num_reading)
break; /* done */
if (!cur)
{
while (!incdep_head_done)
incdep_wait_done ();
cur = incdep_head_done;
}
/* we grab the entire done list and work thru it. */
incdep_head_done = incdep_tail_done = NULL;
incdep_unlock ();
while (cur)
{
struct incdep *next = cur->next;
#ifdef PARSE_IN_WORKER
incdep_flush_recorded_instructions (cur);
#else
eval_include_dep_file (cur, f);
#endif
incdep_freeit (cur);
cur = next;
}
incdep_lock ();
} /* outer loop */
incdep_unlock ();
}
/* splits up a list of file names and feeds it to eval_include_dep_file,
employing threads to try speed up the file reading. */
void
eval_include_dep (const char *names, struct floc *f, enum incdep_op op)
{
struct incdep *head = 0;
struct incdep *tail = 0;
struct incdep *cur;
const char *names_iterator = names;
const char *name;
unsigned int name_len;
/* loop through NAMES, creating a todo list out of them. */
while ((name = find_next_token (&names_iterator, &name_len)) != 0)
{
cur = xmalloc (sizeof (*cur) + name_len); /* not incdep_xmalloc here */
cur->file_base = cur->file_end = NULL;
memcpy (cur->name, name, name_len);
cur->name[name_len] = '\0';
cur->worker_tid = -1;
#ifdef PARSE_IN_WORKER
cur->err_line_no = 0;
cur->err_msg = NULL;
cur->recorded_variables_in_set_head = NULL;
cur->recorded_variables_in_set_tail = NULL;
cur->recorded_variable_defs_head = NULL;
cur->recorded_variable_defs_tail = NULL;
cur->recorded_file_head = NULL;
cur->recorded_file_tail = NULL;
#endif
cur->next = NULL;
if (tail)
tail->next = cur;
else
head = cur;
tail = cur;
}
#ifdef ELECTRIC_HEAP
if (1)
#else
if (op == incdep_read_it)
#endif
{
/* work our way thru the files directly */
cur = head;
while (cur)
{
struct incdep *next = cur->next;
incdep_read_file (cur, f);
eval_include_dep_file (cur, f);
incdep_freeit (cur);
cur = next;
}
}
else
{
/* initialize the worker threads and related stuff the first time around. */
if (!incdep_initialized)
incdep_init (f);
/* queue the files and notify the worker threads. */
incdep_lock ();
if (incdep_tail_todo)
incdep_tail_todo->next = head;
else
incdep_head_todo = head;
incdep_tail_todo = tail;
incdep_signal_todo ();
incdep_unlock ();
/* flush the todo queue if we're requested to do so. */
if (op == incdep_flush)
incdep_flush_it (f);
}
}
#endif /* CONFIG_WITH_INCLUDEDEP */
|