|
| 1 | +# SPDX-FileCopyrightText: 2012-2021 Istituto Italiano di Tecnologia (IIT) |
| 2 | +# SPDX-FileCopyrightText: 2008-2013 Kitware Inc. |
| 3 | +# SPDX-License-Identifier: BSD-3-Clause |
| 4 | + |
| 5 | +#[=======================================================================[.rst: |
| 6 | +AddUninstallTarget |
| 7 | +------------------ |
| 8 | +
|
| 9 | +Add the "uninstall" target for your project:: |
| 10 | +
|
| 11 | + include(AddUninstallTarget) |
| 12 | +
|
| 13 | +
|
| 14 | +will create a file ``cmake_uninstall.cmake`` in the build directory and add a |
| 15 | +custom target ``uninstall`` (or ``UNINSTALL`` on Visual Studio and Xcode) that |
| 16 | +will remove the files installed by your package (using |
| 17 | +``install_manifest.txt``). |
| 18 | +See also |
| 19 | +https://gitlab.kitware.com/cmake/community/wikis/FAQ#can-i-do-make-uninstall-with-cmake |
| 20 | +
|
| 21 | +The :module:`AddUninstallTarget` module must be included in your main |
| 22 | +``CMakeLists.txt``. If included in a subdirectory it does nothing. |
| 23 | +This allows you to use it safely in your main ``CMakeLists.txt`` and include |
| 24 | +your project using ``add_subdirectory`` (for example when using it with |
| 25 | +:cmake:module:`FetchContent`). |
| 26 | +
|
| 27 | +If the ``uninstall`` target already exists, the module does nothing. |
| 28 | +#]=======================================================================] |
| 29 | + |
| 30 | + |
| 31 | +# AddUninstallTarget works only when included in the main CMakeLists.txt |
| 32 | +if(NOT "${CMAKE_CURRENT_BINARY_DIR}" STREQUAL "${CMAKE_BINARY_DIR}") |
| 33 | + return() |
| 34 | +endif() |
| 35 | + |
| 36 | +# The name of the target is uppercase in MSVC and Xcode (for coherence with the |
| 37 | +# other standard targets) |
| 38 | +if("${CMAKE_GENERATOR}" MATCHES "^(Visual Studio|Xcode)") |
| 39 | + set(_uninstall "UNINSTALL") |
| 40 | +else() |
| 41 | + set(_uninstall "uninstall") |
| 42 | +endif() |
| 43 | + |
| 44 | +# If target is already defined don't do anything |
| 45 | +if(TARGET ${_uninstall}) |
| 46 | + return() |
| 47 | +endif() |
| 48 | + |
| 49 | + |
| 50 | +set(_filename cmake_uninstall.cmake) |
| 51 | + |
| 52 | +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${_filename}" |
| 53 | +"if(NOT EXISTS \"${CMAKE_CURRENT_BINARY_DIR}/install_manifest.txt\") |
| 54 | + message(WARNING \"Cannot find install manifest: \\\"${CMAKE_CURRENT_BINARY_DIR}/install_manifest.txt\\\"\") |
| 55 | + return() |
| 56 | +endif() |
| 57 | +
|
| 58 | +file(READ \"${CMAKE_CURRENT_BINARY_DIR}/install_manifest.txt\" files) |
| 59 | +string(STRIP \"\${files}\" files) |
| 60 | +string(REGEX REPLACE \"\\n\" \";\" files \"\${files}\") |
| 61 | +list(REVERSE files) |
| 62 | +foreach(file \${files}) |
| 63 | + if(IS_SYMLINK \"\$ENV{DESTDIR}\${file}\" OR EXISTS \"\$ENV{DESTDIR}\${file}\") |
| 64 | + message(STATUS \"Uninstalling: \$ENV{DESTDIR}\${file}\") |
| 65 | + execute_process( |
| 66 | + COMMAND \${CMAKE_COMMAND} -E remove \"\$ENV{DESTDIR}\${file}\" |
| 67 | + OUTPUT_VARIABLE rm_out |
| 68 | + RESULT_VARIABLE rm_retval) |
| 69 | + if(NOT \"\${rm_retval}\" EQUAL 0) |
| 70 | + message(FATAL_ERROR \"Problem when removing \\\"\$ENV{DESTDIR}\${file}\\\"\") |
| 71 | + endif() |
| 72 | + else() |
| 73 | + message(STATUS \"Not-found: \$ENV{DESTDIR}\${file}\") |
| 74 | + endif() |
| 75 | +endforeach(file) |
| 76 | +") |
| 77 | + |
| 78 | +set(_desc "Uninstall the project...") |
| 79 | +if(CMAKE_GENERATOR STREQUAL "Unix Makefiles") |
| 80 | + set(_comment COMMAND \$\(CMAKE_COMMAND\) -E cmake_echo_color --switch=$\(COLOR\) --cyan "${_desc}") |
| 81 | +else() |
| 82 | + set(_comment COMMENT "${_desc}") |
| 83 | +endif() |
| 84 | +add_custom_target(${_uninstall} |
| 85 | + ${_comment} |
| 86 | + COMMAND ${CMAKE_COMMAND} -P ${_filename} |
| 87 | + USES_TERMINAL |
| 88 | + BYPRODUCTS uninstall_byproduct |
| 89 | + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") |
| 90 | +set_property(SOURCE uninstall_byproduct PROPERTY SYMBOLIC 1) |
| 91 | + |
| 92 | +set_property(TARGET ${_uninstall} PROPERTY FOLDER "CMakePredefinedTargets") |
0 commit comments