Makefile.host 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. host-csingle := $(addprefix $(obj)/,$(host-csingle))
  48. host-cmulti := $(addprefix $(obj)/,$(host-cmulti))
  49. host-cobjs := $(addprefix $(obj)/,$(host-cobjs))
  50. host-cxxmulti := $(addprefix $(obj)/,$(host-cxxmulti))
  51. host-cxxobjs := $(addprefix $(obj)/,$(host-cxxobjs))
  52. host-cshlib := $(addprefix $(obj)/,$(host-cshlib))
  53. host-cxxshlib := $(addprefix $(obj)/,$(host-cxxshlib))
  54. host-cshobjs := $(addprefix $(obj)/,$(host-cshobjs))
  55. host-cxxshobjs := $(addprefix $(obj)/,$(host-cxxshobjs))
  56. host-shared := $(addprefix $(obj)/,$(host-shared))
  57. #####
  58. # Handle options to gcc. Support building with separate output directory
  59. _hostc_flags = $(KBUILD_HOSTCFLAGS) $(HOST_EXTRACFLAGS) \
  60. $(HOSTCFLAGS_$(basetarget).o)
  61. _hostcxx_flags = $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
  62. $(HOSTCXXFLAGS_$(basetarget).o)
  63. ifeq ($(KBUILD_SRC),)
  64. __hostc_flags = $(_hostc_flags)
  65. __hostcxx_flags = $(_hostcxx_flags)
  66. else
  67. __hostc_flags = -I$(obj) $(call flags,_hostc_flags)
  68. __hostcxx_flags = -I$(obj) $(call flags,_hostcxx_flags)
  69. endif
  70. hostc_flags = -Wp,-MD,$(depfile) $(__hostc_flags)
  71. hostcxx_flags = -Wp,-MD,$(depfile) $(__hostcxx_flags)
  72. #####
  73. # Compile programs on the host
  74. # Create executable from a single .c file
  75. # host-csingle -> Executable
  76. quiet_cmd_host-csingle = HOSTCC $@
  77. cmd_host-csingle = $(HOSTCC) $(hostc_flags) -o $@ $< \
  78. $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(@F))
  79. $(host-csingle): $(obj)/%: $(src)/%.c FORCE
  80. $(call if_changed_dep,host-csingle)
  81. # Link an executable based on list of .o files, all plain c
  82. # host-cmulti -> executable
  83. quiet_cmd_host-cmulti = HOSTLD $@
  84. cmd_host-cmulti = $(HOSTCC) $(KBUILD_HOSTLDFLAGS) -o $@ \
  85. $(addprefix $(obj)/,$($(@F)-objs)) \
  86. $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(@F))
  87. $(host-cmulti): FORCE
  88. $(call if_changed,host-cmulti)
  89. $(call multi_depend, $(host-cmulti), , -objs)
  90. # Create .o file from a single .c file
  91. # host-cobjs -> .o
  92. quiet_cmd_host-cobjs = HOSTCC $@
  93. cmd_host-cobjs = $(HOSTCC) $(hostc_flags) -c -o $@ $<
  94. $(host-cobjs): $(obj)/%.o: $(src)/%.c FORCE
  95. $(call if_changed_dep,host-cobjs)
  96. # Link an executable based on list of .o files, a mixture of .c and .cc
  97. # host-cxxmulti -> executable
  98. quiet_cmd_host-cxxmulti = HOSTLD $@
  99. cmd_host-cxxmulti = $(HOSTCXX) $(KBUILD_HOSTLDFLAGS) -o $@ \
  100. $(foreach o,objs cxxobjs,\
  101. $(addprefix $(obj)/,$($(@F)-$(o)))) \
  102. $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(@F))
  103. $(host-cxxmulti): FORCE
  104. $(call if_changed,host-cxxmulti)
  105. $(call multi_depend, $(host-cxxmulti), , -objs -cxxobjs)
  106. # Create .o file from a single .cc (C++) file
  107. quiet_cmd_host-cxxobjs = HOSTCXX $@
  108. cmd_host-cxxobjs = $(HOSTCXX) $(hostcxx_flags) -c -o $@ $<
  109. $(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE
  110. $(call if_changed_dep,host-cxxobjs)
  111. # Compile .c file, create position independent .o file
  112. # host-cshobjs -> .o
  113. quiet_cmd_host-cshobjs = HOSTCC -fPIC $@
  114. cmd_host-cshobjs = $(HOSTCC) $(hostc_flags) -fPIC -c -o $@ $<
  115. $(host-cshobjs): $(obj)/%.o: $(src)/%.c FORCE
  116. $(call if_changed_dep,host-cshobjs)
  117. # Compile .c file, create position independent .o file
  118. # Note that plugin capable gcc versions can be either C or C++ based
  119. # therefore plugin source files have to be compilable in both C and C++ mode.
  120. # This is why a C++ compiler is invoked on a .c file.
  121. # host-cxxshobjs -> .o
  122. quiet_cmd_host-cxxshobjs = HOSTCXX -fPIC $@
  123. cmd_host-cxxshobjs = $(HOSTCXX) $(hostcxx_flags) -fPIC -c -o $@ $<
  124. $(host-cxxshobjs): $(obj)/%.o: $(src)/%.c FORCE
  125. $(call if_changed_dep,host-cxxshobjs)
  126. # Link a shared library, based on position independent .o files
  127. # *.o -> .so shared library (host-cshlib)
  128. quiet_cmd_host-cshlib = HOSTLLD -shared $@
  129. cmd_host-cshlib = $(HOSTCC) $(HOSTLDFLAGS) -shared -o $@ \
  130. $(addprefix $(obj)/,$($(@F:.so=-objs))) \
  131. $(HOST_LOADLIBES) $(HOSTLDLIBS_$(@F))
  132. $(host-cshlib): FORCE
  133. $(call if_changed,host-cshlib)
  134. $(call multi_depend, $(host-cshlib), .so, -objs)
  135. # Link a shared library, based on position independent .o files
  136. # *.o -> .so shared library (host-cxxshlib)
  137. quiet_cmd_host-cxxshlib = HOSTLLD -shared $@
  138. cmd_host-cxxshlib = $(HOSTCXX) $(HOSTLDFLAGS) -shared -o $@ \
  139. $(addprefix $(obj)/,$($(@F:.so=-objs))) \
  140. $(HOST_LOADLIBES) $(HOSTLDLIBS_$(@F))
  141. $(host-cxxshlib): FORCE
  142. $(call if_changed,host-cxxshlib)
  143. $(call multi_depend, $(host-cxxshlib), .so, -objs)
  144. targets += $(host-csingle) $(host-cmulti) $(host-cobjs)\
  145. $(host-cxxmulti) $(host-cxxobjs) $(host-shared) \
  146. $(host-cshlib) $(host-cshobjs) $(host-cxxshlib) $(host-cxxshobjs)