1 # SPDX-License-Identifier: BSD-3-Clause
4 # This file is meant to be included by in-tree payloads
5 # to provide default targets for incremental builds.
7 # Variables with file names and directory overrides have
8 # to be defined in advance for proper dependency tracking.
9 # Then, include this file. e.g
12 # OBJS := $(obj)/payload.o
13 # TARGET := $(obj)/payload.elf
14 # include ../path/to/libpayload/Makefile.payload
17 # Find relative path to libpayload (where this Makefile resides).
18 LIBPAYLOAD_SRC := $(dir $(lastword $(MAKEFILE_LIST)))
19 LIBPAYLOAD_SRC := $(patsubst %/,%,$(LIBPAYLOAD_SRC))
21 # Build dir and config for libpayload. Need absolute
22 # paths to pass to libpayload's sub-make.
23 LIBPAYLOAD_OBJ ?= $(CURDIR)/libpayload
24 LIBPAYLOAD := $(LIBPAYLOAD_OBJ)/libpayload.a
25 LIBPAYLOAD_CONFIG_H := $(LIBPAYLOAD_OBJ)/libpayload-config.h
26 LIBPAYLOAD_DOTCONFIG ?= $(CURDIR)/.lp.config
27 LIBPAYLOAD_DEFCONFIG ?= $(CURDIR)/$(LIBPAYLOAD_SRC)/configs/defconfig
29 # Some default dependencies for all targets:
30 DEFAULT_DEPS := Makefile $(lastword $(MAKEFILE_LIST))
31 DEFAULT_DEPS += $(PAYLOAD_DEPS)
39 CFLAGS = $(CFLAGS_$(ARCH))
40 CFLAGS += -Os -ffreestanding
41 CFLAGS += -Wall -Wextra -Wmissing-prototypes -Wvla -Werror
47 # Make is silent per default, but `make V=1` will show all calls.
57 ifeq ($(filter %clean,$(MAKECMDGOALS)),)
59 -include $(LIBPAYLOAD_DOTCONFIG)
61 xcompile := $(obj)/xcompile
62 xcompile_script := $(LIBPAYLOAD_SRC)/../../util/xcompile/xcompile
64 # In addition to the dependency below, create the file if it doesn't exist
65 # to silence warnings about a file that would be generated anyway.
66 $(if $(wildcard $(xcompile)),,$(shell \
67 mkdir -p $(dir $(xcompile)) && \
68 $(xcompile_script) $(XGCCPATH) > $(xcompile) || rm -f $(xcompile)))
70 $(xcompile): $(xcompile_script)
75 ifneq ($(XCOMPILE_COMPLETE),1)
76 $(shell rm -f $(XCOMPILE_COMPLETE))
77 $(error $(xcompile) deleted because it's invalid. \
78 Restarting the build should fix that, or explain the problem.)
81 # `lpgcc` in in-tree mode:
82 LPGCC = CC="$(CCACHE) $(GCC_CC_$(ARCH))"
83 LPGCC += _OBJ="$(LIBPAYLOAD_OBJ)"
84 LPGCC += $(LIBPAYLOAD_SRC)/bin/lpgcc
86 LPAS = AS="$(AS_$(ARCH))"
87 LPAS += $(LIBPAYLOAD_SRC)/bin/lpas
89 OBJCOPY = $(OBJCOPY_$(ARCH))
91 $(obj)/%.bin: $(OBJS) $(LIBPAYLOAD) $(DEFAULT_DEPS)
92 @printf " LPGCC $(subst $(obj)/,,$@)\n"
93 $(LPGCC) $(CFLAGS) -o $@ $(OBJS)
95 $(obj)/%.map: $(obj)/%.bin
96 @printf " SYMS $(subst $(obj)/,,$@)\n"
97 $(NM_$(ARCH)) -n $< > $@
99 $(obj)/%.debug: $(obj)/%.bin
100 @printf " DEBUG $(subst $(obj)/,,$@)\n"
101 $(OBJCOPY) --only-keep-debug $< $@
103 .PRECIOUS: $(obj)/%.debug
105 $(obj)/%.elf: $(obj)/%.bin $(obj)/%.debug
106 @printf " STRIP $(subst $(obj)/,,$@)\n"
107 $(OBJCOPY) --strip-$(STRIP) $< $@
108 $(OBJCOPY) --add-gnu-debuglink=$(obj)/$*.debug $@
110 $(obj)/%.o: %.c $(LIBPAYLOAD_CONFIG_H) $(DEFAULT_DEPS)
111 @printf " LPGCC $(subst $(obj)/,,$@)\n"
112 $(LPGCC) -MMD $(CFLAGS) -c $< -o $@
114 $(obj)/%.S.o: %.S $(LIBPAYLOAD_CONFIG_H) $(DEFAULT_DEPS)
115 @printf " LPAS $(subst $(obj)/,,$@)\n"
118 -include $(OBJS:.o=.d)
122 LIBPAYLOAD_OPTS := obj="$(LIBPAYLOAD_OBJ)"
123 LIBPAYLOAD_OPTS += DOTCONFIG="$(LIBPAYLOAD_DOTCONFIG)"
124 LIBPAYLOAD_OPTS += CONFIG_=CONFIG_LP_
125 LIBPAYLOAD_OPTS += $(if $(CCACHE),CONFIG_LP_CCACHE=y)
127 ifneq ($(LIBPAYLOAD_DEFCONFIG),)
128 $(LIBPAYLOAD_DOTCONFIG): $(LIBPAYLOAD_DEFCONFIG)
129 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) \
130 KBUILD_DEFCONFIG=$(LIBPAYLOAD_DEFCONFIG) defconfig
133 $(LIBPAYLOAD_CONFIG_H): $(LIBPAYLOAD_DOTCONFIG)
134 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) $(LIBPAYLOAD_CONFIG_H)
139 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) $*
141 $(LIBPAYLOAD): force-relay | $(LIBPAYLOAD_CONFIG_H)
142 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS)
144 $(shell mkdir -p $(sort $(dir $(OBJS))))
148 else # %clean,$(MAKECMDGOALS)
150 default-payload-clean:
151 rm -rf $(obj) $(LIBPAYLOAD_OBJ)
152 clean: default-payload-clean
154 default-payload-distclean: clean
155 rm -f $(LIBPAYLOAD_DOTCONFIG) $(LIBPAYLOAD_DOTCONFIG).old
156 distclean: default-payload-distclean
158 .PHONY: default-payload-clean clean default-payload-distclean distclean