Skip to content

Commit f1372ac

Browse files
committed
cmake: compiler: add support for Andes GCC and Clang
Add support for the Andes GCC/Clang (andes/andes-clang) compiler. Both inherit the generic GCC/Clang flags with the following adjustments: Andes GCC: 1. Set RISC-V -march with the Andes suffix (_xandes). 2. Enable Andes-specific machine options (e.g. -mexecit, -mext-dsp). Andes Clang: 1. Apply the same -march suffix and machine options as Andes GCC. 2. Recognize the prefix 'riscv[32|64]-elf-clang' intead of 'clang'. 3. For Clang < 17, strip unsupported _zicsr/_zifencei from -march. Signed-off-by: Jimmy Zheng <jimmyzhe@andestech.com>
1 parent ca68c77 commit f1372ac

File tree

9 files changed

+97
-12
lines changed

9 files changed

+97
-12
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
include(${ZEPHYR_BASE}/cmake/compiler/clang/compiler_flags.cmake)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
find_program(CMAKE_C_COMPILER "${CROSS_COMPILE}clang" PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH REQUIRED)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
set(NOSTDINC "")
4+
5+
# Note that NOSYSDEF_CFLAG may be an empty string, and
6+
# set_ifndef() does not work with empty string.
7+
if(NOT DEFINED NOSYSDEF_CFLAG)
8+
set(NOSYSDEF_CFLAG -undef)
9+
endif()
10+
11+
find_program(CMAKE_C_COMPILER "${CROSS_COMPILE}clang" PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH REQUIRED)
12+
find_program(CMAKE_CXX_COMPILER "${CROSS_COMPILE}clang++" PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH REQUIRED)
13+
14+
if(SYSROOT_DIR)
15+
# The toolchain has specified a sysroot dir, pass it to the compiler
16+
list(APPEND TOOLCHAIN_C_FLAGS "--sysroot=${SYSROOT_DIR}")
17+
list(APPEND TOOLCHAIN_LD_FLAGS "--sysroot=${SYSROOT_DIR}")
18+
endif()
19+
20+
include(${ZEPHYR_BASE}/cmake/gcc-m-cpu.cmake)
21+
include(${ZEPHYR_BASE}/cmake/gcc-m-fpu.cmake)
22+
23+
include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_riscv.cmake)
24+
25+
execute_process(
26+
COMMAND ${CMAKE_C_COMPILER} --version
27+
OUTPUT_VARIABLE full_version_output
28+
)
29+
30+
string(REGEX REPLACE ".*clang version ([0-9]+(\\.[0-9]+)*).*" "\\1" CLANG_COMPILER_VERSION "${full_version_output}")
31+
32+
# Clang < 17 doesn't support zicsr, zifencei in -march, strip them.
33+
if(CLANG_COMPILER_VERSION VERSION_LESS "17.0.0")
34+
foreach(var TOOLCHAIN_C_FLAGS TOOLCHAIN_LD_FLAGS LLEXT_APPEND_FLAGS)
35+
list(TRANSFORM ${var} REPLACE "^-march=([^ ]*)_zicsr([^ ]*)$" "-march=\\1\\2")
36+
list(TRANSFORM ${var} REPLACE "^-march=([^ ]*)_zifencei([^ ]*)$" "-march=\\1\\2")
37+
endforeach()
38+
endif()
39+
40+
foreach(file_name include/stddef.h)
41+
execute_process(
42+
COMMAND ${CMAKE_C_COMPILER} --print-file-name=${file_name}
43+
OUTPUT_VARIABLE _OUTPUT
44+
)
45+
get_filename_component(_OUTPUT "${_OUTPUT}" DIRECTORY)
46+
string(REGEX REPLACE "\n" "" _OUTPUT ${_OUTPUT})
47+
48+
list(APPEND NOSTDINC ${_OUTPUT})
49+
endforeach()
50+
51+
foreach(isystem_include_dir ${NOSTDINC})
52+
list(APPEND isystem_include_flags -isystem "\"${isystem_include_dir}\"")
53+
endforeach()
54+
55+
list(APPEND CMAKE_REQUIRED_FLAGS -nostartfiles -nostdlib ${isystem_include_flags})
56+
string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
57+
58+
include(${ZEPHYR_BASE}/cmake/compiler/andes/common.cmake)

