RecipeAddUninstallTarget: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
(New page: 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 wo...)
 
(Replace content with link to new CMake community wiki)
 
(2 intermediate revisions by one other user not shown)
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.
{{CMake/Template/Moved}}
Add this to your top-level CMakeLists.txt:
<pre>########### Add uninstall target ###############
CONFIGURE_FILE(
  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in"
  "${CMAKE_CURRENT_BINARY_DIR}/cmake/cmake_uninstall.cmake"
  IMMEDIATE @ONLY)
ADD_CUSTOM_TARGET(uninstall
  "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake/cmake_uninstall.cmake")
</pre>


Create this file as cmake_uninstall.cmake into your project's "cmake" folder:
This page has moved [https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/recipe/AddUninstallTarget here].
<pre>IF(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt")
  MESSAGE(FATAL_ERROR "Cannot find install manifest: "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt"")
ENDIF(NOT EXISTS "@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}"")
  IF(EXISTS "$ENV{DESTDIR}${file}")
    EXEC_PROGRAM(
      "@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)
  ELSE(EXISTS "$ENV{DESTDIR}${file}")
    MESSAGE(STATUS "File "$ENV{DESTDIR}${file}" does not exist.")
  ENDIF(EXISTS "$ENV{DESTDIR}${file}")
ENDFOREACH(file)
</pre>
 
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.