1# SPDX-License-Identifier: GPL-2.0
2# ==========================================================================
3# Building binaries on the host system
4# Binaries are used during the compilation of the kernel, for example
5# to preprocess a data file.
6#
7# Both C and C++ are supported, but preferred language is C for such utilities.
8#
9# Sample syntax (see Documentation/kbuild/makefiles.txt for reference)
10# hostprogs-y := bin2hex
11# Will compile bin2hex.c and create an executable named bin2hex
12#
13# hostprogs-y    := lxdialog
14# lxdialog-objs := checklist.o lxdialog.o
15# Will compile lxdialog.c and checklist.c, and then link the executable
16# lxdialog, based on checklist.o and lxdialog.o
17#
18# hostprogs-y      := qconf
19# qconf-cxxobjs   := qconf.o
20# qconf-objs      := menu.o
21# Will compile qconf as a C++ program, and menu as a C program.
22# They are linked as C++ code to the executable qconf
23
24__hostprogs := $(sort $(hostprogs-y) $(hostprogs-m))
25host-cshlib := $(sort $(hostlibs-y) $(hostlibs-m))
26host-cxxshlib := $(sort $(hostcxxlibs-y) $(hostcxxlibs-m))
27
28# C code
29# Executables compiled from a single .c file
30host-csingle	:= $(foreach m,$(__hostprogs), \
31			$(if $($(m)-objs)$($(m)-cxxobjs)$($(m)-sharedobjs),,$(m)))
32
33# C executables linked based on several .o files
34host-cmulti	:= $(foreach m,$(__hostprogs),\
35		   $(if $($(m)-cxxobjs),,$(if $($(m)-objs),$(m))))
36
37# Shared object libraries
38host-shared	:= $(foreach m,$(__hostprogs),\
39		   $(if $($(m)-sharedobjs),$(m))))
40
41# Object (.o) files compiled from .c files
42host-cobjs	:= $(sort $(foreach m,$(__hostprogs),$($(m)-objs)))
43
44# C++ code
45# C++ executables compiled from at least one .cc file
46# and zero or more .c files
47host-cxxmulti	:= $(foreach m,$(__hostprogs),$(if $($(m)-cxxobjs),$(m)))
48
49# C++ Object (.o) files compiled from .cc files
50host-cxxobjs	:= $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs)))
51
52# Object (.o) files used by the shared libaries
53host-cshobjs	:= $(sort $(foreach m,$(host-cshlib),$($(m:.so=-objs))))
54host-cxxshobjs	:= $(sort $(foreach m,$(host-cxxshlib),$($(m:.so=-objs))))
55
56host-csingle	:= $(addprefix $(obj)/,$(host-csingle))
57host-cmulti	:= $(addprefix $(obj)/,$(host-cmulti))
58host-cobjs	:= $(addprefix $(obj)/,$(host-cobjs))
59host-cxxmulti	:= $(addprefix $(obj)/,$(host-cxxmulti))
60host-cxxobjs	:= $(addprefix $(obj)/,$(host-cxxobjs))
61host-cshlib	:= $(addprefix $(obj)/,$(host-cshlib))
62host-cxxshlib	:= $(addprefix $(obj)/,$(host-cxxshlib))
63host-cshobjs	:= $(addprefix $(obj)/,$(host-cshobjs))
64host-cxxshobjs	:= $(addprefix $(obj)/,$(host-cxxshobjs))
65host-shared	:= $(addprefix $(obj)/,$(host-shared))
66
67#####
68# Handle options to gcc. Support building with separate output directory
69
70_hostc_flags   = $(KBUILD_HOSTCFLAGS)   $(HOST_EXTRACFLAGS)   \
71                 $(HOSTCFLAGS_$(basetarget).o)
72_hostcxx_flags = $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
73                 $(HOSTCXXFLAGS_$(basetarget).o)
74
75ifeq ($(KBUILD_SRC),)
76__hostc_flags	= $(_hostc_flags)
77__hostcxx_flags	= $(_hostcxx_flags)
78else
79__hostc_flags	= -I$(obj) $(call flags,_hostc_flags)
80__hostcxx_flags	= -I$(obj) $(call flags,_hostcxx_flags)
81endif
82
83hostc_flags    = -Wp,-MD,$(depfile) $(__hostc_flags)
84hostcxx_flags  = -Wp,-MD,$(depfile) $(__hostcxx_flags)
85
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  $@
92      cmd_host-csingle	= $(HOSTCC) $(hostc_flags) -o $@ $< \
93	  	$(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(@F))
94$(host-csingle): $(obj)/%: $(src)/%.c FORCE
95	$(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  $@
100      cmd_host-cmulti	= $(HOSTCC) $(KBUILD_HOSTLDFLAGS) -o $@ \
101			  $(addprefix $(obj)/,$($(@F)-objs)) \
102			  $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(@F))
103$(host-cmulti): FORCE
104	$(call if_changed,host-cmulti)
105$(call multi_depend, $(host-cmulti), , -objs)
106
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 $@ $<
111$(host-cobjs): $(obj)/%.o: $(src)/%.c FORCE
112	$(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  $@
117      cmd_host-cxxmulti	= $(HOSTCXX) $(KBUILD_HOSTLDFLAGS) -o $@ \
118			  $(foreach o,objs cxxobjs,\
119			  $(addprefix $(obj)/,$($(@F)-$(o)))) \
120			  $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(@F))
121$(host-cxxmulti): FORCE
122	$(call if_changed,host-cxxmulti)
123$(call multi_depend, $(host-cxxmulti), , -objs -cxxobjs)
124
125# Create .o file from a single .cc (C++) file
126quiet_cmd_host-cxxobjs	= HOSTCXX $@
127      cmd_host-cxxobjs	= $(HOSTCXX) $(hostcxx_flags) -c -o $@ $<
128$(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE
129	$(call if_changed_dep,host-cxxobjs)
130
131# Compile .c file, create position independent .o file
132# host-cshobjs -> .o
133quiet_cmd_host-cshobjs	= HOSTCC  -fPIC $@
134      cmd_host-cshobjs	= $(HOSTCC) $(hostc_flags) -fPIC -c -o $@ $<
135$(host-cshobjs): $(obj)/%.o: $(src)/%.c FORCE
136	$(call if_changed_dep,host-cshobjs)
137
138# Compile .c file, create position independent .o file
139# Note that plugin capable gcc versions can be either C or C++ based
140# therefore plugin source files have to be compilable in both C and C++ mode.
141# This is why a C++ compiler is invoked on a .c file.
142# host-cxxshobjs -> .o
143quiet_cmd_host-cxxshobjs	= HOSTCXX -fPIC $@
144      cmd_host-cxxshobjs	= $(HOSTCXX) $(hostcxx_flags) -fPIC -c -o $@ $<
145$(host-cxxshobjs): $(obj)/%.o: $(src)/%.c FORCE
146	$(call if_changed_dep,host-cxxshobjs)
147
148# Link a shared library, based on position independent .o files
149# *.o -> .so shared library (host-cshlib)
150quiet_cmd_host-cshlib	= HOSTLLD -shared $@
151      cmd_host-cshlib	= $(HOSTCC) $(HOSTLDFLAGS) -shared -o $@ \
152			  $(addprefix $(obj)/,$($(@F:.so=-objs))) \
153			  $(HOST_LOADLIBES) $(HOSTLDLIBS_$(@F))
154$(host-cshlib): FORCE
155	$(call if_changed,host-cshlib)
156$(call multi_depend, $(host-cshlib), .so, -objs)
157
158# Link a shared library, based on position independent .o files
159# *.o -> .so shared library (host-cxxshlib)
160quiet_cmd_host-cxxshlib	= HOSTLLD -shared $@
161      cmd_host-cxxshlib	= $(HOSTCXX) $(HOSTLDFLAGS) -shared -o $@ \
162			  $(addprefix $(obj)/,$($(@F:.so=-objs))) \
163			  $(HOST_LOADLIBES) $(HOSTLDLIBS_$(@F))
164$(host-cxxshlib): FORCE
165	$(call if_changed,host-cxxshlib)
166$(call multi_depend, $(host-cxxshlib), .so, -objs)
167
168targets += $(host-csingle)  $(host-cmulti) $(host-cobjs)\
169	   $(host-cxxmulti) $(host-cxxobjs) $(host-shared) \
170	   $(host-cshlib) $(host-cshobjs) $(host-cxxshlib) $(host-cxxshobjs)
171