Skip to content

Commit 3314201

Browse files
W-M-Rxiaoxiang781216
authored andcommitted
arm64/cmake: support clang compile
enable CONFIG_ARM64_TOOLCHAIN_CLANG can use clang compiler Signed-off-by: wangmingrong1 <wangmingrong1@xiaomi.com>
1 parent 8676214 commit 3314201

File tree

4 files changed

+399
-201
lines changed

4 files changed

+399
-201
lines changed

arch/arm64/src/cmake/Toolchain.cmake

Lines changed: 5 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -25,52 +25,6 @@ set(CMAKE_SYSTEM_VERSION 1)
2525
set(CMAKE_C_COMPILER_FORCED TRUE)
2626
set(CMAKE_CXX_COMPILER_FORCED TRUE)
2727

28-
# Default toolchain
29-
set(TOOLCHAIN_PREFIX aarch64-none-elf)
30-
31-
set(CMAKE_LIBRARY_ARCHITECTURE ${TOOLCHAIN_PREFIX})
32-
set(CMAKE_C_COMPILER_TARGET ${TOOLCHAIN_PREFIX})
33-
set(CMAKE_CXX_COMPILER_TARGET ${TOOLCHAIN_PREFIX})
34-
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
35-
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
36-
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
37-
set(CMAKE_STRIP ${TOOLCHAIN_PREFIX}-strip --strl dunneeded)
38-
set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}-objcopy)
39-
set(CMAKE_OBJDUMP ${TOOLCHAIN_PREFIX}-objdump)
40-
set(CMAKE_LINKER ${TOOLCHAIN_PREFIX}-ld)
41-
set(CMAKE_LD ${TOOLCHAIN_PREFIX}-ld)
42-
set(CMAKE_AR ${TOOLCHAIN_PREFIX}-ar)
43-
set(CMAKE_NM ${TOOLCHAIN_PREFIX}-nm)
44-
set(CMAKE_RANLIB ${TOOLCHAIN_PREFIX}-gcc-ranlib)
45-
if(CONFIG_LTO_FULL)
46-
add_compile_options(-flto)
47-
if(CONFIG_ARM64_TOOLCHAIN_GNU_EABI)
48-
set(CMAKE_LD ${TOOLCHAIN_PREFIX}-gcc)
49-
set(CMAKE_AR ${TOOLCHAIN_PREFIX}-gcc-ar)
50-
set(CMAKE_NM ${TOOLCHAIN_PREFIX}-gcc-nm)
51-
add_compile_options(-fuse-linker-plugin)
52-
add_compile_options(-fno-builtin)
53-
endif()
54-
endif()
55-
56-
add_link_options(--entry=__start)
57-
# override the ARCHIVE command
58-
set(CMAKE_ARCHIVE_COMMAND "<CMAKE_AR> rcs <TARGET> <LINK_FLAGS> <OBJECTS>")
59-
set(CMAKE_RANLIB_COMMAND "<CMAKE_RANLIB> <TARGET>")
60-
set(CMAKE_C_ARCHIVE_CREATE ${CMAKE_ARCHIVE_COMMAND})
61-
set(CMAKE_CXX_ARCHIVE_CREATE ${CMAKE_ARCHIVE_COMMAND})
62-
set(CMAKE_ASM_ARCHIVE_CREATE ${CMAKE_ARCHIVE_COMMAND})
63-
64-
set(CMAKE_C_ARCHIVE_APPEND ${CMAKE_ARCHIVE_COMMAND})
65-
set(CMAKE_CXX_ARCHIVE_APPEND ${CMAKE_ARCHIVE_COMMAND})
66-
set(CMAKE_ASM_ARCHIVE_APPEND ${CMAKE_ARCHIVE_COMMAND})
67-
68-
set(CMAKE_C_ARCHIVE_FINISH ${CMAKE_RANLIB_COMMAND})
69-
set(CMAKE_CXX_ARCHIVE_FINISH ${CMAKE_RANLIB_COMMAND})
70-
set(CMAKE_ASM_ARCHIVE_FINISH ${CMAKE_RANLIB_COMMAND})
71-
72-
set(NO_LTO "-fno-lto")
73-
7428
if(CONFIG_ARCH_ARMV8A)
7529
add_compile_options(-march=armv8-a)
7630
elseif(CONFIG_ARCH_ARMV8R)
@@ -89,155 +43,10 @@ elseif(CONFIG_ARCH_CORTEX_R82)
8943
add_compile_options(-mcpu=cortex-r82)
9044
endif()
9145

