3 * $Id: sw_build.c,v 1.2 1999/07/16 12:50:24 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 /*----- Revision history --------------------------------------------------*
31 * $Log: sw_build.c,v $
32 * Revision 1.2 1999/07/16 12:50:24 mdw
33 * Improve exit status display. New interface from `doto' project.
35 * Revision 1.1.1.1 1999/06/02 16:53:34 mdw
40 /*----- Header files ------------------------------------------------------*/
52 #include <sys/types.h>
54 #include <sys/select.h>
61 extern char **environ;
64 #include <mLib/alloc.h>
65 #include <mLib/dstr.h>
67 #include <mLib/quis.h>
68 #include <mLib/report.h>
77 #define PRES_DEFAULT 0
78 #define PRES_PREFERENCE 0
79 #include "pres_plain.h"
80 #include "pres_curses.h"
82 /*----- Data structures ---------------------------------------------------*/
84 /*----- Static variables --------------------------------------------------*/
86 static pres *preslist = PRES_LINK;
88 /*----- Main code ---------------------------------------------------------*/
90 /* --- @swbuild_archlist@ --- *
92 * Arguments: @swinfo *sw@ = pointer to the build information block
94 * Returns: A list of architectures which are to be built.
96 * Use: Decides which architectures need building, and returns them
100 archcons *swbuild_archlist(swinfo *sw)
102 archcons *a = arch_readtab();
103 const char *only = 0;
104 unsigned and = 0, xor = 0;
106 /* --- Restrict the architecture list appropriately --- */
108 only = opt_arch ? opt_arch : sw->only_arch;
110 /* --- Apply the built flags --- */
112 if (!(opt_flags & optFlag_force) && sw->arch) {
113 archcons *aa = arch_filter(a, sw->arch, 0, 0);
115 for (c = aa; c; c = c->cdr)
116 c->car->flags |= archFlag_built;
118 and |= archFlag_built;
121 return (arch_filter(a, only, and, xor));
124 /*----- Main build command ------------------------------------------------*/
126 /* --- @sw_run@ --- *
128 * Arguments: @int argc@ = number of command line arguments
129 * @char *argv[]@ = array of command line arguments
131 * Returns: Zero on success (all builds OK) or nonzero for failure.
133 * Use: Runs a multi-architecture build.
136 int sw_run(int argc, char *argv[])
140 pres *p = PRES_DEFAULT;
146 /* --- Handle help on output styles --- */
148 if (opt_output && strcmp(opt_output, "help") == 0) {
149 printf("Presentation styles supported:");
150 for (p = preslist; p; p = p->next)
151 printf(" %s", p->name);
153 printf("The default presentation style is %s\n", (PRES_DEFAULT)->name);
157 /* --- Validate arguments --- */
160 die(1, "Usage: run COMMAND [ARG...]");
162 /* --- Choose an output presentation style --- */
166 size_t sz = strlen(opt_output);
169 for (q = preslist; q; q = q->next) {
170 if (strncmp(opt_output, q->name, sz) == 0) {
171 if (q->name[sz] == 0) {
175 die(1, "ambiguous output style `%s'", opt_output);
182 die(1, "unknown output style `%s'", opt_output);
185 if (p->ok && !p->ok()) {
186 moan("output style `%s' can't run; using `plain' instead", p->name);
190 /* --- Decide on an architecture --- */
192 if (swinfo_fetch(&sw)) {
193 die(1, "couldn't read build status: %s (try running setup)",
197 a = swbuild_archlist(&sw);
200 moan("All desired architectures already built OK.");
201 moan("(Perhaps you forgot `--force', or want to say `%s reset'.)", QUIS);
205 /* --- Tie on remote context blocks, and crank up the presentation --- */
210 for (aa = a; aa; aa = aa->cdr)
211 aa->car->r = xmalloc(sizeof(sw_remote));
212 if (p->init && p->init(a))
213 die(1, "presentation style refused to start: %s", strerror(errno));
216 signal(SIGINT, SIG_IGN);
217 signal(SIGQUIT, SIG_IGN);
219 /* --- Trap any exceptions coming this way --- *
221 * It's important, for example, that a curses-based presentation system
222 * reset the terminal flags appropriately.
226 /* --- Run remote build processes on the remote hosts --- */
232 for (aa = a; aa; aa = aa->cdr) {
233 archent *e = aa->car;
235 if (swrsh(r, e->flags & archFlag_home ? 0 : e->host,
236 "build", argv + 1)) {
238 dstr_putf(&d, "%s: couldn't start build for architecture `%s': %s",
239 QUIS, e->arch, strerror(errno));
240 p->output(e, d.buf, d.len);
254 /* --- Watch the builds until they do something interesting --- */
262 /* --- Find out what interesting things are happening --- */
264 memcpy(&f, &fdin, sizeof(f));
265 n = select(maxfd, &f, 0, 0, 0);
267 if (errno == EINTR || errno == EAGAIN)
270 THROW(EXC_ERRNO, errno);
273 /* --- Scan through for jobs which need attention --- */
275 for (aa = a; aa; aa = aa->cdr) {
276 archent *e = aa->car;
280 if (!FD_ISSET(r->fdin, &f))
283 switch (t = pkrecv(r)) {
286 p->output(e, r->buf, r->sz);
289 case PKTYPE_STATUS: {
294 dstr_putf(&d, "failed (%s)", r->buf);
297 } else if (r->buf[0]) {
298 dstr_putf(&d, "failed (status %u)", (unsigned char)r->buf[0]);
302 dstr_puts(&d, "finished");
303 if (opt_flags & optFlag_install)
304 e->flags |= archFlag_built;
307 p->close(e, ok, d.buf);
309 FD_CLR(r->fdin, &fdin);
316 p->close(e, 0, "unexpected exit");
318 FD_CLR(r->fdin, &fdin);
324 const static char msg[] = "\n[Unexpected packet, type %i]\n";
325 p->output(e, msg, sizeof(msg) - 1);
332 /* --- Handle any exceptions coming this way --- *
334 * I could do more cleanup here (freeing buffers and so) but it's not worth
335 * it. Nobody's bothering to catch exceptions anyway.
343 die(1, "unexpected error: %s", strerror(exc_i));
351 /* --- Tell the presentation that everything's done --- */
355 else if (opt_flags & optFlag_beep)
358 /* --- Clean up the unwanted remote contexts --- */
362 for (aa = a; aa; aa = aa->cdr)
366 /* --- Tidy away the architecture list --- */
370 /* --- Mark built architectures as having been completed now --- */
372 if (opt_flags & optFlag_install) {
376 arch_toText(&d, arch_readtab(), archFlag_built, archFlag_built);
378 swinfo_update(&sw, &skel);
381 die(1, "error writing build status: %s", strerror(errno));
386 /*----- Main remote entry point -------------------------------------------*/
390 * Arguments: @sw_remote *r@ = pointer to remote context
391 * @FILE *fp@ = log file handle
392 * @const char *fmt@ = format string
393 * @...@ = other arguments
397 * Use: Reports a string to the log file and the remote controller
401 static void putf(sw_remote *r, FILE *fp, const char *fmt, ...)
406 dstr_vputf(&d, fmt, ap);
409 pksend(r, PKTYPE_DATA, d.buf, d.len);
411 fwrite(d.buf, 1, d.len, fp);
415 /* --- @swrsh_build@ --- *
417 * Arguments: @sw_remote *r@ = pointer to remote context
418 * @char *argv[]@ = pointer to argument list
419 * @char *env[]@ = pointer to environment list
423 * Use: Runs a remote build command.
426 void swrsh_build(sw_remote *r, char *argv[], char *env[])
432 /* --- Validate arguments --- */
435 swdie(r, 1, "Usage: build COMMAND [ARG...]");
437 /* --- Change into architecture directory --- */
439 if (mkdir(ARCH, 0775) && errno != EEXIST)
440 swdie(r, 1, "mkdir(`%s') failed: %s", ARCH, strerror(errno));
442 swdie(r, 1, "couldn't change directory to `%s': %s",
443 ARCH, strerror(errno));
446 swdie(r, 1, "couldn't create pipe: %s", strerror(errno));
448 /* --- Open the log file --- */
451 int logfd = open(".build-log", O_WRONLY | O_APPEND | O_CREAT, 0664);
458 swdie(r, 1, "couldn't open `.build-log' file: %s", strerror(errno));
459 if ((logfp = fdopen(logfd, "a")) == 0) {
460 swdie(r, 1, "couldn't open stream on `.build-log' file: %s",
465 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
466 fprintf(logfp, "\n\n*** %s: started build: %s", buf, argv[0]);
467 for (p = argv + 1; *p; p++)
468 fprintf(logfp, " %s", *p);
469 fputs("\n\n", logfp);
472 /* --- Start off the child process --- */
483 nullfd = open("/dev/null", O_RDONLY);
489 execvp(argv[0], argv);
490 fprintf(stderr, "failed to start `%s': %s\n", argv[0], strerror(errno));
494 /* --- Read from the pipe, and write to the socket and logfile --- */
498 ssize_t n = read(fd[0], r->buf, PKMAX);
502 putf(r, logfp, "*** error reading from pipe: %s\n", strerror(errno));
506 fwrite(r->buf, 1, n, logfp);
507 pksend(r, PKTYPE_DATA, r->buf, n);
511 /* --- Reap exit status and produce final report --- */
515 if (waitpid(kid, &status, 0) < 0)
516 putf(r, logfp, "*** error reading exit status: %s\n", strerror(errno));
518 if (WIFSIGNALED(status))
519 fprintf(logfp, "*** exited on signal %i\n", WTERMSIG(status));
520 else if (WIFEXITED(status))
521 fprintf(logfp, "*** exited with status %i\n", WEXITSTATUS(status));
523 fprintf(logfp, "*** reaped, but didn't exit. Strange\n");
530 /*----- Syntactic sugar ---------------------------------------------------*/
533 * `Syntactic sugar causes cancer of the semicolon.'
534 * -- Alan Perlis, `Epigrams in Programming'
539 * Arguments: @char **u@ = first segment
540 * @char **v@ = second segment
542 * Returns: Return code from the build.
544 * Use: Combines two @argv@-style arrays and then runs a build on the
548 static int build(char **u, char **v)
553 for (i = 0, p = u; *p; p++, i++) ;
554 for (j = 0, p = v; *p; p++, j++) ;
555 p = xmalloc((i + j + 2) * sizeof(char **));
556 memcpy(p + 1, u, i * sizeof(char **));
557 memcpy(p + i + 1, v, j * sizeof(char **));
558 p[0] = p[i + j + 1] = 0;
559 return (sw_run(i + j + 1, p));
562 /* --- @sw_make@ --- */
564 int sw_make(int argc, char *argv[])
566 static char *mk[] = { 0, 0 };
569 if ((m = getenv("SW_MAKE")) == 0 &&
570 (m = getenv("MAKE")) == 0)
574 return (build(mk, argv + 1));
577 /* --- @sw_conf@ --- */
579 int sw_conf(int argc, char *argv[])
581 static char *cf[] = { "../configure", "--prefix=" PREFIX, 0 };
582 return (build(cf, argv + 1));
585 /*----- Other subcommands -------------------------------------------------*/
587 /* --- @sw_reset@ --- */
589 int sw_reset(int argc, char *argv[])
593 die(1, "Usage: reset");
594 if (swinfo_fetch(&sw)) {
595 die(1, "couldn't read build status: %s (try running setup)",
601 swinfo_update(&sw, &skel);
603 die(1, "couldn't write build status: %s", strerror(errno));
607 /*----- That's all, folks -------------------------------------------------*/