cmake_minimum_required(VERSION 3.14)

project(BoundaryScanExplorer)

# Put the built executable at the build/ root (build/bs), not nested under
# build/src/bs/ where the source tree would otherwise mirror it.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

# ---- libbsdl (BSDL parser, extracted from this repo and now standalone) ----
# Pulled via FetchContent from the canonical Gitea URL. For a local checkout,
# set -DFETCHCONTENT_SOURCE_DIR_BSDL=/path/to/libbsdl at configure time
# (built-in FetchContent override — no clone happens).
include(FetchContent)
set(BSDL_BUILD_CLI   OFF CACHE BOOL "" FORCE)
set(BSDL_BUILD_TESTS OFF CACHE BOOL "" FORCE)
FetchContent_Declare(bsdl
    GIT_REPOSITORY ssh://gitea@git.beafrancois.fr:8329/electronics/libbsdl.git
    GIT_TAG        main
)
FetchContent_MakeAvailable(bsdl)

# ---- libsvf (SVF player, extracted from this repo and now standalone) ----
# Same pattern as libbsdl. Local checkout override:
# -DFETCHCONTENT_SOURCE_DIR_SVF=/path/to/libsvf
FetchContent_Declare(svf
    GIT_REPOSITORY ssh://gitea@git.beafrancois.fr:8329/electronics/libsvf.git
    GIT_TAG        main
)
FetchContent_MakeAvailable(svf)

# Digilent JTAG-SMT* backend. The driver dlopen's libdjtg/libdmgr at
# runtime and degrades to "no probe" if they're absent, so building it
# in costs nothing — default ON. Needs <dlfcn.h>, so UNIX only.
if(UNIX)
    set(BS_DIGILENT_DEFAULT ON)
else()
    set(BS_DIGILENT_DEFAULT OFF)
endif()
option(BS_ENABLE_DIGILENT
       "Enable Digilent JTAG-SMT* backend (dlopen libdjtg.so / libdmgr.so at runtime; Adept Runtime only needed to actually use such a probe)"
       ${BS_DIGILENT_DEFAULT})

# script and jtag_core must be the last linked archive for the application to compile
set(BS_MODULES script jtag_core)
set(DIR_MODULES ${CMAKE_SOURCE_DIR}/src/modules)
set(DIR_LIBS ${CMAKE_SOURCE_DIR}/src/libs)

# We dive into submodules
file(GLOB MODULES_DIRS RELATIVE ${DIR_MODULES} ${DIR_MODULES}/*)
foreach(module ${MODULES_DIRS})
    set(module_path "${DIR_MODULES}/${module}")

    # checks if it is a sub-directory and if it contains a cmake file
    if(IS_DIRECTORY ${module_path} AND EXISTS "${module_path}/CMakeLists.txt")
        message(STATUS "Submodule : ${module}")
        # already existing modules are not included in the list
        if(NOT ${module} IN_LIST BS_MODULES)
            list(APPEND BS_MODULES ${module})
        endif()
        # We'll compile the module
        add_subdirectory(src/modules/${module})
    else()
        message(STATUS "Ignored   : ${module}")
    endif()
endforeach()

add_subdirectory(src/bs)
