3

I need to make a Makefile to install a bash script into a user's bin directory, but I'm not sure what the best way of doing this would be. Should I be using a variable that accepts user input to specify their bin directory's location, or is there an environment variable for this (at least in most distros)?

1 Answers1

5

In general a user may not have a bin directory. A Makefile like this allows you to say make DESTDIR=/usr/local/bin install to install to /usr/local/bin, and a default to ~/bin

# DESTDIR is where the program should be installed
DESTDIR = $$HOME/bin
prog: a.c b.c
      ${CC} -o $@ $<
install: prog
      mkdir -p ${DESTDIR}
      cp $< ${DESTDIR}

this makes the program from a couple of C file. The install target creates the bin directory if it doesn't exist (the -p stops it erroring out if it does exist) and then copies the file. Note the $$ on the shell variables.

GNU make would allow an order only prerequisite to only create the directory if it didn't exist, but the time to run mkdir is likely small compared to the time to do the compiles.

icarus
  • 17,920