cmake_minimum_required(VERSION 3.22)

#
# This file is generated only once,
# and is not re-generated if converter is called multiple times.
#
# User is free to modify the file as much as necessary
#

# Core project settings
project(CLS_Master)
enable_language(C CXX ASM)
message("Build type: " ${CMAKE_BUILD_TYPE})

# Setup compiler settings
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)

# Core MCU flags, CPU type, instruction set and FPU setup
set(cpu_PARAMS)

# Sources
set(sources_SRCS)

# Include directories for all compilers
set(include_DIRS)

# Include directories for each compiler
set(include_c_DIRS)
set(include_cxx_DIRS)
set(include_asm_DIRS)

# Symbols definition for all compilers
set(symbols_SYMB)

# Symbols definition for each compiler
set(symbols_c_SYMB)
set(symbols_cxx_SYMB)
set(symbols_asm_SYMB)

# Link directories and names of libraries
set(link_DIRS)
set(link_LIBS)

# Linker script
set(linker_script_SRC)

# Compiler options
set(compiler_OPTS)

# Linker options
set(linker_OPTS)

# Now call generated cmake
# This will add script generated
# information to the project
include("cmake_generated/cmake_generated.cmake")


# these options need to be applied for the hole project
# so that all subprojects and libaries are compiled and compatable
add_compile_options(    
    ${cpu_PARAMS}
    ${compiler_OPTS}
    -std=c2x
    -Wall
    -Wextra
    -Wpedantic
    -Wno-unused-parameter)

add_link_options(
    -T${linker_script_SRC}
    ${cpu_PARAMS}
    ${linker_OPTS}
    -Wl,-Map=${CMAKE_PROJECT_NAME}.map
    -u _printf_float # STDIO float formatting support (remove if not used)
    --specs=nosys.specs
    -Wl,--start-group
    -lc
    -lm
    -lstdc++
    -lsupc++
    -Wl,--end-group
    -Wl,-z,max-page-size=8 # Allow good software remapping across address space (with proper GCC section making)
    -Wl,--print-memory-usage
)

# Add project symbols (macros)
add_compile_definitions(
    ULOG_ENABLED
    ${symbols_SYMB}
    ${symbols_c_SYMB}
    ${symbols_cxx_SYMB}
    ${symbols_asm_SYMB}
)


# Link directories setup
# Must be before executable is added

# Create an executable object type

# Add sources to executable

# Add include paths
# these options need to be applied for the hole project
include_directories(
    ${include_DIRS}
    $<$<COMPILE_LANGUAGE:C>: ${include_c_DIRS}>
    $<$<COMPILE_LANGUAGE:CXX>: ${include_cxx_DIRS}>
    $<$<COMPILE_LANGUAGE:ASM>: ${include_asm_DIRS}>
)


add_subdirectory("lib")
add_subdirectory("Application")

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib/nanopb/extra)
find_package(Nanopb REQUIRED)
include_directories(${NANOPB_INCLUDE_DIRS})
nanopb_generate_cpp(PROTO_SRCS PROTO_HDRS RELPATH proto 
    proto/firmware.proto
    proto/cls_device.proto
    proto/light.proto
    proto/usb.proto
)
add_library(PROTOS ${PROTO_SRCS} ${PROTO_HDRS})
target_include_directories(PROTOS PUBLIC ${NANOPB_INCLUDE_DIRS} ${PROJECT_BINARY_DIR})
include_directories(${PROJECT_BINARY_DIR})

# Add linked libraries
# target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC protobuf-nanopb-static)

add_executable(${CMAKE_PROJECT_NAME})
target_sources(${CMAKE_PROJECT_NAME} PUBLIC ${sources_SRCS})

target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC Tasks CLS CLS_BSP BSP EE24 INA219 ulog)
#target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC PROTOS)
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC uart_driver)
#target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC lwrb)

target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE 
    -Wall
    -Wextra
    -Werror
    -Wpedantic
)

# Compiler options
target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
    $<$<COMPILE_LANGUAGE:C>: >
    $<$<COMPILE_LANGUAGE:CXX>:
    
     -Wno-volatile
     -Wold-style-cast
     -Wuseless-cast
     -Wsuggest-override
    >
    $<$<COMPILE_LANGUAGE:ASM>:-x assembler-with-cpp -MMD -MP>
    $<$<CONFIG:Debug>:-Og -g3 -ggdb>
    $<$<CONFIG:Release>:-O3 -g0>
)


# Execute post-build to print size, generate hex and bin
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${CMAKE_PROJECT_NAME}>
    COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.hex
    COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.bin
)


add_subdirectory(tools)