92-
if(CONFIG_DEBUG_CUSTOMOPT)
93-
add_compile_options(${CONFIG_DEBUG_OPTLEVEL})
94-
elseif(CONFIG_DEBUG_FULLOPT)
95-
add_compile_options(-Os)
96-
endif()
97-
98-
if(NOT CONFIG_DEBUG_NOOPT)
99-
add_compile_options(-fno-strict-aliasing)
100-
endif()
101-
102-
if(CONFIG_FRAME_POINTER)
103-
add_compile_options(-fno-omit-frame-pointer -fno-optimize-sibling-calls)
104-
else()
105-
add_compile_options(-fomit-frame-pointer)
106-
endif()
107-
108-
if(CONFIG_STACK_CANARIES)
109-
add_compile_options(-fstack-protector-all)
110-
endif()
111-
112-
if(CONFIG_STACK_USAGE)
113-
add_compile_options(-fstack-usage)
114-
endif()
115-
116-
if(CONFIG_STACK_USAGE_WARNING)
117-
add_compile_options(-Wstack-usage=${CONFIG_STACK_USAGE_WARNING})
118-
endif()
119-
120-
if(CONFIG_MM_UBSAN_ALL)
121-
add_compile_options(${CONFIG_MM_UBSAN_OPTION})
122-
endif()
123-
124-
if(CONFIG_MM_UBSAN_TRAP_ON_ERROR)
125-
add_compile_options(-fsanitize-undefined-trap-on-error)
126-
endif()
127-
128-
if(CONFIG_MM_KASAN_INSTRUMENT_ALL)
129-
add_compile_options(-fsanitize=kernel-address)
130-
set(KASAN_PARAM "")
131-
list(APPEND KASAN_PARAM "asan-stack=0")
132-
list(APPEND KASAN_PARAM "asan-instrumentation-with-call-threshold=0")
133-
134-
if(CONFIG_MM_KASAN_GLOBAL)
135-
list(APPEND KASAN_PARAM "asan-globals=1")
136-
else()
137-
list(APPEND KASAN_PARAM "asan-globals=0")
138-
endif()
139-
140-
if(CONFIG_MM_KASAN_DISABLE_READS_CHECK)
141-
list(APPEND KASAN_PARAM "asan-instrument-reads=0")
142-
endif()
143-
144-
if(CONFIG_MM_KASAN_DISABLE_WRITES_CHECK)
145-
list(APPEND KASAN_PARAM "asan-instrument-writes=0")
146-
endif()
147-
148-
if(CONFIG_ARM_TOOLCHAIN_CLANG)
149-
foreach(param IN LISTS KASAN_PARAM)
150-
add_compile_options("-mllvm=${param}")
151-
endforeach()
152-
else()
153-
foreach(param IN LISTS KASAN_PARAM)
154-
add_compile_options("--param=${param}")
155-
endforeach()
156-
endif()
157-
158-
endif()
159-
160-
if(CONFIG_ARCH_INSTRUMENT_ALL)
161-
add_compile_options(-finstrument-functions)
162-
endif()
163-
164-
if(CONFIG_COVERAGE_ALL)
165-
if(CONFIG_ARCH_TOOLCHAIN_GCC)
166-
add_compile_options(-fprofile-arcs -ftest-coverage -fno-inline)
167-
elseif(CONFIG_ARCH_TOOLCHAIN_CLANG)
168-
add_compile_options(-fprofile-instr-generate -fcoverage-mapping)
169-
endif()
170-
endif()
171-
172-
if(CONFIG_PROFILE_ALL)
173-
add_compile_options(-pg)
174-
endif()
175-
176-
if(CONFIG_ARCH_FPU)
177-
add_compile_options(-D_LDBL_EQ_DBL)
178-
endif()
179-
180-
add_compile_options(
181-
-fno-common
182-
-Wall
183-
-Wshadow
184-
-Wundef
185-
-Wno-attributes
186-
-Wno-unknown-pragmas
187-
$<$<COMPILE_LANGUAGE:C>:-Werror>
188-
$<$<COMPILE_LANGUAGE:C>:-Wstrict-prototypes>)
189-
190-
if(NOT CONFIG_LIBCXXTOOLCHAIN)
191-
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-nostdinc++>)
192-
endif()
193-
194-
if(NOT CONFIG_ARCH_TOOLCHAIN_CLANG)
195-
add_compile_options(-Wno-psabi)
196-
endif()
197-
198-
if(CONFIG_CXX_STANDARD)
199-
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-std=${CONFIG_CXX_STANDARD}>)
200-
endif()
201-
202-
if(NOT CONFIG_CXX_EXCEPTION)
203-
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
204-
$<$<COMPILE_LANGUAGE:CXX>:-fcheck-new>)
205-
endif()
206-
207-
if(NOT CONFIG_CXX_RTTI)
208-
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>)
209-
endif()
210-
211-
add_link_options(-nostdlib)
212-
213-
if(CONFIG_DEBUG_OPT_UNUSED_SECTIONS)
214-
add_link_options(-Wl,--gc-sections)
215-
add_compile_options(-ffunction-sections -fdata-sections)
216-
endif()
217-
218-
if(CONFIG_DEBUG_LINK_MAP)
219-
add_link_options(-Wl,-cref,-Map=nuttx.map)
220-
endif()
221-
222-
if(CONFIG_DEBUG_SYMBOLS)
223-
add_compile_options(${CONFIG_DEBUG_SYMBOLS_LEVEL})
224-
endif()
225-
226-
if(NOT CONFIG_ARCH_USE_MMU)
227-
add_compile_options(-fno-builtin)
228-
endif()
229-
230-
if(CONFIG_ARCH_TOOLCHAIN_GNU AND NOT CONFIG_ARCH_TOOLCHAIN_CLANG)
231-
if(NOT GCCVER)
232-
execute_process(COMMAND ${CMAKE_C_COMPILER} --version
233-
OUTPUT_VARIABLE GCC_VERSION_OUTPUT)
234-
string(REGEX MATCH "([0-9]+)\\.[0-9]+" GCC_VERSION_REGEX
235-
"${GCC_VERSION_OUTPUT}")
236-
set(GCCVER ${CMAKE_MATCH_1})
237-
endif()
238-
if(GCCVER GREATER_EQUAL 12)
239-
add_link_options(-Wl,--print-memory-usage)
240-
add_link_options(-Wl,--no-warn-rwx-segments)
241-
endif()
46+
# include the toolchain specific cmake file
24247

