18c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci# LEX
48c2ecf20Sopenharmony_ci# ---------------------------------------------------------------------------
58c2ecf20Sopenharmony_ciquiet_cmd_flex = LEX     $@
68c2ecf20Sopenharmony_ci      cmd_flex = $(LEX) -o$@ -L $<
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci$(obj)/%.lex.c: $(src)/%.l FORCE
98c2ecf20Sopenharmony_ci	$(call if_changed,flex)
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci# YACC
128c2ecf20Sopenharmony_ci# ---------------------------------------------------------------------------
138c2ecf20Sopenharmony_ciquiet_cmd_bison = YACC    $(basename $@).[ch]
148c2ecf20Sopenharmony_ci      cmd_bison = $(YACC) -o $(basename $@).c --defines=$(basename $@).h -t -l $<
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci$(obj)/%.tab.c $(obj)/%.tab.h: $(src)/%.y FORCE
178c2ecf20Sopenharmony_ci	$(call if_changed,bison)
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci# ==========================================================================
208c2ecf20Sopenharmony_ci# Building binaries on the host system
218c2ecf20Sopenharmony_ci# Binaries are used during the compilation of the kernel, for example
228c2ecf20Sopenharmony_ci# to preprocess a data file.
238c2ecf20Sopenharmony_ci#
248c2ecf20Sopenharmony_ci# Both C and C++ are supported, but preferred language is C for such utilities.
258c2ecf20Sopenharmony_ci#
268c2ecf20Sopenharmony_ci# Sample syntax (see Documentation/kbuild/makefiles.rst for reference)
278c2ecf20Sopenharmony_ci# hostprogs := bin2hex
288c2ecf20Sopenharmony_ci# Will compile bin2hex.c and create an executable named bin2hex
298c2ecf20Sopenharmony_ci#
308c2ecf20Sopenharmony_ci# hostprogs     := lxdialog
318c2ecf20Sopenharmony_ci# lxdialog-objs := checklist.o lxdialog.o
328c2ecf20Sopenharmony_ci# Will compile lxdialog.c and checklist.c, and then link the executable
338c2ecf20Sopenharmony_ci# lxdialog, based on checklist.o and lxdialog.o
348c2ecf20Sopenharmony_ci#
358c2ecf20Sopenharmony_ci# hostprogs       := qconf
368c2ecf20Sopenharmony_ci# qconf-cxxobjs   := qconf.o
378c2ecf20Sopenharmony_ci# qconf-objs      := menu.o
388c2ecf20Sopenharmony_ci# Will compile qconf as a C++ program, and menu as a C program.
398c2ecf20Sopenharmony_ci# They are linked as C++ code to the executable qconf
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci# C code
428c2ecf20Sopenharmony_ci# Executables compiled from a single .c file
438c2ecf20Sopenharmony_cihost-csingle	:= $(foreach m,$(hostprogs), \
448c2ecf20Sopenharmony_ci			$(if $($(m)-objs)$($(m)-cxxobjs),,$(m)))
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci# C executables linked based on several .o files
478c2ecf20Sopenharmony_cihost-cmulti	:= $(foreach m,$(hostprogs),\
488c2ecf20Sopenharmony_ci		   $(if $($(m)-cxxobjs),,$(if $($(m)-objs),$(m))))
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci# Object (.o) files compiled from .c files
518c2ecf20Sopenharmony_cihost-cobjs	:= $(sort $(foreach m,$(hostprogs),$($(m)-objs)))
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci# C++ code
548c2ecf20Sopenharmony_ci# C++ executables compiled from at least one .cc file
558c2ecf20Sopenharmony_ci# and zero or more .c files
568c2ecf20Sopenharmony_cihost-cxxmulti	:= $(foreach m,$(hostprogs),$(if $($(m)-cxxobjs),$(m)))
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci# C++ Object (.o) files compiled from .cc files
598c2ecf20Sopenharmony_cihost-cxxobjs	:= $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs)))
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cihost-csingle	:= $(addprefix $(obj)/,$(host-csingle))
628c2ecf20Sopenharmony_cihost-cmulti	:= $(addprefix $(obj)/,$(host-cmulti))
638c2ecf20Sopenharmony_cihost-cobjs	:= $(addprefix $(obj)/,$(host-cobjs))
648c2ecf20Sopenharmony_cihost-cxxmulti	:= $(addprefix $(obj)/,$(host-cxxmulti))
658c2ecf20Sopenharmony_cihost-cxxobjs	:= $(addprefix $(obj)/,$(host-cxxobjs))
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#####
688c2ecf20Sopenharmony_ci# Handle options to gcc. Support building with separate output directory
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci_hostc_flags   = $(KBUILD_HOSTCFLAGS)   $(HOST_EXTRACFLAGS)   \
718c2ecf20Sopenharmony_ci                 $(HOSTCFLAGS_$(target-stem).o)
728c2ecf20Sopenharmony_ci_hostcxx_flags = $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
738c2ecf20Sopenharmony_ci                 $(HOSTCXXFLAGS_$(target-stem).o)
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci# $(objtree)/$(obj) for including generated headers from checkin source files
768c2ecf20Sopenharmony_ciifeq ($(KBUILD_EXTMOD),)
778c2ecf20Sopenharmony_ciifdef building_out_of_srctree
788c2ecf20Sopenharmony_ci_hostc_flags   += -I $(objtree)/$(obj)
798c2ecf20Sopenharmony_ci_hostcxx_flags += -I $(objtree)/$(obj)
808c2ecf20Sopenharmony_ciendif
818c2ecf20Sopenharmony_ciendif
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cihostc_flags    = -Wp,-MMD,$(depfile) $(_hostc_flags)
848c2ecf20Sopenharmony_cihostcxx_flags  = -Wp,-MMD,$(depfile) $(_hostcxx_flags)
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci#####
878c2ecf20Sopenharmony_ci# Compile programs on the host
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci# Create executable from a single .c file
908c2ecf20Sopenharmony_ci# host-csingle -> Executable
918c2ecf20Sopenharmony_ciquiet_cmd_host-csingle 	= HOSTCC  $@
928c2ecf20Sopenharmony_ci      cmd_host-csingle	= $(HOSTCC) $(hostc_flags) $(KBUILD_HOSTLDFLAGS) -o $@ $< \
938c2ecf20Sopenharmony_ci		$(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
948c2ecf20Sopenharmony_ci$(host-csingle): $(obj)/%: $(src)/%.c FORCE
958c2ecf20Sopenharmony_ci	$(call if_changed_dep,host-csingle)
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci# Link an executable based on list of .o files, all plain c
988c2ecf20Sopenharmony_ci# host-cmulti -> executable
998c2ecf20Sopenharmony_ciquiet_cmd_host-cmulti	= HOSTLD  $@
1008c2ecf20Sopenharmony_ci      cmd_host-cmulti	= $(HOSTCC) $(KBUILD_HOSTLDFLAGS) -o $@ \
1018c2ecf20Sopenharmony_ci			  $(addprefix $(obj)/, $($(target-stem)-objs)) \
1028c2ecf20Sopenharmony_ci			  $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
1038c2ecf20Sopenharmony_ci$(host-cmulti): FORCE
1048c2ecf20Sopenharmony_ci	$(call if_changed,host-cmulti)
1058c2ecf20Sopenharmony_ci$(call multi_depend, $(host-cmulti), , -objs)
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci# Create .o file from a single .c file
1088c2ecf20Sopenharmony_ci# host-cobjs -> .o
1098c2ecf20Sopenharmony_ciquiet_cmd_host-cobjs	= HOSTCC  $@
1108c2ecf20Sopenharmony_ci      cmd_host-cobjs	= $(HOSTCC) $(hostc_flags) -c -o $@ $<
1118c2ecf20Sopenharmony_ci$(host-cobjs): $(obj)/%.o: $(src)/%.c FORCE
1128c2ecf20Sopenharmony_ci	$(call if_changed_dep,host-cobjs)
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci# Link an executable based on list of .o files, a mixture of .c and .cc
1158c2ecf20Sopenharmony_ci# host-cxxmulti -> executable
1168c2ecf20Sopenharmony_ciquiet_cmd_host-cxxmulti	= HOSTLD  $@
1178c2ecf20Sopenharmony_ci      cmd_host-cxxmulti	= $(HOSTCXX) $(KBUILD_HOSTLDFLAGS) -o $@ \
1188c2ecf20Sopenharmony_ci			  $(foreach o,objs cxxobjs,\
1198c2ecf20Sopenharmony_ci			  $(addprefix $(obj)/, $($(target-stem)-$(o)))) \
1208c2ecf20Sopenharmony_ci			  $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
1218c2ecf20Sopenharmony_ci$(host-cxxmulti): FORCE
1228c2ecf20Sopenharmony_ci	$(call if_changed,host-cxxmulti)
1238c2ecf20Sopenharmony_ci$(call multi_depend, $(host-cxxmulti), , -objs -cxxobjs)
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci# Create .o file from a single .cc (C++) file
1268c2ecf20Sopenharmony_ciquiet_cmd_host-cxxobjs	= HOSTCXX $@
1278c2ecf20Sopenharmony_ci      cmd_host-cxxobjs	= $(HOSTCXX) $(hostcxx_flags) -c -o $@ $<
1288c2ecf20Sopenharmony_ci$(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE
1298c2ecf20Sopenharmony_ci	$(call if_changed_dep,host-cxxobjs)
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_citargets += $(host-csingle) $(host-cmulti) $(host-cobjs) \
1328c2ecf20Sopenharmony_ci	   $(host-cxxmulti) $(host-cxxobjs)
133