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