@@ -14,11 +14,25 @@ # if any are not present, the build will fail, although a missing # `which` can be worked around by specifying the paths to lua, luac, # and `sh` directly # -# eventually you will be able to set a "standalone" variable to -# create a truly standalone binary, by embedding the binary in a -# C program and statically linking it to lua. +# to create a truly standalone cortav binary, build the target +# $(build)/cortav.bin - this will wrap the cortav bytecode in a C +# shim and produce a binary executable. depending on your needs, +# you may want to link lua statically or as a shared library. +# to do this, you'll need to set the variable lua-prefix to the +# location of your lua build. if you have lua installed systemwide, +# this will probably be something like `lua-lib-prefix=/usr/lib`, +# or on Nix `lua-lib-prefix=$(nix path-info lua5_3)/lib`. normally +# however if you're building a standalone binary, you probably don't +# have lua installed systemwide, so you'll want to set this to the +# directory where liblua.a is to be found. +# +# note that the standalone build script assumes you're using a +# relatively standard cc, not something like msvc. if your cc +# doesn't accept options like -l -x -o then you'll have to build +# the binary by hand, sorry. but if you want to contribute a build +# script to the repository, i'll happily take merge requests :) lua != which lua luac != which luac sh != which sh @@ -34,8 +48,22 @@ prefix = $(HOME)/.local bin-prefix = $(prefix)/bin share-prefix = $(prefix)/share/$(executable) + +lua-standalone = $(if $(lua-lib-prefix),$(lua-lib-prefix)/liblua.a,-llua) +lua-bindeps = -lm -ldl + +dbg-flags-luac = $(if $(debug),,-s) +dbg-flags-cc = $(if $(debug),-g,-s) + +# sterilize the operating theatre +export LUA_PATH=./?.lua;./?.lc +export LUA_PATH_5_3=./?.lc;./?.lua +export LUA_PATH_5_4=./?.lc;./?.lua +export LUA_INIT= +export LUA_INIT_5_3= +export LUA_INIT_5_4= # by default, we fetch and parse information about encodings we # support so that cortav can do fancy things like format math # equations by character class (e.g. italicizing variables) @@ -45,14 +73,24 @@ encoding-data = ucstbls encoding-files = $(patsubst %,$(build)/%.lc,$(encoding-data)) encoding-data-ucs = https://www.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt -$(build)/$(executable): sirsem.lua $(encoding-files) cortav.lua $(rendrs) $(extens) cli.lua | $(build)/ +# "standalone" bytecode file that can be run directly as a script +$(build)/$(executable): $(build)/$(executable).ct + echo '#!$(lua)' > $@ + cat $< >>$@ + chmod +x $@ + +# raw bytecode without shebang header, must be run as `lua cortav.lc` +$(build)/$(executable).lc: sirsem.lua $(encoding-files) cortav.lua $(rendrs) $(extens) cli.lua | $(build)/ @echo ' » building with extensions $(extens-names)' @echo ' » building with renderers $(rendrs-names)' - echo '#!$(lua)' > $@ - luac -o - $^ >> $@ - chmod +x $@ + $(luac) $(dbg-flags-luac) -o $@ $^ + +# true standalone binary, wraps bytecode file and (optionally) lua +$(build)/$(executable).bin: $(build)/$(executable).lc + $(lua) tools/makeshim.lua $< |\ + $(CC) -o$@ -xc - -xnone $(lua-standalone) $(lua-bindeps) $(build)/cortav.html: cortav.ct $(build)/$(executable) | $(build)/ $(build)/$(executable) $< -o $@ -m render:format html -y html:fossil-uv @@ -60,11 +98,15 @@ syncdoc: $(build)/cortav.html fossil uv add $< --as cortav.html fossil uv sync +# clean is written in overly cautious fashion to minimize damage, +# just in case it ever gets invoked in a bad way .PHONY: clean clean: - rm -f $(build)/cortav $(build)/cortav.html $(build)/velartrill-cortav-view.desktop $(build)/cortav-view.sh + rm -f $(build)/*.{html,lc,sh,txt,desktop} \ + $(build)/$(executable){,.bin} + rmdir $(build) $(build)/%.sh: desk/%.sh echo >$@ "#!$(sh)" echo >>$@ 'cortav_exec="$(bin-prefix)/$(executable)"'