Skip to content

Commit d6d3000

Browse files
author
Yuanfang Chen
committed
[CMake][WinMsvc] Fix user passed compiler/linker flags
Users could pass flags by environment variables like CFLAGS/CXXFLAGS/LDFLAGS or by using CMAKE_<LANG>_FLAGS_INIT/CMAKE_<t>_LINKER_FLAGS_INIT. So this toolchain file should append to INIT flags instead. Otherwise, user flags would be discarded here by assigning to CMAKE_<LANG>_FLAGS directly. Reviewed By: smeenai, mstorsjo Differential Revision: https://reviews.llvm.org/D116709
1 parent 69c8e64 commit d6d3000

File tree

1 file changed

+8
-22
lines changed

1 file changed

+8
-22
lines changed

llvm/cmake/platforms/WinMsvc.cmake

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
# up a VFS overlay for the SDK headers and case-correcting symlinks for the
8585
# libraries when running on a case-sensitive filesystem.
8686

87+
include_guard(GLOBAL)
8788

8889
# When configuring CMake with a toolchain file against a top-level CMakeLists.txt,
8990
# it will actually run CMake many times, once for each small test program used to
@@ -251,6 +252,8 @@ list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_ASM_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/b
251252
list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_C_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang")
252253
list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_CXX_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang++")
253254

255+
# These flags are used during build time. So if CFLAGS/CXXFLAGS/LDFLAGS is set
256+
# for the target, makes sure these are unset during build time.
254257
set(CROSS_TOOLCHAIN_FLAGS_NATIVE "${_CTF_NATIVE_DEFAULT}" CACHE STRING "")
255258

256259
set(COMPILE_FLAGS
@@ -277,18 +280,8 @@ if(case_sensitive_filesystem)
277280
endif()
278281

279282
string(REPLACE ";" " " COMPILE_FLAGS "${COMPILE_FLAGS}")
280-
281-
# We need to preserve any flags that were passed in by the user. However, we
282-
# can't append to CMAKE_C_FLAGS and friends directly, because toolchain files
283-
# will be re-invoked on each reconfigure and therefore need to be idempotent.
284-
# The assignments to the _INITIAL cache variables don't use FORCE, so they'll
285-
# only be populated on the initial configure, and their values won't change
286-
# afterward.
287-
set(_CMAKE_C_FLAGS_INITIAL "${CMAKE_C_FLAGS}" CACHE STRING "")
288-
set(CMAKE_C_FLAGS "${_CMAKE_C_FLAGS_INITIAL} ${COMPILE_FLAGS}" CACHE STRING "" FORCE)
289-
290-
set(_CMAKE_CXX_FLAGS_INITIAL "${CMAKE_CXX_FLAGS}" CACHE STRING "")
291-
set(CMAKE_CXX_FLAGS "${_CMAKE_CXX_FLAGS_INITIAL} ${COMPILE_FLAGS}" CACHE STRING "" FORCE)
283+
string(APPEND CMAKE_C_FLAGS_INIT " ${COMPILE_FLAGS}")
284+
string(APPEND CMAKE_CXX_FLAGS_INIT " ${COMPILE_FLAGS}")
292285

293286
set(LINK_FLAGS
294287
# Prevent CMake from attempting to invoke mt.exe. It only recognizes the slashed form and not the dashed form.
@@ -312,16 +305,9 @@ if(case_sensitive_filesystem)
312305
endif()
313306

314307
string(REPLACE ";" " " LINK_FLAGS "${LINK_FLAGS}")
315-
316-
# See explanation for compiler flags above for the _INITIAL variables.
317-
set(_CMAKE_EXE_LINKER_FLAGS_INITIAL "${CMAKE_EXE_LINKER_FLAGS}" CACHE STRING "")
318-
set(CMAKE_EXE_LINKER_FLAGS "${_CMAKE_EXE_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
319-
320-
set(_CMAKE_MODULE_LINKER_FLAGS_INITIAL "${CMAKE_MODULE_LINKER_FLAGS}" CACHE STRING "")
321-
set(CMAKE_MODULE_LINKER_FLAGS "${_CMAKE_MODULE_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
322-
323-
set(_CMAKE_SHARED_LINKER_FLAGS_INITIAL "${CMAKE_SHARED_LINKER_FLAGS}" CACHE STRING "")
324-
set(CMAKE_SHARED_LINKER_FLAGS "${_CMAKE_SHARED_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
308+
string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " ${LINK_FLAGS}")
309+
string(APPEND CMAKE_MODULE_LINKER_FLAGS_INIT " ${LINK_FLAGS}")
310+
string(APPEND CMAKE_SHARED_LINKER_FLAGS_INIT " ${LINK_FLAGS}")
325311

326312
# CMake populates these with a bunch of unnecessary libraries, which requires
327313
# extra case-correcting symlinks and what not. Instead, let projects explicitly

0 commit comments

Comments
 (0)