# Generic Makefile for one or more executable programs # Last edited on 2024-12-25 09:22:32 by stolfi ###################################################################### # To be included by the top-level Makefile in the program's directory. # The caller must define # # ${STOLFIHOME} directory with stolfi's bin, lib, include, etc. # ${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 or installed. # ${JS_LIBS} list of Stolfi's libraries to bind with (WITHOUT directories). # ${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 ${STOLFIHOME}/programs/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 # ---------------------------------------------------------------------- # Installation directories # Where to install the program: INST_LIB_DIR := ${INST_LIB_DIR}/${PLATFORM} INST_BIN_DIR := ${INST_BIN_DIR}/${PLATFORM} # ---------------------------------------------------------------------- # Locations of imported packages # Location of Stolfi's headers and libraries: JS_PUB_DIR := ${STOLFIHOME} JS_LIB_DIR := ${JS_PUB_DIR}/lib/${PLATFORM} JS_INC_DIR := ${JS_PUB_DIR}/include # Location of X11 headers and libraries (including ICE): X11_INC_DIR := /usr/include/X11 X11_LIB_DIR := /usr/lib/x86_64-linux-gnu GL_LIB_DIR := /usr/lib/x86_64-linux-gnu # ---------------------------------------------------------------------- # Optional libraries and include directories # Get OpenGL libraries and headers if requested: ifeq "/${USE_GL}" "/YES" GL_LIBRARIES := \ ${GL_LIB_DIR}/libglut.so \ ${GL_LIB_DIR}/libGLU.so \ ${GL_LIB_DIR}/libGL.so GL_I_FLAGS := \ -I${JS_INC_DIR}/GL \ -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_I_FLAGS := -I${X11_INC_DIR} else X11_LIBRARIES := X11_I_FLAGS := endif NOT_X11_LIBRARIES := \ ${X11_LIB_DIR}/libSM.so.6 \ ${X11_LIB_DIR}/libICE.so.6 #--------------------------------------------------------------------- # Default target for "make" with no target: all: uninstall build-progs install #--------------------------------------------------------------------- # "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${JS_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}: ${CFILES} ${HFILES} @/bin/rm -f ${DEPFILE} @extract_ho_deps ${I_FLAGS} -I/usr/include \ ${HFILES} ${CFILES} \ | egrep -v ': /usr/include' \ > ${DEPFILE} # ---------------------------------------------------------------------- # Create a local symlink for {extract_ho_deps}, for {tar-export}'s sake extract_ho_deps: ${STOLFIHOME}/bin/extract_ho_deps ln -s ${STOLFIHOME}/bin/extract_ho_deps #--------------------------------------------------------------------- # "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 := \ ${addprefix ${JS_LIB_DIR}/, ${JS_LIBS}} \ ${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 install" symlinks to program in the installation dir. install: install-progs install-progs: ${PROGS} @for prog in ${PROGS} ; do \ compdir="$${PWD}" ; \ ( cd ${INST_BIN_DIR}/ && rm -f $${prog} ; ln -s $${compdir}/$${prog} ; ls -ld $${prog} ) ; \ done #--------------------------------------------------------------------- # "make uninstall" deletes the exported program and makefile: uninstall: uninstall-progs uninstall-progs: ( cd ${INST_BIN_DIR}/. && rm -f ${PROGS} ) #--------------------------------------------------------------------- # "make tar-export" creates a tarball of sources and executables. THIS_DIR := ${notdir ${shell pwd}} NOW := ${shell date '+%Y-%m-%d-%H%M%S'} TARBALL_FILE := ${THIS_DIR}-${NOW}.tgz tar-export: ${PROGS} extract_ho_deps @echo "archiving to ${TARBALL_FILE} ..." tar -cvzf ${TARBALL_FILE} \ 00-README Makefile \ extract_ho_deps \ ${PROGS} \ ${HFILES} ${CFILES} #--------------------------------------------------------------------- # "make tar-save" creates a date-stamped tarball of all files except # edit backups and derived files. SAVEDIR := SAVE SAVETARFILE := ${LIBNAME}-${NOW}-save.tgz tar-save: clean tar -cvzf ${SAVEDIR}/${SAVETARFILE} \ `find ./ -type f -print | egrep -v -e '[~]$'` #--------------------------------------------------------------------- # "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}"