2018-08-04 01:50:32 +00:00
|
|
|
# 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}")
|
2018-08-07 04:54:47 +00:00
|
|
|
(emit-rule out "${TARGET}" "${OBJECTS}" "${CC} ${CFLAGS} -o $@ $^ ${LDFLAGS}")
|
2018-08-04 01:50:32 +00:00
|
|
|
(emit-rule out "clean" "" "rm ${OBJECTS}")
|
2018-08-07 00:46:26 +00:00
|
|
|
(emit-rule out "clean" "" @["rm ${OBJECTS}" "rm @{TARGET}"])
|
2018-08-04 01:50:32 +00:00
|
|
|
# Phony targets
|
|
|
|
(emit-rule out ".PHONY" @["all" "clean"])
|
|
|
|
nil)
|