This is an archive of the discontinued LLVM Phabricator instance.

[runtimes] [CMake] Fix checks for -Werror when building with incomplete toolchains
ClosedPublic

Authored by mstorsjo on Apr 25 2022, 4:01 AM.

Details

Reviewers
phosek
ldionne
Group Reviewers
Restricted Project
Restricted Project
Commits
rG4169b5251f58: [runtimes] [CMake] Fix checks for -Werror when building with incomplete…
Summary

When we add --unwindlib=none during the CMake configure phase (to
make CMake linking tests succeed before the unwind library has been
built for the first time - when bootstrapping a cross toolchain from
scratch), we add it to CMAKE_REQUIRED_FLAGS to make later CMake tests
pass.

When the option is added to CMAKE_REQUIRED_FLAGS, it gets added to
both compilation and linking commands. When --unwindlib=none is added
to the compilation command, it causes warnings (about being unused
during compilation, as it only affects linking).

When all CMake test compilations produce warnings, later CMake tests
for -Werror fail.

Add --{start,end}-no-unused-arguments around --unwindlib=none, if
supported, to avoid unnecessary warnings due to this option.

If the CMake requirement is bumped to 3.14, we could use
CMAKE_REQUIRED_LINK_OPTIONS instead, removing the need for the
--{start,end}-no-unused-arguments options. (However, do note that
CMAKE_REQUIRED_LINK_OPTIONS is problematic in combination with
CMAKE_TRY_COMPILE_TARGET_TYPE set to STATIC_LIBRARY; see
https://gitlab.kitware.com/cmake/cmake/-/issues/23454.)

Diff Detail

Event Timeline

mstorsjo created this revision.Apr 25 2022, 4:01 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 25 2022, 4:01 AM
Herald added a subscriber: mgorny. · View Herald Transcript
mstorsjo requested review of this revision.Apr 25 2022, 4:01 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptApr 25 2022, 4:01 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript
Herald added a reviewer: Restricted Project. · View Herald Transcript
ldionne added inline comments.
runtimes/CMakeLists.txt
106–124

Shouldn't this be more like

if (C_SUPPORTS_START_NO_UNUSED_ARGUMENTS)
  set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS} --start-no-unused-arguments --unwindlib=none --end-no-unused-arguments")
else()
  set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --unwindlib=none")
endif()

Right now you seem to be executing

set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --unwindlib=none")

in all cases.

Edit Oh, I see it now. You override CMAKE_REQUIRED_FLAGS with ORIG_CMAKE_REQUIRED_FLAGS when C_SUPPORTS_START_NO_UNUSED_ARGUMENTS is supported. I think it would be clearer to use a if-else instead.

mstorsjo added inline comments.Apr 25 2022, 11:59 AM
runtimes/CMakeLists.txt
106–124

That's actually how I wrote it first, but it doesn't work quite like that.

The problem is that if we're using an incomplete toolchain where linking fails, the check_c_compiler_flag to check for whether --start-no-unused-arguments is supported also will fail (because cmake tests fail in linking) until we've set --unwindlib=none in CMAKE_REQUIRED_FLAGS. So we must set it first, then test for the other option, and then finally see if we can revise it later.

ldionne accepted this revision.Apr 25 2022, 1:24 PM
ldionne added inline comments.
runtimes/CMakeLists.txt
106–124

Tricky!

This revision is now accepted and ready to land.Apr 25 2022, 1:24 PM
phosek accepted this revision.Apr 25 2022, 2:25 PM

LGTM

runtimes/CMakeLists.txt
105

This is a minor nit, but it'd be nice to have a naming convention for this pattern. I did a quick search and found various different instances, like ORIG_<name>, <name>_ORIG, SAVED_<name>, __SAVED_<name>. The last one, __SAVED_<name> is the one I'd prefer. It's already used in CMake's own modules and I think it's best at signaling that this is an internal variable that has a specific purpose.

mstorsjo updated this revision to Diff 429047.May 12 2022, 12:32 PM

Rebased, rerunning CI. (This codepath probably isn't exercised at all in the CI environment though.)

This revision was landed with ongoing or failed builds.May 12 2022, 2:30 PM
This revision was automatically updated to reflect the committed changes.