3 * $Id: sw_build.c,v 1.1 1999/06/02 16:53:34 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.1 1999/06/02 16:53:34 mdw
37 /*----- Header files ------------------------------------------------------*/
49 #include <sys/types.h>
51 #include <sys/select.h>
58 extern char **environ;
61 #include <mLib/alloc.h>
62 #include <mLib/dstr.h>
64 #include <mLib/quis.h>
65 #include <mLib/report.h>
74 #define PRES_DEFAULT 0
75 #define PRES_PREFERENCE 0
76 #include "pres_plain.h"
77 #include "pres_curses.h"
79 /*----- Data structures ---------------------------------------------------*/
81 /*----- Static variables --------------------------------------------------*/
83 static pres *preslist = PRES_LINK;
85 /*----- Main code ---------------------------------------------------------*/
87 /* --- @swbuild_archlist@ --- *
89 * Arguments: @swinfo *sw@ = pointer to the build information block
91 * Returns: A list of architectures which are to be built.
93 * Use: Decides which architectures need building, and returns them
97 archcons *swbuild_archlist(swinfo *sw)
99 archcons *a = arch_readtab();
100 const char *only = 0;
101 unsigned and = 0, xor = 0;
103 /* --- Restrict the architecture list appropriately --- */
105 only = opt_arch ? opt_arch : sw->only_arch;
107 /* --- Apply the built flags --- */
109 if (!(opt_flags & optFlag_force) && sw->arch) {
110 archcons *aa = arch_filter(a, sw->arch, 0, 0);
112 for (c = aa; c; c = c->cdr)
113 c->car->flags |= archFlag_built;
115 and |= archFlag_built;
118 return (arch_filter(a, only, and, xor));
121 /*----- Main build command ------------------------------------------------*/
123 /* --- @sw_run@ --- *
125 * Arguments: @int argc@ = number of command line arguments
126 * @char *argv[]@ = array of command line arguments
128 * Returns: Zero on success (all builds OK) or nonzero for failure.
130 * Use: Runs a multi-architecture build.
133 int sw_run(int argc, char *argv[])
137 pres *p = PRES_DEFAULT;
143 /* --- Handle help on output styles --- */
145 if (opt_output && strcmp(opt_output, "help") == 0) {
146 printf("Presentation styles supported:");
147 for (p = preslist; p; p = p->next)
148 printf(" %s", p->name);
150 printf("The default presentation style is %s\n", (PRES_DEFAULT)->name);
154 /* --- Validate arguments --- */
157 die(1, "Usage: run COMMAND [ARG...]");
159 /* --- Choose an output presentation style --- */
163 size_t sz = strlen(opt_output);
166 for (q = preslist; q; q = q->next) {
167 if (strncmp(opt_output, q->name, sz) == 0) {
168 if (q->name[sz] == 0) {
172 die(1, "ambiguous output style `%s'", opt_output);
179 die(1, "unknown output style `%s'", opt_output);
182 if (p->ok && !p->ok()) {
183 moan("output style `%s' can't run; using `plain' instead", p->name);
187 /* --- Decide on an architecture --- */
189 if (swinfo_fetch(&sw)) {
190 die(1, "couldn't read build status: %s (try running setup)",
194 a = swbuild_archlist(&sw);
197 moan("All desired architectures already built OK.");
198 moan("(Perhaps you forgot `--force', or want to say `%s reset'.)", QUIS);
202 /* --- Tie on remote context blocks, and crank up the presentation --- */
207 for (aa = a; aa; aa = aa->cdr)
208 aa->car->r = xmalloc(sizeof(sw_remote));
209 if (p->init && p->init(a))
210 die(1, "presentation style refused to start: %s", strerror(errno));
213 signal(SIGINT, SIG_IGN);
214 signal(SIGQUIT, SIG_IGN);
216 /* --- Trap any exceptions coming this way --- *
218 * It's important, for example, that a curses-based presentation system
219 * reset the terminal flags appropriately.
223 /* --- Run remote build processes on the remote hosts --- */
229 for (aa = a; aa; aa = aa->cdr) {
230 archent *e = aa->car;
232 if (swrsh(r, e->flags & archFlag_home ? 0 : e->host,
233 "build", argv + 1)) {
235 dstr_putf(&d, "%s: couldn't start build for architecture `%s': %s",
236 QUIS, e->arch, strerror(errno));
237 p->output(e, d.buf, d.len);
251 /* --- Watch the builds until they do something interesting --- */
259 /* --- Find out what interesting things are happening --- */
261 memcpy(&f, &fdin, sizeof(f));
262 n = select(maxfd, &f, 0, 0, 0);
264 if (errno == EINTR || errno == EAGAIN)
267 THROW(EXC_ERRNO, errno);
270 /* --- Scan through for jobs which need attention --- */
272 for (aa = a; aa; aa = aa->cdr) {
273 archent *e = aa->car;
277 if (!FD_ISSET(r->fdin, &f))
280 switch (t = pkrecv(r)) {
283 p->output(e, r->buf, r->sz);
286 case PKTYPE_STATUS: {
291 dstr_putf(&d, "\nTerminated by signal: %s.\n", r->buf);
294 } else if (r->buf[0]) {
295 dstr_putf(&d, "\nExited with status %u.\n",
296 (unsigned char)r->buf[0]);
299 } else if (opt_flags & optFlag_install)
300 e->flags |= archFlag_built;
302 p->output(e, d.buf, d.len);
306 FD_CLR(r->fdin, &fdin);
312 const static char msg[] = "\nUnexpected exit.\n";
313 p->output(e, msg, sizeof(msg) - 1);
317 FD_CLR(r->fdin, &fdin);
323 const static char msg[] = "\n[Unexpected packet, type %i]\n";
324 p->output(e, msg, sizeof(msg) - 1);
331 /* --- Handle any exceptions coming this way --- *
333 * I could do more cleanup here (freeing buffers and so) but it's not worth
334 * it. Nobody's bothering to catch exceptions anyway.
342 die(1, "unexpected error: %s", exc_i);
350 /* --- Tell the presentation that everything's done --- */
354 else if (opt_flags & optFlag_beep)
357 /* --- Clean up the unwanted remote contexts --- */
361 for (aa = a; aa; aa = aa->cdr)
365 /* --- Tidy away the architecture list --- */
369 /* --- Mark built architectures as having been completed now --- */
371 if (opt_flags & optFlag_install) {
375 arch_toText(&d, arch_readtab(), archFlag_built, archFlag_built);
377 swinfo_update(&sw, &skel);
380 die(1, "error writing build status: %s", strerror(errno));
385 /*----- Main remote entry point -------------------------------------------*/
389 * Arguments: @sw_remote *r@ = pointer to remote context
390 * @FILE *fp@ = log file handle
391 * @const char *fmt@ = format string
392 * @...@ = other arguments
396 * Use: Reports a string to the log file and the remote controller
400 static void putf(sw_remote *r, FILE *fp, const char *fmt, ...)
405 dstr_vputf(&d, fmt, ap);
408 pksend(r, PKTYPE_DATA, d.buf, d.len);
410 fwrite(d.buf, 1, d.len, fp);
414 /* --- @swrsh_build@ --- *
416 * Arguments: @sw_remote *r@ = pointer to remote context
417 * @char *argv[]@ = pointer to argument list
418 * @char *env[]@ = pointer to environment list
422 * Use: Runs a remote build command.
425 void swrsh_build(sw_remote *r, char *argv[], char *env[])
431 /* --- Validate arguments --- */
434 swdie(r, 1, "Usage: build COMMAND [ARG...]");
436 /* --- Change into architecture directory --- */
438 if (mkdir(ARCH, 0775) && errno != EEXIST)
439 swdie(r, 1, "mkdir(`%s') failed: %s", ARCH, strerror(errno));
441 swdie(r, 1, "couldn't change directory to `%s': %s",
442 ARCH, strerror(errno));
445 swdie(r, 1, "couldn't create pipe: %s", strerror(errno));
447 /* --- Open the log file --- */
450 int logfd = open(".build-log", O_WRONLY | O_APPEND | O_CREAT, 0664);
457 swdie(r, 1, "couldn't open `.build-log' file: %s", strerror(errno));
458 if ((logfp = fdopen(logfd, "a")) == 0) {
459 swdie(r, 1, "couldn't open stream on `.build-log' file: %s",
464 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
465 fprintf(logfp, "\n\n*** %s: started build: %s", buf, argv[0]);
466 for (p = argv + 1; *p; p++)
467 fprintf(logfp, " %s", *p);
468 fputs("\n\n", logfp);
471 /* --- Start off the child process --- */
482 nullfd = open("/dev/null", O_RDONLY);
488 execvp(argv[0], argv);
489 fprintf(stderr, "failed to start `%s': %s\n", argv[0], strerror(errno));
493 /* --- Read from the pipe, and write to the socket and logfile --- */
497 ssize_t n = read(fd[0], r->buf, PKMAX);
501 putf(r, logfp, "*** error reading from pipe: %s\n", strerror(errno));
505 fwrite(r->buf, 1, n, logfp);
506 pksend(r, PKTYPE_DATA, r->buf, n);
510 /* --- Reap exit status and produce final report --- */
514 if (waitpid(kid, &status, 0) < 0)
515 putf(r, logfp, "*** error reading exit status: %s\n", strerror(errno));
517 if (WIFSIGNALED(status))
518 fprintf(logfp, "*** exited on signal %i\n", WTERMSIG(status));
519 else if (WIFEXITED(status))
520 fprintf(logfp, "*** exited with status %i\n", WEXITSTATUS(status));
522 fprintf(logfp, "*** reaped, but didn't exit. Strange\n");
529 /*----- Syntactic sugar ---------------------------------------------------*/
532 * `Syntactic sugar causes cancer of the semicolon.'
533 * -- Alan Perlis, `Epigrams in Programming'
538 * Arguments: @char **u@ = first segment
539 * @char **v@ = second segment
541 * Returns: Return code from the build.
543 * Use: Combines two @argv@-style arrays and then runs a build on the
547 static int build(char **u, char **v)
552 for (i = 0, p = u; *p; p++, i++) ;
553 for (j = 0, p = v; *p; p++, j++) ;
554 p = xmalloc((i + j + 2) * sizeof(char **));
555 memcpy(p + 1, u, i * sizeof(char **));
556 memcpy(p + i + 1, v, j * sizeof(char **));
557 p[0] = p[i + j + 1] = 0;
558 return (sw_run(i + j + 1, p));
561 /* --- @sw_make@ --- */
563 int sw_make(int argc, char *argv[])
565 static char *mk[] = { 0, 0 };
568 if ((m = getenv("SW_MAKE")) == 0 &&
569 (m = getenv("MAKE")) == 0)
573 return (build(mk, argv + 1));
576 /* --- @sw_conf@ --- */
578 int sw_conf(int argc, char *argv[])
580 static char *cf[] = { "../configure", "--prefix=" PREFIX, 0 };
581 return (build(cf, argv + 1));
584 /*----- Other subcommands -------------------------------------------------*/
586 /* --- @sw_reset@ --- */
588 int sw_reset(int argc, char *argv[])
592 die(1, "Usage: reset");
593 if (swinfo_fetch(&sw)) {
594 die(1, "couldn't read build status: %s (try running setup)",
600 swinfo_update(&sw, &skel);
602 die(1, "couldn't write build status: %s", strerror(errno));
606 /*----- That's all, folks -------------------------------------------------*/