工作代码片段-makefile模板

时间:2022-01-08 12:41:41

项目领导要求比较高,全部是手写makefile,没有借助autotools或者cmake之类,不过却形成了一个可以复用的定义模板:

ifdef release
DIR = .
LIB_DIR    = ${DIR}/../../../lib/fep
DEPS       = -lplatform -lappconf -lconfig -ltinyxml -lkhreadline
CFLAGS     = -O2 -fPIC -rdynamic -Wall
LDFLAGS    = -fPIC -m64 -lreadline -lncurses -Wall
else
DIR        = $(shell pwd)
LIB_DIR    = 
DEPS       = ${LIB_DIR}/libplatform.so
CFLAGS     = -O2 -fPIC -rdynamic -pg -Wall
LDFLAGS    = -fPIC -m64 -lreadline -lncurses -pg -Wall
endif

OBJ_DIR    = 
BIN_DIR    = 

CC         = g++
LDLIBS     = -shared

TARGETS    = ${BIN_DIR}/fepconfig
LDTARGETS  = ${LIB_DIR}/libconfig.so
BINOBJS    = 
OBJECTS    = $(patsubst cpp/%.cpp,${OBJ_DIR}/%.o,$(wildcard cpp/*.cpp))
SOURCES    = $(wildcard cpp/*.cpp)
TARG_OBJS  = $(patsubst %.cpp,${OBJ_DIR}/%.o,$(wildcard *.cpp))

VPATH      = .:${DIR}/../platform:${OBJ_DIR}:${LIB_DIR}

INCLUDES   = -I${DIR}/../platform/include \
             -I${DIR}/../platform/readline \
             -Iappconf/include \
             -I./include \
             -I../protocol/include \
             -Itinyxml

APPCONF    = ${LIB_DIR}/libappconf.so
TINYXML    = ${LIB_DIR}/libtinyxml.so

.PHONY:all clean

all:${APPCONF} ${TINYXML} ${LDTARGETS} ${TARGETS}
	@echo y | cp -r template/*.{conf,xml} ../../../conf/fep/

${TARGETS}:${TARG_OBJS}
	${CC} ${LDFLAGS} $^ ${INCLUDES} -L${LIB_DIR} ${DEPS}  -o $@
	
${TARG_OBJS}:${OBJ_DIR}/%.o:%.cpp
	${CC} ${CFLAGS} {1}lt; ${INCLUDES} -c  -o $@

${LDTARGETS}:${OBJECTS}
	${CC} ${LDLIBS} $^ -o $@

${APPCONF}:
	(cd appconf;make)
	
${TINYXML}:
	(cd tinyxml;make)

${OBJECTS}:${OBJ_DIR}/%.o:cpp/%.cpp
	${CC} ${CFLAGS} {1}lt; ${INCLUDES} -c  -o $@

clean:
	-rm -f ${TARGETS} ${LDTARGETS} ${OBJECTS}
	(cd appconf;make clean)
	(cd tinyxml;make clean)