############################################################################ # # Another Programmer's Editor Makefile Template # # This is a template Makefile for small to medium projects. # It is meant to serve as a starting point for creating a portable # Makefile, suitable for use under ports systems like *BSD ports, # MacPorts, Portage, etc. It contains examples of most # Makefile components you will need. # # For most small projects, you should be able to create a usable # Makefile from this template by assigning the appropriate values # to variables like BIN1, LIB1, etc., and removing everything you # don't need. # # If you have a very complex project that requires more configurability # than this, consider using a Makefile generator such as GNU autotools, # or cmake. # # Variables that are conditionally assigned (with ?=) can be overridden # on the command line as follows: # # make VAR=value # # They can also inheret values from parent Makefiles (as in *BSD ports). # This allows different systems to use the Makefile without modifications. # For example, MacPorts installs to /opt/local instead of /usr/local, # and hence might use the following: # # make PREFIX=/opt/local # # Lastly, they can be overridden by the environment. I like to add -Wall # to my CFLAGS for development, without having to put it in the Makefile, # since it's a gcc specific flag. Hence, I have the following in my # .cshrc on gcc-based systems (BSD, Linux, Mac OS X): # # setenv CFLAGS "-O -Wall -pipe" # # Different systems may also use different compilers and keep libraries in # different locations: # # make CC=gcc CFLAGS=-O2 LFLAGS1="-L/usr/X11R6 -lX11" # # Variables can also be appended in the Makefile (with +=), so that # flags specified on the command line can be combined with those in # the Makefile. # # Author: Jason W. Bacon ############################################################################ ############################################################################ # Files to be installed by make. BIN1 = program1 BIN2 = program2 BINS = ${BIN1} ${BIN2} LIB1 = library1.so LIB2 = library2.a LIBS = ${LIB1} ${LIB2} HEADERS = MAN1 = MAN3 = SCRIPTS = ############################################################################ # List object files that comprise BIN1, BIN2, LIB1, LIB2, etc. OBJS1 = OBJS2 = ############################################################################ # Compile, link, and install options # Override from the command line with "make VAR=value" # or by setting in the parent Makefile. # Install in /usr/local, unless defined by the parent Makefile, the # environment, or a command line option such as PREFIX=/opt/local. PREFIX ?= /usr/local MANPREFIX ?= ${PREFIX} # Where to find local libraries and headers. For MacPorts, override # with LOCALBASE=/opt/local. LOCALBASE ?= /usr/local ############################################################################ # Build flags # Override with CC=gcc, CC=icc, etc. # Do not add non-portable options (such as -Wall) using += # Portable defaults. Can be overridden by mk.conf or command line. CC ?= cc CFLAGS ?= -O INCLUDES+= -I${LOCALBASE}/include CFLAGS += ${INCLUDES} LFLAGS1 += -L${LOCALBASE}/lib LFLAGS2 += -L${LOCALBASE}/lib ############################################################################ # Assume first command in PATH. Override with full pathnames if necessary. # E.g. make INSTALL=/usr/local/bin/ginstall # Do not place flags here (e.g. RM = rm -f). Just provide the command # and let usages dictate the flags. INSTALL ?= install LN ?= ln RM ?= rm AR ?= ar ############################################################################ # Standard targets required by ports all: ${BINS} ${LIBS} # Link rules ${BIN1}: ${OBJS1} ${CC} -o ${BIN1} ${OBJS1} ${LFLAGS1} ${BIN2}: ${OBJS2} ${CC} -o ${BIN2} ${OBJS2} ${LFLAGS2} # Static library ${LIB1}: ${OBJS1} ${AR} r ${LIB1} ${OBJS1} # Dynamic library ${LIB2}: ${OBJS1} ${CC} -shared -o ${LIB2} ${OBJS1} ############################################################################ # Include dependencies generated by "make depend", if they exist. # These rules explicitly list dependencies for each object file. # See "depend" target below. If Makefile.depend does not exist, use # generic source compile rules. These have some limitations, so you # may prefer to create explicit rules for each target file. This can # be done automatically using "cpp -M" or "cpp -MM". Run "man cpp" # for more information, or see the "depend" target below. # Rules generated by "make depend" include Makefile.depend # Generic rules %.o: %.c Makefile ${HEADERS} ${CC} -c ${CFLAGS} $< ############################################################################ # Self-generate dependencies the old-fashioned way depend: rm -f Makefile.depend for file in *.c; do \ ${CC} ${INCLUDES} -MM $${file} >> Makefile.depend; \ echo -e "\t\$${CC} -c \$${CFLAGS} $${file}" >> Makefile.depend; \ echo "" >> Makefile.depend; \ done ############################################################################ # Generate a header containing prototypes for C files. Requires # the cproto command, which is freely available on the WEB. protos: (cproto ${INCLUDES} *.c > temp_protos.h && mv -f temp_protos.h protos.h) ############################################################################ # Remove generated files (objs and nroff output from man pages) clean: rm -f ${OBJS} ${BINS} ${LIBS} *.nr # Keep backup files during normal clean, but provide an option to remove them realclean: clean rm -f .*.bak *.bak *.BAK *.gmon ############################################################################ # Install all target files (binaries, libraries, docs, etc.) install: all ${MKDIR} -p ${PREFIX}/bin ${PREFIX}/lib ${PREFIX}/include \ ${PREFIX}/man/man1 @for file in ${BINS} ; do \ ${INSTALL} -s -c -m 0555 $${file} ${PREFIX}/bin; \ done @for file in ${SCRIPTS} ; do \ ${INSTALL} -c -m 0555 $${file} ${PREFIX}/bin; \ done @for file in ${HEADERS} ; do \ ${INSTALL} -c -m 0444 $${file} ${PREFIX}/include; \ done @for file in ${LIBS} ; do \ ${INSTALL} -c -m 0444 $${file} ${PREFIX}/lib; \ done @for file in ${MAN1} ; do \ ${INSTALL} -c -m 0444 $${file} ${MANPREFIX}/man/man1; \ done @for file in ${MAN3} ; do \ ${INSTALL} -c -m 0444 $${file} ${MANPREFIX}/man/man3; \ done ############################################################################ # Remove all installed files uninstall: @for file in ${BINS} ; do \ ${RM} ${PREFIX}/bin/$${file}; \ done @for file in ${SCRIPTS} ; do \ ${RM} ${PREFIX}/bin/$${file}; \ done @for file in ${HEADERS} ; do \ ${RM} ${PREFIX}/include/$${file}; \ done @for file in ${LIBS} ; do \ ${RM} ${PREFIX}/lib/$${file}; \ done @for file in ${MAN1} ; do \ ${RM} ${MANPREFIX}/man/man1/$${file}; \ done @for file in ${MAN3} ; do \ ${RM} ${MANPREFIX}/man/man3/$${file}; \ done