# Generic makefile for a library # Last edited on 2016-12-26 16:55:54 by stolfilocal ###################################################################### # To be included by the top-level Makefile in the library's directory. # The caller must define # # ${LIBNAME} name of library (WITHOUT the directory or the ".a") # # and may also define # # ${IGNORE} a list of ".h" or ".c" files that should not be compiled. # ${OTHER_I_FLAGS} additional -I flags for C compilation and dependency checks. # ${OTHER_C_FLAGS} additional flags to compile C source files. # Back-compatibility hack: OTHER_C_FLAGS := ${OTHER_C_FLAGS} ${EXTRA_C_FLAGS} include GENERIC.make ifneq "/${LIBNAME}" "/" # ---------------------------------------------------------------------- # Package components and directories # Library file name: LIBFILE = ${LIBNAME}.a # Get list of sources: HFILES := ${filter-out ${IGNORE}, ${wildcard *.h}} CFILES := ${filter-out ${IGNORE}, ${wildcard *.c}} # Derived object files: HOFILES := ${subst .h,.ho,${HFILES}} OFILES := ${subst .c,.o,${CFILES}} # Directory with test programs and runs thereof: TEST_DIR := tests # ---------------------------------------------------------------------- # Locations of imported packages # Location of X11 headers and libraries (including ICE): X11_INC_DIR := /usr/include/X11 # ---------------------------------------------------------------------- # Optional include directories # Get OpenGL libraries is requested: ifeq "/${USE_GL}" "/YES" GL_I_FLAGS := \ -I /usr/include/GL else GL_I_FLAGS := endif # Get X11 libraries and headers if requested: ifeq "/${USE_X11}" "/YES" X11_I_FLAGS := -I${X11_INC_DIR} else X11_I_FLAGS := endif #--------------------------------------------------------------------- # Default target for "make" with no target: all: build-lib #--------------------------------------------------------------------- # "make actions" performs the ${ACTIONS} locally: actions: ifeq "/${ACTIONS}" "/" @echo '$${ACTIONS} is empty' else ${MAKE} ${ACTIONS} endif #--------------------------------------------------------------------- # Directories to search for #include files: I_FLAGS := \ -I. \ ${GL_I_FLAGS} \ ${X11_I_FLAGS} \ ${OTHER_I_FLAGS} #--------------------------------------------------------------------- # "make depend" recreates the source dependency file ${DEPFILE}. DEPFILE := Deps.make depend: depend-lib depend-tests depend-lib: ${DEPFILE} ${DEPFILE}: ${HFILES} ${CFILES} @/bin/rm -f ${DEPFILE} @extract-ho-deps ${I_FLAGS} -I/usr/include \ ${HFILES} ${CFILES} \ | egrep -v ': /usr/include' \ > ${DEPFILE} depend-tests: if [[ -d ${TEST_DIR} ]]; then ( cd ${TEST_DIR} && ${MAKE} depend ) fi #--------------------------------------------------------------------- # "make build" and "make build-lib" make sure that the ${DEPFILE} # is up to date, and then recreate the object files from the source files # as necessary. "make build" and "make build-tests" also rebuild # the test programs. build: build-lib build-tests build-libs: build-lib build-lib: ${DEPFILE} ${MAKE} do-build-lib do-build-lib: ${LIBFILE} %.o: %.c ${CC} -c ${C_FLAGS} ${OTHER_C_FLAGS} ${I_FLAGS} $*.c %.ho: %.h ${CC} -o $*.ho -c ${C_FLAGS} ${OTHER_C_FLAGS} ${I_FLAGS} -x c $*.h \ || /bin/rm -f $*.ho ${LIBFILE}: ${HOFILES} ${OFILES} -rm -f $*.a ar crv $*.a ${OFILES} ranlib $*.a build-tests: ${LIBFILE} if [[ -d ${TEST_DIR} ]]; then ( cd ${TEST_DIR} && ${MAKE} build ) fi ifneq "/${wildcard ${DEPFILE}}" "/" # Include specific dependencies extracted by "make depend" include ${DEPFILE} endif #--------------------------------------------------------------------- # "make check" runs the tests in the ${TEST_DIR} subdirectory. check: if [[ -d ${TEST_DIR} ]]; then ( cd ${TEST_DIR} && ${MAKE} all ) fi #--------------------------------------------------------------------- # "make clean" deletes all derived files. clean: clean-lib clean-tests clean-lib: clean-tests -/bin/rm -f *.o *.ho *.a core ${MAKE} depend clean-tests: if [[ -d ${TEST_DIR} ]]; then ( cd ${TEST_DIR} && ${MAKE} clean ) fi endif # End of ${LIBNAME} section. ###################################################################### #--------------------------------------------------------------------- # "make debug" prints some debugging info: debug: @echo "LIBNAME = ${LIBNAME}"