开发时常用的Makefile,make编译链接,make run运行。(支持汇编 c c++ vala)
#Program build mode : debug/release
BUILD_MODE := debug
#BUILD_MODE := release
## target name
TARGET := GtkmmSample
## packages used by target
PACKAGES = gtk+-3.0
SRC_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 resources
RES_DIR = res
TOP ?= $(shell pwd)
## alias for shell command
MKDIR := mkdir -p
RM := rm -fr
CP := cp -f
MV := mv -f
## The entended name of files
ASM_EXT ?= s
C_EXT ?= c
CPP_EXT ?= cpp
VALA_EXT ?= vala
SRC_ROOT ?= .
LOCAL_CC ?= gcc
LOCAL_CXX ?= g++
LOCAL_VALAC ?= valac
LOCAL_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=%) --thread
ifeq ($(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)")
endif
endif
ifneq ($(RES_DIR),)
LOCAL_CPPFLAGS +=-DRES_DIR=\""$(RES_DIR)"\"
LOCAL_INCLUDES += $(RES_DIR)
endif
LOCAL_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))
endef
define 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-p
cp $(@:%.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 flags
define 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 $@ $<
endef
define transform-c-to-o
$(call transform-c-or-s-to-o-no-deps,)
@$(transform-d-to-p)
endef
define 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 run
all: $(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)
endif
ifneq ($(strip $(c_objects)),)
$(c_objects): $(OUT_OBJS)/%.o: $(SRC_ROOT)/%.$(C_EXT)
$(transform-c-to-o)
-include $(c_objects:%.o=%.P)
endif
ifneq ($(strip $(cpp_objects)),)
$(cpp_objects): $(OUT_OBJS)/%.o: $(SRC_ROOT)/%.$(CPP_EXT)
$(transform-cpp-to-o)
-include $(cpp_objects:%.o=%.P)
endif
ifneq ($(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)
endif
ifneq ($(strip $(static_libs_objs)),)
$(static_libs_objs): $(OUT_ROOT)/%.a: %.a
$(transform-files)
endif
ifneq ($(strip $(dynamic_libs_objs)),)
$(dynamic_libs_objs): $(OUT_ROOT)/%.so: %.so
$(transform-files)
endif
ifneq ($(strip $(all_resources)),)
$(all_resources): $(OUT_ROOT)/%: %
$(transform-files)
endif
clean:
@$(RM) $(OUT_ROOT) $(vala_src_stamp)
run:
@$(linked_module)