3 * $Id: sw_build.c,v 1.3 1999/09/10 15:27:33 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.3 1999/09/10 15:27:33 mdw
33 * Include `%'-escape substitution.
35 * Revision 1.2 1999/07/16 12:50:24 mdw
36 * Improve exit status display. New interface from `doto' project.
38 * Revision 1.1.1.1 1999/06/02 16:53:34 mdw
43 /*----- Header files ------------------------------------------------------*/
55 #include <sys/types.h>
57 #include <sys/select.h>
62 #include <sys/utsname.h>
65 extern char **environ
;
68 #include <mLib/alloc.h>
69 #include <mLib/dstr.h>
71 #include <mLib/quis.h>
72 #include <mLib/report.h>
81 #define PRES_DEFAULT 0
82 #define PRES_PREFERENCE 0
83 #include "pres_plain.h"
84 #include "pres_curses.h"
86 /*----- Data structures ---------------------------------------------------*/
88 /*----- Static variables --------------------------------------------------*/
90 static pres
*preslist
= PRES_LINK
;
92 /*----- Main code ---------------------------------------------------------*/
94 /* --- @swbuild_archlist@ --- *
96 * Arguments: @swinfo *sw@ = pointer to the build information block
98 * Returns: A list of architectures which are to be built.
100 * Use: Decides which architectures need building, and returns them
104 archcons
*swbuild_archlist(swinfo
*sw
)
106 archcons
*a
= arch_readtab();
107 const char *only
= 0;
108 unsigned and = 0, xor = 0;
110 /* --- Restrict the architecture list appropriately --- */
112 only
= opt_arch ? opt_arch
: sw
->only_arch
;
114 /* --- Apply the built flags --- */
116 if (!(opt_flags
& optFlag_force
) && sw
->arch
) {
117 archcons
*aa
= arch_filter(a
, sw
->arch
, 0, 0);
119 for (c
= aa
; c
; c
= c
->cdr
)
120 c
->car
->flags
|= archFlag_built
;
122 and |= archFlag_built
;
125 return (arch_filter(a
, only
, and, xor));
128 /*----- Main build command ------------------------------------------------*/
130 /* --- @sw_run@ --- *
132 * Arguments: @int argc@ = number of command line arguments
133 * @char *argv[]@ = array of command line arguments
135 * Returns: Zero on success (all builds OK) or nonzero for failure.
137 * Use: Runs a multi-architecture build.
140 int sw_run(int argc
, char *argv
[])
144 pres
*p
= PRES_DEFAULT
;
150 /* --- Handle help on output styles --- */
152 if (opt_output
&& strcmp(opt_output
, "help") == 0) {
153 printf("Presentation styles supported:");
154 for (p
= preslist
; p
; p
= p
->next
)
155 printf(" %s", p
->name
);
157 printf("The default presentation style is %s\n", (PRES_DEFAULT
)->name
);
161 /* --- Validate arguments --- */
164 die(1, "Usage: run COMMAND [ARG...]");
166 /* --- Choose an output presentation style --- */
170 size_t sz
= strlen(opt_output
);
173 for (q
= preslist
; q
; q
= q
->next
) {
174 if (strncmp(opt_output
, q
->name
, sz
) == 0) {
175 if (q
->name
[sz
] == 0) {
179 die(1, "ambiguous output style `%s'", opt_output
);
186 die(1, "unknown output style `%s'", opt_output
);
189 if (p
->ok
&& !p
->ok()) {
190 moan("output style `%s' can't run; using `plain' instead", p
->name
);
194 /* --- Decide on an architecture --- */
196 if (swinfo_fetch(&sw
)) {
197 die(1, "couldn't read build status: %s (try running setup)",
201 a
= swbuild_archlist(&sw
);
204 moan("All desired architectures already built OK.");
205 moan("(Perhaps you forgot `--force', or want to say `%s reset'.)", QUIS
);
209 /* --- Tie on remote context blocks, and crank up the presentation --- */
214 for (aa
= a
; aa
; aa
= aa
->cdr
)
215 aa
->car
->r
= xmalloc(sizeof(sw_remote
));
216 if (p
->init
&& p
->init(a
))
217 die(1, "presentation style refused to start: %s", strerror(errno
));
220 signal(SIGINT
, SIG_IGN
);
221 signal(SIGQUIT
, SIG_IGN
);
223 /* --- Trap any exceptions coming this way --- *
225 * It's important, for example, that a curses-based presentation system
226 * reset the terminal flags appropriately.
230 /* --- Run remote build processes on the remote hosts --- */
238 /* --- Fill in the hostname --- */
241 strcpy(u
.nodename
, "<unknown>");
243 /* --- If necessary, set up the output @argv@ array --- */
245 if (opt_flags
& optFlag_percent
)
246 av
= xmalloc(argc
* sizeof(char *));
250 /* --- Run through the target build hosts --- */
253 for (aa
= a
; aa
; aa
= aa
->cdr
) {
254 archent
*e
= aa
->car
;
257 /* --- If necessary, translate `%'-escapes --- */
259 if (opt_flags
& optFlag_percent
) {
262 for (pp
= argv
+ 1, qq
= av
; *pp
; pp
++, qq
++) {
263 if (strchr(*pp
, '%') == 0)
268 for (p
= *pp
; *p
; p
++) {
280 dstr_puts(&d
, e
->arch
);
283 dstr_puts(&d
, e
->flags
& archFlag_home ?
284 u
.nodename
: e
->host
);
287 dstr_puts(&d
, PREFIX
);
290 dstr_puts(&d
, sw
.package
);
293 dstr_puts(&d
, sw
.version
);
296 dstr_puts(&d
, sw
.maintainer
);
309 *qq
= xstrdup(d
.buf
);
316 /* --- Start a new process off --- */
318 if (swrsh(r
, e
->flags
& archFlag_home ?
0 : e
->host
,
321 dstr_putf(&d
, "%s: couldn't start build for architecture `%s': %s",
322 QUIS
, e
->arch
, strerror(errno
));
323 p
->output(e
, d
.buf
, d
.len
);
335 /* --- Free up the argument array --- */
337 if (opt_flags
& optFlag_percent
) {
340 for (pp
= argv
+ 1, qq
= av
; *pp
; pp
++, qq
++) {
347 if (opt_flags
& optFlag_percent
)
351 /* --- Watch the builds until they do something interesting --- */
359 /* --- Find out what interesting things are happening --- */
361 memcpy(&f
, &fdin
, sizeof(f
));
362 n
= select(maxfd
, &f
, 0, 0, 0);
364 if (errno
== EINTR
|| errno
== EAGAIN
)
367 THROW(EXC_ERRNO
, errno
);
370 /* --- Scan through for jobs which need attention --- */
372 for (aa
= a
; aa
; aa
= aa
->cdr
) {
373 archent
*e
= aa
->car
;
377 if (!FD_ISSET(r
->fdin
, &f
))
380 switch (t
= pkrecv(r
)) {
383 p
->output(e
, r
->buf
, r
->sz
);
386 case PKTYPE_STATUS
: {
391 dstr_putf(&d
, "failed (%s)", r
->buf
);
394 } else if (r
->buf
[0]) {
395 dstr_putf(&d
, "failed (status %u)", (unsigned char)r
->buf
[0]);
399 dstr_puts(&d
, "finished");
400 if (opt_flags
& optFlag_install
)
401 e
->flags
|= archFlag_built
;
404 p
->close(e
, ok
, d
.buf
);
406 FD_CLR(r
->fdin
, &fdin
);
413 p
->close(e
, 0, "unexpected exit");
415 FD_CLR(r
->fdin
, &fdin
);
421 const static char msg
[] = "\n[Unexpected packet, type %i]\n";
422 p
->output(e
, msg
, sizeof(msg
) - 1);
429 /* --- Handle any exceptions coming this way --- *
431 * I could do more cleanup here (freeing buffers and so) but it's not worth
432 * it. Nobody's bothering to catch exceptions anyway.
440 die(1, "unexpected error: %s", strerror(exc_i
));
448 /* --- Tell the presentation that everything's done --- */
452 else if (opt_flags
& optFlag_beep
)
455 /* --- Clean up the unwanted remote contexts --- */
459 for (aa
= a
; aa
; aa
= aa
->cdr
)
463 /* --- Tidy away the architecture list --- */
467 /* --- Mark built architectures as having been completed now --- */
469 if (opt_flags
& optFlag_install
) {
473 arch_toText(&d
, arch_readtab(), archFlag_built
, archFlag_built
);
475 swinfo_update(&sw
, &skel
);
478 die(1, "error writing build status: %s", strerror(errno
));
483 /*----- Main remote entry point -------------------------------------------*/
487 * Arguments: @sw_remote *r@ = pointer to remote context
488 * @FILE *fp@ = log file handle
489 * @const char *fmt@ = format string
490 * @...@ = other arguments
494 * Use: Reports a string to the log file and the remote controller
498 static void putf(sw_remote
*r
, FILE *fp
, const char *fmt
, ...)
503 dstr_vputf(&d
, fmt
, ap
);
506 pksend(r
, PKTYPE_DATA
, d
.buf
, d
.len
);
508 fwrite(d
.buf
, 1, d
.len
, fp
);
512 /* --- @swrsh_build@ --- *
514 * Arguments: @sw_remote *r@ = pointer to remote context
515 * @char *argv[]@ = pointer to argument list
516 * @char *env[]@ = pointer to environment list
520 * Use: Runs a remote build command.
523 void swrsh_build(sw_remote
*r
, char *argv
[], char *env
[])
529 /* --- Validate the arguments --- */
532 swdie(r
, 1, "Usage: build COMMAND [ARG...]");
534 /* --- Change into architecture directory --- */
536 if (mkdir(ARCH
, 0775) && errno
!= EEXIST
)
537 swdie(r
, 1, "mkdir(`%s') failed: %s", ARCH
, strerror(errno
));
539 swdie(r
, 1, "couldn't change directory to `%s': %s",
540 ARCH
, strerror(errno
));
543 swdie(r
, 1, "couldn't create pipe: %s", strerror(errno
));
545 /* --- Open the log file --- */
548 int logfd
= open(".build-log", O_WRONLY
| O_APPEND
| O_CREAT
, 0664);
556 swdie(r
, 1, "couldn't get hostname: %s", strerror(errno
));
558 swdie(r
, 1, "couldn't open `.build-log' file: %s", strerror(errno
));
559 if ((logfp
= fdopen(logfd
, "a")) == 0) {
560 swdie(r
, 1, "couldn't open stream on `.build-log' file: %s",
565 strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", tm
);
566 fprintf(logfp
, "\n\n*** %s: %s started build: %s",
567 buf
, u
.nodename
, argv
[0]);
568 for (p
= argv
+ 1; *p
; p
++)
569 fprintf(logfp
, " %s", *p
);
570 fputs("\n\n", logfp
);
573 /* --- Start off the child process --- */
584 nullfd
= open("/dev/null", O_RDONLY
);
590 execvp(argv
[0], argv
);
591 fprintf(stderr
, "failed to start `%s': %s\n", argv
[0], strerror(errno
));
595 /* --- Read from the pipe, and write to the socket and logfile --- */
599 ssize_t n
= read(fd
[0], r
->buf
, PKMAX
);
603 putf(r
, logfp
, "\n*** error reading from pipe: %s\n", strerror(errno
));
607 fwrite(r
->buf
, 1, n
, logfp
);
608 pksend(r
, PKTYPE_DATA
, r
->buf
, n
);
612 /* --- Reap exit status and produce final report --- */
616 if (waitpid(kid
, &status
, 0) < 0) {
617 putf(r
, logfp
, "\n*** error reading exit status: %s\n",
620 if (WIFSIGNALED(status
))
621 fprintf(logfp
, "\n*** exited on signal %i\n", WTERMSIG(status
));
622 else if (WIFEXITED(status
))
623 fprintf(logfp
, "\n*** exited with status %i\n", WEXITSTATUS(status
));
625 fprintf(logfp
, "\n*** reaped, but didn't exit. Strange\n");
632 /*----- Syntactic sugar ---------------------------------------------------*/
635 * `Syntactic sugar causes cancer of the semicolon.'
636 * -- Alan Perlis, `Epigrams in Programming'
641 * Arguments: @char **u@ = first segment
642 * @char **v@ = second segment
644 * Returns: Return code from the build.
646 * Use: Combines two @argv@-style arrays and then runs a build on the
650 static int build(char **u
, char **v
)
655 for (i
= 0, p
= u
; *p
; p
++, i
++) ;
656 for (j
= 0, p
= v
; *p
; p
++, j
++) ;
657 p
= xmalloc((i
+ j
+ 2) * sizeof(char **));
658 memcpy(p
+ 1, u
, i
* sizeof(char **));
659 memcpy(p
+ i
+ 1, v
, j
* sizeof(char **));
660 p
[0] = p
[i
+ j
+ 1] = 0;
661 return (sw_run(i
+ j
+ 1, p
));
664 /* --- @sw_make@ --- */
666 int sw_make(int argc
, char *argv
[])
668 static char *mk
[] = { 0, 0 };
671 if ((m
= getenv("SW_MAKE")) == 0 &&
672 (m
= getenv("MAKE")) == 0)
676 return (build(mk
, argv
+ 1));
679 /* --- @sw_conf@ --- */
681 int sw_conf(int argc
, char *argv
[])
683 static char *cf
[] = { "../configure", "--prefix=" PREFIX
, 0 };
684 return (build(cf
, argv
+ 1));
687 /*----- Other subcommands -------------------------------------------------*/
689 /* --- @sw_reset@ --- */
691 int sw_reset(int argc
, char *argv
[])
695 die(1, "Usage: reset");
696 if (swinfo_fetch(&sw
)) {
697 die(1, "couldn't read build status: %s (try running setup)",
703 swinfo_update(&sw
, &skel
);
705 die(1, "couldn't write build status: %s", strerror(errno
));
709 /*----- That's all, folks -------------------------------------------------*/