summaryrefslogtreecommitdiff
path: root/cesar/common/make/func.mk
diff options
context:
space:
mode:
Diffstat (limited to 'cesar/common/make/func.mk')
-rw-r--r--cesar/common/make/func.mk71
1 files changed, 71 insertions, 0 deletions
diff --git a/cesar/common/make/func.mk b/cesar/common/make/func.mk
new file mode 100644
index 0000000000..6564fc8c5e
--- /dev/null
+++ b/cesar/common/make/func.mk
@@ -0,0 +1,71 @@
+# 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)
+
+# Build type (target or host) to variable name.
+# $(call type2var,target)_CC
+# => TARGET_CC
+# $(call type2var,host)_CC
+# => HOST_CC
+type2var = $(if $(filter target,$1),TARGET,$(if $(filter host,$1),HOST,$(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 first argument when debug, to the second one when non debug.
+# $(call isdebug,-g,-O2), if debug is activated
+# => -g
+# else
+# => -O2
+ifeq ($(CONFIG_DEBUG),n)
+isdebug = $2
+else
+isdebug = $1
+endif