# HG changeset patch
# User koda
# Date 1371028674 -7200
# Node ID acb2492288e53bb1e1b3ec0bba8b9377b0cb7450
# Parent  16373565692a0b9b8394438cd61bb195726d9671
heavily rework lua discovery and linking, needs testing but it's more similar to what we do for other libraries now

diff -r 16373565692a -r acb2492288e5 CMakeLists.txt
--- a/CMakeLists.txt	Wed Jun 12 09:49:46 2013 +0200
+++ b/CMakeLists.txt	Wed Jun 12 11:17:54 2013 +0200
@@ -21,6 +21,12 @@
 #set this to ON when 2.1.0 becomes more widespread (and only for linux)
 option(PHYSFS_SYSTEM "Use system physfs (off)" OFF)
 
+if(WIN32 OR APPLE)
+    option(LUA_SYSTEM "Use system lua (off)" OFF)
+else()
+    option(LUA_SYSTEM "Use system lua (on)" ON)
+endif()
+
 option(BUILD_ENGINE_LIBRARY "Enable hwengine library (off)" OFF)
 option(ANDROID "Enable Android build (off)" OFF)
 
@@ -135,12 +141,21 @@
 include(${CMAKE_MODULE_PATH}/utils.cmake)
 
 #lua discovery
-find_package(Lua)
-if(LUA_FOUND)
-    message(STATUS "Found LUA: ${LUA_DEFAULT}")
+if (${LUA_SYSTEM})
+    if (NOT LUA_LIBRARY OR NOT LUA_INCLUDE_DIR)
+        find_package(Lua)
+    endif()
+
+    if (LUA_LIBRARY AND LUA_INCLUDE_DIR)
+        set(LUA_FOUND TRUE)
+    else()
+        message(FATAL_ERROR "Missing Lua! Rerun cmake with -DLUA_SYSTEM=off to build the internal version")
+    endif()
 else()
     message(STATUS "LUA will be provided by the bundled sources")
     add_subdirectory(misc/liblua)
+    set(lua_output_name "hwlua")
+    list(APPEND pascal_flags "-XLAlua=${lua_output_name}" "-dLUA_INTERNAL")
 endif()
 
 
diff -r 16373565692a -r acb2492288e5 cmake_modules/FindLua.cmake
--- a/cmake_modules/FindLua.cmake	Wed Jun 12 09:49:46 2013 +0200
+++ b/cmake_modules/FindLua.cmake	Wed Jun 12 11:17:54 2013 +0200
@@ -1,37 +1,26 @@
-# Find the Lua library
-# --------------------
-# On Android/Windows/OSX this just defines the name of the library that
-#  will be compiled from our bundled sources
-# On Linux it will try to load the system library and fallback to compiling
-#  the bundled one when nothing is found
-
-set(LUA_FOUND false)
-set(LUA_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/misc/liblua)
+# Find liblua
+#
+# Once done this will define
+#  LUA_FOUND - system has Lua
+#  LUA_INCLUDE_DIR - the Lua include directory
+#  LUA_LIBRARY - The library needed to use Lua
+# Copyright (c) 2013, Vittorio Giovara <vittorio.giovara@gmail.com>
+#
+# Distributed under the OSI-approved BSD License (the "License");
+# see accompanying file Copyright.txt for details.
+#
+# This software is distributed WITHOUT ANY WARRANTY; without even the
+# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the License for more information.
 
-if (ANDROID)
-    SET(LUA_DEFAULT "liblua5.1.so")
-else (ANDROID)
-    IF(WIN32)
-        SET(LUA_DEFAULT lua.dll)
-    ELSE(WIN32)
-        IF(APPLE)
-            SET(LUA_DEFAULT lua)
-        ELSE(APPLE)
-            #locate the system's lua library
-            FIND_LIBRARY(LUA_DEFAULT NAMES lua51 lua5.1 lua-5.1 lua PATHS /lib /usr/lib /usr/local/lib /usr/pkg/lib)
-            IF(${LUA_DEFAULT} MATCHES "LUA_DEFAULT-NOTFOUND")
-                set(LUA_DEFAULT lua)
-            ELSE()
-                set(LUA_FOUND true)
-                message(STATUS "LibLua 5.1 found at ${LUA_DEFAULT}")
-                find_path(LUA_INCLUDE_DIR lua.h)
-                #remove the path (fpc doesn't like it - why?)
-                GET_FILENAME_COMPONENT(LUA_DEFAULT ${LUA_DEFAULT} NAME)
-            ENDIF()
-        ENDIF(APPLE)
-    ENDIF(WIN32)
-ENDIF(ANDROID)
+include(FindPackageHandleStandardArgs)
 
-SET(LUA_LIBRARY ${LUA_DEFAULT} CACHE STRING "Lua library to link to; file name without path only!")
+find_path(LUA_INCLUDE_DIR lua.h
+                          PATHS /usr/include /usr/local/include /usr/pkg/include
+                          PATH_SUFFIXES lua5.1)
+find_library(LUA_LIBRARY NAMES lua51 lua5.1 lua-5.1 lua
+                         PATHS /lib /usr/lib /usr/local/lib /usr/pkg/lib)
 
