1*6008105bSBenjamin Tissoires# SPDX-License-Identifier: GPL-2.0 2*6008105bSBenjamin Tissoires# ========================================================================== 3*6008105bSBenjamin Tissoires# Building binaries on the host system 4*6008105bSBenjamin Tissoires# Binaries are not used during the compilation of the kernel, and intended 5*6008105bSBenjamin Tissoires# to be build for target board, target board can be host of course. Added to 6*6008105bSBenjamin Tissoires# build binaries to run not on host system. 7*6008105bSBenjamin Tissoires# 8*6008105bSBenjamin Tissoires# Sample syntax 9*6008105bSBenjamin Tissoires# tprogs-y := xsk_example 10*6008105bSBenjamin Tissoires# Will compile xsk_example.c and create an executable named xsk_example 11*6008105bSBenjamin Tissoires# 12*6008105bSBenjamin Tissoires# tprogs-y := xdpsock 13*6008105bSBenjamin Tissoires# xdpsock-objs := xdpsock_1.o xdpsock_2.o 14*6008105bSBenjamin Tissoires# Will compile xdpsock_1.c and xdpsock_2.c, and then link the executable 15*6008105bSBenjamin Tissoires# xdpsock, based on xdpsock_1.o and xdpsock_2.o 16*6008105bSBenjamin Tissoires# 17*6008105bSBenjamin Tissoires# Derived from scripts/Makefile.host 18*6008105bSBenjamin Tissoires# 19*6008105bSBenjamin Tissoires__tprogs := $(sort $(tprogs-y)) 20*6008105bSBenjamin Tissoires 21*6008105bSBenjamin Tissoires# C code 22*6008105bSBenjamin Tissoires# Executables compiled from a single .c file 23*6008105bSBenjamin Tissoirestprog-csingle := $(foreach m,$(__tprogs), \ 24*6008105bSBenjamin Tissoires $(if $($(m)-objs),,$(m))) 25*6008105bSBenjamin Tissoires 26*6008105bSBenjamin Tissoires# C executables linked based on several .o files 27*6008105bSBenjamin Tissoirestprog-cmulti := $(foreach m,$(__tprogs),\ 28*6008105bSBenjamin Tissoires $(if $($(m)-objs),$(m))) 29*6008105bSBenjamin Tissoires 30*6008105bSBenjamin Tissoires# Object (.o) files compiled from .c files 31*6008105bSBenjamin Tissoirestprog-cobjs := $(sort $(foreach m,$(__tprogs),$($(m)-objs))) 32*6008105bSBenjamin Tissoires 33*6008105bSBenjamin Tissoirestprog-csingle := $(addprefix $(obj)/,$(tprog-csingle)) 34*6008105bSBenjamin Tissoirestprog-cmulti := $(addprefix $(obj)/,$(tprog-cmulti)) 35*6008105bSBenjamin Tissoirestprog-cobjs := $(addprefix $(obj)/,$(tprog-cobjs)) 36*6008105bSBenjamin Tissoires 37*6008105bSBenjamin Tissoires##### 38*6008105bSBenjamin Tissoires# Handle options to gcc. Support building with separate output directory 39*6008105bSBenjamin Tissoires 40*6008105bSBenjamin Tissoires_tprogc_flags = $(TPROGS_CFLAGS) \ 41*6008105bSBenjamin Tissoires $(TPROGCFLAGS_$(basetarget).o) 42*6008105bSBenjamin Tissoires 43*6008105bSBenjamin Tissoires# $(objtree)/$(obj) for including generated headers from checkin source files 44*6008105bSBenjamin Tissoiresifeq ($(KBUILD_EXTMOD),) 45*6008105bSBenjamin Tissoiresifdef building_out_of_srctree 46*6008105bSBenjamin Tissoires_tprogc_flags += -I $(objtree)/$(obj) 47*6008105bSBenjamin Tissoiresendif 48*6008105bSBenjamin Tissoiresendif 49*6008105bSBenjamin Tissoires 50*6008105bSBenjamin Tissoirestprogc_flags = -Wp,-MD,$(depfile) $(_tprogc_flags) 51*6008105bSBenjamin Tissoires 52*6008105bSBenjamin Tissoires# Create executable from a single .c file 53*6008105bSBenjamin Tissoires# tprog-csingle -> Executable 54*6008105bSBenjamin Tissoiresquiet_cmd_tprog-csingle = CC $@ 55*6008105bSBenjamin Tissoires cmd_tprog-csingle = $(CC) $(tprogc_flags) $(TPROGS_LDFLAGS) -o $@ $< \ 56*6008105bSBenjamin Tissoires $(TPROGS_LDLIBS) $(TPROGLDLIBS_$(@F)) 57*6008105bSBenjamin Tissoires$(tprog-csingle): $(obj)/%: $(src)/%.c FORCE 58*6008105bSBenjamin Tissoires $(call if_changed_dep,tprog-csingle) 59*6008105bSBenjamin Tissoires 60*6008105bSBenjamin Tissoires# Link an executable based on list of .o files, all plain c 61*6008105bSBenjamin Tissoires# tprog-cmulti -> executable 62*6008105bSBenjamin Tissoiresquiet_cmd_tprog-cmulti = LD $@ 63*6008105bSBenjamin Tissoires cmd_tprog-cmulti = $(CC) $(tprogc_flags) $(TPROGS_LDFLAGS) -o $@ \ 64*6008105bSBenjamin Tissoires $(addprefix $(obj)/,$($(@F)-objs)) \ 65*6008105bSBenjamin Tissoires $(TPROGS_LDLIBS) $(TPROGLDLIBS_$(@F)) 66*6008105bSBenjamin Tissoires$(tprog-cmulti): $(tprog-cobjs) FORCE 67*6008105bSBenjamin Tissoires $(call if_changed,tprog-cmulti) 68*6008105bSBenjamin Tissoires$(call multi_depend, $(tprog-cmulti), , -objs) 69*6008105bSBenjamin Tissoires 70*6008105bSBenjamin Tissoires# Create .o file from a single .c file 71*6008105bSBenjamin Tissoires# tprog-cobjs -> .o 72*6008105bSBenjamin Tissoiresquiet_cmd_tprog-cobjs = CC $@ 73*6008105bSBenjamin Tissoires cmd_tprog-cobjs = $(CC) $(tprogc_flags) -c -o $@ $< 74*6008105bSBenjamin Tissoires$(tprog-cobjs): $(obj)/%.o: $(src)/%.c FORCE 75*6008105bSBenjamin Tissoires $(call if_changed_dep,tprog-cobjs) 76