cmake_minimum_required(VERSION 3.13)

# Project Name
project(romeo-obsw C ASM)

# Set path names
set(OBSW_NAME romeo-obsw)
set(FreeRTOS_PATH contrib/FreeRTOS-Kernel/)

# Compiler Options
set(ROMEO_Z7_COMPILE_OPTIONS -c -fmessage-length=0 -g -O0 -mcpu=cortex-a9 -mfpu=vfpv3 -mfloat-abi=hard -ffunction-sections -fdata-sections)
set(ROMEO_LINUX_COMPILE_OPTIONS -c -fmessage-length=0 -g -O0 -ffunction-sections -fdata-sections)
set(ROMEO_Z7_LINK_OPTIONS -Wl,--cref -Wl,-Map=${OBSW_NAME}.map -mcpu=cortex-a9 -mfpu=vfpv3 -mfloat-abi=hard -Wl,-build-id=none -T${CMAKE_SOURCE_DIR}/bsp_z7/freeRTOS/lscript.ld -specs=${CMAKE_SOURCE_DIR}/bsp_z7/freeRTOS/Xilinx.spec )
set(ROMEO_WARNING_FLAGS
    -Wall
    -Wextra
    -Wpedantic
    -Werror) # TODO so far, this only affects mission code, not bsp

option(ROMEO_LOW_LEVEL_TESTS "build low level tests INSTEAD of normal binary" OFF)

if(${ROMEO_LOW_LEVEL_TESTS})
    add_compile_definitions(LOW_LEVEL_TESTS)
    set(OBSW_NAME romeo-low_level_tests)
endif()

# CMake options which are only available when crosscompiling
if (${CMAKE_CROSSCOMPILING})
    set(ZYNQ_UART UART1 CACHE STRING "Which PS UART to use for stdout")
    set_property(CACHE ZYNQ_UART PROPERTY STRINGS UART0 UART1)

    if(${ZYNQ_UART} STREQUAL UART0)
        add_compile_definitions(ZYNQ_USE_UART0)
    endif()

    option(ARM_SEMIHOSTING "enable semihosting for emulation" OFF)

    if(${ARM_SEMIHOSTING})
        add_compile_definitions(ARM_SEMIHOSTING)
    endif()
else()
    unset(ZYNQ_UART)
    unset(ZYNQ_UART CACHE)
    unset(ARM_SEMIHOSTING)
    unset(ARM_SEMIHOSTING CACHE)
endif()

# Add main executable
add_executable(${OBSW_NAME})

# export lwip path, so the bsp can find it
if(${CMAKE_CROSSCOMPILING})
    set (LWIP_DIR ${CMAKE_CURRENT_SOURCE_DIR}/contrib/lwip)
endif()

# Platform dependend configuration
# Includes setting the FreeRTOS port, compiler options and
# selecting the bsp
if(${CMAKE_CROSSCOMPILING})
    #TODO: this somewhat hardcodes zynq as the only cross target
    set(FREERTOS_PORT GCC_ARM_CA9 CACHE STRING "")
    set(FREERTOS_HEAP ${CMAKE_CURRENT_SOURCE_DIR}/bsp_z7/freeRTOS/no_heap.c CACHE STRING "")

    # config library for FreeRTOS
    add_library(freertos_config INTERFACE)

    # The config file directory
    target_include_directories(freertos_config SYSTEM
        INTERFACE bsp_z7/freeRTOS)

    # no coverage tests
    target_compile_definitions(freertos_config
        INTERFACE
        projCOVERAGE_TEST=0)
    
    # FreeRTOS config depends on bsp headers
    target_include_directories(
        freertos_config INTERFACE bsp_z7/ps7_cortexa9_0/include)

    # Compiler options, we set them on the FreeRTOS config, will trickle down from there
    target_compile_options(freertos_config INTERFACE ${ROMEO_Z7_COMPILE_OPTIONS})
    
    # add the bsp
    add_subdirectory(bsp_z7)

    # Link options for cross build
    target_link_options(${OBSW_NAME} PRIVATE ${ROMEO_Z7_LINK_OPTIONS})
else()
    # So far, only unix port supported when not crosscompiling
    set(FREERTOS_PORT GCC_POSIX CACHE STRING "")

    # config library for FreeRTOS
    add_library(freertos_config INTERFACE)
    
    # The config file directory
    target_include_directories(freertos_config SYSTEM
        INTERFACE bsp_linux/freeRTOS)

    # Compiler options, we set them on the FreeRTOS config, will trickle down from there
    target_compile_options(freertos_config INTERFACE ${ROMEO_LINUX_COMPILE_OPTIONS})
    
    # No coverage tests, no tracing
    target_compile_definitions(freertos_config
        INTERFACE
        projCOVERAGE_TEST=0
        projENABLE_TRACING=0)

    # add the bsp
    add_subdirectory(bsp_linux)
endif()

# Add FreeRTOS
add_subdirectory(${FreeRTOS_PATH})

# Common stuff that is not mission code, mostly code shared between bsps
add_subdirectory(common)

# Mission code, in our case C glue between rust and bsp
add_subdirectory(mission)

# The actual mission code, in rust, exported as library `mission_rust`
add_subdirectory(mission_rust)

# We provide a firmware image for an interface board as part of the z7 build
# This is a second executable, excluded from `all` builds
if(${CMAKE_CROSSCOMPILING})
    add_subdirectory(sim_interface)
endif()

# Link all libraries into the binary
# bsp depends on FreeRTOS, so we do not need to link it again
target_link_libraries(${OBSW_NAME} PUBLIC bsp mission_rust )

# Common link options
target_link_options(${OBSW_NAME} PRIVATE "-Wl,--gc-sections")


# Common compile options
target_compile_options(${OBSW_NAME} PRIVATE ${ROMEO_WARNING_FLAGS})