summaryrefslogtreecommitdiff
path: root/cesar/common/make/func.mk
blob: 25c92dc389fb506aedeaab9bcd425146c462e08e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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 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,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