-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
123 lines (100 loc) · 5.64 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
# This file was automagically generated by mbed.org.
# If you would like to add your own targets, create a
# project.cmake file locally in your project directory.
CMAKE_MINIMUM_REQUIRED(VERSION 3.9)
# here starts the project -> Rename this
set(PROJECT_TITLE ProjectNameCHANGEME)
#Im linking to static library called MBED_OS
set(CMAKE_STATIC_LIBRARY_PREFIX "")
set(CMAKE_STATIC_LIBRARY_SUFFIX "")
set(CMAKE_FIND_LIBRARY_PREFIXES ${CMAKE_STATIC_LIBRARY_PREFIX})
set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
#Need a Global Reference to this project folder so mbed can find mbed_config.h
set(ENV{CMAKE_MAIN_PROJECT_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
#The mbed folder need not be located here. it can be moved...updated this variable
set(ENV{MBED_DIR} $ENV{CMAKE_MAIN_PROJECT_DIR}/mbed)
#Program Settings
SET(TRAP_ERRORS 1)
#Yes you can develop a static library and link her here.
set(LIBRARIES_TO_LINK
-lstdc++ -lsupc++ -lm -lc -lgcc -lnosys #These are your standard libraries.
MBED_OS #This is the Mbed Static Library.
)
#Add Compiler information
include($ENV{MBED_DIR}/cmake/stm32l432.cmake)
#Add CMakeLists for Libraries
include($ENV{MBED_DIR}/CMakeLists.txt)
PROJECT(${PROJECT_TITLE} C CXX ASM)
#Define Project source files to compile here (should include headers as well)
set(MY_SOURCES
src/main.cpp
)
#Define your include directories here
INCLUDE_DIRECTORIES(
./
src
)
ADD_EXECUTABLE(${PROJECT_TITLE}
${MY_SOURCES}
)
SET_TARGET_PROPERTIES(${PROJECT_TITLE} PROPERTIES ENABLE_EXPORTS 1)
# add syslibs dependencies to create the correct linker order
TARGET_LINK_LIBRARIES(${PROJECT_TITLE} ${LIBRARIES_TO_LINK})
#Print the linked libraries
function (ListToString result delim)
list(GET ARGV 2 temp)
math(EXPR N "${ARGC}-1")
foreach(IDX RANGE 3 ${N})
list(GET ARGV ${IDX} STR)
set(temp "${temp}${delim}${STR}")
endforeach()
set(${result} "${temp}" PARENT_SCOPE)
endfunction(ListToString)
ListToString(librariesToPrint ", " ${LIBRARIES_TO_LINK})
message("\nLibraries to Link: \n\t" ${librariesToPrint} "\n")
add_custom_command(TARGET ${PROJECT_TITLE} PRE_LINK
COMMAND "arm-none-eabi-cpp" -E -P -Wl,--gc-sections -Wl,--wrap,main -Wl,--wrap,_malloc_r -Wl,--wrap,_free_r -Wl,--wrap,_realloc_r -Wl,--wrap,_memalign_r -Wl,--wrap,_calloc_r -Wl,--wrap,exit -Wl,--wrap,atexit -Wl,-n -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -fmessage-length=0 -fno-exceptions -ffunction-sections -fdata-sections -funsigned-char -MMD -fomit-frame-pointer -Os -g -DMBED_TRAP_ERRORS_ENABLED=${TRAP_ERRORS} -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -DMBED_ROM_START=0x8000000 -DMBED_ROM_SIZE=0x40000 -DMBED_RAM_START=0x20000000 -DMBED_RAM_SIZE=0xc000 -DMBED_RAM1_START=0x10000000 -DMBED_RAM1_SIZE=0x4000 -DMBED_BOOT_STACK_SIZE=1024 $ENV{PROJECT_LINKER_SCRIPT_FILE} -o ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_TITLE}_pp.link_script.ld
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_TITLE}_pp.link_script.ld"
)
add_custom_command(TARGET ${PROJECT_TITLE} POST_BUILD
COMMAND ${ELF2BIN} -O ihex $<TARGET_FILE:${PROJECT_TITLE}> $<TARGET_FILE:${PROJECT_TITLE}>.hex
COMMAND ${CMAKE_COMMAND} -E echo "-- built: $<TARGET_FILE:${PROJECT_TITLE}>.hex"
COMMAND ${ELF2BIN} -O binary $<TARGET_FILE:${PROJECT_TITLE}> $<TARGET_FILE:${PROJECT_TITLE}>.bin
COMMAND ${CMAKE_COMMAND} -E echo "-- built: $<TARGET_FILE:${PROJECT_TITLE}>.bin"
)
add_custom_command(TARGET ${PROJECT_TITLE} POST_BUILD
COMMAND cp $<TARGET_FILE:${PROJECT_TITLE}> $<TARGET_FILE:${PROJECT_TITLE}>.elf
COMMAND ${CMAKE_COMMAND} -E echo "-- built: $<TARGET_FILE:${PROJECT_TITLE}>.elf"
)
##########################################################################
# mbed-cli specific targets
##########################################################################
# detect the build type and select the corresponding cli profile
SET(MBED_BUILD_PROFILE "")
STRING(TOLOWER ${CMAKE_BUILD_TYPE} LOWERCASE_CMAKE_BUILD_TYPE)
IF(LOWERCASE_CMAKE_BUILD_TYPE MATCHES debug)
SET(MBED_BUILD_PROFILE "mbed-os/tools/profiles/debug.json")
ELSEIF(LOWERCASE_CMAKE_BUILD_TYPE MATCHES relwithdebinfo)
SET(MBED_BUILD_PROFILE "mbed-os/tools/profiles/develop.json")
ELSEIF(LOWERCASE_CMAKE_BUILD_TYPE MATCHES release)
SET(MBED_BUILD_PROFILE "mbed-os/tools/profiles/release.json")
ELSEIF(LOWERCASE_CMAKE_BUILD_TYPE MATCHES minsizerel)
SET(MBED_BUILD_PROFILE "mbed-os/tools/profiles/release.json")
ELSE()
MESSAGE(WARNING "Build type '${CMAKE_BUILD_TYPE}' is unknown, using debug profile")
SET(MBED_BUILD_PROFILE "mbed-os/tools/profiles/debug.json")
ENDIF()
# optional custom target to build via mbed-cli
ADD_CUSTOM_TARGET(mbed-cli-build
COMMAND ${CMAKE_COMMAND} -E echo "mbed compile --build BUILD/${CMAKE_BUILD_TYPE} --profile ${MBED_BUILD_PROFILE}"
COMMAND mbed compile --build BUILD/${CMAKE_BUILD_TYPE} --profile ${MBED_BUILD_PROFILE}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
SOURCES ${SOURCE_FILES} ${SYS_SOURCE_FILES})
IF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/project.cmake)
INCLUDE(${CMAKE_CURRENT_SOURCE_DIR}/project.cmake)
ELSE()
MESSAGE(STATUS "Add a local project.cmake file to add your own targets.")
ENDIF()
# this will take care of binary directories generated by cmake/clion not to confuse the cli build
FILE(GENERATE OUTPUT "${CMAKE_BINARY_DIR}/.mbedignore" CONTENT "*")