xref: /dragonfly/usr.bin/dsynth/dsynth.c (revision 49837aef)
1 /*
2  * Copyright (c) 2019 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * This code uses concepts and configuration based on 'synth', by
8  * John R. Marino <draco@marino.st>, which was written in ada.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
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
19  *    distribution.
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.
23  *
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
35  * SUCH DAMAGE.
36  */
37 
38 #include "dsynth.h"
39 
40 static void DoInit(void);
41 static void usage(int ecode) __dead2;
42 
43 int YesOpt;
44 int DebugOpt;
45 int ColorOpt = 1;
46 int NullStdinOpt = 1;
47 int SlowStartOpt = 1;
48 long PkgDepMemoryTarget;
49 char *DSynthExecPath;
50 
51 int
52 main(int ac, char **av)
53 {
54 	pkg_t *pkgs;
55 	int isworker;
56 	int c;
57 	int sopt;
58 
59 	/*
60 	 * Get our exec path so we can self-exec clean WORKER
61 	 * processes.
62 	 */
63 	{
64 		size_t len;
65 		const int name[] = {
66 			CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1,
67 		};
68 		if (sysctl(name, 4, NULL, &len, NULL, 0) < 0)
69 			dfatal_errno("Cannot get binary path");
70 		DSynthExecPath = malloc(len + 1);
71 		if (sysctl(name, 4, DSynthExecPath, &len, NULL, 0) < 0)
72 			dfatal_errno("Cannot get binary path");
73 		DSynthExecPath[len] = 0;
74 	}
75 
76 	/*
77 	 * Process options and make sure the directive is present
78 	 */
79 	sopt = 0;
80 	while ((c = getopt(ac, av, "dhm:vys:DPS")) != -1) {
81 		switch(c) {
82 		case 'y':
83 			++YesOpt;
84 			break;
85 		case 'D':
86 			WorkerProcFlags |= WORKER_PROC_DEVELOPER;
87 			break;
88 		case 'P':
89 			WorkerProcFlags |= WORKER_PROC_CHECK_PLIST;
90 			break;
91 		case 'S':
92 			UseNCurses = 0;
93 			if (++sopt == 2)
94 				ColorOpt = 0;
95 			break;
96 		case 'd':
97 			++DebugOpt;
98 			if (DebugOpt >= 2)
99 				UseNCurses = 0;
100 			break;
101 		case 'h':
102 			usage(0);
103 			/* NOT REACHED */
104 			exit(0);
105 		case 'v':
106 			printf("dsynth %s\n", DSYNTH_VERSION);
107 			exit(0);
108 		case 's':
109 			SlowStartOpt = strtol(optarg, NULL, 0);
110 			break;
111 		case 'm':
112 			PkgDepMemoryTarget = strtoul(optarg, NULL, 0);
113 			PkgDepMemoryTarget *= ONEGB;
114 			break;
115 		default:
116 			fprintf(stderr, "Unknown option: %c\n", c);
117 			usage(2);
118 			/* NOT REACHED */
119 			break;
120 		}
121 	}
122 	ac -= optind;
123 	av += optind;
124 	pkgs = NULL;
125 	if (ac < 1) {
126 		fprintf(stderr, "Missing directive\n");
127 		usage(2);
128 		/* NOT REACHED */
129 	}
130 
131 	if (strcmp(av[0], "init") == 0) {
132 		DoInit();
133 		exit(0);
134 	}
135 
136 	if (strcmp(av[0], "WORKER") == 0) {
137 		isworker = 1;
138 	} else {
139 		isworker = 0;
140 	}
141 
142 	/*
143 	 * Preconfiguration.
144 	 */
145 	signal(SIGPIPE, SIG_IGN);
146 	ParseConfiguration(isworker);
147 
148 	/*
149 	 * Setup some environment for bulk operations (pkglist scan).
150 	 * These are not used by the builder (the builder will replicate
151 	 * all of these).
152 	 */
153 	addbuildenv("PORTSDIR", DPortsPath,
154 		    BENV_ENVIRONMENT | BENV_PKGLIST);
155 	addbuildenv("BATCH", "yes",
156 		    BENV_ENVIRONMENT | BENV_PKGLIST);
157 	addbuildenv("PKG_SUFX", UsePkgSufx,
158 		    BENV_ENVIRONMENT | BENV_PKGLIST);
159 	addbuildenv("PACKAGE_BUILDING", "yes",
160 		    BENV_ENVIRONMENT | BENV_PKGLIST);
161 	addbuildenv("ARCH", ArchitectureName,
162 		    BENV_ENVIRONMENT | BENV_PKGLIST);
163 
164 #if 0
165 	/*
166 	 *
167 	 */
168 	addbuildenv("OSTYPE", OperatingSystemName,
169 		    BENV_ENVIRONMENT | BENV_PKGLIST);
170 	addbuildenv("MACHTYPE", MachineName,
171 		    BENV_ENVIRONMENT | BENV_PKGLIST);
172 #endif
173 
174 	/*
175 	 * Special directive for when dsynth execs itself to manage
176 	 * a worker chroot.
177 	 */
178 	if (isworker) {
179 		WorkerProcess(ac, av);
180 		exit(0);
181 	}
182 
183 	DoInitBuild(-1);
184 
185 	if (strcmp(av[0], "debug") == 0) {
186 		WorkerProcFlags |= WORKER_PROC_DEBUGSTOP;
187 		DoCleanBuild(1);
188 		OptimizeEnv();
189 		pkgs = ParsePackageList(ac - 1, av + 1);
190 		RemovePackages(pkgs);
191 		DoBuild(pkgs);
192 	} else if (strcmp(av[0], "status") == 0) {
193 		OptimizeEnv();
194 		if (ac - 1)
195 			pkgs = ParsePackageList(ac - 1, av + 1);
196 		else
197 			pkgs = GetLocalPackageList();
198 		DoStatus(pkgs);
199 	} else if (strcmp(av[0], "monitor") == 0) {
200 		char *spath;
201 		char *lpath;
202 
203 		if (ac == 1) {
204 			asprintf(&spath, "%s/%s", StatsBase, STATS_FILE);
205 			asprintf(&lpath, "%s/%s", StatsBase, STATS_LOCKFILE);
206 			MonitorDirective(spath, lpath);
207 			free(spath);
208 			free(lpath);
209 		} else {
210 			MonitorDirective(av[1], NULL);
211 		}
212 	} else if (strcmp(av[0], "cleanup") == 0) {
213 		DoCleanBuild(0);
214 	} else if (strcmp(av[0], "configure") == 0) {
215 		DoCleanBuild(0);
216 		DoConfigure();
217 	} else if (strcmp(av[0], "upgrade-system") == 0) {
218 		DoCleanBuild(1);
219 		OptimizeEnv();
220 		pkgs = GetLocalPackageList();
221 		DoBuild(pkgs);
222 		DoRebuildRepo(0);
223 		DoUpgradePkgs(pkgs, 0);
224 	} else if (strcmp(av[0], "prepare-system") == 0) {
225 		DoCleanBuild(1);
226 		OptimizeEnv();
227 		pkgs = GetLocalPackageList();
228 		DoBuild(pkgs);
229 		DoRebuildRepo(0);
230 	} else if (strcmp(av[0], "rebuild-repository") == 0) {
231 		OptimizeEnv();
232 		DoRebuildRepo(0);
233 	} else if (strcmp(av[0], "purge-distfiles") == 0) {
234 		OptimizeEnv();
235 		pkgs = GetFullPackageList();
236 		PurgeDistfiles(pkgs);
237 	} else if (strcmp(av[0], "status-everything") == 0) {
238 		OptimizeEnv();
239 		pkgs = GetFullPackageList();
240 		DoStatus(pkgs);
241 	} else if (strcmp(av[0], "everything") == 0) {
242 		if (WorkerProcFlags & WORKER_PROC_DEVELOPER)
243 			WorkerProcFlags |= WORKER_PROC_CHECK_PLIST;
244 		DoCleanBuild(1);
245 		OptimizeEnv();
246 		pkgs = GetFullPackageList();
247 		DoBuild(pkgs);
248 		DoRebuildRepo(1);
249 	} else if (strcmp(av[0], "version") == 0) {
250 		printf("dsynth %s\n", DSYNTH_VERSION);
251 		exit(0);
252 	} else if (strcmp(av[0], "help") == 0) {
253 		usage(0);
254 		/* NOT REACHED */
255 		exit(0);
256 	} else if (strcmp(av[0], "build") == 0) {
257 		DoCleanBuild(1);
258 		OptimizeEnv();
259 		pkgs = ParsePackageList(ac - 1, av + 1);
260 		DoBuild(pkgs);
261 		DoRebuildRepo(1);
262 		DoUpgradePkgs(pkgs, 1);
263 	} else if (strcmp(av[0], "just-build") == 0) {
264 		DoCleanBuild(1);
265 		OptimizeEnv();
266 		pkgs = ParsePackageList(ac - 1, av + 1);
267 		DoBuild(pkgs);
268 	} else if (strcmp(av[0], "install") == 0) {
269 		DoCleanBuild(1);
270 		OptimizeEnv();
271 		pkgs = ParsePackageList(ac - 1, av + 1);
272 		DoBuild(pkgs);
273 		DoRebuildRepo(0);
274 		DoUpgradePkgs(pkgs, 0);
275 	} else if (strcmp(av[0], "force") == 0) {
276 		DoCleanBuild(1);
277 		OptimizeEnv();
278 		pkgs = ParsePackageList(ac - 1, av + 1);
279 		RemovePackages(pkgs);
280 		DoBuild(pkgs);
281 		DoRebuildRepo(1);
282 		DoUpgradePkgs(pkgs, 1);
283 	} else if (strcmp(av[0], "test") == 0) {
284 		WorkerProcFlags |= WORKER_PROC_CHECK_PLIST;
285 		DoCleanBuild(1);
286 		OptimizeEnv();
287 		pkgs = ParsePackageList(ac - 1, av + 1);
288 		RemovePackages(pkgs);
289 		WorkerProcFlags |= WORKER_PROC_DEVELOPER;
290 		DoBuild(pkgs);
291 	} else {
292 		fprintf(stderr, "Unknown directive '%s'\n", av[0]);
293 		usage(2);
294 	}
295 
296 	return 0;
297 }
298 
299 static void
300 DoInit(void)
301 {
302 	struct stat st;
303 	char *path;
304 	FILE *fp;
305 
306 	if (stat(ConfigBase1, &st) == 0) {
307 		dfatal("init will not overwrite %s", ConfigBase1);
308 	}
309 	if (stat(ConfigBase2, &st) == 0) {
310 		dfatal("init will not create %s if %s exists",
311 		       ConfigBase2, ConfigBase1);
312 	}
313 	if (mkdir(ConfigBase1, 0755) < 0)
314 		dfatal_errno("Unable to mkdir %s", ConfigBase1);
315 
316 	asprintf(&path, "%s/dsynth.ini", ConfigBase1);
317 	fp = fopen(path, "w");
318 	dassert_errno(fp, "Unable to create %s", path);
319 	fprintf(fp, "%s",
320 	    "; This Synth configuration file is automatically generated\n"
321 	    "; Take care when hand editing!\n"
322 	    "\n"
323 	    "[Global Configuration]\n"
324 	    "profile_selected= LiveSystem\n"
325 	    "\n"
326 	    "[LiveSystem]\n"
327 	    "Operating_system= DragonFly\n"
328 	    "Directory_packages= /build/synth/live_packages\n"
329 	    "Directory_repository= /build/synth/live_packages/All\n"
330 	    "Directory_portsdir= /build/synth/dports\n"
331 	    "Directory_options= /build/synth/options\n"
332 	    "Directory_distfiles= /build/synth/distfiles\n"
333 	    "Directory_buildbase= /build/synth/build\n"
334 	    "Directory_logs= /build/synth/logs\n"
335 	    "Directory_ccache= disabled\n"
336 	    "Directory_system= /\n"
337 	    "Package_suffix= .txz\n"
338 	    "Number_of_builders= 0\n"
339 	    "Max_jobs_per_builder= 0\n"
340 	    "Tmpfs_workdir= true\n"
341 	    "Tmpfs_localbase= true\n"
342 	    "Display_with_ncurses= true\n"
343 	    "leverage_prebuilt= false\n"
344 	    "\n");
345 	if (fclose(fp))
346 		dfatal_errno("Unable to write to %s\n", ConfigBase1);
347 	free(path);
348 
349 	asprintf(&path, "%s/LiveSystem-make.conf", ConfigBase1);
350 	fp = fopen(path, "w");
351 	dassert_errno(fp, "Unable to create %s", path);
352 	fprintf(fp, "%s",
353 	    "#\n"
354 	    "# Various dports options that might be of interest\n"
355 	    "#\n"
356 	    "#LICENSES_ACCEPTED=      NONE\n"
357 	    "#DISABLE_LICENSES=       yes\n"
358 	    "#DEFAULT_VERSIONS=       ssl=openssl\n"
359 	    "#FORCE_PACKAGE=          yes\n"
360 	    "#DPORTS_BUILDER=         yes\n"
361 	    "#\n"
362 	    "# Turn these on to generate debug binaries.  However, these\n"
363 	    "# options will seriously bloat memory use and storage use,\n"
364 	    "# do not use lightly\n"
365 	    "#\n"
366 	    "#STRIP=\n"
367 	    "#WITH_DEBUG=yes\n"
368 	);
369 	if (fclose(fp))
370 		dfatal_errno("Unable to write to %s\n", ConfigBase1);
371 	free(path);
372 }
373 
374 __dead2 static void
375 usage(int ecode)
376 {
377 	if (ecode == 2) {
378 		fprintf(stderr, "Run 'dsynth help' for usage\n");
379 		exit(1);
380 	}
381 
382 	fprintf(stderr,
383     "dsynth [options] directive\n"
384     "    -d                   - Debug verbosity (-dd disables ncurses)\n"
385     "    -h                   - Display this screen and exit\n"
386     "    -m gb                - Load management based on pkgdep memory\n"
387     "    -v                   - Print version info and exit\n"
388     "    -y                   - Automatically answer yes to dsynth questions\n"
389     "    -s n                 - Set initial DynamicMaxWorkers\n"
390     "    -D                   - Enable DEVELOPER mode\n"
391     "    -P                   - Include the check-plist stage\n"
392     "    -S                   - Disable ncurses\n"
393     "\n"
394     "    init                 - Initialize /etc/dsynth\n"
395     "    status               - Dry-run of 'upgrade-system'\n"
396     "    cleanup              - Clean-up mounts\n"
397     "    configure            - Bring up configuration menu\n"
398     "    upgrade-system       - Incremental build and upgrade using pkg list\n"
399     "                           from local system, then upgrade the local\n"
400     "                           system.\n"
401     "    prepare-system       - 'upgrade-system' but stops after building\n"
402     "    rebuild-repository   - Rebuild database files for current repository\n"
403     "    purge-distfiles      - Delete obsolete source distribution files\n"
404     "    status-everything    - Dry-run of 'everything'\n"
405     "    everything           - Build entire dports tree and repo database\n"
406     "				(-D everything infers -P)\n"
407     "    version              - Print version info and exit\n"
408     "    help                 - Display this screen and exit\n"
409     "    status     [ports]   - Dry-run of 'build' with given list\n"
410     "    build      [ports]   - Incrementally build dports based on the given\n"
411     "                           list, but asks before updating the repo\n"
412     "                           database and system\n"
413     "    just-build [ports]   - 'build' but skips post-build steps\n"
414     "    install    [ports]   - 'build' but upgrades system without asking\n"
415     "    force      [ports]   - 'build' but deletes existing packages first\n"
416     "    test       [ports]   - 'build' w/DEVELOPER=yes and pre-deletes pkgs\n"
417     "				(also infers -P)\n"
418     "    debug      [ports]   - like 'test' but leaves mounts intact\n"
419     "    monitor    [datfile] - Monitor a running dsynth\n"
420     "\n"
421     "    [ports] is a space-delimited list of origins, e.g. editors/joe.  It\n"
422     "            may also be a path to a file containing one origin per line.\n"
423 	);
424 
425 	exit(ecode);
426 }
427