1 # SPDX-License-Identifier: BSD-3-Clause
2 # Copyright 2015, Olivier MATZ <zer0@droids-corp.org>
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.
8 # list all object builds requested by user
9 all-obj := $(patsubst obj-y-%,%,$(filter obj-y-%,$(.VARIABLES)))
11 # add them to the list of targets
12 all-targets += $(all-obj)
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 \
22 $(filter-out %.o,$(1)))))))
24 # return the file if it matches a extension that is built with cc
26 is_cc_source = $(filter %.c %.s %S,$(1))
28 # return the file if it's already an object file: in this case no
29 # intermediate object is needed
31 is_obj_source = $(filter %.o,$(1))
33 # return the file if it's a static library
35 is_alib_source = $(filter %.a,$(1))
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)) \
54 $(if $(call is_obj_source,$(src)),\
55 $(eval pre-$(obj) += $(src)) \
57 $(error "unsupported source format: $(src)"))) \
59 $(eval all-clean-file += $(obj)) \
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)); \
70 $(call create_empty_depfile,$(1)); \
76 compile_cmd = $(CC) -Wp,-MD,$(call file2tmpdep,$(2)) \
77 $(CPPFLAGS) $(cppflags-$(2)) \
78 $(CFLAGS) $(cflags-$(2)) \
81 # print line used to compile a file
83 compile_print_cmd = echo $(call protect_quote,$(call compile_cmd,$1,$2))
85 compile_print_cmd = echo " CC $(2)"
88 # combine several *.o files into one
91 combine_cmd = $(LD) -r $(1) -o $(2)
93 # print line used to combine object files
95 combine_print_cmd = echo $(call protect_quote,$(call combine_cmd,$1,$2))
97 combine_print_cmd = echo " LD $(2)"
100 all-clean-file += $(all-obj)