自己常用的Makefile

来源:互联网 发布:java构造函数执行顺序 编辑:程序博客网 时间:2024/06/04 22:16

开发时常用的Makefile,make编译链接,make run运行。(支持汇编 c c++ vala)

#Program build mode : debug/release                                                                     BUILD_MODE := debug#BUILD_MODE := release## target nameTARGET := GtkmmSample## packages used by targetPACKAGES = gtk+-3.0SRC_ROOT = src## uncomment below line if use custom include dirs#LOCAL_INCLUDES =##uncomment below line if use custom libraries#EXTRA_LIBS_DIR =## the resources dir, uncomment below line if use resourcesRES_DIR = resTOP ?= $(shell pwd)## alias for shell commandMKDIR := mkdir -pRM := rm -fr CP := cp -fMV := mv -f## The entended name of filesASM_EXT ?= sC_EXT ?= cCPP_EXT ?= cppVALA_EXT ?= valaSRC_ROOT ?= .LOCAL_CC ?= gccLOCAL_CXX ?= g++LOCAL_VALAC ?= valacLOCAL_ASFLAGS +=-D__ASSEMBLY__#LOCAL_INCLUDES =LOCAL_CFLAGS += $(shell pkg-config --cflags $(PACKAGES))LOCAL_LDFLAGS += $(shell pkg-config --libs $(PACKAGES))LOCAL_CPPFLAGS +=LOCAL_VALAFLAGS += $(PACKAGES:%=--pkg=%) --threadifeq ($(BUILD_MODE),debug)  LOCAL_CFLAGS += -g   LOCAL_CPPFLAGS += -Wall -DDEBUG  LOCAL_VALAFLAGS+= -g else  ifeq ($(BUILD_MODE),release)    LOCAL_CFLAGS += -O2 -DG_DISABLE_CHECKS -DG_DISABLE_ASSERT  else    $(error "BUILD_MODE error !(debug/release)")  endifendififneq ($(RES_DIR),)    LOCAL_CPPFLAGS +=-DRES_DIR=\""$(RES_DIR)"\"    LOCAL_INCLUDES += $(RES_DIR)endifLOCAL_INCLUDES += $(SRC_ROOT) $(EXTRA_LIBS_DIR)OUT_ROOT = $(BUILD_MODE)OUT_OBJS = $(BUILD_MODE)/objs############################################################# Convert "path/to/libXXX.so" to "-lXXX".## Any "path/to/libXXX.a" elements pass through unchanged.###########################################################define normalize-libraries$(foreach so,$(filter %.so,$(1)),-l$(patsubst lib%.so,%,$(notdir $(so))))\$(filter-out %.so,$(1))endefdefine normalize-target-libraries$(call normalize-libraries,$(1))endef############################################################# Find all of the files with xxx extend name under the ## named directories. Meant to be used like:## SRC_C_FILES := $(call all-files-under,SRC_DIR, *.c)## SRC_VALA_FILES := $(call all-files-under,SRC_DIR, *.vala)###########################################################define all-files-under$(patsubst ./%,%, \  $(shell find $(1) -name $(2) -and -not -name ".*") )endef############################################################# Commands for copy files(custom libs and resources files) ###########################################################define transform-files@$(MKDIR) $(dir $@)@$(CP) $< $@endef############################################################# Commands for munging the dependency files GCC generates###########################################################define transform-d-to-pcp $(@:%.o=%.d) $(@:%.o=%.P); \sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \-e '/^$$/ d' -e 's/$$/ :/' < $(@:%.o=%.d) >> $(@:%.o=%.P); \    rm -f $(@:%.o=%.d)endef############################################################# Commands for running gcc to compile a C file############################################################ $(1): extra flagsdefine transform-c-or-s-to-o-no-deps@$(MKDIR) $(dir $@)$(LOCAL_CC) $(addprefix -I , $(LOCAL_INCLUDES)) \$(LOCAL_CPPFLAGS) \$(LOCAL_CFLAGS) \-c \$(1) \-MD -MF $(patsubst %.o,%.d,$@) -o $@ $<endefdefine transform-c-to-o$(call transform-c-or-s-to-o-no-deps,)@$(transform-d-to-p)endefdefine transform-s-to-o$(call transform-c-or-s-to-o-no-deps, $(LOCAL_ASFLAGS))$(transform-d-to-p)endef############################################################# Commands for running g++ to compile a C++ file###########################################################define transform-cpp-to-o@mkdir -p $(dir $@)$(LOCAL_CXX) $(addprefix -I , $(LOCAL_INCLUDES)) \-c \$(LOCAL_CPPFLAGS) \$(LOCAL_CFLAGS) \-MD -MF $(patsubst %.o,%.d,$@) -o $@ $<endef############################################################# Commands for running g++ to link a executable target###########################################################define transform-o-to-executable                                                                        @$(MKDIR) $(dir $@)$(LOCAL_CXX) $(all_objects) -o $@ $(LOCAL_LDFLAGS) \$(if $(EXTRA_LIBS_DIR), $(addprefix -L, $(EXTRA_LIBS_DIR))) \$(if $(EXTRA_LIBS_DIR),$(call normalize-target-libraries,$(all_libraries)))endef##=========================================================##           All  Objects ##=========================================================asm_sources := $(call all-files-under,$(SRC_ROOT),*.$(ASM_EXT))asm_objects := $(patsubst $(SRC_ROOT)/%.$(ASM_EXT), $(OUT_OBJS)/%.o, $(asm_sources))c_sources := $(call all-files-under,$(SRC_ROOT),*.$(C_EXT))c_objects := $(patsubst $(SRC_ROOT)/%.$(C_EXT), $(OUT_OBJS)/%.o, $(c_sources))cpp_sources := $(call all-files-under,$(SRC_ROOT),*.$(CPP_EXT))cpp_objects := $(patsubst $(SRC_ROOT)/%.$(CPP_EXT), $(OUT_OBJS)/%.o, $(cpp_sources))vala_sources := $(call all-files-under,$(SRC_ROOT),*.$(VALA_EXT))vala_c_sources := $(patsubst $(SRC_ROOT)/%.$(VALA_EXT), $(OUT_OBJS)/%.$(C_EXT), $(vala_sources))vala_objects := $(vala_c_sources:%.$(C_EXT)=%.o)tmp_all_objects := $(asm_objects) $(c_objects) $(cpp_objects) $(vala_objects)all_objects := $(strip $(tmp_all_objects))##=========================================================##           all custom  libraries ##=========================================================ifneq ($(strip $(EXTRA_LIBS_DIR)),)libs_path = $(addprefix -L, $(EXTRA_LIBS_DIR))static_libs_sources := $(call all-files-under,$(EXTRA_LIBS_DIR),*.a)static_libs_objs := $(addprefix $(OUT_ROOT)/, $(static_libs_sources))dynamic_libs_sources := $(call all-files-under,$(EXTRA_LIBS_DIR),*.so)dynamic_libs_objs := $(addprefix $(OUT_ROOT)/, $(dynamic_libs_sources))all_libraries := $(static_libs_objs) $(dynamic_libs_objs)endif##=========================================================##           all resources##=========================================================ifneq ($(strip $(RES_DIR)),)res_sources := $(call all-files-under,$(RES_DIR),*.*)res_objs := $(addprefix $(OUT_ROOT)/, $(res_sources))all_resources := $(res_objs)endif#############################################################          Rules ###########################################################linked_module :=$(OUT_ROOT)/$(TARGET).PHONY : all clean runall: $(linked_module)$(linked_module): $(vala_src_stamp) $(all_objects) $(all_libraries) $(all_resources)@echo $(transform-o-to-executable)ifneq ($(strip $(asm_objects)),)$(asm_objects): $(OUT_OBJS)/%.o: $(SRC_ROOT)/%.$(ASM_EXT)$(transform-s-to-o)                                                                            -include $(asm_objects:%.o=%.P)endififneq ($(strip $(c_objects)),)$(c_objects): $(OUT_OBJS)/%.o: $(SRC_ROOT)/%.$(C_EXT)$(transform-c-to-o)                                                                            -include $(c_objects:%.o=%.P)endififneq ($(strip $(cpp_objects)),)$(cpp_objects): $(OUT_OBJS)/%.o: $(SRC_ROOT)/%.$(CPP_EXT)$(transform-cpp-to-o)                                                                            -include $(cpp_objects:%.o=%.P)endififneq ($(strip $(vala_sources)),)vala_src_stamp = .vala.src.stamp$(vala_src_stamp): $(vala_sources)@$(RM) $@ && echo stamp > $@-t@$(RM) $(patsubst $(SRC_ROOT)/%.$(VALA_EXT), $(OUT_OBJS)/%.o, $? )$(LOCAL_VALAC) -d $(OUT_OBJS) -b $(SRC_ROOT) $(LOCAL_VALAFLAGS) -C $(vala_sources)@$(MV) $@-t $@$(vala_c_sources):%.$(C_EXT) : $(vala_src_stamp)@if test -f $@; then :; else rm -f $(vala_src_stamp) ; fi@if test -f $@; then :; else make $(vala_src_stamp) ; fi$(vala_objects): %.o: %.$(C_EXT)$(transform-c-to-o)                                                                            -include $(vala_objects:%.o=%.P)endififneq ($(strip $(static_libs_objs)),)$(static_libs_objs): $(OUT_ROOT)/%.a: %.a$(transform-files)endififneq ($(strip $(dynamic_libs_objs)),)$(dynamic_libs_objs): $(OUT_ROOT)/%.so: %.so$(transform-files)endififneq ($(strip $(all_resources)),)$(all_resources): $(OUT_ROOT)/%: %$(transform-files)endifclean:@$(RM) $(OUT_ROOT) $(vala_src_stamp)run:@$(linked_module) 





原创粉丝点击