This is an archive of the discontinued LLVM Phabricator instance.

[compiler-rt] Honor CMAKE_SYSROOT in tests
Needs ReviewPublic

Authored by abrachet on May 21 2023, 9:23 PM.

Details

Summary

This makes sure that compiler-rt properly uses --sysroot when compiling.
This isn't an issue for normal CMake targets, tests however often invoke
clang on their own. A few places were not correctly using the flags from
COMPILER_RT_TEST_COMPILER_CFLAGS, this patch fixes that.

Diff Detail

Event Timeline

abrachet created this revision.May 21 2023, 9:23 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 21 2023, 9:23 PM
Herald added subscribers: Enna1, dberris. · View Herald Transcript
abrachet requested review of this revision.May 21 2023, 9:23 PM

I tried to address a similar issue in D126936. I wonder if the concern raised @delcypher applies to this change as well. Specifically, that there is no single sysroot that would be apply to all Darwin targets. Concretely, on Apple platforms we use find_darwin_sdk_dir to find the right sysroot for each Darwin flavor, ignoring CMAKE_SYSROOT.

phosek added inline comments.May 22 2023, 3:43 PM
compiler-rt/CMakeLists.txt
573–575

A potential path forward that sidesteps the Darwin issue would be to restrict this only to non-Apple platforms.

abrachet updated this revision to Diff 524520.May 22 2023, 3:45 PM
abrachet marked an inline comment as done.
phosek added inline comments.May 22 2023, 3:52 PM
compiler-rt/lib/tsan/rtl/CMakeLists.txt
117

This is a bit surprising but after reading how EXTRA_CFLAGS is being used, I understand why this needed. I'd add a short comment explaining why we need to add test CFLAGS here.

abrachet updated this revision to Diff 524523.May 22 2023, 3:59 PM
abrachet marked an inline comment as done.
gulfem added a subscriber: gulfem.May 24 2023, 8:34 PM
vitalybuka resigned from this revision.May 25 2023, 2:15 PM

LGTM if it does not break bots

delcypher added a comment.EditedMay 26 2023, 12:52 AM

@abrachet This change looks reasonable but it could really do with an actual description of the change being made and why.

The fact its only touching the TSan, SafeStack, and LibFuzzer test suites (and not others) should also be explained.

delcypher added inline comments.May 26 2023, 1:13 AM
compiler-rt/CMakeLists.txt
574

@abrachet There should probably be comment here (or at the very least an explanation in the commit message/phabricator review description) as to why this is needed.

CMake's docs say CMAKE_SYSROOT is set in toolchain files, so I'm guessing the motivation here is to make sure if compiler-rt's CMake configure is set to use that if a toolchain file was used?

compiler-rt/test/tsan/CMakeLists.txt
43 ↗(On Diff #524523)

@abrachet get_test_cc_for_arch is already supposed to add COMPILER_RT_TEST_COMPILER_CFLAGS in several cases. Why doesn't that work for your use case?

delcypher requested changes to this revision.May 26 2023, 1:19 AM

@abrachet I left some comments/questions. When putting a patch up for review in phabricator please include the message that would be in the commit in the review to help reviewers understand your change and why it's being made.

This revision now requires changes to proceed.May 26 2023, 1:19 AM
abrachet updated this revision to Diff 526508.May 29 2023, 9:54 PM
abrachet edited the summary of this revision. (Show Details)
abrachet marked 2 inline comments as done.May 29 2023, 10:03 PM

The fact its only touching the TSan, SafeStack, and LibFuzzer test suites (and not others) should also be explained.

These just happen to be the only others that were breaking because they accidentally omitted adding config.target_cflags in their respective compiler substitutions.

compiler-rt/test/tsan/CMakeLists.txt
43 ↗(On Diff #524523)

It only does this when "cross compiling" (I use quotes because the comment says this but the check is just ${arch} MATCHES "arm|aarch64|riscv32|riscv64". Other platforms won't get this. I've instead changed the way that target_cflags is set in lit.common.configured.in so COMPILER_RT_TEST_COMPILER_CFLAGS will always be appended to target_cflags. That solution also allows us to remove this workaround of adding COMPILER_RT_TEST_COMPILER_CFLAGS here. I didn't previously realize that setting *_TEST_TARGET_CFLAGS would override the default for config.target_cflags which is COMPILER_RT_TEST_COMPILER_CFLAGS

@abrachet Thanks for addressing my feedback. I have an additional question but other than that I think the change looks reasonable. I'm going to add some other reviewers from Apple who work on compiler-rt more than me for visibility.

compiler-rt/test/lit.common.configured.in
70

Why was it necessary to remove the call to set_default and add this manual code here?

@yln @thetruestblue @rsundahl @wrotki Adding you for visibility. I suspect this change is fine but I just wanted to make sure you know its happening.

...Concretely, on Apple platforms we use find_darwin_sdk_dir to find the right sysroot for each Darwin flavor, ignoring CMAKE_SYSROOT.

This seems important enough of a distinction to document early in say compiler-rt/CMakeLists.txt so we can refer back to it as rationale when you add "if (NOT APPLE AND (...)), else the "NOT APPLE" is just hanging there as a head scratcher.

abrachet updated this revision to Diff 539036.Jul 11 2023, 5:22 AM
abrachet marked an inline comment as done.
abrachet marked an inline comment as done.Jul 11 2023, 5:27 AM

...Concretely, on Apple platforms we use find_darwin_sdk_dir to find the right sysroot for each Darwin flavor, ignoring CMAKE_SYSROOT.

This seems important enough of a distinction to document early in say compiler-rt/CMakeLists.txt so we can refer back to it as rationale when you add "if (NOT APPLE AND (...)), else the "NOT APPLE" is just hanging there as a head scratcher.

Added a comment

compiler-rt/test/lit.common.configured.in
70

set_default would only set target_cflags to @COMPILER_RT_TEST_COMPILER_CFLAGS@ if target_cflags isn't already set. Almost all of the local lit.site.cfg.py.in files have something like config.target_cflags = "@ASAN_TEST_TARGET_CFLAGS@". So if ASAN_TEST_TARGET_CFLAGS is set, the asan tests won't have the flags specified in COMPILER_RT_TEST_COMPILER_CFLAGS, which is surprising behavior. This was a problem specifically for tsan because TSAN_TEST_TARGET_CFLAGS was being set, see https://reviews.llvm.org/D151057?id=524523#inline-1465740

I've changed this here so COMPILER_RT_TEST_COMPILER_CFLAGS will be appended to target_cflags for all test suites regardless of setting sanitizer test specific flags via *_TEST_TARGET_CFLAGS.