Skip to content

Commit 67b7021

Browse files
committed
feat: CMake downloads the CMSIS, GCC
These are mandatory dependencies, writing CMake modules to fetch them automatically makes for an easier set-up.
1 parent 52209c6 commit 67b7021

File tree

5 files changed

+136
-19
lines changed

5 files changed

+136
-19
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ __pycache__/
3232
# VisualStudioCode
3333
.vscode/*
3434
*.code-workspace
35+
36+
# CMake-related
37+
/CMSIS_5/
38+
/xpack-arm-none-eabi-gcc/

CMakeLists.txt

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
cmake_minimum_required(VERSION 3.13)
22

3+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)
34

4-
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
5-
6-
find_program(CMAKE_SIZE "arm-none-eabi-size")
7-
find_program(PYTHON3 "python3")
8-
find_program(SFDP "sfdp") # graphviz layout engine -- you may change the engine, use dot or whatever
5+
include(ensure_CMSIS)
96

107
project("Arduino_Core_STM32" CXX C ASM)
118

@@ -41,9 +38,11 @@ set(BUILD_SOURCE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/sketch" CACHE STRING "")
4138
set(BUILD_CORE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cores/arduino")
4239
set(BUILD_SYSTEM_PATH "${CMAKE_CURRENT_SOURCE_DIR}/system")
4340
set(BUILD_LIB_PATH "${CMAKE_CURRENT_SOURCE_DIR}/libraries")
44-
set(BUILD_CMSIS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../CMSIS_5/CMSIS") # https://github.com/ARM-software/CMSIS_5.git
4541
set(BUILD_VARIANT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/variants/${BOARD_SERIE}/${BOARD_VARIANT}")
42+
set(CMSIS5_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMSIS_5" CACHE STRING "Path to CMSIS_5")
4643

44+
option(AUTODL_CMSIS OFF)
45+
ensure_CMSIS(${CMSIS5_PATH} ${AUTODL_CMSIS})
4746

4847
add_library(core_config INTERFACE)
4948

@@ -223,7 +222,7 @@ target_link_options(core_config INTERFACE
223222
LINKER:--script=${BUILD_SYSTEM_PATH}/ldscript.ld
224223
)
225224
target_link_directories(core_config INTERFACE
226-
"${BUILD_CMSIS_PATH}/CMSIS/DSP/Lib/GCC"
225+
"${CMSIS5_PATH}/CMSIS/DSP/Lib/GCC"
227226
)
228227
target_include_directories(core_config INTERFACE
229228
"${BUILD_SOURCE_PATH}"
@@ -242,9 +241,10 @@ target_include_directories(core_config INTERFACE
242241
"${BUILD_SYSTEM_PATH}/Middlewares/ST/STM32_USB_Device_Library/Core/Src"
243242
"${BUILD_SYSTEM_PATH}/Drivers/CMSIS/Device/ST/${BOARD_SERIE}/Include/"
244243
"${BUILD_SYSTEM_PATH}/Drivers/CMSIS/Device/ST/${BOARD_SERIE}/Source/Templates/gcc/"
245-
"${BUILD_CMSIS_PATH}/DSP/Include"
246-
"${BUILD_CMSIS_PATH}/DSP/PrivateInclude"
247-
"${BUILD_CMSIS_PATH}/Core/Include/"
244+
"${CMSIS5_PATH}/CMSIS/DSP/Include"
245+
"${CMSIS5_PATH}/CMSIS/DSP/PrivateInclude"
246+
"${CMSIS5_PATH}/CMSIS/Core/Include/"
247+
"${CMSIS5_PATH}/CMSIS"
248248
"${BUILD_VARIANT_PATH}"
249249

250250
"${BUILD_LIB_PATH}/SrcWrapper/src"
@@ -276,6 +276,3 @@ target_link_libraries(stm32_runtime INTERFACE
276276
c
277277
gcc
278278
)
279-
280-
281-
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/handle_sketch.cmake)

cmake/ensure_CMSIS.cmake

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
include(FetchContent)
3+
4+
function(ensure_CMSIS CMSIS5_PATH AUTODL_CMSIS)
5+
FetchContent_Declare(
6+
CMSIS_5
7+
SOURCE_DIR ${CMSIS5_PATH} # put the CMSIS along the other sources of the core
8+
URL https://github.com/stm32duino/ArduinoModule-CMSIS/releases/download/5.7.0/CMSIS-5.7.0.tar.bz2
9+
# see also https://github.com/ARM-software/CMSIS_5/releases/tag/5.7.0
10+
)
11+
12+
if (NOT EXISTS ${CMSIS5_PATH})
13+
if (${AUTODL_CMSIS})
14+
message(STATUS "Downloading CMSIS...")
15+
FetchContent_MakeAvailable(CMSIS_5)
16+
else()
17+
message(SEND_ERROR
18+
"
19+
CMSIS not found.
20+
Please pass the path to your CMSIS installation through CMSIS5_PATH
21+
or explicitly activate downloading with AUTODL_CMSIS.
22+
"
23+
)
24+
endif()
25+
endif()
26+
endfunction()

cmake/ensure_binutils.cmake

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
include(FetchContent)
3+
4+
function(ensure_binutils XPACK_PATH AUTODL_XPACK)
5+
# not "required" _yet_, if we don't find them we'll try to download them
6+
find_program(CMAKE_ASM_COMPILER arm-none-eabi-gcc PATHS ${XPACK_PATH}/bin) # not arm-none-eabi-as, because we want preprocessing
7+
find_program(CMAKE_C_COMPILER arm-none-eabi-gcc PATHS ${XPACK_PATH}/bin)
8+
find_program(CMAKE_CXX_COMPILER arm-none-eabi-g++ PATHS ${XPACK_PATH}/bin)
9+
find_program(CMAKE_AR arm-none-eabi-ar PATHS ${XPACK_PATH}/bin)
10+
find_program(CMAKE_LD arm-none-eabi-ld PATHS ${XPACK_PATH}/bin)
11+
find_program(CMAKE_OBJCOPY arm-none-eabi-objcopy PATHS ${XPACK_PATH}/bin)
12+
find_program(CMAKE_SIZE arm-none-eabi-size PATHS ${XPACK_PATH}/bin)
13+
14+
if (NOT EXISTS ${CMAKE_ASM_COMPILER} OR NOT EXISTS ${CMAKE_C_COMPILER} OR NOT EXISTS ${CMAKE_CXX_COMPILER} OR NOT EXISTS ${CMAKE_AR} OR NOT EXISTS ${CMAKE_LD} OR NOT EXISTS ${CMAKE_OBJCOPY})
15+
16+
cmake_host_system_information(
17+
RESULT HOSTINFO
18+
QUERY OS_NAME OS_PLATFORM
19+
)
20+
list(GET HOSTINFO 0 HOST_OS)
21+
list(GET HOSTINFO 1 HOST_ARCH)
22+
23+
unset(OSCODE)
24+
unset(ARCHIVE_EXT)
25+
if (${HOST_OS} STREQUAL "Linux")
26+
set(OSCODE "linux")
27+
set(ARCHIVE_EXT ".tar.gz")
28+
elseif (${HOST_OS} STREQUAL "Windows")
29+
set(OSCODE "win32")
30+
set(ARCHIVE_EXT ".zip")
31+
elseif (${HOST_OS} STREQUAL "Darwin ")
32+
set(OSCODE "darwin")
33+
set(ARCHIVE_EXT ".tar.gz")
34+
endif()
35+
36+
unset(CPUCODE)
37+
string(TOUPPER ${HOST_ARCH} HOST_ARCH)
38+
if (${HOST_ARCH} MATCHES "^(AMD64|X86_64|x64)$")
39+
set(CPUCODE "x64")
40+
elseif (${HOST_ARCH} MATCHES "^(ARM64)$")
41+
set(CPUCODE "arm64")
42+
elseif (${HOST_ARCH} MATCHES "^(ARM)$")
43+
set(CPUCODE "arm")
44+
elseif (${HOST_ARCH} MATCHES "^(I386|IA32|x86|i686)$")
45+
set(CPUCODE "ia32")
46+
endif()
47+
48+
if (DEFINED OSCODE AND DEFINED CPUCODE AND DEFINED ARCHIVE_EXT)
49+
if (${AUTODL_XPACK})
50+
FetchContent_Declare(
51+
xpack
52+
SOURCE_DIR ${XPACK_PATH}
53+
URL "https://github.com/xpack-dev-tools/arm-none-eabi-gcc-xpack/releases/download/v10.3.1-2.3/xpack-arm-none-eabi-gcc-10.3.1-2.3-${OSCODE}-${CPUCODE}${ARCHIVE_EXT}"
54+
)
55+
if (NOT EXISTS ${XPACK_PATH})
56+
message(STATUS "Downloading xpack...")
57+
FetchContent_MakeAvailable(xpack)
58+
endif()
59+
else() # supported platform, no autodl
60+
message(FATAL_ERROR
61+
"
62+
Could not find arm-none-eabi-gcc/g++/ar/ld/objcopy. These are required to build for stm32.
63+
Please install them and make them available in your $PATH, or let me make my private install using the AUTODL_XPACK option.
64+
"
65+
)
66+
endif()
67+
else() # unsupported platform
68+
message(FATAL_ERROR
69+
"
70+
Could not find arm-none-eabi-gcc/g++/ar/ld/objcopy. These are required to build for stm32.
71+
Please install them and make them available in your $PATH.
72+
"
73+
)
74+
endif()
75+
76+
77+
endif()
78+
79+
find_program(CMAKE_ASM_COMPILER arm-none-eabi-gcc PATHS ${XPACK_PATH}/bin REQUIRED)
80+
find_program(CMAKE_C_COMPILER arm-none-eabi-gcc PATHS ${XPACK_PATH}/bin REQUIRED)
81+
find_program(CMAKE_CXX_COMPILER arm-none-eabi-g++ PATHS ${XPACK_PATH}/bin REQUIRED)
82+
find_program(CMAKE_AR arm-none-eabi-ar PATHS ${XPACK_PATH}/bin REQUIRED)
83+
find_program(CMAKE_LD arm-none-eabi-ld PATHS ${XPACK_PATH}/bin REQUIRED)
84+
find_program(CMAKE_OBJCOPY arm-none-eabi-objcopy PATHS ${XPACK_PATH}/bin REQUIRED)
85+
find_program(CMAKE_SIZE arm-none-eabi-size PATHS ${XPACK_PATH}/bin REQUIRED)
86+
87+
endfunction()

cmake/toolchain.cmake

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
2+
find_program(PYTHON3 "python3")
3+
find_program(SFDP "sfdp") # graphviz layout engine -- you may change the engine, use dot or whatever
4+
5+
include("${CMAKE_CURRENT_LIST_DIR}/ensure_binutils.cmake")
6+
17
# Setting Linux is forcing th extension to be .o instead of .obj when building on WIndows.
28
# It is important because armlink is failing when files have .obj extensions (error with
39
# scatter file section not found)
410
SET(CMAKE_SYSTEM_NAME Linux)
511
SET(CMAKE_SYSTEM_PROCESSOR arm)
612

7-
find_program(CMAKE_ASM_COMPILER arm-none-eabi-gcc REQUIRED) # not arm-none-eabi-as, because we want preprocessing
8-
find_program(CMAKE_C_COMPILER arm-none-eabi-gcc REQUIRED)
9-
find_program(CMAKE_CXX_COMPILER arm-none-eabi-g++ REQUIRED)
10-
find_program(CMAKE_AR arm-none-eabi-ar REQUIRED)
11-
find_program(CMAKE_LD arm-none-eabi-ld REQUIRED)
12-
find_program(CMAKE_OBJCOPY arm-none-eabi-objcopy REQUIRED)
13+
set(XPACK_PATH "${CMAKE_CURRENT_LIST_DIR}/../xpack-arm-none-eabi-gcc")
14+
option(AUTODL_XPACK OFF)
15+
ensure_binutils(${XPACK_PATH} ${AUTODL_XPACK})
1316

1417
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) # don't try to link when testing the compiler, it won't work anyway
1518
set(BUILD_SHARED_LIBS false CACHE STRING "")

0 commit comments

Comments
 (0)