I've seen the same question was also asked on CMake's mailing list and never received an answer. So let me try to answer it here.
CMake doesn't have an official support for map files nor - if you add the necessary linker flags manually - to have more then one output of an executable target.
If you don't want to add it as an extra custom command/target step, you could link to an object file being part of you executable with OBJECT_OUTPUTS
source file property (works unfortunately only with Makefile generators).
I've successfully tested the following example with a GNU toolchain:
cmake_minimum_required(VERSION 2.6)
project(MapFileDep)
file(WRITE "main.cpp" "int main() { return 0; }")
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(
CMAKE_EXE_LINKER_FLAGS
"${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=output.map"
)
endif()
add_executable(${PROJECT_NAME} "main.cpp")
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set_source_files_properties(
"main.cpp"
PROPERTIES OBJECT_OUTPUTS "output.map"
)
endif()
Alternative
And here the more advanced alternative (also covering @jww's use case):
Add something that generates/creates/copies the output.map
file (when it's not there) and use LINK_DEPENDS
target property to make CMake aware of re-linking when output.map
changes:
add_custom_command(
OUTPUT "output.map"
COMMAND "${CMAKE_COMMAND}" -E touch "output.map"
)
add_executable(${PROJECT_NAME} "main.cpp" "output.map")
set_target_properties(
${PROJECT_NAME}
PROPERTIES
LINK_DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/output.map"
)