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

Yuanfang Chen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 6 13:52:32 PST 2022


ychen added a comment.

In D116709#3224870 <https://reviews.llvm.org/D116709#3224870>, @mstorsjo wrote:

>> 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.
>
> Can you elaborate a bit more here so I follow properly (with more explanation of exactly what cmake does here)? If the user has set e.g. `CXXFLAGS`, that does end up picked up by cmake and set in `CMAKE_CXX_FLAGS_INIT` - is that correct? What happened before, when we appended things into `CMAKE_CXX_FLAGS` without taking `CMAKE_CXX_FLAGS_INIT` into account - were the flags from `CXXFLAGS` and/or `CMAKE_CXX_FLAGS_INIT` missed/forgotten/overridden? Can you explain the implicit cmake behavious that are at play here?

cmake uses `CMAKE_<LANG>_FLAGS_INIT` (INIT flags themselves are initialized by environment variables like `CFLAGS/CXXFLAGS/LDFLAGS`, or by a cmake toolchain file) to initialize `CMAKE_<LANG>_FLAGS`. If `CMAKE_<LANG>_FLAGS` is set by the user directly, cmake will not initialize it from `CMAKE_<LANG>_FLAGS_INIT anymore`, so the user-specified flags are lost. Docs on `CMAKE_<LANG>_FLAGS_INIT` suggests that the preferred behavior is for a toolchain file to manipulate INIT flags.

>> Also, for the native tool build, target flags should not be used.
>
> Flags specified to the main cmake build (`CMAKE_<LANG>_FLAGS*`) shouldn't be propagated automatically to the inner native cmake build, or am I missing something?

That's right except when users specify flags by environment vars which will be picked up by sub-cmake through the above-described initialization process.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116709/new/

https://reviews.llvm.org/D116709



More information about the llvm-commits mailing list