72

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 What's the opposite of 'make install', ie. how do you uninstall a library in Linux? 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'?

Community
  • 1
  • 1
katang
  • 2,246
  • 3
  • 19
  • 42

5 Answers5

126

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.

John
  • 1,222
  • 1
  • 15
  • 20
  • 8
    But this only removes files not directories. That's ok if you only have headers and libraries but if you install a python package, you have to create directories like `/home/user/.local/miniconda3/envs/galario3/lib/python3.6/site-packages/galario/`. the simple `xargs ...` does not delete them so you have to manually add the logic, for example in an `uninstall` target – Fred Schoen Sep 29 '17 at 11:00
  • Thanks for the tip for a way to get around one of the many glaring stupidities of cmake. An uninstall target it very important. – Edward Hartnett Feb 18 '19 at 14:47
32

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]

usr1234567
  • 19,156
  • 14
  • 105
  • 120
  • 3
    I am glad to see that there are people who belive that if one installs a file during the development, it shall stay there forever. Thanks for the hint, I will use the example code. BTW: I think you also answered my #2 question: simply removing the directory containing the files will have no side effect. – katang Jan 04 '17 at 20:14
24

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.

qwertzguy
  • 12,947
  • 7
  • 60
  • 63
  • 2
    Seems like xargs in the first command will not [handle spaces](https://stackoverflow.com/questions/16758525/how-can-i-make-xargs-handle-filenames-that-contain-spaces) in file names correctly. – Xunie May 04 '18 at 01:01
  • Right, it would need some tweaking to support that. But at least the good thing is it will just error out, and won't delete bunch of unrelated files because of the space character. – qwertzguy May 05 '18 at 02:46
  • 2
    **Definitely not!** It will NOT error out if the path actually exists. Example: If the working directory is `~` and xargs is passed `/tmp/school work`, this is interpreted as both `/tmp/school` and `work`. Have fun recovering `~/work`! -- `rm` might halt if it finds out it's a directory, though. – Xunie May 06 '18 at 15:38
  • 4
    `rm` _will_ not delete it if it's a directory. And `rmdir` _will_ only delete if it's an empty directory. (Unless you made some alias to those commands of course). So my comment still stands. The only thing it could delete is one single file that matches that exact name in between space characters. – qwertzguy May 06 '18 at 20:53
  • 5
    AFAIK using `xargs -I "{}" -- rm -- '{}'` should solve the whitespace problem – JepZ Feb 15 '19 at 13:09
  • this seems to work also on ubuntu 18 `cat install_manifest.txt | xargs -L1 dirname | sudo xargs rm -rv` – briancollins081 Jan 22 '21 at 06:52
8

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.

V Li
  • 106
  • 1
  • 4
1
# 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.

Yan QiDong
  • 2,744
  • 1
  • 18
  • 25