6

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?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
lanoxx
  • 12,249
  • 13
  • 87
  • 142

2 Answers2

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.

lanoxx
  • 12,249
  • 13
  • 87
  • 142
  • 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 '15 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 '15 at 19:59
  • 1
    I believe these should be `$(DESTDIR)$(pkgdatadir)`, etc. I suspect this solution will not work when DESTDIR is non-empty. – William Pursell Oct 07 '20 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?

Community
  • 1
  • 1
Linas
  • 773
  • 6
  • 13