blob: bb6a11f4f19387e404839d913b747d6571e6ec7e [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))
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025# C code
26# Executables compiled from a single .c file
Masahiro Yamadaedb950c2014-07-16 16:12:20 +090027host-csingle := $(foreach m,$(__hostprogs), \
28 $(if $($(m)-objs)$($(m)-cxxobjs),,$(m)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30# C executables linked based on several .o files
31host-cmulti := $(foreach m,$(__hostprogs),\
32 $(if $($(m)-cxxobjs),,$(if $($(m)-objs),$(m))))
33
34# Object (.o) files compiled from .c files
35host-cobjs := $(sort $(foreach m,$(__hostprogs),$($(m)-objs)))
36
37# C++ code
Masahiro Yamadad8d9efe2014-07-16 16:12:19 +090038# C++ executables compiled from at least one .cc file
Linus Torvalds1da177e2005-04-16 15:20:36 -070039# and zero or more .c files
40host-cxxmulti := $(foreach m,$(__hostprogs),$(if $($(m)-cxxobjs),$(m)))
41
42# C++ Object (.o) files compiled from .cc files
43host-cxxobjs := $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs)))
44
Sam Ravnborg7b5b8202006-08-07 21:55:33 +020045# output directory for programs/.o files
Robert P. J. Day3156fd02008-02-18 04:48:20 -050046# hostprogs-y := tools/build may have been specified. Retrieve directory
Masahiro Yamada66393552014-07-16 16:12:22 +090047host-objdirs := $(dir $(__hostprogs))
Sam Ravnborg7b5b8202006-08-07 21:55:33 +020048# directory of .o files from prog-objs notation
Masahiro Yamada66393552014-07-16 16:12:22 +090049host-objdirs += $(dir $(foreach f,$(host-cmulti), $($(f)-objs)))
Sam Ravnborg7b5b8202006-08-07 21:55:33 +020050# directory of .o files from prog-cxxobjs notation
Masahiro Yamada66393552014-07-16 16:12:22 +090051host-objdirs += $(dir $(foreach f,$(host-cxxmulti), $($(f)-cxxobjs)))
Sam Ravnborg7b5b8202006-08-07 21:55:33 +020052
53host-objdirs := $(strip $(sort $(filter-out ./,$(host-objdirs))))
54
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056__hostprogs := $(addprefix $(obj)/,$(__hostprogs))
57host-csingle := $(addprefix $(obj)/,$(host-csingle))
58host-cmulti := $(addprefix $(obj)/,$(host-cmulti))
59host-cobjs := $(addprefix $(obj)/,$(host-cobjs))
60host-cxxmulti := $(addprefix $(obj)/,$(host-cxxmulti))
61host-cxxobjs := $(addprefix $(obj)/,$(host-cxxobjs))
Pavel Roskin9870a932006-06-01 21:28:50 -040062host-objdirs := $(addprefix $(obj)/,$(host-objdirs))
63
64obj-dirs += $(host-objdirs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#####
67# Handle options to gcc. Support building with separate output directory
68
Sam Ravnborg5e8d7802006-07-01 09:58:02 +020069_hostc_flags = $(HOSTCFLAGS) $(HOST_EXTRACFLAGS) \
70 $(HOSTCFLAGS_$(basetarget).o)
71_hostcxx_flags = $(HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
72 $(HOSTCXXFLAGS_$(basetarget).o)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74ifeq ($(KBUILD_SRC),)
75__hostc_flags = $(_hostc_flags)
76__hostcxx_flags = $(_hostcxx_flags)
77else
78__hostc_flags = -I$(obj) $(call flags,_hostc_flags)
79__hostcxx_flags = -I$(obj) $(call flags,_hostcxx_flags)
80endif
81
82hostc_flags = -Wp,-MD,$(depfile) $(__hostc_flags)
83hostcxx_flags = -Wp,-MD,$(depfile) $(__hostcxx_flags)
84
85#####
86# Compile programs on the host
87
88# Create executable from a single .c file
89# host-csingle -> Executable
90quiet_cmd_host-csingle = HOSTCC $@
Matthias Urlichse0af0d82005-02-18 10:23:41 +010091 cmd_host-csingle = $(HOSTCC) $(hostc_flags) -o $@ $< \
92 $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
Sam Ravnborg767e5812007-05-06 09:23:45 +020093$(host-csingle): $(obj)/%: $(src)/%.c FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 $(call if_changed_dep,host-csingle)
95
96# Link an executable based on list of .o files, all plain c
97# host-cmulti -> executable
98quiet_cmd_host-cmulti = HOSTLD $@
99 cmd_host-cmulti = $(HOSTCC) $(HOSTLDFLAGS) -o $@ \
100 $(addprefix $(obj)/,$($(@F)-objs)) \
101 $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
Masahiro Yamada62e22102014-07-16 16:12:21 +0900102$(host-cmulti): $(obj)/%: $(host-cobjs) FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 $(call if_changed,host-cmulti)
104
105# Create .o file from a single .c file
106# host-cobjs -> .o
107quiet_cmd_host-cobjs = HOSTCC $@
108 cmd_host-cobjs = $(HOSTCC) $(hostc_flags) -c -o $@ $<
Sam Ravnborg767e5812007-05-06 09:23:45 +0200109$(host-cobjs): $(obj)/%.o: $(src)/%.c FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 $(call if_changed_dep,host-cobjs)
111
112# Link an executable based on list of .o files, a mixture of .c and .cc
113# host-cxxmulti -> executable
114quiet_cmd_host-cxxmulti = HOSTLD $@
115 cmd_host-cxxmulti = $(HOSTCXX) $(HOSTLDFLAGS) -o $@ \
116 $(foreach o,objs cxxobjs,\
117 $(addprefix $(obj)/,$($(@F)-$(o)))) \
118 $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
Masahiro Yamada62e22102014-07-16 16:12:21 +0900119$(host-cxxmulti): $(obj)/%: $(host-cobjs) $(host-cxxobjs) FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 $(call if_changed,host-cxxmulti)
121
122# Create .o file from a single .cc (C++) file
123quiet_cmd_host-cxxobjs = HOSTCXX $@
124 cmd_host-cxxobjs = $(HOSTCXX) $(hostcxx_flags) -c -o $@ $<
Sam Ravnborg767e5812007-05-06 09:23:45 +0200125$(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 $(call if_changed_dep,host-cxxobjs)
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128targets += $(host-csingle) $(host-cmulti) $(host-cobjs)\
Masahiro Yamada62e22102014-07-16 16:12:21 +0900129 $(host-cxxmulti) $(host-cxxobjs)