# Define utility functions. # Module name to variable name. # $(call mod2var,foo/bar) # => foo_bar mod2var = $(subst /,_,$1) # Get source file basename. # $(call src2base,foo.c bar.S) # => foo bar src2base = $(patsubst %.cpp,%,$(patsubst %.c,%,$(patsubst %.S,%,$1)))\ $(if $(filter-out %.c %.S %.cpp,$1),$(error unknown source extension)) # Source file to source file, adding path. # $(call src2src,foo.c bar.c,mod/src) # => mod/src/foo.c mod/src/bar.c # $(call src2src,all,mod/src) # => $(call wild2src,mod/src/*.c mod/src/*.cpp) src2src = $(if $(filter all,$1),\ $(call wild2src,$(patsubst %,$2/%,*.c *.cpp)),$(1:%=$2/%)) # Wildcard expand a source pattern. # $(call wild2src,mod/src/*.c mod/src/*.cpp) # => mod/src/foo.c mod/src/bar.c wild2src = $(wildcard $1) $(patsubst $(BASE)/%,%,$(wildcard $(1:%=$(BASE)/%))) # Source file to object file. # $(call src2obj,foo.c,target) # => $(OBJ_DIR)/foo.target.o # $(call src2obj,foo/bar.c,host) # => $(OBJ_DIR)/foo__bar.host.o src2obj = $(patsubst %,$(OBJ_DIR)/%.$2.o,$(subst /,__,$(call src2base,$1))) # Source file to dependency file. # $(call src2dep,foo.c,target) # => $(OBJ_DIR)/foo.target.d # $(call src2dep,bar.c,host) # => $(OBJ_DIR)/bar.host.d src2dep = $(patsubst %.o,%.d,$(call src2obj,$1,$2)) # Program name to executable file name. # $(call prog2exe,test_foo,target) # => $(OBJ_DIR)/test_foo.elf # $(call prog2exe,test_bar,host) # => $(OBJ_DIR)/test_bar prog2exe = $(patsubst %,$(OBJ_DIR)/%$(if $(filter host,$2),,.elf),$1) # Program name to library file name. # $(call prog2lib,test_foo.a) # => $(OBJ_DIR)/test_foo.a prog2lib = $(patsubst %,$(OBJ_DIR)/%,$1) # Program name to binary file name. # $(call prog2bin,test_foo) # => $(OBJ_DIR)/test_foo.bin prog2bin = $(patsubst %,$(OBJ_DIR)/%.bin,$1) # Program name to ROM file name. # $(call prog2rom,test_foo) # => $(OBJ_DIR)/test_foo.rom prog2rom = $(patsubst %,$(OBJ_DIR)/%.rom,$1) # Program name to list file name. # $(call prog2lst,test_foo) # => $(OBJ_DIR)/test_foo.lst prog2lst = $(patsubst %,$(OBJ_DIR)/%.lst,$1) # Program name to SIZE file name. # $(call prog2size,test_foo) # => $(OBJ_DIR)/test_foo.size prog2size = $(patsubst %,$(OBJ_DIR)/%.size,$1) # Build type (target or host) to variable name. # $(call type2var,host)_CC # => HOST_CC # $(call type2var,target)_CC # => TARGET_CC # $(call type2var,target-arm)_CC # => TARGET_arm_CC type2var = $(if $(filter host,$1),HOST,$(if $(filter target,$1),TARGET,$(if $(filter target-%,$1),$(subst -,_,$(subst target,TARGET,$1)),$(error unknown type)))) # Find whether a C++ link should be made. # $(call iscxx,prog), if prog sources include a c++ file # => _CXX # else # => iscxx = $(if $(filter %.cpp,$($1_SOURCES) $($1_MODULES_SOURCES)),_CXX,) # Expand to the second argument when debug, to the third one when non debug. # $(call isdebug,host,-g,-O2), if debug is activated for host # => -g # else # => -O2 isdebug = $(if $(filter y,$($(call type2var,$1)_CONFIG_DEBUG)),$2,$3) # Export a list of variables by removing a prefix. # $(call exportvars,host,CONFIG_DEBUG CONFIG_DEBUG_MORE) # => CONFIG_DEBUG := $(HOST_CONFIG_DEBUG) # CONFIG_DEBUG_MORE := $(HOST_CONFIG_DEBUG_MORE) define exportvars $(foreach var,$2,$(var) := $$($(call type2var,$1)_$(var)) ) endef # Unexport a list of variables exported by exportvars. # $(call unexportvars,CONFIG_DEBUG CONFIG_DEBUG_MORE) # => CONFIG_DEBUG := # CONFIG_DEBUG_MORE := define unexportvars $(foreach var,$2,$(var) := ) endef # Evaluate a variable for each build types. # $(call foreach_type,A) # => $(eval $(call A,host,HOST)) $(eval $(call A,target,TARGET))... foreach_type = $(foreach type,$(BUILD_TYPES),\ $(eval $(call $1,$(type),$(call type2var,$(type))))) # Gather a variable for all build type. # $(call gather_type,A) # => $(HOST_A) $(TARGET_A)... gather_type = $(foreach type,$(BUILD_TYPES),$($(call type2var,$(type))_$1))