Skip to content

Commit

Permalink
Improved Lua version detection and Lua 5.3 support (#132)
Browse files Browse the repository at this point in the history
* libtu/Makefile: it's C89, not C98

* Lua 5.3 removed deprecated variadic function `arg' parameter

Variadic functions were introduced in 5.0, but the method of accessing
them changed in 5.1. This makes the code incompatible with 5.0 (it is
already incompatible since at least b2b1755), which will be removed
from the build system in a subsequent commit.

Fixes #124. See also #90.

* Remove Lua 5.0 from the build system and improve lua-detect.mk

Lua doesn't include an official pkg-config file, therefore we can't rely on
pkg-config variables like "V" or "major_version" - on my system, it detected
the 5.3 library/cflags and used the 5.1 lua/luac binaries (I have both installed
and there was a fallback to 5.1 if it couldn't query these variables from
pkg-config).
  • Loading branch information
wilhelmy authored and raboof committed Jun 23, 2019
1 parent 4d39759 commit 7f0d1d8
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 27 deletions.
74 changes: 59 additions & 15 deletions build/lua-detect.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# It uses pkg-config to do this, but will fail if you have liblua, but
# not the corresponding interpreter/compiler. Let's say you have liblua5.2
# but want to build with liblua5.1 (for which you have the lib, interpreter
# and compiler), you can override by setting LUA_VERSION=5.0 when invoking
# and compiler), you can override by setting LUA_VERSION=5.1 when invoking
# make.
#
# If successful, sets the following variables:
Expand All @@ -12,25 +12,69 @@
# * LUA_INCLUDES (can be appended to CFLAGS directly)
# * LUA (full path to lua interpreter)
# * LUAC (full path to lua compiler)
#
# If unsuccessful, you can set these manually.

ifndef LUA

# first off, some helper functions to $(call) - this one executes lua/luac to match it's version
lua_ver_match_bin = $(shell $2 -v 2>&1 | grep -qFo $1. && echo 1)
# this one is like which(1), which isn't portable - from the gmake manual
pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
# Extract "5.x" from lua -v
lua_ver_extract = $(shell $1 -v 2>&1 | cut -f2 -d' ' | cut -f-2 -d.)

# Lua does not provide an official .pc file, so finding one at all is tricky,
# and there won't be any guarantees it looks the same as elsewhere.
# We try the package names lua$(ver), lua-$(ver) for all candidate versions in
# descending order and last but not least "lua" here.
PKG_CONFIG_ALL_PACKAGES:= $(shell pkg-config --list-all | cut -f1 -d' ')

# these are in order of preference
LUA_CANDIDATES:= $(or $(LUA_VERSION),5.3 5.2 5.1)
LUA_BIN_CANDIDATES:= $(foreach ver,$(LUA_CANDIDATES),lua$(ver) lua-$(ver))
LUAC_BIN_CANDIDATES:= $(foreach ver,$(LUA_CANDIDATES),luac$(ver) luac-$(ver))

# the binaries might of course also just be called lua and luac
ifndef LUA_VERSION
LUA_BIN_CANDIDATES+= lua
LUAC_BIN_CANDIDATES+= luac
endif

LUA_VERSIONS_CANDIDATES = $(or $(LUA_VERSION),5.3 5.2 5.1 5.0)
# find pkg-config packages in the same order of preference
PKG_CONFIG_LUA_PACKAGES:= $(foreach lua,$(LUA_BIN_CANDIDATES),$(filter $(lua),$(PKG_CONFIG_ALL_PACKAGES)))
PKG_CONFIG_LUA:= $(firstword $(PKG_CONFIG_LUA_PACKAGES))

LUA_PKG := $(firstword $(foreach ver,$(LUA_VERSIONS_CANDIDATES),$(shell \
($(PKG_CONFIG) --exists lua-$(ver) && echo lua-$(ver)) \
|| ($(PKG_CONFIG) --exists lua$(ver:5.0=) && echo lua$(ver:5.0=)))))
ifeq ($(PKG_CONFIG_LUA),)
$(error It seems that pkg-config is not aware of your lua package. Make sure \
the -dev package is installed along with a .pc file within the \
pkg-config search-path. Alternatively, you can set the following \
variables manually: LUA LUAC LUA_VERSION LUA_LIBS LUA_INCLUDES)
endif

PKG_CONFIG_LUA_VERSION:= $(shell pkg-config --modversion $(PKG_CONFIG_LUA) | cut -d. -f-2)

$(info >> pkg-config found Lua $(PKG_CONFIG_LUA_VERSION) (available: $(PKG_CONFIG_LUA_PACKAGES:=)).)

LUA_LIBS= $(shell pkg-config --libs $(PKG_CONFIG_LUA))
LUA_INCLUDES= $(shell pkg-config --cflags $(PKG_CONFIG_LUA))
LUA_VERSION?= $(PKG_CONFIG_LUA_VERSION)

ifeq ($(LUA_PKG),)
$(error Could not find $(or $(LUA_VERSION),any) lua version. (Did you install the -dev package?))
LUA= $(firstword $(foreach bin,$(LUA_BIN_CANDIDATES),$(call pathsearch,$(bin))))
LUAC= $(firstword $(foreach bin,$(LUAC_BIN_CANDIDATES),$(call pathsearch,$(bin))))

$(info >> Lua $(LUA_VERSION) binary is $(LUA) and luac is $(LUAC))

ifneq ($(call lua_ver_match_bin,$(LUA_VERSION),$(LUA)),1)
$(error $(LUA) should be $(LUA_VERSION) but is $(call lua_ver_extract,$(LUA)))
endif

ifeq ($(LUA_VERSION),)
LUA_VERSION := $(or $(shell $(PKG_CONFIG) --variable=V $(LUA_PKG)),$(or $(shell $(PKG_CONFIG) --variable=major_version $(LUA_PKG)),5.0))
ifneq ($(call lua_ver_match_bin,$(LUA_VERSION),$(LUAC)),1)
$(error $(LUAC) should be $(LUA_VERSION) but is $(call lua_ver_extract,$(LUAC)))
endif

# prior to 5.1 the lib didn't include version in name.
LUA_SUFFIX := $(if $(findstring $(LUA_VERSION),5.0),,$(LUA_VERSION))
endif

LUA_LIBS := $(or $(shell $(PKG_CONFIG) --libs $(LUA_PKG)), $(error "pkg-config couldn't find linker flags for lua$(LUA_SUFFIX)!"))
LUA_INCLUDES := $(shell $(PKG_CONFIG) --cflags $(LUA_PKG))
LUA := $(or $(shell which lua$(LUA_SUFFIX)), $(shell which lua), $(error No lua$(LUA_SUFFIX) interpreter found!))
LUAC := $(or $(shell which luac$(LUA_SUFFIX)), $(shell which luac), $(error No lua$(LUA_SUFFIX) compiler found!))
# this is necessary, otherwise the rest of the build process keeps calling
# pkg-config and all these other programs up there
export LUA_VERSION LUA_INCLUDES LUA_LIBS LUA LUAC
4 changes: 1 addition & 3 deletions build/mkman.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

local translations={}

local unpack = unpack or table.unpack

local function gettext(x)
local t=translations[x]
if not t or t=="" then
Expand All @@ -22,7 +20,7 @@ local function gettext(x)
end

local function TR(x, ...)
return string.format(gettext(x), unpack(arg))
return string.format(gettext(x), ...)
end

local function read_translations(pofile)
Expand Down
5 changes: 1 addition & 4 deletions ioncore/ioncore_misc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ local layouts={
default = empty,
}

local unpack = unpack or table.unpack

--DOC
-- Define a new workspace layout with name \var{name}, and
-- attach/creation parameters given in \var{tab}. The layout
Expand Down Expand Up @@ -75,8 +73,7 @@ end
--DOC
-- gettext+string.format
function ioncore.TR(s, ...)
local arg = {...}
return string.format(ioncore.gettext(s), unpack(arg))
return string.format(ioncore.gettext(s), ...)
end


Expand Down
4 changes: 2 additions & 2 deletions libtu/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ include $(TOPDIR)/build/system-inc.mk

#INCLUDES += $(LIBTU_INCLUDES) $(LUA_INCLUDES)

CFLAGS += $(C98_SOURCE) $(POSIX_SOURCE) $(WARN)
CFLAGS += $(C89_SOURCE) $(POSIX_SOURCE) $(WARN)

SOURCES=iterable.c map.c misc.c obj.c objlist.c optparser.c output.c parser.c prefix.c ptrlist.c rb.c setparam.c stringstore.c tokenizer.c util.c errorlog.c

Expand Down Expand Up @@ -47,4 +47,4 @@ install:
done

test:
$(MAKE) -C test test
$(MAKE) -C test test
1 change: 1 addition & 0 deletions mod_query/mod_query.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,7 @@ function mod_query.do_handle_lua(mplex, env, code)
local print_res
local function collect_print(...)
local tmp=""
local arg={...}
local l=#arg
for i=1,l do
tmp=tmp..tostring(arg[i])..(i==l and "\n" or "\t")
Expand Down
4 changes: 1 addition & 3 deletions mod_statusbar/ion-statusd/statusd_mail.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ local defaults={
files = {}
}

local unpack = unpack or table.unpack

local settings=table.join(statusd.get_config(mon), defaults)

local function TR(s, ...)
return string.format(statusd.gettext(s), unpack(arg))
return string.format(statusd.gettext(s), ...)
end

local function check_spool()
Expand Down

0 comments on commit 7f0d1d8

Please sign in to comment.