VS: Set the linker image version using the target's VERSION property

This matches the behavior of NMake, Ninja and older Visual Studio
generators.

Fixes: #13130
This commit is contained in:
Nikita Nemkin
2025-02-08 03:54:26 +05:00
parent 2e798a4137
commit e5fd973b14
4 changed files with 47 additions and 0 deletions
@@ -4590,6 +4590,13 @@ bool cmVisualStudio10TargetGenerator::ComputeLinkOptions(
}
linkOptions.AddFlag("ProgramDataBaseFile", pdb);
// Add image version
int major, minor;
this->GeneratorTarget->GetTargetVersion(major, minor);
if (major || minor) {
linkOptions.AddFlag("Version", cmStrCat(major, '.', minor));
}
// A Windows Runtime component uses internal .NET metadata,
// so does not have an import library.
if (this->GeneratorTarget->GetPropertyAsBool("VS_WINRT_COMPONENT") &&
@@ -49,6 +49,7 @@ run_cmake(VsSourceSettingsTool)
run_cmake(VsPlatformToolset)
run_cmake(VsControlFlowGuardLinkSetting)
run_cmake(VsToolOverride)
run_cmake(VsImageVersion)
run_cmake(VsWinRTByDefault)
@@ -0,0 +1,28 @@
macro(ensure_link_version projectFile expected)
if(NOT EXISTS "${projectFile}")
set(RunCMake_TEST_FAILED "Project file ${projectFile} does not exist.")
return()
endif()
file(STRINGS "${projectFile}" lines)
set(version "")
foreach(line IN LISTS lines)
if(line MATCHES "<Link>")
set(in_link TRUE)
elseif(line MATCHES "</Link>")
if(NOT version STREQUAL "${expected}")
set(RunCMake_TEST_FAILED "<Version> not found or incorrect: ${version} vs ${expected}")
return()
endif()
set(in_link FALSE)
set(version "")
elseif(in_link AND line MATCHES "<Version>([^<]+)</Version>")
set(version ${CMAKE_MATCH_1})
endif()
endforeach()
endmacro()
ensure_link_version("${RunCMake_TEST_BINARY_DIR}/app-C.vcxproj" 0.1)
ensure_link_version("${RunCMake_TEST_BINARY_DIR}/app-CXX.vcxproj" 1.0)
ensure_link_version("${RunCMake_TEST_BINARY_DIR}/lib-C.vcxproj" 65535.65535)
ensure_link_version("${RunCMake_TEST_BINARY_DIR}/lib-CXX.vcxproj" "")
@@ -0,0 +1,11 @@
enable_language(C CXX)
add_executable(app-C empty.c)
add_executable(app-CXX empty.cxx)
add_library(lib-C SHARED empty.c)
add_library(lib-CXX SHARED empty.cxx)
set_property(TARGET app-C PROPERTY VERSION 0.1)
set_property(TARGET app-CXX PROPERTY VERSION 1.0)
set_property(TARGET lib-C PROPERTY VERSION 65535.65535)
set_property(TARGET lib-CXX PROPERTY VERSION "")