48+
if(CONFIG_ARCH_TOOLCHAIN_CLANG)
49+
include(clang)
50+
elseif(CONFIG_ARCH_TOOLCHAIN_GCC) # gcc
51+
include(gcc)
24352
endif()

arch/arm64/src/cmake/clang.cmake

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# ##############################################################################
2+
# arch/arm64/src/cmake/clang.cmake
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
7+
# license agreements. See the NOTICE file distributed with this work for
8+
# additional information regarding copyright ownership. The ASF licenses this
9+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
10+
# use this file except in compliance with the License. You may obtain a copy of
11+
# the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations under
19+
# the License.
20+
#
21+
# ##############################################################################
22+
23+
set(CMAKE_ASM_COMPILER clang)
24+
set(CMAKE_C_COMPILER clang)
25+
set(CMAKE_CXX_COMPILER clang++)
26+
set(CMAKE_PREPROCESSOR clang -E -P -x c)
27+
set(CMAKE_STRIP llvm-strip --strip-unneeded)
28+
set(CMAKE_OBJCOPY llvm-objcopy)
29+
set(CMAKE_OBJDUMP llvm-objdump)
30+
set(CMAKE_LINKER ld.lld)
31+
set(CMAKE_LD ld.lld)
32+
set(CMAKE_AR llvm-ar)
33+
set(CMAKE_NM llvm-nm)
34+
set(CMAKE_RANLIB llvm-ranlib)
35+
36+
add_link_options(-Wl,--entry=__start)
37+
add_link_options(-nostdlib)
38+
39+
set(NO_LTO "-fno-lto")
40+
41+
add_compile_options(--target=aarch64-none-elf)
42+
43+
if(CONFIG_DEBUG_CUSTOMOPT)
44+
add_compile_options(${CONFIG_DEBUG_OPTLEVEL})
45+
elseif(CONFIG_DEBUG_FULLOPT)
46+
add_compile_options(-Os)
47+
endif()
48+
49+
if(NOT CONFIG_DEBUG_NOOPT)
50+
add_compile_options(-fno-strict-aliasing)
51+
endif()
52+
53+
if(CONFIG_FRAME_POINTER)
54+
add_compile_options(-fno-omit-frame-pointer -fno-optimize-sibling-calls)
55+
else()
56+
add_compile_options(-fomit-frame-pointer)
57+
endif()
58+
59+
if(CONFIG_STACK_CANARIES)
60+
add_compile_options(-fstack-protector-all)
61+
endif()
62+
63+
if(CONFIG_STACK_USAGE)
64+
add_compile_options(-fstack-usage)
65+
endif()
66+
67+
if(CONFIG_STACK_USAGE_WARNING)
68+
add_compile_options(-Wstack-usage=${CONFIG_STACK_USAGE_WARNING})
69+
endif()
70+
71+
if(CONFIG_MM_UBSAN_ALL)
72+
add_compile_options(${CONFIG_MM_UBSAN_OPTION})
73+
endif()
74+
75+
if(CONFIG_MM_UBSAN_TRAP_ON_ERROR)
76+
add_compile_options(-fsanitize-undefined-trap-on-error)
77+
endif()
78+
79+
if(CONFIG_MM_KASAN_INSTRUMENT_ALL)
80+
add_compile_options(-fsanitize=kernel-address)
81+
set(KASAN_PARAM "")
82+
list(APPEND KASAN_PARAM "asan-stack=0")
83+
list(APPEND KASAN_PARAM "asan-instrumentation-with-call-threshold=0")
84+
85+
if(CONFIG_MM_KASAN_GLOBAL)
86+
list(APPEND KASAN_PARAM "asan-globals=1")
87+
else()
88+
list(APPEND KASAN_PARAM "asan-globals=0")
89+
endif()
90+
91+
if(CONFIG_MM_KASAN_DISABLE_READS_CHECK)
92+
list(APPEND KASAN_PARAM "asan-instrument-reads=0")
93+
endif()
94+
95+
if(CONFIG_MM_KASAN_DISABLE_WRITES_CHECK)
96+
list(APPEND KASAN_PARAM "asan-instrument-writes=0")
97+
endif()
98+
99+
foreach(param IN LISTS KASAN_PARAM)
100+
add_compile_options("-mllvm=${param}")
101+
endforeach()
102+
103+
endif()
104+
105+
if(CONFIG_ARCH_INSTRUMENT_ALL)
106+
add_compile_options(-finstrument-functions)
107+
endif()
108+
109+
if(CONFIG_COVERAGE_ALL)
110+
add_compile_options(-fprofile-instr-generate -fcoverage-mapping)
111+
endif()
112+
113+
if(CONFIG_PROFILE_ALL)
114+
add_compile_options(-pg)
115+
endif()
116+
117+
if(CONFIG_ARCH_FPU)
118+
add_compile_options(-D_LDBL_EQ_DBL)
119+
endif()
120+
121+
add_compile_options(
122+
-fno-common
123+
-Wall
124+
-Wshadow
125+
-Wundef
126+
-Wno-attributes
127+
-Wno-unknown-pragmas
128+
$<$<COMPILE_LANGUAGE:C>:-Werror>
129+
$<$<COMPILE_LANGUAGE:C>:-Wstrict-prototypes>)
130+
131+
if(NOT CONFIG_LIBCXXTOOLCHAIN)
132+
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-nostdinc++>)
133+
endif()
134+
135+
if(NOT CONFIG_ARCH_TOOLCHAIN_CLANG)
136+
add_compile_options(-Wno-psabi)
137+
endif()
138+
139+
if(CONFIG_CXX_STANDARD)
140+
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-std=${CONFIG_CXX_STANDARD}>)
141+
endif()
142+
143+
if(NOT CONFIG_CXX_EXCEPTION)
144+
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
145+
$<$<COMPILE_LANGUAGE:CXX>:-fcheck-new>)
146+
endif()
147+
148+
if(NOT CONFIG_CXX_RTTI)
149+
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>)
150+
endif()
151+
152+
add_link_options(-nostdlib)
153+
154+
if(CONFIG_DEBUG_OPT_UNUSED_SECTIONS)
155+
add_link_options(-Wl,--gc-sections)
156+
add_compile_options(-ffunction-sections -fdata-sections)
157+
endif()
158+
159+
if(CONFIG_DEBUG_LINK_MAP)
160+
add_link_options(-Wl,--cref -Wl,-Map=nuttx.map)
161+
endif()
162+
163+
if(CONFIG_DEBUG_SYMBOLS)
164+
add_compile_options(${CONFIG_DEBUG_SYMBOLS_LEVEL})
165+
endif()
166+
167+
if(NOT CONFIG_ARCH_USE_MMU)
168+
add_compile_options(-fno-builtin)
169+
endif()
170+
171+
add_link_options(-no-pie)

0 commit comments

Comments
 (0)