# Generic Makefile for one or more executable programs # Last edited on 2016-12-26 21:40:19 by stolfilocal ###################################################################### # To be included by the top-level Makefile in the program's directory. # The caller must define # # ${PROG} or ${PROGS} the programs (whose main C cources are in current directory). # # and 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 ifeq "/${PROGS}" "/" PROGS := ${PROG} endif ifneq "/${PROGS}" "/" # ---------------------------------------------------------------------- # Package components and directories # 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 all ${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}} # Directory with test runs: TEST_DIR := tests # ---------------------------------------------------------------------- # Locations of imported packages # Location of X11 headers and libraries (including ICE): X11_INC_DIR := /usr/include/X11 X11_LIB_DIR := /lib64 # ---------------------------------------------------------------------- # Optional libraries and include directories # Get OpenGL libraries and headers if requested: ifeq "/${USE_GL}" "/YES" GL_LIBRARIES := \ /lib64/libglut.so.3 \ /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: build-progs #--------------------------------------------------------------------- # "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: ${DEPFILE} ${DEPFILE}: ${CFILES} ${HFILES} @/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 build" and "make build-progs" assumes that the dependencies in ${DEPFILE} # are up-to-date. build: build-progs build-prog: build-progs build-progs: ${DEPFILE} ${MAKE} debug do-build-progs do-build-progs: ${PROGS} LIBRARIES := \ ${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 check" runs the tests in the "tests" subdirectory. TEST_DIR := tests check: ${TEST_DIR} if [[ -d ${TEST_DIR} ]]; then ( cd ${TEST_DIR}/. && ${MAKE} all ) fi #--------------------------------------------------------------------- # "make clean" deletes all derived files. # It is a double-colon rule so that clients may add more actions. # Also recreates the ".deps" files. clean:: clean-progs clean-tests clean-progs: -/bin/rm -f *.o *.ho *.a core ${PROGS} *.deps ${DEPFILE} touch --date='Jan 01 1970' ${DEPFILE} clean-tests: if [[ -d ${TEST_DIR} ]]; then ( cd ${TEST_DIR} && make clean ) fi endif # End of ${PROG} or ${PROGS} section. ###################################################################### #--------------------------------------------------------------------- # "make debug" prints some debugging info: debug: @echo "PROG = ${PROG}" @echo "PROGS = ${PROGS}"