[PATCH] D116709: [CMake][WinMsvc] Fix user passed compiler/linker flags

Yuanfang Chen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 5 16:26:07 PST 2022


ychen created this revision.
ychen added reviewers: smeenai, mstorsjo, zturner.
Herald added a subscriber: mgorny.
ychen requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

User 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. Also, for the native tool build, target flags should not be
used.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116709

Files:
  llvm/cmake/platforms/WinMsvc.cmake


Index: llvm/cmake/platforms/WinMsvc.cmake
===================================================================
--- llvm/cmake/platforms/WinMsvc.cmake
+++ llvm/cmake/platforms/WinMsvc.cmake
@@ -250,6 +250,11 @@
 list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_ASM_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang")
 list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_C_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang")
 list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_CXX_COMPILER=${LLVM_NATIVE_TOOLCHAIN}/bin/clang++")
+list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_C_FLAGS=\"\"")
+list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_CXX_FLAGS=\"\"")
+list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_EXE_LINKER_FLAGS=\"\"")
+list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_MODULE_LINKER_FLAGS=\"\"")
+list(APPEND _CTF_NATIVE_DEFAULT "-DCMAKE_SHARED_LINKER_FLAGS=\"\"")
 
 set(CROSS_TOOLCHAIN_FLAGS_NATIVE "${_CTF_NATIVE_DEFAULT}" CACHE STRING "")
 
@@ -277,18 +282,8 @@
 endif()
 
 string(REPLACE ";" " " COMPILE_FLAGS "${COMPILE_FLAGS}")
-
-# We need to preserve any flags that were passed in by the user. However, we
-# can't append to CMAKE_C_FLAGS and friends directly, because toolchain files
-# will be re-invoked on each reconfigure and therefore need to be idempotent.
-# The assignments to the _INITIAL cache variables don't use FORCE, so they'll
-# only be populated on the initial configure, and their values won't change
-# afterward.
-set(_CMAKE_C_FLAGS_INITIAL "${CMAKE_C_FLAGS}" CACHE STRING "")
-set(CMAKE_C_FLAGS "${_CMAKE_C_FLAGS_INITIAL} ${COMPILE_FLAGS}" CACHE STRING "" FORCE)
-
-set(_CMAKE_CXX_FLAGS_INITIAL "${CMAKE_CXX_FLAGS}" CACHE STRING "")
-set(CMAKE_CXX_FLAGS "${_CMAKE_CXX_FLAGS_INITIAL} ${COMPILE_FLAGS}" CACHE STRING "" FORCE)
+string(APPEND CMAKE_C_FLAGS_INIT " ${COMPILE_FLAGS}")
+string(APPEND CMAKE_CXX_FLAGS_INIT " ${COMPILE_FLAGS}")
 
 set(LINK_FLAGS
     # Prevent CMake from attempting to invoke mt.exe. It only recognizes the slashed form and not the dashed form.
@@ -312,16 +307,9 @@
 endif()
 
 string(REPLACE ";" " " LINK_FLAGS "${LINK_FLAGS}")
-
-# See explanation for compiler flags above for the _INITIAL variables.
-set(_CMAKE_EXE_LINKER_FLAGS_INITIAL "${CMAKE_EXE_LINKER_FLAGS}" CACHE STRING "")
-set(CMAKE_EXE_LINKER_FLAGS "${_CMAKE_EXE_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
-
-set(_CMAKE_MODULE_LINKER_FLAGS_INITIAL "${CMAKE_MODULE_LINKER_FLAGS}" CACHE STRING "")
-set(CMAKE_MODULE_LINKER_FLAGS "${_CMAKE_MODULE_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
-
-set(_CMAKE_SHARED_LINKER_FLAGS_INITIAL "${CMAKE_SHARED_LINKER_FLAGS}" CACHE STRING "")
-set(CMAKE_SHARED_LINKER_FLAGS "${_CMAKE_SHARED_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
+string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " ${LINK_FLAGS}")
+string(APPEND CMAKE_MODULE_LINKER_FLAGS_INIT " ${LINK_FLAGS}")
+string(APPEND CMAKE_SHARED_LINKER_FLAGS_INIT " ${LINK_FLAGS}")
 
 # CMake populates these with a bunch of unnecessary libraries, which requires
 # extra case-correcting symlinks and what not. Instead, let projects explicitly


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116709.397738.patch
Type: text/x-patch
Size: 3030 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220106/413a461b/attachment.bin>


More information about the llvm-commits mailing list