Makefile.host 6.3 KB

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