7

When I uninstall an application using make uninstall it leaves over some empty directories which make install had created, e.g. such as /usr/share/foo/ and its subdirectories such as /usr/share/foo/applications, etc.

I found via Google, that automake's generated uninstall target does not automatically delete empty directories because it does not know if the the application owns the directories (e.g. it created it during make install), or borrowed it (e.g. the directory existed during make install).

Currently none of my make files has a definitive uninstall target, make implicitly seems to know which files it has to remove. How can I teach it to also remove the folders in question?

2 Answers 2

7

Here is the solution, I just had to register an uninstall-hook target like this, and then perform the necessary tasks, for removing the directories. In this example ${pkgdatadir} would expand to /usr/share/foo. The if-then is necesseary as otherwise the make distcheck target would fail.

uninstall-hook:
    if test -d ${pkgdatadir}/applications; then rmdir ${pkgdatadir}/applications; fi
    if test -d ${pkgdatadir}; then rmdir ${pkgdatadir}; fi

The uninstall-hook rule will be called after the uninstall target has run.

3
  • 1
    It's not clear where you registered the uninstall-hook. Did you append this segment to a Makefile? Please elaborate some more...
    – einpoklum
    Sep 26, 2015 at 16:44
  • 1
    Added to my Makefile.am file, the position does not matter, just add it somewhere at the end.
    – lanoxx
    Sep 27, 2015 at 19:59
  • 1
    I believe these should be $(DESTDIR)$(pkgdatadir), etc. I suspect this solution will not work when DESTDIR is non-empty. Oct 7, 2020 at 16:44
5

The above is excessively verbose. Here's a shorter version:

Register an uninstall-hook target, and then perform the necessary tasks, for removing the directories. In this example ${pkgdatadir} would expand to /usr/share/foo. The minus sign in front of rmdir tells make to ignore the return code (so that if the directory isn't empty, make won't fail) Note that the minus sign can be used in front of any make target command.

uninstall-hook:
    -rmdir ${pkgdatadir}/applications
    -rmdir ${pkgdatadir}

The uninstall-hook rule will be called after the uninstall target has run.

Here's more about the minus sign: What do @, - and + do as prefixes to recipe lines in Make?

2
  • Forgive my ignorance... Does this go in configure.ac or Makefile.am?
    – jww
    Nov 14, 2017 at 20:17
  • 1
    It goes in Makefile.am !
    – Linas
    Nov 16, 2017 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.