-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
162 lines (127 loc) · 4.6 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
cmake_minimum_required(VERSION 3.21)
project(target-cortexm LANGUAGES C CXX)
# store defaults in cache so they can be overriden on the command line
set(CMAKE_C_STANDARD 11 CACHE STRING "Default C version")
set(CMAKE_CXX_STANDARD 17 CACHE STRING "Default C++ version")
message(STATUS "Standards: C${CMAKE_C_STANDARD} C++${CMAKE_CXX_STANDARD}")
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
option(RTOS "Enable RTOS support" OFF)
message(STATUS "Toolchain file: ${CMAKE_TOOLCHAIN_FILE}")
# clang-tidy config needs to include Arm cross compiler header dirs
cmake_path(GET TOOLCHAIN PARENT_PATH TOOLCHAIN_BASE)
cmake_path(SET TOOLCHAIN_INCLUDE ${TOOLCHAIN_BASE}/arm-none-eabi/include)
FILE(GLOB TOOLCHAIN_CXX_VER RELATIVE ${TOOLCHAIN_INCLUDE}/c++ ${TOOLCHAIN_INCLUDE}/c++/*)
message(STATUS "Toolchain include folder: ${TOOLCHAIN_INCLUDE}")
set(CLANG_TIDY_OPTIONS
--checks=-*,readability-*
--extra-arg-before=-I${TOOLCHAIN_INCLUDE}/c++/${TOOLCHAIN_CXX_VER}/arm-none-eabi
--extra-arg-before=-I${TOOLCHAIN_INCLUDE}/c++/${TOOLCHAIN_CXX_VER}
--extra-arg-before=-I${TOOLCHAIN_INCLUDE}
CACHE STRING "clang-tidy options"
)
# uncomment this line to include clang_tidy as part of
# the compilation phase
# set(CMAKE_CXX_CLANG_TIDY clang-tidy ${CLANG_TIDY_OPTIONS})
add_compile_options(
-Wall
-Wextra
-Wconversion
-Wsign-conversion
# -save-temps
$<IF:$<STREQUAL:${CMAKE_CXX_STANDARD},20>,-Wno-deprecated,>
$<$<CONFIG:DEBUG>:-g3>
$<$<CONFIG:DEBUG>:-Og>
$<$<CONFIG:RELEASE>:-O3>
$<IF:$<BOOL:${EXCEPTIONS}>,-fexceptions,-fno-exceptions>
# $<IF:$<AND:$<BOOL:${RTOS}>,$<COMPILE_LANGUAGE:CXX>>,-fexceptions,>
)
add_compile_definitions(
$<$<CONFIG:DEBUG>:DEBUG>
$<$<CONFIG:DEBUG>:TRACE_ENABLED>
$<$<CONFIG:RELEASE>:NDEBUG> # GCC/G++ disables asserts
$<$<BOOL:${RTOS}>:RTOS>
)
# Standard configuration - change with care
FILE (GLOB USER_SRC ${CMAKE_SOURCE_DIR}/src/*.c ${CMAKE_SOURCE_DIR}/src/*.cpp)
add_executable(Application ${USER_SRC})
set(LINKER_SCRIPTS
${CMAKE_SOURCE_DIR}/ldscripts/mem.ld
${CMAKE_SOURCE_DIR}/ldscripts/sections.ld
)
set_target_properties(Application PROPERTIES
SUFFIX .elf
LINK_DEPENDS "${LINKER_SCRIPTS}"
)
target_link_options(Application PRIVATE
-T${CMAKE_SOURCE_DIR}/ldscripts/mem.ld
-T${CMAKE_SOURCE_DIR}/ldscripts/sections.ld
LINKER:--print-memory-usage
LINKER:--sort-section=name
LINKER:--cref
LINKER:-Map,${CMAKE_CURRENT_BINARY_DIR}/Application.map
)
target_include_directories(Application PRIVATE
src
include
)
if (IS_DIRECTORY ${CMAKE_SOURCE_DIR}/system)
add_subdirectory(${CMAKE_SOURCE_DIR}/system)
target_link_libraries(Application PRIVATE system)
endif()
if (RTOS AND IS_DIRECTORY ${CMAKE_SOURCE_DIR}/middleware)
message(STATUS "Building RTOS middleware")
add_subdirectory(${CMAKE_SOURCE_DIR}/middleware)
target_link_libraries(Application PRIVATE middleware)
endif()
if (EXISTS ${CMAKE_SOURCE_DIR}/src/main.c AND IS_DIRECTORY ${CMAKE_SOURCE_DIR}/drivers-c)
add_subdirectory(${CMAKE_SOURCE_DIR}/drivers-c)
target_link_libraries(Application PRIVATE drivers-c)
endif()
if (EXISTS ${CMAKE_SOURCE_DIR}/src/main.cpp AND IS_DIRECTORY ${CMAKE_SOURCE_DIR}/drivers-cpp)
add_subdirectory(${CMAKE_SOURCE_DIR}/drivers-cpp)
target_link_libraries(Application PRIVATE drivers-cpp)
endif()
# Post build processing
if (EXISTS "${TOOLCHAIN_SIZE}")
add_custom_command(
TARGET Application
POST_BUILD
COMMAND ${TOOLCHAIN_SIZE} --format=berkeley $<TARGET_FILE:Application>
>${CMAKE_CURRENT_BINARY_DIR}/$<TARGET_NAME:Application>.bsz
)
add_custom_command(
TARGET Application
POST_BUILD
COMMAND ${TOOLCHAIN_SIZE} --format=sysv -x $<TARGET_FILE:Application>
>${CMAKE_CURRENT_BINARY_DIR}/$<TARGET_NAME:Application>.ssz
)
else()
message(STATUS "'size' not found: cannot generate .[bs]sz files")
endif()
if (EXISTS ${CMAKE_OBJCOPY})
add_custom_command(
TARGET Application
POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:Application>
${CMAKE_CURRENT_BINARY_DIR}/$<TARGET_NAME:Application>.hex
)
else()
message(STATUS "'objcopy' not found: cannot generate .hex file")
endif()
# static analysis targets
find_program(CLANG_TIDY clang-tidy)
if (CLANG_TIDY)
add_custom_target(
clang-tidy clang-tidy -p ${CMAKE_BINARY_DIR} ${CLANG_TIDY_OPTIONS} ${USER_SRC}
DEPENDS ${USER_SRC}
)
endif()
# optional testing
if (EXISTS ${CMAKE_SOURCE_DIR}/tests/CMakeLists.txt)
enable_testing()
add_subdirectory(tests)
endif()