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
13 changes: 9 additions & 4 deletions .github/rockspec.template
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,23 @@ build = {
type = "make",
copy_directories = $copy_directories,
build_variables = {
LUA_INCDIR="$(LUA_INCDIR)",
LIBFLAG="$(LIBFLAG)",
LUA_BINDIR="$(LUA_BINDIR)",
LUA_INCDIR="$(LUA_INCDIR)",
LUA="$(LUA)",
LUA_VERSION="$(LUA_VERSION)",
LUA_LIBDIR="$(LUA_LIBDIR)",
LUALIB="$(LUALIB)",
},
install_variables = {
WRAP = "false",
PREFIX="$(PREFIX)",
LIBDIR="$(LIBDIR)",
BINDIR="$(BINDIR)",
LUADIR="$(LUADIR)",
},
}

test = {
type = "command",
command = "make test",
type = "command",
command = "make test",
}
152 changes: 100 additions & 52 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,36 +1,90 @@
src ?= .
SRC ?= $(src)
SRC := $(abspath $(SRC))
DESTDIR ?= ./build/lib
CC ?= gcc
LUA ?= lua
BEAR ?= bear
CFLAGS ?= -fPIC -x c -O3 -flto -Wl,-s -Winline
LIBFLAG ?= -shared

ifdef LUA_INC
LUA_INCDIR ?= $(LUA_INC)
src ?= .
SRC ?= $(src)
DESTDIR ?= ./build
ifdef LUA_BINDIR
LUA := $(if $(wildcard $(LUA_BINDIR)/luajit),$(LUA_BINDIR)/luajit,$(if $(wildcard $(LUA_BINDIR)/lua),$(LUA_BINDIR)/lua,lua))
else
ifdef LUA_DIR
LUA := $(if $(wildcard $(LUA_DIR)/bin/luajit),$(LUA_DIR)/bin/luajit,$(if $(wildcard $(LUA_DIR)/bin/lua),$(LUA_DIR)/bin/lua,lua))
else
LUA ?= lua
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
endif
BEAR ?= bear

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

define FIX_SHEBANG
io.write(string.format("#!%s%cpackage.cpath = %q .. [[/?.so;]] .. package.cpath%c-- ", arg[1], 10, arg[2], 10))
endef
CC ?= gcc
LIBFLAG ?= -shared
CFLAGS ?= -fPIC -x c -O3 -flto -Winline
LDFLAGS ?= -Wl,-s

LUA_VERSION ?= 5.1
LUA_NAME ?= $(notdir $(LUA))
ifeq ($(LUA_NAME),luajit)
LUALIB ?= $(LUA_NAME)-$(LUA_VERSION)
else
LUALIB ?= $(LUA_NAME)
endif
ifeq ($(strip $(LUALIB)),)
ifeq ($(LUA_NAME),luajit)
override LUALIB := $(LUA_NAME)-$(LUA_VERSION)
else
override LUALIB := $(LUA_NAME)
endif
endif

ifdef LUA_INC
LUA_INCDIR ?= $(LUA_INC)
endif
ifdef LUA_DIR
LUA_LIBDIR ?= $(LUA_DIR)/lib
LUA_INCDIR ?= $(LUA_DIR)/include
endif
ifeq ($(strip $(LUA_LIBDIR)),)
ifdef LUA_DIR
override LUA_LIBDIR := $(LUA_DIR)/lib
endif
endif

BINFLAG := -lm
ifneq ($(strip $(LUA_LIBDIR)),)
BINFLAG += -L$(LUA_LIBDIR)
endif
ifdef LUALIB
BINFLAG += -l$(LUALIB)
endif

ifneq ($(strip $(LUA_LIBDIR)$(LUALIB)),)
MAKE_BINARY := 1
endif

CFLAGS += -I$(LUA_INCDIR)

SRC := $(abspath $(SRC))
TESTDIR := $(SRC)/tests
SRCS := $(SRC)/src/tomlua.c \
$(SRC)/src/decode.c \
$(SRC)/src/encode.c \
$(SRC)/src/dates.c

CLI_SRCS := $(SRC)/src/tomlua_cli.c \
$(SRC)/src/argus.c

BUILD_DIR ?= $(DESTDIR)
BUILD_DIR := $(abspath $(DESTDIR))
LIB_BUILD_DIR ?= $(BUILD_DIR)/lib
BIN_BUILD_DIR ?= $(BUILD_DIR)/bin

check_lua_incdir = \
@if [ -z "$(LUA_INCDIR)" ]; then \
Expand All @@ -39,8 +93,8 @@ check_lua_incdir = \
fi

check_so_was_built = \
@if [ ! -f "$(DESTDIR)/tomlua.so" ]; then \
echo "Error: $(DESTDIR)/tomlua.so not built. Run make build first."; \
@if [ ! -f "$(LIB_BUILD_DIR)/tomlua.so" ]; then \
echo "Error: $(LIB_BUILD_DIR)/tomlua.so not built. Run make build first."; \
false; \
fi

