Build static archive; build static interpreter where possible

This commit is contained in:
K. Lange 2021-04-09 21:50:44 +09:00
parent 40dcc7e4ea
commit dc91091de4

127
Makefile
View File

@ -1,13 +1,16 @@
CFLAGS ?= -g -O3 -Wall -Wextra -pedantic -Wno-unused-parameter CFLAGS ?= -g -O3 -Wall -Wextra -pedantic -Wno-unused-parameter
CFLAGS += -Isrc CFLAGS += -Isrc
LDFLAGS += -L.
TARGET = kuroko TARGET = kuroko
OBJS = $(patsubst %.c, %.o, $(filter-out src/module_% src/kuroko.c,$(sort $(wildcard src/*.c)))) OBJS = $(patsubst %.c, %.o, $(filter-out src/module_% src/kuroko.c,$(sort $(wildcard src/*.c))))
SOOBJS = $(patsubst %.o, %.lo, $(OBJS))
MODULES = $(patsubst src/module_%.c, modules/%.so, $(sort $(wildcard src/module_*.c))) MODULES = $(patsubst src/module_%.c, modules/%.so, $(sort $(wildcard src/module_*.c)))
HEADERS = $(wildcard src/kuroko/*.h) HEADERS = $(wildcard src/kuroko/*.h)
TOOLS = $(patsubst tools/%.c, krk-%, $(sort $(wildcard tools/*.c))) TOOLS = $(patsubst tools/%.c, krk-%, $(sort $(wildcard tools/*.c)))
GENMODS = modules/codecs/sbencs.krk modules/codecs/dbdata.krk GENMODS = modules/codecs/sbencs.krk modules/codecs/dbdata.krk
BIN_OBJS = $(OBJS)
# These are used by the install target. We call the local kuroko to get the # These are used by the install target. We call the local kuroko to get the
# version string to use for the final library, so, uh, probably don't # version string to use for the final library, so, uh, probably don't
@ -16,113 +19,75 @@ VERSION = $(shell ./kuroko --version | sed 's/.* //')
SONAME = libkuroko-$(VERSION).so SONAME = libkuroko-$(VERSION).so
KRKMODS = $(wildcard modules/*.krk modules/*/*.krk modules/*/*/*.krk) KRKMODS = $(wildcard modules/*.krk modules/*/*.krk modules/*/*/*.krk)
ifndef KRK_ENABLE_STATIC all: ${TARGET} ${MODULES} ${TOOLS} ${GENMODS}
# The normal build configuration is as a shared library or DLL (on Windows)
all: ${TARGET} ${MODULES} ${TOOLS} ${GENMODS} ifeq (,$(findstring mingw,$(CC)))
CFLAGS += -fPIC LDLIBS += -ldl -lpthread
ifeq (,$(findstring mingw,$(CC))) BIN_FLAGS = -rdynamic
# We set rpath here mostly so you can run the locally-built interpreter LIBRARY = libkuroko.so
# with the correct library; it shouldn't be needed in a real installation. ifeq (Darwin,$(shell uname -s))
LDFLAGS += -Wl,-rpath -Wl,'$$ORIGIN' -L. MODLIBS += -undefined dynamic_lookup -DKRK_MEDIOCRE_TLS
# On POSIX-like platforms, link with libdl and assume -lkuroko gives us
# our own library.
LDLIBS += -ldl -lpthread
ifeq (Darwin,$(shell uname -s))
# macOS needs us to link modules back to the main library at build time
MODLIBS = libkuroko.so
endif
else
# For Windows, disable format string warnings because gcc will get mad
# about non-portable Windows format specifiers...
CFLAGS += -Wno-format -static-libgcc
# And we need to link this by name with extension because I don't want
# to actually rename it to kuroko.dll or whatever.
MODLIBS = libkuroko.so
${OBJS}: CFLAGS += -DKRKINLIB
libkuroko.so: LDLIBS += -l:libwinpthread.a -Wl,--require-defined=tc_malloc libtcmalloc_minimal.a -l:libpsapi.a -l:libstdc++.a
libkuroko.so: libtcmalloc_minimal.a
modules/socket.so: LDLIBS += -lws2_32
endif endif
KUROKO_LIBS = libkuroko.so
else else
# Static builds are a little different... CFLAGS += -Wno-format -static-libgcc
CFLAGS +=-DSTATIC_ONLY ${SOOBJS}: CFLAGS += -DKRKINLIB
LDFLAGS += -static BIN_OBJS =
all: ${TARGET} ${GENMODS} LIBRARY = libkuroko.dll
KUROKO_LIBS = ${OBJS} -lpthread kuroko: LDLIBS = -lkuroko
kuroko: ${LIBRARY}
MODLIBS += -lkuroko
modules/socket.so: MODLIBS += -lws2_32
endif endif
ifdef KRK_NO_DOCUMENTATION ifdef KRK_DISABLE_DOCS
CFLAGS += -DKRK_NO_DOCUMENTATION -Wno-unused-value CFLAGS += -DKRK_NO_DOCUMENTATION -Wno-unused-value
endif endif
ifndef KRK_DISABLE_RLINE ifndef KRK_DISABLE_RLINE
# Normally, we link the rich line editor into the BIN_OBJS += src/vendor/rline.o
# interpreter (and not the main library!)
KUROKO_LIBS += src/vendor/rline.o
else else
# ... but it can be disabled if you want a more "pure" build,
# or if you don't have solid support for the escape sequences
# it requires on your target platform.
CFLAGS += -DNO_RLINE CFLAGS += -DNO_RLINE
endif endif
ifndef KRK_DISABLE_DEBUG ifndef KRK_DISABLE_DEBUG
# Disabling debug functions doesn't really do much; it may result in a smaller
# library when stripped as there's a lot of debug text, but no performance
# difference has ever been noted from disabling, eg., instruction tracing.
CFLAGS += -DKRK_ENABLE_DEBUG CFLAGS += -DKRK_ENABLE_DEBUG
endif endif
ifdef KRK_ENABLE_BUNDLE
# When bundling, disable shared object modules.
MODULES =
# Add the sources from the shared object modules as regular sources.
KUROKO_LIBS += $(patsubst %.c,%.o,$(sort $(wildcard src/module_*.c)))
# Enable the build flag so the interpreter binary knows to run startup functions
CFLAGS += -DBUNDLE_LIBS=1
# And link anything our core modules would have needed
LDLIBS += -lm
ifeq (,$(findstring mingw,$(CC)))
LDLIBS += -lws2_32
endif
endif
.PHONY: help .PHONY: help
help: help:
@echo "Configuration options available:" @echo "Configuration options available:"
@echo " KRK_DISABLE_RLINE=1 Do not build with the rich line editing library enabled." @echo " KRK_DISABLE_RLINE=1 Do not build with the rich line editing library enabled."
@echo " KRK_DISABLE_DEBUG=1 Disable debugging features (might be faster)." @echo " KRK_DISABLE_DEBUG=1 Disable debugging features (might be faster)."
@echo " KRK_ENABLE_STATIC=1 Build a single static binary." @echo " KRK_DISABLE_DOCS=1 Do not include docstrings for builtins."
@echo " KRK_ENABLE_BUNDLE=1 Link C modules directly into the interpreter."
@echo " KRK_NO_DOCUMENTATION=1 Do not include docstrings for builtins."
@echo "" @echo ""
@echo "Available tools: ${TOOLS}" @echo "Available tools: ${TOOLS}"
kuroko: src/kuroko.o ${KUROKO_LIBS} kuroko: src/kuroko.c ${BIN_OBJS} ${HEADERS}
${CC} ${CFLAGS} ${LDFLAGS} -o $@ src/kuroko.o ${KUROKO_LIBS} ${CC} ${CFLAGS} ${LDFLAGS} ${BIN_FLAGS} -o $@ $< ${BIN_OBJS} ${LDLIBS}
krk-%: tools/%.c ${KUROKO_LIBS} krk-%: tools/%.c ${LIBRARY} ${HEADERS}
${CC} -Itools ${CFLAGS} ${LDFLAGS} -o $@ $< ${KUROKO_LIBS} ${CC} -Itools ${CFLAGS} ${LDFLAGS} -o $@ $< -lkuroko
libkuroko.so: ${OBJS} libkuroko.so: ${SOOBJS} ${HEADERS}
${CC} ${CFLAGS} ${LDFLAGS} -shared -o $@ ${OBJS} ${LDLIBS} ${CC} ${CFLAGS} ${LDFLAGS} -fPIC -shared -o $@ ${SOOBJS} ${LDLIBS}
# Make sure we rebuild things when headers change as we have a lot of WINLIBS= -l:libwinpthread.a -Wl,--require-defined=tc_malloc libtcmalloc_minimal.a -l:libpsapi.a -l:libstdc++.a
# headers that define build flags... libkuroko.dll: ${SOOBJS} ${HEADERS} libtcmalloc_minimal.a
%.o: ${HEADERS} ${CC} ${CFLAGS} ${LDFLAGS} -fPIC -shared -o $@ ${SOOBJS} ${WINLIBS} -Wl,--export-all-symbols,--out-implib,libkuroko.a
# Modules are built as shared objects. We link them with LDLIBS libkuroko.a: ${OBJS}
# as well, but this probably isn't necessary? ${AR} ${ARFLAGS} $@ ${OBJS}
modules/%.so: src/module_%.c libkuroko.so
${CC} ${CFLAGS} ${LDFLAGS} -shared -o $@ $< ${LDLIBS} ${MODLIBS}
# A module can have dependencies that didn't exist in the main lib, %.o: %.c ${HEADERS}
# like how the math library pulls in libm but we kept references ${CC} ${CFLAGS} -c -o $@ $<
# to that out of the main interpreter.
modules/math.so: src/module_math.c libkuroko.so %.lo: %.c ${HEADERS}
${CC} ${CFLAGS} ${LDFLAGS} -shared -o $@ $< -lm ${LDLIBS} ${MODLIBS} ${CC} ${CFLAGS} -fPIC -c -o $@ $<
modules/math.so: MODLIBS += -lm
modules/%.so: src/module_%.c ${LIBRARY}
${CC} ${CFLAGS} ${LDFLAGS} -fPIC -shared -o $@ $< ${LDLIBS} ${MODLIBS}
modules/codecs/sbencs.krk: tools/codectools/gen_sbencs.krk tools/codectools/encodings.json tools/codectools/indexes.json | kuroko modules/codecs/sbencs.krk: tools/codectools/gen_sbencs.krk tools/codectools/encodings.json tools/codectools/indexes.json | kuroko
./kuroko tools/codectools/gen_sbencs.krk ./kuroko tools/codectools/gen_sbencs.krk
@ -132,8 +97,11 @@ modules/codecs/dbdata.krk: tools/codectools/gen_dbdata.krk tools/codectools/enco
.PHONY: clean .PHONY: clean
clean: clean:
@rm -f ${OBJS} ${TARGET} ${MODULES} libkuroko.so *.so.debug src/*.o src/vendor/*.o kuroko.exe ${TOOLS} $(patsubst %,%.exe,${TOOLS}) -rm -f ${OBJS} ${SOOBJS} ${TARGET} ${MODULES}
@rm -rf docs/html -rm -f libkuroko.so libkuroko.a libkuroko.dll *.so.debug
-rm -f src/*.o src/*.lo src/vendor/*.o
-rm -f kuroko.exe ${TOOLS} $(patsubst %,%.exe,${TOOLS})
-rm -rf docs/html *.dSYM modules/*.dSYM
tags: $(wildcard src/*.c) $(wildcard src/*.h) tags: $(wildcard src/*.c) $(wildcard src/*.h)
@ctags --c-kinds=+lx src/*.c src/*.h src/kuroko/*.h src/vendor/*.h @ctags --c-kinds=+lx src/*.c src/*.h src/kuroko/*.h src/vendor/*.h
@ -186,6 +154,7 @@ install: all libkuroko.so ${HEADERS} $(KRKMODS) $(MODULES)
@echo "Installing libraries..." @echo "Installing libraries..."
$(INSTALL_PROGRAM) libkuroko.so $(DESTDIR)$(libdir)/$(SONAME) $(INSTALL_PROGRAM) libkuroko.so $(DESTDIR)$(libdir)/$(SONAME)
ln -s -f $(SONAME) $(DESTDIR)$(libdir)/libkuroko.so ln -s -f $(SONAME) $(DESTDIR)$(libdir)/libkuroko.so
$(INSTALL_DATA) libkuroko.a $(DESTDIR)$(libdir)/
@echo "Installing source modules..." @echo "Installing source modules..."
$(INSTALL_DATA) modules/*.krk $(DESTDIR)$(bindir)/../lib/kuroko/ $(INSTALL_DATA) modules/*.krk $(DESTDIR)$(bindir)/../lib/kuroko/
$(INSTALL_DATA) modules/foo/*.krk $(DESTDIR)$(bindir)/../lib/kuroko/foo/ $(INSTALL_DATA) modules/foo/*.krk $(DESTDIR)$(bindir)/../lib/kuroko/foo/