cmake/compiler/andes/common.cmake

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
list(TRANSFORM TOOLCHAIN_C_FLAGS REPLACE "^-march=([^ ]+)$" "-march=\\1_xandes")
4+
list(TRANSFORM TOOLCHAIN_LD_FLAGS REPLACE "^-march=([^ ]+)$" "-march=\\1_xandes")
5+
list(TRANSFORM LLEXT_APPEND_FLAGS REPLACE "^-march=([^ ]+)$" "-march=\\1_xandes")
6+
7+
if(CONFIG_RISCV_CUSTOM_CSR_ANDES_EXECIT)
8+
list(APPEND TOOLCHAIN_C_FLAGS -mexecit)
9+
list(APPEND TOOLCHAIN_LD_FLAGS -Wl,--mexecit)
10+
11+
if(CONFIG_RISCV_CUSTOM_CSR_ANDES_NEXECIT)
12+
list(APPEND TOOLCHAIN_LD_FLAGS -Wl,--mnexecitop)
13+
endif()
14+
else()
15+
# Disable execit explicitly, as -Os enables it by default.
16+
list(APPEND TOOLCHAIN_C_FLAGS -mno-execit)
17+
list(APPEND TOOLCHAIN_LD_FLAGS -Wl,--mno-execit)
18+
endif()
19+
20+
if(CONFIG_RISCV_CUSTOM_CSR_ANDES_HWDSP)
21+
list(APPEND TOOLCHAIN_C_FLAGS -mext-dsp)
22+
endif()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
include(${ZEPHYR_BASE}/cmake/compiler/gcc/compiler_flags.cmake)

cmake/compiler/andes/generic.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
include(${ZEPHYR_BASE}/cmake/compiler/gcc/generic.cmake)

cmake/compiler/andes/target.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
include(${ZEPHYR_BASE}/cmake/compiler/gcc/target.cmake)
4+
5+
include(${ZEPHYR_BASE}/cmake/compiler/andes/common.cmake)

soc/andestech/ae350/CMakeLists.txt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,6 @@ zephyr_linker_sources(ROM_START SORT_KEY 0x0 common_linker/init.ld)
1313
zephyr_linker_sources_ifdef(CONFIG_SOC_ANDES_V5_EXECIT RODATA SORT_KEY 0x0 common_linker/execit.ld)
1414
zephyr_linker_sources_ifdef(CONFIG_XIP RAM_SECTIONS SORT_KEY 0x0 common_linker/ram_start_nonzero.ld)
1515

16-
# Note: AndeStar V5 DSP needs custom Andes V5 toolchain
17-
if(CONFIG_SOC_ANDES_V5_HWDSP)
18-
zephyr_cc_option(-mext-dsp)
19-
endif()
20-
21-
# Note: AndeStar V5 EXEC.IT needs custom Andes V5 toolchain
22-
if(CONFIG_SOC_ANDES_V5_EXECIT)
23-
zephyr_cc_option(-mexecit)
24-
zephyr_ld_options(-Wl,--mexecit)
25-
endif()
26-
2716
if(CONFIG_SOC_SERIES_ANDES_AE350)
2817
set(SOC_LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/linker.ld CACHE INTERNAL "")
2918
endif()

soc/telink/tlsr/tlsr951x/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ zephyr_include_directories(.)
1313
zephyr_ld_options(-fuse-ld=bfd)
1414

1515
# Set compile options
16-
zephyr_compile_options_ifdef(CONFIG_TELINK_B91_HWDSP -mext-dsp)
1716
zephyr_compile_options_ifndef(CONFIG_RISCV_GP -mno-relax)
1817
zephyr_linker_sources(ROM_START SORT_KEY 0x0 init.ld)
1918

0 commit comments

Comments
 (0)