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)
41 # Make is silent per default, but `make V=1` will show all calls.
51 ifeq ($(filter %clean,$(MAKECMDGOALS)),)
53 -include $(LIBPAYLOAD_DOTCONFIG)
55 xcompile := $(obj)/xcompile
56 xcompile_script := $(LIBPAYLOAD_SRC)/../../util/xcompile/xcompile
58 # In addition to the dependency below, create the file if it doesn't exist
59 # to silence warnings about a file that would be generated anyway.
60 $(if $(wildcard $(xcompile)),,$(shell \
61 mkdir -p $(dir $(xcompile)) && \
62 $(xcompile_script) $(XGCCPATH) > $(xcompile) || rm -f $(xcompile)))
64 $(xcompile): $(xcompile_script)
69 ifneq ($(XCOMPILE_COMPLETE),1)
70 $(shell rm -f $(XCOMPILE_COMPLETE))
71 $(error $(xcompile) deleted because it's invalid. \
72 Restarting the build should fix that, or explain the problem.)
75 ifeq ($(CONFIG_LP_ARCH_ARM),y)
77 else ifeq ($(CONFIG_LP_ARCH_X86_64),y)
79 else ifeq ($(CONFIG_LP_ARCH_X86_32),y)
81 else ifeq ($(CONFIG_LP_ARCH_ARM64),y)
85 CFLAGS = $(CFLAGS_$(ARCH))
86 CFLAGS += -Os -ffreestanding
87 CFLAGS += -Wall -Wextra -Wmissing-prototypes -Wvla -Werror
88 ifeq ($(CONFIG_LP_LTO),y)
92 # `lpgcc` in in-tree mode:
93 LPGCC = CC="$(CCACHE) $(CC_$(ARCH))"
94 LPGCC += _OBJ="$(LIBPAYLOAD_OBJ)"
95 LPGCC += $(LIBPAYLOAD_SRC)/bin/lpgcc
97 LPAS = AS="$(AS_$(ARCH))"
98 LPAS += $(LIBPAYLOAD_SRC)/bin/lpas
100 OBJCOPY = $(OBJCOPY_$(ARCH))
102 $(obj)/%.bin: $(OBJS) $(LIBPAYLOAD) $(DEFAULT_DEPS)
103 @printf " LPGCC $(subst $(obj)/,,$@)\n"
104 $(LPGCC) $(CFLAGS) -o $@ $(OBJS)
106 $(obj)/%.map: $(obj)/%.bin
107 @printf " SYMS $(subst $(obj)/,,$@)\n"
108 $(NM_$(ARCH)) -n $< > $@
110 $(obj)/%.debug: $(obj)/%.bin
111 @printf " DEBUG $(subst $(obj)/,,$@)\n"
112 $(OBJCOPY) --only-keep-debug $< $@
114 .PRECIOUS: $(obj)/%.debug
116 $(obj)/%.elf: $(obj)/%.bin $(obj)/%.debug
117 @printf " STRIP $(subst $(obj)/,,$@)\n"
118 $(OBJCOPY) --strip-$(STRIP) $< $@
119 $(OBJCOPY) --add-gnu-debuglink=$(obj)/$*.debug $@
121 $(obj)/%.o: %.c $(LIBPAYLOAD_CONFIG_H) $(DEFAULT_DEPS)
122 @printf " LPGCC $(subst $(obj)/,,$@)\n"
123 $(LPGCC) -MMD $(CFLAGS) -c $< -o $@
125 $(obj)/%.S.o: %.S $(LIBPAYLOAD_CONFIG_H) $(DEFAULT_DEPS)
126 @printf " LPAS $(subst $(obj)/,,$@)\n"
129 -include $(OBJS:.o=.d)
133 LIBPAYLOAD_OPTS := obj="$(LIBPAYLOAD_OBJ)"
134 LIBPAYLOAD_OPTS += DOTCONFIG="$(LIBPAYLOAD_DOTCONFIG)"
135 LIBPAYLOAD_OPTS += CONFIG_=CONFIG_LP_
136 LIBPAYLOAD_OPTS += $(if $(CCACHE),CONFIG_LP_CCACHE=y)
138 ifneq ($(LIBPAYLOAD_DEFCONFIG),)
139 $(LIBPAYLOAD_DOTCONFIG): $(LIBPAYLOAD_DEFCONFIG)
140 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) \
141 KBUILD_DEFCONFIG=$(LIBPAYLOAD_DEFCONFIG) defconfig
144 $(LIBPAYLOAD_CONFIG_H): $(LIBPAYLOAD_DOTCONFIG)
145 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) $(LIBPAYLOAD_CONFIG_H)
150 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) $*
152 $(LIBPAYLOAD): force-relay | $(LIBPAYLOAD_CONFIG_H)
153 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS)
155 $(shell mkdir -p $(sort $(dir $(OBJS))))
159 else # %clean,$(MAKECMDGOALS)
161 default-payload-clean:
162 rm -rf $(obj) $(LIBPAYLOAD_OBJ)
163 clean: default-payload-clean
165 default-payload-distclean: clean
166 rm -f $(LIBPAYLOAD_DOTCONFIG) $(LIBPAYLOAD_DOTCONFIG).old
167 distclean: default-payload-distclean
169 .PHONY: default-payload-clean clean default-payload-distclean distclean