cmake_minimum_required(VERSION 3.10)

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})

# 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)
