shithub: scc

Download patch

ref: ac7e1ec4946bd0460dfa82358abe7c225bee8501
parent: 5e9e3c00f29aabac133ecce2b1b3c7a29b5816a2
author: Quentin Rameau <quinq@fifth.space>
date: Thu Jun 2 12:08:31 EDT 2016

[driver] add support for naming intermediary files

--- a/driver/posix/scc.c
+++ b/driver/posix/scc.c
@@ -30,6 +30,7 @@
 	char  cmd[PATH_MAX];
 	char *args[NARGS];
 	char  bin[16];
+	char *outfile;
 	int   nargs, in, out;
 	pid_t pid;
 } tools[NR_TOOLS] = {
@@ -90,15 +91,58 @@
 	return tool;
 }
 
+char *
+newfileext(char *name, char *ext)
+{
+	char *new, *dot;
+	size_t newsz, nameln = strlen(name);
+	int n;
+
+	if (!(dot = strrchr(name, '.')))
+		dot = &name[nameln];
+
+	nameln = nameln - strlen(dot);
+	newsz  = nameln + strlen(ext) + 1 + 1;
+
+	new = xmalloc(newsz);
+
+	n = snprintf(new, newsz, "%.*s.%s", nameln, name, ext);
+	if (n < 0 || n >= newsz)
+		die("wrong output filename");
+
+	return new;
+}
+
 int
-settool(int tool, int output)
+settool(int tool, char *input, int output)
 {
 	struct tool *t = &tools[tool];
 	int fds[2];
+	char *ext;
 	static int fdin;
 
-	if (tool == TEE)
-		t->args[1] = "out.ir";
+	switch (tool) {
+	case CC1:
+		ADDARG(tool, input);
+		break;
+	case TEE:
+		switch (output) {
+		case CC2:
+			ext = "ir"; break;
+		case QBE:
+			ext = "qbe"; break;
+		case NR_TOOLS:
+			if (!Sflag)
+				break;
+		case AS:
+			ext = "as"; break;
+		}
+		t->outfile = newfileext(input, ext);
+		t->args[1] = t->outfile;
+		break;
+	default:
+		break;
+	}
 
 	if (fdin) {
 		t->in = fdin;
@@ -156,7 +200,6 @@
 			out = Eflag ? NR_TOOLS : CC2;
 			if (!Eflag)
 				keepfile = kflag;
-			ADDARG(tool, file);
 			break;
 		case CC2:
 			if (!arch || strcmp(arch, "qbe")) {
@@ -186,7 +229,9 @@
 			out = TEE;
 		}
 
-		spawn(settool(inittool(tool), out));
+		spawn(settool(inittool(tool), file, out));
+
+		free(tools[tool].outfile);
 	}
 	for (i = 0; i < NR_TOOLS; ++i) {
 		if ((pid = tools[i].pid) == 0)