Skip to content

Commit 782375c

Browse files
committed
CMake: Enable improved armclang support in CMake 3.21
CMake versions 3.20 and below always add compiler and linker flags based on `CMAKE_SYSTEM_PROCESSOR`, for example `--cpu=Cortex-M33`. This sometimes overrides flags we set in tools/cmake/cores/*.cmake and results in link failure or unbootable binaries. To workaround this, we added more linker flags to "counter" what CMake automatically adds. From CMake 3.21 onwards, CMake by default does not add flags to armclang anymore, and it fully relies on projects to set all flags. In this case we do not need to set `CMAKE_SYSTEM_PROCESSOR` or use workarounds anymore. We still turn on workarounds when an older version of CMake (3.19 and 3.20) is used, but in the future we might require users to have at least CMake 3.21.
1 parent 4b6f243 commit 782375c

File tree

5 files changed

+99
-61
lines changed

5 files changed

+99
-61
lines changed

tools/cmake/app.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ include(mbed_set_post_build)
1616
# Load toolchain file
1717
if(NOT CMAKE_TOOLCHAIN_FILE OR MBED_TOOLCHAIN_FILE_USED)
1818
set(MBED_TOOLCHAIN_FILE_USED TRUE CACHE INTERNAL "")
19-
include(mbed_toolchain)
19+
# We want to bring CMP0123 we set in mbed_toolchain.cmake
20+
# to the whole Mbed OS.
21+
include(mbed_toolchain NO_POLICY_SCOPE)
2022
endif()
2123

2224
# Specify available build profiles and add options for the selected build profile

tools/cmake/cores/Cortex-M33-NS.cmake

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ elseif(${MBED_TOOLCHAIN} STREQUAL "ARM")
1212
"-mcpu=cortex-m33+nodsp"
1313
"-mfpu=none"
1414
)
15-
list(APPEND link_options
16-
# Necessary as the linker does not always detect
17-
# the architecture from the objectfiles correctly.
18-
# Also, the complete flag should be "--cpu=Cortex-M33.no_dsp.no_fp"
19-
# but this currently conflicts with CMake's compiler test until fixed
20-
"--cpu=Cortex-M33.no_fp"
21-
)
15+
if(deprecated_system_processor)
16+
# Normally `--cpu` is not needed, because `armlink` can infer
17+
# features from object files. But CMake versions below 3.21
18+
# automatically add `--cpu=${CMAKE_SYSTEM_PROCESSOR}` which is
19+
# incorrect, so as a workaround we need to add `no_fp`.
20+
list(APPEND link_options
21+
"--cpu=Cortex-M33.no_fp"
22+
)
23+
endif()
2224
endif()
2325

2426
function(mbed_set_cpu_core_definitions target)

tools/cmake/cores/Cortex-M33.cmake

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ elseif(${MBED_TOOLCHAIN} STREQUAL "ARM")
1212
"-mcpu=cortex-m33+nodsp"
1313
"-mfpu=none"
1414
)
15-
list(APPEND link_options
16-
# Necessary as the linker does not always detect
17-
# the architecture from the objectfiles correctly.
18-
# Also, the complete flag should be "--cpu=Cortex-M33.no_dsp.no_fp"
19-
# but this currently conflicts with CMake's compiler test until fixed
20-
"--cpu=Cortex-M33.no_fp"
21-
)
15+
if(deprecated_system_processor)
16+
# Normally `--cpu` is not needed, because `armlink` can infer
17+
# features from object files. But CMake versions below 3.21
18+
# automatically add `--cpu=${CMAKE_SYSTEM_PROCESSOR}` which is
19+
# incorrect, so as a workaround we need to add `no_fp`.
20+
list(APPEND link_options
21+
"--cpu=Cortex-M33.no_fp"
22+
)
23+
endif()
2224
endif()
2325

2426
function(mbed_set_cpu_core_definitions target)

tools/cmake/cores/Cortex-M4.cmake

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ elseif(${MBED_TOOLCHAIN} STREQUAL "ARM")
1212
"-mcpu=cortex-m4"
1313
"-mfpu=none"
1414
)
15-
#Necessary as the linker does not always detect
16-
#the architecture from the objectfiles correctly.
17-
list(APPEND link_options
18-
"--cpu=Cortex-M4.no_fp"
19-
)
15+
if(deprecated_system_processor)
16+
# Normally `--cpu` is not needed, because `armlink` can infer
17+
# features from object files. But CMake versions below 3.21
18+
# automatically add `--cpu=${CMAKE_SYSTEM_PROCESSOR}` which is
19+
# incorrect, so as a workaround we need to add `no_fp`.
20+
list(APPEND link_options
21+
"--cpu=Cortex-M4.no_fp"
22+
)
23+
endif()
2024
endif()
2125

2226
function(mbed_set_cpu_core_definitions target)

tools/cmake/mbed_toolchain.cmake

Lines changed: 69 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,75 @@ function(mbed_generate_options_for_linker target output_response_file_path)
2222
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/compile_time_defs.txt" CONTENT "${_compile_definitions}\n")
2323
set(${output_response_file_path} @${CMAKE_CURRENT_BINARY_DIR}/compile_time_defs.txt PARENT_SCOPE)
2424
endfunction()
25-
# Set the system processor depending on the CPU core type
26-
if (MBED_CPU_CORE STREQUAL Cortex-A9)
27-
set(CMAKE_SYSTEM_PROCESSOR cortex-a9)
28-
elseif (MBED_CPU_CORE STREQUAL Cortex-A5)
29-
set(CMAKE_SYSTEM_PROCESSOR cortex-a5)
30-
elseif (MBED_CPU_CORE STREQUAL Cortex-M0+)
31-
set(CMAKE_SYSTEM_PROCESSOR cortex-m0plus)
32-
elseif (MBED_CPU_CORE STREQUAL Cortex-M0)
33-
set(CMAKE_SYSTEM_PROCESSOR cortex-m0)
34-
elseif (MBED_CPU_CORE STREQUAL Cortex-M1)
35-
set(CMAKE_SYSTEM_PROCESSOR cortex-m1)
36-
elseif (MBED_CPU_CORE STREQUAL Cortex-M23-NS)
37-
set(CMAKE_SYSTEM_PROCESSOR cortex-m23)
38-
elseif (MBED_CPU_CORE STREQUAL Cortex-M23)
39-
set(CMAKE_SYSTEM_PROCESSOR cortex-m23)
40-
elseif (MBED_CPU_CORE STREQUAL Cortex-M3)
41-
set(CMAKE_SYSTEM_PROCESSOR cortex-m3)
42-
elseif (MBED_CPU_CORE STREQUAL Cortex-M33-NS)
43-
set(CMAKE_SYSTEM_PROCESSOR cortex-m33)
44-
elseif (MBED_CPU_CORE STREQUAL Cortex-M33)
45-
set(CMAKE_SYSTEM_PROCESSOR cortex-m33)
46-
elseif (MBED_CPU_CORE STREQUAL Cortex-M33F-NS)
47-
set(CMAKE_SYSTEM_PROCESSOR cortex-m33)
48-
elseif (MBED_CPU_CORE STREQUAL Cortex-M33F)
49-
set(CMAKE_SYSTEM_PROCESSOR cortex-m33)
50-
elseif (MBED_CPU_CORE STREQUAL Cortex-M33FE-NS)
51-
set(CMAKE_SYSTEM_PROCESSOR cortex-m33)
52-
elseif (MBED_CPU_CORE STREQUAL Cortex-M33FE)
53-
set(CMAKE_SYSTEM_PROCESSOR cortex-m33)
54-
elseif (MBED_CPU_CORE STREQUAL Cortex-M4)
55-
set(CMAKE_SYSTEM_PROCESSOR cortex-m4)
56-
elseif (MBED_CPU_CORE STREQUAL Cortex-M4F)
57-
set(CMAKE_SYSTEM_PROCESSOR cortex-m4)
58-
elseif (MBED_CPU_CORE STREQUAL Cortex-M55)
59-
set(CMAKE_SYSTEM_PROCESSOR cortex-m55)
60-
elseif (MBED_CPU_CORE STREQUAL Cortex-M7)
61-
set(CMAKE_SYSTEM_PROCESSOR cortex-m7)
62-
elseif (MBED_CPU_CORE STREQUAL Cortex-M7F)
63-
set(CMAKE_SYSTEM_PROCESSOR cortex-m7)
64-
elseif (MBED_CPU_CORE STREQUAL Cortex-M7FD)
65-
set(CMAKE_SYSTEM_PROCESSOR cortex-m7)
25+
26+
# Backward compatibility with older CMake which uses CMAKE_SYSTEM_PROCESSOR to
27+
# automatically add compile and link options for the Arm Compiler.
28+
# Note: From version 3.21, CMake by default (policy CMP0123 set to NEW) does not
29+
# use this macro anymore, and projects have full control over compile and link
30+
# options. This is because the old algorithm based on CMAKE_SYSTEM_PROCESSOR
31+
# is too restrictive and does not support things like Cortex-M33.no_dsp.no_fp.
32+
if(MBED_TOOLCHAIN STREQUAL "ARM")
33+
if(NOT POLICY CMP0123)
34+
# Old versions of CMake do not have CMP0123.
35+
# In the future, support for old versions of CMake will be
36+
# dropped from Mbed OS.
37+
set(deprecated_system_processor ON)
38+
else()
39+
cmake_policy(GET CMP0123 policy_CMP0123)
40+
if("${policy_CMP0123}" STREQUAL "")
41+
# CMP0123 is unset if an old `cmake_minimum_required()` is used with a
42+
# new CMake. Enable new CMP0123 to take advantage of the improvement
43+
# and dismiss deprecation warnings from CMake.
44+
cmake_policy(SET CMP0123 NEW)
45+
elseif("${policy_CMP0123}" STREQUAL "OLD")
46+
# Respect old CMP0123 forced by user application
47+
set(deprecated_system_processor ON)
48+
endif()
49+
endif()
50+
endif()
51+
52+
if(deprecated_system_processor)
53+
if (MBED_CPU_CORE STREQUAL Cortex-A9)
54+
set(CMAKE_SYSTEM_PROCESSOR cortex-a9)
55+
elseif (MBED_CPU_CORE STREQUAL Cortex-A5)
56+
set(CMAKE_SYSTEM_PROCESSOR cortex-a5)
57+
elseif (MBED_CPU_CORE STREQUAL Cortex-M0+)
58+
set(CMAKE_SYSTEM_PROCESSOR cortex-m0plus)
59+
elseif (MBED_CPU_CORE STREQUAL Cortex-M0)
60+
set(CMAKE_SYSTEM_PROCESSOR cortex-m0)
61+
elseif (MBED_CPU_CORE STREQUAL Cortex-M1)
62+
set(CMAKE_SYSTEM_PROCESSOR cortex-m1)
63+
elseif (MBED_CPU_CORE STREQUAL Cortex-M23-NS)
64+
set(CMAKE_SYSTEM_PROCESSOR cortex-m23)
65+
elseif (MBED_CPU_CORE STREQUAL Cortex-M23)
66+
set(CMAKE_SYSTEM_PROCESSOR cortex-m23)
67+
elseif (MBED_CPU_CORE STREQUAL Cortex-M3)
68+
set(CMAKE_SYSTEM_PROCESSOR cortex-m3)
69+
elseif (MBED_CPU_CORE STREQUAL Cortex-M33-NS)
70+
set(CMAKE_SYSTEM_PROCESSOR cortex-m33)
71+
elseif (MBED_CPU_CORE STREQUAL Cortex-M33)
72+
set(CMAKE_SYSTEM_PROCESSOR cortex-m33)
73+
elseif (MBED_CPU_CORE STREQUAL Cortex-M33F-NS)
74+
set(CMAKE_SYSTEM_PROCESSOR cortex-m33)
75+
elseif (MBED_CPU_CORE STREQUAL Cortex-M33F)
76+
set(CMAKE_SYSTEM_PROCESSOR cortex-m33)
77+
elseif (MBED_CPU_CORE STREQUAL Cortex-M33FE-NS)
78+
set(CMAKE_SYSTEM_PROCESSOR cortex-m33)
79+
elseif (MBED_CPU_CORE STREQUAL Cortex-M33FE)
80+
set(CMAKE_SYSTEM_PROCESSOR cortex-m33)
81+
elseif (MBED_CPU_CORE STREQUAL Cortex-M4)
82+
set(CMAKE_SYSTEM_PROCESSOR cortex-m4)
83+
elseif (MBED_CPU_CORE STREQUAL Cortex-M4F)
84+
set(CMAKE_SYSTEM_PROCESSOR cortex-m4)
85+
elseif (MBED_CPU_CORE STREQUAL Cortex-M55)
86+
set(CMAKE_SYSTEM_PROCESSOR cortex-m55)
87+
elseif (MBED_CPU_CORE STREQUAL Cortex-M7)
88+
set(CMAKE_SYSTEM_PROCESSOR cortex-m7)
89+
elseif (MBED_CPU_CORE STREQUAL Cortex-M7F)
90+
set(CMAKE_SYSTEM_PROCESSOR cortex-m7)
91+
elseif (MBED_CPU_CORE STREQUAL Cortex-M7FD)
92+
set(CMAKE_SYSTEM_PROCESSOR cortex-m7)
93+
endif()
6694
endif()
6795

6896
# Compiler setup

0 commit comments

Comments
 (0)