Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[cmake] cleanups in the Lua detection
[simgrid.git] / tools / cmake / Modules / FindLuaSimgrid.cmake
index fe6f2b6..cc27338 100644 (file)
@@ -1,48 +1,62 @@
-include(./FindLua)
-find_program(HAVE_LUA_BIN NAMES lua)
-mark_as_advanced(HAVE_LUA_BIN)
+# Search for the Lua 5.3 include files and libraries
+# 
+#  Input variable:
+#     LUA_HINT: path to Lua installation -- only needed for non-standard installs
+#  Output variable:
+#     HAVE_LUA         : if Lua was found
+#     LUA_LIBRARY      : the path to the dynamic library
+#     LUA_INCLUDE_DIR  : where to find lua.h
+#     LUA_VERSION_MAJOR: First part of the version (often, 5)
+#     LUA_VERSION_MINOR: Second part of the version (3 when we have 5.3)
 
-message(STATUS "Looking for lua.h")
-if(LUA_INCLUDE_DIR)
-  message(STATUS "Looking for lua.h - found")
-else()
-  message(STATUS "Looking for lua.h - not found")
-endif()
+find_path(LUA_INCLUDE_DIR lua.h
+  HINTS
+    ENV LUA_HINT
+  PATH_SUFFIXES include/lua53 include/lua5.3 include/lua-5.3 include/lua include
+  PATHS
+  ~/Library/Frameworks
+  /Library/Frameworks
+  /sw # Fink
+  /opt/local # DarwinPorts
+  /opt/csw # Blastwave
+  /opt
+)
 
-if(HAVE_LUA_BIN)
-  message(STATUS "Found Lua: ${HAVE_LUA_BIN}")
+find_library(LUA_LIBRARY
+  NAMES lua53 lua5.3 lua-5.3 lua
+  HINTS
+    ENV LUA_HINT
+  PATH_SUFFIXES lib
+  PATHS
+  ~/Library/Frameworks
+  /Library/Frameworks
+  /sw
+  /opt/local
+  /opt/csw
+  /opt
+)
+if (NOT LUA_LIBRARY) 
+  message(FATAL_ERROR "Error: Lua library not found. Please install that package (and set LUA_HINT) or disable Lua.")
 endif()
-
-set(LIB_LUA_NAME "")
-
-foreach(lib_path ${LUA_LIBRARIES})
-  if(NOT LIB_LUA_NAME)
-    string(REGEX MATCH "liblua.*$" LIB_LUA_NAME "${lib_path}")
-    string(REPLACE ".${LIB_EXE}" "" LIB_LUA_NAME "${LIB_LUA_NAME}")
-    string(REPLACE "lib" "" LIB_LUA_NAME "${LIB_LUA_NAME}")
-    if(LIB_LUA_NAME)
-      string(REPLACE "/lib${LIB_LUA_NAME}.${LIB_EXE}" "" LUA_LIBRARY_DIR ${lib_path})
-    endif()
-  endif()
-endforeach(lib_path ${LUA_LIBRARIES})
-
-message(STATUS "Looking for lib lua")
-if(LUA_LIBRARY_DIR)
-  message(STATUS "Looking for lib lua - found")
-  message(STATUS "Lua version: ${LUA_VERSION_STRING}")
-  message(STATUS "Lib path   : ${LUA_LIBRARY_DIR}")
-else()
-  message(STATUS "Looking for lib lua - not found")
+if (NOT LUA_INCLUDE_DIR OR NOT EXISTS "${LUA_INCLUDE_DIR}/lua.h")
+  message(FATAL_ERROR "Error: Lua header file not found. Please install that package (and set LUA_HINT) or disable Lua.")
 endif()
 
-if(LUA_FOUND)
-  if(LUA_VERSION_MAJOR EQUAL 5 AND LUA_VERSION_MINOR EQUAL 3)
-    set(HAVE_LUA 1)
-    include_directories(${LUA_INCLUDE_DIR})
-    link_directories(${LUA_LIBRARY_DIR})
-  else()
-    message(FATAL_ERROR "Error: Lua version 5.3 is required, but version ${LUA_VERSION_STRING} found instead.")
-  endif()
+# Extract the version info out of the header file
+file(STRINGS "${LUA_INCLUDE_DIR}/lua.h" lua_version_str REGEX "^#define[ \t]+LUA_VERSION_MAJOR[ \t]+\"[456]+\"")
+  string(REGEX REPLACE "^#define[ \t]+LUA_VERSION_MAJOR[ \t]+\"([^\"]+)\"" "\\1" LUA_VERSION_MAJOR "${lua_version_str}")
+file(STRINGS "${LUA_INCLUDE_DIR}/lua.h" lua_version_str REGEX "^#define[ \t]+LUA_VERSION_MINOR[ \t]+\"[0123456789]+\"")
+  string(REGEX REPLACE "^#define[ \t]+LUA_VERSION_MINOR[ \t]+\"([^\"]+)\"" "\\1" LUA_VERSION_MINOR "${lua_version_str}")
+unset(lua_version_str)
+  
+# Check that we have a sufficient version of Lua
+if(LUA_VERSION_MAJOR EQUAL 5 AND LUA_VERSION_MINOR EQUAL 3)
+  set(HAVE_LUA 1)
+    
+  include_directories(${LUA_INCLUDE_DIR})
 else()
-  message(FATAL_ERROR "Error: Lua version 5.3 is required, but Lua not found.")
+  message(FATAL_ERROR "Error: Lua version 5.3 is required, but version ${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR} found instead.")
 endif()
+
+message(STATUS "Lua version: ${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}")
+message(STATUS "Lua library: ${LUA_LIBRARY}")