Friday, September 2, 2022

[SOLVED] CMake support "make uninstall"?

Issue

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'?


Solution

One solution is to use packaging with CPack. That will create a package that can be installed/uninstalled by your package manager. This works great for linux distros, but I'm not sure about windows. There are a few generators for windows, but I'm not sure if any of them produce un-installers.

Here's a basic example of creating a debian package:

$ touch file
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.0)

install(FILES file DESTINATION etc)

set(CPACK_PACKAGE_NAME foo)
set(CPACK_PACKAGE_CONTACT "me <[email protected]>")
set(CPACK_GENERATOR DEB)
include(CPack)
$ cmake .
$ cpack

Then instead of make install DESTDIR=/usr/local use sudo dpkg -i foo-0.1.1-Linux.deb.

To uninstall use sudo dpkg -P foo or sudo apt purge foo.

The advantage of using a package manager over make install are numerous. Here are a few:

  • If you lose the source code, you can still uninstall the software.
  • If you dpkg -S /etc/foo, it will tell you which package "owns" this file.
  • If you want to install a new version of the software, you won't need to manually uninstall the previous version. It's all automatic.
  • You can publish the package so others can install it.
  • If your package deploys a file that is also owned by another package, it will fail to install. That's good because it prevents you from accidentally destroying other packages.


Answered By - Stewart
Answer Checked By - Candace Johnson (WPSolving Volunteer)