1b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0 2cf8dfd15SMasahiro Yamada 3cf8dfd15SMasahiro Yamada# LEX 4cf8dfd15SMasahiro Yamada# --------------------------------------------------------------------------- 5cf8dfd15SMasahiro Yamadaquiet_cmd_flex = LEX $@ 6cf8dfd15SMasahiro Yamada cmd_flex = $(LEX) -o$@ -L $< 7cf8dfd15SMasahiro Yamada 8cf8dfd15SMasahiro Yamada$(obj)/%.lex.c: $(src)/%.l FORCE 9cf8dfd15SMasahiro Yamada $(call if_changed,flex) 10cf8dfd15SMasahiro Yamada 11cf8dfd15SMasahiro Yamada# YACC 12cf8dfd15SMasahiro Yamada# --------------------------------------------------------------------------- 13cf8dfd15SMasahiro Yamadaquiet_cmd_bison = YACC $(basename $@).[ch] 14cf8dfd15SMasahiro Yamada cmd_bison = $(YACC) -o $(basename $@).c --defines=$(basename $@).h -t -l $< 15cf8dfd15SMasahiro Yamada 16cf8dfd15SMasahiro Yamada$(obj)/%.tab.c $(obj)/%.tab.h: $(src)/%.y FORCE 17cf8dfd15SMasahiro Yamada $(call if_changed,bison) 18cf8dfd15SMasahiro Yamada 191da177e4SLinus Torvalds# ========================================================================== 201da177e4SLinus Torvalds# Building binaries on the host system 211da177e4SLinus Torvalds# Binaries are used during the compilation of the kernel, for example 221da177e4SLinus Torvalds# to preprocess a data file. 231da177e4SLinus Torvalds# 243156fd05SRobert P. J. Day# Both C and C++ are supported, but preferred language is C for such utilities. 252f7ab126SMiguel Ojeda# Rust is also supported, but it may only be used in scenarios where a Rust 262f7ab126SMiguel Ojeda# toolchain is required to be available (e.g. when `CONFIG_RUST` is enabled). 271da177e4SLinus Torvalds# 28cd238effSMauro Carvalho Chehab# Sample syntax (see Documentation/kbuild/makefiles.rst for reference) 295f2fb52fSMasahiro Yamada# hostprogs := bin2hex 301da177e4SLinus Torvalds# Will compile bin2hex.c and create an executable named bin2hex 311da177e4SLinus Torvalds# 325f2fb52fSMasahiro Yamada# hostprogs := lxdialog 331da177e4SLinus Torvalds# lxdialog-objs := checklist.o lxdialog.o 341da177e4SLinus Torvalds# Will compile lxdialog.c and checklist.c, and then link the executable 351da177e4SLinus Torvalds# lxdialog, based on checklist.o and lxdialog.o 361da177e4SLinus Torvalds# 375f2fb52fSMasahiro Yamada# hostprogs := qconf 381da177e4SLinus Torvalds# qconf-cxxobjs := qconf.o 391da177e4SLinus Torvalds# qconf-objs := menu.o 401da177e4SLinus Torvalds# Will compile qconf as a C++ program, and menu as a C program. 411da177e4SLinus Torvalds# They are linked as C++ code to the executable qconf 422f7ab126SMiguel Ojeda# 432f7ab126SMiguel Ojeda# hostprogs := target 442f7ab126SMiguel Ojeda# target-rust := y 452f7ab126SMiguel Ojeda# Will compile `target` as a Rust program, using `target.rs` as the crate root. 462f7ab126SMiguel Ojeda# The crate may consist of several source files. 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds# C code 491da177e4SLinus Torvalds# Executables compiled from a single .c file 5085569d19SMasahiro Yamadahost-csingle := $(foreach m,$(hostprogs), \ 512f7ab126SMiguel Ojeda $(if $($(m)-objs)$($(m)-cxxobjs)$($(m)-rust),,$(m))) 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds# C executables linked based on several .o files 5485569d19SMasahiro Yamadahost-cmulti := $(foreach m,$(hostprogs),\ 552f7ab126SMiguel Ojeda $(if $($(m)-cxxobjs)$($(m)-rust),,$(if $($(m)-objs),$(m)))) 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds# Object (.o) files compiled from .c files 5885569d19SMasahiro Yamadahost-cobjs := $(sort $(foreach m,$(hostprogs),$($(m)-objs))) 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds# C++ code 61d8d9efe2SMasahiro Yamada# C++ executables compiled from at least one .cc file 621da177e4SLinus Torvalds# and zero or more .c files 6385569d19SMasahiro Yamadahost-cxxmulti := $(foreach m,$(hostprogs),$(if $($(m)-cxxobjs),$(m))) 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds# C++ Object (.o) files compiled from .cc files 661da177e4SLinus Torvaldshost-cxxobjs := $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs))) 671da177e4SLinus Torvalds 682f7ab126SMiguel Ojeda# Rust code 692f7ab126SMiguel Ojeda# Executables compiled from a single Rust crate (which may consist of 702f7ab126SMiguel Ojeda# one or more .rs files) 712f7ab126SMiguel Ojedahost-rust := $(foreach m,$(hostprogs),$(if $($(m)-rust),$(m))) 722f7ab126SMiguel Ojeda 731da177e4SLinus Torvaldshost-csingle := $(addprefix $(obj)/,$(host-csingle)) 741da177e4SLinus Torvaldshost-cmulti := $(addprefix $(obj)/,$(host-cmulti)) 751da177e4SLinus Torvaldshost-cobjs := $(addprefix $(obj)/,$(host-cobjs)) 761da177e4SLinus Torvaldshost-cxxmulti := $(addprefix $(obj)/,$(host-cxxmulti)) 771da177e4SLinus Torvaldshost-cxxobjs := $(addprefix $(obj)/,$(host-cxxobjs)) 782f7ab126SMiguel Ojedahost-rust := $(addprefix $(obj)/,$(host-rust)) 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds##### 811da177e4SLinus Torvalds# Handle options to gcc. Support building with separate output directory 821da177e4SLinus Torvalds 8316169a47SMasahiro Yamadahostc_flags = -Wp,-MMD,$(depfile) \ 8416169a47SMasahiro Yamada $(KBUILD_HOSTCFLAGS) $(HOST_EXTRACFLAGS) \ 8554b8ae66SMasahiro Yamada $(HOSTCFLAGS_$(target-stem).o) 8616169a47SMasahiro Yamadahostcxx_flags = -Wp,-MMD,$(depfile) \ 8716169a47SMasahiro Yamada $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \ 8854b8ae66SMasahiro Yamada $(HOSTCXXFLAGS_$(target-stem).o) 89*df01b7cfSMiguel Ojeda 90*df01b7cfSMiguel Ojeda# `--out-dir` is required to avoid temporaries being created by `rustc` in the 91*df01b7cfSMiguel Ojeda# current working directory, which may be not accessible in the out-of-tree 92*df01b7cfSMiguel Ojeda# modules case. 93*df01b7cfSMiguel Ojedahostrust_flags = --out-dir $(dir $@) --emit=dep-info=$(depfile) \ 94295d8398SMasahiro Yamada $(KBUILD_HOSTRUSTFLAGS) $(HOST_EXTRARUSTFLAGS) \ 952f7ab126SMiguel Ojeda $(HOSTRUSTFLAGS_$(target-stem)) 961da177e4SLinus Torvalds 97cdd750bfSMasahiro Yamada# $(objtree)/$(obj) for including generated headers from checkin source files 9858156ba4SMasahiro Yamadaifeq ($(KBUILD_EXTMOD),) 99051f278eSMasahiro Yamadaifdef building_out_of_srctree 10016169a47SMasahiro Yamadahostc_flags += -I $(objtree)/$(obj) 10116169a47SMasahiro Yamadahostcxx_flags += -I $(objtree)/$(obj) 1021da177e4SLinus Torvaldsendif 10358156ba4SMasahiro Yamadaendif 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds##### 1061da177e4SLinus Torvalds# Compile programs on the host 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds# Create executable from a single .c file 1091da177e4SLinus Torvalds# host-csingle -> Executable 1101da177e4SLinus Torvaldsquiet_cmd_host-csingle = HOSTCC $@ 111b90a3680SLaura Abbott cmd_host-csingle = $(HOSTCC) $(hostc_flags) $(KBUILD_HOSTLDFLAGS) -o $@ $< \ 11254b8ae66SMasahiro Yamada $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem)) 113767e581dSSam Ravnborg$(host-csingle): $(obj)/%: $(src)/%.c FORCE 1141da177e4SLinus Torvalds $(call if_changed_dep,host-csingle) 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds# Link an executable based on list of .o files, all plain c 1171da177e4SLinus Torvalds# host-cmulti -> executable 1181da177e4SLinus Torvaldsquiet_cmd_host-cmulti = HOSTLD $@ 119b90a3680SLaura Abbott cmd_host-cmulti = $(HOSTCC) $(KBUILD_HOSTLDFLAGS) -o $@ \ 12054b8ae66SMasahiro Yamada $(addprefix $(obj)/, $($(target-stem)-objs)) \ 12154b8ae66SMasahiro Yamada $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem)) 12297e3226eSMasahiro Yamada$(host-cmulti): FORCE 1231da177e4SLinus Torvalds $(call if_changed,host-cmulti) 12497e3226eSMasahiro Yamada$(call multi_depend, $(host-cmulti), , -objs) 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds# Create .o file from a single .c file 1271da177e4SLinus Torvalds# host-cobjs -> .o 1281da177e4SLinus Torvaldsquiet_cmd_host-cobjs = HOSTCC $@ 1291da177e4SLinus Torvalds cmd_host-cobjs = $(HOSTCC) $(hostc_flags) -c -o $@ $< 130767e581dSSam Ravnborg$(host-cobjs): $(obj)/%.o: $(src)/%.c FORCE 1311da177e4SLinus Torvalds $(call if_changed_dep,host-cobjs) 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds# Link an executable based on list of .o files, a mixture of .c and .cc 1341da177e4SLinus Torvalds# host-cxxmulti -> executable 1351da177e4SLinus Torvaldsquiet_cmd_host-cxxmulti = HOSTLD $@ 136b90a3680SLaura Abbott cmd_host-cxxmulti = $(HOSTCXX) $(KBUILD_HOSTLDFLAGS) -o $@ \ 1371da177e4SLinus Torvalds $(foreach o,objs cxxobjs,\ 13854b8ae66SMasahiro Yamada $(addprefix $(obj)/, $($(target-stem)-$(o)))) \ 13954b8ae66SMasahiro Yamada $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem)) 14097e3226eSMasahiro Yamada$(host-cxxmulti): FORCE 1411da177e4SLinus Torvalds $(call if_changed,host-cxxmulti) 14297e3226eSMasahiro Yamada$(call multi_depend, $(host-cxxmulti), , -objs -cxxobjs) 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds# Create .o file from a single .cc (C++) file 1451da177e4SLinus Torvaldsquiet_cmd_host-cxxobjs = HOSTCXX $@ 1461da177e4SLinus Torvalds cmd_host-cxxobjs = $(HOSTCXX) $(hostcxx_flags) -c -o $@ $< 147767e581dSSam Ravnborg$(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE 1481da177e4SLinus Torvalds $(call if_changed_dep,host-cxxobjs) 1491da177e4SLinus Torvalds 1502f7ab126SMiguel Ojeda# Create executable from a single Rust crate (which may consist of 1512f7ab126SMiguel Ojeda# one or more `.rs` files) 1522f7ab126SMiguel Ojeda# host-rust -> Executable 1532f7ab126SMiguel Ojedaquiet_cmd_host-rust = HOSTRUSTC $@ 1542f7ab126SMiguel Ojeda cmd_host-rust = \ 1552185242fSMasahiro Yamada $(HOSTRUSTC) $(hostrust_flags) --emit=link=$@ $< 1562f7ab126SMiguel Ojeda$(host-rust): $(obj)/%: $(src)/%.rs FORCE 1572f7ab126SMiguel Ojeda $(call if_changed_dep,host-rust) 1582f7ab126SMiguel Ojeda 1591da177e4SLinus Torvaldstargets += $(host-csingle) $(host-cmulti) $(host-cobjs) \ 1602f7ab126SMiguel Ojeda $(host-cxxmulti) $(host-cxxobjs) $(host-rust) 161