-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCMakeLists.txt
107 lines (88 loc) · 3.78 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
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Author: Farzan Saleem <[email protected]>
#
cmake_minimum_required(VERSION 3.0)
# Project title and languages
project (STM32L011XX C CXX)
# Controller Definition
add_definitions(-DSTM32L011xx)
add_definitions(-DUSE_HAL_DRIVER)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR Arm)
set(CMAKE_BUILD_TYPE Debug)
# Toolchain path
set(TOOLCHAIN_PATH "$ENV{HOME}/opt/gcc-arm-none-eabi/bin")
# Specify C, C++ Compiler
set (CMAKE_ASM_COMPILER ${TOOLCHAIN_PATH}/arm-none-eabi-as)
set (CMAKE_C_COMPILER ${TOOLCHAIN_PATH}/arm-none-eabi-gcc)
set (CMAKE_CXX_COMPILER ${TOOLCHAIN_PATH}/arm-none-eabi-g++)
set (CMAKE_OBJCOPY ${TOOLCHAIN_PATH}/arm-none-eabi-objcopy)
set (CMAKE_OBJDUMP ${TOOLCHAIN_PATH}/arm-none-eabi-objdump)
set (CMAKE_SIZE ${TOOLCHAIN_PATH}/arm-none-eabi-size)
# Linker Script path
set(LINKER_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/ld.ld")
# Startup file
file (GLOB_RECURSE STARTUP "sys/startup_stm32l011xx.s")
set_property(SOURCE ${STARTUP} PROPERTY LANGUAGE C)
# Map File
set(MAP_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cmake-build-debug/${PROJECT_NAME}.map)
# Compiler and linker flags for both debug and release build
set(COMMON_FLAGS "-Og \
-mcpu=cortex-m0plus \
-mthumb \
-fdata-sections \
-ffunction-sections \
-Wall"
)
# Compiler options for Debug build type
if(CMAKE_BUILD_TYPE MATCHES Debug)
message("Build Type: Debug")
set(CMAKE_C_FLAGS_DEBUG "${COMMON_FLAGS} -std=gnu11 -g -gdwarf-3 -gstrict-dwarf" CACHE INTERNAL "c compiler flags")
set(CMAKE_CXX_FLAGS_DEBUG "${COMMON_FLAGS} -std=c++17 -g -gdwarf-3 -gstrict-dwarf" CACHE INTERNAL "cxx compiler flags")
endif(CMAKE_BUILD_TYPE MATCHES Debug)
# Compiler options for Release build type
if(CMAKE_BUILD_TYPE MATCHES Release)
message("Build Type: Release")
set(CMAKE_C_FLAGS_RELEASE "${COMMON_FLAGS} -std=gnu11" CACHE INTERNAL "c compiler flags")
set(CMAKE_CXX_FLAGS_RELEASE "${COMMON_FLAGS} -std=c++17" CACHE INTERNAL "cxx compiler flags")
endif(CMAKE_BUILD_TYPE MATCHES Release)
# Path for system headers
include_directories(SYSTEM sys/CMSIS/Device/ST/STM32L0xx/Include
sys/CMSIS/Include
sys/config
sys/STM32L0xx_HAL_Driver/Inc
sys/STM32L0xx_HAL_Driver/Inc/Legacy
)
# Path for system sources
file (GLOB_RECURSE CMSIS sys/config/*.c
sys/STM32L0xx_HAL_Driver/Src/*.c
)
# Path for user headers
include_directories(src
)
# Path for user sources
file (GLOB_RECURSE SOURCE src/*.c
src/*.cpp
)
add_executable(${PROJECT_NAME}.elf ${SOURCE} ${CMSIS} ${STARTUP})
set(CMAKE_EXE_LINKER_FLAGS "-mcpu=cortex-m0plus -mthumb -specs=nano.specs -specs=nosys.specs -T${LINKER_SCRIPT} -lc -lm -lnosys -Wl,-Map=${MAP_FILE},--cref -Wl,--gc-sections")
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
set(ELF_FILE ${PROJECT_NAME}.elf)
set(BIN_FILE ${PROJECT_NAME}.bin)
set(HEX_FILE ${PROJECT_NAME}.hex)
add_custom_command(
TARGET ${PROJECT_NAME}.elf
COMMENT "Building ${BIN_FILE}"
COMMAND arm-none-eabi-objcopy -O ihex ${ELF_FILE} ${HEX_FILE} # convert .elf to .hex
COMMAND arm-none-eabi-objcopy -O binary ${ELF_FILE} ${BIN_FILE} # convert .elf to .bin
COMMAND arm-none-eabi-size --format=berkeley ${ELF_FILE}
)