blob: 687ca3f309e9b9762e7f3baca0ffe4369a265767 [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
Masahiro Yamada5f2fb522020-02-02 01:49:24 +090041__hostprogs := $(sort $(hostprogs))
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043# C code
44# Executables compiled from a single .c file
Masahiro Yamadaedb950c2014-07-16 16:12:20 +090045host-csingle := $(foreach m,$(__hostprogs), \
46 $(if $($(m)-objs)$($(m)-cxxobjs),,$(m)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48# C executables linked based on several .o files
49host-cmulti := $(foreach m,$(__hostprogs),\
50 $(if $($(m)-cxxobjs),,$(if $($(m)-objs),$(m))))
51
52# Object (.o) files compiled from .c files
53host-cobjs := $(sort $(foreach m,$(__hostprogs),$($(m)-objs)))
54
55# C++ code
Masahiro Yamadad8d9efe2014-07-16 16:12:19 +090056# C++ executables compiled from at least one .cc file
Linus Torvalds1da177e2005-04-16 15:20:36 -070057# and zero or more .c files
58host-cxxmulti := $(foreach m,$(__hostprogs),$(if $($(m)-cxxobjs),$(m)))
59
60# C++ Object (.o) files compiled from .cc files
61host-cxxobjs := $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs)))
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063host-csingle := $(addprefix $(obj)/,$(host-csingle))
64host-cmulti := $(addprefix $(obj)/,$(host-cmulti))
65host-cobjs := $(addprefix $(obj)/,$(host-cobjs))
66host-cxxmulti := $(addprefix $(obj)/,$(host-cxxmulti))
67host-cxxobjs := $(addprefix $(obj)/,$(host-cxxobjs))
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#####
70# Handle options to gcc. Support building with separate output directory
71
Laura Abbott96f14fe2018-07-09 17:45:58 -070072_hostc_flags = $(KBUILD_HOSTCFLAGS) $(HOST_EXTRACFLAGS) \
Masahiro Yamada54b8ae62019-08-30 13:34:01 +090073 $(HOSTCFLAGS_$(target-stem).o)
Laura Abbott10844ae2018-07-09 17:45:59 -070074_hostcxx_flags = $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
Masahiro Yamada54b8ae62019-08-30 13:34:01 +090075 $(HOSTCXXFLAGS_$(target-stem).o)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Masahiro Yamadacdd750b2019-05-13 15:22:17 +090077# $(objtree)/$(obj) for including generated headers from checkin source files
Masahiro Yamada58156ba2019-01-16 11:56:40 +090078ifeq ($(KBUILD_EXTMOD),)
Masahiro Yamada051f2782019-07-06 12:07:12 +090079ifdef building_out_of_srctree
Masahiro Yamadacdd750b2019-05-13 15:22:17 +090080_hostc_flags += -I $(objtree)/$(obj)
81_hostcxx_flags += -I $(objtree)/$(obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082endif
Masahiro Yamada58156ba2019-01-16 11:56:40 +090083endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Masahiro Yamada30a77292020-04-23 23:23:53 +090085hostc_flags = -Wp,-MMD,$(depfile) $(_hostc_flags)
86hostcxx_flags = -Wp,-MMD,$(depfile) $(_hostcxx_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#####
89# Compile programs on the host
90
91# Create executable from a single .c file
92# host-csingle -> Executable
93quiet_cmd_host-csingle = HOSTCC $@
Laura Abbottb90a3682018-07-09 17:46:00 -070094 cmd_host-csingle = $(HOSTCC) $(hostc_flags) $(KBUILD_HOSTLDFLAGS) -o $@ $< \
Masahiro Yamada54b8ae62019-08-30 13:34:01 +090095 $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
Sam Ravnborg767e5812007-05-06 09:23:45 +020096$(host-csingle): $(obj)/%: $(src)/%.c FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 $(call if_changed_dep,host-csingle)
98
99# Link an executable based on list of .o files, all plain c
100# host-cmulti -> executable
101quiet_cmd_host-cmulti = HOSTLD $@
Laura Abbottb90a3682018-07-09 17:46:00 -0700102 cmd_host-cmulti = $(HOSTCC) $(KBUILD_HOSTLDFLAGS) -o $@ \
Masahiro Yamada54b8ae62019-08-30 13:34:01 +0900103 $(addprefix $(obj)/, $($(target-stem)-objs)) \
104 $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
Masahiro Yamada97e32262014-08-19 16:34:21 +0900105$(host-cmulti): FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 $(call if_changed,host-cmulti)
Masahiro Yamada97e32262014-08-19 16:34:21 +0900107$(call multi_depend, $(host-cmulti), , -objs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109# Create .o file from a single .c file
110# host-cobjs -> .o
111quiet_cmd_host-cobjs = HOSTCC $@
112 cmd_host-cobjs = $(HOSTCC) $(hostc_flags) -c -o $@ $<
Sam Ravnborg767e5812007-05-06 09:23:45 +0200113$(host-cobjs): $(obj)/%.o: $(src)/%.c FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 $(call if_changed_dep,host-cobjs)
115
116# Link an executable based on list of .o files, a mixture of .c and .cc
117# host-cxxmulti -> executable
118quiet_cmd_host-cxxmulti = HOSTLD $@
Laura Abbottb90a3682018-07-09 17:46:00 -0700119 cmd_host-cxxmulti = $(HOSTCXX) $(KBUILD_HOSTLDFLAGS) -o $@ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 $(foreach o,objs cxxobjs,\
Masahiro Yamada54b8ae62019-08-30 13:34:01 +0900121 $(addprefix $(obj)/, $($(target-stem)-$(o)))) \
122 $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
Masahiro Yamada97e32262014-08-19 16:34:21 +0900123$(host-cxxmulti): FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 $(call if_changed,host-cxxmulti)
Masahiro Yamada97e32262014-08-19 16:34:21 +0900125$(call multi_depend, $(host-cxxmulti), , -objs -cxxobjs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127# Create .o file from a single .cc (C++) file
128quiet_cmd_host-cxxobjs = HOSTCXX $@
129 cmd_host-cxxobjs = $(HOSTCXX) $(hostcxx_flags) -c -o $@ $<
Sam Ravnborg767e5812007-05-06 09:23:45 +0200130$(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 $(call if_changed_dep,host-cxxobjs)
132
Masahiro Yamada42640b132020-07-29 12:15:36 +0900133targets += $(host-csingle) $(host-cmulti) $(host-cobjs) \
134 $(host-cxxmulti) $(host-cxxobjs)