^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) # LEX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) # ---------------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) quiet_cmd_flex = LEX $@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) cmd_flex = $(LEX) -o$@ -L $<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) $(obj)/%.lex.c: $(src)/%.l FORCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) $(call if_changed,flex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) # YACC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) # ---------------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) quiet_cmd_bison = YACC $(basename $@).[ch]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) cmd_bison = $(YACC) -o $(basename $@).c --defines=$(basename $@).h -t -l $<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) $(obj)/%.tab.c $(obj)/%.tab.h: $(src)/%.y FORCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) $(call if_changed,bison)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) # ==========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) # Building binaries on the host system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) # Binaries are used during the compilation of the kernel, for example
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) # to preprocess a data file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) # Both C and C++ are supported, but preferred language is C for such utilities.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) # Sample syntax (see Documentation/kbuild/makefiles.rst for reference)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) # hostprogs := bin2hex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) # Will compile bin2hex.c and create an executable named bin2hex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) # hostprogs := lxdialog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) # lxdialog-objs := checklist.o lxdialog.o
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) # Will compile lxdialog.c and checklist.c, and then link the executable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) # lxdialog, based on checklist.o and lxdialog.o
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) # hostprogs := qconf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) # qconf-cxxobjs := qconf.o
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) # qconf-objs := menu.o
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) # Will compile qconf as a C++ program, and menu as a C program.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) # They are linked as C++ code to the executable qconf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) # C code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) # Executables compiled from a single .c file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) host-csingle := $(foreach m,$(hostprogs), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) $(if $($(m)-objs)$($(m)-cxxobjs),,$(m)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) # C executables linked based on several .o files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) host-cmulti := $(foreach m,$(hostprogs),\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) $(if $($(m)-cxxobjs),,$(if $($(m)-objs),$(m))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) # Object (.o) files compiled from .c files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) host-cobjs := $(sort $(foreach m,$(hostprogs),$($(m)-objs)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) # C++ code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) # C++ executables compiled from at least one .cc file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) # and zero or more .c files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) host-cxxmulti := $(foreach m,$(hostprogs),$(if $($(m)-cxxobjs),$(m)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) # C++ Object (.o) files compiled from .cc files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) host-cxxobjs := $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) host-csingle := $(addprefix $(obj)/,$(host-csingle))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) host-cmulti := $(addprefix $(obj)/,$(host-cmulti))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) host-cobjs := $(addprefix $(obj)/,$(host-cobjs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) host-cxxmulti := $(addprefix $(obj)/,$(host-cxxmulti))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) host-cxxobjs := $(addprefix $(obj)/,$(host-cxxobjs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #####
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) # Handle options to gcc. Support building with separate output directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) _hostc_flags = $(KBUILD_HOSTCFLAGS) $(HOST_EXTRACFLAGS) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) $(HOSTCFLAGS_$(target-stem).o)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) _hostcxx_flags = $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) $(HOSTCXXFLAGS_$(target-stem).o)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) # $(objtree)/$(obj) for including generated headers from checkin source files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) ifeq ($(KBUILD_EXTMOD),)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) ifdef building_out_of_srctree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) _hostc_flags += -I $(objtree)/$(obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) _hostcxx_flags += -I $(objtree)/$(obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) hostc_flags = -Wp,-MMD,$(depfile) $(_hostc_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) hostcxx_flags = -Wp,-MMD,$(depfile) $(_hostcxx_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #####
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) # Compile programs on the host
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) # Create executable from a single .c file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) # host-csingle -> Executable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) quiet_cmd_host-csingle = HOSTCC $@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) cmd_host-csingle = $(HOSTCC) $(hostc_flags) $(KBUILD_HOSTLDFLAGS) -o $@ $< \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) $(host-csingle): $(obj)/%: $(src)/%.c FORCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) $(call if_changed_dep,host-csingle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) # Link an executable based on list of .o files, all plain c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) # host-cmulti -> executable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) quiet_cmd_host-cmulti = HOSTLD $@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) cmd_host-cmulti = $(HOSTCC) $(KBUILD_HOSTLDFLAGS) -o $@ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) $(addprefix $(obj)/, $($(target-stem)-objs)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) $(host-cmulti): FORCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) $(call if_changed,host-cmulti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) $(call multi_depend, $(host-cmulti), , -objs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) # Create .o file from a single .c file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) # host-cobjs -> .o
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) quiet_cmd_host-cobjs = HOSTCC $@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) cmd_host-cobjs = $(HOSTCC) $(hostc_flags) -c -o $@ $<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) $(host-cobjs): $(obj)/%.o: $(src)/%.c FORCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) $(call if_changed_dep,host-cobjs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) # Link an executable based on list of .o files, a mixture of .c and .cc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) # host-cxxmulti -> executable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) quiet_cmd_host-cxxmulti = HOSTLD $@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) cmd_host-cxxmulti = $(HOSTCXX) $(KBUILD_HOSTLDFLAGS) -o $@ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) $(foreach o,objs cxxobjs,\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) $(addprefix $(obj)/, $($(target-stem)-$(o)))) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) $(host-cxxmulti): FORCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) $(call if_changed,host-cxxmulti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) $(call multi_depend, $(host-cxxmulti), , -objs -cxxobjs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) # Create .o file from a single .cc (C++) file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) quiet_cmd_host-cxxobjs = HOSTCXX $@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) cmd_host-cxxobjs = $(HOSTCXX) $(hostcxx_flags) -c -o $@ $<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) $(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) $(call if_changed_dep,host-cxxobjs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) targets += $(host-csingle) $(host-cmulti) $(host-cobjs) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) $(host-cxxmulti) $(host-cxxobjs)