blob: fa98d8a4de94f157a994f8493b5193ffa67711d7 [file] [log] [blame]
Nico Huber9c8f0422020-11-15 19:18:41 +01001# 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
Nico Hubere1e76272022-02-22 02:09:43 +010039CFLAGS = $(CFLAGS_$(ARCH))
Nico Huber9c8f0422020-11-15 19:18:41 +010040CFLAGS += -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
Nico Huber27c1da62022-02-22 13:58:26 +010059-include $(LIBPAYLOAD_DOTCONFIG)
60
Nico Huber9c8f0422020-11-15 19:18:41 +010061xcompile := $(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)"
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100124LIBPAYLOAD_OPTS += CONFIG_=CONFIG_LP_
Nico Huber9c8f0422020-11-15 19:18:41 +0100125LIBPAYLOAD_OPTS += $(if $(CCACHE),CONFIG_LP_CCACHE=y)
126
Nico Huber543c7922022-02-22 18:58:48 +0100127ifneq ($(LIBPAYLOAD_DEFCONFIG),)
128$(LIBPAYLOAD_DOTCONFIG): $(LIBPAYLOAD_DEFCONFIG)
Nico Huber9c8f0422020-11-15 19:18:41 +0100129 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) \
130 KBUILD_DEFCONFIG=$(LIBPAYLOAD_DEFCONFIG) defconfig
Nico Huber543c7922022-02-22 18:58:48 +0100131endif
Nico Huber9c8f0422020-11-15 19:18:41 +0100132
133$(LIBPAYLOAD_CONFIG_H): $(LIBPAYLOAD_DOTCONFIG)
134 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) $(LIBPAYLOAD_CONFIG_H)
135
Nico Huber543c7922022-02-22 18:58:48 +0100136force-relay:
Nico Huber9c8f0422020-11-15 19:18:41 +0100137
Nico Huber543c7922022-02-22 18:58:48 +0100138lp-%: force-relay
139 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) $*
140
141$(LIBPAYLOAD): force-relay | $(LIBPAYLOAD_CONFIG_H)
Nico Huber9c8f0422020-11-15 19:18:41 +0100142 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS)
143
144$(shell mkdir -p $(sort $(dir $(OBJS))))
145
Nico Huber543c7922022-02-22 18:58:48 +0100146.PHONY: force-relay
Nico Huber9c8f0422020-11-15 19:18:41 +0100147
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