Skip to content

Commit

Permalink
wip(cmake): make custom targets more explicit
Browse files Browse the repository at this point in the history
- Adxl345b target won't compile on MacOS
- More hardware tests need to have their build config
  adjusted to the new cmake style
- unit tests are completely converted
- also adds new run configurations for ide
  • Loading branch information
glencoe committed Oct 29, 2024
1 parent bef97d0 commit 9969e45
Show file tree
Hide file tree
Showing 29 changed files with 206 additions and 468 deletions.
7 changes: 7 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/runConfigurations/Unit_Tests.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/runConfigurations/all.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 7 additions & 39 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cmake_minimum_required(VERSION 3.20...3.24)

include(cmake/set_build_target.cmake)


Expand All @@ -13,20 +14,6 @@ add_runtime_c()

set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

if(MVC)
add_compile_options(/W4)
else()
add_compile_options(
-Wall
-Wextra
-Wdouble-promotion # warn when float gets promoted to double as this can be quite expensive
-Wnull-dereference
-Wimplicit-fallthrough # warn about fall through in switch case
)

endif ()

# enable debug prints
if (DEBUG_OUTPUT)
add_definitions(-DDEBUG_MODE)
endif ()
Expand All @@ -38,40 +25,21 @@ if(NOT BUILDING_FOR_ELASTIC_NODE)
endif ()

project(enV5 C CXX ASM)
pico_sdk_init()

# enable test execution via CMake
include(CTest)

# include unit-test framework
include(cmake/custom_targets.cmake)
include(cmake/pico_targets.cmake)

include(CTest)

pico_sdk_init()

add_freertos()

include(cmake/pico_targets.cmake)
add_subdirectory(src)
add_subdirectory(test/unit)
add_subdirectory(test/hardware/Helper)
add_subdirectory(test/hardware/Sensors)
add_freertos()

if(BUILDING_FOR_ELASTIC_NODE AND NOT TARGET tinyusb_device)
message(WARNING "not building project, because tinyusb not initialized")
else()
endif ()
# if (TARGET tinyusb_device)
# # include required libraries
# add_basic_functionality()
# add_freertos_kernel()
#
# add_rp2040_hal()
# add_sensor_libraries()
#
# # include hardware tests
# add_subdirectory(test/hardware)
#
# add_dependency_graph()
# elseif (PICO_ON_DEVICE)
# message(WARNING "Not building Project, because TinyUSB submodule is not initialized in the SDK!")
# endif ()
#endif ()

9 changes: 5 additions & 4 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"generator": "Ninja",
"binaryDir": "build/host/",
"cacheVariables": {
"ELASTIC_AI_TARGET": "HOST"
"ELASTIC_AI_TARGET": "HOST",
"CMAKE_BUILD_TYPE": "Release"
}
},
{
Expand All @@ -22,7 +23,8 @@
"generator": "Ninja",
"binaryDir": "build/env5_rev2",
"cacheVariables": {
"ELASTIC_AI_TARGET": "ENV5_REV2"
"ELASTIC_AI_TARGET": "ENV5_REV2",
"CMAKE_BUILD_TYPE": "Release"
}
}
],
Expand All @@ -34,8 +36,7 @@
},
{
"name": "unit_test",
"configurePreset": "host",
"jobs": 4,
"inherits": "host",
"targets": "all_unit_tests"
}
],
Expand Down
56 changes: 25 additions & 31 deletions cmake/custom_targets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,29 @@
################


function(__target_use_interfaces target)
foreach (arg ${ARGN})
target_link_libraries(${target} PRIVATE ${arg}__hdrs)

function(elastic_ai_target_link_hdrs target)
foreach (d ${ARGN})
target_link_libraries(${target} PRIVATE ${d}__hdrs)
endforeach ()
endfunction()

