summaryrefslogtreecommitdiff
path: root/myrbuild
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2012-09-28 04:35:09 -0400
committerOri Bernstein <ori@eigenstate.org>2012-09-28 04:35:09 -0400
commit4ff9f95d84eb12bbbb0895669cc888eea1867bf5 (patch)
tree6769dc71253e9d9b7cd4cf1944ca013ebb8d8f9f /myrbuild
parent5cdfb5869c227819c6aa69b9337d9cafe7882444 (diff)
downloadmc-4ff9f95d84eb12bbbb0895669cc888eea1867bf5.tar.gz
Myrbuild now correctly walks dependencies.
Diffstat (limited to 'myrbuild')
-rw-r--r--myrbuild/myrbuild.c122
1 files changed, 114 insertions, 8 deletions
diff --git a/myrbuild/myrbuild.c b/myrbuild/myrbuild.c
index 432e0f2..949dd38 100644
--- a/myrbuild/myrbuild.c
+++ b/myrbuild/myrbuild.c
@@ -1,26 +1,33 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
-#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/wait.h>
+#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
-#include <sys/types.h>
#include <regex.h>
#include "parse.h"
+#include "../config.h"
+
/* make libparse happy */
Node *file;
char *filename;
+/* the name of the output file */
char *libname;
char *binname;
+/* additional paths to search for packages */
char **incpaths;
size_t nincpaths;
+/* libraries to link against. */
+char **libs;
+size_t nlibs;
regex_t usepat;
@@ -33,28 +40,127 @@ static void usage(char *prog)
printf("\t-I path\tAdd 'path' to use search path\n");
}
-void getdeps(char *file)
+int hassuffix(char *path, char *suffix)
+{
+ int pathlen;
+ int sufflen;
+
+ pathlen = strlen(path);
+ sufflen = strlen(suffix);
+
+ if (sufflen > pathlen)
+ return 0;
+ return !strcmp(&path[pathlen-sufflen], suffix);
+}
+
+int isquoted(char *path)
+{
+ return path[0] == '"' && path[strlen(path) - 1] == '"';
+}
+
+char *fromuse(char *path)
+{
+ char buf[1024];
+ /* skip initial quote */
+ path++;
+ swapsuffix(buf, 1024, path, ".use\"", ".myr");
+ return strdup(buf);
+}
+
+int run(char *bin, char **inc, size_t ninc, char *file)
+{
+ char **args;
+ size_t i, nargs;
+ pid_t pid;
+ int status;
+
+ nargs = 0;
+ args = NULL;
+
+ lappend(&args, &nargs, bin);
+ for (i = 0; i < nincpaths; i++) {
+ lappend(&args, &nargs, "-I");
+ lappend(&args, &nargs, incpaths[i]);
+ }
+ lappend(&args, &nargs, file);
+ lappend(&args, &nargs, NULL);
+ for (i = 0; args[i]; i++)
+ printf("%s\t", args[i]);
+ printf("\n");
+ pid = fork();
+ if (pid == -1) {
+ die("Could not fork\n");
+ } else if (pid == 0) {
+ if (execvp(bin, args) == -1)
+ die("Failed to exec %s\n", bin);
+ } else {
+ waitpid(pid, &status, 0);
+ }
+ return WIFEXITED(status) && WEXITSTATUS(status) == 0;
+}
+
+int isfresh(char *from, char *to)
+{
+ struct stat from_sb, to_sb;
+
+ if (stat(from, &from_sb))
+ die("Could not find %s\n", from);
+ if (stat(to, &to_sb) == -1)
+ return 1;
+
+ return from_sb.st_mtime >= to_sb.st_mtime;
+}
+
+void getdeps(char *file, char **deps, size_t depsz, size_t *ndeps)
{
char buf[2048]; /* if you hit this limit, shoot yourself */
regmatch_t m[2];
- char *usepath;
+ size_t i;
FILE *f;
f = fopen(file, "r");
if (!f)
die("Could not open file %s\n", file);
+ i = 0;
while (fgets(buf, sizeof buf, f)) {
if (regexec(&usepat, buf, 2, m, 0) == REG_NOMATCH)
continue;
- usepath = strdupn(&buf[m[1].rm_so], m[1].rm_eo - m[1].rm_so);
- printf("use = %s\n", usepath);
+ if (i == depsz)
+ die("Too many deps for file %s\n", file);
+ deps[i++] = strdupn(&buf[m[1].rm_so], m[1].rm_eo - m[1].rm_so);
}
+ *ndeps = i;
}
-void compile()
+void compile(char *file)
{
+ size_t i, ndeps;
+ char *localdep;
+ char *deps[512];
+ char buf[1024];
+
+ if (hassuffix(file, ".myr")) {
+ getdeps(file, deps, 512, &ndeps);
+ for (i = 0; i < ndeps; i++) {
+ if (isquoted(deps[i])) {
+ localdep = fromuse(deps[i]);
+ compile(localdep);
+ free(localdep);
+ } else {
+ lappend(&libs, &nlibs, deps[i]);
+ }
+ }
+ swapsuffix(buf, sizeof buf, file, ".myr", ".use");
+ if (isfresh(file, buf))
+ run("muse", incpaths, nincpaths, file);
+
+ swapsuffix(buf, sizeof buf, file, ".myr", ".o");
+ if (isfresh(file, buf))
+ run("6m", incpaths, nincpaths, file);
+ } else if (hassuffix(file, ".s")) {
+ }
}
int main(int argc, char **argv)
@@ -89,7 +195,7 @@ int main(int argc, char **argv)
regcomp(&usepat, "^[[:space:]]*use[[:space:]]+([^[:space:]]+)", REG_EXTENDED);
for (i = optind; i < argc; i++)
- getdeps(argv[i]);
+ compile(argv[i]);
return 0;
}