Skip to content

Commit 5304034

Browse files
committed
Revert "[CMake] Unify llvm_check_linker_flag and llvm_check_compiler_linker_flag"
This reverts commit 55e65ad.
1 parent cfdcdf0 commit 5304034

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed
Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
1-
include(CheckLinkerFlag OPTIONAL)
1+
include(CMakePushCheckState)
22

3-
if (COMMAND check_linker_flag)
4-
macro(llvm_check_compiler_linker_flag)
5-
check_linker_flag(${ARGN})
6-
endmacro()
7-
else()
8-
# Until the minimum CMAKE version is 3.18
3+
include(CheckCompilerFlag OPTIONAL)
94

5+
if(NOT COMMAND check_compiler_flag)
6+
include(CheckCCompilerFlag)
107
include(CheckCXXCompilerFlag)
8+
endif()
9+
10+
function(llvm_check_compiler_linker_flag lang flag out_var)
11+
# If testing a flag with check_c_compiler_flag, it gets added to the compile
12+
# command only, but not to the linker command in that test. If the flag
13+
# is vital for linking to succeed, the test would fail even if it would
14+
# have succeeded if it was included on both commands.
15+
#
16+
# Therefore, try adding the flag to CMAKE_REQUIRED_FLAGS, which gets
17+
# added to both compiling and linking commands in the tests.
1118

12-
# cmake builtin compatible, except we assume lang is C or CXX
13-
function(llvm_check_compiler_linker_flag lang flag out_var)
14-
cmake_policy(PUSH)
15-
cmake_policy(SET CMP0056 NEW)
16-
set(_CMAKE_EXE_LINKER_FLAGS_SAVE ${CMAKE_EXE_LINKER_FLAGS})
17-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${flag}")
19+
cmake_push_check_state()
20+
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${flag}")
21+
if(COMMAND check_compiler_flag)
22+
check_compiler_flag("${lang}" "" ${out_var})
23+
else()
24+
# Until the minimum CMAKE version is 3.19
25+
# cmake builtin compatible, except we assume lang is C or CXX
1826
if("${lang}" STREQUAL "C")
1927
check_c_compiler_flag("" ${out_var})
2028
elseif("${lang}" STREQUAL "CXX")
2129
check_cxx_compiler_flag("" ${out_var})
2230
else()
2331
message(FATAL_ERROR "\"${lang}\" is not C or CXX")
2432
endif()
25-
set(CMAKE_EXE_LINKER_FLAGS ${_CMAKE_EXE_LINKER_FLAGS_SAVE})
26-
cmake_policy(POP)
27-
endfunction()
28-
endif()
33+
endif()
34+
cmake_pop_check_state()
35+
endfunction()

runtimes/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,8 @@ endif()
142142
# Check for -nostdlib++ first; if there's no C++ standard library yet,
143143
# all check_cxx_compiler_flag commands will fail until we add -nostdlib++
144144
# (or -nodefaultlibs).
145-
# TODO: Switch to check_linker_flag after raise the minimum CMake version past 3.14.
146-
check_cxx_compiler_flag(-nostdlib++ CXX_SUPPORTS_NOSTDLIBXX_FLAG)
145+
llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG)
147146
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
148-
# TODO: This is link only flag and should be added to CMAKE_REQUIRED_LINK_OPTIONS
149-
# but that's only supported since CMake 3.14.
150147
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
151148
endif()
152149
check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG)

0 commit comments

Comments
 (0)