function(__target_add_default_includes target)
target_include_directories(${target} PRIVATE ${CMAKE_CURRENT_LIST_DIR})
target_include_directories(${target} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
function(elastic_ai_target_link_impl target)
foreach(d ${ARGN})
if (TARGET ${d}__impl)
target_link_libraries(${target} INTERFACE ${d}__impl)
endif ()
endforeach ()
endfunction()

function(__add_elastic_ai_implementation)
set(oneValueArgs NAME)
set(multiValueArgs SOURCES DEPS)
cmake_parse_arguments(PARSE_ARGV 0 arg
"${options}" "${oneValueArgs}" "${multiValueArgs}"
)
add_library(${arg_NAME})
target_sources(${arg_NAME} PRIVATE ${arg_SOURCES})
target_include_directories(${arg_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR})
function(elastic_ai_connect_libs target)
target_include_directories(${target}__hdrs INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)
if(TARGET ${target}__impl)
target_link_libraries(${target}__impl PRIVATE ${target}__hdrs)
target_include_directories(${target}__impl PRIVATE ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(${target} INTERFACE ${target}__impl)
endif ()
target_link_libraries(${target} INTERFACE ${target}__hdrs)
endfunction()

function(add_elastic_ai_lib)
Expand Down Expand Up @@ -71,27 +74,18 @@ function(add_elastic_ai_lib)
cmake_parse_arguments(PARSE_ARGV 0 arg
"${options}" "${oneValueArgs}" "${multiValueArgs}"
)
set(BUILD_IMPLEMENTATION (arg_SRCS AND ((NOT ${arg_HW_ONLY}) OR BUILDING_FOR_ELASTIC_NODE)))
set(BUILD_IMPLEMENTATION ((NOT ${arg_HW_ONLY}) OR BUILDING_FOR_ELASTIC_NODE))

foreach (file ${arg_SRCS})
if(NOT ((NOT IS_ABSOLUTE ${file} AND EXISTS ${CMAKE_CURRENT_LIST_DIR}/${file}) OR EXISTS ${file}))
message(FATAL_ERROR "${file} does not exist")
endif ()
endforeach ()
add_library(${arg_NAME}__hdrs INTERFACE)
if(${BUILD_IMPLEMENTATION})
__add_elastic_ai_implementation(NAME ${arg_NAME}__impl SOURCES ${arg_SRCS})
__target_use_interfaces(${arg_NAME}__impl ${arg_DEPS})
add_library(${arg_NAME}__impl ${arg_SRCS})
elastic_ai_target_link_hdrs(${arg_NAME}__impl ${arg_DEPS})
elastic_ai_target_link_impl(${arg_NAME}__impl ${arg_DEPS})
endif ()

add_library(${arg_NAME}__hdrs INTERFACE)
target_include_directories(${arg_NAME}__hdrs INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)

add_library(${arg_NAME} INTERFACE)

if(${BUILD_IMPLEMENTATION})
target_link_libraries(${arg_NAME} INTERFACE ${arg_NAME}__impl)
endif ()
target_link_libraries(${arg_NAME} INTERFACE ${arg_NAME}__hdrs)
elastic_ai_connect_libs(${arg_NAME})

endfunction()


Expand Down
4 changes: 2 additions & 2 deletions cmake/env5.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
message("Building for env5")
set(BUILDING_FOR_ELASTIC_NODE ON CACHE INTERNAL "we're building for elastic node")
set(PICO_BOARD none CACHE STRING "")
set(PICO_PLATFORM rp2040 CACHE STRING "")
set(PICO_BOARD none)
set(PICO_PLATFORM rp2040)

if(${ELASTIC_AI_TARGET} EQUAL ENV5_REV1)
set(REVISION "1" CACHE INTERNAL "")
Expand Down
4 changes: 2 additions & 2 deletions cmake/host.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
message("building for native host platform")
set(BUILDING_FOR_ELASTIC_NODE OFF CACHE INTERNAL "we're building for elastic node")
set(PICO_BOARD none CACHE STRING "")
set(PICO_PLATFORM host CACHE STRING "")
set(PICO_BOARD none)
set(PICO_PLATFORM host)
set(REVISION "1" CACHE INTERNAL "")
2 changes: 2 additions & 0 deletions cmake/pico_targets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ function(__add_pico_lib original)
endif ()
add_library(Pico::${original}__hdrs ALIAS pico_empty_lib)
add_library(Pico::${original} ALIAS pico_empty_lib)
add_library(Pico::${original}__impl ALIAS pico_empty_lib)
else ()
add_library(Pico::${original} ALIAS ${original})
add_library(Pico::${original}__hdrs ALIAS ${original}_headers)
add_library(Pico::${original}__impl ALIAS ${original})
endif ()
endfunction()

Expand Down
5 changes: 3 additions & 2 deletions cmake/third_party_deps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ function(add_cexception)
GIT_TAG v1.3.3
)
FetchContent_Populate(cexception)
add_library(CException ${cexception_SOURCE_DIR}/lib/CException.c)
add_library(CException__impl ${cexception_SOURCE_DIR}/lib/CException.c)
add_library(CException__hdrs INTERFACE)
target_include_directories(CException__hdrs INTERFACE ${cexception_SOURCE_DIR}/lib/)
target_link_libraries(CException PUBLIC CException__hdrs)
add_library(CException INTERFACE)
target_link_libraries(CException INTERFACE CException__hdrs CException__impl)
endfunction()

function(add_unity)
Expand Down
2 changes: 1 addition & 1 deletion src/fpga/stub/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ set(MIDDLEWARE_STUB_VERSION CACHE STRING "2" "use stub version 2, compatible wit

add_elastic_ai_lib(
NAME Stub
SOURCES stub.c
SRCS stub.c
DEPS Common Sleep Middleware
)
9 changes: 5 additions & 4 deletions src/hal/enV5HwConfiguration/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
add_elastic_ai_lib(
NAME
EnV5HwConfiguration
)

add_library(EnV5HwConfiguration INTERFACE)
add_library(EnV5HwConfiguration__hdrs INTERFACE)
target_link_libraries(EnV5HwConfiguration INTERFACE EnV5HwConfiguration__hdrs)

set(REVISION_FILEPATH ${REVISION} CACHE FILEPATH "Helper variable needed for casting" FORCE)

if(NOT DEFINED REVISION)
Expand Down
4 changes: 2 additions & 2 deletions src/hal/enV5HwController/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ add_elastic_ai_lib(
HW_ONLY
NAME EnV5HwController
SRCS EnV5HwController.c
DEPS Common EnV5HwConfiguration Gpio I2c Spi Uart Sleep Flash Adxl345b Pac193x Sht3x

DEPS Common EnV5HwConfiguration Gpio Sleep
)

2 changes: 1 addition & 1 deletion src/hal/enV5HwController/EnV5HwController.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

#include "Common.h"
#include "EnV5HwConfiguration.h"
#include "EnV5HwController.h"
#include "Gpio.h"
#include "Sleep.h"
#include "include/EnV5HwController.h"

void env5HwControllerInit() {
env5HwControllerLedsInit();
Expand Down
21 changes: 15 additions & 6 deletions src/hal/gpio/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# Provides a Facade for the pico-sdk GPIO library

add_elastic_ai_lib(
HW_ONLY
NAME Gpio
SRCS Gpio.c
DEPS Common Pico::hardware_gpio
)
block(SCOPE_FOR VARIABLES)
set(deps Common Pico::hardware_gpio)

add_library(Gpio__hdrs INTERFACE)
if(BUILDING_FOR_ELASTIC_NODE)
add_library(Gpio__impl Gpio.c)
elastic_ai_target_link_hdrs(Gpio__impl ${deps})
elastic_ai_target_link_impl(Gpio__impl ${deps})
endif ()
add_library(Gpio INTERFACE)

elastic_ai_connect_libs(Gpio)


endblock()
2 changes: 1 addition & 1 deletion src/hal/sleep/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
add_elastic_ai_lib(
HW_ONLY
NAME Sleep
SRCS ${CMAKE_CURRENT_LIST_DIR}/Sleep.c
SRCS Sleep.c
DEPS Common Pico::pico_time
)
2 changes: 1 addition & 1 deletion src/hal/uart/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ add_elastic_ai_lib(
HW_ONLY
NAME Uart
SRCS Uart.c
DEPS Common Gpio Pico::hardware_uart Pico::hardware_irq
DEPS Common Gpio Pico::hardware_uart Pico::hardware_irq Pico::pico_runtime
)
6 changes: 4 additions & 2 deletions src/network/atCommands/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
add_elastic_ai_lib(
NAME AtCommands)
add_library(AtCommands INTERFACE)
add_library(AtCommands__hdrs INTERFACE)
target_include_directories(AtCommands__hdrs INTERFACE include)
target_link_libraries(AtCommands INTERFACE AtCommands__hdrs)
2 changes: 1 addition & 1 deletion src/network/broker/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ add_elastic_ai_lib(

target_include_directories(
MqttBroker__impl
INTERFACE
PRIVATE
$<TARGET_PROPERTY:topicMatcher,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:protocol,INTERFACE_INCLUDE_DIRECTORIES>
)
Expand Down
2 changes: 1 addition & 1 deletion src/sensor/bmi323/Bmi323.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

#include "CException.h"

#include "Bmi323.h"
#include "Common.h"
#include "Sleep.h"
#include "Spi.h"
#include "SpiTypedefs.h"
#include "include/Bmi323.h"

#include "bmi3.h"
#include "bmi323.h"
Expand Down
14 changes: 11 additions & 3 deletions src/sensor/bmi323/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,29 @@ FetchContent_Declare(
)
FetchContent_Populate(bmi323)

add_library(__external_bmi323_bosch_lib
add_library(__external_bmi323_bosch_lib__impl
"${bmi323_SOURCE_DIR}/bmi3.c"
"${bmi323_SOURCE_DIR}/bmi323.c")

target_include_directories(__external_bmi323_bosch_lib PUBLIC "${bmi323_SOURCE_DIR}")
add_library(__external_bmi323_bosch_lib__hdrs INTERFACE)
target_include_directories(__external_bmi323_bosch_lib__hdrs INTERFACE "${bmi323_SOURCE_DIR}")
target_link_libraries(__external_bmi323_bosch_lib__impl PRIVATE __external_bmi323_bosch_lib__hdrs)
add_library(__external_bmi323_bosch_lib INTERFACE)
target_link_libraries(__external_bmi323_bosch_lib INTERFACE __external_bmi323_bosch_lib__hdrs __external_bmi323_bosch_lib__impl)

add_elastic_ai_lib(
HW_ONLY
NAME Bmi323
SRCS Bmi323.c
DEPS
__external_bmi323_bosch_lib
Common
Pico::pico_stdlib
CException
Sleep
Gpio
Spi
)
)

target_link_libraries(Bmi323__hdrs INTERFACE __external_bmi323_bosch_lib__hdrs)

Loading

0 comments on commit 9969e45

Please sign in to comment.