Skip to content

Default CMAKE_CXX_FLAGS value is sometimes overridden #6781

@DownerCase

Description

@DownerCase

Xmake Version

v3.0.2+HEAD.993b5c30d

Operating System Version and Architecture

Windows 11 24H2

Describe Bug

Depending on the configuration packages installed with package.tools.cmake may or may not use the default flags produced by CMake.

For example:

If the project requires no modifications to the CXX flags then the CMake flags would look something like this:

//Flags used by the CXX compiler during all build types.
CMAKE_CXX_FLAGS:STRING=/DWIN32 /D_WINDOWS /EHsc

//Flags used by the CXX compiler during DEBUG builds.
CMAKE_CXX_FLAGS_DEBUG:STRING=/Zi /Ob0 /Od /RTC1

//Flags used by the CXX compiler during RELEASE builds.
CMAKE_CXX_FLAGS_RELEASE:STRING=/O2 /Ob2 /DNDEBUG

XMake however, for msvc at least, wants to set the runtime so it actually ends up looking like:

//Flags used by the CXX compiler during all build types.
CMAKE_CXX_FLAGS:STRING=/DWIN32 /D_WINDOWS /GR /EHsc

//Flags used by the CXX compiler during DEBUG builds.
CMAKE_CXX_FLAGS_DEBUG:STRING=/Zi /Ob0 /Od /RTC1

//Flags used by the CXX compiler during RELEASE builds.
CMAKE_CXX_FLAGS_RELEASE:STRING=/DWIN32 /D_WINDOWS /GR /EHsc /O2 /Ob2 /DNDEBUG -MD

However if we then enable policies=build.sanitizer.address the flags suddenly end up as:

//Flags used by the CXX compiler during all build types.
CMAKE_CXX_FLAGS:STRING=-fsanitize=address

//Flags used by the CXX compiler during DEBUG builds.
CMAKE_CXX_FLAGS_DEBUG:STRING=-fsanitize=address /Zi /Ob0 /Od /RTC1 -MD

//Flags used by the CXX compiler during RELEASE builds.
CMAKE_CXX_FLAGS_RELEASE:STRING=/O2 /Ob2 /DNDEBUG

CMAKE_CXX_FLAGS has been replaced by just -fsanitize=address! This has had the major effect of removing /EHsc which will break projects that were otherwise working fine.

For example, turning on ASAN with catch2 and trying to use SKIP (how I discovered this issue) will turn passing tests into failures with the error:

Catch will terminate because it needed to throw an exception.
The message was: Explicitly skipping tests during runtime requires exceptions

Which is very unexpected for just turning on ASAN...

I do see a seemingly random distributions of /EHsc being added in xmake-repo but this behavior effects all packages using package.tools.cmake so I've filed this here.

Expected Behavior

I would expect a consistent behavior and not have /EHsc (and other default flags) disappear when I enable ASAN or add other cxx flags.

Project Configuration

Any project that requires modification of the CXX flags will be affected but here's an example that exposed this issue to me:

NB: This is easiest to cause a crash with MSVC, other compilers like GCC don't cause CMake to write any default flags to get overriden.

xmake.lua

add_requires("catch2")

add_rules("mode.debug", "mode.release")

target("main")
    add_packages("catch2")
    set_kind("binary")
    add_files("main.cpp")

main.cpp

#include <catch2/catch_test_macros.hpp>

TEST_CASE("Skip") {
    SKIP("Shouldn't throw an error");
}

Configure with: xmake f --mode=releasedbg --policies=build.sanitizer.address --clean
Run: xmake run
Fails with output:

Randomness seeded to: 3570943976

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.exe is a Catch2 v3.10.0 host application.
Run with -? for options

-------------------------------------------------------------------------------
Skip
-------------------------------------------------------------------------------
main.cpp(3)
...............................................................................

main.cpp(4): SKIPPED:
explicitly with message:
  Shouldn't throw an error

Catch will terminate because it needed to throw an exception.
The message was: Explicitly skipping tests during runtime requires exceptions
error: execv(Z:\repos\misc\catch2-skip\build\windows\x64\releasedbg\main.exe ) failed(-1073740791)

Additional Information and Error Logs

It is not just ASAN that causes this to occur, anything that sets a cx or cxx flag in XMake will trigger this.
For example, instead of enabling ASAN you may also use add_requireconfs("catch2", {configs = {cxflags = "-DFOO"}}) to achieve the same result.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions