Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | # |
Robert P. J. Day | 3156fd0 | 2008-02-18 04:48:20 -0500 | [diff] [blame] | 6 | # Both C and C++ are supported, but preferred language is C for such utilities. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | # |
Robert P. J. Day | 3156fd0 | 2008-02-18 04:48:20 -0500 | [diff] [blame] | 8 | # Sample syntax (see Documentation/kbuild/makefiles.txt for reference) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Ross Biro | 8f5cbd7 | 2006-09-16 12:15:39 -0700 | [diff] [blame] | 23 | __hostprogs := $(sort $(hostprogs-y) $(hostprogs-m)) |
Emese Revfy | 2440387 | 2016-05-24 00:08:25 +0200 | [diff] [blame] | 24 | host-cshlib := $(sort $(hostlibs-y) $(hostlibs-m)) |
| 25 | host-cxxshlib := $(sort $(hostcxxlibs-y) $(hostcxxlibs-m)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | # C code |
| 28 | # Executables compiled from a single .c file |
Masahiro Yamada | edb950c | 2014-07-16 16:12:20 +0900 | [diff] [blame] | 29 | host-csingle := $(foreach m,$(__hostprogs), \ |
| 30 | $(if $($(m)-objs)$($(m)-cxxobjs),,$(m))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | # C executables linked based on several .o files |
| 33 | host-cmulti := $(foreach m,$(__hostprogs),\ |
| 34 | $(if $($(m)-cxxobjs),,$(if $($(m)-objs),$(m)))) |
| 35 | |
| 36 | # Object (.o) files compiled from .c files |
| 37 | host-cobjs := $(sort $(foreach m,$(__hostprogs),$($(m)-objs))) |
| 38 | |
| 39 | # C++ code |
Masahiro Yamada | d8d9efe | 2014-07-16 16:12:19 +0900 | [diff] [blame] | 40 | # C++ executables compiled from at least one .cc file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | # and zero or more .c files |
| 42 | host-cxxmulti := $(foreach m,$(__hostprogs),$(if $($(m)-cxxobjs),$(m))) |
| 43 | |
| 44 | # C++ Object (.o) files compiled from .cc files |
| 45 | host-cxxobjs := $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs))) |
| 46 | |
Emese Revfy | 2440387 | 2016-05-24 00:08:25 +0200 | [diff] [blame] | 47 | # Object (.o) files used by the shared libaries |
| 48 | host-cshobjs := $(sort $(foreach m,$(host-cshlib),$($(m:.so=-objs)))) |
| 49 | host-cxxshobjs := $(sort $(foreach m,$(host-cxxshlib),$($(m:.so=-objs)))) |
| 50 | |
Sam Ravnborg | 7b5b820 | 2006-08-07 21:55:33 +0200 | [diff] [blame] | 51 | # output directory for programs/.o files |
Masahiro Yamada | 1791ff7 | 2014-07-16 16:12:23 +0900 | [diff] [blame] | 52 | # hostprogs-y := tools/build may have been specified. |
| 53 | # Retrieve also directory of .o files from prog-objs or prog-cxxobjs notation |
| 54 | host-objdirs := $(dir $(__hostprogs) $(host-cobjs) $(host-cxxobjs)) |
Sam Ravnborg | 7b5b820 | 2006-08-07 21:55:33 +0200 | [diff] [blame] | 55 | |
| 56 | host-objdirs := $(strip $(sort $(filter-out ./,$(host-objdirs)))) |
| 57 | |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | __hostprogs := $(addprefix $(obj)/,$(__hostprogs)) |
| 60 | host-csingle := $(addprefix $(obj)/,$(host-csingle)) |
| 61 | host-cmulti := $(addprefix $(obj)/,$(host-cmulti)) |
| 62 | host-cobjs := $(addprefix $(obj)/,$(host-cobjs)) |
| 63 | host-cxxmulti := $(addprefix $(obj)/,$(host-cxxmulti)) |
| 64 | host-cxxobjs := $(addprefix $(obj)/,$(host-cxxobjs)) |
Emese Revfy | 2440387 | 2016-05-24 00:08:25 +0200 | [diff] [blame] | 65 | host-cshlib := $(addprefix $(obj)/,$(host-cshlib)) |
| 66 | host-cxxshlib := $(addprefix $(obj)/,$(host-cxxshlib)) |
| 67 | host-cshobjs := $(addprefix $(obj)/,$(host-cshobjs)) |
| 68 | host-cxxshobjs := $(addprefix $(obj)/,$(host-cxxshobjs)) |
Pavel Roskin | 9870a93 | 2006-06-01 21:28:50 -0400 | [diff] [blame] | 69 | host-objdirs := $(addprefix $(obj)/,$(host-objdirs)) |
| 70 | |
| 71 | obj-dirs += $(host-objdirs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | ##### |
| 74 | # Handle options to gcc. Support building with separate output directory |
| 75 | |
Sam Ravnborg | 5e8d780 | 2006-07-01 09:58:02 +0200 | [diff] [blame] | 76 | _hostc_flags = $(HOSTCFLAGS) $(HOST_EXTRACFLAGS) \ |
| 77 | $(HOSTCFLAGS_$(basetarget).o) |
| 78 | _hostcxx_flags = $(HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \ |
| 79 | $(HOSTCXXFLAGS_$(basetarget).o) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | ifeq ($(KBUILD_SRC),) |
| 82 | __hostc_flags = $(_hostc_flags) |
| 83 | __hostcxx_flags = $(_hostcxx_flags) |
| 84 | else |
| 85 | __hostc_flags = -I$(obj) $(call flags,_hostc_flags) |
| 86 | __hostcxx_flags = -I$(obj) $(call flags,_hostcxx_flags) |
| 87 | endif |
| 88 | |
| 89 | hostc_flags = -Wp,-MD,$(depfile) $(__hostc_flags) |
| 90 | hostcxx_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 |
| 97 | quiet_cmd_host-csingle = HOSTCC $@ |
Robin Jarry | 8b201ca | 2018-02-26 19:41:47 +0100 | [diff] [blame] | 98 | cmd_host-csingle = $(HOSTCC) $(hostc_flags) $(HOSTLDFLAGS) -o $@ $< \ |
Matthias Urlichs | e0af0d8 | 2005-02-18 10:23:41 +0100 | [diff] [blame] | 99 | $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F)) |
Sam Ravnborg | 767e581 | 2007-05-06 09:23:45 +0200 | [diff] [blame] | 100 | $(host-csingle): $(obj)/%: $(src)/%.c FORCE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
| 105 | quiet_cmd_host-cmulti = HOSTLD $@ |
| 106 | cmd_host-cmulti = $(HOSTCC) $(HOSTLDFLAGS) -o $@ \ |
| 107 | $(addprefix $(obj)/,$($(@F)-objs)) \ |
| 108 | $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F)) |
Masahiro Yamada | 97e3226 | 2014-08-19 16:34:21 +0900 | [diff] [blame] | 109 | $(host-cmulti): FORCE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | $(call if_changed,host-cmulti) |
Masahiro Yamada | 97e3226 | 2014-08-19 16:34:21 +0900 | [diff] [blame] | 111 | $(call multi_depend, $(host-cmulti), , -objs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | # Create .o file from a single .c file |
| 114 | # host-cobjs -> .o |
| 115 | quiet_cmd_host-cobjs = HOSTCC $@ |
| 116 | cmd_host-cobjs = $(HOSTCC) $(hostc_flags) -c -o $@ $< |
Sam Ravnborg | 767e581 | 2007-05-06 09:23:45 +0200 | [diff] [blame] | 117 | $(host-cobjs): $(obj)/%.o: $(src)/%.c FORCE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
| 122 | quiet_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 Yamada | 97e3226 | 2014-08-19 16:34:21 +0900 | [diff] [blame] | 127 | $(host-cxxmulti): FORCE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | $(call if_changed,host-cxxmulti) |
Masahiro Yamada | 97e3226 | 2014-08-19 16:34:21 +0900 | [diff] [blame] | 129 | $(call multi_depend, $(host-cxxmulti), , -objs -cxxobjs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | # Create .o file from a single .cc (C++) file |
| 132 | quiet_cmd_host-cxxobjs = HOSTCXX $@ |
| 133 | cmd_host-cxxobjs = $(HOSTCXX) $(hostcxx_flags) -c -o $@ $< |
Sam Ravnborg | 767e581 | 2007-05-06 09:23:45 +0200 | [diff] [blame] | 134 | $(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | $(call if_changed_dep,host-cxxobjs) |
| 136 | |
Emese Revfy | 2440387 | 2016-05-24 00:08:25 +0200 | [diff] [blame] | 137 | # Compile .c file, create position independent .o file |
| 138 | # host-cshobjs -> .o |
| 139 | quiet_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 |
| 149 | quiet_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) |
| 156 | quiet_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) |
| 166 | quiet_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | targets += $(host-csingle) $(host-cmulti) $(host-cobjs)\ |
Emese Revfy | 2440387 | 2016-05-24 00:08:25 +0200 | [diff] [blame] | 175 | $(host-cxxmulti) $(host-cxxobjs) $(host-cshlib) $(host-cshobjs) $(host-cxxshlib) $(host-cxxshobjs) |