Fixed bug 3866 - CMake error when trying to make the 'uninstall' target when it already exists
authorSam Lantinga <slouken@libsdl.org>
Thu, 12 Oct 2017 08:47:02 -0700
changeset 11606a54e7ade4382
parent 11605 b914622c734e
child 11607 db7ee6a1ba6a
Fixed bug 3866 - CMake error when trying to make the 'uninstall' target when it already exists

Steve Robinson

In my project, the 'uninstall' target is already created by the glew library. I get this error when SDL2 tries to create it:

CMake Error at _build/3rdparty/SDL2/SDL2-2.0.6/CMakeLists.txt:1816 (add_custom_target):
add_custom_target cannot create target "uninstall" because another target
with the same name already exists. The existing target is a custom target
created in source directory
"D:/Code/sdl2-tutorial/_build/3rdparty/glew/glew-2.1.0/build/cmake". See
documentation for policy CMP0002 for more details.

To fix it, go to the bottom of the SDL2 CMakeLists.txt file. Add an if statement to check for the existence of the target before creating it. The end result looks like this:

if(NOT TARGET uninstall)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
IMMEDIATE @ONLY)

add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()

This is how the glew library deals with this possibility in their CMakeLists.txt file.
CMakeLists.txt
     1.1 --- a/CMakeLists.txt	Thu Oct 12 08:44:45 2017 -0700
     1.2 +++ b/CMakeLists.txt	Thu Oct 12 08:47:02 2017 -0700
     1.3 @@ -1808,10 +1808,12 @@
     1.4  
     1.5  ##### Uninstall target #####
     1.6  
     1.7 -configure_file(
     1.8 -    "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
     1.9 -    "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
    1.10 -    IMMEDIATE @ONLY)
    1.11 +if(NOT TARGET uninstall)
    1.12 +  configure_file(
    1.13 +      "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
    1.14 +      "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
    1.15 +      IMMEDIATE @ONLY)
    1.16  
    1.17 -add_custom_target(uninstall
    1.18 -    COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
    1.19 +  add_custom_target(uninstall
    1.20 +      COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
    1.21 +endif()