b6fe847d866cc1ced5888b4e2d5fea51f000b13c
[protos/libecoli.git] / mk / ecoli-obj-vars.mk
1 # SPDX-License-Identifier: BSD-3-Clause
2 # Copyright 2015, Olivier MATZ <zer0@droids-corp.org>
3
4 # obj-y-$(obj) is provided by the user
5 #  $(obj) is the path of the object, and the variable contains
6 #  the list of sources. Several obj-y-$(obj) can be present.
7
8 # list all object builds requested by user
9 all-obj := $(patsubst obj-y-%,%,$(filter obj-y-%,$(.VARIABLES)))
10
11 # add them to the list of targets
12 all-targets += $(all-obj)
13
14 # convert source path to intermediate object path, and filter
15 # objects from sources
16 #  $1: list of source paths
17 #  $2: output directory (including trailing slash)
18 #  return: list of intermediate object paths
19 src2iobj = $(addprefix $(filter-out ./,$(2)),$(notdir $(strip \
20         $(patsubst %.c,%.o,\
21         $(patsubst %.s,%.o,\
22         $(filter-out %.o,$(1)))))))
23
24 # return the file if it matches a extension that is built with cc
25 #   $1: source file
26 is_cc_source = $(filter %.c %.s %S,$(1))
27
28 # return the file if it's already an object file: in this case no
29 # intermediate object is needed
30 #   $1: source file
31 is_obj_source = $(filter %.o,$(1))
32
33 # return the file if it's a static library
34 #   $1: source file
35 is_alib_source = $(filter %.a,$(1))
36
37 # for each obj, create the following variables:
38 #   out-$(obj) = output path of the object
39 #   pre-$(obj) = list of prerequisites for this object
40 # Some source files need intermediate objects, we define these variables
41 # for them too, and add them in a list: $(all-iobj).
42 # Last, we add the generated files in $(all-clean-file).
43 $(foreach obj,$(all-obj),\
44         $(eval out-$(obj) := $(dir $(obj))) \
45         $(eval pre-$(obj) := ) \
46         $(foreach src,$(obj-y-$(obj)), \
47                 $(if $(call is_cc_source,$(src)), \
48                         $(eval iobj := $(call src2iobj,$(src),$(out-$(obj)))) \
49                         $(eval pre-$(iobj) := $(src)) \
50                         $(eval all-iobj += $(iobj)) \
51                         $(eval all-clean-file += $(iobj)) \
52                         $(eval pre-$(obj) += $(iobj)) \
53                 , \
54                 $(if $(call is_obj_source,$(src)),\
55                         $(eval pre-$(obj) += $(src)) \
56                 , \
57                 $(error "unsupported source format: $(src)"))) \
58         )\
59         $(eval all-clean-file += $(obj)) \
60 )
61
62 # fix the format of .o.d.tmp (generated by gcc) to a .o.d that defines
63 # dependencies as makefile variables
64 #  $1: object file (.o)
65 obj-fixdep = if [ -f $(call file2tmpdep,$(1)) ]; then\
66                 echo -n "dep-$(1) = " > $(call depfile,$(1)) && \
67                 sed 's,^[^ ][^:]*: ,,' $(call file2tmpdep,$(1)) >> $(call depfile,$(1)) && \
68                 rm -f $(call file2tmpdep,$(1)); \
69         else \
70                 $(call create_empty_depfile,$(1)); \
71         fi
72
73 # compile a file
74 #  $1: sources
75 #  $2: dst
76 compile_cmd = $(CC) -Wp,-MD,$(call file2tmpdep,$(2)) \
77         $(CPPFLAGS) $(cppflags-$(2)) \
78         $(CFLAGS) $(cflags-$(2)) \
79         -c -o $2 $1
80
81 # print line used to compile a file
82 ifeq ($(V),1)
83 compile_print_cmd = echo $(call protect_quote,$(call compile_cmd,$1,$2))
84 else
85 compile_print_cmd = echo "  CC $(2)"
86 endif
87
88 # combine several *.o files into one
89 #   $1: sources (*.o)
90 #   $2: dst (xyz.o too)
91 combine_cmd = $(LD) -r $(1) -o $(2)
92
93 # print line used to combine object files
94 ifeq ($(V),1)
95 combine_print_cmd = echo $(call protect_quote,$(call combine_cmd,$1,$2))
96 else
97 combine_print_cmd = echo "  LD $(2)"
98 endif
99
100 all-clean-file += $(all-obj)