cmake_minimum_required(VERSION 3.21)
cmake_policy(SET CMP0171 NEW)
project(twnlua LANGUAGES C)

if(NOT CMAKE_BUILD_TYPE)
        set(CMAKE_BUILD_TYPE Debug)
endif()

add_subdirectory($ENV{TWNROOT} $ENV{TWNROOT}/build)

set(FLAGS
        $<$<NOT:$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>>:--no-dynlib-game>
)

add_custom_command(
   OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/luabind.c
   COMMAND ${PYTHON3} ${CMAKE_CURRENT_SOURCE_DIR}/bindgen.py $ENV{TWNROOT}/share/twn_api.json ${FLAGS} > ${CMAKE_CURRENT_SOURCE_DIR}/luabind.c
   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/bindgen.py $ENV{TWNROOT}/share/twn_api.json
   CODEGEN
)

add_custom_command(
   OUTPUT ${CMAKE_SOURCE_DIR}/data/scripts/twnapi.lua
   COMMAND ${PYTHON3} ${CMAKE_CURRENT_SOURCE_DIR}/docgen.py $ENV{TWNROOT}/share/twn_api.json > ${CMAKE_SOURCE_DIR}/data/scripts/twnapi.lua
   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/docgen.py $ENV{TWNROOT}/share/twn_api.json
)

add_custom_target(
    twnlua_docgen ALL
    DEPENDS ${CMAKE_SOURCE_DIR}/data/scripts/twnapi.lua
)

add_compile_definitions(LUA_32BITS)

set(SOURCE_FILES
        game.c
        state.h
        minilua.c
        ${CMAKE_CURRENT_SOURCE_DIR}/luabind.c
)

use_townengine("${SOURCE_FILES}" ${TWN_OUT_DIR})
