2 * Copyright (c) 2019 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * This code uses concepts and configuration based on 'synth', by
8 * John R. Marino <draco@marino.st>, which was written in ada.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
20 * 3. Neither the name of The DragonFly Project nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific, prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #define PKG_HSIZE 32768
41 #define PKG_HMASK 32767
43 static void childGetPackageInfo(bulk_t *bulk);
44 static void childGetBinaryDistInfo(bulk_t *bulk);
45 static void childOptimizeEnv(bulk_t *bulk);
46 static pkg_t *resolveDeps(pkg_t *dep_list, pkg_t ***list_tailp, int gentopo);
47 static void resolveDepString(pkg_t *pkg, char *depstr,
48 int gentopo, int dep_type);
49 static pkg_t *processPackageListBulk(int total);
50 static int scan_and_queue_dir(const char *path, const char *level1, int level);
51 static int scan_binary_repo(const char *path);
53 static void pkgfree(pkg_t *pkg);
56 pkg_t *PkgHash1[PKG_HSIZE]; /* by portdir */
57 pkg_t *PkgHash2[PKG_HSIZE]; /* by pkgfile */
60 * Allocate a new pkg structure plus basic initialization.
62 static __inline pkg_t *
67 pkg = calloc(1, sizeof(*pkg));
68 pkg->idepon_list.next = &pkg->idepon_list;
69 pkg->idepon_list.prev = &pkg->idepon_list;
70 pkg->deponi_list.next = &pkg->deponi_list;
71 pkg->deponi_list.prev = &pkg->deponi_list;
77 * Simple hash for lookups
80 pkghash(const char *str)
84 hv = (hv << 5) ^ *str;
87 hv = hv ^ (hv / PKG_HSIZE) ^ (hv / PKG_HSIZE / PKG_HSIZE);
88 return (hv & PKG_HMASK);
98 pkgp = &PkgHash1[pkghash(pkg->portdir)];
99 while ((scan = *pkgp) != NULL) {
100 if (strcmp(pkg->portdir, scan->portdir) == 0)
102 pkgp = &scan->hnext1;
104 if (scan && (scan->flags & PKGF_PLACEHOLD)) {
106 pkg->hnext1 = scan->hnext1;
116 pkgp = &PkgHash2[pkghash(pkg->pkgfile)];
117 while ((scan = *pkgp) != NULL) {
118 if (strcmp(pkg->pkgfile, scan->pkgfile) == 0)
120 pkgp = &scan->hnext2;
128 pkg_find(const char *match)
133 pkgp = &PkgHash1[pkghash(match)];
134 for (pkg = *pkgp; pkg; pkg = pkg->hnext1) {
135 if (strcmp(pkg->portdir, match) == 0)
138 pkgp = &PkgHash2[pkghash(match)];
139 for (pkg = *pkgp; pkg; pkg = pkg->hnext2) {
140 if (strcmp(pkg->pkgfile, match) == 0)
147 * Parse a specific list of ports via origin name (portdir/subdir)
150 ParsePackageList(int n, char **ary)
157 initbulk(childGetPackageInfo, MaxBulk);
160 * Always include ports-mgmt/pkg. A non-null s4 field just tells
161 * the processing code that this isn't a manual selection.
163 queuebulk("ports-mgmt", "pkg", NULL, "x");
165 for (i = 0; i < n; ++i) {
170 l2 = strchr(l1, '/');
173 queuebulk(l1, l2, NULL, NULL);
176 printf("Bad portdir specification: %s\n", l1);
180 printf("Processing %d ports\n", total);
182 list = processPackageListBulk(total);
188 * Parse packages from the list installed on the system.
191 GetLocalPackageList(void)
201 initbulk(childGetPackageInfo, MaxBulk);
204 fp = popen("pkg info -a -o", "r");
207 * Always include ports-mgmt/pkg. A non-null s4 field just tells
208 * the processing code that this isn't a manual selection.
210 queuebulk("ports-mgmt", "pkg", NULL, "x");
212 while ((base = fgetln(fp, &len)) != NULL) {
213 if (len == 0 || base[len-1] != '\n')
216 if (strtok(base, " \t") == NULL) {
217 printf("Badly formatted pkg info line: %s\n", base);
220 l1 = strtok(NULL, " \t");
222 printf("Badly formatted pkg info line: %s\n", base);
226 l2 = strchr(l1, '/');
229 queuebulk(l1, l2, NULL, NULL);
232 printf("Badly formatted specification: %s\n", l1);
237 printf("Processing %d ports\n", total);
239 list = processPackageListBulk(total);
245 GetFullPackageList(void)
249 initbulk(childGetPackageInfo, MaxBulk);
251 total = scan_and_queue_dir(DPortsPath, NULL, 1);
252 printf("Scanning %d ports\n", total);
254 return processPackageListBulk(total);
258 * Caller has queued the process list for bulk operation. We retrieve
259 * the results and clean up the bulk operation (we may have to do a second
260 * bulk operation so we have to be the ones to clean it up).
263 processPackageListBulk(int total)
276 while ((bulk = getbulk()) != NULL) {
278 if ((count & 255) == 0) {
280 (double)count * 100.0 / (double)total + 0.001);
284 *list_tail = bulk->list;
286 while ((scan = *list_tail) != NULL) {
287 if (bulk->s4 == NULL)
288 scan->flags |= PKGF_MANUALSEL;
290 list_tail = &scan->bnext;
295 printf("100.00%%\n");
296 printf("\nTotal %d\n", count);
300 * Resolve all dependencies for the related packages, potentially
301 * adding anything that could not be found to the list. This will
302 * continue to issue bulk operations and process the result until
303 * no dependencies are left.
305 printf("Resolving dependencies...");
309 dep_list = resolveDeps(dep_list, &list_tail, 0);
316 * Generate the topology
318 resolveDeps(list, NULL, 1);
321 * Do a final count, ignore place holders.
324 for (scan = list; scan; scan = scan->bnext) {
325 if ((scan->flags & PKGF_ERROR) == 0) {
329 printf("Total Returned %d\n", count);
332 * Scan our binary distributions and related dependencies looking
333 * for any packages that have already been built.
335 initbulk(childGetBinaryDistInfo, MaxBulk);
336 total = scan_binary_repo(RepositoryPath);
338 printf("Scanning %d packages\n", total);
340 while ((bulk = getbulk()) != NULL) {
342 if ((count & 255) == 0) {
344 (double)count * 100.0 / (double)total + 0.001);
349 printf("100.00%%\n");
350 printf("\nTotal %d\n", count);
354 printf("all done\n");
360 GetPkgPkg(pkg_t *list)
365 for (scan = list; scan; scan = scan->bnext) {
366 if (strcmp(scan->portdir, "ports-mgmt/pkg") == 0)
371 * This will force pkg to be built, but generally this code
372 * is not reached because the package list processing code
373 * adds ports-mgmt/pkg unconditionally.
375 initbulk(childGetPackageInfo, MaxBulk);
376 queuebulk("ports-mgmt", "pkg", NULL, "x");
378 dassert(bulk, "Cannot find ports-mgmt/pkg");
388 * Try to optimize the environment by supplying information that
389 * the ports system would generally have to run stuff to get on
392 * See childOptimizeEnv() for the actual handling. We execute
393 * a single make -V... -V... for ports-mgmt/pkg from within the
394 * bulk system (which handles the environment and disables
395 * /etc/make.conf), and we then call addbuildenv() as appropriate.
405 initbulk(childOptimizeEnv, MaxBulk);
406 queuebulk("ports-mgmt", "pkg", NULL, NULL);
413 * Run through the list resolving dependencies and constructing the topology
414 * linkages. This may append packages to the list.
417 resolveDeps(pkg_t *list, pkg_t ***list_tailp, int gentopo)
420 pkg_t *ret_list = NULL;
423 for (scan = list; scan; scan = scan->bnext) {
424 resolveDepString(scan, scan->fetch_deps,
425 gentopo, DEP_TYPE_FETCH);
426 resolveDepString(scan, scan->ext_deps,
427 gentopo, DEP_TYPE_EXT);
428 resolveDepString(scan, scan->patch_deps,
429 gentopo, DEP_TYPE_PATCH);
430 resolveDepString(scan, scan->build_deps,
431 gentopo, DEP_TYPE_BUILD);
432 resolveDepString(scan, scan->lib_deps,
433 gentopo, DEP_TYPE_LIB);
434 resolveDepString(scan, scan->run_deps,
435 gentopo, DEP_TYPE_RUN);
439 * No bulk ops are queued when doing the final topology
444 while ((bulk = getbulk()) != NULL) {
446 if (ret_list == NULL)
447 ret_list = bulk->list;
448 **list_tailp = bulk->list;
450 while (**list_tailp) {
451 pkg_enter(**list_tailp);
452 *list_tailp = &(**list_tailp)->bnext;
461 resolveDepString(pkg_t *pkg, char *depstr, int gentopo, int dep_type)
471 if (depstr == NULL || depstr[0] == 0)
474 copy_base = strdup(depstr);
479 dep = strsep(©, " \t");
480 } while (dep && *dep == 0);
485 * Ignore dependencies prefixed with ${NONEXISTENT}
487 if (strncmp(dep, "/nonexistent:", 13) == 0)
490 dep = strchr(dep, ':');
491 if (dep == NULL || *dep != ':') {
492 printf("Error parsing dependency for %s: %s\n",
493 pkg->portdir, copy_base);
499 * Strip-off any DPortsPath prefix. EXTRACT_DEPENDS
500 * often (always?) generates this prefix.
502 if (strncmp(dep, DPortsPath, strlen(DPortsPath)) == 0) {
503 dep += strlen(DPortsPath);
509 * Strip-off any tag (such as :patch). We don't try to
510 * organize dependencies at this fine a grain (for now).
512 tag = strchr(dep, ':');
517 * Locate the dependency
519 if ((dpkg = pkg_find(dep)) != NULL) {
524 * NOTE: idep_count is calculated recursively
527 ddprintf(0, "Add Dependency %s -> %s\n",
528 pkg->portdir, dpkg->portdir);
529 link = calloc(1, sizeof(*link));
531 link->next = &pkg->idepon_list;
532 link->prev = pkg->idepon_list.prev;
533 link->next->prev = link;
534 link->prev->next = link;
535 link->dep_type = dep_type;
537 link = calloc(1, sizeof(*link));
539 link->next = &dpkg->deponi_list;
540 link->prev = dpkg->deponi_list.prev;
541 link->next->prev = link;
542 link->prev->next = link;
543 link->dep_type = dep_type;
550 * This shouldn't happen because we already took a first
551 * pass and should have generated the pkgs.
554 printf("Topology Generate failed for %s: %s\n",
555 pkg->portdir, copy_base);
560 * Separate out the two dports directory components and
561 * extract the optional '@flavor' specification.
563 sep = strchr(dep, '/');
565 printf("Error parsing dependency for %s: %s\n",
566 pkg->portdir, copy_base);
572 flavor = strrchr(tag, '@');
574 flavor = strrchr(sep, '@');
580 ddprintf(0, "QUEUE DEPENDENCY FROM PKG %s: %s/%s@%s\n",
581 pkg->portdir, dep, sep, flavor);
583 ddprintf(0, "QUEUE DEPENDENCY FROM PKG %s: %s/%s\n",
584 pkg->portdir, dep, sep);
587 * Use a place-holder to prevent duplicate dependencies from
588 * being processed. The placeholder will be replaced by
589 * the actual dependency.
593 asprintf(&dpkg->portdir, "%s/%s@%s", dep, sep, flavor);
595 asprintf(&dpkg->portdir, "%s/%s", dep, sep);
596 dpkg->flags = PKGF_PLACEHOLD;
599 queuebulk(dep, sep, flavor, NULL);
605 FreePackageList(pkg_t *pkgs __unused)
607 dfatal("not implemented");
611 * Scan some or all dports to allocate the related pkg structure. Dependencies
612 * are stored but not processed.
617 childGetPackageInfo(bulk_t *bulk)
631 const char *cav[MAXCAC];
636 * If the package has flavors we will loop on each one. If a flavor
637 * is not passed in s3 we will loop on all flavors, otherwise we will
638 * only process the passed-in flavor.
640 flavor = bulk->s3; /* usually NULL */
646 list_tail = &bulk->list;
648 asprintf(&portpath, "%s/%s/%s", DPortsPath, bulk->s1, bulk->s2);
650 asprintf(&flavarg, "FLAVOR=%s", flavor);
655 cav[cac++] = MAKE_BINARY;
657 cav[cac++] = portpath;
659 cav[cac++] = flavarg;
660 cav[cac++] = "-VPKGVERSION";
661 cav[cac++] = "-VPKGFILE:T";
662 cav[cac++] = "-VDISTFILES";
663 cav[cac++] = "-VDIST_SUBDIR";
664 cav[cac++] = "-VMAKE_JOBS_NUMBER";
665 cav[cac++] = "-VIGNORE";
666 cav[cac++] = "-VFETCH_DEPENDS";
667 cav[cac++] = "-VEXTRACT_DEPENDS";
668 cav[cac++] = "-VPATCH_DEPENDS";
669 cav[cac++] = "-VBUILD_DEPENDS";
670 cav[cac++] = "-VLIB_DEPENDS";
671 cav[cac++] = "-VRUN_DEPENDS";
672 cav[cac++] = "-VSELECTED_OPTIONS";
673 cav[cac++] = "-VDESELECTED_OPTIONS";
674 cav[cac++] = "-VUSE_LINUX";
675 cav[cac++] = "-VFLAVORS";
676 cav[cac++] = "-VUSES";
678 fp = dexec_open(cav, cac, &pid, 1, 1);
684 asprintf(&pkg->portdir, "%s/%s@%s", bulk->s1, bulk->s2, flavor);
686 asprintf(&pkg->portdir, "%s/%s", bulk->s1, bulk->s2);
689 while ((ptr = fgetln(fp, &len)) != NULL) {
690 if (len == 0 || ptr[len-1] != '\n') {
691 dfatal("Bad package info for %s/%s response line %d",
692 bulk->s1, bulk->s2, line);
697 case 1: /* PKGVERSION */
698 asprintf(&pkg->version, "%s", ptr);
700 case 2: /* PKGFILE */
701 asprintf(&pkg->pkgfile, "%s", ptr);
703 case 3: /* DISTFILES */
704 asprintf(&pkg->distfiles, "%s", ptr);
706 case 4: /* DIST_SUBDIR */
707 pkg->distsubdir = strdup_or_null(ptr);
709 case 5: /* MAKE_JOBS_NUMBER */
710 pkg->make_jobs_number = strtol(ptr, NULL, 0);
713 pkg->ignore = strdup_or_null(ptr);
715 case 7: /* FETCH_DEPENDS */
716 pkg->fetch_deps = strdup_or_null(ptr);
718 case 8: /* EXTRACT_DEPENDS */
719 pkg->ext_deps = strdup_or_null(ptr);
721 case 9: /* PATCH_DEPENDS */
722 pkg->patch_deps = strdup_or_null(ptr);
724 case 10: /* BUILD_DEPENDS */
725 pkg->build_deps = strdup_or_null(ptr);
727 case 11: /* LIB_DEPENDS */
728 pkg->lib_deps = strdup_or_null(ptr);
730 case 12: /* RUN_DEPENDS */
731 pkg->run_deps = strdup_or_null(ptr);
733 case 13: /* SELECTED_OPTIONS */
734 pkg->pos_options = strdup_or_null(ptr);
736 case 14: /* DESELECTED_OPTIONS */
737 pkg->neg_options = strdup_or_null(ptr);
739 case 15: /* USE_LINUX */
743 case 16: /* FLAVORS */
744 asprintf(&pkg->flavors, "%s", ptr);
747 asprintf(&pkg->uses, "%s", ptr);
748 if (strstr(pkg->uses, "metaport"))
749 pkg->flags |= PKGF_META;
752 printf("EXTRA LINE: %s\n", ptr);
758 printf("DPort not found: %s/%s\n", bulk->s1, bulk->s2);
759 pkg->flags |= PKGF_NOTFOUND;
760 } else if (line != 17 + 1) {
761 printf("DPort corrupt: %s/%s\n", bulk->s1, bulk->s2);
762 pkg->flags |= PKGF_CORRUPT;
764 if (dexec_close(fp, pid)) {
765 printf("make -V* command for %s/%s failed\n",
767 pkg->flags |= PKGF_CORRUPT;
774 if (flavor == NULL) {
776 * If there are flavors add the current unflavored pkg
777 * as a dummy node so dependencies can attach to it,
778 * then iterate the first flavor and loop.
780 * We must NULL out pkgfile because it will have the
781 * default flavor and conflict with the actual flavored
784 if (pkg->flavors && pkg->flavors[0]) {
787 pkg->flags |= PKGF_DUMMY;
789 freestrp(&pkg->fetch_deps);
790 freestrp(&pkg->ext_deps);
791 freestrp(&pkg->patch_deps);
792 freestrp(&pkg->build_deps);
793 freestrp(&pkg->lib_deps);
794 freestrp(&pkg->run_deps);
796 freestrp(&pkg->pkgfile);
799 list_tail = &(*list_tail)->bnext;
801 flavors_save = strdup(pkg->flavors);
802 flavors = flavors_save;
804 flavor = strsep(&flavors, " \t");
805 } while (flavor && *flavor == 0);
810 * No flavors, add the current unflavored pkg as a real
815 list_tail = &(*list_tail)->bnext;
818 * Add flavored package and iterate.
822 list_tail = &(*list_tail)->bnext;
825 * Flavor iteration under dummy node, add dependency
830 ddprintf(0, "Add Dependency %s -> %s (flavor rollup)\n",
831 dummy_node->portdir, pkg->portdir);
832 link = calloc(1, sizeof(*link));
834 link->next = &dummy_node->idepon_list;
835 link->prev = dummy_node->idepon_list.prev;
836 link->next->prev = link;
837 link->prev->next = link;
838 link->dep_type = DEP_TYPE_BUILD;
840 link = calloc(1, sizeof(*link));
841 link->pkg = dummy_node;
842 link->next = &pkg->deponi_list;
843 link->prev = pkg->deponi_list.prev;
844 link->next->prev = link;
845 link->prev->next = link;
846 link->dep_type = DEP_TYPE_BUILD;
852 flavor = strsep(&flavors, " \t");
853 } while (flavor && *flavor == 0);
862 * Query the package (at least to make sure it hasn't been truncated)
863 * and mark it as PACKAGED if found.
868 childGetBinaryDistInfo(bulk_t *bulk)
874 const char *cav[MAXCAC];
880 asprintf(&repopath, "%s/%s", RepositoryPath, bulk->s1);
883 cav[cac++] = PKG_BINARY;
884 cav[cac++] = "query";
886 cav[cac++] = repopath;
887 cav[cac++] = "%n-%v";
889 fp = dexec_open(cav, cac, &pid, 1, 0);
891 while ((ptr = fgetln(fp, &len)) != NULL) {
892 if (len == 0 || ptr[len-1] != '\n')
895 snprintf(buf, sizeof(buf), "%s%s", ptr, USE_PKG_SUFX);
899 pkg->flags |= PKGF_PACKAGED;
901 ddprintf(0, "Note: package scan, not in list, "
902 "skipping %s\n", buf);
905 if (dexec_close(fp, pid)) {
906 printf("pkg query command failed for %s\n", repopath);
912 childOptimizeEnv(bulk_t *bulk)
919 const char *cav[MAXCAC];
923 asprintf(&portpath, "%s/%s/%s", DPortsPath, bulk->s1, bulk->s2);
926 cav[cac++] = MAKE_BINARY;
928 cav[cac++] = portpath;
929 cav[cac++] = "-V_PERL5_FROM_BIN";
931 fp = dexec_open(cav, cac, &pid, 1, 1);
935 while ((ptr = fgetln(fp, &len)) != NULL) {
936 if (len == 0 || ptr[len-1] != '\n') {
937 dfatal("Bad package info for %s/%s response line %d",
938 bulk->s1, bulk->s2, line);
943 case 1: /* _PERL5_FROM_BIN */
944 addbuildenv("_PERL5_FROM_BIN", ptr, BENV_ENVIRONMENT);
947 printf("childOptimizeEnv: EXTRA LINE: %s\n", ptr);
953 printf("DPort not found: %s/%s\n", bulk->s1, bulk->s2);
954 } else if (line != 1 + 1) {
955 printf("DPort corrupt: %s/%s\n", bulk->s1, bulk->s2);
957 if (dexec_close(fp, pid)) {
958 printf("childOptimizeEnv() failed\n");
963 scan_and_queue_dir(const char *path, const char *level1, int level)
973 dassert(dir, "Cannot open dports path \"%s\"", path);
975 while ((den = readdir(dir)) != NULL) {
976 if (den->d_namlen == 1 && den->d_name[0] == '.')
978 if (den->d_namlen == 2 &&
979 den->d_name[0] == '.' && den->d_name[1] == '.')
981 asprintf(&s1, "%s/%s", path, den->d_name);
982 if (lstat(s1, &st) < 0 || !S_ISDIR(st.st_mode)) {
987 count += scan_and_queue_dir(s1, den->d_name, 2);
991 asprintf(&s2, "%s/Makefile", s1);
992 if (lstat(s2, &st) == 0) {
993 queuebulk(level1, den->d_name, NULL, NULL);
1005 scan_binary_repo(const char *path)
1013 dir = opendir(path);
1014 dassert(dir, "Cannot open repository path \"%s\"", path);
1017 * NOTE: Test includes the '.' in the suffix.
1019 while ((den = readdir(dir)) != NULL) {
1020 len = strlen(den->d_name);
1022 strcmp(den->d_name + len - 4, USE_PKG_SUFX) == 0) {
1023 queuebulk(den->d_name, NULL, NULL, NULL);
1036 freestrp(&pkg->portdir);
1037 freestrp(&pkg->version);
1038 freestrp(&pkg->pkgfile);
1039 freestrp(&pkg->ignore);
1040 freestrp(&pkg->fetch_deps);
1041 freestrp(&pkg->ext_deps);
1042 freestrp(&pkg->patch_deps);
1043 freestrp(&pkg->build_deps);
1044 freestrp(&pkg->lib_deps);
1045 freestrp(&pkg->run_deps);
1046 freestrp(&pkg->pos_options);
1047 freestrp(&pkg->neg_options);
1048 freestrp(&pkg->flavors);