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 | |
| 59 | xcompile := $(obj)/xcompile |
| 60 | xcompile_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 | |
| 71 | include $(xcompile) |
| 72 | |
| 73 | ifneq ($(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.) |
| 77 | endif |
| 78 | |
| 79 | # `lpgcc` in in-tree mode: |
| 80 | LPGCC = CC="$(CCACHE) $(GCC_CC_$(ARCH))" |
| 81 | LPGCC += _OBJ="$(LIBPAYLOAD_OBJ)" |
| 82 | LPGCC += $(LIBPAYLOAD_SRC)/bin/lpgcc |
| 83 | |
| 84 | LPAS = AS="$(AS_$(ARCH))" |
| 85 | LPAS += $(LIBPAYLOAD_SRC)/bin/lpas |
| 86 | |
| 87 | OBJCOPY = $(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 | |
| 120 | LIBPAYLOAD_OPTS := obj="$(LIBPAYLOAD_OBJ)" |
| 121 | LIBPAYLOAD_OPTS += DOTCONFIG="$(LIBPAYLOAD_DOTCONFIG)" |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 122 | LIBPAYLOAD_OPTS += CONFIG_=CONFIG_LP_ |
Nico Huber | 9c8f042 | 2020-11-15 19:18:41 +0100 | [diff] [blame] | 123 | LIBPAYLOAD_OPTS += $(if $(CCACHE),CONFIG_LP_CCACHE=y) |
| 124 | |
| 125 | defconfig: lp-defconfig |
| 126 | lp-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 | |
| 134 | oldconfig: lp-oldconfig |
| 135 | lp-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 | |
| 146 | else # %clean,$(MAKECMDGOALS) |
| 147 | |
| 148 | default-payload-clean: |
| 149 | rm -rf $(obj) $(LIBPAYLOAD_OBJ) |
| 150 | clean: default-payload-clean |
| 151 | |
| 152 | default-payload-distclean: clean |
| 153 | rm -f $(LIBPAYLOAD_DOTCONFIG) $(LIBPAYLOAD_DOTCONFIG).old |
| 154 | distclean: default-payload-distclean |
| 155 | |
| 156 | .PHONY: default-payload-clean clean default-payload-distclean distclean |
| 157 | |
| 158 | endif |