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

Fix build failures with MSVC. #248

Merged
merged 1 commit into from
Jan 28, 2024
Merged

Fix build failures with MSVC. #248

merged 1 commit into from
Jan 28, 2024

Conversation

Clownacy
Copy link
Contributor

@Clownacy Clownacy commented Jan 28, 2024

In my Doom source-port, I use WildMIDI as a CMake subproject. When running CMake for the first time, targeting MSVC, I do not set CMAKE_BUILD_TYPE as MSVC uses a multi-configuration generator, meaning that it should have no effect. However, if I make MSVC attempt to compile a Release build, it will fail, claiming that the WildMIDI binary could not be found. This seems to be caused by an odd combination of things:

WildMIDI detects that CMAKE_BUILD_TYPE is not set, forcefully sets it to 'Debug', and then sets CMAKE_CONFIGURATION_TYPES to match it, which presumably disables every other build type for multi-configuration generators.

This state-mutilation should be unnecessary, as the setting (or lack thereof) of CMAKE_BUILD_TYPE should have no effect on multi-configuration generators in the first place. Likewise, forcing CMAKE_BUILD_TYPE to 'Debug' in the absence of an explicit value is overbearing, as it is desirable to build software with no particular build type, as it causes the minimum amount of compiler flags to be automatically added by CMake. Notably, Arch Linux relies on this, opting instead to manually provide its own compiler flags for optimisation and hardening.

https://wiki.archlinux.org/title/CMake_package_guidelines#CMake_can_automatically_override_the_default_compiler_optimization_flag

Later on, CMAKE_RUNTIME_OUTPUT_DIRECTORY is redundantly set to the wildmidi_BINARY_DIR variable. This causes the
automatically-generated per-configuration CMAKE_RUNTIME_OUTPUT_DIRECTORY_[CONFIG] variables to become undefined.

Finally, in an apparent attempt to counteract the above bug, the per-configuration CMAKE_RUNTIME_OUTPUT_DIRECTORY_[CONFIG] variables are manually redefined, using CMAKE_CONFIGURATION_TYPES. Because CMAKE_CONFIGURATION_TYPES was forcefully set earlier, however, it only contains 'Debug', causing only the Debug configuration to have a valid output directory set. This causes all other configurations to fail to produce a binary, resulting in a build failure as the binary cannot be linked.

Personally, my rule-of-thumb is to keep CMake scripts as simple as possible and trust that CMake has sane defaults. Seeing redundant logic like this strikes me as a code smell, and this bug seems to validate that.

In my Doom source-port, I use WildMIDI as a CMake subproject. When
running CMake for the first time, targetting MSVC, I do not set
`CMAKE_BUILD_TYPE` as MSVC uses a multi-configuration generator,
meaning that it should have no effect. However, if I make MSVC
attempt to compile a Release build, it will fail, claiming that the
WildMIDI binary could not be found. This seems to be caused by an odd
combination of things:

WildMIDI detects that `CMAKE_BUILD_TYPE` is not set, forcefully sets
it to 'Debug', and then sets `CMAKE_CONFIGURATION_TYPES` to match it,
which presumably disables every other build type for
multi-configuration generators.

This state-mutilation should be unnecessary, as the setting (or lack
thereof) of `CMAKE_BUILD_TYPE` should have no effect on
multi-configuration generators in the first place. Likewise, forcing
`CMAKE_BUILD_TYPE` to 'Debug' in the absense of an explicit value is
overbearing, as it is desirable to build software with no particular
build type, as it causes the minimum amount of compiler flags to be
automatically added by CMake. Notably, Arch Linux relies on this,
opting instead to manually provide its own compiler flags for
optimisations and hardening.

https://wiki.archlinux.org/title/CMake_package_guidelines#CMake_can_automatically_override_the_default_compiler_optimization_flag

Later on, `CMAKE_RUNTIME_OUTPUT_DIRECTORY` is redundantly set to the
`wildmidi_BINARY_DIR` variable. This causes the
automatically-generated per-configuration
`CMAKE_RUNTIME_OUTPUT_DIRECTORY_[CONFIG]` variables to become
undefined.

Finally, in an apparent attempt to counteract the above bug, the
per-configuration `CMAKE_RUNTIME_OUTPUT_DIRECTORY_[CONFIG]` variables
are manually redefined, using `CMAKE_CONFIGURATION_TYPES`. Because
`CMAKE_CONFIGURATION_TYPES` was forcefully set earlier, however, it
only contains 'Debug', causing only the Debug configuration to have a
valid output directory set. This causes all other configurations to
fail to produce a binary, resulting in a build failure as the binary
cannot be linked.

Personally, my rule-of-thumb is to keep CMake scripts as simple as
possible and trust that CMake has sane defaults. Seeing redundant
logic like this strikes me as a code smell, and this bug seems to
validate that.
@sezero sezero merged commit 7b59371 into Mindwerks:master Jan 28, 2024
4 checks passed
@sezero
Copy link
Contributor

sezero commented Jan 28, 2024

Thanks

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.

2 participants