# SPDX-License-Identifier: BSD-3-Clause # Copyright 2015, Olivier MATZ # obj-y-$(obj) is provided by the user # $(obj) is the path of the object, and the variable contains # the list of sources. Several obj-y-$(obj) can be present. # list all object builds requested by user all-obj := $(patsubst obj-y-%,%,$(filter obj-y-%,$(.VARIABLES))) # add them to the list of targets all-targets += $(all-obj) # convert source path to intermediate object path, and filter # objects from sources # $1: list of source paths # $2: output directory (including trailing slash) # return: list of intermediate object paths src2iobj = $(addprefix $(filter-out ./,$(2)),$(notdir $(strip \ $(patsubst %.c,%.o,\ $(patsubst %.s,%.o,\ $(filter-out %.o,$(1))))))) # return the file if it matches a extension that is built with cc # $1: source file is_cc_source = $(filter %.c %.s %S,$(1)) # return the file if it's already an object file: in this case no # intermediate object is needed # $1: source file is_obj_source = $(filter %.o,$(1)) # return the file if it's a static library # $1: source file is_alib_source = $(filter %.a,$(1)) # for each obj, create the following variables: # out-$(obj) = output path of the object # pre-$(obj) = list of prerequisites for this object # Some source files need intermediate objects, we define these variables # for them too, and add them in a list: $(all-iobj). # Last, we add the generated files in $(all-clean-file). $(foreach obj,$(all-obj),\ $(eval out-$(obj) := $(dir $(obj))) \ $(eval pre-$(obj) := ) \ $(foreach src,$(obj-y-$(obj)), \ $(if $(call is_cc_source,$(src)), \ $(eval iobj := $(call src2iobj,$(src),$(out-$(obj)))) \ $(eval pre-$(iobj) := $(src)) \ $(eval all-iobj += $(iobj)) \ $(eval all-clean-file += $(iobj)) \ $(eval pre-$(obj) += $(iobj)) \ , \ $(if $(call is_obj_source,$(src)),\ $(eval pre-$(obj) += $(src)) \ , \ $(error "unsupported source format: $(src)"))) \ )\ $(eval all-clean-file += $(obj)) \ ) # fix the format of .o.d.tmp (generated by gcc) to a .o.d that defines # dependencies as makefile variables # $1: object file (.o) obj-fixdep = if [ -f $(call file2tmpdep,$(1)) ]; then\ echo -n "dep-$(1) = " > $(call depfile,$(1)) && \ sed 's,^[^ ][^:]*: ,,' $(call file2tmpdep,$(1)) >> $(call depfile,$(1)) && \ rm -f $(call file2tmpdep,$(1)); \ else \ $(call create_empty_depfile,$(1)); \ fi # compile a file # $1: sources # $2: dst compile_cmd = $(CC) -Wp,-MD,$(call file2tmpdep,$(2)) \ $(CPPFLAGS) $(cppflags-$(2)) \ $(CFLAGS) $(cflags-$(2)) \ -c -o $2 $1 # print line used to compile a file ifeq ($(V),1) compile_print_cmd = echo $(call protect_quote,$(call compile_cmd,$1,$2)) else compile_print_cmd = echo " CC $(2)" endif # combine several *.o files into one # $1: sources (*.o) # $2: dst (xyz.o too) combine_cmd = $(LD) -r $(1) -o $(2) # print line used to combine object files ifeq ($(V),1) combine_print_cmd = echo $(call protect_quote,$(call combine_cmd,$1,$2)) else combine_print_cmd = echo " LD $(2)" endif all-clean-file += $(all-obj)