Skip to content

Commit f1429d9

Browse files
committed
cmake: clang: Support host's clang for non-MCU x86 targets on Linux
Pick host's clang if ZEPHYR_TOOLCHAIN_VARIANT=host-llvm. Signed-off-by: Oleg Zhurakivskyy <[email protected]>
1 parent d9876be commit f1429d9

File tree

10 files changed

+47
-8
lines changed

10 files changed

+47
-8
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,8 @@ zephyr_ld_options(
386386
)
387387

388388
get_property(TOPT GLOBAL PROPERTY TOPT)
389-
set_ifndef( TOPT -T)
389+
set_ifndef( TOPT -Wl,-T) # clang doesn't pick -T for some reason and complains,
390+
# while -Wl,-T works for both, gcc and clang
390391

391392
if(NOT CONFIG_NATIVE_APPLICATION)
392393
# Funny thing is if this is set to =error, some architectures will

arch/posix/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
zephyr_compile_options(
2-
-fno-freestanding
32
-m32
43
-MMD
54
-MP
65
${ARCH_FLAG}
76
-include ${ZEPHYR_BASE}/arch/posix/include/posix_cheats.h
87
)
98

9+
if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang") # clang errors on -fno-freestanding
10+
zephyr_compile_options(
11+
-fno-freestanding
12+
)
13+
endif()
14+
1015
zephyr_include_directories(${BOARD_DIR})
1116

1217
zephyr_compile_options_ifdef(CONFIG_COVERAGE

cmake/compiler/gcc/target_security_fortify.cmake

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ macro(toolchain_cc_security_fortify)
55
# _FORTIFY_SOURCE: Detect common-case buffer overflows for certain functions
66
# _FORTIFY_SOURCE=1 : Compile-time checks (requires -O1 at least)
77
# _FORTIFY_SOURCE=2 : Additional lightweight run-time checks
8-
zephyr_compile_definitions(
9-
_FORTIFY_SOURCE=2
10-
)
8+
9+
if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
10+
# clang errors on _FORTIFY_SOURCE=2
11+
zephyr_compile_definitions(
12+
_FORTIFY_SOURCE=2
13+
)
14+
endif()
1115
endif()
1216

1317
endmacro()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Configures CMake for using host's clang
2+
3+
find_program(CMAKE_C_COMPILER clang)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Configures CMake for using clang
2+
3+
find_program(CMAKE_C_COMPILER clang )
4+
#find_program(CMAKE_CXX_COMPILER clang++ ) # Not used currently
5+
find_program(CMAKE_OBJCOPY objcopy )
6+
#find_program(CMAKE_OBJDUMP llvm-objdump) # Not used currently
7+
#find_program(CMAKE_LINKER ld ) # Not used currently
8+
find_program(CMAKE_AR llvm-ar )
9+
#find_program(CMAKE_RANLILB llvm-ranlib ) # Not used currently
10+
find_program(CMAKE_READELF readelf )
11+
find_program(CMAKE_GDB gdb )
12+
13+
# Load toolchain_cc-family macros
14+
# Significant overlap with freestanding gcc compiler so reuse it
15+
include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_security_fortify.cmake)
16+
include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_security_canaries.cmake)
17+
include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_optimizations.cmake)
18+
include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_cpp.cmake)
19+
include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_asm.cmake)
20+
include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_baremetal.cmake)

cmake/generic_toolchain.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ assert(ZEPHYR_TOOLCHAIN_VARIANT "Zephyr toolchain variant invalid: please set th
4848

4949
# Pick host system's toolchain if we are targeting posix
5050
if((${ARCH} STREQUAL "posix") OR (${ARCH} STREQUAL "x86_64"))
51-
set(ZEPHYR_TOOLCHAIN_VARIANT "host")
51+
if(NOT "${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "host-llvm")
52+
set(ZEPHYR_TOOLCHAIN_VARIANT "host")
53+
endif()
5254
endif()
5355

5456
# Configure the toolchain based on what SDK/toolchain is in use.

cmake/target_toolchain.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ set(CMAKE_SYSTEM_VERSION ${PROJECT_VERSION})
3030
# We are not building dynamically loadable libraries
3131
set(BUILD_SHARED_LIBS OFF)
3232

33-
if(NOT (COMPILER STREQUAL "host-gcc"))
33+
if(NOT ((COMPILER STREQUAL "host-gcc") OR (COMPILER STREQUAL "host-clang")))
3434
include(${TOOLCHAIN_ROOT}/cmake/toolchain/${ZEPHYR_TOOLCHAIN_VARIANT}/target.cmake)
3535
endif()
3636

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
set(COMPILER host-clang)
2+
set(TOOLCHAIN_HAS_NEWLIB OFF CACHE BOOL "True if toolchain supports newlib")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This file intentionally left blank.

doc/getting_started/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ Set Up a Toolchain
157157
In some specific configurations like non-MCU x86 targets on Linux,
158158
you may be able to re-use the native development tools provided by
159159
your operating system instead of an SDK by setting
160-
``ZEPHYR_TOOLCHAIN_VARIANT=host``.
160+
``ZEPHYR_TOOLCHAIN_VARIANT=host`` for gcc or
161+
``ZEPHYR_TOOLCHAIN_VARIANT=host-llvm`` for clang.
161162

162163
If you want, you can use the SDK host tools (such as OpenOCD) with a
163164
different toolchain by keeping the :envvar:`ZEPHYR_SDK_INSTALL_DIR`

0 commit comments

Comments
 (0)