# STruC++ Runtime Library CMake Configuration
#
# This CMakeLists.txt is used to build and test the C++ runtime library
# that supports code generated by STruC++.

cmake_minimum_required(VERSION 3.14)
project(strucpp_runtime VERSION 0.1.0 LANGUAGES CXX)

# Require C++17 or later
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Compiler warnings
if(MSVC)
    add_compile_options(/W4 /WX)
else()
    add_compile_options(-Wall -Wextra -Wpedantic -Werror)
endif()

# =============================================================================
# Runtime Library (Header-Only)
# =============================================================================

# The runtime is header-only, so we create an INTERFACE library
add_library(strucpp_runtime INTERFACE)

target_include_directories(strucpp_runtime INTERFACE
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>
)

# =============================================================================
# Testing
# =============================================================================

option(BUILD_TESTING "Build tests" ON)

if(BUILD_TESTING)
    enable_testing()

    # Fetch Google Test
    include(FetchContent)
    FetchContent_Declare(
        googletest
        GIT_REPOSITORY https://github.com/google/googletest.git
        GIT_TAG v1.14.0
    )
    # For Windows: Prevent overriding the parent project's compiler/linker settings
    set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
    FetchContent_MakeAvailable(googletest)

    # Test executable
    add_executable(runtime_tests
        tests/test_iec_types.cpp
        tests/test_iec_var.cpp
        tests/test_std_lib.cpp
        tests/test_debug_dispatch.cpp
    )

    target_link_libraries(runtime_tests PRIVATE
        strucpp_runtime
        GTest::gtest_main
    )

    include(GoogleTest)
    gtest_discover_tests(runtime_tests)
endif()

# =============================================================================
# Installation
# =============================================================================

include(GNUInstallDirs)

install(TARGETS strucpp_runtime
    EXPORT strucpp_runtime-targets
)

install(DIRECTORY include/
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

install(EXPORT strucpp_runtime-targets
    FILE strucpp_runtime-targets.cmake
    NAMESPACE strucpp::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/strucpp_runtime
)
