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