Makefile.host 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. # Rust is also supported, but it may only be used in scenarios where a Rust
  21. # toolchain is required to be available (e.g. when `CONFIG_RUST` is enabled).
  22. #
  23. # Sample syntax (see Documentation/kbuild/makefiles.rst for reference)
  24. # hostprogs := bin2hex
  25. # Will compile bin2hex.c and create an executable named bin2hex
  26. #
  27. # hostprogs := lxdialog
  28. # lxdialog-objs := checklist.o lxdialog.o
  29. # Will compile lxdialog.c and checklist.c, and then link the executable
  30. # lxdialog, based on checklist.o and lxdialog.o
  31. #
  32. # hostprogs := qconf
  33. # qconf-cxxobjs := qconf.o
  34. # qconf-objs := menu.o
  35. # Will compile qconf as a C++ program, and menu as a C program.
  36. # They are linked as C++ code to the executable qconf
  37. #
  38. # hostprogs := target
  39. # target-rust := y
  40. # Will compile `target` as a Rust program, using `target.rs` as the crate root.
  41. # The crate may consist of several source files.
  42. # C code
  43. # Executables compiled from a single .c file
  44. host-csingle := $(foreach m,$(hostprogs), \
  45. $(if $($(m)-objs)$($(m)-cxxobjs)$($(m)-rust),,$(m)))
  46. # C executables linked based on several .o files
  47. host-cmulti := $(foreach m,$(hostprogs),\
  48. $(if $($(m)-cxxobjs)$($(m)-rust),,$(if $($(m)-objs),$(m))))
  49. # Object (.o) files compiled from .c files
  50. host-cobjs := $(sort $(foreach m,$(hostprogs),$($(m)-objs)))
  51. # C++ code
  52. # C++ executables compiled from at least one .cc file
  53. # and zero or more .c files
  54. host-cxxmulti := $(foreach m,$(hostprogs),$(if $($(m)-cxxobjs),$(m)))
  55. # C++ Object (.o) files compiled from .cc files
  56. host-cxxobjs := $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs)))
  57. # Rust code
  58. # Executables compiled from a single Rust crate (which may consist of
  59. # one or more .rs files)
  60. host-rust := $(foreach m,$(hostprogs),$(if $($(m)-rust),$(m)))
  61. host-csingle := $(addprefix $(obj)/,$(host-csingle))
  62. host-cmulti := $(addprefix $(obj)/,$(host-cmulti))
  63. host-cobjs := $(addprefix $(obj)/,$(host-cobjs))
  64. host-cxxmulti := $(addprefix $(obj)/,$(host-cxxmulti))
  65. host-cxxobjs := $(addprefix $(obj)/,$(host-cxxobjs))
  66. host-rust := $(addprefix $(obj)/,$(host-rust))
  67. #####
  68. # Handle options to gcc. Support building with separate output directory
  69. hostc_flags = -Wp,-MMD,$(depfile) \
  70. $(KBUILD_HOSTCFLAGS) $(HOST_EXTRACFLAGS) \
  71. $(HOSTCFLAGS_$(target-stem).o)
  72. hostcxx_flags = -Wp,-MMD,$(depfile) \
  73. $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
  74. $(HOSTCXXFLAGS_$(target-stem).o)
  75. # `--out-dir` is required to avoid temporaries being created by `rustc` in the
  76. # current working directory, which may be not accessible in the out-of-tree
  77. # modules case.
  78. hostrust_flags = --out-dir $(dir $@) --emit=dep-info=$(depfile) \
  79. -Clinker-flavor=gcc -Clinker=$(HOSTCC) \
  80. -Clink-args='$(call escsq,$(KBUILD_HOSTLDFLAGS))' \
  81. $(KBUILD_HOSTRUSTFLAGS) $(HOST_EXTRARUSTFLAGS) \
  82. $(HOSTRUSTFLAGS_$(target-stem))
  83. # $(obj) for including generated headers from checkin source files
  84. ifdef building_out_of_srctree
  85. hostc_flags += -I $(obj)
  86. hostcxx_flags += -I $(obj)
  87. endif
  88. #####
  89. # Compile programs on the host
  90. # Create executable from a single .c file
  91. # host-csingle -> Executable
  92. quiet_cmd_host-csingle = HOSTCC $@
  93. cmd_host-csingle = $(HOSTCC) $(hostc_flags) $(KBUILD_HOSTLDFLAGS) -o $@ $< \
  94. $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
  95. $(host-csingle): $(obj)/%: $(obj)/%.c FORCE
  96. $(call if_changed_dep,host-csingle)
  97. # Link an executable based on list of .o files, all plain c
  98. # host-cmulti -> executable
  99. quiet_cmd_host-cmulti = HOSTLD $@
  100. cmd_host-cmulti = $(HOSTCC) $(KBUILD_HOSTLDFLAGS) -o $@ \
  101. $(addprefix $(obj)/, $($(target-stem)-objs)) \
  102. $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
  103. $(host-cmulti): FORCE
  104. $(call if_changed,host-cmulti)
  105. $(call multi_depend, $(host-cmulti), , -objs)
  106. # Create .o file from a single .c file
  107. # host-cobjs -> .o
  108. quiet_cmd_host-cobjs = HOSTCC $@
  109. cmd_host-cobjs = $(HOSTCC) $(hostc_flags) -c -o $@ $<
  110. $(host-cobjs): $(obj)/%.o: $(obj)/%.c FORCE
  111. $(call if_changed_dep,host-cobjs)
  112. # Link an executable based on list of .o files, a mixture of .c and .cc
  113. # host-cxxmulti -> executable
  114. quiet_cmd_host-cxxmulti = HOSTLD $@
  115. cmd_host-cxxmulti = $(HOSTCXX) $(KBUILD_HOSTLDFLAGS) -o $@ \
  116. $(foreach o,objs cxxobjs,\
  117. $(addprefix $(obj)/, $($(target-stem)-$(o)))) \
  118. $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
  119. $(host-cxxmulti): FORCE
  120. $(call if_changed,host-cxxmulti)
  121. $(call multi_depend, $(host-cxxmulti), , -objs -cxxobjs)
  122. # Create .o file from a single .cc (C++) file
  123. quiet_cmd_host-cxxobjs = HOSTCXX $@
  124. cmd_host-cxxobjs = $(HOSTCXX) $(hostcxx_flags) -c -o $@ $<
  125. $(host-cxxobjs): $(obj)/%.o: $(obj)/%.cc FORCE
  126. $(call if_changed_dep,host-cxxobjs)
  127. # Create executable from a single Rust crate (which may consist of
  128. # one or more `.rs` files)
  129. # host-rust -> Executable
  130. quiet_cmd_host-rust = HOSTRUSTC $@
  131. cmd_host-rust = \
  132. $(HOSTRUSTC) $(hostrust_flags) --emit=link=$@ $<
  133. $(host-rust): $(obj)/%: $(src)/%.rs FORCE
  134. +$(call if_changed_dep,host-rust)
  135. targets += $(host-csingle) $(host-cmulti) $(host-cobjs) \
  136. $(host-cxxmulti) $(host-cxxobjs) $(host-rust)
  137. # %.lex.o <- %.lex.c <- %.l
  138. # %.tab.o <- %.tab.[ch] <- %.y
  139. targets += $(call intermediate_targets, .lex.o, .lex.c) \
  140. $(call intermediate_targets, .tab.o, .tab.c .tab.h)