CMake support "make uninstall"?

CmakeUninstallation

Cmake Problem Overview


I am trying to find some sudo-free solution to enable my users install and unistall my application. Using

set(CMAKE_INSTALL_PREFIX "$ENV{HOME}/opt/${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}/")
SET(CMAKE_INSTALL_RPATH "$ENV{HOME}/${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}/")

I can direct the files to the user's home directory, and

make install

works fine. With reference to https://stackoverflow.com/questions/1439950/whats-the-opposite-of-make-install-ie-how-do-you-uninstall-a-library-in-lin I did not find any idea, which is sudo-free and is not complex for a non-system-admin person.

  1. Is anyhow make uninstall supported by CMake?

  2. My uninstall is quite simple: all files go in a subdirectory of the user's home. In principle, removed that new subdirectory could solve the problem. Has make install, with parameters above, any side effect, or I can write in my user's guide that the newly produced subdirectory can be removed as 'uninstall'?

Cmake Solutions


Solution 1 - Cmake

If you want to add an uninstall target you can take a look to the official CMake FAQ at: > https://gitlab.kitware.com/cmake/community/wikis/FAQ#can-i-do-make-uninstall-with-cmake

If you just want a quick way to uninstall all files, just run:

xargs rm < install_manifest.txt

install_manifest.txt file is created when you run make install.

Solution 2 - Cmake

No there is not. See in the FAQ from CMake wiki:

> By default, CMake does not provide the "make uninstall" target, so you > cannot do this. We do not want "make uninstall" to remove useful files > from the system. > >If you want an "uninstall" target in your project, > then nobody prevents you from providing one. You need to delete the > files listed in install_manifest.txt file. [followed by some example code]

Solution 3 - Cmake

Remove files and folders (empty only) added by make install from a cmake project:

cat install_manifest.txt | sudo xargs rm
cat install_manifest.txt | xargs -L1 dirname | sudo xargs rmdir -p

The second command will print a bunch of errors because it recursively deletes folders until it finds one that is not empty. I like seeing those errors to know which folders are left. If you want to hide these errors you can add --ignore-fail-on-non-empty to rmdir.

Solution 4 - Cmake

From the source folder:

  1. open install_manifest.txt (created by make install)

  2. remove all the directories/files listed

  3. remove any remaining files you missed:

    xargs rm < install_manifest.txt

  4. remove any hidden directories/files:

    $rm -rf ~/.packagename

Remove the source folder.

Solution 5 - Cmake

# make uninstall
add_custom_target("uninstall" COMMENT "Uninstall installed files")
add_custom_command(
    TARGET "uninstall"
    POST_BUILD
    COMMENT "Uninstall files with install_manifest.txt"
    COMMAND xargs rm -vf < install_manifest.txt || echo Nothing in
            install_manifest.txt to be uninstalled!
)

Add this to CMakeLists.txt, then an uninstall target is made by hand.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkatangView Question on Stackoverflow
Solution 1 - CmakeEmilio González MontañaView Answer on Stackoverflow
Solution 2 - Cmakeusr1234567View Answer on Stackoverflow
Solution 3 - CmakeqwertzguyView Answer on Stackoverflow
Solution 4 - CmakeV LiView Answer on Stackoverflow
Solution 5 - CmakeYan QiDongView Answer on Stackoverflow