Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
Community
Community
  • Project overview
    • Project overview
    • Details
    • Activity
  • Analytics
    • Analytics
    • Value Stream
  • Wiki
    • Wiki
  • Members
    • Members
  • Activity
Collapse sidebar
  • CMake
  • CommunityCommunity
  • Wiki
    • Doc
    • Cmake
    • Recipe
  • AddUninstallTarget

Last edited by Kitware Robot Apr 27, 2018
Page history

AddUninstallTarget

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:

########### 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")

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"")
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)

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.


This page was initially populated by conversion from its original location in another wiki.

Clone repository
  • CMake Versions on Linux Distros
  • Contrib
  • Editing Guidelines
  • FAQ
  • Home
  • contrib
    • macros
      • AddCxxTest
      • CompareVersionStrings
      • CopyIfDifferent
      • CreateFinalFile
      • FilterOut
      • ForceAddFlags
      • GatherProjectFiles
      • GenerateProject
      • LibtoolFile
      • ListOperations
View All Pages