blob: 421d96b3732c38d3d9694b6fc1d2134343a6e5c1 [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
39CFLAGS = $(GCC_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
59xcompile := $(obj)/xcompile
60xcompile_script := $(LIBPAYLOAD_SRC)/../../util/xcompile/xcompile
61
62# In addition to the dependency below, create the file if it doesn't exist
63# to silence warnings about a file that would be generated anyway.
64$(if $(wildcard $(xcompile)),,$(shell \
65 mkdir -p $(dir $(xcompile)) && \
66 $(xcompile_script) $(XGCCPATH) > $(xcompile) || rm -f $(xcompile)))
67
68$(xcompile): $(xcompile_script)
69 $< $(XGCCPATH) > $@
70
71include $(xcompile)
72
73ifneq ($(XCOMPILE_COMPLETE),1)
74$(shell rm -f $(XCOMPILE_COMPLETE))
75$(error $(xcompile) deleted because it's invalid. \
76 Restarting the build should fix that, or explain the problem.)
77endif
78
79# `lpgcc` in in-tree mode:
80LPGCC = CC="$(CCACHE) $(GCC_CC_$(ARCH))"
81LPGCC += _OBJ="$(LIBPAYLOAD_OBJ)"
82LPGCC += $(LIBPAYLOAD_SRC)/bin/lpgcc
83
84LPAS = AS="$(AS_$(ARCH))"
85LPAS += $(LIBPAYLOAD_SRC)/bin/lpas
86
87OBJCOPY = $(OBJCOPY_$(ARCH))
88
89$(obj)/%.bin: $(OBJS) $(LIBPAYLOAD) $(DEFAULT_DEPS)
90 @printf " LPGCC $(subst $(obj)/,,$@)\n"
91 $(LPGCC) $(CFLAGS) -o $@ $(OBJS)
92
93$(obj)/%.map: $(obj)/%.bin
94 @printf " SYMS $(subst $(obj)/,,$@)\n"
95 $(NM_$(ARCH)) -n $< > $@
96
97$(obj)/%.debug: $(obj)/%.bin
98 @printf " DEBUG $(subst $(obj)/,,$@)\n"
99 $(OBJCOPY) --only-keep-debug $< $@
100
101.PRECIOUS: $(obj)/%.debug
102
103$(obj)/%.elf: $(obj)/%.bin $(obj)/%.debug
104 @printf " STRIP $(subst $(obj)/,,$@)\n"
105 $(OBJCOPY) --strip-$(STRIP) $< $@
106 $(OBJCOPY) --add-gnu-debuglink=$(obj)/$*.debug $@
107
108$(obj)/%.o: %.c $(LIBPAYLOAD_CONFIG_H) $(DEFAULT_DEPS)
109 @printf " LPGCC $(subst $(obj)/,,$@)\n"
110 $(LPGCC) -MMD $(CFLAGS) -c $< -o $@
111
112$(obj)/%.S.o: %.S $(LIBPAYLOAD_CONFIG_H) $(DEFAULT_DEPS)
113 @printf " LPAS $(subst $(obj)/,,$@)\n"
114 $(LPAS) $< -o $@
115
116-include $(OBJS:.o=.d)
117
118.PRECIOUS: $(OBJS)
119
120LIBPAYLOAD_OPTS := obj="$(LIBPAYLOAD_OBJ)"
121LIBPAYLOAD_OPTS += DOTCONFIG="$(LIBPAYLOAD_DOTCONFIG)"
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100122LIBPAYLOAD_OPTS += CONFIG_=CONFIG_LP_
Nico Huber9c8f0422020-11-15 19:18:41 +0100123LIBPAYLOAD_OPTS += $(if $(CCACHE),CONFIG_LP_CCACHE=y)
124
125defconfig: lp-defconfig
126lp-defconfig: $(LIBPAYLOAD_DOTCONFIG)
127$(LIBPAYLOAD_DOTCONFIG): $(LIBPAYLOAD_DEFCONFIG) | $(PAYLOAD_DEPS)
128 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) \
129 KBUILD_DEFCONFIG=$(LIBPAYLOAD_DEFCONFIG) defconfig
130
131$(LIBPAYLOAD_CONFIG_H): $(LIBPAYLOAD_DOTCONFIG)
132 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) $(LIBPAYLOAD_CONFIG_H)
133
134oldconfig: lp-oldconfig
135lp-oldconfig:
136 [ ! -f $(LIBPAYLOAD_DOTCONFIG) ] || \
137 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) oldconfig
138
139$(LIBPAYLOAD): lp-defconfig | $(LIBPAYLOAD_CONFIG_H)
140 $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS)
141
142$(shell mkdir -p $(sort $(dir $(OBJS))))
143
144.PHONY: oldconfig lp-oldconfig defconfig lp-defconfig
145
146else # %clean,$(MAKECMDGOALS)
147
148default-payload-clean:
149 rm -rf $(obj) $(LIBPAYLOAD_OBJ)
150clean: default-payload-clean
151
152default-payload-distclean: clean
153 rm -f $(LIBPAYLOAD_DOTCONFIG) $(LIBPAYLOAD_DOTCONFIG).old
154distclean: default-payload-distclean
155
156.PHONY: default-payload-clean clean default-payload-distclean distclean
157
158endif