Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSVC: Enable Edit & Continue in debug builds #7702

Merged
merged 1 commit into from
Feb 4, 2025

Conversation

obligaron
Copy link
Contributor

PR enables Edit & Continue support for MSVC, see https://learn.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio#edit-and-continue-for-cmake-projects.

This avoids the need to restart a debug session. This is especially useful when debugging multiplayer sessions.

I wasn't sure if we should add a CMake option for this, and if so, whether or not to enable it by default.

@obligaron obligaron marked this pull request as draft February 2, 2025 20:56
CMakeLists.txt Outdated
@@ -282,6 +282,11 @@ endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
# u8path() function is deprecated but there is no sensible alternative and it might even get un-deprecated.
add_definitions(-D_SILENCE_CXX20_U8PATH_DEPRECATION_WARNING)
if(CMAKE_BUILD_TYPE STREQUAL "Debug" AND ${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.25")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think CMAKE_BUILD_TYPE works properly in multi-config IDE such as Visual Studio, so you need to use a $<CONFIG:Debug> generator expression (like in the example in CMake documentation).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to

  if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.25")
    # This enables Edit & Continue support, see https://learn.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio#edit-and-continue-for-cmake-projects
    set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<$<CONFIG:Debug>:EditAndContinue>") # Sets /ZI compiler option, see https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_DEBUG_INFORMATION_FORMAT.html
    add_link_options("$<$<CONFIG:Debug>:/INCREMENTAL>")
  endif()

and removed the CMAKE_BUILD_TYPE check.
Do I need an additional protection for msvc? (as in your example above)

Thanks for the help glebm. ❤️

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I need an additional protection for msvc? (as in your example above)

Not according to cmake documentation:

The value is ignored on compilers not targeting the MSVC ABI

However, it does require setting CMP0141 to NEW

This variable has effect only when policy CMP0141 is set to NEW

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the CMP0141 policy in the beginning of the CMake file, where we also set other policies to NEW.

Another question:
We also use CMAKE_BUILD_TYPE STREQUAL "Debug" in

devilutionX/CMakeLists.txt

Lines 197 to 202 in 5ed7ca9

if(MSVC AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug" AND NOT DISABLE_LTO)
# Work around MSVC + CMake bug when LTO is enabled.
# See https://github.com/diasurgical/devilutionX/issues/3778
# and https://gitlab.kitware.com/cmake/cmake/-/issues/23035
set(BUILD_TESTING OFF)
endif()

should this also be changed (in another PR)?

Copy link
Collaborator

@glebm glebm Feb 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The add_link_options call is already inside if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") so it should be fine.

Copy link
Collaborator

@glebm glebm Feb 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also use CMAKE_BUILD_TYPE STREQUAL "Debug"

I'm not sure how to handle that one or whether it even works as expected.
It's a bit tricky because BUILD_TESTING is used at configure-time to decide whether to define the test targets.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it, but it didn't work for me. 😢
But that is not directly related to this PR.

I did some reconfiguring and rebuilding with this PR and it looks like it works. Do you think the PR is ok this way? 🙂

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine but while I know a bit about CMake I don't use Visual Studio at all, so let's get @StephenCWills or someone else who uses VS to also have a look.

@obligaron obligaron marked this pull request as ready for review February 2, 2025 21:51
Copy link
Member

@StephenCWills StephenCWills left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that developers will get the warning that both /Zi and /ZI are set unless they use Delete Cache and Reconfigure because /Zi has already been added to CMAKE_C(XX)_FLAGS_DEBUG in CMakeCache.txt. There's not really anything we can do about this, but we can explain it for anyone who asks.

I've also read that the enc_temp_folder may actually be somewhat annoying when using Find in Files. I suppose if anyone has any issues with this, we can revisit making this into an option.

I don't think we need to add /INCREMENTAL via add_link_options() because CMake already adds it to Debug and RelWithDebInfo build types. But it doesn't hurt anything, and Microsoft docs suggested it so maybe it's necessary in older CMake versions. 🤷

@StephenCWills StephenCWills merged commit a3eea0f into diasurgical:master Feb 4, 2025
23 checks passed
@obligaron obligaron deleted the editandcontinue branch February 4, 2025 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants