This is an archive of the discontinued LLVM Phabricator instance.

Do not set CMAKE_CXX_FLAGS_<config> with FORCE
Needs ReviewPublic

Authored by ArtemSkrebkov on May 27 2023, 7:59 AM.

Details

Summary

Updating CMAKE_CXX_FLAGS_RELEASE (with CACHE FORCE) forces
to re-initialize it each time with a new value taken from cache.

In my project , it leads to CMAKE_CXX_FLAGS_RELEASE like this /MD /O2
/Ob2 /DNDEBUG /sdl /guard:cf /wd4996 /wd4146 /wd4703 /wd4996 /wd4146
/wd4703 /wd4996 /wd4146 /wd4703 /wd4996 /wd4146 /wd4703 repeating flags
again.

The consequences of this is that the project has to be rebuilt from
scratch every time you call cmake configure.

Diff Detail

Event Timeline

ArtemSkrebkov created this revision.May 27 2023, 7:59 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 27 2023, 7:59 AM
ArtemSkrebkov requested review of this revision.May 27 2023, 7:59 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 27 2023, 7:59 AM

@aaronpuchert @beanz

I added you as reviewers since you had touched this file last year :)

I did a minor cleanup that touched this file, but I don't actually use MSVC. I think @beanz is the right reviewer, just a very busy one. ;)

FWIW, it seems reasonable to me, especially compared with line 41. If we can't get @beanz to look at it, I can also accept it.

beanz added a subscriber: mstorsjo.Aug 20 2023, 6:14 PM

Sorry, this slipped my notice. @mstorsjo made some changes in this area recently with D155233. The approach there is to migrate to using the CMake built-in option CMAKE_MSVC_RUNTIME_LIBRARY.

@ArtemSkrebkov, does the issue you were encountering still occur on main now or did D155233 resolve it?

FORCE setting is in place after https://reviews.llvm.org/D155233 , so I guess it still should be the case.

But let me check to be sure.

beanz added a comment.Aug 21 2023, 6:35 AM

FORCE setting is in place after https://reviews.llvm.org/D155233 , so I guess it still should be the case.

But let me check to be sure.

The problem isn’t actually with the FORCE flag. The problem you were seeing was caused by CMake and LLVM’s configure scripts each having its own way of setting those flags. When these scripts were first written CMake didn’t have an official way to set those flags. LLVM adopts new CMake behaviors fairly slowly to maintain support for older versions of CMake.

The code today removes the -M* options from the C and CXX flags variables and relies on setting the CMAKE_MSVC_RUNTIME_LIBRARY variable instead. @mstorsjo could speak more about the need for it to be FORCEd, but my guess is that since that is a CMake option, a default value for it gets set, and _only_ when translating the LLVM option to the CMake option we need to FORCE overwriting the value. So if you don’t pass LLVM_USE_CRT_*, the FORCE line shouldn’t get hit.

FORCE setting is in place after https://reviews.llvm.org/D155233 , so I guess it still should be the case.

But let me check to be sure.

The problem isn’t actually with the FORCE flag. The problem you were seeing was caused by CMake and LLVM’s configure scripts each having its own way of setting those flags. When these scripts were first written CMake didn’t have an official way to set those flags. LLVM adopts new CMake behaviors fairly slowly to maintain support for older versions of CMake.

The code today removes the -M* options from the C and CXX flags variables and relies on setting the CMAKE_MSVC_RUNTIME_LIBRARY variable instead. @mstorsjo could speak more about the need for it to be FORCEd, but my guess is that since that is a CMake option, a default value for it gets set, and _only_ when translating the LLVM option to the CMake option we need to FORCE overwriting the value. So if you don’t pass LLVM_USE_CRT_*, the FORCE line shouldn’t get hit.

I don't have much insight/opinion on the use of FORCE here, I just refactored this piece of code to keep it around somewhat, until everybody are transitioned over to CMAKE_MSVC_RUNTIME_LIBRARY.

@beanz you are right :) I confirm that my issue is not reproducible anymore. Thanks for sharing the insights on CMake handling in LLVM.