blob: 9cfd5c84d76f555e7be0f8fbfae1b60b79fe2831 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001# ==========================================================================
2# Building binaries on the host system
3# Binaries are used during the compilation of the kernel, for example
4# to preprocess a data file.
5#
Robert P. J. Day3156fd02008-02-18 04:48:20 -05006# Both C and C++ are supported, but preferred language is C for such utilities.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#
Robert P. J. Day3156fd02008-02-18 04:48:20 -05008# Sample syntax (see Documentation/kbuild/makefiles.txt for reference)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009# hostprogs-y := bin2hex
10# Will compile bin2hex.c and create an executable named bin2hex
11#
12# hostprogs-y := lxdialog
13# lxdialog-objs := checklist.o lxdialog.o
14# Will compile lxdialog.c and checklist.c, and then link the executable
15# lxdialog, based on checklist.o and lxdialog.o
16#
17# hostprogs-y := qconf
18# qconf-cxxobjs := qconf.o
19# qconf-objs := menu.o
20# Will compile qconf as a C++ program, and menu as a C program.
21# They are linked as C++ code to the executable qconf
22
Ross Biro8f5cbd72006-09-16 12:15:39 -070023__hostprogs := $(sort $(hostprogs-y) $(hostprogs-m))
Emese Revfy24403872016-05-24 00:08:25 +020024host-cshlib := $(sort $(hostlibs-y) $(hostlibs-m))
25host-cxxshlib := $(sort $(hostcxxlibs-y) $(hostcxxlibs-m))
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027# C code
28# Executables compiled from a single .c file
Masahiro Yamadaedb950c2014-07-16 16:12:20 +090029host-csingle := $(foreach m,$(__hostprogs), \
30 $(if $($(m)-objs)$($(m)-cxxobjs),,$(m)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32# C executables linked based on several .o files
33host-cmulti := $(foreach m,$(__hostprogs),\
34 $(if $($(m)-cxxobjs),,$(if $($(m)-objs),$(m))))
35
36# Object (.o) files compiled from .c files
37host-cobjs := $(sort $(foreach m,$(__hostprogs),$($(m)-objs)))
38
39# C++ code
Masahiro Yamadad8d9efe2014-07-16 16:12:19 +090040# C++ executables compiled from at least one .cc file
Linus Torvalds1da177e2005-04-16 15:20:36 -070041# and zero or more .c files
42host-cxxmulti := $(foreach m,$(__hostprogs),$(if $($(m)-cxxobjs),$(m)))
43
44# C++ Object (.o) files compiled from .cc files
45host-cxxobjs := $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs)))
46
Emese Revfy24403872016-05-24 00:08:25 +020047# Object (.o) files used by the shared libaries
48host-cshobjs := $(sort $(foreach m,$(host-cshlib),$($(m:.so=-objs))))
49host-cxxshobjs := $(sort $(foreach m,$(host-cxxshlib),$($(m:.so=-objs))))
50
Sam Ravnborg7b5b8202006-08-07 21:55:33 +020051# output directory for programs/.o files
Masahiro Yamada1791ff72014-07-16 16:12:23 +090052# hostprogs-y := tools/build may have been specified.
53# Retrieve also directory of .o files from prog-objs or prog-cxxobjs notation
54host-objdirs := $(dir $(__hostprogs) $(host-cobjs) $(host-cxxobjs))
Sam Ravnborg7b5b8202006-08-07 21:55:33 +020055
56host-objdirs := $(strip $(sort $(filter-out ./,$(host-objdirs))))
57
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059__hostprogs := $(addprefix $(obj)/,$(__hostprogs))
60host-csingle := $(addprefix $(obj)/,$(host-csingle))
61host-cmulti := $(addprefix $(obj)/,$(host-cmulti))
62host-cobjs := $(addprefix $(obj)/,$(host-cobjs))
63host-cxxmulti := $(addprefix $(obj)/,$(host-cxxmulti))
64host-cxxobjs := $(addprefix $(obj)/,$(host-cxxobjs))
Emese Revfy24403872016-05-24 00:08:25 +020065host-cshlib := $(addprefix $(obj)/,$(host-cshlib))
66host-cxxshlib := $(addprefix $(obj)/,$(host-cxxshlib))
67host-cshobjs := $(addprefix $(obj)/,$(host-cshobjs))
68host-cxxshobjs := $(addprefix $(obj)/,$(host-cxxshobjs))
Pavel Roskin9870a932006-06-01 21:28:50 -040069host-objdirs := $(addprefix $(obj)/,$(host-objdirs))
70
71obj-dirs += $(host-objdirs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73#####
74# Handle options to gcc. Support building with separate output directory
75
Sam Ravnborg5e8d7802006-07-01 09:58:02 +020076_hostc_flags = $(HOSTCFLAGS) $(HOST_EXTRACFLAGS) \
77 $(HOSTCFLAGS_$(basetarget).o)
78_hostcxx_flags = $(HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
79 $(HOSTCXXFLAGS_$(basetarget).o)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81ifeq ($(KBUILD_SRC),)
82__hostc_flags = $(_hostc_flags)
83__hostcxx_flags = $(_hostcxx_flags)
84else
85__hostc_flags = -I$(obj) $(call flags,_hostc_flags)
86__hostcxx_flags = -I$(obj) $(call flags,_hostcxx_flags)
87endif
88
89hostc_flags = -Wp,-MD,$(depfile) $(__hostc_flags)
90hostcxx_flags = -Wp,-MD,$(depfile) $(__hostcxx_flags)
91
92#####
93# Compile programs on the host
94
95# Create executable from a single .c file
96# host-csingle -> Executable
97quiet_cmd_host-csingle = HOSTCC $@
Matthias Urlichse0af0d82005-02-18 10:23:41 +010098 cmd_host-csingle = $(HOSTCC) $(hostc_flags) -o $@ $< \
99 $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
Sam Ravnborg767e5812007-05-06 09:23:45 +0200100$(host-csingle): $(obj)/%: $(src)/%.c FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 $(call if_changed_dep,host-csingle)
102
103# Link an executable based on list of .o files, all plain c
104# host-cmulti -> executable
105quiet_cmd_host-cmulti = HOSTLD $@
106 cmd_host-cmulti = $(HOSTCC) $(HOSTLDFLAGS) -o $@ \
107 $(addprefix $(obj)/,$($(@F)-objs)) \
108 $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
Masahiro Yamada97e32262014-08-19 16:34:21 +0900109$(host-cmulti): FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 $(call if_changed,host-cmulti)
Masahiro Yamada97e32262014-08-19 16:34:21 +0900111$(call multi_depend, $(host-cmulti), , -objs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113# Create .o file from a single .c file
114# host-cobjs -> .o
115quiet_cmd_host-cobjs = HOSTCC $@
116 cmd_host-cobjs = $(HOSTCC) $(hostc_flags) -c -o $@ $<
Sam Ravnborg767e5812007-05-06 09:23:45 +0200117$(host-cobjs): $(obj)/%.o: $(src)/%.c FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 $(call if_changed_dep,host-cobjs)
119
120# Link an executable based on list of .o files, a mixture of .c and .cc
121# host-cxxmulti -> executable
122quiet_cmd_host-cxxmulti = HOSTLD $@
123 cmd_host-cxxmulti = $(HOSTCXX) $(HOSTLDFLAGS) -o $@ \
124 $(foreach o,objs cxxobjs,\
125 $(addprefix $(obj)/,$($(@F)-$(o)))) \
126 $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
Masahiro Yamada97e32262014-08-19 16:34:21 +0900127$(host-cxxmulti): FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 $(call if_changed,host-cxxmulti)
Masahiro Yamada97e32262014-08-19 16:34:21 +0900129$(call multi_depend, $(host-cxxmulti), , -objs -cxxobjs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131# Create .o file from a single .cc (C++) file
132quiet_cmd_host-cxxobjs = HOSTCXX $@
133 cmd_host-cxxobjs = $(HOSTCXX) $(hostcxx_flags) -c -o $@ $<
Sam Ravnborg767e5812007-05-06 09:23:45 +0200134$(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 $(call if_changed_dep,host-cxxobjs)
136
Emese Revfy24403872016-05-24 00:08:25 +0200137# Compile .c file, create position independent .o file
138# host-cshobjs -> .o
139quiet_cmd_host-cshobjs = HOSTCC -fPIC $@
140 cmd_host-cshobjs = $(HOSTCC) $(hostc_flags) -fPIC -c -o $@ $<
141$(host-cshobjs): $(obj)/%.o: $(src)/%.c FORCE
142 $(call if_changed_dep,host-cshobjs)
143
144# Compile .c file, create position independent .o file
145# Note that plugin capable gcc versions can be either C or C++ based
146# therefore plugin source files have to be compilable in both C and C++ mode.
147# This is why a C++ compiler is invoked on a .c file.
148# host-cxxshobjs -> .o
149quiet_cmd_host-cxxshobjs = HOSTCXX -fPIC $@
150 cmd_host-cxxshobjs = $(HOSTCXX) $(hostcxx_flags) -fPIC -c -o $@ $<
151$(host-cxxshobjs): $(obj)/%.o: $(src)/%.c FORCE
152 $(call if_changed_dep,host-cxxshobjs)
153
154# Link a shared library, based on position independent .o files
155# *.o -> .so shared library (host-cshlib)
156quiet_cmd_host-cshlib = HOSTLLD -shared $@
157 cmd_host-cshlib = $(HOSTCC) $(HOSTLDFLAGS) -shared -o $@ \
158 $(addprefix $(obj)/,$($(@F:.so=-objs))) \
159 $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
160$(host-cshlib): FORCE
161 $(call if_changed,host-cshlib)
162$(call multi_depend, $(host-cshlib), .so, -objs)
163
164# Link a shared library, based on position independent .o files
165# *.o -> .so shared library (host-cxxshlib)
166quiet_cmd_host-cxxshlib = HOSTLLD -shared $@
167 cmd_host-cxxshlib = $(HOSTCXX) $(HOSTLDFLAGS) -shared -o $@ \
168 $(addprefix $(obj)/,$($(@F:.so=-objs))) \
169 $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
170$(host-cxxshlib): FORCE
171 $(call if_changed,host-cxxshlib)
172$(call multi_depend, $(host-cxxshlib), .so, -objs)
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174targets += $(host-csingle) $(host-cmulti) $(host-cobjs)\
Emese Revfy24403872016-05-24 00:08:25 +0200175 $(host-cxxmulti) $(host-cxxobjs) $(host-cshlib) $(host-cshobjs) $(host-cxxshlib) $(host-cxxshobjs)