2
1

pkg-cargo.mk 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. ################################################################################
  2. # Cargo package infrastructure
  3. #
  4. # This file implements an infrastructure that eases development of package
  5. # .mk files for Cargo packages. It should be used for all packages that use
  6. # Cargo as their build system.
  7. #
  8. # See the Buildroot documentation for details on the usage of this
  9. # infrastructure
  10. #
  11. # In terms of implementation, this Cargo infrastructure requires the .mk file
  12. # to only specify metadata information about the package: name, version,
  13. # download URL, etc.
  14. #
  15. # We still allow the package .mk file to override what the different steps
  16. # are doing, if needed. For example, if <PKG>_BUILD_CMDS is already defined,
  17. # it is used as the list of commands to perform to build the package,
  18. # instead of the default Cargo behaviour. The package can also define some
  19. # post operation hooks.
  20. #
  21. ################################################################################
  22. PKG_COMMON_CARGO_ENV = \
  23. CARGO_HOME=$(HOST_DIR)/share/cargo
  24. # __CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS is needed to allow
  25. # passing the -Z target-applies-to-host, which is needed together with
  26. # CARGO_TARGET_APPLIES_TO_HOST to fix build problems when target
  27. # architecture == host architecture.
  28. # __CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS="nightly" is to allow
  29. # using nighly features on stable releases, i.e features that are not
  30. # yet considered stable.
  31. #
  32. # CARGO_UNSTABLE_TARGET_APPLIES_TO_HOST="true" "enables the nightly
  33. # configuration option target-applies-to-host value to be set
  34. #
  35. # CARGO_TARGET_APPLIES_TO_HOST="false" is actually setting the value
  36. # for this feature, which we disable, to make sure builds where target
  37. # arch == host arch work correctly
  38. PKG_CARGO_ENV = \
  39. $(PKG_COMMON_CARGO_ENV) \
  40. __CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS="nightly" \
  41. CARGO_UNSTABLE_TARGET_APPLIES_TO_HOST="true" \
  42. CARGO_TARGET_APPLIES_TO_HOST="false" \
  43. CARGO_BUILD_TARGET="$(RUSTC_TARGET_NAME)" \
  44. CARGO_TARGET_$(call UPPERCASE,$(RUSTC_TARGET_NAME))_LINKER=$(notdir $(TARGET_CROSS))gcc
  45. HOST_PKG_CARGO_ENV = \
  46. $(PKG_COMMON_CARGO_ENV)
  47. ################################################################################
  48. # inner-cargo-package -- defines how the configuration, compilation and
  49. # installation of a cargo package should be done, implements a few hooks
  50. # to tune the build process for cargo specifities and calls the generic
  51. # package infrastructure to generate the necessary make targets
  52. #
  53. # argument 1 is the lowercase package name
  54. # argument 2 is the uppercase package name, including a HOST_ prefix
  55. # for host packages
  56. # argument 3 is the uppercase package name, without the HOST_ prefix
  57. # for host packages
  58. # argument 4 is the type (target or host)
  59. ################################################################################
  60. define inner-cargo-package
  61. # We need host-rustc to run cargo
  62. $(2)_DOWNLOAD_DEPENDENCIES += host-rustc
  63. $(2)_DOWNLOAD_POST_PROCESS = cargo
  64. $(2)_DL_ENV = CARGO_HOME=$$(HOST_DIR)/share/cargo
  65. # Due to vendoring, it is pretty likely that not all licenses are
  66. # listed in <pkg>_LICENSE.
  67. $(2)_LICENSE += , vendored dependencies licenses probably not listed
  68. # Note: in all the steps below, we "cd" into the build directory to
  69. # execute the "cargo" tool instead of passing $(@D)/Cargo.toml as the
  70. # manifest-path. Indeed while the latter seems to work, it in fact
  71. # breaks in subtle ways as the way cargo searches for its
  72. # configuration file is based (among other rules) on the current
  73. # directory. This means that if cargo is started outside of a package
  74. # directory, its configuration file will not be taken into account.
  75. #
  76. # Also, we pass:
  77. # * --offline to prevent cargo from downloading anything: all
  78. # dependencies should have been built by the download post
  79. # process logic
  80. # * --locked to force cargo to use the Cargo.lock file, which ensures
  81. # that a fixed set of dependency versions is used
  82. #
  83. # Build step. Only define it if not already defined by the package .mk
  84. # file.
  85. #
  86. ifndef $(2)_BUILD_CMDS
  87. ifeq ($(4),target)
  88. define $(2)_BUILD_CMDS
  89. cd $$(@D) && \
  90. $$(TARGET_MAKE_ENV) \
  91. $$(TARGET_CONFIGURE_OPTS) \
  92. $$(PKG_CARGO_ENV) \
  93. $$($(2)_CARGO_ENV) \
  94. cargo build \
  95. --offline \
  96. $$(if $$(BR2_ENABLE_DEBUG),,--release) \
  97. --manifest-path Cargo.toml \
  98. --locked \
  99. $$($(2)_CARGO_BUILD_OPTS)
  100. endef
  101. else # ifeq ($(4),target)
  102. define $(2)_BUILD_CMDS
  103. cd $$(@D) && \
  104. $$(HOST_MAKE_ENV) \
  105. RUSTFLAGS="$$(addprefix -C link-args=,$$(HOST_LDFLAGS))" \
  106. $$(HOST_CONFIGURE_OPTS) \
  107. $$(HOST_PKG_CARGO_ENV) \
  108. $$($(2)_CARGO_ENV) \
  109. cargo build \
  110. --offline \
  111. --release \
  112. --manifest-path Cargo.toml \
  113. --locked \
  114. $$($(2)_CARGO_BUILD_OPTS)
  115. endef
  116. endif # ifeq ($(4),target)
  117. endif # ifndef $(2)_BUILD_CMDS
  118. #
  119. # Target installation step. Only define it if not already defined by
  120. # the package .mk file.
  121. #
  122. ifndef $(2)_INSTALL_TARGET_CMDS
  123. define $(2)_INSTALL_TARGET_CMDS
  124. cd $$(@D) && \
  125. $$(TARGET_MAKE_ENV) \
  126. $$(TARGET_CONFIGURE_OPTS) \
  127. $$(PKG_CARGO_ENV) \
  128. $$($(2)_CARGO_ENV) \
  129. cargo install \
  130. --offline \
  131. --root $$(TARGET_DIR)/usr/ \
  132. --bins \
  133. --path ./ \
  134. --force \
  135. --locked \
  136. -Z target-applies-to-host \
  137. $$($(2)_CARGO_INSTALL_OPTS)
  138. endef
  139. endif
  140. ifndef $(2)_INSTALL_CMDS
  141. define $(2)_INSTALL_CMDS
  142. cd $$(@D) && \
  143. $$(HOST_MAKE_ENV) \
  144. RUSTFLAGS="$$(addprefix -C link-args=,$$(HOST_LDFLAGS))" \
  145. $$(HOST_CONFIGURE_OPTS) \
  146. $$(HOST_PKG_CARGO_ENV) \
  147. $$($(2)_CARGO_ENV) \
  148. cargo install \
  149. --offline \
  150. --root $$(HOST_DIR) \
  151. --bins \
  152. --path ./ \
  153. --force \
  154. --locked \
  155. $$($(2)_CARGO_INSTALL_OPTS)
  156. endef
  157. endif
  158. # Call the generic package infrastructure to generate the necessary
  159. # make targets
  160. $(call inner-generic-package,$(1),$(2),$(3),$(4))
  161. endef
  162. ################################################################################
  163. # cargo-package -- the target generator macro for Cargo packages
  164. ################################################################################
  165. cargo-package = $(call inner-cargo-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
  166. host-cargo-package = $(call inner-cargo-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)