Makefile.host 6.7 KB

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