shithub: mc

Download patch

ref: 61a91a2cc9be621ee517b2cb4114922bd5da351f
parent: 210b24e9cf07baef21fb0fd943f81f38a3196945
author: Ori Bernstein <ori@eigenstate.org>
date: Fri Feb 17 17:21:41 EST 2017

Search for usefiles relative to the output.

	For most uses, this is equivalent to compiler cwd, but
	supports out of tree builds better. It also means that
	builds behave the same way regardless of the directory
	that the compiler is run from, which is convenient for
	testing.

	This also drops support for `.use` suffixes on local
	uses.

--- a/6/main.c
+++ b/6/main.c
@@ -28,6 +28,7 @@
 int p9asm;
 char *outfile;
 char **incpaths;
+char *localincpath;
 size_t nincpaths;
 Asmsyntax asmsyntax;
 
@@ -99,6 +100,17 @@
 	}
 }
 
+static char *dirname(char *path)
+{
+	char *p;
+
+	p = strrchr(path, '/');
+	if (p)
+		return strdupn(path, p - path);
+	else
+		return xstrdup(".");
+}
+
 static char *gentempfile(char *buf, size_t bufsz, char *path, char *suffix)
 {
 	char *tmpdir;
@@ -216,11 +228,15 @@
 	if (ctx.nargs == 0) {
 		fprintf(stderr, "No input files given\n");
 		exit(1);
-	}
-	else if (ctx.nargs > 1)
+	} else if (ctx.nargs > 1)
 		outfile = NULL;
 
 	for (i = 0; i < ctx.nargs; i++) {
+		if (outfile)
+			localincpath = dirname(outfile);
+		else
+			localincpath = dirname(ctx.args[i]);
+
 		globls = mkstab(0);
 		tyinit(globls);
 		tokinit(ctx.args[i]);
@@ -250,6 +266,8 @@
 		genuse(ctx.args[i]);
 		gen(file, buf);
 		assemble(buf, ctx.args[i]);
+
+		free(localincpath);
 	}
 
 	return 0;
--- a/lib/crypto/test/md5.myr
+++ b/lib/crypto/test/md5.myr
@@ -1,7 +1,7 @@
 use std
 use crypto
 
