2
0

add fennelc, a script to do what writeFennel does

it's a bit simpler because no need to do quite as much string surgery
to compute package.{c,}path

also it can be used in Makefiles
This commit is contained in:
2025-10-26 20:32:52 +00:00
parent 6478c56ed1
commit 6f559e3700
3 changed files with 69 additions and 0 deletions
+1
View File
@@ -85,6 +85,7 @@ in
devout = callPackage ./devout { };
fetch-freebsd = callPackage ./fetch-freebsd { };
fennel = callPackage ./fennel { };
fennelc = callPackage ./fennelc { };
fennelrepl = callPackage ./fennelrepl { };
firewallgen = callPackage ./firewallgen { };
firmware-utils = callPackage ./firmware-utils { };
+19
View File
@@ -0,0 +1,19 @@
{
lua,
runCommand
}:
let fennel = lua.pkgs.fennel; in
runCommand "build-fennelc"
{
nativeBuildInputs = [ fennel ] ;
} ''
LUAPATH=$(ls -d ${fennel}/share/lua/*)
mkdir -p $out/bin
(
exec > $out/bin/fennelc
echo '#! ${lua}/bin/lua'
echo "package.path = \"''${LUAPATH}/?.lua;''${LLPATH}/?.lua;\" .. package.path"
fennel --compile ${./fennelc.fnl}
)
chmod +x $out/bin/fennelc
''
+49
View File
@@ -0,0 +1,49 @@
(local { : view &as fennel } ( require :fennel))
(fn assoc [tbl k v & more]
(tset tbl k v)
(case more
[k v] (assoc tbl k v)
_ tbl))
(fn parse-args [args]
(case args
["--correlate" & rest]
(assoc (parse-args rest)
:correlate true)
["--main" main-fn & rest]
(assoc (parse-args rest)
:main main-fn)
["-o" output-file & rest]
(assoc (parse-args rest)
:output-file output-file)
[source] { :source-file source }))
(fn output-name [opts]
(or opts.output-file
(string.gsub opts.source-file "%.fnl$" "")))
(let [luapath (os.getenv "LUA_PATH")
luacpath (os.getenv "LUA_CPATH")
path (os.getenv "PATH")
opts (parse-args arg)
output-file (output-name opts)]
(with-open [o (io.open output-file :w)]
(o:write "#!/usr/bin/env lua\n")
(and luapath
(o:write (string.format "package.path = %q .. \";\" .. package.path\n" luapath)))
(and luacpath
(o:write (string.format "package.cpath = %q .. \";\" .. package.cpath\n" luacpath)))
(let [(ok? msg)
(pcall
fennel.compile-string
(: (io.open opts.source-file :r) :read "*a")
{:filename opts.source-file
:correlate opts.correlate})]
(when (not ok?)
(error (.. "error: " msg)))
(o:write msg)
(os.execute (string.format "chmod +x %q" output-file)))))