3 * $Id: sw_build.c,v 1.5 2004/04/08 01:52:19 mdw Exp $
5 * Management of build processes
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of sw-tools.
14 * sw-tools is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * sw-tools is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with sw-tools; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
41 #include <sys/types.h>
43 #include <sys/select.h>
48 #include <sys/utsname.h>
51 extern char **environ;
54 #include <mLib/alloc.h>
55 #include <mLib/dstr.h>
57 #include <mLib/quis.h>
58 #include <mLib/report.h>
67 #define PRES_DEFAULT 0
68 #define PRES_PREFERENCE 0
69 #include "pres_plain.h"
70 #include "pres_curses.h"
72 /*----- Data structures ---------------------------------------------------*/
74 /*----- Static variables --------------------------------------------------*/
76 static pres *preslist = PRES_LINK;
78 /*----- Main code ---------------------------------------------------------*/
80 /* --- @swbuild_archlist@ --- *
82 * Arguments: @swinfo *sw@ = pointer to the build information block
84 * Returns: A list of architectures which are to be built.
86 * Use: Decides which architectures need building, and returns them
90 archcons *swbuild_archlist(swinfo *sw)
92 archcons *a = arch_readtab();
94 unsigned and = 0, xor = 0;
96 /* --- Restrict the architecture list appropriately --- */
98 only = opt_arch ? opt_arch : sw->only_arch;
100 /* --- Apply the built flags --- */
102 if (!(opt_flags & optFlag_force) && sw->arch) {
103 archcons *aa = arch_filter(a, sw->arch, 0, 0);
105 for (c = aa; c; c = c->cdr)
106 c->car->flags |= archFlag_built;
108 and |= archFlag_built;
111 return (arch_filter(a, only, and, xor));
114 /*----- Main build command ------------------------------------------------*/
116 /* --- @sw_run@ --- *
118 * Arguments: @int argc@ = number of command line arguments
119 * @char *argv[]@ = array of command line arguments
121 * Returns: Zero on success (all builds OK) or nonzero for failure.
123 * Use: Runs a multi-architecture build.
126 int sw_run(int argc, char *argv[])
130 pres *p = PRES_DEFAULT;
136 /* --- Handle help on output styles --- */
138 if (opt_output && strcmp(opt_output, "help") == 0) {
139 printf("Presentation styles supported:");
140 for (p = preslist; p; p = p->next)
141 printf(" %s", p->name);
143 printf("The default presentation style is %s\n", (PRES_DEFAULT)->name);
147 /* --- Validate arguments --- */
150 die(1, "Usage: run COMMAND [ARG...]");
152 /* --- Choose an output presentation style --- */
156 size_t sz = strlen(opt_output);
159 for (q = preslist; q; q = q->next) {
160 if (strncmp(opt_output, q->name, sz) == 0) {
161 if (q->name[sz] == 0) {
165 die(1, "ambiguous output style `%s'", opt_output);
172 die(1, "unknown output style `%s'", opt_output);
175 if (p->ok && !p->ok()) {
176 moan("output style `%s' can't run; using `plain' instead", p->name);
180 /* --- Decide on an architecture --- */
182 if (swinfo_fetch(&sw)) {
183 die(1, "couldn't read build status: %s (try running setup)",
187 a = swbuild_archlist(&sw);
190 moan("All desired architectures already built OK.");
191 moan("(Perhaps you forgot `--force', or want to say `%s reset'.)", QUIS);
195 /* --- Tie on remote context blocks, and crank up the presentation --- */
200 for (aa = a; aa; aa = aa->cdr)
201 aa->car->r = xmalloc(sizeof(sw_remote));
202 if (p->init && p->init(a))
203 die(1, "presentation style refused to start: %s", strerror(errno));
206 signal(SIGINT, SIG_IGN);
207 signal(SIGQUIT, SIG_IGN);
209 /* --- Trap any exceptions coming this way --- *
211 * It's important, for example, that a curses-based presentation system
212 * reset the terminal flags appropriately.
216 /* --- Run remote build processes on the remote hosts --- */
224 /* --- Fill in the hostname --- */
227 strcpy(u.nodename, "<unknown>");
229 /* --- If necessary, set up the output @argv@ array --- */
231 if (opt_flags & optFlag_percent)
232 av = xmalloc(argc * sizeof(char *));
236 /* --- Run through the target build hosts --- */
239 for (aa = a; aa; aa = aa->cdr) {
240 archent *e = aa->car;
243 /* --- If necessary, translate `%'-escapes --- */
245 if (opt_flags & optFlag_percent) {
248 for (pp = argv + 1, qq = av; *pp; pp++, qq++) {
249 if (strchr(*pp, '%') == 0)
254 for (p = *pp; *p; p++) {
266 dstr_puts(&d, e->arch);
269 dstr_puts(&d, e->flags & archFlag_home ?
270 u.nodename : e->host);
273 dstr_puts(&d, PREFIX);
276 dstr_puts(&d, sw.package);
279 dstr_puts(&d, sw.version);
282 dstr_puts(&d, sw.maintainer);
295 *qq = xstrdup(d.buf);
302 /* --- Start a new process off --- */
304 if (swrsh(r, e->flags & archFlag_home ? 0 : e->host,
307 dstr_putf(&d, "%s: couldn't start build for architecture `%s': %s",
308 QUIS, e->arch, strerror(errno));
309 p->output(e, d.buf, d.len);
321 /* --- Free up the argument array --- */
323 if (opt_flags & optFlag_percent) {
326 for (pp = argv + 1, qq = av; *pp; pp++, qq++) {
333 if (opt_flags & optFlag_percent)
337 /* --- Watch the builds until they do something interesting --- */
345 /* --- Find out what interesting things are happening --- */
347 memcpy(&f, &fdin, sizeof(f));
348 n = select(maxfd, &f, 0, 0, 0);
350 if (errno == EINTR || errno == EAGAIN)
353 THROW(EXC_ERRNO, errno);
356 /* --- Scan through for jobs which need attention --- */
358 for (aa = a; aa; aa = aa->cdr) {
359 archent *e = aa->car;
363 if (!FD_ISSET(r->fdin, &f))
366 switch (t = pkrecv(r)) {
369 p->output(e, r->buf, r->sz);
372 case PKTYPE_STATUS: {
377 dstr_putf(&d, "failed (%s)", r->buf);
380 } else if (r->buf[0]) {
381 dstr_putf(&d, "failed (status %u)", (unsigned char)r->buf[0]);
385 dstr_puts(&d, "finished");
386 if (opt_flags & optFlag_install)
387 e->flags |= archFlag_built;
390 p->close(e, ok, d.buf);
392 FD_CLR(r->fdin, &fdin);
399 p->close(e, 0, "unexpected exit");
401 FD_CLR(r->fdin, &fdin);
407 const static char msg[] = "\n[Unexpected packet, type %i]\n";
408 p->output(e, msg, sizeof(msg) - 1);
415 /* --- Handle any exceptions coming this way --- *
417 * I could do more cleanup here (freeing buffers and so) but it's not worth
418 * it. Nobody's bothering to catch exceptions anyway.
426 die(1, "unexpected error: %s", strerror(exc_i));
434 /* --- Tell the presentation that everything's done --- */
438 else if (opt_flags & optFlag_beep)
441 /* --- Clean up the unwanted remote contexts --- */
445 for (aa = a; aa; aa = aa->cdr)
449 /* --- Tidy away the architecture list --- */
453 /* --- Mark built architectures as having been completed now --- */
455 if (opt_flags & optFlag_install) {
459 arch_toText(&d, arch_readtab(), archFlag_built, archFlag_built);
461 swinfo_update(&sw, &skel);
464 die(1, "error writing build status: %s", strerror(errno));
469 /*----- Main remote entry point -------------------------------------------*/
473 * Arguments: @sw_remote *r@ = pointer to remote context
474 * @FILE *fp@ = log file handle
475 * @const char *fmt@ = format string
476 * @...@ = other arguments
480 * Use: Reports a string to the log file and the remote controller
484 static void putf(sw_remote *r, FILE *fp, const char *fmt, ...)
489 dstr_vputf(&d, fmt, ap);
492 pksend(r, PKTYPE_DATA, d.buf, d.len);
494 fwrite(d.buf, 1, d.len, fp);
498 /* --- @swrsh_build@ --- *
500 * Arguments: @sw_remote *r@ = pointer to remote context
501 * @char *argv[]@ = pointer to argument list
502 * @char *env[]@ = pointer to environment list
506 * Use: Runs a remote build command.
509 void swrsh_build(sw_remote *r, char *argv[], char *env[])
515 /* --- Validate the arguments --- */
518 swdie(r, 1, "Usage: build COMMAND [ARG...]");
520 /* --- Change into architecture directory --- */
522 if (mkdir(ARCH, 0775) && errno != EEXIST)
523 swdie(r, 1, "mkdir(`%s') failed: %s", ARCH, strerror(errno));
525 swdie(r, 1, "couldn't change directory to `%s': %s",
526 ARCH, strerror(errno));
529 swdie(r, 1, "couldn't create pipe: %s", strerror(errno));
531 /* --- Open the log file --- */
534 int logfd = open(".build-log", O_WRONLY | O_APPEND | O_CREAT, 0664);
542 swdie(r, 1, "couldn't get hostname: %s", strerror(errno));
544 swdie(r, 1, "couldn't open `.build-log' file: %s", strerror(errno));
545 if ((logfp = fdopen(logfd, "a")) == 0) {
546 swdie(r, 1, "couldn't open stream on `.build-log' file: %s",
551 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
552 fprintf(logfp, "\n\n*** %s: %s started build: %s",
553 buf, u.nodename, argv[0]);
554 for (p = argv + 1; *p; p++)
555 fprintf(logfp, " %s", *p);
556 fputs("\n\n", logfp);
559 /* --- Start off the child process --- */
570 nullfd = open("/dev/null", O_RDONLY);
576 execvp(argv[0], argv);
577 fprintf(stderr, "failed to start `%s': %s\n", argv[0], strerror(errno));
581 /* --- Read from the pipe, and write to the socket and logfile --- */
585 ssize_t n = read(fd[0], r->buf, PKMAX);
589 putf(r, logfp, "\n*** error reading from pipe: %s\n", strerror(errno));
593 fwrite(r->buf, 1, n, logfp);
594 pksend(r, PKTYPE_DATA, r->buf, n);
598 /* --- Reap exit status and produce final report --- */
602 if (waitpid(kid, &status, 0) < 0) {
603 putf(r, logfp, "\n*** error reading exit status: %s\n",
606 if (WIFSIGNALED(status))
607 fprintf(logfp, "\n*** exited on signal %i\n", WTERMSIG(status));
608 else if (WIFEXITED(status))
609 fprintf(logfp, "\n*** exited with status %i\n", WEXITSTATUS(status));
611 fprintf(logfp, "\n*** reaped, but didn't exit. Strange\n");
618 /*----- Syntactic sugar ---------------------------------------------------*/
621 * `Syntactic sugar causes cancer of the semicolon.'
622 * -- Alan Perlis, `Epigrams in Programming'
627 * Arguments: @char **u@ = first segment
628 * @char **v@ = second segment
630 * Returns: Return code from the build.
632 * Use: Combines two @argv@-style arrays and then runs a build on the
636 static int build(char **u, char **v)
641 for (i = 0, p = u; *p; p++, i++) ;
642 for (j = 0, p = v; *p; p++, j++) ;
643 p = xmalloc((i + j + 2) * sizeof(char **));
644 memcpy(p + 1, u, i * sizeof(char **));
645 memcpy(p + i + 1, v, j * sizeof(char **));
646 p[0] = p[i + j + 1] = 0;
647 return (sw_run(i + j + 1, p));
650 /* --- @sw_make@ --- */
652 int sw_make(int argc, char *argv[])
654 static char *mk[] = { 0, 0 };
657 if ((m = getenv("SW_MAKE")) == 0 &&
658 (m = getenv("MAKE")) == 0)
662 return (build(mk, argv + 1));
665 /* --- @sw_conf@ --- */
667 int sw_conf(int argc, char *argv[])
669 static char *cf[] = { "../configure", "--prefix=" PREFIX, 0 };
670 return (build(cf, argv + 1));
673 /*----- Other subcommands -------------------------------------------------*/
675 /* --- @sw_reset@ --- */
677 int sw_reset(int argc, char *argv[])
681 die(1, "Usage: reset");
682 if (swinfo_fetch(&sw)) {
683 die(1, "couldn't read build status: %s (try running setup)",
689 swinfo_update(&sw, &skel);
691 die(1, "couldn't write build status: %s", strerror(errno));
695 /*----- That's all, folks -------------------------------------------------*/