# Generic Makefile for one or more executable programs that test a library # Last edited on 2016-12-26 21:44:05 by stolfilocal ###################################################################### # To be included by the top-level Makefile in the program's directory. # The client must define # # ${TEST_LIB} the library under test (WITH extension, WITHOUT dir). # ${TEST_LIB_DIR} directory with latest sources and headers of ${TEST_LIB}. # ${PROG} or ${PROGS} test programs (whose main C sources are in current directory). # # The client may also define: # # ${IGNORE} list of ".h" or ".c" files that should not be compiled. # ${USE_GL} define as `YES' to bind with the GL headers and libraries. # ${USE_X11} define as `YES' to bind with the X11 headers and libraries. # ${OTHER_LIBS} list of additional libraries (WITH directories). # ${OTHER_I_FLAGS} additional -I flags for C compilation and dependency checks. # ${OTHER_C_FLAGS} additional flags for C compilation. # ${OTHER_LD_FLAGS} additional flags for linking. # # All library names must include the extension (".a", ".so", etc.) # The client must define the "all" target. # The client may extend the "clean" target with double-colon rules. include GENERIC.make .PHONY:: rebuild-test-lib ifeq "/${PROGS}" "/" PROGS := ${PROG} endif ifneq "/${PROGS}" "/" ifneq "/${TEST_LIB}" "/" ifneq "/${TEST_LIB_DIR}" "/" # ---------------------------------------------------------------------- # Package components and directories # Location of headers of the library under test: TEST_INC_DIR := ${TEST_LIB_DIR} # Get list of all local ".h" files, and their derived objects: HFILES := ${filter-out ${IGNORE}, ${wildcard *.h}} HOFILES := ${subst .h,.ho,${HFILES}} # Get list of all local ".c" files (including the main sources of ${PROGS}): CFILES := ${filter-out ${IGNORE}, ${wildcard *.c}} OFILES := ${subst .c,.o,${CFILES}} # Get list of ".c" files of good main programs and their objects: PROG_CFILES := ${addsuffix .c,${PROGS}} PROG_OFILES := ${subst .c,.o,${PROG_CFILES}} # Get list of non-main ".c" files and objects: LIB_CFILES := ${filter-out ${PROG_CFILES} ${IGNORE}, ${CFILES}} LIB_OFILES := ${subst .c,.o,${LIB_CFILES}} # ---------------------------------------------------------------------- # Optional libraries and include directories # Get OpenGL libraries and headers if requested: ifeq "/${USE_GL}" "/YES" GL_LIBRARIES := \ /lib64/libGLU.so \ /lib64/libGL.so GL_I_FLAGS := \ -I /usr/include/GL else GL_LIBRARIES := GL_I_FLAGS := endif # Get X11 libraries and headers if requested: ifeq "/${USE_X11}" "/YES" X11_LIBRARIES := \ ${X11_LIB_DIR}/libX11.so \ ${X11_LIB_DIR}/libXext.so \ ${X11_LIB_DIR}/libXmu.so.6 \ ${X11_LIB_DIR}/libXt.so.6 \ ${X11_LIB_DIR}/libXi.so.6 \ ${X11_LIB_DIR}/libSM.so.6 \ ${X11_LIB_DIR}/libICE.so.6 X11_I_FLAGS := -I${X11_INC_DIR} else X11_LIBRARIES := X11_I_FLAGS := endif #--------------------------------------------------------------------- # Default target for "make" with no target: all: rebuild-test-lib build-progs check #--------------------------------------------------------------------- # "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. \ -I${TEST_INC_DIR} \ ${GL_I_FLAGS} \ ${X11_I_FLAGS} \ ${OTHER_I_FLAGS} #--------------------------------------------------------------------- # "make depend" recreates the source dependency file ${DEPFILE}. DEPFILE := Deps.make depend: ${DEPFILE} ${DEPFILE}: ${HFILES} ${CFILES} @/bin/rm -f ${DEPFILE} @extract-ho-deps ${I_FLAGS} -I/usr/include \ ${HFILES} ${CFILES} \ | egrep -v ': /usr/include' \ > ${DEPFILE} #--------------------------------------------------------------------- # "make build-libs" is void because any local modules and libs will be built # together with the programs: build-lib: build-libs: #--------------------------------------------------------------------- # "make rebuild-test-lib" remakes the test library: rebuild-test-lib: ${TEST_LIB_DIR} cd ${TEST_LIB_DIR} && ${MAKE} ${TEST_LIB} #--------------------------------------------------------------------- # "make build" and "make build-progs" make sure that the ${DEPFILE} is # up to date, and then recreate the object files from the source files # as necessary. build: build-progs build-tests: build-progs build-progs: ${DEPFILE} ${MAKE} do-build-progs do-build-progs: ${PROGS} LIBRARIES := \ ${TEST_LIB_DIR}/${TEST_LIB} \ ${GL_LIBRARIES} \ ${X11_LIBRARIES} \ ${OTHER_LIBS} %.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 ${PROGS}: ${PROG_OFILES} ${LIB_OFILES} ${LIBRARIES} @echo '# - - - - - - - - - - - - - - - - - - - - - - - - - - - - -' @echo building $@ ... -rm -f $@ ${CC} -o $@ ${LD_FLAGS} ${OTHER_LD_FLAGS} $@.o \ ${LIB_OFILES} \ ${LIBRARIES} -lm -lrt @echo '# - - - - - - - - - - - - - - - - - - - - - - - - - - - - -' ifneq "/${wildcard ${DEPFILE}}" "/" # Include specific dependencies extracted by "make depend" include ${DEPFILE} endif #--------------------------------------------------------------------- # "make clean" deletes all derived files. # It is a double-colon rule so that clients may add more actions. # Also recreates the ".deps" file. clean:: -/bin/rm -f *.o *.ho *.a core ${PROGS} *.deps ${DEPFILE} ${MAKE} depend endif endif endif # End of ${PROG} ${TEST_LIB} ${TEST_LIB_DIR} section. ###################################################################### #--------------------------------------------------------------------- # "make debug" prints some debugging info: debug: @echo "TEST_LIB = ${TEST_LIB}" @echo "TEST_LIB_DIR = ${TEST_LIB_DIR}" @echo "PROG = ${PROG}" @echo "PROGS = ${PROGS}"