cmake_minimum_required(VERSION 3.0)


# unpack headers needed
set(SPC_DRV drv_header_v402b6844.zip)
set(SPC_HEADERS
        include/spcerr.h
        include/regs.h
        include/dlltyp.h
        include/spcioctl.inc)

add_custom_command(OUTPUT ${SPC_HEADERS}
        COMMAND unzip -o -q -u ${SPC_DRV} "*.h" "*.txt" "*.inc" -d include/
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        COMMENT "Unpacking SPC headers"
        VERBATIM)

# Kernelmodule is build from NDA source code.
# just go to the current directory and tar xfz drvsrc_all_V*.tgz
# this will usually create a "linux" folder
# the actual kernel source file is in the src_all/micx_drv subfolder
# If the "linux" folder exists the driver will be build

if(DEFINED ENV{SPC_SOURCE})
    message("SPC_SOURCE environment variable set")
	set(SPC_SRC $ENV{SPC_SOURCE})
else()
    message("Spectrum driver source path not defined in envirnoment variable SPC_SOURCE")
	set(SPC_SRC ${CMAKE_CURRENT_SOURCE_DIR}/linux/src_all/micx_drv)
endif()
set(DRIVER_FILE ${SPC_SRC}/spc_smp.ko )
set(KERNEL_DIR "/lib/modules/${CMAKE_SYSTEM_VERSION}/build" )
set(KBUILD_CMD ${CMAKE_MAKE_PROGRAM}
               -C ${KERNEL_DIR}
               M=${SPC_SRC} modules)

#unset(spc_src CACHE)
#find_file(spc_src )

add_custom_command(OUTPUT ${DRIVER_FILE}
                   COMMAND ${KBUILD_CMD}
                   COMMENT "Building spc_smp.ko"
                   WORKING_DIRECTORY ${SPC_SRC}
                   VERBATIM)
if(EXISTS ${CMAKE_SOURCE_DIR}/drivers/Spectrum-MI40xxSeries/linux)
    add_custom_target (micx_drv ALL DEPENDS ${DRIVER_FILE} )
    install(FILES ${DRIVER_FILE} DESTINATION /lib/modules/${CMAKE_SYSTEM_VERSION}/kernel/damaris)

else()
    message(Spectrum MI40xx dirver not found, unpack and configure it)
endif()
add_library(Spectrum_MI40xxSeries STATIC Spectrum-MI40xxSeries.cpp GatedData.cpp ${SPC_HEADERS})

