VS: Multiple link /MERGE:... flags not combined correctly
The following CMakeLists.txt
behaves improperly when using the Visual Studio generator:
# This file is automatically generated from cmake.toml - DO NOT EDIT
# See https://github.com/build-cpp/cmkr for more information
cmake_minimum_required(VERSION 3.15)
project(buddha
VERSION
0.1.0
)
# Target buddha
set(CMKR_TARGET buddha)
set(buddha_SOURCES "")
list(APPEND buddha_SOURCES
"src/main.cpp"
)
set(CMKR_SOURCES ${buddha_SOURCES})
add_executable(buddha)
if(buddha_SOURCES)
target_sources(buddha PRIVATE ${buddha_SOURCES})
endif()
get_directory_property(CMKR_VS_STARTUP_PROJECT DIRECTORY ${PROJECT_SOURCE_DIR} DEFINITION VS_STARTUP_PROJECT)
if(NOT CMKR_VS_STARTUP_PROJECT)
set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT buddha)
endif()
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${buddha_SOURCES})
target_include_directories(buddha PRIVATE
include
)
target_link_options(buddha PRIVATE
"SHELL:/MERGE:.pdata=.text"
"SHELL:/MERGE:.rdata=.text"
"SHELL:/MERGE:.data=.text"
"SHELL:/MERGE:.tls=.text"
"SHELL:/MERGE:.text=.buddha"
"SHELL:/SECTION:.buddha,RWE"
)
unset(CMKR_TARGET)
unset(CMKR_SOURCES)
When using cmake -G Ninja -B build -DCMAKE_BUILD_TYPE=Release
you get the following in the build.ninja
(as expected):
LINK_FLAGS = /machine:x64 /INCREMENTAL:NO /subsystem:console /MERGE:.pdata=.text /MERGE:.rdata=.text /MERGE:.data=.text /MERGE:.tls=.text /MERGE:.text=.buddha /SECTION:.buddha,RWE
However, when using cmake -G "Visual Studio 16 2019" -B build
you get the wrong linker flags:
/OUT:"D:\CodeBlocks\buddha\build-vs\Release\buddha.exe" /MANIFEST /NXCOMPAT /PDB:"D:/CodeBlocks/buddha/build-vs/Release/buddha.pdb" /DYNAMICBASE "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "comdlg32.lib" "advapi32.lib" /IMPLIB:"D:/CodeBlocks/buddha/build-vs/Release/buddha.lib" /MACHINE:X64 /INCREMENTAL:NO /PGD:"D:\CodeBlocks\buddha\build-vs\Release\buddha.pgd" /SUBSYSTEM:CONSOLE /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"buddha.dir\Release\buddha.exe.intermediate.manifest" /LTCGOUT:"buddha.dir\Release\buddha.iobj" /ERRORREPORT:PROMPT /MERGE:".text=.buddha" /NOLOGO /SECTION:".buddha,RWE" /TLBID:1
It looks like the Visual Studio generator is trying to generate an option in the vcxproj, but doesn't handle multiple /MERGE
commands:
<MergeSections>.text=.buddha</MergeSections>
I tried to figure out a way to make the <MergeSections>
merge multiple sections, but the only way I could find is to use the 'Additional options' to do this.
Edited by Brad King