+find_package_handle_standard_args(Lua DEFAULT_MSG LUA_LIBRARY LUA_INCLUDE_DIR)
+mark_as_advanced(LUA_INCLUDE_DIR LUA_LIBRARY)
 
diff -r 16373565692a -r acb2492288e5 hedgewars/CMakeLists.txt
--- a/hedgewars/CMakeLists.txt	Wed Jun 12 09:49:46 2013 +0200
+++ b/hedgewars/CMakeLists.txt	Wed Jun 12 11:17:54 2013 +0200
@@ -172,6 +172,16 @@
     list(APPEND pascal_flags "-dPNG_SCREENSHOTS" "-Fl${PNG_LIBRARY_DIR}" "-k-L${PNG_LIBRARY_DIR}")
 endif()
 
+if(LUA_FOUND AND LUA_SYSTEM)
+    get_filename_component(LUA_LIBRARY_DIR ${LUA_LIBRARY} PATH)
+    get_filename_component(LUA_LIBRARY_NAME ${LUA_LIBRARY} NAME)
+    #NAME_WE would strip the .1 (or .2) next to the ".so"
+    string(REGEX REPLACE "${CMAKE_SHARED_LIBRARY_PREFIX}(.*)${CMAKE_SHARED_LIBRARY_SUFFIX}" "\\1" LUA_LIBRARY_NAME "${LUA_LIBRARY_NAME}")
+    list(APPEND pascal_flags )
+    list(APPEND pascal_flags "-Fl${LUA_LIBRARY_DIR}"
+                             "-k-L${LUA_LIBRARY_DIR}"
+                             "-XLAlua=${LUA_LIBRARY_NAME}")
+endif()
 
 #this command is a workaround to some inlining issues present in older FreePascal versions and fixed in 2.6
 if(FREEPASCAL_VERSION VERSION_LESS "2.6")
diff -r 16373565692a -r acb2492288e5 hedgewars/LuaPas.pas
--- a/hedgewars/LuaPas.pas	Wed Jun 12 09:49:46 2013 +0200
+++ b/hedgewars/LuaPas.pas	Wed Jun 12 11:17:54 2013 +0200
@@ -14,8 +14,11 @@
 uses uConsts;
 {.$DEFINE LUA_GETHOOK}
 
-{$INCLUDE "config.inc"}
-const LuaLibName = cLuaLibrary;
+const LuaLibName = {$IFDEF LUA_INTERNAL}'libhwlua'{$ELSE}'liblua'{$ENDIF};
+
+{$IFNDEF WIN32}
+    {$linklib lua}
+{$ENDIF}
 
 type
     size_t   = Cardinal;
diff -r 16373565692a -r acb2492288e5 hedgewars/config.inc.in
--- a/hedgewars/config.inc.in	Wed Jun 12 09:49:46 2013 +0200
+++ b/hedgewars/config.inc.in	Wed Jun 12 11:17:54 2013 +0200
@@ -25,5 +25,4 @@
       cVersionString = '${HEDGEWARS_VERSION}';
       cRevisionString = '${HEDGEWARS_REVISION}';
       cHashString = '${HEDGEWARS_HASH}';
-      cLuaLibrary = '${LUA_LIBRARY}';
       cDefaultPathPrefix = '${HEDGEWARS_FULL_DATADIR}/Data';
diff -r 16373565692a -r acb2492288e5 hedgewars/uScript.pas
--- a/hedgewars/uScript.pas	Wed Jun 12 09:49:46 2013 +0200
+++ b/hedgewars/uScript.pas	Wed Jun 12 11:17:54 2013 +0200
@@ -55,10 +55,6 @@
 implementation
 {$IFDEF USE_LUA_SCRIPT}
 
-{$IFNDEF WIN32}
-    {$linklib lua}
-{$ENDIF}
-
 uses LuaPas,
     uConsole,
     uConsts,
diff -r 16373565692a -r acb2492288e5 misc/liblua/CMakeLists.txt
--- a/misc/liblua/CMakeLists.txt	Wed Jun 12 09:49:46 2013 +0200
+++ b/misc/liblua/CMakeLists.txt	Wed Jun 12 11:17:54 2013 +0200
@@ -13,13 +13,14 @@
 
 set_target_properties(lua PROPERTIES
                           VERSION "5.1.4"
-                          SOVERSION 1)
+                          SOVERSION 1
+                          OUTPUT_NAME ${lua_output_name}))
 install(TARGETS lua RUNTIME DESTINATION ${target_binary_install_dir}
                     LIBRARY DESTINATION ${target_library_install_dir}
                     ARCHIVE DESTINATION ${target_library_install_dir})
 get_target_property(lua_fullpath lua LOCATION)
 
-set(LUA_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE STRING "" FORCE)
-set(LUA_LIBRARY ${lua_fullpath} CACHE STRING "" FORCE)
+set(LUA_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE STRING "Lua include dir" FORCE)
+set(LUA_LIBRARY ${lua_fullpath} CACHE STRING "Lua library" FORCE)