main compile.c
  1#define _XOPEN_SOURCE 700
  2
  3#include <errno.h>
  4#include <fcntl.h>
  5#include <signal.h>
  6#include <spawn.h>
  7#include <stdio.h>
  8#include <stdlib.h>
  9#include <string.h>
 10#include <unistd.h>
 11#include <sys/wait.h>
 12
 13#include "util.h"
 14#include "kav.h"
 15
 16#define countof(a) (sizeof(a) / sizeof((a)[0]))
 17
 18extern char **environ;
 19
 20struct stage {
 21	const char *name;
 22	pid_t pid;
 23};
 24
 25static struct stage stages[] = {{"qbe", 0}, {"cc", 0}};
 26static const char *output;
 27
 28static char *
 29rtpath(void) {
 30	const char *dir = getenv("KAVPATH");
 31	const char *colon;
 32	stralloc sa = {0};
 33	usize len;
 34
 35	if(!dir)
 36		dir = "/usr/lib/kav";
 37	for(;;) {
 38		colon = strchr(dir, ':');
 39		len = colon ? (usize)(colon - dir) : strlen(dir);
 40		sa.len = 0;
 41		sacatb(&sa, dir, len);
 42		sacatl(&sa, "/rt/x86_64.s");
 43		sa0(&sa);
 44		if(len && access(sa.s, R_OK) == 0)
 45			return sa.s;
 46		if(!colon)
 47			die("unable to find rt/x86_64.s");
 48		dir = colon + 1;
 49	}
 50}
 51
 52static void
 53mkpipe(int fd[2]) {
 54	if(pipe(fd) < 0)
 55		diesys("unable to create pipe");
 56	if(fcntl(fd[0], F_SETFD, FD_CLOEXEC) < 0 || fcntl(fd[1], F_SETFD, FD_CLOEXEC) < 0)
 57		diesys("unable to set close-on-exec");
 58}
 59
 60/* run argv with in as its stdin and out as its stdout, -1 leaves them alone */
 61static pid_t
 62spawn(char *const *argv, int in, int out) {
 63	posix_spawn_file_actions_t fa;
 64	pid_t pid;
 65	int err;
 66
 67	if((err = posix_spawn_file_actions_init(&fa)))
 68		goto fail;
 69	if(in != -1 && (err = posix_spawn_file_actions_adddup2(&fa, in, 0)))
 70		goto fail;
 71	if(out != -1 && (err = posix_spawn_file_actions_adddup2(&fa, out, 1)))
 72		goto fail;
 73	err = posix_spawnp(&pid, argv[0], &fa, nil, argv, environ);
 74	posix_spawn_file_actions_destroy(&fa);
 75	if(!err)
 76		return pid;
 77fail:
 78	errno = err;
 79	diesys("unable to spawn ", argv[0]);
 80}
 81
 82static int
 83succeeded(struct stage *s, int st) {
 84	if(WIFEXITED(st)) {
 85		if(WEXITSTATUS(st) == 0)
 86			return 1;
 87		fputs(PROG, stderr);
 88		fputs(": ", stderr);
 89		fputs(s->name, stderr);
 90		fputs(" failed\n", stderr);
 91	} else if(WIFSIGNALED(st)) {
 92		fputs(PROG, stderr);
 93		fputs(": ", stderr);
 94		fputs(s->name, stderr);
 95		fputs(": ", stderr);
 96		fputs(strsignal(WTERMSIG(st)), stderr);
 97		fputs("\n", stderr);
 98	}
 99	return 0;
100}
101
102static void
103cleanup(void) {
104	usize i;
105	int st;
106
107	for(i = 0; i < countof(stages); i++) {
108		if(!stages[i].pid)
109			continue;
110		kill(stages[i].pid, SIGTERM);
111		waitpid(stages[i].pid, &st, 0);
112		stages[i].pid = 0;
113	}
114	if(output)
115		unlink(output);
116}
117
118void
119compile(struct module *m, const char *out) {
120	int toqbe[2];
121	int tocc[2];
122	char *rt;
123	int ok = 1;
124	int st;
125	pid_t pid;
126	usize i;
127	usize n;
128
129	rt = rtpath();
130	output = out;
131	signal(SIGPIPE, SIG_IGN);
132	atexit(cleanup);
133	mkpipe(toqbe);
134	mkpipe(tocc);
135	stages[0].pid = spawn((char *const[]){"qbe", nil}, toqbe[0], tocc[1]);
136	stages[1].pid = spawn((char *const[]){"cc", "-static", "-Wl,--gc-sections", "-o", (char *)out, rt,
137		"-x", "assembler", "-", nil}, tocc[0], -1);
138	close(toqbe[0]);
139	close(tocc[0]);
140	close(tocc[1]);
141
142	fflush(stdout);
143	if(dup2(toqbe[1], 1) < 0)
144		diesys("unable to redirect output");
145	close(toqbe[1]);
146	qbe(m);
147	fflush(stdout);
148	close(1);
149
150	for(n = countof(stages); n > 0; ) {
151		pid = wait(&st);
152		if(pid < 0)
153			diesys("unable to wait");
154		for(i = 0; i < countof(stages); i++)
155			if(stages[i].pid == pid)
156				break;
157		if(i == countof(stages))
158			continue;
159		stages[i].pid = 0;
160		n--;
161		if(!succeeded(&stages[i], st))
162			ok = 0;
163	}
164	if(!ok)
165		exit(1);
166	output = nil;
167}