# Copyright (C) 2023 Advanced Micro Devices, Inc.  All rights reserved.
# Copyright (c) 2021 - 2022 Xilinx, Inc.  All rights reserved.
# SPDX-License-Identifier: MIT
cmake_minimum_required(VERSION 3.14.7)
include(${CMAKE_CURRENT_SOURCE_DIR}/UsbpsuExample.cmake)
project(usbpsu)

find_package(common)
enable_language(C ASM)
collect(PROJECT_LIB_DEPS xilstandalone)
collect(PROJECT_LIB_DEPS xil)
collect(PROJECT_LIB_DEPS xiltimer)
collect(PROJECT_LIB_DEPS gcc)
collect(PROJECT_LIB_DEPS c)
collector_list (_deps PROJECT_LIB_DEPS)

SET(DRIVER_INSTANCES "${NUM_DRIVER_INSTANCES}" CACHE STRING "Driver Instances")
SET_PROPERTY(CACHE DRIVER_INSTANCES PROPERTY STRINGS "${NUM_DRIVER_INSTANCES}")
set(index 0)
LIST_INDEX(${index} ${DRIVER_INSTANCES} "${NUM_DRIVER_INSTANCES}")
list(GET TOTAL_EXAMPLE_LIST ${index} ex_list)
list(GET REG_LIST ${index} reg)
SET(XUSBPSU_BASEADDRESS "${reg}")
SET(xusbpsu_poll_example xusb_poll_example.c;xusb_ch9.c;xusb_wrapper.c;xusb_class_storage.c;xusb_ch9_storage.c;)
list(APPEND EXAMPLE_LIST xusbpsu_poll_example)
SET(xusbpsu_intr_example xusb_intr_example.c;xusb_ch9.c;xusb_wrapper.c;xusb_class_storage.c;xusb_ch9_storage.c;)
list(APPEND EXAMPLE_LIST xusbpsu_intr_example)
SET(xusbpsu_dfu_example xusb_dfu_example.c;xusb_ch9.c;xusb_wrapper.c;xusb_class_dfu.c;xusb_ch9_dfu.c;)
list(APPEND EXAMPLE_LIST xusbpsu_dfu_example)
SET(xusbpsu_audio_dfu_example xusb_audio_dfu_example.c;xusb_ch9.c;xusb_wrapper.c;xusb_ch9_audio_dfu.c;xusb_class_audio_dfu.c;)
list(APPEND EXAMPLE_LIST xusbpsu_audio_dfu_example)
SET(xusbpsu_composite_example xusb_composite_example.c;xusb_ch9.c;xusb_wrapper.c;xusb_ch9_composite.c;xusb_class_composite.c;)
list(APPEND EXAMPLE_LIST xusbpsu_composite_example)

set(CMAKE_INFILE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../../scripts/linker_files/")
linker_gen(${CMAKE_INFILE_PATH})
gen_exheader(${CMAKE_INFILE_PATH} ${CMAKE_PROJECT_NAME} ${XUSBPSU_BASEADDRESS} x)

foreach(EXAMPLE ${EXAMPLE_LIST})
    message("example ")
    message("${${EXAMPLE}}")
    add_executable(${EXAMPLE}.elf ${${EXAMPLE}})
    target_link_libraries(${EXAMPLE}.elf -Wl,--gc-sections -T\"${CMAKE_SOURCE_DIR}/lscript.ld\" -L\"${CMAKE_SOURCE_DIR}/\" -Wl,--start-group ${_deps} -Wl,--end-group)
endforeach()
