Unexpected behaviour when using MAIN_DEPENDENCY
The documentation says: Specify the primary input source file to the command. This is treated just like any value given to the DEPENDS option but also suggests to Visual Studio generators where to hang the custom command. At most one custom command may specify a given source file as its main dependency.
Then the following should work:
cmake_minimum_required ( VERSION 3.6 )
project ( bug CXX )
add_custom_command ( OUTPUT main_copy.cpp
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_SOURCE_DIR}/main.cpp main_copy.cpp
MAIN_DEPENDENCY ${CMAKE_SOURCE_DIR}/main.cpp
)
add_executable ( xyz
main.cpp
)
The main.cpp file just contains:
int main ()
{
return 0;
}
Output when trying to build:
D:\CMake-Bugs\1\build>cmake -G "NMake Makefiles" ..
-- The CXX compiler identification is MSVC 19.0.24215.1
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/CMake-Bugs/1/build
D:\CMake-Bugs\1\build>nmake
Microsoft (R) Program Maintenance Utility Version 14.00.24210.0
Copyright (C) Microsoft Corporation. All rights reserved.
[ 50%] Generating main_copy.cpp
Scanning dependencies of target xyz
[100%] Linking CXX executable xyz.exe
LINK : warning LNK4001: no object files specified; libraries used
[...]
It is totally unexpected that main.cpp is not added to xyz target. No error or warning message at cmake time. The things works when using DEPENDS instead of MAIN_DEPENDENCY, so the statement in the documentation is just plain wrong.