/ payloads / libpayload / Makefile.payload
Makefile.payload
  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  OBJS ?=
 36  CCACHE ?=
 37  STRIP ?= debug
 38  
 39  $(TARGET):
 40  
 41  # Make is silent per default, but `make V=1` will show all calls.
 42  Q:=@
 43  ifneq ($(V),1)
 44  ifneq ($(Q),)
 45  .SILENT:
 46  MAKEFLAGS += -s
 47  endif
 48  endif
 49  export V
 50  
 51  ifeq ($(filter %clean,$(MAKECMDGOALS)),)
 52  
 53  -include $(LIBPAYLOAD_DOTCONFIG)
 54  
 55  xcompile	:= $(obj)/xcompile
 56  xcompile_script	:= $(LIBPAYLOAD_SRC)/../../util/xcompile/xcompile
 57  
 58  # In addition to the dependency below, create the file if it doesn't exist
 59  # to silence warnings about a file that would be generated anyway.
 60  $(if $(wildcard $(xcompile)),,$(shell	\
 61  	mkdir -p $(dir $(xcompile)) &&	\
 62  	$(xcompile_script) $(XGCCPATH) > $(xcompile) ||	rm -f $(xcompile)))
 63  
 64  $(xcompile): $(xcompile_script)
 65  	$< $(XGCCPATH) > $@
 66  
 67  include $(xcompile)
 68  
 69  ifneq ($(XCOMPILE_COMPLETE),1)
 70  $(shell rm -f $(XCOMPILE_COMPLETE))
 71  $(error $(xcompile) deleted because it's invalid. \
 72          Restarting the build should fix that, or explain the problem.)
 73  endif
 74  
 75  ifeq ($(CONFIG_LP_ARCH_ARM),y)
 76  ARCH ?= arm
 77  else ifeq ($(CONFIG_LP_ARCH_X86_64),y)
 78  ARCH ?= x86_64
 79  else ifeq ($(CONFIG_LP_ARCH_X86_32),y)
 80  ARCH ?= x86_32
 81  else ifeq ($(CONFIG_LP_ARCH_ARM64),y)
 82  ARCH ?= arm64
 83  endif
 84  
 85  CFLAGS  = $(CFLAGS_$(ARCH))
 86  CFLAGS += -Os -ffreestanding
 87  CFLAGS += -Wall -Wextra -Wmissing-prototypes -Wvla -Werror
 88  ifeq ($(CONFIG_LP_LTO),y)
 89  CFLAGS += -flto
 90  endif
 91  
 92  # `lpgcc` in in-tree mode:
 93  LPGCC  = CC="$(CCACHE) $(CC_$(ARCH))"
 94  LPGCC += _OBJ="$(LIBPAYLOAD_OBJ)"
 95  LPGCC += $(LIBPAYLOAD_SRC)/bin/lpgcc
 96  
 97  LPAS  = AS="$(AS_$(ARCH))"
 98  LPAS += $(LIBPAYLOAD_SRC)/bin/lpas
 99  
100  OBJCOPY = $(OBJCOPY_$(ARCH))
101  
102  $(obj)/%.bin: $(OBJS) $(LIBPAYLOAD) $(DEFAULT_DEPS)
103  	@printf "    LPGCC      $(subst $(obj)/,,$@)\n"
104  	$(LPGCC) $(CFLAGS) -o $@ $(OBJS)
105  
106  $(obj)/%.map: $(obj)/%.bin
107  	@printf "    SYMS       $(subst $(obj)/,,$@)\n"
108  	$(NM_$(ARCH)) -n $< > $@
109  
110  $(obj)/%.debug: $(obj)/%.bin
111  	@printf "    DEBUG      $(subst $(obj)/,,$@)\n"
112  	$(OBJCOPY) --only-keep-debug $< $@
113  
114  .PRECIOUS: $(obj)/%.debug
115  
116  $(obj)/%.elf: $(obj)/%.bin $(obj)/%.debug
117  	@printf "    STRIP      $(subst $(obj)/,,$@)\n"
118  	$(OBJCOPY) --strip-$(STRIP) $< $@
119  	$(OBJCOPY) --add-gnu-debuglink=$(obj)/$*.debug $@
120  
121  $(obj)/%.o: %.c $(LIBPAYLOAD_CONFIG_H) $(DEFAULT_DEPS)
122  	@printf "    LPGCC      $(subst $(obj)/,,$@)\n"
123  	$(LPGCC) -MMD $(CFLAGS) -c $< -o $@
124  
125  $(obj)/%.S.o: %.S $(LIBPAYLOAD_CONFIG_H) $(DEFAULT_DEPS)
126  	@printf "    LPAS       $(subst $(obj)/,,$@)\n"
127  	$(LPAS) $< -o $@
128  
129  -include $(OBJS:.o=.d)
130  
131  .PRECIOUS: $(OBJS)
132  
133  LIBPAYLOAD_OPTS := obj="$(LIBPAYLOAD_OBJ)"
134  LIBPAYLOAD_OPTS += DOTCONFIG="$(LIBPAYLOAD_DOTCONFIG)"
135  LIBPAYLOAD_OPTS += CONFIG_=CONFIG_LP_
136  LIBPAYLOAD_OPTS += $(if $(CCACHE),CONFIG_LP_CCACHE=y)
137  
138  ifneq ($(LIBPAYLOAD_DEFCONFIG),)
139  $(LIBPAYLOAD_DOTCONFIG): $(LIBPAYLOAD_DEFCONFIG)
140  	$(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) \
141  		KBUILD_DEFCONFIG=$(LIBPAYLOAD_DEFCONFIG) defconfig
142  endif
143  
144  $(LIBPAYLOAD_CONFIG_H): $(LIBPAYLOAD_DOTCONFIG)
145  	$(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) $(LIBPAYLOAD_CONFIG_H)
146  
147  force-relay:
148  
149  lp-%: force-relay
150  	$(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS) $*
151  
152  $(LIBPAYLOAD): force-relay | $(LIBPAYLOAD_CONFIG_H)
153  	$(MAKE) -C $(LIBPAYLOAD_SRC) $(LIBPAYLOAD_OPTS)
154  
155  $(shell mkdir -p $(sort $(dir $(OBJS))))
156  
157  .PHONY: force-relay
158  
159  else # %clean,$(MAKECMDGOALS)
160  
161  default-payload-clean:
162  	rm -rf $(obj) $(LIBPAYLOAD_OBJ)
163  clean: default-payload-clean
164  
165  default-payload-distclean: clean
166  	rm -f $(LIBPAYLOAD_DOTCONFIG) $(LIBPAYLOAD_DOTCONFIG).old
167  distclean: default-payload-distclean
168  
169  .PHONY: default-payload-clean clean default-payload-distclean distclean
170  
171  endif