-
Notifications
You must be signed in to change notification settings - Fork 2
/
CMakeLists.txt
174 lines (137 loc) · 4.87 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
162
163
164
165
166
167
168
169
170
171
172
173
174
cmake_minimum_required (VERSION 3.15.3)
# Path to toolchain file. This one has to be before 'project()' below
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/arm-none-eabi-gcc.cmake)
# Switch for FreeRTOS support, enable it for some Applications who needs it.
set(ENABLE_FREERTOS OFF CACHE BOOL "Enable FreeRTOS")
# Switch for use newlib-nano , enable it for some Applications who needs it.
set(USE_NEWLIBNANO OFF CACHE BOOL "Use newlib-nano")
# Set Project Name
project (stm_basic_cmake)
#Optimization Options
set (OPTIM Os CACHE STRING "Default Optimization is Os")
# Allow switching of target mcu
set(MCU_LIST STM32F1xx STM32F4xx STM32F0xx)
set(MCU STM32F1xx CACHE STRING "Default target is STM32F1xx")
set_property(CACHE MCU PROPERTY STRINGS ${MCU_LIST})
if(MCU STREQUAL STM32F1xx)
message(STATUS "-------------STM32F1xx-------------")
include(stm32f1xx.cmake)
elseif(MCU STREQUAL STM32F4xx)
message(STATUS "-------------STM32F4xx-------------")
include(stm32f4xx.cmake)
elseif(MCU STREQUAL STM32F0xx)
message(STATUS "-------------STM32F0xx-------------")
include(stm32f0xx.cmake)
else()
list(JOIN MCU_LIST "\r\n" strlist)
message(FATAL_ERROR "Please Select correct MCU from list: \n${strlist}")
endif()
# List of source files
set(SRC_FILES
src/main.c
)
# List of include files
set(INC_FILES
Inc/
BSP/
)
# List of defines
set (TARGET_DEF_LISTS
${TARGET_MCU_DEF}
)
# List of libraries
set(LIB_LIST
)
# compiler option
set(COMPILER_OPTION ${TARGET_MCU_COMPILER})
# Linker Selection
set(LINKER_SECTIONS
-Tmem.ld
-Tsections.ld
)
configure_file (
"${PROJECT_SOURCE_DIR}/system/ldscripts/mem.ld.in"
"${PROJECT_BINARY_DIR}/system/ldscripts/mem.ld"
)
configure_file (
"${PROJECT_SOURCE_DIR}/system/ldscripts/sections.ld.in"
"${PROJECT_BINARY_DIR}/system/ldscripts/sections.ld"
)
find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
# Update submodules as needed
option(GIT_SUBMODULE "Check submodules during build" ON)
if(GIT_SUBMODULE)
message(STATUS "Submodule update")
execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_SUBMOD_RESULT)
if(NOT GIT_SUBMOD_RESULT EQUAL "0")
message(FATAL_ERROR "git submodule update --init --recursive failed with ${GIT_SUBMOD_RESULT}, please checkout submodules")
endif()
endif()
endif()
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
# Optional: issue a message to be sure it uses the correct toolchain file.
message(STATUS "CMAKE_TOOLCHAIN_FILE is: ${CMAKE_TOOLCHAIN_FILE}")
# Add System core file
include(system/stm_system.cmake)
# Add FreeRTOS
if(ENABLE_FREERTOS)
message(STATUS "ٍEnable FreeRTOS")
add_library(freertos_config INTERFACE)
target_include_directories(freertos_config SYSTEM
INTERFACE
system
)
target_compile_definitions(freertos_config
INTERFACE
projCOVERAGE_TEST=0
FREERTOS_HEAPMEM=${FREERTOS_HEAPMEM}
)
target_compile_options(freertos_config INTERFACE ${COMPILER_OPTION})
# Select the native compile PORT
set(FREERTOS_HEAP "4" CACHE STRING "")
set(FREERTOS_MAINSTACK "512" CACHE STRING "FreeRTOS main task stack in Byte")
add_subdirectory(system/FreeRTOS)
list(APPEND LIB_LIST "freertos_kernel")
list(APPEND TARGET_DEF_LISTS USE_FREERTOS)
list(APPEND TARGET_DEF_LISTS FREERTOS_MAINSTACK=${FREERTOS_MAINSTACK})
endif(ENABLE_FREERTOS)
# use new lib nano
if(USE_NEWLIBNANO)
list(APPEND TARGET_MCU_LINKER --specs=nano.specs)
endif(USE_NEWLIBNANO)
# Build the executable based on the source files
set(EXECUTABLE ${PROJECT_NAME}.elf)
add_executable(${EXECUTABLE} ${SRC_FILES})
# include directories to use when compiling a given target.
target_include_directories(${EXECUTABLE} PUBLIC ${INC_FILES})
# Link the executable to the simple_cmake library.
target_link_libraries(${EXECUTABLE} ${LIB_LIST})
# List of compiler defines, prefix with -D compiler option
target_compile_definitions(${EXECUTABLE} PUBLIC ${TARGET_DEF_LISTS})
# Compiler options
target_compile_options(${EXECUTABLE} PUBLIC ${COMPILER_OPTION})
# Linker options
target_link_options(${EXECUTABLE} PRIVATE
${LINKER_SECTIONS}
-L${CMAKE_CURRENT_BINARY_DIR}/system/ldscripts
${TARGET_MCU_LINKER}
-Wl,-Map=${PROJECT_NAME}.map,--cref
-Wl,--gc-sections
)
# Print executable size as part of the post build process
add_custom_command(TARGET ${EXECUTABLE}
POST_BUILD
COMMAND ${CMAKE_SIZE_UTIL} ${EXECUTABLE}
)
# Create hex and bin files after the build
add_custom_command(TARGET ${EXECUTABLE}
POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -O binary ${EXECUTABLE} ${PROJECT_NAME}.bin
COMMAND ${CMAKE_OBJCOPY} -O ihex ${EXECUTABLE} ${PROJECT_NAME}.hex
)
#include Doxygen module
include (doxygen.cmake)