blob: 278b4d6ac9454d9cc5f3fc43a77881dd3d9bcbfe [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001# SPDX-License-Identifier: GPL-2.0
Masahiro Yamadacf8dfd12019-07-21 01:27:40 +09002
3# LEX
4# ---------------------------------------------------------------------------
5quiet_cmd_flex = LEX $@
6 cmd_flex = $(LEX) -o$@ -L $<
7
8$(obj)/%.lex.c: $(src)/%.l FORCE
9 $(call if_changed,flex)
10
11# YACC
12# ---------------------------------------------------------------------------
13quiet_cmd_bison = YACC $(basename $@).[ch]
14 cmd_bison = $(YACC) -o $(basename $@).c --defines=$(basename $@).h -t -l $<
15
16$(obj)/%.tab.c $(obj)/%.tab.h: $(src)/%.y FORCE
17 $(call if_changed,bison)
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019# ==========================================================================
20# Building binaries on the host system
21# Binaries are used during the compilation of the kernel, for example
22# to preprocess a data file.
23#
Robert P. J. Day3156fd02008-02-18 04:48:20 -050024# Both C and C++ are supported, but preferred language is C for such utilities.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#
Mauro Carvalho Chehabcd238ef2019-06-12 14:52:48 -030026# Sample syntax (see Documentation/kbuild/makefiles.rst for reference)
Masahiro Yamada5f2fb522020-02-02 01:49:24 +090027# hostprogs := bin2hex
Linus Torvalds1da177e2005-04-16 15:20:36 -070028# Will compile bin2hex.c and create an executable named bin2hex
29#
Masahiro Yamada5f2fb522020-02-02 01:49:24 +090030# hostprogs := lxdialog
Linus Torvalds1da177e2005-04-16 15:20:36 -070031# lxdialog-objs := checklist.o lxdialog.o
32# Will compile lxdialog.c and checklist.c, and then link the executable
33# lxdialog, based on checklist.o and lxdialog.o
34#
Masahiro Yamada5f2fb522020-02-02 01:49:24 +090035# hostprogs := qconf
Linus Torvalds1da177e2005-04-16 15:20:36 -070036# qconf-cxxobjs := qconf.o
37# qconf-objs := menu.o
38# Will compile qconf as a C++ program, and menu as a C program.
39# They are linked as C++ code to the executable qconf
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041# C code
42# Executables compiled from a single .c file
Masahiro Yamada85569d12020-07-29 12:15:37 +090043host-csingle := $(foreach m,$(hostprogs), \
Masahiro Yamadaedb950c2014-07-16 16:12:20 +090044 $(if $($(m)-objs)$($(m)-cxxobjs),,$(m)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46# C executables linked based on several .o files
Masahiro Yamada85569d12020-07-29 12:15:37 +090047host-cmulti := $(foreach m,$(hostprogs),\
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 $(if $($(m)-cxxobjs),,$(if $($(m)-objs),$(m))))
49
50# Object (.o) files compiled from .c files
Masahiro Yamada85569d12020-07-29 12:15:37 +090051host-cobjs := $(sort $(foreach m,$(hostprogs),$($(m)-objs)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53# C++ code
Masahiro Yamadad8d9efe2014-07-16 16:12:19 +090054# C++ executables compiled from at least one .cc file
Linus Torvalds1da177e2005-04-16 15:20:36 -070055# and zero or more .c files
Masahiro Yamada85569d12020-07-29 12:15:37 +090056host-cxxmulti := $(foreach m,$(hostprogs),$(if $($(m)-cxxobjs),$(m)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58# C++ Object (.o) files compiled from .cc files
59host-cxxobjs := $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs)))
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061host-csingle := $(addprefix $(obj)/,$(host-csingle))
62host-cmulti := $(addprefix $(obj)/,$(host-cmulti))
63host-cobjs := $(addprefix $(obj)/,$(host-cobjs))
64host-cxxmulti := $(addprefix $(obj)/,$(host-cxxmulti))
65host-cxxobjs := $(addprefix $(obj)/,$(host-cxxobjs))
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67#####
68# Handle options to gcc. Support building with separate output directory
69
Laura Abbott96f14fe2018-07-09 17:45:58 -070070_hostc_flags = $(KBUILD_HOSTCFLAGS) $(HOST_EXTRACFLAGS) \
Masahiro Yamada54b8ae62019-08-30 13:34:01 +090071 $(HOSTCFLAGS_$(target-stem).o)
Laura Abbott10844ae2018-07-09 17:45:59 -070072_hostcxx_flags = $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
Masahiro Yamada54b8ae62019-08-30 13:34:01 +090073 $(HOSTCXXFLAGS_$(target-stem).o)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Masahiro Yamadacdd750b2019-05-13 15:22:17 +090075# $(objtree)/$(obj) for including generated headers from checkin source files
Masahiro Yamada58156ba2019-01-16 11:56:40 +090076ifeq ($(KBUILD_EXTMOD),)
Masahiro Yamada051f2782019-07-06 12:07:12 +090077ifdef building_out_of_srctree
Masahiro Yamadacdd750b2019-05-13 15:22:17 +090078_hostc_flags += -I $(objtree)/$(obj)
79_hostcxx_flags += -I $(objtree)/$(obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080endif
Masahiro Yamada58156ba2019-01-16 11:56:40 +090081endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Masahiro Yamada30a77292020-04-23 23:23:53 +090083hostc_flags = -Wp,-MMD,$(depfile) $(_hostc_flags)
84hostcxx_flags = -Wp,-MMD,$(depfile) $(_hostcxx_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#####
87# Compile programs on the host
88
89# Create executable from a single .c file
90# host-csingle -> Executable
91quiet_cmd_host-csingle = HOSTCC $@
Laura Abbottb90a3682018-07-09 17:46:00 -070092 cmd_host-csingle = $(HOSTCC) $(hostc_flags) $(KBUILD_HOSTLDFLAGS) -o $@ $< \
Masahiro Yamada54b8ae62019-08-30 13:34:01 +090093 $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
Sam Ravnborg767e5812007-05-06 09:23:45 +020094$(host-csingle): $(obj)/%: $(src)/%.c FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 $(call if_changed_dep,host-csingle)
96
97# Link an executable based on list of .o files, all plain c
98# host-cmulti -> executable
99quiet_cmd_host-cmulti = HOSTLD $@
Laura Abbottb90a3682018-07-09 17:46:00 -0700100 cmd_host-cmulti = $(HOSTCC) $(KBUILD_HOSTLDFLAGS) -o $@ \
Masahiro Yamada54b8ae62019-08-30 13:34:01 +0900101 $(addprefix $(obj)/, $($(target-stem)-objs)) \
102 $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
Masahiro Yamada97e32262014-08-19 16:34:21 +0900103$(host-cmulti): FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 $(call if_changed,host-cmulti)
Masahiro Yamada97e32262014-08-19 16:34:21 +0900105$(call multi_depend, $(host-cmulti), , -objs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107# Create .o file from a single .c file
108# host-cobjs -> .o
109quiet_cmd_host-cobjs = HOSTCC $@
110 cmd_host-cobjs = $(HOSTCC) $(hostc_flags) -c -o $@ $<
Sam Ravnborg767e5812007-05-06 09:23:45 +0200111$(host-cobjs): $(obj)/%.o: $(src)/%.c FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 $(call if_changed_dep,host-cobjs)
113
114# Link an executable based on list of .o files, a mixture of .c and .cc
115# host-cxxmulti -> executable
116quiet_cmd_host-cxxmulti = HOSTLD $@
Laura Abbottb90a3682018-07-09 17:46:00 -0700117 cmd_host-cxxmulti = $(HOSTCXX) $(KBUILD_HOSTLDFLAGS) -o $@ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 $(foreach o,objs cxxobjs,\
Masahiro Yamada54b8ae62019-08-30 13:34:01 +0900119 $(addprefix $(obj)/, $($(target-stem)-$(o)))) \
120 $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
Masahiro Yamada97e32262014-08-19 16:34:21 +0900121$(host-cxxmulti): FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 $(call if_changed,host-cxxmulti)
Masahiro Yamada97e32262014-08-19 16:34:21 +0900123$(call multi_depend, $(host-cxxmulti), , -objs -cxxobjs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125# Create .o file from a single .cc (C++) file
126quiet_cmd_host-cxxobjs = HOSTCXX $@
127 cmd_host-cxxobjs = $(HOSTCXX) $(hostcxx_flags) -c -o $@ $<
Sam Ravnborg767e5812007-05-06 09:23:45 +0200128$(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 $(call if_changed_dep,host-cxxobjs)
130
Masahiro Yamada42640b132020-07-29 12:15:36 +0900131targets += $(host-csingle) $(host-cmulti) $(host-cobjs) \
132 $(host-cxxmulti) $(host-cxxobjs)