cmake_minimum_required(VERSION 3.20)
project(luacpp VERSION 0.2.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

option(LUACPP_BUILD_TESTS "Build the conformance test" ON)

# --- Lua 5.4 -----------------------------------------------------------------
find_package(PkgConfig QUIET)
if(PkgConfig_FOUND)
  pkg_check_modules(LUA54 QUIET IMPORTED_TARGET lua5.4)
endif()
if(NOT LUA54_FOUND)
  find_package(Lua 5.4 REQUIRED)
endif()

add_library(luacpp_rt INTERFACE)
add_library(luacpp::rt ALIAS luacpp_rt)
target_include_directories(luacpp_rt INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
if(LUA54_FOUND)
  target_link_libraries(luacpp_rt INTERFACE PkgConfig::LUA54)
  message(STATUS "luacpp: using Lua 5.4 from pkg-config")
else()
  target_include_directories(luacpp_rt INTERFACE ${LUA_INCLUDE_DIR})
  target_link_libraries(luacpp_rt INTERFACE ${LUA_LIBRARIES})
  message(STATUS "luacpp: using Lua 5.4 from FindLua")
endif()

# --- compiler ----------------------------------------------------------------
# `luacpp run` needs to invoke the C++ compiler and link the Lua VM itself, so
# the include path and link flags are baked into the binary at configure time.
if(LUA54_FOUND)
  execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --cflags lua5.4
    OUTPUT_VARIABLE _lua_cflags OUTPUT_STRIP_TRAILING_WHITESPACE)
  execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --libs lua5.4
    OUTPUT_VARIABLE _lua_libs OUTPUT_STRIP_TRAILING_WHITESPACE)
else()
  set(_lua_cflags "-I${LUA_INCLUDE_DIR}")
  string(REPLACE ";" " " _lua_libs "${LUA_LIBRARIES}")
endif()
set(LUACPP_RUNTIME_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include" CACHE PATH
  "Directory holding the luacpp runtime headers (include/luacpp/*.hpp)")
set(LUACPP_LUA_CFLAGS "${_lua_cflags}")
set(LUACPP_LUA_LIBS "${_lua_libs}")

# --- optional GUI backend -----------------------------------------------------
# Programs compiled with `luacpp run` get a global `gui` table (window, drawing
# primitives, events) when SDL2 is available. Without SDL2 nothing changes.
pkg_check_modules(SDL2 QUIET sdl2)
if(SDL2_FOUND)
  execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --cflags sdl2
    OUTPUT_VARIABLE _sdl_cflags OUTPUT_STRIP_TRAILING_WHITESPACE)
  execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --libs sdl2
    OUTPUT_VARIABLE _sdl_libs OUTPUT_STRIP_TRAILING_WHITESPACE)
  set(LUACPP_GUI_FLAGS "-DLUACPP_HAS_GUI=1 ${_sdl_cflags} ${_sdl_libs}")
  message(STATUS "luacpp: SDL2 found, compiled programs get the 'gui' API")
else()
  set(LUACPP_GUI_FLAGS "")
  message(STATUS "luacpp: SDL2 not found, the 'gui' API is disabled")
endif()
configure_file(cmake/luacpp-config.hpp.in
  "${CMAKE_CURRENT_BINARY_DIR}/generated/luacpp-config.hpp" @ONLY)

add_executable(luacpp
  src/main.cpp
  src/frontend.cpp
  src/codegen.cpp)
target_include_directories(luacpp PRIVATE src "${CMAKE_CURRENT_BINARY_DIR}/generated")
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
  target_compile_options(luacpp PRIVATE -Wall -Wextra -Wpedantic)
endif()

include(cmake/lua-package.cmake)

# --- install ------------------------------------------------------------------
# `cmake --install build --prefix <dir>` produces a relocatable tree:
#   <dir>/bin/luacpp, <dir>/include/luacpp/*.hpp, <dir>/share/luacpp/examples
# `luacpp run` looks for include/ next to its own binary, so the tree can be moved.
include(GNUInstallDirs)
install(TARGETS luacpp RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
install(FILES include/luacpp/rt.hpp include/luacpp/gui.hpp
  DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/luacpp")
install(DIRECTORY Demo/
  DESTINATION "${CMAKE_INSTALL_DATADIR}/luacpp/examples"
  PATTERN "build" EXCLUDE)
install(FILES README.md DESTINATION "${CMAKE_INSTALL_DATADIR}/doc/luacpp")

if(LUACPP_BUILD_TESTS)
  enable_testing()
  add_subdirectory(tests)
endif()
