Makefile.host 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. __hostprogs := $(sort $(hostprogs-y) $(hostprogs-m))
  24. host-cshlib := $(sort $(hostlibs-y) $(hostlibs-m))
  25. host-cxxshlib := $(sort $(hostcxxlibs-y) $(hostcxxlibs-m))
  26. # C code
  27. # Executables compiled from a single .c file
  28. host-csingle := $(foreach m,$(__hostprogs), \
  29. $(if $($(m)-objs)$($(m)-cxxobjs)$($(m)-sharedobjs),,$(m)))
  30. # C executables linked based on several .o files
  31. host-cmulti := $(foreach m,$(__hostprogs),\
  32. $(if $($(m)-cxxobjs),,$(if $($(m)-objs),$(m))))
  33. # Shared object libraries
  34. host-shared := $(foreach m,$(__hostprogs),\
  35. $(if $($(m)-sharedobjs),$(m))))
  36. # Object (.o) files compiled from .c files
  37. host-cobjs := $(sort $(foreach m,$(__hostprogs),$($(m)-objs)))
  38. # C++ code
  39. # C++ executables compiled from at least one .cc file
  40. # and zero or more .c files
  41. host-cxxmulti := $(foreach m,$(__hostprogs),$(if $($(m)-cxxobjs),$(m)))
  42. # C++ Object (.o) files compiled from .cc files
  43. host-cxxobjs := $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs)))
  44. # Object (.o) files used by the shared libaries
  45. host-cshobjs := $(sort $(foreach m,$(host-cshlib),$($(m:.so=-objs))))
  46. host-cxxshobjs := $(sort $(foreach m,$(host-cxxshlib),$($(m:.so=-objs))))
  47. # output directory for programs/.o files
  48. # hostprogs-y := tools/build may have been specified.
  49. # Retrieve also directory of .o files from prog-objs or prog-cxxobjs notation
  50. host-objdirs := $(dir $(__hostprogs) $(host-cobjs) $(host-cxxobjs))
  51. host-objdirs := $(strip $(sort $(filter-out ./,$(host-objdirs))))
  52. __hostprogs := $(addprefix $(obj)/,$(__hostprogs))
  53. host-csingle := $(addprefix $(obj)/,$(host-csingle))
  54. host-cmulti := $(addprefix $(obj)/,$(host-cmulti))
  55. host-cobjs := $(addprefix $(obj)/,$(host-cobjs))
  56. host-cxxmulti := $(addprefix $(obj)/,$(host-cxxmulti))
  57. host-cxxobjs := $(addprefix $(obj)/,$(host-cxxobjs))
  58. host-cshlib := $(addprefix $(obj)/,$(host-cshlib))
  59. host-cxxshlib := $(addprefix $(obj)/,$(host-cxxshlib))
  60. host-cshobjs := $(addprefix $(obj)/,$(host-cshobjs))
  61. host-cxxshobjs := $(addprefix $(obj)/,$(host-cxxshobjs))
  62. host-shared := $(addprefix $(obj)/,$(host-shared))
  63. host-objdirs := $(addprefix $(obj)/,$(host-objdirs))
  64. obj-dirs += $(host-objdirs)
  65. #####
  66. # Handle options to gcc. Support building with separate output directory
  67. _hostc_flags = $(KBUILD_HOSTCFLAGS) $(HOST_EXTRACFLAGS) \
  68. $(HOSTCFLAGS_$(basetarget).o)
  69. _hostcxx_flags = $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
  70. $(HOSTCXXFLAGS_$(basetarget).o)
  71. ifeq ($(KBUILD_SRC),)
  72. __hostc_flags = $(_hostc_flags)
  73. __hostcxx_flags = $(_hostcxx_flags)
  74. else
  75. __hostc_flags = -I$(obj) $(call flags,_hostc_flags)
  76. __hostcxx_flags = -I$(obj) $(call flags,_hostcxx_flags)
  77. endif
  78. hostc_flags = -Wp,-MD,$(depfile) $(__hostc_flags)
  79. hostcxx_flags = -Wp,-MD,$(depfile) $(__hostcxx_flags)
  80. #####
  81. # Compile programs on the host
  82. # Create executable from a single .c file
  83. # host-csingle -> Executable
  84. quiet_cmd_host-csingle = HOSTCC $@
  85. cmd_host-csingle = $(HOSTCC) $(hostc_flags) -o $@ $< \
  86. $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(@F))
  87. $(host-csingle): $(obj)/%: $(src)/%.c FORCE
  88. $(call if_changed_dep,host-csingle)
  89. # Link an executable based on list of .o files, all plain c
  90. # host-cmulti -> executable
  91. quiet_cmd_host-cmulti = HOSTLD $@
  92. cmd_host-cmulti = $(HOSTCC) $(KBUILD_HOSTLDFLAGS) -o $@ \
  93. $(addprefix $(obj)/,$($(@F)-objs)) \
  94. $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(@F))
  95. $(host-cmulti): FORCE
  96. $(call if_changed,host-cmulti)
  97. $(call multi_depend, $(host-cmulti), , -objs)
  98. # Create .o file from a single .c file
  99. # host-cobjs -> .o
  100. quiet_cmd_host-cobjs = HOSTCC $@
  101. cmd_host-cobjs = $(HOSTCC) $(hostc_flags) -c -o $@ $<
  102. $(host-cobjs): $(obj)/%.o: $(src)/%.c FORCE
  103. $(call if_changed_dep,host-cobjs)
  104. # Link an executable based on list of .o files, a mixture of .c and .cc
  105. # host-cxxmulti -> executable
  106. quiet_cmd_host-cxxmulti = HOSTLD $@
  107. cmd_host-cxxmulti = $(HOSTCXX) $(KBUILD_HOSTLDFLAGS) -o $@ \
  108. $(foreach o,objs cxxobjs,\
  109. $(addprefix $(obj)/,$($(@F)-$(o)))) \
  110. $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(@F))
  111. $(host-cxxmulti): FORCE
  112. $(call if_changed,host-cxxmulti)
  113. $(call multi_depend, $(host-cxxmulti), , -objs -cxxobjs)
  114. # Create .o file from a single .cc (C++) file
  115. quiet_cmd_host-cxxobjs = HOSTCXX $@
  116. cmd_host-cxxobjs = $(HOSTCXX) $(hostcxx_flags) -c -o $@ $<
  117. $(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE
  118. $(call if_changed_dep,host-cxxobjs)
  119. # Compile .c file, create position independent .o file
  120. # host-cshobjs -> .o
  121. quiet_cmd_host-cshobjs = HOSTCC -fPIC $@
  122. cmd_host-cshobjs = $(HOSTCC) $(hostc_flags) -fPIC -c -o $@ $<
  123. $(host-cshobjs): $(obj)/%.o: $(src)/%.c FORCE
  124. $(call if_changed_dep,host-cshobjs)
  125. # Compile .c file, create position independent .o file
  126. # Note that plugin capable gcc versions can be either C or C++ based
  127. # therefore plugin source files have to be compilable in both C and C++ mode.
  128. # This is why a C++ compiler is invoked on a .c file.
  129. # host-cxxshobjs -> .o
  130. quiet_cmd_host-cxxshobjs = HOSTCXX -fPIC $@
  131. cmd_host-cxxshobjs = $(HOSTCXX) $(hostcxx_flags) -fPIC -c -o $@ $<
  132. $(host-cxxshobjs): $(obj)/%.o: $(src)/%.c FORCE
  133. $(call if_changed_dep,host-cxxshobjs)
  134. # Link a shared library, based on position independent .o files
  135. # *.o -> .so shared library (host-cshlib)
  136. quiet_cmd_host-cshlib = HOSTLLD -shared $@
  137. cmd_host-cshlib = $(HOSTCC) $(HOSTLDFLAGS) -shared -o $@ \
  138. $(addprefix $(obj)/,$($(@F:.so=-objs))) \
  139. $(HOST_LOADLIBES) $(HOSTLDLIBS_$(@F))
  140. $(host-cshlib): FORCE
  141. $(call if_changed,host-cshlib)
  142. $(call multi_depend, $(host-cshlib), .so, -objs)
  143. # Link a shared library, based on position independent .o files
  144. # *.o -> .so shared library (host-cxxshlib)
  145. quiet_cmd_host-cxxshlib = HOSTLLD -shared $@
  146. cmd_host-cxxshlib = $(HOSTCXX) $(HOSTLDFLAGS) -shared -o $@ \
  147. $(addprefix $(obj)/,$($(@F:.so=-objs))) \
  148. $(HOST_LOADLIBES) $(HOSTLDLIBS_$(@F))
  149. $(host-cxxshlib): FORCE
  150. $(call if_changed,host-cxxshlib)
  151. $(call multi_depend, $(host-cxxshlib), .so, -objs)
  152. targets += $(host-csingle) $(host-cmulti) $(host-cobjs)\
  153. $(host-cxxmulti) $(host-cxxobjs) $(host-shared) \
  154. $(host-cshlib) $(host-cshobjs) $(host-cxxshlib) $(host-cxxshobjs)