cmake_minimum_required(VERSION 3.14)
project(FEAPI LANGUAGES C CXX)

add_library(FEAPI INTERFACE)
add_library(FE::API ALIAS FEAPI)
target_include_directories(FEAPI INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}")
target_compile_features(FEAPI INTERFACE cxx_std_17)

option(FEAPI_BUILD_EXAMPLE "Build the sample user-code module" ON)
if(FEAPI_BUILD_EXAMPLE)
    add_library(FEAPI_Example MODULE ExampleModule.cpp)
    target_link_libraries(FEAPI_Example PRIVATE FE::API)
    target_compile_definitions(FEAPI_Example PRIVATE FEAPI_BUILD_MODULE=1)
    set_target_properties(FEAPI_Example PROPERTIES
        PREFIX ""
        OUTPUT_NAME "FEAPI_Example"
        CXX_VISIBILITY_PRESET hidden
        VISIBILITY_INLINES_HIDDEN YES
    )
endif()

option(FEAPI_BUILD_USER_MODULE "Build the C++ files from a Sim project Code directory" OFF)
set(FEAPI_USER_CODE_DIR "" CACHE PATH "Project Code directory containing user .cpp files")
set(FEAPI_USER_SOURCE_FILE "" CACHE FILEPATH "Single project .cpp file used to build this module")
set(FEAPI_USER_MODULE_OUTPUT_DIR "" CACHE PATH "Optional output directory for FEProgram")
set(FEAPI_USER_MODULE_NAME "FEProgram" CACHE STRING "Output name for the user module")

if(FEAPI_BUILD_USER_MODULE)
    if(NOT FEAPI_USER_CODE_DIR)
        message(FATAL_ERROR "Set FEAPI_USER_CODE_DIR to the Sim project's Code directory")
    endif()

    if(FEAPI_USER_SOURCE_FILE)
        if(NOT EXISTS "${FEAPI_USER_SOURCE_FILE}" OR IS_DIRECTORY "${FEAPI_USER_SOURCE_FILE}")
            message(FATAL_ERROR "FEAPI_USER_SOURCE_FILE does not name an existing .cpp file: ${FEAPI_USER_SOURCE_FILE}")
        endif()
        get_filename_component(FEAPI_USER_SOURCE_EXTENSION "${FEAPI_USER_SOURCE_FILE}" EXT)
        if(NOT FEAPI_USER_SOURCE_EXTENSION STREQUAL ".cpp")
            message(FATAL_ERROR "FEAPI_USER_SOURCE_FILE must have a .cpp extension")
        endif()
        set(FEAPI_USER_SOURCES "${FEAPI_USER_SOURCE_FILE}")
    else()
        # Preserve the standalone build behavior for callers that do not select
        # one source file explicitly.
        file(GLOB_RECURSE FEAPI_USER_SOURCES CONFIGURE_DEPENDS
            "${FEAPI_USER_CODE_DIR}/*.cpp")
    endif()
    if(NOT FEAPI_USER_SOURCES)
        message(FATAL_ERROR "No .cpp files found in ${FEAPI_USER_CODE_DIR}")
    endif()

    add_library(FEProgram MODULE ${FEAPI_USER_SOURCES})
    target_link_libraries(FEProgram PRIVATE FE::API)
    target_compile_definitions(FEProgram PRIVATE FEAPI_BUILD_MODULE=1)
    target_include_directories(FEProgram PRIVATE "${FEAPI_USER_CODE_DIR}")
    set_target_properties(FEProgram PROPERTIES
        PREFIX ""
        OUTPUT_NAME "${FEAPI_USER_MODULE_NAME}"
    )

    if(FEAPI_USER_MODULE_OUTPUT_DIR)
        set_target_properties(FEProgram PROPERTIES
            LIBRARY_OUTPUT_DIRECTORY "${FEAPI_USER_MODULE_OUTPUT_DIR}"
            RUNTIME_OUTPUT_DIRECTORY "${FEAPI_USER_MODULE_OUTPUT_DIR}"
        )
        # Multi-configuration generators (Visual Studio, Xcode) otherwise
        # append a configuration directory such as Release/ to the output.
        foreach(config DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
            set_target_properties(FEProgram PROPERTIES
                LIBRARY_OUTPUT_DIRECTORY_${config} "${FEAPI_USER_MODULE_OUTPUT_DIR}"
                RUNTIME_OUTPUT_DIRECTORY_${config} "${FEAPI_USER_MODULE_OUTPUT_DIR}"
            )
        endforeach()
    endif()
endif()
