-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmake: clang: Support host's clang for non-MCU x86 targets on Linux #14077
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doc change LGTM...
Codecov Report
@@ Coverage Diff @@
## master #14077 +/- ##
==========================================
+ Coverage 52.02% 52.68% +0.65%
==========================================
Files 307 307
Lines 45484 45473 -11
Branches 10535 10530 -5
==========================================
+ Hits 23662 23956 +294
+ Misses 17030 16640 -390
- Partials 4792 4877 +85
Continue to review full report at Codecov.
|
|
||
find_program(CMAKE_C_COMPILER clang ) | ||
#find_program(CMAKE_CXX_COMPILER clang++ ) # Not used currently | ||
#find_program(CMAKE_OBJCOPY objcopy ) # Not used currently |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure about that? I know a bunch of x86 post build steps use objcopy.
I'd rather we not commit commented-out code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just hecked for native_posix, it’s used at CMake configure/generate step, but not at compile step.
Sure, let’s include it.
arch/posix/CMakeLists.txt
Outdated
-m32 | ||
-MMD | ||
-MP | ||
${ARCH_FLAG} | ||
-include ${ZEPHYR_BASE}/arch/posix/include/posix_cheats.h | ||
) | ||
|
||
if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang") # clang errors on -fno-freestanding | ||
zephyr_compile_options( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can and should avoid this if statement by doing zephyr_cc_options() instead of zephyr_compile_options.
zephyr_compile_definitions( | ||
_FORTIFY_SOURCE=2 | ||
) | ||
endif() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't sane. We are doing "if clang" in a gcc file. Clang should instead not use gcc's target_security_fortify.cmake.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. I checked, clang doesn't understand fortify at all. I refined it, please take a look.
Would it be possible/desirable to have clang and llvm support host toolchains instead of introducing host-clang and host-llvm? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine, a few minor changes required though.
(Eventually, the toolchain abstraction work will also abstract the linker and binutils, so TOPT will be dealt with)
arch/posix/CMakeLists.txt
Outdated
-m32 | ||
-MMD | ||
-MP | ||
${ARCH_FLAG} | ||
-include ${ZEPHYR_BASE}/arch/posix/include/posix_cheats.h | ||
) | ||
|
||
if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang") # clang errors on -fno-freestanding |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are many toolchains that are not clang.
I'd suggest the inverse: If GNU, rather than if not Clang.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. Fixed, have a look.
zephyr_compile_definitions( | ||
_FORTIFY_SOURCE=2 | ||
) | ||
endif() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is for gcc, but until now clang has been reusing gcc stuff. But now there is a difference, so now clang does need its own target_security_fortify.cmake
.
|
||
# Load toolchain_cc-family macros | ||
# Significant overlap with freestanding gcc compiler so reuse it | ||
include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_security_fortify.cmake) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be ${ZEPHYR_BASE}/cmake/compiler/${COMPILER}/target_security_fortify.cmake
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just checked, clang doesn't understand fortify at all. Fixed, have a look.
d0f9d2c
to
2b3763b
Compare
|
@nashif @SebastianBoe @andrewboie @mped-oticon And a wish for the native_posix is to make it optionally possible to relax –m32, so any host library can be linked into as is. |
I'm suggesting to merge them, or in other words, have this PR patch the original build scripts instead of creating new ones. |
Allowing people to override it would be ok (if they know what they are doing, and are willing to suffer the pain). |
cmake/target_toolchain.cmake
Outdated
@@ -30,7 +30,7 @@ set(CMAKE_SYSTEM_VERSION ${PROJECT_VERSION}) | |||
# We are not building dynamically loadable libraries | |||
set(BUILD_SHARED_LIBS OFF) | |||
|
|||
if(NOT (COMPILER STREQUAL "host-gcc")) | |||
if(NOT ((COMPILER STREQUAL "host-gcc") OR (COMPILER STREQUAL "host-clang"))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how is host-clang different from the current clang compiler definition we have?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how is host-clang different from the current clang compiler definition we have?
Not too much. After the discussion with @SebastianBoe, I am in progress of merging this with the existing cmake/toolchain/llvm and cmake/compiler/clang.
However I am missing something subtle there and can’t get it to work that way yet.
The original need is to use libFuzzer to fuzz a few states in the experimental TCP compiled as a native_posix application.
Merged with the existing cmake/toolchain/llvm and cmake/compiler/clang, take a look. |
Sorry, I pushed a wrong stuff, ignore please. |
This is the correct one, please take a look. |
CMakeLists.txt
Outdated
toolchain_cc_security_fortify() | ||
if(COMMAND toolchain_cc_security_fortify) | ||
toolchain_cc_security_fortify() | ||
endif() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that by-design toolchains that don't support this should declare NO-OP functions.
Correct me if I'm wrong @mped-oticon
cmake/compiler/clang/generic.cmake
Outdated
@@ -1 +1,5 @@ | |||
find_program(CMAKE_C_COMPILER clang PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) | |||
if(NOT "${ARCH}" STREQUAL "posix") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the NOT
c9142b2
to
0067801
Compare
Added.
Resolved, please take a look. |
@ozhuraki looks better now, but if you rebase on top of latest master...
|
OK, I will take a look. |
When linking, clang doesn't pick -T for some reason and complains, while -Wl,-T works for both, gcc and clang. Signed-off-by: Oleg Zhurakivskyy <[email protected]>
Mention that host installed clang can be used for native_posix. Signed-off-by: Oleg Zhurakivskyy <[email protected]>
Allow host installed clang to be used for native_posix when ZEPHYR_TOOLCHAIN_VARIANT=llvm. Signed-off-by: Oleg Zhurakivskyy <[email protected]>
Use it's own variable HOST_TOOLS_HOME for host tools and don't unconditionally set TOOLCHAIN_HOME, preventing the detection of llvm/clang host toolchain. Signed-off-by: Oleg Zhurakivskyy <[email protected]>
Host-tools don't unconditionally set TOOLCHAIN_HOME anymore, but in case Zephyr's SDK toolchain is used, set TOOLCHAIN_HOME. Signed-off-by: Oleg Zhurakivskyy <[email protected]>
The logic is practically intact and is the following: 1. Use any host installed llvm/clang in the path in case ZEPHYR_TOOLCHAIN_VARIANT=llvm is requested alone. 2. This can be further restricted with TOOLCHAIN_HOME. 3. And can be further overridden with CLANG_ROOT_DIR, like previously. So, only the unconditional restriction to /usr is lifted. Together with fixing the unconditional set of TOOLCHAIN_HOME by host tools for non-toolchain needs, this makes the logic more flexible. Now, after the logic is controllable by TOOLCHAIN_HOME, 3) might be an extra, but is left intact for backward compatibility. Signed-off-by: Oleg Zhurakivskyy <[email protected]>
In case TOOLCHAIN_HOME isn't explicitly reuqested, (or indirectly forced with CLANG_ROOT_DIR), detect any host installed clang in the path. Signed-off-by: Oleg Zhurakivskyy <[email protected]>
clang doesn't understand fortify at all, provide no op macro, in order to handle the request to fortify in a generic way. Signed-off-by: Oleg Zhurakivskyy <[email protected]>
native_posix should build with standard includes. Signed-off-by: Oleg Zhurakivskyy <[email protected]>
Fix comment, llvm -> clang. Signed-off-by: Oleg Zhurakivskyy <[email protected]>
clang has problems compiling the native_posix with -fno-freestanding. Signed-off-by: Oleg Zhurakivskyy <[email protected]>
Add llvm to supported toolchains. Signed-off-by: Oleg Zhurakivskyy <[email protected]>
libgcc isn't used by native_posix. Signed-off-by: Oleg Zhurakivskyy <[email protected]>
Invalidate toolchain capability cache on toolchain configuration. Signed-off-by: Oleg Zhurakivskyy <[email protected]>
OK, reproduced. Looks like toolchain capability cache is in the way on Fedora. On Ubuntu, I don’t see such problem. There’s probably less intrusive way to overpass this. |
@benpicco also hit this weird cache issue with just gcc, no clang involved. See his detailed and useful report in issue #15551 PS: I just discovered this PR in the git log. Considering my deterministic builds work in progress (see links in PR #14593) please copy me on major cmake and build PRs like this one, thanks! |
Changing in between gcc and clang for native_posix on Fedora was reliably triggering this. On Ubuntu it wasn’t reproducible. Removing the ${USER_CACHE_DIR} was fixing the issue. So, possibly something is wrong/incompatible with the toolchain capability database in such cases, hence this patch. |
There should at the very least be a comment in the source saying this was a blind shot and at what. |
Pick host's clang if ZEPHYR_TOOLCHAIN_VARIANT=llvm.
This has been tested on samples/net/sockets/echo native_posix application with clang-7 and upstream’s version.
Compiles, links and runs with and without optimizations.
Instructions: