-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathensure_core_deps.cmake
170 lines (154 loc) · 6.4 KB
/
ensure_core_deps.cmake
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
cmake_minimum_required(VERSION 3.21)
include(FetchContent)
function(get_core_version OUTVAR)
file(READ ${PLATFORMTXT_PATH} PLATFORMTXT)
string(REGEX MATCH "version=.+\n" LINE "${PLATFORMTXT}")
string(SUBSTRING "${LINE}" 8 -1 INVAR)
set(${OUTVAR} ${INVAR} PARENT_SCOPE)
endfunction()
function(get_host OUTVAR)
cmake_host_system_information(
RESULT HOSTINFO
QUERY OS_NAME OS_PLATFORM
)
list(GET HOSTINFO 0 HOST_OS)
list(GET HOSTINFO 1 HOST_ARCH)
string(TOUPPER ${HOST_ARCH} HOST_ARCH)
set(${OUTVAR} "all" PARENT_SCOPE) # fallback value
if (${HOST_OS} STREQUAL "Linux")
if (${HOST_ARCH} MATCHES "^(AMD64|X86_64|x64)$")
set(${OUTVAR} "x86_64-pc-linux-gnu" PARENT_SCOPE)
elseif (${HOST_ARCH} MATCHES "^(ARM64)$")
set(${OUTVAR} "aarch64-linux-gnu" PARENT_SCOPE)
elseif (${HOST_ARCH} MATCHES "^(ARM)$")
# TODO: check for FPU with cmake_host_system_information(HAS_FPU)
set(${OUTVAR} "arm-linux-gnueabihf" PARENT_SCOPE)
elseif (${HOST_ARCH} MATCHES "^(I386|IA32|x86|i686)$")
set(${OUTVAR} "i686-pc-linux-gnu" PARENT_SCOPE)
endif()
elseif (${HOST_OS} STREQUAL "Windows")
if (${HOST_ARCH} MATCHES "^(AMD64|X86_64|x64|I386|IA32|x86|I686)$")
set(${OUTVAR} "i686-mingw32" PARENT_SCOPE)
endif()
elseif (${HOST_OS} STREQUAL "Darwin" OR ${HOST_OS} STREQUAL "macOS")
if (${HOST_ARCH} MATCHES "^(AMD64|X86_64|x64)$")
set(${OUTVAR} "x86_64-apple-darwin" PARENT_SCOPE)
endif()
endif()
endfunction()
function(get_target_url JSONARR OUT_URL OUT_SHA)
get_host(HOSTID)
string(JSON LEN LENGTH "${JSONARR}")
math(EXPR LEN "${LEN}-1") # iterate from 0 to len-1
foreach(I RANGE ${LEN})
string(JSON IHOST GET "${JSONARR}" ${I} "host")
if(${IHOST} STREQUAL "all" OR ${IHOST} STREQUAL ${HOSTID})
string(JSON IURL GET "${JSONARR}" ${I} "url")
string(JSON ISUM GET "${JSONARR}" ${I} "checksum")
set(${OUT_URL} ${IURL} PARENT_SCOPE)
string(SUBSTRING "${ISUM}" 8 -1 ISUM) # assume "SHA-256:", remove that prefix
set(${OUT_SHA} ${ISUM} PARENT_SCOPE)
return()
endif()
endforeach()
endfunction()
function(declare_deps CORE_VERSION)
# Prevent warnings in CMake>=3.24 regarding ExternalProject_Add()
# cf. https://cmake.org/cmake/help/latest/policy/CMP0135.html
if (POLICY CMP0135)
cmake_policy(SET CMP0135 OLD)
endif()
file(REAL_PATH "${DL_DIR}/package_stmicroelectronics_index.json" JSONFILE)
if (NOT EXISTS ${JSONFILE})
file(DOWNLOAD "${JSONCONFIG_URL}" ${JSONFILE})
endif()
file(READ ${JSONFILE} JSONCONFIG)
string(JSON PLATFORMS GET "${JSONCONFIG}" "packages" 0 "platforms")
string(JSON TOOLS GET "${JSONCONFIG}" "packages" 0 "tools")
string(JSON LEN_PLATFORM LENGTH "${PLATFORMS}")
math(EXPR LEN_PLATFORM "${LEN_PLATFORM}-1") # iterate from 0 to len-1
string(JSON LEN_TOOLS LENGTH "${TOOLS}")
math(EXPR LEN_TOOLS "${LEN_TOOLS}-1")
# 1. extract the dependencies of the core at the specified version
# (or the latest version older than the target version)
set(VERS "0.0.0")
set(DEPS "")
foreach(I_PKG RANGE ${LEN_PLATFORM})
string(JSON I_ARCH GET "${PLATFORMS}" ${I_PKG} "architecture")
if (NOT ${I_ARCH} STREQUAL "stm32")
continue()
endif()
string(JSON I_VER GET "${PLATFORMS}" ${I_PKG} "version")
if(${I_VER} VERSION_LESS_EQUAL ${CORE_VERSION} AND ${I_VER} VERSION_GREATER ${VERS})
set(VERS ${I_VER})
string(JSON DEPS GET "${PLATFORMS}" ${I_PKG} "toolsDependencies")
endif()
endforeach()
# 2. find the versions of the dependencies for that core
set(XPACK_VERSION "0.0.0")
set(CMSIS_VERSION "0.0.0")
# Note: we're ignoring the STM32Tools dep, because unlike Arduino IDE we don't need it (upload + misc scripts)
string(JSON LEN_DEPS LENGTH "${DEPS}")
math(EXPR LEN_DEPS "${LEN_DEPS}-1")
foreach(I_DEP RANGE ${LEN_DEPS})
string(JSON DEP_NAME GET "${DEPS}" ${I_DEP} "name")
if(${DEP_NAME} STREQUAL "xpack-arm-none-eabi-gcc")
string(JSON XPACK_VERSION GET "${DEPS}" ${I_DEP} "version")
elseif(${DEP_NAME} STREQUAL "CMSIS")
string(JSON CMSIS_VERSION GET "${DEPS}" ${I_DEP} "version")
endif()
endforeach()
# 3. grab the correct download URL + SHAsum for each dep
set(XPACK_URL "")
set(XPACK_SHA "")
set(CMSIS_URL "")
set(CMSIS_SHA "")
foreach(I_TOOL RANGE ${LEN_TOOLS})
string(JSON TOOL_NAME GET "${TOOLS}" ${I_TOOL} "name")
string(JSON TOOL_VERSION GET "${TOOLS}" ${I_TOOL} "version")
string(JSON TOOL_SUPPORT GET "${TOOLS}" ${I_TOOL} "systems")
if(${TOOL_NAME} STREQUAL "xpack-arm-none-eabi-gcc" AND ${TOOL_VERSION} VERSION_EQUAL ${XPACK_VERSION})
get_target_url("${TOOL_SUPPORT}" XPACK_URL XPACK_SHA)
elseif(${TOOL_NAME} STREQUAL "CMSIS" AND ${TOOL_VERSION} VERSION_EQUAL ${CMSIS_VERSION})
get_target_url("${TOOL_SUPPORT}" CMSIS_URL CMSIS_SHA)
endif()
endforeach()
# 4. declare the download (not executed yet, that will be up to the caller)
FetchContent_Declare(
xpack
SOURCE_DIR ${DL_DIR}/dist/xpack
PREFIX ${DL_DIR}
URL "${XPACK_URL}"
URL_HASH SHA256=${XPACK_SHA}
UPDATE_DISCONNECTED
)
FetchContent_Declare(
CMSIS5
SOURCE_DIR ${DL_DIR}/dist/CMSIS5
PREFIX ${DL_DIR}
URL "${CMSIS_URL}"
URL_HASH SHA256=${CMSIS_SHA}
UPDATE_DISCONNECTED
)
endfunction()
# defines a CMSIS5_PATH in the caller's scope
function(ensure_core_deps)
if(NOT EXISTS ${DL_DIR}/dist/CMSIS5 OR NOT EXISTS ${DL_DIR}/dist/xpack)
get_core_version(COREVER)
declare_deps(${COREVER})
message(STATUS "Downloading the CMSIS...")
FetchContent_MakeAvailable(CMSIS5)
message(STATUS "Downloading the CMSIS... Done.")
message(STATUS "Downloading the compiler toolchain...")
FetchContent_MakeAvailable(xpack)
message(STATUS "Downloading the compiler toolchain... Done.")
endif()
set(CMSIS5_PATH ${DL_DIR}/dist/CMSIS5 PARENT_SCOPE)
find_program(CMAKE_ASM_COMPILER arm-none-eabi-gcc PATHS ${DL_DIR}/dist/xpack/bin REQUIRED)
find_program(CMAKE_C_COMPILER arm-none-eabi-gcc PATHS ${DL_DIR}/dist/xpack/bin REQUIRED)
find_program(CMAKE_CXX_COMPILER arm-none-eabi-g++ PATHS ${DL_DIR}/dist/xpack/bin REQUIRED)
find_program(CMAKE_AR arm-none-eabi-ar PATHS ${DL_DIR}/dist/xpack/bin REQUIRED)
find_program(CMAKE_LD arm-none-eabi-ld PATHS ${DL_DIR}/dist/xpack/bin REQUIRED)
find_program(CMAKE_OBJCOPY arm-none-eabi-objcopy PATHS ${DL_DIR}/dist/xpack/bin REQUIRED)
find_program(CMAKE_SIZE arm-none-eabi-size PATHS ${DL_DIR}/dist/xpack/bin REQUIRED)
endfunction()