Nico Huber | 9c8f042 | 2020-11-15 19:18:41 +0100 | [diff] [blame] | 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). |
| 18 | LIBPAYLOAD_SRC := $(dir $(lastword $(MAKEFILE_LIST))) |
| 19 | LIBPAYLOAD_SRC := $(patsubst %/,%,$(LIBPAYLOAD_SRC)) |
| 20 | |
| 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 |
| 28 | |
| 29 | # Some default dependencies for all targets: |
| 30 | DEFAULT_DEPS := Makefile $(lastword $(MAKEFILE_LIST)) |
| 31 | DEFAULT_DEPS += $(PAYLOAD_DEPS) |
| 32 | |
| 33 | obj ?= build |
| 34 | |
| 35 | ARCH ?= |
| 36 | OBJS ?= |
| 37 | CCACHE ?= |
| 38 | |
| 39 | CFLAGS = $(GCC_CFLAGS_$(ARCH)) |
| 40 | CFLAGS += -Os -ffreestanding |
| 41 | CFLAGS += -Wall -Wextra -Wmissing-prototypes -Wvla -Werror |
| 42 | |
| 43 | STRIP ?= debug |
| 44 | |
| 45 | $(TARGET): |
| 46 | |
| 47 | # Make is silent per default, but `make V=1` will show all calls. |
| 48 | Q:=@ |
| 49 | ifneq ($(V),1) |
| 50 | ifneq ($(Q),) |
| 51 | .SILENT: |
| 52 | MAKEFLAGS += -s |
| 53 | endif |
| 54 | endif |
| 55 | export V |
| 56 | |
| 57 | ifeq ($(filter %clean,$(MAKECMDGOALS)),) |
| 58 | |
Nico Huber | 27c1da6 | 2022-02-22 13:58:26 +0100 | [diff] [blame^] | 59 | -include $(LIBPAYLOAD_DOTCONFIG) |
| 60 | |
Nico Huber | 9c8f042 | 2020-11-15 19:18:41 +0100 | [diff] [blame] | 61 | xcompile := $(obj)/xcompile |
| 62 | xcompile_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 | |
| 73 | include $(xcompile) |
| 74 | |
| 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.) |
| 79 | endif |
| 80 | |
| 81 | # `lpgcc` in in-tree mode: |
| 82 | LPGCC = CC="$(CCACHE) $(GCC_CC_$(ARCH))" |
| 83 | LPGCC += _OBJ="$(LIBPAYLOAD_OBJ)" |
| 84 | LPGCC += $(LIBPAYLOAD_SRC)/bin/lpgcc |
| 85 | |
| 86 | LPAS = AS="$(AS_$(ARCH))" |
| 87 | LPAS += $(LIBPAYLOAD_SRC)/bin/lpas |
| 88 | |
| 89 | OBJCOPY = $(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 | |
| 122 | LIBPAYLOAD_OPTS := obj="$(LIBPAYLOAD_OBJ)" |
| 123 | LIBPAYLOAD_OPTS += DOTCONFIG="$(LIBPAYLOAD_DOTCONFIG)" |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 124 | LIBPAYLOAD_OPTS += CONFIG_=CONFIG_LP_ |
Nico Huber | 9c8f042 | 2020-11-15 19:18:41 +0100 | [diff] [blame] | 125 | LIBPAYLOAD_OPTS += $(if $(CCACHE),CONFIG_LP_CCACHE=y) |
| 126 | |
Nico Huber | 543c792 | 2022-02-22 18:58:48 +0100 | [diff] [blame] | 127 | ifneq ($(LIBPAYLOAD_DEFCONFIG),) |
| 128 | $(LIBPAYLOAD_DOTCONFIG): $(LIBPAYLOAD_DEFCONFIG) |
Nico Huber | 9c8f042 | 2020-11-15 19:18:41 +0100 | [diff] [blame] | 129 | $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) \ |
| 130 | KBUILD_DEFCONFIG=$(LIBPAYLOAD_DEFCONFIG) defconfig |
Nico Huber | 543c792 | 2022-02-22 18:58:48 +0100 | [diff] [blame] | 131 | endif |
Nico Huber | 9c8f042 | 2020-11-15 19:18:41 +0100 | [diff] [blame] | 132 | |
| 133 | $(LIBPAYLOAD_CONFIG_H): $(LIBPAYLOAD_DOTCONFIG) |
| 134 | $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) $(LIBPAYLOAD_CONFIG_H) |
| 135 | |
Nico Huber | 543c792 | 2022-02-22 18:58:48 +0100 | [diff] [blame] | 136 | force-relay: |
Nico Huber | 9c8f042 | 2020-11-15 19:18:41 +0100 | [diff] [blame] | 137 | |
Nico Huber | 543c792 | 2022-02-22 18:58:48 +0100 | [diff] [blame] | 138 | lp-%: force-relay |
| 139 | $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) $* |
| 140 | |
| 141 | $(LIBPAYLOAD): force-relay | $(LIBPAYLOAD_CONFIG_H) |
Nico Huber | 9c8f042 | 2020-11-15 19:18:41 +0100 | [diff] [blame] | 142 | $(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) |
| 143 | |
| 144 | $(shell mkdir -p $(sort $(dir $(OBJS)))) |
| 145 | |
Nico Huber | 543c792 | 2022-02-22 18:58:48 +0100 | [diff] [blame] | 146 | .PHONY: force-relay |
Nico Huber | 9c8f042 | 2020-11-15 19:18:41 +0100 | [diff] [blame] | 147 | |
| 148 | else # %clean,$(MAKECMDGOALS) |
| 149 | |
| 150 | default-payload-clean: |
| 151 | rm -rf $(obj) $(LIBPAYLOAD_OBJ) |
| 152 | clean: default-payload-clean |
| 153 | |
| 154 | default-payload-distclean: clean |
| 155 | rm -f $(LIBPAYLOAD_DOTCONFIG) $(LIBPAYLOAD_DOTCONFIG).old |
| 156 | distclean: default-payload-distclean |
| 157 | |
| 158 | .PHONY: default-payload-clean clean default-payload-distclean distclean |
| 159 | |
| 160 | endif |