xref: /linux/scripts/Makefile.host (revision c411ed854584a71b0e86ac3019b60e4789d88086)
1# ==========================================================================
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#
6# Both C and C++ are supported, but preferred language is C for such utilities.
7#
8# Sample syntax (see Documentation/kbuild/makefiles.txt for reference)
9# 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
23__hostprogs := $(sort $(hostprogs-y) $(hostprogs-m))
24host-cshlib := $(sort $(hostlibs-y) $(hostlibs-m))
25host-cxxshlib := $(sort $(hostcxxlibs-y) $(hostcxxlibs-m))
26
27# C code
28# Executables compiled from a single .c file
29host-csingle	:= $(foreach m,$(__hostprogs), \
30			$(if $($(m)-objs)$($(m)-cxxobjs),,$(m)))
31
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
40# C++ executables compiled from at least one .cc file
41# 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
47# 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
51# output directory for programs/.o files
52# 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))
55
56host-objdirs := $(strip $(sort $(filter-out ./,$(host-objdirs))))
57
58
59__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))
65host-cshlib	:= $(addprefix $(obj)/,$(host-cshlib))
66host-cxxshlib	:= $(addprefix $(obj)/,$(host-cxxshlib))
67host-cshobjs	:= $(addprefix $(obj)/,$(host-cshobjs))
68host-cxxshobjs	:= $(addprefix $(obj)/,$(host-cxxshobjs))
69host-objdirs    := $(addprefix $(obj)/,$(host-objdirs))
70
71obj-dirs += $(host-objdirs)
72
73#####
74# Handle options to gcc. Support building with separate output directory
75
76_hostc_flags   = $(HOSTCFLAGS)   $(HOST_EXTRACFLAGS)   \
77                 $(HOSTCFLAGS_$(basetarget).o)
78_hostcxx_flags = $(HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
79                 $(HOSTCXXFLAGS_$(basetarget).o)
80
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  $@
98      cmd_host-csingle	= $(HOSTCC) $(hostc_flags) -o $@ $< \
99	  	$(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
100$(host-csingle): $(obj)/%: $(src)/%.c FORCE
101	$(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))
109$(host-cmulti): FORCE
110	$(call if_changed,host-cmulti)
111$(call multi_depend, $(host-cmulti), , -objs)
112
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 $@ $<
117$(host-cobjs): $(obj)/%.o: $(src)/%.c FORCE
118	$(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))
127$(host-cxxmulti): FORCE
128	$(call if_changed,host-cxxmulti)
129$(call multi_depend, $(host-cxxmulti), , -objs -cxxobjs)
130
131# Create .o file from a single .cc (C++) file
132quiet_cmd_host-cxxobjs	= HOSTCXX $@
133      cmd_host-cxxobjs	= $(HOSTCXX) $(hostcxx_flags) -c -o $@ $<
134$(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE
135	$(call if_changed_dep,host-cxxobjs)
136
137# 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
174targets += $(host-csingle)  $(host-cmulti) $(host-cobjs)\
175	   $(host-cxxmulti) $(host-cxxobjs) $(host-cshlib) $(host-cshobjs) $(host-cxxshlib) $(host-cxxshobjs)
176