1
0
mirror of https://github.com/janet-lang/janet synced 2025-01-12 16:40:27 +00:00

Fix init.dst, work on metabuild tool to make

native module creation easier
This commit is contained in:
Calvin Rose 2018-08-03 21:50:32 -04:00
parent 9cad4eb91d
commit 169a22f03e
2 changed files with 60 additions and 9 deletions

51
extra/metabuild.dst Normal file
View File

@ -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)

View File

@ -10,7 +10,7 @@
# Flag handlers # Flag handlers
(def handlers :private (def handlers :private
{"h" (fn [] {"h" (fn @[]
(print "usage: " (get args 0) " [options] scripts...") (print "usage: " (get args 0) " [options] scripts...")
(print (print
`Options are: `Options are:
@ -23,17 +23,17 @@
-- Stop handling options`) -- Stop handling options`)
(os.exit 0) (os.exit 0)
1) 1)
"v" (fn [] (print VERSION) (os.exit 0) 1) "v" (fn @[] (print VERSION) (os.exit 0) 1)
"s" (fn [] (:= *raw-stdin* true) (:= *should-repl* true) 1) "s" (fn @[] (:= *raw-stdin* true) (:= *should-repl* true) 1)
"r" (fn [] (:= *should-repl* true) 1) "r" (fn @[] (:= *should-repl* true) 1)
"p" (fn [] (:= *exit-on-error* false) 1) "p" (fn @[] (:= *exit-on-error* false) 1)
"-" (fn [] (:= *handleopts* false) 1) "-" (fn @[] (:= *handleopts* false) 1)
"e" (fn [i] "e" (fn @[i]
(:= *no-file* false) (:= *no-file* false)
(eval (get args (+ i 1))) (eval (get args (+ i 1)))
2)}) 2)})
(defn- dohandler [n i] (defn- dohandler @[n i]
(def h (get handlers n)) (def h (get handlers n))
(if h (h i) (print "unknown flag -" n))) (if h (h i) (print "unknown flag -" n)))
@ -54,7 +54,7 @@
(repl nil identity) (repl nil identity)
(do (do
(print (string "Dst " VERSION " Copyright (C) 2017-2018 Calvin Rose")) (print (string "Dst " VERSION " Copyright (C) 2017-2018 Calvin Rose"))
(repl (fn [buf p] (repl (fn @[buf p]
(def [line] (parser.where p)) (def [line] (parser.where p))
(def prompt (string "dst:" line ":" (parser.state p) "> ")) (def prompt (string "dst:" line ":" (parser.state p) "> "))
(getline prompt buf))))))) (getline prompt buf)))))))