-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.c
3336 lines (3056 loc) · 82 KB
/
build.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
/*
* Copyright (c) 2019-2022 The DragonFly Project. All rights reserved.
*
* This code is derived from software contributed to The DragonFly Project
* by Matthew Dillon <[email protected]>
*
* This code uses concepts and configuration based on 'synth', by
* John R. Marino <[email protected]>, which was written in ada.
*
* 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 DragonFly Project 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 COPYRIGHT HOLDERS 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
* COPYRIGHT HOLDERS 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 <netdb.h>
#include "dsynth.h"
#include "compat.h"
static worker_t WorkerAry[MAXWORKERS];
static int BuildInitialized;
static int RunningWorkers;
int DynamicMaxWorkers;
static int FailedWorkers;
static long RunningPkgDepSize;
static pthread_mutex_t DbmMutex;
static pthread_mutex_t WorkerMutex;
static pthread_cond_t WorkerCond;
static int build_find_leaves(pkg_t *parent, pkg_t *pkg,
pkg_t ***build_tailp, int *app, int *hasworkp,
int depth, int first, int first_one_only);
static int buildCalculateDepiDepth(pkg_t *pkg);
static void build_clear_trav(pkg_t *pkg);
static void startbuild(pkg_t **build_listp, pkg_t ***build_tailp);
static int qsort_depi(const void *pkg1, const void *pkg2);
static int qsort_idep(const void *pkg1, const void *pkg2);
static void startworker(pkg_t *pkg, worker_t *work);
static void cleanworker(worker_t *work);
static void waitbuild(int whilematch, int dynamicmax);
static void workercomplete(worker_t *work);
static void *childBuilderThread(void *arg);
static int childInstallPkgDeps(worker_t *work);
static size_t childInstallPkgDeps_recurse(FILE *fp, pkglink_t *list,
int undoit, int depth, int first_one_only);
static void dophase(worker_t *work, wmsg_t *wmsg,
int wdog, int phaseid, const char *phase);
static void phaseReapAll(void);
static void phaseTerminateSignal(int sig);
static char *buildskipreason(pkglink_t *parent, pkg_t *pkg);
static int buildskipcount_dueto(pkg_t *pkg, int mode);
static int mptylogpoll(int ptyfd, int fdlog, wmsg_t *wmsg,
time_t *wdog_timep);
static void doHook(pkg_t *pkg, const char *id, const char *path, int waitfor);
static void childHookRun(bulk_t *bulk);
static void adjloadavg(double *dload);
static void check_packaged(const char *dbmpath, pkg_t *pkgs);
static worker_t *SigWork;
static int MasterPtyFd = -1;
static int CopyFileFd = -1;
static pid_t SigPid;
static const char *WorkerFlavorPrt = ""; /* "" or "@flavor" */
static DBM *CheckDBM;
#define MPTY_FAILED -2
#define MPTY_AGAIN -1
#define MPTY_EOF 0
#define MPTY_DATA 1
int BuildTotal;
int BuildCount;
int BuildSkipCount;
int BuildIgnoreCount;
int BuildFailCount;
int BuildSuccessCount;
int BuildMissingCount;
int BuildMetaCount;
int PkgVersionPkgSuffix;
/*
* Initialize the WorkerAry[]
*/
void
DoInitBuild(int slot_override)
{
worker_t *work;
struct stat st;
char *path;
int i;
ddassert(slot_override < 0 || MaxWorkers == 1);
bzero(WorkerAry, MaxWorkers * sizeof(worker_t));
pthread_mutex_init(&WorkerMutex, NULL);
pthread_mutex_init(&DbmMutex, NULL);
for (i = 0; i < MaxWorkers; ++i) {
work = &WorkerAry[i];
work->index = (slot_override >= 0) ? slot_override : i;
work->state = WORKER_NONE;
asprintf(&work->basedir, "%s/SL%02d", BuildBase, work->index);
pthread_cond_init(&work->cond, NULL);
}
BuildCount = 0;
/*
* Create required sub-directories. The base directories must already
* exist as a dsynth configuration safety.
*/
if (stat(RepositoryPath, &st) < 0) {
if (mkdir(RepositoryPath, 0755) < 0)
dfatal("Cannot mkdir %s\n", RepositoryPath);
}
/*
* An empty directory for LOCALBASE= in the pkg scan.
*/
asprintf(&path, "%s/empty", BuildBase);
if (stat(path, &st) < 0) {
if (mkdir(path, 0755) < 0)
dfatal("Cannot mkdir %s\n", path);
}
free(path);
BuildInitialized = 1;
/*
* slow-start (increases at a rate of 1 per 5 seconds)
*/
if (SlowStartOpt > MaxWorkers)
DynamicMaxWorkers = MaxWorkers;
else if (SlowStartOpt > 0)
DynamicMaxWorkers = SlowStartOpt;
else
DynamicMaxWorkers = MaxWorkers;
}
/*
* Called by the frontend to clean-up any hanging mounts.
*/
void
DoCleanBuild(int resetlogs)
{
int i;
ddassert(BuildInitialized);
if (resetlogs)
dlogreset();
for (i = 0; i < MaxWorkers; ++i) {
DoWorkerUnmounts(&WorkerAry[i]);
}
}
void
DoBuild(pkg_t *pkgs)
{
pkg_t *build_list = NULL;
pkg_t **build_tail = &build_list;
pkg_t *scan;
bulk_t *bulk;
int haswork = 1;
int first = 1;
int newtemplate;
time_t startTime;
time_t t;
int h, m, s;
char *dbmpath;
/*
* We use our bulk system to run hooks. This will be for
* Skipped and Ignored. Success and Failure are handled by
* WorkerProcess() which is a separately-exec'd dsynth.
*/
if (UsingHooks)
initbulk(childHookRun, MaxBulk);
/*
* Count up the packages, not counting dummies
*/
for (scan = pkgs; scan; scan = scan->bnext) {
if ((scan->flags & PKGF_DUMMY) == 0)
++BuildTotal;
}
/*
* Remove binary package files for ports whos directory
* has changed.
*/
asprintf(&dbmpath, "%s/ports_crc", BuildBase);
CheckDBM = dbm_open(dbmpath, O_CREAT|O_RDWR, 0644);
check_packaged(dbmpath, pkgs);
doHook(NULL, "hook_run_start", HookRunStart, 1);
/*
* The pkg and pkg-static binaries are needed. If already present
* then assume that the template is also valid, otherwise add to
* the list and build both.
*/
scan = GetPkgPkg(&pkgs);
/*
* Create our template. The template will be missing pkg
* and pkg-static.
*/
if (FetchOnlyOpt) {
newtemplate = DoCreateTemplate(0);
} else if ((scan->flags & (PKGF_SUCCESS | PKGF_PACKAGED)) == 0) {
/* force a fresh template */
newtemplate = DoCreateTemplate(1);
} else {
newtemplate = DoCreateTemplate(0);
}
/*
* This will clear the screen and set-up our gui, so sleep
* a little first in case the user wants to see what was
* printed before.
*/
sleep(2);
pthread_mutex_lock(&WorkerMutex);
startTime = time(NULL);
RunStatsInit();
RunStatsReset();
/*
* Build pkg/pkg-static
*/
if ((scan->flags & (PKGF_SUCCESS | PKGF_PACKAGED)) == 0 && FetchOnlyOpt == 0) {
build_list = scan;
build_tail = &scan->build_next;
startbuild(&build_list, &build_tail);
while (RunningWorkers == 1)
waitbuild(1, 0);
if (scan->flags & PKGF_NOBUILD)
dfatal("Unable to build 'pkg'");
if (scan->flags & PKGF_ERROR)
dfatal("Error building 'pkg'");
if ((scan->flags & PKGF_SUCCESS) == 0)
dfatal("Error building 'pkg'");
newtemplate = 1;
}
/*
* Install pkg/pkg-static into the template
*/
if (newtemplate && FetchOnlyOpt == 0) {
char *buf;
int rc;
asprintf(&buf,
"cd %s/Template; "
"tar --exclude '+*' --exclude '*/man/*' "
"-xvzpf %s/%s > /dev/null 2>&1",
BuildBase,
RepositoryPath,
scan->pkgfile);
rc = system(buf);
if (rc)
dfatal("Command failed: %s\n", buf);
freestrp(&buf);
}
/*
* Figure out pkg version
*/
if (scan->version) {
int v1;
int v2;
dlog(DLOG_ALL, "[XXX] pkg version %s\n", scan->version);
if (sscanf(scan->version, "%d.%d", &v1, &v2) == 2) {
if ((v1 == 1 && v2 >= 17) || v1 > 1)
PkgVersionPkgSuffix = 1;
}
}
/*
* Calculate depi_depth, the longest chain of dependencies
* for who depends on me, weighted by powers of two.
*/
for (scan = pkgs; scan; scan = scan->bnext) {
buildCalculateDepiDepth(scan);
}
/*
* Nominal bulk build sequence
*/
while (haswork) {
haswork = 0;
fflush(stdout);
for (scan = pkgs; scan; scan = scan->bnext) {
ddprintf(0, "SCANLEAVES %08x %s\n",
scan->flags, scan->portdir);
scan->flags |= PKGF_BUILDLOOP;
/*
* NOTE: We must still find dependencies if PACKAGED
* to fill in the gaps, as some of them may
* need to be rebuilt.
*/
if (scan->flags & (PKGF_SUCCESS | PKGF_FAILURE |
PKGF_ERROR)) {
#if 0
ddprintf(0, "%s: already built\n",
scan->portdir);
#endif
} else {
int ap = 0;
build_find_leaves(NULL, scan, &build_tail,
&ap, &haswork, 0, first, 0);
ddprintf(0, "TOPLEVEL %s %08x\n",
scan->portdir, ap);
}
scan->flags &= ~PKGF_BUILDLOOP;
build_clear_trav(scan);
}
first = 0;
fflush(stdout);
startbuild(&build_list, &build_tail);
if (haswork == 0 && RunningWorkers) {
waitbuild(RunningWorkers, 1);
haswork = 1;
}
}
pthread_mutex_unlock(&WorkerMutex);
/*
* What is left that cannot be built? The list really ought to be
* empty at this point, report anything that remains.
*/
for (scan = pkgs; scan; scan = scan->bnext) {
if (scan->flags & (PKGF_SUCCESS | PKGF_FAILURE))
continue;
dlog(DLOG_ABN, "[XXX] %s lost in the ether [flags=%08x]\n",
scan->portdir, scan->flags);
++BuildMissingCount;
}
/*
* Final updates
*/
RunStatsUpdateTop(0);
RunStatsUpdateLogs();
RunStatsSync();
RunStatsDone();
doHook(NULL, "hook_run_end", HookRunEnd, 1);
if (UsingHooks) {
while ((bulk = getbulk()) != NULL)
freebulk(bulk);
donebulk();
}
t = time(NULL) - startTime;
h = t / 3600;
m = t / 60 % 60;
s = t % 60;
if (CheckDBM) {
dbm_close(CheckDBM);
CheckDBM = NULL;
}
dlog(DLOG_ALL|DLOG_STDOUT,
"\n"
"Initial queue size: %d\n"
" packages built: %d\n"
" ignored: %d\n"
" skipped: %d\n"
" failed: %d\n"
" missing: %d\n"
" meta-nodes: %d\n"
"\n"
"Duration: %02d:%02d:%02d\n"
"\n",
BuildTotal,
BuildSuccessCount,
BuildIgnoreCount,
BuildSkipCount,
BuildFailCount,
BuildMissingCount,
BuildMetaCount,
h, m, s);
}
/*
* Traverse the packages (pkg) depends on recursively until we find
* a leaf to build or report as unbuildable. Calculates and assigns a
* dependency count. Returns all parallel-buildable packages.
*
* (pkg) itself is only added to the list if it is immediately buildable.
*/
static
int
build_find_leaves(pkg_t *parent, pkg_t *pkg, pkg_t ***build_tailp,
int *app, int *hasworkp, int depth, int first,
int first_one_only)
{
pkglink_t *link;
pkg_t *scan;
int idep_count = 0;
int apsub;
int dfirst_one_only;
int ndepth;
char *buf;
ndepth = depth + 1;
/*
* Already on build list, possibly in-progress, tell caller that
* it is not ready.
*/
ddprintf(ndepth, "sbuild_find_leaves %d %s %08x {\n",
depth, pkg->portdir, pkg->flags);
if (pkg->flags & PKGF_BUILDLIST) {
ddprintf(ndepth, "} (already on build list)\n");
*app |= PKGF_NOTREADY;
return (pkg->idep_count);
}
/*
* Check dependencies
*/
PKGLIST_FOREACH(link, &pkg->idepon_list) {
scan = link->pkg;
if (scan == NULL) {
if (first_one_only)
break;
continue;
}
ddprintf(ndepth, "check %s %08x\t", scan->portdir, scan->flags);
/*
* If this dependency is to a DUMMY node it is a dependency
* only on the default flavor which is only the first node
* under this one, not all of them.
*
* DUMMY nodes can be marked SUCCESS so the build skips past
* them, but this doesn't mean that their sub-nodes succeeded.
* We have to check, so recurse even if it is marked
* successful.
*
* NOTE: The depth is not being for complex dependency type
* tests like it is in childInstallPkgDeps_recurse(),
* so we don't have to hicup it like we do in that
* procedure.
*/
dfirst_one_only = (scan->flags & PKGF_DUMMY) ? 1 : 0;
if (dfirst_one_only)
goto skip_to_flavor;
/*
* When accounting for a successful build, just bump
* idep_count by one. scan->idep_count will heavily
* overlap packages that we count down multiple branches.
*
* We must still recurse through PACKAGED packages as
* some of their dependencies might be missing.
*/
if (scan->flags & PKGF_SUCCESS) {
ddprintf(0, "SUCCESS - OK\n");
++idep_count;
if (first_one_only)
break;
continue;
}
/*
* ERROR includes FAILURE, which is set in numerous situations
* including when NOBUILD state is finally processed. So
* check for NOBUILD state first.
*
* An ERROR in a sub-package causes a NOBUILD in packages
* that depend on it.
*/
if (scan->flags & PKGF_NOBUILD) {
ddprintf(0, "NOBUILD - OK "
"(propagate failure upward)\n");
*app |= PKGF_NOBUILD_S;
if (first_one_only)
break;
continue;
}
if (scan->flags & PKGF_ERROR) {
ddprintf(0, "ERROR - OK (propagate failure upward)\n");
*app |= PKGF_NOBUILD_S;
if (first_one_only)
break;
continue;
}
/*
* If already on build-list this dependency is not ready.
*/
if (scan->flags & PKGF_BUILDLIST) {
ddprintf(0, " [BUILDLIST]");
*app |= PKGF_NOTREADY;
}
/*
* If not packaged this dependency is not ready for
* the caller.
*/
if ((scan->flags & PKGF_PACKAGED) == 0) {
ddprintf(0, " [NOT_PACKAGED]");
*app |= PKGF_NOTREADY;
}
/*
* Reduce search complexity, if we have already processed
* scan in the traversal it will either already be on the
* build list or it will not be buildable. Either way
* the parent is not buildable.
*/
if (scan->flags & PKGF_BUILDTRAV) {
ddprintf(0, " [BUILDTRAV]\n");
*app |= PKGF_NOTREADY;
if (first_one_only)
break;
continue;
}
skip_to_flavor:
/*
* Assert on dependency loop
*/
++idep_count;
if (scan->flags & PKGF_BUILDLOOP) {
dfatal("pkg dependency loop %s -> %s",
parent->portdir, scan->portdir);
}
/*
* NOTE: For debug tabbing purposes we use (ndepth + 1)
* here (i.e. depth + 2) in our iteration.
*/
scan->flags |= PKGF_BUILDLOOP;
apsub = 0;
ddprintf(0, " SUBRECURSION {\n");
idep_count += build_find_leaves(pkg, scan, build_tailp,
&apsub, hasworkp,
ndepth + 1, first,
dfirst_one_only);
scan->flags &= ~PKGF_BUILDLOOP;
*app |= apsub;
if (apsub & PKGF_NOBUILD) {
ddprintf(ndepth, "} (sub-nobuild)\n");
} else if (apsub & PKGF_ERROR) {
ddprintf(ndepth, "} (sub-error)\n");
} else if (apsub & PKGF_NOTREADY) {
ddprintf(ndepth, "} (sub-notready)\n");
} else {
ddprintf(ndepth, "} (sub-ok)\n");
}
if (first_one_only)
break;
}
pkg->idep_count = idep_count;
pkg->flags |= PKGF_BUILDTRAV;
/*
* Incorporate scan results into pkg state.
*/
if ((pkg->flags & PKGF_NOBUILD) == 0 && (*app & PKGF_NOBUILD)) {
*hasworkp = 1;
} else if ((pkg->flags & PKGF_ERROR) == 0 && (*app & PKGF_ERROR)) {
*hasworkp = 1;
}
pkg->flags |= *app & ~PKGF_NOTREADY;
/*
* Clear the PACKAGED bit if sub-dependencies aren't clean.
*
* NOTE: PKGF_NOTREADY is not stored in pkg->flags, only in *app,
* so incorporate *app to test for it.
*/
if ((pkg->flags & PKGF_PACKAGED) &&
((pkg->flags | *app) & (PKGF_NOTREADY|PKGF_ERROR|PKGF_NOBUILD))) {
pkg->flags &= ~PKGF_PACKAGED;
ddassert(pkg->pkgfile);
asprintf(&buf, "%s/%s", RepositoryPath, pkg->pkgfile);
if (OverridePkgDeleteOpt >= 1) {
pkg->flags |= PKGF_PACKAGED;
dlog(DLOG_ALL,
"[XXX] %s DELETE-PACKAGE %s "
"(OVERRIDE, NOT DELETED)\n",
pkg->portdir, buf);
} else if (remove(buf) < 0) {
dlog(DLOG_ALL,
"[XXX] %s DELETE-PACKAGE %s (failed)\n",
pkg->portdir, buf);
} else {
dlog(DLOG_ALL,
"[XXX] %s DELETE-PACKAGE %s "
"(due to dependencies)\n",
pkg->portdir, buf);
}
freestrp(&buf);
*hasworkp = 1;
}
/*
* Set PKGF_NOBUILD_I if there is IGNORE data
*/
if (pkg->ignore) {
pkg->flags |= PKGF_NOBUILD_I;
}
/*
* Handle propagated flags
*/
if (pkg->flags & PKGF_ERROR) {
/*
* This can only happen if the ERROR has already been
* processed and accounted for.
*/
ddprintf(depth, "} (ERROR - %s)\n", pkg->portdir);
} else if (*app & PKGF_NOTREADY) {
/*
* We don't set PKGF_NOTREADY in the pkg, it is strictly
* a transient flag propagated via build_find_leaves().
*
* Just don't add the package to the list.
*
* NOTE: Even if NOBUILD is set (meaning we could list it
* and let startbuild() finish it up as a skip, we
* don't process it to the list because we want to
* process all the dependencies, so someone doing a
* manual build can get more complete information and
* does not have to iterate each failed dependency one
* at a time.
*/
;
} else if (pkg->flags & PKGF_SUCCESS) {
ddprintf(depth, "} (SUCCESS - %s)\n", pkg->portdir);
} else if (pkg->flags & PKGF_DUMMY) {
/*
* Just mark dummy packages as successful when all of their
* sub-depends (flavors) complete successfully. Note that
* dummy packages are not counted in the total, so do not
* decrement BuildTotal.
*
* Do not propagate *app up for the dummy node or add it to
* the build list. The dummy node itself is not an actual
* dependency. Packages which depend on the default flavor
* (aka this dummy node) actually depend on the first flavor
* under this node.
*
* So if there is a generic dependency (i.e. no flavor
* specified), the upper recursion detects PKGF_DUMMY and
* traverses through the dummy node to the default flavor
* without checking the error/nobuild flags on this dummy
* node.
*/
if (pkg->flags & PKGF_NOBUILD) {
ddprintf(depth, "} (DUMMY/META - IGNORED "
"- MARK SUCCESS ANYWAY)\n");
} else {
ddprintf(depth, "} (DUMMY/META - SUCCESS)\n");
}
pkg->flags |= PKGF_SUCCESS;
*hasworkp = 1;
if (first) {
dlog(DLOG_ALL | DLOG_FILTER,
"[XXX] %s META-ALREADY-BUILT\n",
pkg->portdir);
} else {
dlog(DLOG_SUCC, "[XXX] %s meta-node complete\n",
pkg->portdir);
RunStatsUpdateCompletion(NULL, DLOG_SUCC, pkg, "", "");
++BuildMetaCount; /* Only for not built meta nodes */
}
} else if (pkg->flags & PKGF_PACKAGED) {
/*
* We can just mark the pkg successful. If this is
* the first pass, we count this as an initial pruning
* pass and reduce BuildTotal.
*/
ddprintf(depth, "} (PACKAGED - SUCCESS)\n");
pkg->flags |= PKGF_SUCCESS;
*hasworkp = 1;
if (first) {
dlog(DLOG_ALL | DLOG_FILTER,
"[XXX] %s Already-Built\n",
pkg->portdir);
--BuildTotal;
} else {
dlog(DLOG_ABN | DLOG_FILTER,
"[XXX] %s flags=%08x Packaged Unexpectedly\n",
pkg->portdir, pkg->flags);
/* ++BuildSuccessTotal; XXX not sure */
goto addlist;
}
} else {
/*
* All dependencies are successful, queue new work
* and indicate not-ready to the parent (since our
* package has to be built).
*
* NOTE: The NOBUILD case propagates to here as well
* and is ultimately handled by startbuild().
*/
addlist:
*hasworkp = 1;
if (pkg->flags & PKGF_NOBUILD_I)
ddprintf(depth, "} (ADDLIST(IGNORE/BROKEN) - %s)\n",
pkg->portdir);
else if (pkg->flags & PKGF_NOBUILD)
ddprintf(depth, "} (ADDLIST(NOBUILD) - %s)\n",
pkg->portdir);
else
ddprintf(depth, "} (ADDLIST - %s)\n", pkg->portdir);
pkg->flags |= PKGF_BUILDLIST;
**build_tailp = pkg;
*build_tailp = &pkg->build_next;
pkg->build_next = NULL;
*app |= PKGF_NOTREADY;
}
return idep_count;
}
static
void
build_clear_trav(pkg_t *pkg)
{
pkglink_t *link;
pkg_t *scan;
pkg->flags &= ~PKGF_BUILDTRAV;
PKGLIST_FOREACH(link, &pkg->idepon_list) {
scan = link->pkg;
if (scan && (scan->flags & PKGF_BUILDTRAV))
build_clear_trav(scan);
}
}
/*
* Calculate the longest chain of packages that depend on me. The
* long the chain, the more important my package is to build earlier
* rather than later.
*/
static int
buildCalculateDepiDepth(pkg_t *pkg)
{
pkglink_t *link;
pkg_t *scan;
int best_depth = 0;
int res;
if (pkg->depi_depth)
return(pkg->depi_depth + 1);
pkg->flags |= PKGF_BUILDLOOP;
PKGLIST_FOREACH(link, &pkg->deponi_list) {
scan = link->pkg;
if (scan && (scan->flags & PKGF_BUILDLOOP) == 0) {
res = buildCalculateDepiDepth(scan);
if (best_depth < res)
best_depth = res;
}
}
pkg->flags &= ~PKGF_BUILDLOOP;
pkg->depi_depth = best_depth;
return (best_depth + 1);
}
/*
* Take a list of pkg ready to go, sort it, and assign it to worker
* slots. This routine blocks in waitbuild() until it can dispose of
* the entire list.
*
* WorkerMutex is held by the caller.
*/
static
void
startbuild(pkg_t **build_listp, pkg_t ***build_tailp)
{
pkg_t *pkg;
pkg_t **idep_ary;
pkg_t **depi_ary;
int count;
int idep_index;
int depi_index;
int i;
int n;
worker_t *work;
static int IterateWorker;
/*
* Nothing to do
*/
if (*build_listp == NULL)
return;
/*
* Sort
*/
count = 0;
for (pkg = *build_listp; pkg; pkg = pkg->build_next)
++count;
idep_ary = calloc(count, sizeof(pkg_t *));
depi_ary = calloc(count, sizeof(pkg_t *));
count = 0;
for (pkg = *build_listp; pkg; pkg = pkg->build_next) {
idep_ary[count] = pkg;
depi_ary[count] = pkg;
++count;
}
/*
* idep_ary - sorted by #of dependencies this pkg has.
* depi_ary - sorted by #of other packages that depend on this pkg.
*/
qsort(idep_ary, count, sizeof(pkg_t *), qsort_idep);
qsort(depi_ary, count, sizeof(pkg_t *), qsort_depi);
idep_index = 0;
depi_index = 0;
/*
* Half the workers build based on the highest depi count,
* the other half build based on the highest idep count.
*
* This is an attempt to get projects which many other projects
* depend on built first, but to also try to build large projects
* (which tend to have a lot of dependencies) earlier rather than
* later so the end of the bulk run doesn't inefficiently build
* the last few huge projects.
*
* Loop until we manage to assign slots to everyone. We do not
* wait for build completion.
*
* This is the point where we handle DUMMY packages (these are
* dummy unflavored packages which 'cover' all the flavors for
* a package). These are not real packages are marked SUCCESS
* at this time because their dependencies (the flavors) have all
* been built.
*/
while (idep_index != count || depi_index != count) {
pkg_t *pkgi;
pkg_t *ipkg;
/*
* Find candidate to start sorted by depi or idep.
*/
ipkg = NULL;
while (idep_index < count) {
ipkg = idep_ary[idep_index];
if ((ipkg->flags &
(PKGF_SUCCESS | PKGF_FAILURE |
PKGF_ERROR | PKGF_RUNNING)) == 0) {
break;
}
ipkg = NULL;
++idep_index;
}
pkgi = NULL;
while (depi_index < count) {
pkgi = depi_ary[depi_index];
if ((pkgi->flags &
(PKGF_SUCCESS | PKGF_FAILURE |
PKGF_ERROR | PKGF_RUNNING)) == 0) {
break;
}
pkgi = NULL;
++depi_index;
}
/*
* ipkg and pkgi must either both be NULL, or both
* be non-NULL.
*/
if (ipkg == NULL && pkgi == NULL)
break;
ddassert(ipkg && pkgi);
/*
* Handle the NOBUILD case right here, there's no point
* queueing it anywhere.
*/
if (ipkg->flags & PKGF_NOBUILD) {
char *reason;
char skipbuf[16];
int scount;
scount = buildskipcount_dueto(ipkg, 1);
buildskipcount_dueto(ipkg, 0);
if (scount) {
snprintf(skipbuf, sizeof(skipbuf), " %d",
scount);
} else {
skipbuf[0] = 0;
}
ipkg->flags |= PKGF_FAILURE;
ipkg->flags &= ~PKGF_BUILDLIST;
reason = buildskipreason(NULL, ipkg);
if (ipkg->flags & PKGF_NOBUILD_I) {
++BuildIgnoreCount;
dlog(DLOG_IGN,
"[XXX] %s%s ignored due to %s\n",
ipkg->portdir, skipbuf, reason);
RunStatsUpdateCompletion(NULL, DLOG_IGN, ipkg,
reason, skipbuf);
doHook(ipkg, "hook_pkg_ignored",
HookPkgIgnored, 0);
} else {
++BuildSkipCount;
dlog(DLOG_SKIP,
"[XXX] %s%s skipped due to %s\n",
ipkg->portdir, skipbuf, reason);
RunStatsUpdateCompletion(NULL, DLOG_SKIP, ipkg,
reason, skipbuf);
doHook(ipkg, "hook_pkg_skipped",
HookPkgSkipped, 0);
}
free(reason);
++BuildCount;
continue;
}
if (pkgi->flags & PKGF_NOBUILD) {
char *reason;
char skipbuf[16];
int scount;
scount = buildskipcount_dueto(pkgi, 1);
buildskipcount_dueto(pkgi, 0);
if (scount) {
snprintf(skipbuf, sizeof(skipbuf), " %d",
scount);
} else {
skipbuf[0] = 0;
}
pkgi->flags |= PKGF_FAILURE;
pkgi->flags &= ~PKGF_BUILDLIST;
reason = buildskipreason(NULL, pkgi);
if (pkgi->flags & PKGF_NOBUILD_I) {
++BuildIgnoreCount;
dlog(DLOG_IGN, "[XXX] %s%s ignored due to %s\n",
pkgi->portdir, skipbuf, reason);
RunStatsUpdateCompletion(NULL, DLOG_IGN, pkgi,
reason, skipbuf);
doHook(pkgi, "hook_pkg_ignored",
HookPkgIgnored, 0);
} else {
++BuildSkipCount;
dlog(DLOG_SKIP,
"[XXX] %s%s skipped due to %s\n",
pkgi->portdir, skipbuf, reason);
RunStatsUpdateCompletion(NULL, DLOG_SKIP, pkgi,
reason, skipbuf);
doHook(pkgi, "hook_pkg_skipped",
HookPkgSkipped, 0);
}
free(reason);
++BuildCount;
continue;
}
/*
* Block while no slots are available. waitbuild()
* will clean out any DONE states.
*/
while (RunningWorkers >= DynamicMaxWorkers ||
RunningWorkers >= MaxWorkers - FailedWorkers) {
waitbuild(RunningWorkers, 1);