From 70ece71ee4e8f8bff5cbc572710c0fa6fc3b355f Mon Sep 17 00:00:00 2001
From: Ferdinand Thiessen <rpm@fthiessen.de>
Date: Thu, 3 Dec 2015 21:18:39 +0100
Subject: [PATCH] Improve LuaJIT detection

On openSUSE luajit is not detected correctly.
This is because openSUSE is using a lua version suffix, like other Linux distributions do it also.
So the include directory is:
include/luajit-5_1-2.0
---
 cmake/Modules/FindLua.cmake    | 25 -----------------
 cmake/Modules/FindLuaJIT.cmake | 50 ++++++++++++++++++++++++++++++++++
 src/CMakeLists.txt             | 19 +++++++++++--
 3 files changed, 67 insertions(+), 27 deletions(-)
 delete mode 100644 cmake/Modules/FindLua.cmake
 create mode 100644 cmake/Modules/FindLuaJIT.cmake

diff --git a/cmake/Modules/FindLua.cmake b/cmake/Modules/FindLua.cmake
deleted file mode 100644
index 479dfcf41..000000000
--- a/cmake/Modules/FindLua.cmake
+++ /dev/null
@@ -1,25 +0,0 @@
-
-option(ENABLE_LUAJIT "Enable LuaJIT support" TRUE)
-mark_as_advanced(LUA_LIBRARY LUA_INCLUDE_DIR)
-set(USE_LUAJIT FALSE)
-
-if(ENABLE_LUAJIT)
-	find_library(LUA_LIBRARY luajit
-			NAMES luajit-5.1)
-	find_path(LUA_INCLUDE_DIR luajit.h
-		NAMES luajit.h
-		PATH_SUFFIXES luajit-2.0)
-	if(LUA_LIBRARY AND LUA_INCLUDE_DIR)
-		set(USE_LUAJIT TRUE)
-	endif()
-else()
-	message (STATUS "LuaJIT detection disabled! (ENABLE_LUAJIT=0)")
-endif()
-
-if(NOT USE_LUAJIT)
-	message(STATUS "LuaJIT not found, using bundled Lua.")
-	set(LUA_LIBRARY "lua")
-	set(LUA_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/lua/src")
-	add_subdirectory(lua)
-endif()
-
diff --git a/cmake/Modules/FindLuaJIT.cmake b/cmake/Modules/FindLuaJIT.cmake
new file mode 100644
index 000000000..e4335d834
--- /dev/null
+++ b/cmake/Modules/FindLuaJIT.cmake
@@ -0,0 +1,50 @@
+# Locate LuaJIT library
+# This module defines
+#  LUAJIT_FOUND, if false, do not try to link to Lua
+#  LUA_INCLUDE_DIR, where to find lua.h
+#  LUA_VERSION_STRING, the version of Lua found (since CMake 2.8.8)
+#
+# This module is similar to FindLua51.cmake except that it finds LuaJit instead.
+
+FIND_PATH(LUA_INCLUDE_DIR luajit.h
+	HINTS
+	$ENV{LUA_DIR}
+	PATH_SUFFIXES include/luajit-2.0 include/luajit-5_1-2.0 include
+	PATHS
+	~/Library/Frameworks
+	/Library/Frameworks
+	/sw # Fink
+	/opt/local # DarwinPorts
+	/opt/csw # Blastwave
+	/opt
+)
+
+FIND_LIBRARY(LUA_LIBRARY
+	NAMES luajit-5.1
+	HINTS
+	$ENV{LUA_DIR}
+	PATH_SUFFIXES lib64 lib
+	PATHS
+	~/Library/Frameworks
+	/Library/Frameworks
+	/sw
+	/opt/local
+	/opt/csw
+	/opt
+)
+
+IF(LUA_INCLUDE_DIR AND EXISTS "${LUA_INCLUDE_DIR}/luajit.h")
+	FILE(STRINGS "${LUA_INCLUDE_DIR}/luajit.h" lua_version_str REGEX "^#define[ \t]+LUA_RELEASE[ \t]+\"LuaJIT .+\"")
+
+	STRING(REGEX REPLACE "^#define[ \t]+LUA_RELEASE[ \t]+\"LuaJIT ([^\"]+)\".*" "\\1" LUA_VERSION_STRING "${lua_version_str}")
+	UNSET(lua_version_str)
+ENDIF()
+
+INCLUDE(FindPackageHandleStandardArgs)
+# handle the QUIETLY and REQUIRED arguments and set LUAJIT_FOUND to TRUE if
+# all listed variables are TRUE
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(LuaJit
+	REQUIRED_VARS LUA_LIBRARY LUA_INCLUDE_DIR
+	VERSION_VAR LUA_VERSION_STRING)
+
+MARK_AS_ADVANCED(LUA_INCLUDE_DIR LUA_LIBRARY LUA_MATH_LIBRARY)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index dfbd2f5d8..6256a8504 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -155,8 +155,23 @@ if(ENABLE_FREETYPE)
 	endif()
 endif(ENABLE_FREETYPE)
 
-
-find_package(Lua REQUIRED)
+# LuaJIT
+option(ENABLE_LUAJIT "Enable LuaJIT support" TRUE)
+set(USE_LUAJIT FALSE)
+if(ENABLE_LUAJIT)
+	find_package(LuaJIT)
+	if(LUAJIT_FOUND)
+		set(USE_LUAJIT TRUE)
+	endif(LUAJIT_FOUND)
+else()
+	message (STATUS "LuaJIT detection disabled! (ENABLE_LUAJIT=0)")
+endif()
+if(NOT USE_LUAJIT)
+	message(STATUS "LuaJIT not found, using bundled Lua.")
+	set(LUA_LIBRARY "lua")
+	set(LUA_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/lua/src")
+	add_subdirectory(lua)
+endif()
 
 find_package(GMP REQUIRED)
 
-- 
GitLab