Expand All @@ -66,56 +120,50 @@ BENCH_ITERS ?= 100000

build: $(SRC)/src/*
$(check_lua_incdir)
@mkdir -p $(DESTDIR)
$(CC) $(CFLAGS) -o $(DESTDIR)/tomlua.so $(SRCS)
@mkdir -p $(LIB_BUILD_DIR)
$(CC) $(LIBFLAG) $(LDFLAGS) $(CFLAGS) -o $(LIB_BUILD_DIR)/tomlua.so $(SRCS)
ifdef MAKE_BINARY
@mkdir -p $(BIN_BUILD_DIR)
-$(CC) $(CFLAGS) $(LDFLAGS) $(BINFLAG) -o $(BIN_BUILD_DIR)/tomlua $(SRCS) $(CLI_SRCS)
endif

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;
@$(BEAR) -- $(CC) -### $(BINFLAG) $(CFLAGS) $(LDFLAGS) -o $(BIN_BUILD_DIR)/tomlua $(SRCS) $(CLI_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)
$(LUA) "$(TESTDIR)/test.lua" "$(DESTDIR)"
$(LUA) "$(TESTDIR)/test.lua" "$(LIB_BUILD_DIR)"

scratch: $(SRC)/src/* $(TESTDIR)/*
$(check_so_was_built)
$(LUA) "$(TESTDIR)/test.lua" "$(DESTDIR)" 1
scratch: $(SRC)/src/* $(TESTDIR)/* build
$(LUA) "$(TESTDIR)/test.lua" "$(LIB_BUILD_DIR)" 1

bench: $(SRC)/src/* $(TESTDIR)/*
$(check_so_was_built)
$(LUA) "$(TESTDIR)/test.lua" "$(DESTDIR)" 2 $(BENCH_ITERS) $(SKIP_TOML_EDIT)
bench: $(SRC)/src/* $(TESTDIR)/* build
$(LUA) "$(TESTDIR)/test.lua" "$(LIB_BUILD_DIR)" 2 $(BENCH_ITERS) $(SKIP_TOML_EDIT)

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

clean:
rm -rf $(DESTDIR) compile_commands.json compile_commands.tmp build
rm -rf $(BUILD_DIR) compile_commands.json compile_commands.tmp
41 changes: 31 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,48 @@ luarocks install tomlua
* Flake or flakeless support, both methods return according to the [flake outputs schema](https://wiki.nixos.org/wiki/Flakes).
* Overlay and packages available for Lua versions 5.1+, as well as a neovim plugin.
* Dev shell included for building via `make`.
* Not yet on nixpkgs
* `nix build nixpkgs#${lua,luajit,lua5_${1..5}}.pkgs.tomlua`

### Using Make

To build and add to your environment:
Requires a C compiler (GCC, Clang, MinGW). If not gcc, set `CC` as well.
Run from the root of the repository.

If you don't know where your Lua headers are, find them with:

```bash
# Ensure LUA_INCDIR points to Lua headers directory
make LUA_INCDIR=/path/to/lua/includes
gcc -xc -E -v - <<< '#include <lua.h>' 2>&1 | grep lua.h | head -n 1 | awk '{print $3}' | tr -d '"' | xargs dirname
```

**Build the library:**

```bash
make build LUA_INCDIR=/path/to/lua/includes
# or luajit
make build LUA_INCDIR=/path/to/lua/includes LUA=luajit
```

# Add the resulting module to Lua's package.cpath
export LUA_CPATH="$LUA_CPATH;/path/to/tomlua/lib/?.so"
You may need to pass `LUA_DIR` (e.g. `/usr`) instead, must contain `include` and `lib` directories,
in which case it will also attempt to detect luajit automatically.

You can also set `LUA_LIBDIR` and `LUALIB` individually instead, to be passed to `-L` and `-l` flags for the cli binary

Output goes to `DESTDIR` (defaults to `./build`). The library ends up at `build/lib/tomlua.so` and the CLI binary at `build/bin/tomlua`.

**Add to Lua's `package.cpath` after building:**

```bash
export LUA_CPATH="$LUA_CPATH;/path/to/tomlua/build/lib/?.so"
```

* Requires a C compiler (GCC, Clang, MinGW). If not gcc, set CC variable as well
* You should be in the root of the repository.
**Install Artifacts:**

Install to `PREFIX` (`LIBDIR`, `LUADIR`, `BINDIR`) |

If you do not know where your lua headers are, you may use some or all of this command to find out
`LUADIR` just contains type definitions for `lua_ls`

```bash
gcc -xc -E -v - <<< '#include <lua.h>' 2>&1 | grep lua.h | head -n 1 | awk '{print $3}' | tr -d '"' | xargs dirname
make install prefix=/path/to/install/location LUADIR=/dev/null
```

### Useage
Expand Down
Loading