Makefile.host 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # SPDX-License-Identifier: GPL-2.0
  2. # LEX
  3. # ---------------------------------------------------------------------------
  4. quiet_cmd_flex = LEX $@
  5. cmd_flex = $(LEX) -o$@ -L $<
  6. $(obj)/%.lex.c: $(src)/%.l FORCE
  7. $(call if_changed,flex)
  8. # YACC
  9. # ---------------------------------------------------------------------------
  10. quiet_cmd_bison = YACC $(basename $@).[ch]
  11. cmd_bison = $(YACC) -o $(basename $@).c --defines=$(basename $@).h -t -l $<
  12. $(obj)/%.tab.c $(obj)/%.tab.h: $(src)/%.y FORCE
  13. $(call if_changed,bison)
  14. # ==========================================================================
  15. # Building binaries on the host system
  16. # Binaries are used during the compilation of the kernel, for example
  17. # to preprocess a data file.
  18. #
  19. # Both C and C++ are supported, but preferred language is C for such utilities.
  20. #
  21. # Sample syntax (see Documentation/kbuild/makefiles.rst for reference)
  22. # hostprogs := bin2hex
  23. # Will compile bin2hex.c and create an executable named bin2hex
  24. #
  25. # hostprogs := lxdialog
  26. # lxdialog-objs := checklist.o lxdialog.o
  27. # Will compile lxdialog.c and checklist.c, and then link the executable
  28. # lxdialog, based on checklist.o and lxdialog.o
  29. #
  30. # hostprogs := qconf
  31. # qconf-cxxobjs := qconf.o
  32. # qconf-objs := menu.o
  33. # Will compile qconf as a C++ program, and menu as a C program.
  34. # They are linked as C++ code to the executable qconf
  35. # C code
  36. # Executables compiled from a single .c file
  37. host-csingle := $(foreach m,$(hostprogs), \
  38. $(if $($(m)-objs)$($(m)-cxxobjs),,$(m)))
  39. # C executables linked based on several .o files
  40. host-cmulti := $(foreach m,$(hostprogs),\
  41. $(if $($(m)-cxxobjs),,$(if $($(m)-objs),$(m))))
  42. # Object (.o) files compiled from .c files
  43. host-cobjs := $(sort $(foreach m,$(hostprogs),$($(m)-objs)))
  44. # C++ code
  45. # C++ executables compiled from at least one .cc file
  46. # and zero or more .c files
  47. host-cxxmulti := $(foreach m,$(hostprogs),$(if $($(m)-cxxobjs),$(m)))
  48. # C++ Object (.o) files compiled from .cc files
  49. host-cxxobjs := $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs)))
  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. #####
  56. # Handle options to gcc. Support building with separate output directory
  57. _hostc_flags = $(KBUILD_HOSTCFLAGS) $(HOST_EXTRACFLAGS) \
  58. $(HOSTCFLAGS_$(target-stem).o)
  59. _hostcxx_flags = $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
  60. $(HOSTCXXFLAGS_$(target-stem).o)
  61. # $(objtree)/$(obj) for including generated headers from checkin source files
  62. ifeq ($(KBUILD_EXTMOD),)
  63. ifdef building_out_of_srctree
  64. _hostc_flags += -I $(objtree)/$(obj)
  65. _hostcxx_flags += -I $(objtree)/$(obj)
  66. endif
  67. endif
  68. hostc_flags = -Wp,-MMD,$(depfile) $(_hostc_flags)
  69. hostcxx_flags = -Wp,-MMD,$(depfile) $(_hostcxx_flags)
  70. #####
  71. # Compile programs on the host
  72. # Create executable from a single .c file
  73. # host-csingle -> Executable
  74. quiet_cmd_host-csingle = HOSTCC $@
  75. cmd_host-csingle = $(HOSTCC) $(hostc_flags) $(KBUILD_HOSTLDFLAGS) -o $@ $< \
  76. $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
  77. $(host-csingle): $(obj)/%: $(src)/%.c FORCE
  78. $(call if_changed_dep,host-csingle)
  79. # Link an executable based on list of .o files, all plain c
  80. # host-cmulti -> executable
  81. quiet_cmd_host-cmulti = HOSTLD $@
  82. cmd_host-cmulti = $(HOSTCC) $(KBUILD_HOSTLDFLAGS) -o $@ \
  83. $(addprefix $(obj)/, $($(target-stem)-objs)) \
  84. $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
  85. $(host-cmulti): FORCE
  86. $(call if_changed,host-cmulti)
  87. $(call multi_depend, $(host-cmulti), , -objs)
  88. # Create .o file from a single .c file
  89. # host-cobjs -> .o
  90. quiet_cmd_host-cobjs = HOSTCC $@
  91. cmd_host-cobjs = $(HOSTCC) $(hostc_flags) -c -o $@ $<
  92. $(host-cobjs): $(obj)/%.o: $(src)/%.c FORCE
  93. $(call if_changed_dep,host-cobjs)
  94. # Link an executable based on list of .o files, a mixture of .c and .cc
  95. # host-cxxmulti -> executable
  96. quiet_cmd_host-cxxmulti = HOSTLD $@
  97. cmd_host-cxxmulti = $(HOSTCXX) $(KBUILD_HOSTLDFLAGS) -o $@ \
  98. $(foreach o,objs cxxobjs,\
  99. $(addprefix $(obj)/, $($(target-stem)-$(o)))) \
  100. $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
  101. $(host-cxxmulti): FORCE
  102. $(call if_changed,host-cxxmulti)
  103. $(call multi_depend, $(host-cxxmulti), , -objs -cxxobjs)
  104. # Create .o file from a single .cc (C++) file
  105. quiet_cmd_host-cxxobjs = HOSTCXX $@
  106. cmd_host-cxxobjs = $(HOSTCXX) $(hostcxx_flags) -c -o $@ $<
  107. $(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE
  108. $(call if_changed_dep,host-cxxobjs)
  109. targets += $(host-csingle) $(host-cmulti) $(host-cobjs) \
  110. $(host-cxxmulti) $(host-cxxobjs)