Difference between revisions of "RecipeAddUninstallTarget"

From KitwarePublic
Jump to navigationJump to search
(Replace content with link to new CMake community wiki)
Line 1: Line 1:
This is a slight hack for adding an uninstall target.  It parses the install manifest and removes all of those same files.  If your program generates files at runtime,  this uninstaller won't even know they exist and will leave them.  There are a few assumptions made here, such as that your project has a folder called "cmake" that contains the cmake_uninstall.cmake file.  Credit is due to the original author but I've had this one for so long, I can't remember the origins.

Add this to your top-level CMakeLists.txt:
This page has moved [https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/recipe/AddUninstallTarget here].
########### Add uninstall target ###############
  "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake/cmake_uninstall.cmake")
Create this file as cmake_uninstall.cmake into your project's "cmake" folder:
IF(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt")
  MESSAGE(FATAL_ERROR "Cannot find install manifest: "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt"")
FILE(READ "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt" files)
STRING(REGEX REPLACE " " ";" files "${files}")
FOREACH(file ${files})
  MESSAGE(STATUS "Uninstalling "$ENV{DESTDIR}${file}"")
      "@CMAKE_COMMAND@" ARGS "-E remove "$ENV{DESTDIR}${file}""
      OUTPUT_VARIABLE rm_out
      RETURN_VALUE rm_retval
    IF(NOT "${rm_retval}" STREQUAL 0)
      MESSAGE(FATAL_ERROR "Problem when removing "$ENV{DESTDIR}${file}"")
    ENDIF(NOT "${rm_retval}" STREQUAL 0)
    MESSAGE(STATUS "File "$ENV{DESTDIR}${file}" does not exist.")
Now, you should be able to run "make install" to install your project and then "make uninstall" to remove the files.
Credit to the CMake mailing list archives for providing this solution.

Latest revision as of 15:41, 30 April 2018

The CMake community Wiki has moved to the Kitware GitLab Instance.

This page has moved here.