Skip to content

Instantly share code, notes, and snippets.

@kbenzie
Last active November 1, 2019 14:57
Show Gist options
  • Save kbenzie/1db9e28659fdb1b86b58549b4115819b to your computer and use it in GitHub Desktop.
Save kbenzie/1db9e28659fdb1b86b58549b4115819b to your computer and use it in GitHub Desktop.
CMake Uninstall Module
# Uninstall.cmake adds an uninstall target to remove previously installed files,
# it had been adapted from the CMake FAQ at the link.
# https://gitlab.kitware.com/cmake/community/wikis/FAQ#can-i-do-make-uninstall-with-cmake
#
# Copy Uninstall.cmake to a directory on the CMAKE_MODULE_PATH, then include the
# module using: include(Uninstall)
message(STATUS "Uninstall target enabled")
file(WRITE ${CMAKE_BINARY_DIR}/cmake_uninstall.cmake "\
message(STATUS \"Uninstall configuration: \\\"${CMAKE_BUILD_TYPE}\\\"\")
set(InstallManifest \"${CMAKE_BINARY_DIR}/install_manifest.txt\")
if(NOT EXISTS $\{InstallManifest\})
message(FATAL_ERROR \"File not found: $\{InstallManifest\}\")
endif()
file(READ \"$\{InstallManifest\}\" InstalledFiles)
string(REGEX REPLACE \"\\n\" \";\" InstalledFiles \"$\{InstalledFiles\}\")
foreach(InstalledFile IN LISTS InstalledFiles)
set(InstalledFile \"$ENV\{DESTDIR\}$\{InstalledFile\}\")
if(IS_SYMLINK $\{InstalledFile\} OR EXISTS $\{InstalledFile\})
message(STATUS \"Uninstalling: $\{InstalledFile\}\")
execute_process(
COMMAND \"${CMAKE_COMMAND}\" -E remove \"$\{InstalledFile\}\"
RESULT_VARIABLE Result OUTPUT_VARIABLE Output ERROR_VARIABLE Error)
if(NOT Result EQUAL 0)
message(FATAL_ERROR \"$\{Error\}\")
endif()
else()
message(STATUS \"File not found: $\{InstalledFile\}\")
endif()
endforeach()
")
if(NOT TARGET uninstall)
add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/cmake_uninstall.cmake
COMMENT "Uninstall the project...")
endif()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment