Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/rockspec.template
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ build = {
},
install_variables = {
LIBDIR="$(LIBDIR)",
BINDIR="$(BINDIR)",
LUADIR="$(LUADIR)",
},
}
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
**/result
**/lib
**/result-*
**/repl-result-*
**/.cache
build/
**/compile_commands.json
65 changes: 55 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
src ?= .
SRC ?= $(src)
SRC := $(abspath $(SRC))
DESTDIR ?= $(SRC)/lib
DESTDIR ?= ./build/lib
CC ?= gcc
LUA ?= lua
BEAR ?= bear
GREP ?= grep
CFLAGS ?= -fPIC -x c -O3 -flto -Wl,-s -Winline
LIBFLAG ?= -shared

ifdef LUA_INC
LUA_INCDIR ?= $(LUA_INC)
endif

CFLAGS += $(LIBFLAG) -I"$(LUA_INCDIR)"
TESTDIR := $(SRC)/tests
SRCS := $(SRC)/src/tomlua.c \
$(SRC)/src/decode.c \
$(SRC)/src/encode.c \
$(SRC)/src/dates.c

BENCH_ITERS ?= 100000
ifdef out
PREFIX ?= $(out)
endif
ifdef PREFIX
BINDIR ?= $(PREFIX)/bin
LUADIR ?= $(PREFIX)/lua
LIBDIR ?= $(PREFIX)/lib
endif

FIX_SHEBANG := print(string.format("\#!%s\\npackage.cpath = %q .. [[/?.so;]] .. package.cpath\\n-- ", arg[1], arg[2]))

check_lua_incdir = \
@if [ -z "$(LUA_INCDIR)" ]; then \
Expand All @@ -30,15 +42,36 @@ check_so_was_built = \
false; \
fi

define newline


endef
define FIX_BEAR_RESULT
local input, tmp = "compile_commands.json", "compile_commands.tmp";
local infile = assert(io.open(input, "r"));
local outfile = assert(io.open(tmp, "w"));
for line in infile:lines() do
if not line:find("-###", 1, true) then
outfile:write(line, "\n");
end
end
infile:close();
outfile:close();
assert(os.rename(tmp, input));
endef

BENCH_ITERS ?= 100000

build: $(SRC)/src/*
$(check_lua_incdir)
@mkdir -p $(DESTDIR)
$(CC) $(CFLAGS) -o $(DESTDIR)/tomlua.so $(SRCS)

bear: # used to generate compile_commands.json, which editor tools such as clangd and ccls use
$(check_lua_incdir)
$(BEAR) -- $(CC) -### $(CFLAGS) -o $(DESTDIR)/tomlua.so $(SRCS) > /dev/null 2>&1
$(GREP) -v -- "-###" compile_commands.json > compile_commands.tmp && mv compile_commands.tmp compile_commands.json
@$(BEAR) -- $(CC) -### $(CFLAGS) -o $(DESTDIR)/tomlua.so $(SRCS) > /dev/null 2>&1;
@echo '$(subst $(newline), ,$(FIX_BEAR_RESULT))' | $(LUA) -;
@echo "Created compile_commands.json";

test: $(SRC)/src/* $(TESTDIR)/*
$(check_so_was_built)
Expand All @@ -52,19 +85,31 @@ bench: $(SRC)/src/* $(TESTDIR)/*
$(check_so_was_built)
$(LUA) "$(TESTDIR)/test.lua" "$(DESTDIR)" 2 $(BENCH_ITERS) $(SKIP_TOML_EDIT)

install: $(SRC)/lua/tomlua/meta.lua
install: $(SRC)/lua/tomlua/meta.lua $(SRC)/bin/tomlua
ifdef LIBDIR
$(check_so_was_built)
@mkdir -p "$(LIBDIR)";
cp "$(DESTDIR)/tomlua.so" "$(LIBDIR)/";
@echo "Installed to $(LIBDIR)";
@cp "$(DESTDIR)/tomlua.so" "$(LIBDIR)/";
@echo "Installed library to $(LIBDIR)";
ifdef LUADIR
@mkdir -p "$(LUADIR)/tomlua";
cp "$(SRC)/lua/tomlua/meta.lua" "$(LUADIR)/tomlua/";
@cp "$(SRC)/lua/tomlua/meta.lua" "$(LUADIR)/tomlua/";
@echo "Installed type definitions to $(LUADIR)";
endif
ifdef BINDIR
@mkdir -p "$(BINDIR)";
ifeq ($(filter /%,$(LUA)),)
@echo '$(FIX_SHEBANG)' | $(LUA) - "/usr/bin/env $(LUA)" "$(LIBDIR)" > "$(BINDIR)/tomlua"
else
@echo '$(FIX_SHEBANG)' | $(LUA) - "$(LUA)" "$(LIBDIR)" > "$(BINDIR)/tomlua"
endif
@cat "$(SRC)/bin/tomlua" >> "$(BINDIR)/tomlua";
@chmod +x "$(BINDIR)/tomlua";
@echo "Installed binary to $(BINDIR)";
endif
else
@echo "LIBDIR not set, skipping install"
endif

clean:
rm -rf $(DESTDIR) compile_commands.json compile_commands.tmp
rm -rf $(DESTDIR) compile_commands.json compile_commands.tmp build
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ It is not intended to replace packages like [toml\_edit](https://github.com/nvim
* Compatible with Lua 5.1+.
* Some advanced TOML compliance features are optional (`fancy_dates`, etc.).
* Allows you to read directly into an existing lua table of defaults. This cuts out the step of merging them yourself, making it even more performant and easier to use!
* Command for using the library from the command line. (`tomlua --help` to see the list of options).

## Limitations

Expand Down
Loading