cmake_minimum_required(VERSION 3.1)
include(ExternalProject)

# This file will
function(AddExternalLua _lua_version _lua_hash)

    if (CMAKE_HOST_SYSTEM_NAME STREQUAL Linux)
        set(LUA_MAKE_TARGET linux)
    elseif (CMAKE_HOST_SYSTEM_NAME STREQUAL Darwin)
        set(LUA_MAKE_TARGET macosx)
    elseif (CMAKE_HOST_SYSTEM_NAME STREQUAL Windows)
        set(LUA_MAKE_TARGET posix)
    else()
        set(LUA_MAKE_TARGET posix)
    endif()
    message("-- Lua: Building Lua for ${LUA_MAKE_TARGET}")

    ExternalProject_Add(LuaCore
            URL "https://www.lua.org/ftp/lua-${_lua_version}.tar.gz"
            URL_HASH SHA1=${_lua_hash}
            SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/lua
            CONFIGURE_COMMAND ""
            BUILD_IN_SOURCE True
            BUILD_COMMAND make ${LUA_MAKE_TARGET}
            INSTALL_COMMAND ""
            )

    set(LUA_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/external/lua/src" PARENT_SCOPE)
    set(LUA_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/external/lua/src/liblua.a" "-ldl" PARENT_SCOPE)

endfunction()