1# SPDX-License-Identifier: BSD-3-Clause 2 3# 4# This file is meant to be included by in-tree payloads 5# to provide default targets for incremental builds. 6# 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 10# 11# obj := output 12# OBJS := $(obj)/payload.o 13# TARGET := $(obj)/payload.elf 14# include ../path/to/libpayload/Makefile.payload 15# 16 17# Find relative path to libpayload (where this Makefile resides). 18LIBPAYLOAD_SRC := $(dir $(lastword $(MAKEFILE_LIST))) 19LIBPAYLOAD_SRC := $(patsubst %/,%,$(LIBPAYLOAD_SRC)) 20 21# Build dir and config for libpayload. Need absolute 22# paths to pass to libpayload's sub-make. 23LIBPAYLOAD_OBJ ?= $(CURDIR)/libpayload 24LIBPAYLOAD := $(LIBPAYLOAD_OBJ)/libpayload.a 25LIBPAYLOAD_CONFIG_H := $(LIBPAYLOAD_OBJ)/libpayload-config.h 26LIBPAYLOAD_DOTCONFIG ?= $(CURDIR)/.lp.config 27LIBPAYLOAD_DEFCONFIG ?= $(CURDIR)/$(LIBPAYLOAD_SRC)/configs/defconfig 28 29# Some default dependencies for all targets: 30DEFAULT_DEPS := Makefile $(lastword $(MAKEFILE_LIST)) 31DEFAULT_DEPS += $(PAYLOAD_DEPS) 32 33obj ?= build 34 35ARCH ?= 36OBJS ?= 37CCACHE ?= 38 39CFLAGS = $(CFLAGS_$(ARCH)) 40CFLAGS += -Os -ffreestanding 41CFLAGS += -Wall -Wextra -Wmissing-prototypes -Wvla -Werror 42 43STRIP ?= debug 44 45$(TARGET): 46 47# Make is silent per default, but `make V=1` will show all calls. 48Q:=@ 49ifneq ($(V),1) 50ifneq ($(Q),) 51.SILENT: 52MAKEFLAGS += -s 53endif 54endif 55export V 56 57ifeq ($(filter %clean,$(MAKECMDGOALS)),) 58 59-include $(LIBPAYLOAD_DOTCONFIG) 60 61xcompile := $(obj)/xcompile 62xcompile_script := $(LIBPAYLOAD_SRC)/../../util/xcompile/xcompile 63 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))) 69 70$(xcompile): $(xcompile_script) 71 $< $(XGCCPATH) > $@ 72 73include $(xcompile) 74 75ifneq ($(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.) 79endif 80 81# `lpgcc` in in-tree mode: 82LPGCC = CC="$(CCACHE) $(GCC_CC_$(ARCH))" 83LPGCC += _OBJ="$(LIBPAYLOAD_OBJ)" 84LPGCC += $(LIBPAYLOAD_SRC)/bin/lpgcc 85 86LPAS = AS="$(AS_$(ARCH))" 87LPAS += $(LIBPAYLOAD_SRC)/bin/lpas 88 89OBJCOPY = $(OBJCOPY_$(ARCH)) 90 91$(obj)/%.bin: $(OBJS) $(LIBPAYLOAD) $(DEFAULT_DEPS) 92 @printf " LPGCC $(subst $(obj)/,,$@)\n" 93 $(LPGCC) $(CFLAGS) -o $@ $(OBJS) 94 95$(obj)/%.map: $(obj)/%.bin 96 @printf " SYMS $(subst $(obj)/,,$@)\n" 97 $(NM_$(ARCH)) -n $< > $@ 98 99$(obj)/%.debug: $(obj)/%.bin 100 @printf " DEBUG $(subst $(obj)/,,$@)\n" 101 $(OBJCOPY) --only-keep-debug $< $@ 102 103.PRECIOUS: $(obj)/%.debug 104 105$(obj)/%.elf: $(obj)/%.bin $(obj)/%.debug 106 @printf " STRIP $(subst $(obj)/,,$@)\n" 107 $(OBJCOPY) --strip-$(STRIP) $< $@ 108 $(OBJCOPY) --add-gnu-debuglink=$(obj)/$*.debug $@ 109 110$(obj)/%.o: %.c $(LIBPAYLOAD_CONFIG_H) $(DEFAULT_DEPS) 111 @printf " LPGCC $(subst $(obj)/,,$@)\n" 112 $(LPGCC) -MMD $(CFLAGS) -c $< -o $@ 113 114$(obj)/%.S.o: %.S $(LIBPAYLOAD_CONFIG_H) $(DEFAULT_DEPS) 115 @printf " LPAS $(subst $(obj)/,,$@)\n" 116 $(LPAS) $< -o $@ 117 118-include $(OBJS:.o=.d) 119 120.PRECIOUS: $(OBJS) 121 122LIBPAYLOAD_OPTS := obj="$(LIBPAYLOAD_OBJ)" 123LIBPAYLOAD_OPTS += DOTCONFIG="$(LIBPAYLOAD_DOTCONFIG)" 124LIBPAYLOAD_OPTS += CONFIG_=CONFIG_LP_ 125LIBPAYLOAD_OPTS += $(if $(CCACHE),CONFIG_LP_CCACHE=y) 126 127ifneq ($(LIBPAYLOAD_DEFCONFIG),) 128$(LIBPAYLOAD_DOTCONFIG): $(LIBPAYLOAD_DEFCONFIG) 129 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) \ 130 KBUILD_DEFCONFIG=$(LIBPAYLOAD_DEFCONFIG) defconfig 131endif 132 133$(LIBPAYLOAD_CONFIG_H): $(LIBPAYLOAD_DOTCONFIG) 134 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) $(LIBPAYLOAD_CONFIG_H) 135 136force-relay: 137 138lp-%: force-relay 139 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) $* 140 141$(LIBPAYLOAD): force-relay | $(LIBPAYLOAD_CONFIG_H) 142 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) 143 144$(shell mkdir -p $(sort $(dir $(OBJS)))) 145 146.PHONY: force-relay 147 148else # %clean,$(MAKECMDGOALS) 149 150default-payload-clean: 151 rm -rf $(obj) $(LIBPAYLOAD_OBJ) 152clean: default-payload-clean 153 154default-payload-distclean: clean 155 rm -f $(LIBPAYLOAD_DOTCONFIG) $(LIBPAYLOAD_DOTCONFIG).old 156distclean: default-payload-distclean 157 158.PHONY: default-payload-clean clean default-payload-distclean distclean 159 160endif 161