[CMake] Problem with custom target dependencies on VS2010

Ben Medina ben.medina at gmail.com
Tue Oct 26 13:40:55 EDT 2010


Hi all,

In our builds, we use custom targets to copy library headers to an
include directory from which clients of the library then #include
from. This works for VS2008 but is broken in VS2010 (using CMake 2.8.3
rc3).

Here is a small example:

-------------------------------
CMakeLists.txt:
-------------------------------

cmake_minimum_required(VERSION 2.8)
project(header-copy-test)

set (header_destination_dir ${CMAKE_BINARY_DIR}/include)
include_directories (${header_destination_dir})

set (headers_to_copy
    ${CMAKE_SOURCE_DIR}/headers/header.h
    )

# Create a target responsible for:
# - creating the include directory
# - copying the header into that directory
add_custom_target (
    ${PROJECT_NAME}_headers ALL
    DEPENDS ${header_destination_dir} ${headers_to_copy}
    SOURCES ${headers_to_copy}
    )
add_custom_command(
    OUTPUT ${header_destination_dir}
    COMMAND ${CMAKE_COMMAND} -E make_directory ${header_destination_dir}
    VERBATIM
    )
foreach (h ${headers_to_copy})
    add_custom_command (
        TARGET ${PROJECT_NAME}_headers
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different "${h}"
"${header_destination_dir}/"
        VERBATIM
        )
endforeach ()

# Now add an executable, and make it depend on the custom target above.
add_executable(
    ${PROJECT_NAME}
    main.cpp
    )
add_dependencies (${PROJECT_NAME} ${PROJECT_NAME}_headers)

-------------------------------
main.cpp
-------------------------------
#include "header.h"
int main() {}

-------------------------------
headers/header.h
-------------------------------
#error

Here are the steps to reproduce:
1. Use the VS2010 generator to generate the project, and open it in VS2010.
2. Right-click on the header-copy-test project in Solution Explorer,
and build. You should get a failure due to the #error in header.h.
3. Edit the original header.h (NOT the copied header) to remove the
#error, then repeat step 2. The corrected header should have been
copied, but it wasn't, leading to the same build error.

As I mentioned, this works as expected in VS2008. Am I doing something
wrong, or is this a bug in CMake or a limitation in VS2010 support?

Another issue is that I specified the headers as SOURCES in the custom
target, but they don't show up in the IDE.

Thanks,
Ben


More information about the CMake mailing list