|
3 | 3 | # It uses pkg-config to do this, but will fail if you have liblua, but
|
4 | 4 | # not the corresponding interpreter/compiler. Let's say you have liblua5.2
|
5 | 5 | # but want to build with liblua5.1 (for which you have the lib, interpreter
|
6 |
| -# and compiler), you can override by setting LUA_VERSION=5.0 when invoking |
| 6 | +# and compiler), you can override by setting LUA_VERSION=5.1 when invoking |
7 | 7 | # make.
|
8 | 8 | #
|
9 | 9 | # If successful, sets the following variables:
|
|
12 | 12 | # * LUA_INCLUDES (can be appended to CFLAGS directly)
|
13 | 13 | # * LUA (full path to lua interpreter)
|
14 | 14 | # * LUAC (full path to lua compiler)
|
| 15 | +# |
| 16 | +# If unsuccessful, you can set these manually. |
| 17 | + |
| 18 | +ifndef LUA |
| 19 | + |
| 20 | +# first off, some helper functions to $(call) - this one executes lua/luac to match it's version |
| 21 | +lua_ver_match_bin = $(shell $2 -v 2>&1 | grep -qFo $1. && echo 1) |
| 22 | +# this one is like which(1), which isn't portable - from the gmake manual |
| 23 | +pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH))))) |
| 24 | +# Extract "5.x" from lua -v |
| 25 | +lua_ver_extract = $(shell $1 -v 2>&1 | cut -f2 -d' ' | cut -f-2 -d.) |
| 26 | + |
| 27 | +# Lua does not provide an official .pc file, so finding one at all is tricky, |
| 28 | +# and there won't be any guarantees it looks the same as elsewhere. |
| 29 | +# We try the package names lua$(ver), lua-$(ver) for all candidate versions in |
| 30 | +# descending order and last but not least "lua" here. |
| 31 | +PKG_CONFIG_ALL_PACKAGES:= $(shell pkg-config --list-all | cut -f1 -d' ') |
| 32 | + |
| 33 | +# these are in order of preference |
| 34 | +LUA_CANDIDATES:= $(or $(LUA_VERSION),5.3 5.2 5.1) |
| 35 | +LUA_BIN_CANDIDATES:= $(foreach ver,$(LUA_CANDIDATES),lua$(ver) lua-$(ver)) |
| 36 | +LUAC_BIN_CANDIDATES:= $(foreach ver,$(LUA_CANDIDATES),luac$(ver) luac-$(ver)) |
| 37 | + |
| 38 | +# the binaries might of course also just be called lua and luac |
| 39 | +ifndef LUA_VERSION |
| 40 | +LUA_BIN_CANDIDATES+= lua |
| 41 | +LUAC_BIN_CANDIDATES+= luac |
| 42 | +endif |
15 | 43 |
|
16 |
| -LUA_VERSIONS_CANDIDATES = $(or $(LUA_VERSION),5.3 5.2 5.1 5.0) |
| 44 | +# find pkg-config packages in the same order of preference |
| 45 | +PKG_CONFIG_LUA_PACKAGES:= $(foreach lua,$(LUA_BIN_CANDIDATES),$(filter $(lua),$(PKG_CONFIG_ALL_PACKAGES))) |
| 46 | +PKG_CONFIG_LUA:= $(firstword $(PKG_CONFIG_LUA_PACKAGES)) |
17 | 47 |
|
18 |
| -LUA_PKG := $(firstword $(foreach ver,$(LUA_VERSIONS_CANDIDATES),$(shell \ |
19 |
| - ($(PKG_CONFIG) --exists lua-$(ver) && echo lua-$(ver)) \ |
20 |
| - || ($(PKG_CONFIG) --exists lua$(ver:5.0=) && echo lua$(ver:5.0=))))) |
| 48 | +ifeq ($(PKG_CONFIG_LUA),) |
| 49 | +$(error It seems that pkg-config is not aware of your lua package. Make sure \ |
| 50 | + the -dev package is installed along with a .pc file within the \ |
| 51 | + pkg-config search-path. Alternatively, you can set the following \ |
| 52 | + variables manually: LUA LUAC LUA_VERSION LUA_LIBS LUA_INCLUDES) |
| 53 | +endif |
| 54 | + |
| 55 | +PKG_CONFIG_LUA_VERSION:= $(shell pkg-config --modversion $(PKG_CONFIG_LUA) | cut -d. -f-2) |
| 56 | + |
| 57 | +$(info >> pkg-config found Lua $(PKG_CONFIG_LUA_VERSION) (available: $(PKG_CONFIG_LUA_PACKAGES:=)).) |
| 58 | + |
| 59 | +LUA_LIBS= $(shell pkg-config --libs $(PKG_CONFIG_LUA)) |
| 60 | +LUA_INCLUDES= $(shell pkg-config --cflags $(PKG_CONFIG_LUA)) |
| 61 | +LUA_VERSION?= $(PKG_CONFIG_LUA_VERSION) |
21 | 62 |
|
22 |
| -ifeq ($(LUA_PKG),) |
23 |
| - $(error Could not find $(or $(LUA_VERSION),any) lua version. (Did you install the -dev package?)) |
| 63 | +LUA= $(firstword $(foreach bin,$(LUA_BIN_CANDIDATES),$(call pathsearch,$(bin)))) |
| 64 | +LUAC= $(firstword $(foreach bin,$(LUAC_BIN_CANDIDATES),$(call pathsearch,$(bin)))) |
| 65 | + |
| 66 | +$(info >> Lua $(LUA_VERSION) binary is $(LUA) and luac is $(LUAC)) |
| 67 | + |
| 68 | +ifneq ($(call lua_ver_match_bin,$(LUA_VERSION),$(LUA)),1) |
| 69 | +$(error $(LUA) should be $(LUA_VERSION) but is $(call lua_ver_extract,$(LUA))) |
24 | 70 | endif
|
25 | 71 |
|
26 |
| -ifeq ($(LUA_VERSION),) |
27 |
| -LUA_VERSION := $(or $(shell $(PKG_CONFIG) --variable=V $(LUA_PKG)),$(or $(shell $(PKG_CONFIG) --variable=major_version $(LUA_PKG)),5.0)) |
| 72 | +ifneq ($(call lua_ver_match_bin,$(LUA_VERSION),$(LUAC)),1) |
| 73 | +$(error $(LUAC) should be $(LUA_VERSION) but is $(call lua_ver_extract,$(LUAC))) |
28 | 74 | endif
|
29 | 75 |
|
30 |
| -# prior to 5.1 the lib didn't include version in name. |
31 |
| -LUA_SUFFIX := $(if $(findstring $(LUA_VERSION),5.0),,$(LUA_VERSION)) |
| 76 | +endif |
32 | 77 |
|
33 |
| -LUA_LIBS := $(or $(shell $(PKG_CONFIG) --libs $(LUA_PKG)), $(error "pkg-config couldn't find linker flags for lua$(LUA_SUFFIX)!")) |
34 |
| -LUA_INCLUDES := $(shell $(PKG_CONFIG) --cflags $(LUA_PKG)) |
35 |
| -LUA := $(or $(shell which lua$(LUA_SUFFIX)), $(shell which lua), $(error No lua$(LUA_SUFFIX) interpreter found!)) |
36 |
| -LUAC := $(or $(shell which luac$(LUA_SUFFIX)), $(shell which luac), $(error No lua$(LUA_SUFFIX) compiler found!)) |
| 78 | +# this is necessary, otherwise the rest of the build process keeps calling |
| 79 | +# pkg-config and all these other programs up there |
| 80 | +export LUA_VERSION LUA_INCLUDES LUA_LIBS LUA LUAC |
0 commit comments