From 169a22f03ea6f3341d8265a718ccda661e759257 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Fri, 3 Aug 2018 21:50:32 -0400 Subject: [PATCH] Fix init.dst, work on metabuild tool to make native module creation easier --- extra/metabuild.dst | 51 +++++++++++++++++++++++++++++++++++++++++ src/mainclient/init.dst | 18 +++++++-------- 2 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 extra/metabuild.dst diff --git a/extra/metabuild.dst b/extra/metabuild.dst new file mode 100644 index 00000000..7a6ecf16 --- /dev/null +++ b/extra/metabuild.dst @@ -0,0 +1,51 @@ +# A script to help generate build files on different platforms + +(def- make-cflags "-std=c99 -Wall -Wextra -O2 -shared -fpic") +(def- make-ldflags "") + +(defn $ + "Do and get the value of a subshell command" + [command] + (def f (file.popen command)) + (def ret (file.read f :all)) + (file.close f) + ret) + +(defn- emit-rule + "Emit a rule in a makefile" + @[out target deps recipe] + (default recipe "@echo '-----'") + (file.write + out + target + ": " + (if (indexed? deps) (string.join deps " ") deps) + "\n\t" + (if (indexed? recipe) (string.join recipe "\n\t") recipe) + "\n\n") + out) + +(defn generate-make + "Generate a makefile" + [out-path sources target] + (def out (file.open out-path :w)) + (def csources (filter (fn [x] (= ".c" (string.slice x -2))) sources)) + (def hsources (filter (fn [x] (= ".h" (string.slice x -2))) sources)) + (file.write + out + "# Autogenerated Makefile, do not edit\n" + "# Generated at " ($ `date`) + "\nCFLAGS:=" make-cflags + "\nLDFLAGS:=" make-ldflags + "\nSOURCES:=" (string.join csources " ") + "\nHEADERS:=" (string.join hsources " ") + "\nOBJECTS:=$(patsubst %.c,%.o,${SOURCES})" + "\nTARGET:=" target ".so" + "\n\n") + (emit-rule out "all" "${TARGET}") + (emit-rule out "%.o" @["%.c" "${HEADERS}"] "${CC} ${CFLAGS} -o $@ $< ${LDFLAGS}") + (emit-rule out "${TARGET}" "${SOURCES}" "${CC} ${CFLAGS} -o $@ $^ ${LDFLAGS}") + (emit-rule out "clean" "" "rm ${OBJECTS}") + # Phony targets + (emit-rule out ".PHONY" @["all" "clean"]) + nil) diff --git a/src/mainclient/init.dst b/src/mainclient/init.dst index 348c427d..ea590dd8 100644 --- a/src/mainclient/init.dst +++ b/src/mainclient/init.dst @@ -10,7 +10,7 @@ # Flag handlers (def handlers :private - {"h" (fn [] + {"h" (fn @[] (print "usage: " (get args 0) " [options] scripts...") (print `Options are: @@ -23,17 +23,17 @@ -- Stop handling options`) (os.exit 0) 1) - "v" (fn [] (print VERSION) (os.exit 0) 1) - "s" (fn [] (:= *raw-stdin* true) (:= *should-repl* true) 1) - "r" (fn [] (:= *should-repl* true) 1) - "p" (fn [] (:= *exit-on-error* false) 1) - "-" (fn [] (:= *handleopts* false) 1) - "e" (fn [i] + "v" (fn @[] (print VERSION) (os.exit 0) 1) + "s" (fn @[] (:= *raw-stdin* true) (:= *should-repl* true) 1) + "r" (fn @[] (:= *should-repl* true) 1) + "p" (fn @[] (:= *exit-on-error* false) 1) + "-" (fn @[] (:= *handleopts* false) 1) + "e" (fn @[i] (:= *no-file* false) (eval (get args (+ i 1))) 2)}) - (defn- dohandler [n i] + (defn- dohandler @[n i] (def h (get handlers n)) (if h (h i) (print "unknown flag -" n))) @@ -54,7 +54,7 @@ (repl nil identity) (do (print (string "Dst " VERSION " Copyright (C) 2017-2018 Calvin Rose")) - (repl (fn [buf p] + (repl (fn @[buf p] (def [line] (parser.where p)) (def prompt (string "dst:" line ":" (parser.state p) "> ")) (getline prompt buf)))))))