When distributing a custom utility I'll typically write a makefile that installs binaries to /usr/local
by default
PREFIX ?= /usr/local
MANPREFIX ?= ${PREFIX}/man
install:
install utility ${PREFIX}/bin/
install utility.1 ${MANPREFIX}/man1/
The path to local binaries is fairly standard across different platforms, but the man path is not
- Linux:
/usr/local/share/man
or/usr/local/man
- MacOS –
/usr/local/share/man
- BSD:
/usr/local/man
Is there a portable way to write this makefile? It seems unreasonable to expect users to set MANPREFIX to the correct path before installing.
EDIT:
BSD make support assignment of a shell command using !=
, which could be used to test if a path exists
SHARE != [ -d ${PREFIX}/share/man ] && echo /share || true
MANPREFIX ?= ${PREFIX}${SHARE}/man
This is a new feature for GNU make, so it's likely not portable yet. This works in GNU make, but not BSD make:
MANPREFIX ?= $(shell [ -d ${PREFIX}/share/man ] && echo ${PREFIX}/share/man || echo ${PREFIX}/man)
Best Answer
The textbook answer is to use autoconf. Finding the right installation directories on every platform is part of its job.
Autoconf isn't perfect, but it's often better than reinventing the wheel.