-use "test/util"
+use "util"
 
 const main = {
 	hasheq(crypto.md5("")[:], \
--- a/lib/crypto/test/sha1.myr
+++ b/lib/crypto/test/sha1.myr
@@ -1,7 +1,7 @@
 use std
 use crypto
 
-use "test/util"
+use "util"
 
 const main = {
 	hasheq(crypto.sha1("")[:], \
--- a/lib/crypto/test/sha256.myr
+++ b/lib/crypto/test/sha256.myr
@@ -1,7 +1,7 @@
 use std
 use crypto
 
-use "test/util"
+use "util"
 
 const main = {
 	hasheq(crypto.sha224("")[:], \
--- a/lib/crypto/test/sha512.myr
+++ b/lib/crypto/test/sha512.myr
@@ -1,7 +1,7 @@
 use std
 use crypto
 
-use "test/util"
+use "util"
 
 const main = {
 	hasheq(crypto.sha384("")[:], \
--- a/lib/sys/sys+openbsd-x64.myr
+++ b/lib/sys/sys+openbsd-x64.myr
@@ -1,4 +1,4 @@
-use "systypes.use"
+use "systypes"
 
 pkg sys =
 	type pid	= int32	/* process id */
--- a/lib/thread/condvar+freebsd.myr
+++ b/lib/thread/condvar+freebsd.myr
@@ -1,9 +1,9 @@
 use std
 use sys
 
-use "atomic.use"
-use "common.use"
-use "mutex.use"
+use "atomic"
+use "common"
+use "mutex"
 
 pkg thread =
 	type cond = struct
--- a/lib/thread/condvar+linux.myr
+++ b/lib/thread/condvar+linux.myr
@@ -1,9 +1,9 @@
 use std
 use sys
 
-use "atomic.use"
-use "common.use"
-use "mutex.use"
+use "atomic"
+use "common"
+use "mutex"
 
 pkg thread =
 	type cond = struct
--- a/lib/thread/future.myr
+++ b/lib/thread/future.myr
@@ -1,6 +1,6 @@
 use std
 
-use "mutex.use"
+use "mutex"
 
 pkg thread =
 	type future(@a) = struct
--- a/lib/thread/mutex+freebsd.myr
+++ b/lib/thread/mutex+freebsd.myr
@@ -1,8 +1,8 @@
 use std
 use sys
 
-use "atomic.use"
-use "common.use"
+use "atomic"
+use "common"
 
 pkg thread =
 	type mutex = struct
--- a/lib/thread/mutex+linux.myr
+++ b/lib/thread/mutex+linux.myr
@@ -1,8 +1,8 @@
 use std
 use sys
 
-use "atomic.use"
-use "common.use"
+use "atomic"
+use "common"
 
 pkg thread =
 	type mutex = struct
--- a/lib/thread/mutex+plan9.myr
+++ b/lib/thread/mutex+plan9.myr
@@ -2,8 +2,8 @@
 use sys
 
 
-use "atomic.use"
-use "common.use"
+use "atomic"
+use "common"
 
 pkg thread =
 	type mutex = struct
--- a/lib/thread/mutex.myr
+++ b/lib/thread/mutex.myr
@@ -2,8 +2,8 @@
 use sys
 
 
-use "atomic.use"
-use "common.use"
+use "atomic"
+use "common"
 
 pkg thread =
 	type mutex = struct
--- a/lib/thread/test/atomic.myr
+++ b/lib/thread/test/atomic.myr
@@ -1,7 +1,7 @@
 use std
 use thread
 
-use "test/util.use"
+use "util"
 
 const Nherd = 20
 
--- a/lib/thread/test/condvar.myr
+++ b/lib/thread/test/condvar.myr
@@ -1,7 +1,7 @@
 use std
 use thread
 
-use "test/util.use"
+use "util"
 
 const Nwakes = 1000
 
--- a/lib/thread/test/future.myr
+++ b/lib/thread/test/future.myr
@@ -2,7 +2,7 @@
 use sys
 use thread
 
-use "test/util.use"
+use "util"
 
 var fut
 var nready : int32
--- a/lib/thread/test/mutex.myr
+++ b/lib/thread/test/mutex.myr
@@ -1,7 +1,7 @@
 use std
 use thread
 
-use "test/util.use"
+use "util"
 
 const Nherd = 20
 
--- a/mbld/deps.myr
+++ b/mbld/deps.myr
@@ -148,7 +148,7 @@
 		;;
 		std.exit(1)
 	;;
-	deps = getdeps(b, ds, path)
+	deps = getdeps(b, ds, path, std.dirname(path))
 	std.htput(g.seen, path, true)
 	for d in deps
 		match d
@@ -253,7 +253,7 @@
 	-> (cflags, libs)
 }
 
-const getdeps = {b, ds, path
+const getdeps = {b, ds, path, dir
 	var deps, lnum
 	var f
 
@@ -266,7 +266,7 @@
 		| `bio.Err e:	std.fatal("unable to read {}: {}\n", path, e)
 		| `bio.Eof:	break
 		| `bio.Ok ln:
-			deps = depname(deps, ln, lnum)
+			deps = depname(deps, ln, lnum, dir)
 			std.slfree(ln)
 		;;
 	;;
@@ -288,7 +288,9 @@
 	;;
 }
 
-const depname = {deps, ln, lnum
+const depname = {deps, ln, lnum, dir
+	var p
+
 	/*
 	the regex pattern does some contortions to either grab
 	an unquoted path and put it into uses[4], or a quoted
@@ -299,7 +301,8 @@
 		if uses[2].len > 0
 			std.slpush(&deps, `Lib (std.sldup(uses[2]), lnum))
 		else
-			std.slpush(&deps, `Local (std.sldup(uses[3]), lnum))
+			p = std.pathcat(dir, std.sldup(uses[3]))
+			std.slpush(&deps, `Local (p, lnum))
 		;;
 		regex.matchfree(uses)
 	| `std.None:
--- a/muse/muse.c
+++ b/muse/muse.c
@@ -25,6 +25,7 @@
 size_t nincpaths;
 char **extralibs;
 size_t nextralibs;
+char *localincpath;
 
 static void usage(char *prog)
 {
@@ -94,6 +95,7 @@
 		fprintf(stderr, "output file needed when merging usefiles.\n");
 		exit(1);
 	}
+	localincpath = ".";
 	if (!pkgname)
 		pkgname = outfile;
 
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -559,5 +559,6 @@
 extern char *outfile;
 extern char **incpaths;
 extern size_t nincpaths;
+extern char *localincpath;
 
 void yyerror(const char *s);
--- a/parse/use.c
+++ b/parse/use.c
@@ -1031,18 +1031,18 @@
 	char *t, *p;
 	char buf[512];
 
-	/* local (quoted) uses are always relative to the cwd */
+	/* local (quoted) uses are always relative to the output */
 	fd = NULL;
 	p = NULL;
 	if (use->use.islocal) {
-		if (hassuffix(use->use.name, ".use"))
-			snprintf(buf, sizeof buf, "%s", use->use.name);
-		else
-			snprintf(buf,sizeof buf, "%s.use", use->use.name);
-
+		snprintf(buf,sizeof buf, "%s/%s.use", localincpath, use->use.name);
 		p = strdup(buf);
 		fd = fopen(p, "r");
-		/* nonlocal (barename) uses are always searched on the include path */
+		if (!fd) {
+			fprintf(stderr, "could not open usefile %s\n", buf);
+			exit(1);
+		}
+	/* nonlocal (barename) uses are always searched on the include path */
 	} else {
 		for (i = 0; i < nincpaths; i++) {
 			snprintf(buf, sizeof buf, "lib%s.use", use->use.name);
@@ -1064,12 +1064,12 @@
 			}
 			free(p);
 		}
-	}
-	if (!fd) {
-		fprintf(stderr, "could not open usefile %s in search path:\n", use->use.name);
-		for (i = 0; i < nincpaths; i++)
-			fprintf(stderr, "\t%s\n", incpaths[i]);
-		exit(1);
+		if (!fd) {
+			fprintf(stderr, "could not open usefile %s in search path:\n", use->use.name);
+			for (i = 0; i < nincpaths; i++)
+				fprintf(stderr, "\t%s\n", incpaths[i]);
+			exit(1);
+		}
 	}
 
 	if (!loaduse(p, fd, st, vis))
--- a/util/util.c
+++ b/util/util.c
@@ -24,6 +24,16 @@
 	return ret;
 }
 
+char *xstrdup(char *s)
+{
+	char *p;
+
+	p = strdup(s);
+	if (!p && s)
+		die("Out of memory");
+	return p;
+}
+
 char *strjoin(char *u, char *v)
 {
 	size_t n;
--- a/util/util.h
+++ b/util/util.h
@@ -121,6 +121,7 @@
 void *xrealloc(void *p, size_t size);
 void die(char *msg, ...) FATAL;
 char *strdupn(char *s, size_t len);
+char *xstrdup(char *s);
 char *strjoin(char *u, char *v);
 void *memdup(void *mem, size_t len);
 size_t bprintf(char *buf, size_t len, char *fmt, ...);