|
1 |
| -#.rst: |
2 |
| -# AddUninstallTarget |
3 |
| -# ------------------ |
4 |
| -# |
5 |
| -# Add the "uninstall" target for your project:: |
6 |
| -# |
7 |
| -# include(AddUninstallTarget) |
8 |
| -# |
9 |
| -# |
10 |
| -# will create a file cmake_uninstall.cmake in the build directory and add a |
11 |
| -# custom target uninstall that will remove the files installed by your package |
12 |
| -# (using install_manifest.txt) |
13 |
| - |
14 |
| -#============================================================================= |
15 |
| -# Copyright 2008-2013 Kitware, Inc. |
16 |
| -# Copyright 2013 Istituto Italiano di Tecnologia (IIT) |
17 |
| -# Authors: Daniele E. Domenichelli <daniele.domenichelli@iit.it> |
18 |
| -# |
19 |
| -# Distributed under the OSI-approved BSD License (the "License"); |
20 |
| -# see accompanying file Copyright.txt for details. |
21 |
| -# |
22 |
| -# This software is distributed WITHOUT ANY WARRANTY; without even the |
23 |
| -# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
24 |
| -# See the License for more information. |
25 |
| -#============================================================================= |
26 |
| -# (To distribute this file outside of CMake, substitute the full |
27 |
| -# License text for the above reference.) |
28 |
| - |
29 |
| - |
30 |
| -if(DEFINED __ADD_UNINSTALL_TARGET_INCLUDED) |
| 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}") |
31 | 33 | return()
|
32 | 34 | endif()
|
33 |
| -set(__ADD_UNINSTALL_TARGET_INCLUDED TRUE) |
34 | 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() |
35 | 48 |
|
36 |
| -set(_filename ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake) |
37 | 49 |
|
38 |
| -file(WRITE ${_filename} |
| 50 | +set(_filename cmake_uninstall.cmake) |
| 51 | + |
| 52 | +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${_filename}" |
39 | 53 | "if(NOT EXISTS \"${CMAKE_CURRENT_BINARY_DIR}/install_manifest.txt\")
|
40 |
| - message(WARNING \"Cannot find install manifest: \\\"${CMAKE_CURRENT_BINARY_DIR}/install_manifest.txt\\\"\") |
41 |
| - return() |
| 54 | + message(WARNING \"Cannot find install manifest: \\\"${CMAKE_CURRENT_BINARY_DIR}/install_manifest.txt\\\"\") |
| 55 | + return() |
42 | 56 | endif()
|
43 | 57 |
|
44 | 58 | file(READ \"${CMAKE_CURRENT_BINARY_DIR}/install_manifest.txt\" files)
|
45 | 59 | string(STRIP \"\${files}\" files)
|
46 | 60 | string(REGEX REPLACE \"\\n\" \";\" files \"\${files}\")
|
47 | 61 | list(REVERSE files)
|
48 | 62 | foreach(file \${files})
|
| 63 | + if(IS_SYMLINK \"\$ENV{DESTDIR}\${file}\" OR EXISTS \"\$ENV{DESTDIR}\${file}\") |
49 | 64 | message(STATUS \"Uninstalling: \$ENV{DESTDIR}\${file}\")
|
50 |
| - if(EXISTS \"\$ENV{DESTDIR}\${file}\") |
51 |
| - execute_process( |
52 |
| - COMMAND \${CMAKE_COMMAND} -E remove \"\$ENV{DESTDIR}\${file}\" |
53 |
| - OUTPUT_VARIABLE rm_out |
54 |
| - RESULT_VARIABLE rm_retval) |
55 |
| - if(NOT \"\${rm_retval}\" EQUAL 0) |
56 |
| - message(FATAL_ERROR \"Problem when removing \\\"\$ENV{DESTDIR}\${file}\\\"\") |
57 |
| - endif() |
58 |
| - else() |
59 |
| - message(STATUS \"File \\\"\$ENV{DESTDIR}\${file}\\\" does not exist.\") |
| 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}\\\"\") |
60 | 71 | endif()
|
| 72 | + else() |
| 73 | + message(STATUS \"Not-found: \$ENV{DESTDIR}\${file}\") |
| 74 | + endif() |
61 | 75 | endforeach(file)
|
62 | 76 | ")
|
63 | 77 |
|
64 |
| -if("${CMAKE_GENERATOR}" MATCHES "^(Visual Studio|Xcode)") |
65 |
| - set(_uninstall "UNINSTALL") |
| 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}") |
66 | 81 | else()
|
67 |
| - set(_uninstall "uninstall") |
| 82 | + set(_comment COMMENT "${_desc}") |
68 | 83 | endif()
|
69 |
| -add_custom_target(${_uninstall} COMMAND "${CMAKE_COMMAND}" -P "${_filename}") |
| 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 | + |
70 | 92 | set_property(TARGET ${_uninstall} PROPERTY FOLDER "CMakePredefinedTargets")
|
0 commit comments