-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
127 lines (112 loc) · 4.03 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
cmake_minimum_required(VERSION 3.14)
# add modules to module path so we can include() them easier
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
project(main LANGUAGES C CXX ASM)
set(EXECUTABLE main)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
set(CPU_PARAMETERS
-mcpu=cortex-m4
-mthumb
-mfpu=fpv4-sp-d16
-mfloat-abi=softfp)
set(PROJECT_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR})
set(PROJECT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(STARTUP_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/src/startup_stm32f407xx.s)
set(MCU_LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/flash/STM32F407VGTX_FLASH.ld)
set(CMSIS_PATH ${CMAKE_CURRENT_SOURCE_DIR}/Drivers/CMSIS)
set(HAL_PATH ${CMAKE_CURRENT_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver)
add_subdirectory(src/modules)
# Production build
if(CMAKE_BUILD_TYPE STREQUAL release)
# -------- Collate Code Sources --------
file(GLOB_RECURSE PROJECT_SOURCES "${PROJECT_DIR}/*.c")
# -------- Include HAL Sources --------
set(HAL_SOURCES
${HAL_PATH}/Src/stm32f4xx_hal_hcd.c
${HAL_PATH}/Src/stm32f4xx_ll_usb.c
${HAL_PATH}/Src/stm32f4xx_hal_rcc.c
${HAL_PATH}/Src/stm32f4xx_hal_rcc_ex.c
${HAL_PATH}/Src/stm32f4xx_hal_flash.c
${HAL_PATH}/Src/stm32f4xx_hal_flash_ex.c
${HAL_PATH}/Src/stm32f4xx_hal_flash_ramfunc.c
${HAL_PATH}/Src/stm32f4xx_hal_gpio.c
${HAL_PATH}/Src/stm32f4xx_hal_dma_ex.c
${HAL_PATH}/Src/stm32f4xx_hal_dma.c
${HAL_PATH}/Src/stm32f4xx_hal_pwr.c
${HAL_PATH}/Src/stm32f4xx_hal_pwr_ex.c
${HAL_PATH}/Src/stm32f4xx_hal_cortex.c
${HAL_PATH}/Src/stm32f4xx_hal.c
${HAL_PATH}/Src/stm32f4xx_hal_exti.c
${HAL_PATH}/Src/stm32f4xx_hal_i2c.c
${HAL_PATH}/Src/stm32f4xx_hal_i2c_ex.c
${HAL_PATH}/Src/stm32f4xx_hal_i2s.c
${HAL_PATH}/Src/stm32f4xx_hal_i2s_ex.c
${HAL_PATH}/Src/stm32f4xx_hal_spi.c
${HAL_PATH}/Src/stm32f4xx_hal_tim.c
${HAL_PATH}/Src/stm32f4xx_hal_tim_ex.c)
# -------- Create executable --------
add_executable(${EXECUTABLE}
${PROJECT_SOURCES}
${HAL_SOURCES}
${STARTUP_SCRIPT})
# -------- Include dependencies --------
target_include_directories(${EXECUTABLE} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${HAL_PATH}/Inc
${HAL_PATH}/Inc/Legacy
${CMSIS_PATH}/Device/ST/STM32F4xx/Include
${CMSIS_PATH}/Include)
# -------- Compilation configuration --------
target_compile_options(${EXECUTABLE} PRIVATE
${CPU_PARAMETERS}
-DUSE_HAL_DRIVER
-DSTM32F407xx
-std=gnu11
-Wall
-Wextra
-Wpedantic
-Wshadow
-Wdouble-promotion
-Wformat=2 -Wformat-truncation
-Wmissing-include-dirs
-Wsign-compare
-Wundef
# -Wcast-align
# -Wconversion
-fno-common
-fsingle-precision-constant
-fomit-frame-pointer
-ffunction-sections
-fdata-sections
-Wno-unused-parameter
--specs=nano.specs
$<$<CONFIG:Debug>:-Og -g3 -ggdb>
$<$<CONFIG:Release>:-Og -g0>)
target_link_options(${EXECUTABLE} PRIVATE
-T${MCU_LINKER_SCRIPT}
${CPU_PARAMETERS}
-Wl,-Map=${EXECUTABLE}.map
--specs=nano.specs
-Wl,--gc-sections
-lc)
# -------- Post-build: create binary --------
add_custom_command(TARGET ${EXECUTABLE} POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${EXECUTABLE}> ${EXECUTABLE}.bin)
# Test build
else()
# -------- GoogleTest --------
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.12.1
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
# set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()
add_subdirectory(test)
endif()