162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci# LEX
462306a36Sopenharmony_ci# ---------------------------------------------------------------------------
562306a36Sopenharmony_ciquiet_cmd_flex = LEX     $@
662306a36Sopenharmony_ci      cmd_flex = $(LEX) -o$@ -L $<
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci$(obj)/%.lex.c: $(src)/%.l FORCE
962306a36Sopenharmony_ci	$(call if_changed,flex)
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci# YACC
1262306a36Sopenharmony_ci# ---------------------------------------------------------------------------
1362306a36Sopenharmony_ciquiet_cmd_bison = YACC    $(basename $@).[ch]
1462306a36Sopenharmony_ci      cmd_bison = $(YACC) -o $(basename $@).c --defines=$(basename $@).h -t -l $<
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci$(obj)/%.tab.c $(obj)/%.tab.h: $(src)/%.y FORCE
1762306a36Sopenharmony_ci	$(call if_changed,bison)
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci# ==========================================================================
2062306a36Sopenharmony_ci# Building binaries on the host system
2162306a36Sopenharmony_ci# Binaries are used during the compilation of the kernel, for example
2262306a36Sopenharmony_ci# to preprocess a data file.
2362306a36Sopenharmony_ci#
2462306a36Sopenharmony_ci# Both C and C++ are supported, but preferred language is C for such utilities.
2562306a36Sopenharmony_ci# Rust is also supported, but it may only be used in scenarios where a Rust
2662306a36Sopenharmony_ci# toolchain is required to be available (e.g. when  `CONFIG_RUST` is enabled).
2762306a36Sopenharmony_ci#
2862306a36Sopenharmony_ci# Sample syntax (see Documentation/kbuild/makefiles.rst for reference)
2962306a36Sopenharmony_ci# hostprogs := bin2hex
3062306a36Sopenharmony_ci# Will compile bin2hex.c and create an executable named bin2hex
3162306a36Sopenharmony_ci#
3262306a36Sopenharmony_ci# hostprogs     := lxdialog
3362306a36Sopenharmony_ci# lxdialog-objs := checklist.o lxdialog.o
3462306a36Sopenharmony_ci# Will compile lxdialog.c and checklist.c, and then link the executable
3562306a36Sopenharmony_ci# lxdialog, based on checklist.o and lxdialog.o
3662306a36Sopenharmony_ci#
3762306a36Sopenharmony_ci# hostprogs       := qconf
3862306a36Sopenharmony_ci# qconf-cxxobjs   := qconf.o
3962306a36Sopenharmony_ci# qconf-objs      := menu.o
4062306a36Sopenharmony_ci# Will compile qconf as a C++ program, and menu as a C program.
4162306a36Sopenharmony_ci# They are linked as C++ code to the executable qconf
4262306a36Sopenharmony_ci#
4362306a36Sopenharmony_ci# hostprogs   := target
4462306a36Sopenharmony_ci# target-rust := y
4562306a36Sopenharmony_ci# Will compile `target` as a Rust program, using `target.rs` as the crate root.
4662306a36Sopenharmony_ci# The crate may consist of several source files.
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci# C code
4962306a36Sopenharmony_ci# Executables compiled from a single .c file
5062306a36Sopenharmony_cihost-csingle	:= $(foreach m,$(hostprogs), \
5162306a36Sopenharmony_ci			$(if $($(m)-objs)$($(m)-cxxobjs)$($(m)-rust),,$(m)))
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci# C executables linked based on several .o files
5462306a36Sopenharmony_cihost-cmulti	:= $(foreach m,$(hostprogs),\
5562306a36Sopenharmony_ci		   $(if $($(m)-cxxobjs)$($(m)-rust),,$(if $($(m)-objs),$(m))))
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci# Object (.o) files compiled from .c files
5862306a36Sopenharmony_cihost-cobjs	:= $(sort $(foreach m,$(hostprogs),$($(m)-objs)))
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci# C++ code
6162306a36Sopenharmony_ci# C++ executables compiled from at least one .cc file
6262306a36Sopenharmony_ci# and zero or more .c files
6362306a36Sopenharmony_cihost-cxxmulti	:= $(foreach m,$(hostprogs),$(if $($(m)-cxxobjs),$(m)))
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci# C++ Object (.o) files compiled from .cc files
6662306a36Sopenharmony_cihost-cxxobjs	:= $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs)))
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci# Rust code
6962306a36Sopenharmony_ci# Executables compiled from a single Rust crate (which may consist of
7062306a36Sopenharmony_ci# one or more .rs files)
7162306a36Sopenharmony_cihost-rust	:= $(foreach m,$(hostprogs),$(if $($(m)-rust),$(m)))
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_cihost-csingle	:= $(addprefix $(obj)/,$(host-csingle))
7462306a36Sopenharmony_cihost-cmulti	:= $(addprefix $(obj)/,$(host-cmulti))
7562306a36Sopenharmony_cihost-cobjs	:= $(addprefix $(obj)/,$(host-cobjs))
7662306a36Sopenharmony_cihost-cxxmulti	:= $(addprefix $(obj)/,$(host-cxxmulti))
7762306a36Sopenharmony_cihost-cxxobjs	:= $(addprefix $(obj)/,$(host-cxxobjs))
7862306a36Sopenharmony_cihost-rust	:= $(addprefix $(obj)/,$(host-rust))
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci#####
8162306a36Sopenharmony_ci# Handle options to gcc. Support building with separate output directory
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_cihostc_flags    = -Wp,-MMD,$(depfile) \
8462306a36Sopenharmony_ci                 $(KBUILD_HOSTCFLAGS) $(HOST_EXTRACFLAGS) \
8562306a36Sopenharmony_ci                 $(HOSTCFLAGS_$(target-stem).o)
8662306a36Sopenharmony_cihostcxx_flags  = -Wp,-MMD,$(depfile) \
8762306a36Sopenharmony_ci                 $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
8862306a36Sopenharmony_ci                 $(HOSTCXXFLAGS_$(target-stem).o)
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci# `--out-dir` is required to avoid temporaries being created by `rustc` in the
9162306a36Sopenharmony_ci# current working directory, which may be not accessible in the out-of-tree
9262306a36Sopenharmony_ci# modules case.
9362306a36Sopenharmony_cihostrust_flags = --out-dir $(dir $@) --emit=dep-info=$(depfile) \
9462306a36Sopenharmony_ci                 $(KBUILD_HOSTRUSTFLAGS) $(HOST_EXTRARUSTFLAGS) \
9562306a36Sopenharmony_ci                 $(HOSTRUSTFLAGS_$(target-stem))
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci# $(objtree)/$(obj) for including generated headers from checkin source files
9862306a36Sopenharmony_ciifeq ($(KBUILD_EXTMOD),)
9962306a36Sopenharmony_ciifdef building_out_of_srctree
10062306a36Sopenharmony_cihostc_flags   += -I $(objtree)/$(obj)
10162306a36Sopenharmony_cihostcxx_flags += -I $(objtree)/$(obj)
10262306a36Sopenharmony_ciendif
10362306a36Sopenharmony_ciendif
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci#####
10662306a36Sopenharmony_ci# Compile programs on the host
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci# Create executable from a single .c file
10962306a36Sopenharmony_ci# host-csingle -> Executable
11062306a36Sopenharmony_ciquiet_cmd_host-csingle 	= HOSTCC  $@
11162306a36Sopenharmony_ci      cmd_host-csingle	= $(HOSTCC) $(hostc_flags) $(KBUILD_HOSTLDFLAGS) -o $@ $< \
11262306a36Sopenharmony_ci		$(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
11362306a36Sopenharmony_ci$(host-csingle): $(obj)/%: $(src)/%.c FORCE
11462306a36Sopenharmony_ci	$(call if_changed_dep,host-csingle)
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci# Link an executable based on list of .o files, all plain c
11762306a36Sopenharmony_ci# host-cmulti -> executable
11862306a36Sopenharmony_ciquiet_cmd_host-cmulti	= HOSTLD  $@
11962306a36Sopenharmony_ci      cmd_host-cmulti	= $(HOSTCC) $(KBUILD_HOSTLDFLAGS) -o $@ \
12062306a36Sopenharmony_ci			  $(addprefix $(obj)/, $($(target-stem)-objs)) \
12162306a36Sopenharmony_ci			  $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
12262306a36Sopenharmony_ci$(host-cmulti): FORCE
12362306a36Sopenharmony_ci	$(call if_changed,host-cmulti)
12462306a36Sopenharmony_ci$(call multi_depend, $(host-cmulti), , -objs)
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci# Create .o file from a single .c file
12762306a36Sopenharmony_ci# host-cobjs -> .o
12862306a36Sopenharmony_ciquiet_cmd_host-cobjs	= HOSTCC  $@
12962306a36Sopenharmony_ci      cmd_host-cobjs	= $(HOSTCC) $(hostc_flags) -c -o $@ $<
13062306a36Sopenharmony_ci$(host-cobjs): $(obj)/%.o: $(src)/%.c FORCE
13162306a36Sopenharmony_ci	$(call if_changed_dep,host-cobjs)
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci# Link an executable based on list of .o files, a mixture of .c and .cc
13462306a36Sopenharmony_ci# host-cxxmulti -> executable
13562306a36Sopenharmony_ciquiet_cmd_host-cxxmulti	= HOSTLD  $@
13662306a36Sopenharmony_ci      cmd_host-cxxmulti	= $(HOSTCXX) $(KBUILD_HOSTLDFLAGS) -o $@ \
13762306a36Sopenharmony_ci			  $(foreach o,objs cxxobjs,\
13862306a36Sopenharmony_ci			  $(addprefix $(obj)/, $($(target-stem)-$(o)))) \
13962306a36Sopenharmony_ci			  $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
14062306a36Sopenharmony_ci$(host-cxxmulti): FORCE
14162306a36Sopenharmony_ci	$(call if_changed,host-cxxmulti)
14262306a36Sopenharmony_ci$(call multi_depend, $(host-cxxmulti), , -objs -cxxobjs)
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci# Create .o file from a single .cc (C++) file
14562306a36Sopenharmony_ciquiet_cmd_host-cxxobjs	= HOSTCXX $@
14662306a36Sopenharmony_ci      cmd_host-cxxobjs	= $(HOSTCXX) $(hostcxx_flags) -c -o $@ $<
14762306a36Sopenharmony_ci$(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE
14862306a36Sopenharmony_ci	$(call if_changed_dep,host-cxxobjs)
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci# Create executable from a single Rust crate (which may consist of
15162306a36Sopenharmony_ci# one or more `.rs` files)
15262306a36Sopenharmony_ci# host-rust -> Executable
15362306a36Sopenharmony_ciquiet_cmd_host-rust	= HOSTRUSTC $@
15462306a36Sopenharmony_ci      cmd_host-rust	= \
15562306a36Sopenharmony_ci	$(HOSTRUSTC) $(hostrust_flags) --emit=link=$@ $<
15662306a36Sopenharmony_ci$(host-rust): $(obj)/%: $(src)/%.rs FORCE
15762306a36Sopenharmony_ci	$(call if_changed_dep,host-rust)
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_citargets += $(host-csingle) $(host-cmulti) $(host-cobjs) \
16062306a36Sopenharmony_ci	   $(host-cxxmulti) $(host-cxxobjs) $(host-rust)
161