From 9455512d22d57a884024c881256a2d35cceedbd9 Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Tue, 22 May 2018 22:49:55 +0200 Subject: FindLua: Add tests for this module --- Tests/RunCMake/CMakeLists.txt | 1 + Tests/RunCMake/FindLua/CMakeLists.txt | 3 + Tests/RunCMake/FindLua/FindLuaTest.cmake | 87 ++++++++++++++++++++++ Tests/RunCMake/FindLua/RunCMakeTest.cmake | 3 + Tests/RunCMake/FindLua/prefix1/include/lua.h | 8 ++ .../RunCMake/FindLua/prefix2/include/lua5.1/lua.h | 8 ++ .../RunCMake/FindLua/prefix2/include/lua5.2/lua.h | 8 ++ .../RunCMake/FindLua/prefix2/include/lua5.3/lua.h | 8 ++ .../RunCMake/FindLua/prefix2/include/lua5.9/lua.h | 8 ++ 9 files changed, 134 insertions(+) create mode 100644 Tests/RunCMake/FindLua/CMakeLists.txt create mode 100644 Tests/RunCMake/FindLua/FindLuaTest.cmake create mode 100644 Tests/RunCMake/FindLua/RunCMakeTest.cmake create mode 100644 Tests/RunCMake/FindLua/prefix1/include/lua.h create mode 100644 Tests/RunCMake/FindLua/prefix2/include/lua5.1/lua.h create mode 100644 Tests/RunCMake/FindLua/prefix2/include/lua5.2/lua.h create mode 100644 Tests/RunCMake/FindLua/prefix2/include/lua5.3/lua.h create mode 100644 Tests/RunCMake/FindLua/prefix2/include/lua5.9/lua.h diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 690c5b4..bb46144 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -152,6 +152,7 @@ add_RunCMake_test(ExternalData) add_RunCMake_test(FeatureSummary) add_RunCMake_test(FPHSA) add_RunCMake_test(FindBoost) +add_RunCMake_test(FindLua) add_RunCMake_test(FindOpenGL) if(NOT CMAKE_C_COMPILER_ID MATCHES "Watcom") add_RunCMake_test(GenerateExportHeader) diff --git a/Tests/RunCMake/FindLua/CMakeLists.txt b/Tests/RunCMake/FindLua/CMakeLists.txt new file mode 100644 index 0000000..a2c4d98 --- /dev/null +++ b/Tests/RunCMake/FindLua/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 3.4) +project(${RunCMake_TEST} C) +include(${RunCMake_TEST}.cmake) diff --git a/Tests/RunCMake/FindLua/FindLuaTest.cmake b/Tests/RunCMake/FindLua/FindLuaTest.cmake new file mode 100644 index 0000000..610d544 --- /dev/null +++ b/Tests/RunCMake/FindLua/FindLuaTest.cmake @@ -0,0 +1,87 @@ +unset(VERSION) + +# Ignore all default paths for this test to avoid finding system Lua +set(CMAKE_INCLUDE_PATH ) +set(CMAKE_PREFIX_PATH ) +set(CMAKE_FRAMEWORK_PATH ) + +set(ENV{CMAKE_INCLUDE_PATH} ) +set(ENV{CMAKE_PREFIX_PATH} ) +set(ENV{CMAKE_FRAMEWORK_PATH} ) + +set(ENV{PATH} ) +set(ENV{INCLUDE} ) + +set(CMAKE_SYSTEM_INCLUDE_PATH ) +set(CMAKE_SYSTEM_PREFIX_PATH ) +set(CMAKE_SYSTEM_FRAMEWORK_PATH ) + +function(require_found path version) + find_package(Lua ${VERSION} QUIET) + if(NOT "${LUA_INCLUDE_DIR}" STREQUAL "${path}") + message(FATAL_ERROR "LUA_INCLUDE_PATH != path: '${LUA_INCLUDE_DIR}' != '${path}'") + endif() + if(NOT LUA_VERSION_STRING MATCHES "^${version}\.[0-9]$") + message(FATAL_ERROR "Wrong versionfound in '${LUA_INCLUDE_DIR}': ${LUA_VERSION_STRING} != ${version}") + endif() +endfunction() + +# Use functions for scoping and better error messages +function(require_find path version) + unset(LUA_INCLUDE_DIR CACHE) + require_found(${lua_path} ${version}) +endfunction() + +function(test_prefix_path path lua_path version) + set(CMAKE_PREFIX_PATH ${path}) + require_find(lua_path ${version}) +endfunction() + +function(test_include_path path lua_path version) + set(CMAKE_INCLUDE_PATH ${path}) + require_find(lua_path ${version}) +endfunction() + +function(test_env_path path lua_path version) + set(ENV{LUA_DIR} ${path}) + require_find(lua_path ${version}) + unset(ENV{LUA_DIR}) +endfunction() + +function(test_path prefix_path lua_path version) + # Shortcut: Make paths relative to current list dir + set(prefix_path ${CMAKE_CURRENT_LIST_DIR}/${prefix_path}) + set(lua_path ${CMAKE_CURRENT_LIST_DIR}/${lua_path}) + + test_prefix_path(${prefix_path} ${lua_path} ${version}) + test_include_path(${prefix_path}/include ${lua_path} ${version}) + test_env_path(${prefix_path} ${lua_path} ${version}) +endfunction() + +# Simple test +test_path(prefix1 prefix1/include 5.3) +# Find highest version +test_path(prefix2 prefix2/include/lua5.3 5.3) +foreach(ver 5.3 5.2 5.1) + # At least X or X.0 -> Highest + set(VERSION "${ver}") + test_path(prefix2 prefix2/include/lua5.3 5.3) + set(VERSION "${ver}.0") + test_path(prefix2 prefix2/include/lua5.3 5.3) + # Exactly X/X.0 + set(VERSION "${ver}" EXACT) + test_path(prefix2 prefix2/include/lua${ver} ${ver}) + set(VERSION "${ver}.0" EXACT) + test_path(prefix2 prefix2/include/lua${ver} ${ver}) +endforeach() + +# Find unknown version +set(VERSION "5.9") +test_path(prefix2 prefix2/include/lua5.9 5.9) +set(VERSION "5.9" EXACT) +test_path(prefix2 prefix2/include/lua5.9 5.9) + +# Set LUA_INCLUDE_DIR (non-cache) to unsuitable version +set(LUA_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/prefix2/include/lua5.2) +set(VERSION "5.1" EXACT) +test_path(prefix2 prefix2/include/lua5.1 5.1) diff --git a/Tests/RunCMake/FindLua/RunCMakeTest.cmake b/Tests/RunCMake/FindLua/RunCMakeTest.cmake new file mode 100644 index 0000000..3f18033 --- /dev/null +++ b/Tests/RunCMake/FindLua/RunCMakeTest.cmake @@ -0,0 +1,3 @@ +include(RunCMake) + +run_cmake(FindLuaTest) diff --git a/Tests/RunCMake/FindLua/prefix1/include/lua.h b/Tests/RunCMake/FindLua/prefix1/include/lua.h new file mode 100644 index 0000000..d33434a --- /dev/null +++ b/Tests/RunCMake/FindLua/prefix1/include/lua.h @@ -0,0 +1,8 @@ + +#define LUA_VERSION_MAJOR "5" +#define LUA_VERSION_MINOR "3" +#define LUA_VERSION_NUM 503 +#define LUA_VERSION_RELEASE "4" + +#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR +#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE diff --git a/Tests/RunCMake/FindLua/prefix2/include/lua5.1/lua.h b/Tests/RunCMake/FindLua/prefix2/include/lua5.1/lua.h new file mode 100644 index 0000000..661e62c --- /dev/null +++ b/Tests/RunCMake/FindLua/prefix2/include/lua5.1/lua.h @@ -0,0 +1,8 @@ + +#define LUA_VERSION_MAJOR "5" +#define LUA_VERSION_MINOR "1" +#define LUA_VERSION_NUM 501 +#define LUA_VERSION_RELEASE "4" + +#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR +#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE diff --git a/Tests/RunCMake/FindLua/prefix2/include/lua5.2/lua.h b/Tests/RunCMake/FindLua/prefix2/include/lua5.2/lua.h new file mode 100644 index 0000000..d53f326 --- /dev/null +++ b/Tests/RunCMake/FindLua/prefix2/include/lua5.2/lua.h @@ -0,0 +1,8 @@ + +#define LUA_VERSION_MAJOR "5" +#define LUA_VERSION_MINOR "2" +#define LUA_VERSION_NUM 502 +#define LUA_VERSION_RELEASE "4" + +#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR +#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE diff --git a/Tests/RunCMake/FindLua/prefix2/include/lua5.3/lua.h b/Tests/RunCMake/FindLua/prefix2/include/lua5.3/lua.h new file mode 100644 index 0000000..d33434a --- /dev/null +++ b/Tests/RunCMake/FindLua/prefix2/include/lua5.3/lua.h @@ -0,0 +1,8 @@ + +#define LUA_VERSION_MAJOR "5" +#define LUA_VERSION_MINOR "3" +#define LUA_VERSION_NUM 503 +#define LUA_VERSION_RELEASE "4" + +#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR +#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE diff --git a/Tests/RunCMake/FindLua/prefix2/include/lua5.9/lua.h b/Tests/RunCMake/FindLua/prefix2/include/lua5.9/lua.h new file mode 100644 index 0000000..730f7cc --- /dev/null +++ b/Tests/RunCMake/FindLua/prefix2/include/lua5.9/lua.h @@ -0,0 +1,8 @@ + +#define LUA_VERSION_MAJOR "5" +#define LUA_VERSION_MINOR "9" +#define LUA_VERSION_NUM 509 +#define LUA_VERSION_RELEASE "4" + +#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR +#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE -- cgit v0.12 From f5d19260f9a05cc4a6a4f94e6d344702586b2c0f Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Sun, 20 May 2018 15:56:34 +0200 Subject: FindLua: Search for lua.h using more conventional paths Do not constrain the search to `include/*`. To provide compatibility the foreach-loop is still used. However `include/xxx` and `xxx` is now both searched. This honors now e.g. CMAKE_INCLUDE_PATH. Fixes: #17999 --- Modules/FindLua.cmake | 134 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 87 insertions(+), 47 deletions(-) diff --git a/Modules/FindLua.cmake b/Modules/FindLua.cmake index 7eba206..8f35fc7 100644 --- a/Modules/FindLua.cmake +++ b/Modules/FindLua.cmake @@ -39,9 +39,16 @@ unset(_lua_include_subdirs) unset(_lua_library_names) unset(_lua_append_versions) +set(_lua_additional_paths + ~/Library/Frameworks + /Library/Frameworks + /sw # Fink + /opt/local # DarwinPorts + /opt/csw # Blastwave + /opt) # this is a function only to have all the variables inside go away automatically -function(_lua_set_version_vars) +function(_lua_get_versions) set(LUA_VERSIONS5 5.3 5.2 5.1 5.0) if (Lua_FIND_VERSION_EXACT) @@ -59,6 +66,10 @@ function(_lua_set_version_vars) list(APPEND _lua_append_versions ${subver}) endif () endforeach () + # New version -> Search for it (heuristic only! Defines in include might have changed) + if (NOT _lua_append_versions) + set(_lua_append_versions ${Lua_FIND_VERSION_MAJOR}.${Lua_FIND_VERSION_MINOR}) + endif() endif () endif () else () @@ -66,22 +77,42 @@ function(_lua_set_version_vars) set(_lua_append_versions ${LUA_VERSIONS5}) endif () - list(APPEND _lua_include_subdirs "include/lua" "include") + if (LUA_Debug) + message(STATUS "Considering following Lua versions: ${_lua_append_versions}") + endif() + + set(_lua_append_versions "${_lua_append_versions}" PARENT_SCOPE) +endfunction() + +function(_lua_set_version_vars) + set(_lua_include_subdirs_raw "lua") foreach (ver IN LISTS _lua_append_versions) string(REGEX MATCH "^([0-9]+)\\.([0-9]+)$" _ver "${ver}") - list(APPEND _lua_include_subdirs - include/lua${CMAKE_MATCH_1}${CMAKE_MATCH_2} - include/lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2} - include/lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2} + list(APPEND _lua_include_subdirs_raw + lua${CMAKE_MATCH_1}${CMAKE_MATCH_2} + lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2} + lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2} ) endforeach () + # Prepend "include/" to each path directly after the path + set(_lua_include_subdirs "include") + foreach (dir IN LISTS _lua_include_subdirs_raw) + list(APPEND _lua_include_subdirs "${dir}" "include/${dir}") + endforeach () + set(_lua_include_subdirs "${_lua_include_subdirs}" PARENT_SCOPE) - set(_lua_append_versions "${_lua_append_versions}" PARENT_SCOPE) endfunction(_lua_set_version_vars) -function(_lua_check_header_version _hdr_file) +function(_lua_get_header_version) + unset(LUA_VERSION_STRING PARENT_SCOPE) + set(_hdr_file "${LUA_INCLUDE_DIR}/lua.h") + + if (NOT EXISTS "${_hdr_file}") + return() + endif () + # At least 5.[012] have different ways to express the version # so all of them need to be tested. Lua 5.2 defines LUA_VERSION # and LUA_RELEASE as joined by the C preprocessor, so avoid those. @@ -111,39 +142,54 @@ function(_lua_check_header_version _hdr_file) return() endif () endforeach () -endfunction(_lua_check_header_version) - -_lua_set_version_vars() - -if (LUA_INCLUDE_DIR AND EXISTS "${LUA_INCLUDE_DIR}/lua.h") - _lua_check_header_version("${LUA_INCLUDE_DIR}/lua.h") -endif () - -if (NOT LUA_VERSION_STRING) - foreach (subdir IN LISTS _lua_include_subdirs) - unset(LUA_INCLUDE_PREFIX CACHE) - unset(LUA_INCLUDE_PREFIX) - find_path(LUA_INCLUDE_PREFIX ${subdir}/lua.h - HINTS - ENV LUA_DIR - PATHS - ~/Library/Frameworks - /Library/Frameworks - /sw # Fink - /opt/local # DarwinPorts - /opt/csw # Blastwave - /opt - ) - if (LUA_INCLUDE_PREFIX) - _lua_check_header_version("${LUA_INCLUDE_PREFIX}/${subdir}/lua.h") - if (LUA_VERSION_STRING) - set(LUA_INCLUDE_DIR "${LUA_INCLUDE_PREFIX}/${subdir}") +endfunction(_lua_get_header_version) + +function(_lua_find_header) + _lua_set_version_vars() + + # Initialize as local variable + set(CMAKE_IGNORE_PATH ${CMAKE_IGNORE_PATH}) + while (TRUE) + # Find the next header to test. Check each possible subdir in order + # This prefers e.g. higher versions as they are earlier in the list + # It is also consistent with previous versions of FindLua + foreach (subdir IN LISTS _lua_include_subdirs) + find_path(LUA_INCLUDE_DIR lua.h + HINTS + ENV LUA_DIR + PATH_SUFFIXES ${subdir} + PATHS ${_lua_additional_paths} + ) + if (LUA_INCLUDE_DIR) break() - endif () - endif () - endforeach () -endif () -unset(_lua_include_subdirs) + endif() + endforeach() + # Did not found header -> Fail + if (NOT LUA_INCLUDE_DIR) + return() + endif() + _lua_get_header_version() + # Found accepted version -> Ok + if (LUA_VERSION_STRING) + if (LUA_Debug) + message(STATUS "Found suitable version ${LUA_VERSION_STRING} in ${LUA_INCLUDE_DIR}/lua.h") + endif() + return() + endif() + # Found wrong version -> Ignore this path and retry + if (LUA_Debug) + message(STATUS "Ignoring unsuitable version in ${LUA_INCLUDE_DIR}") + endif() + list(APPEND CMAKE_IGNORE_PATH "${LUA_INCLUDE_DIR}") + unset(LUA_INCLUDE_DIR CACHE) + unset(LUA_INCLUDE_DIR) + unset(LUA_INCLUDE_DIR PARENT_SCOPE) + endwhile () +endfunction() + +_lua_get_versions() +_lua_find_header() +_lua_get_header_version() unset(_lua_append_versions) if (LUA_VERSION_STRING) @@ -160,13 +206,7 @@ find_library(LUA_LIBRARY HINTS ENV LUA_DIR PATH_SUFFIXES lib - PATHS - ~/Library/Frameworks - /Library/Frameworks - /sw - /opt/local - /opt/csw - /opt + PATHS ${_lua_additional_paths} ) unset(_lua_library_names) -- cgit v0.12