cargo_common.bbclass 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. # SPDX-License-Identifier: MIT
  5. #
  6. ##
  7. ## Purpose:
  8. ## This class is to support building with cargo. It
  9. ## must be different than cargo.bbclass because Rust
  10. ## now builds with Cargo but cannot use cargo.bbclass
  11. ## due to dependencies and assumptions in cargo.bbclass
  12. ## that Rust & Cargo are already installed. So this
  13. ## is used by cargo.bbclass and Rust
  14. ##
  15. # add crate fetch support
  16. inherit rust-common
  17. # Where we download our registry and dependencies to
  18. export CARGO_HOME = "${UNPACKDIR}/cargo_home"
  19. # The pkg-config-rs library used by cargo build scripts disables itself when
  20. # cross compiling unless this is defined. We set up pkg-config appropriately
  21. # for cross compilation, so tell it we know better than it.
  22. export PKG_CONFIG_ALLOW_CROSS = "1"
  23. # Don't instruct cargo to use crates downloaded by bitbake. Some rust packages,
  24. # for example the rust compiler itself, come with their own vendored sources.
  25. # Specifying two [source.crates-io] will not work.
  26. CARGO_DISABLE_BITBAKE_VENDORING ??= "0"
  27. # Used by libstd-rs to point to the vendor dir included in rustc src
  28. CARGO_VENDORING_DIRECTORY ??= "${CARGO_HOME}/bitbake"
  29. # The directory of the Cargo.toml relative to the root directory, per default
  30. # assume there's a Cargo.toml directly in the root directory
  31. CARGO_SRC_DIR ??= ""
  32. # The actual path to the Cargo.toml
  33. CARGO_MANIFEST_PATH ??= "${S}/${CARGO_SRC_DIR}/Cargo.toml"
  34. # Path to Cargo.lock
  35. CARGO_LOCK_PATH ??= "${@ os.path.join(os.path.dirname(d.getVar('CARGO_MANIFEST_PATH', True)), 'Cargo.lock')}"
  36. CARGO_RUST_TARGET_CCLD ??= "${RUST_TARGET_CCLD}"
  37. cargo_common_do_configure () {
  38. mkdir -p ${CARGO_HOME}/bitbake
  39. cat <<- EOF > ${CARGO_HOME}/config
  40. # EXTRA_OECARGO_PATHS
  41. paths = [
  42. $(for p in ${EXTRA_OECARGO_PATHS}; do echo \"$p\",; done)
  43. ]
  44. EOF
  45. cat <<- EOF >> ${CARGO_HOME}/config
  46. # Local mirror vendored by bitbake
  47. [source.bitbake]
  48. directory = "${CARGO_VENDORING_DIRECTORY}"
  49. EOF
  50. if [ ${CARGO_DISABLE_BITBAKE_VENDORING} = "0" ]; then
  51. cat <<- EOF >> ${CARGO_HOME}/config
  52. [source.crates-io]
  53. replace-with = "bitbake"
  54. local-registry = "/nonexistent"
  55. EOF
  56. fi
  57. cat <<- EOF >> ${CARGO_HOME}/config
  58. [http]
  59. # Multiplexing can't be enabled because http2 can't be enabled
  60. # in curl-native without dependency loops
  61. multiplexing = false
  62. # Ignore the hard coded and incorrect path to certificates
  63. cainfo = "${STAGING_ETCDIR_NATIVE}/ssl/certs/ca-certificates.crt"
  64. EOF
  65. cat <<- EOF >> ${CARGO_HOME}/config
  66. # HOST_SYS
  67. [target.${RUST_HOST_SYS}]
  68. linker = "${CARGO_RUST_TARGET_CCLD}"
  69. EOF
  70. if [ "${RUST_HOST_SYS}" != "${RUST_BUILD_SYS}" ]; then
  71. cat <<- EOF >> ${CARGO_HOME}/config
  72. # BUILD_SYS
  73. [target.${RUST_BUILD_SYS}]
  74. linker = "${RUST_BUILD_CCLD}"
  75. EOF
  76. fi
  77. if [ "${RUST_TARGET_SYS}" != "${RUST_BUILD_SYS}" -a "${RUST_TARGET_SYS}" != "${RUST_HOST_SYS}" ]; then
  78. cat <<- EOF >> ${CARGO_HOME}/config
  79. # TARGET_SYS
  80. [target.${RUST_TARGET_SYS}]
  81. linker = "${RUST_TARGET_CCLD}"
  82. EOF
  83. fi
  84. # Put build output in build directory preferred by bitbake instead of
  85. # inside source directory unless they are the same
  86. if [ "${B}" != "${S}" ]; then
  87. cat <<- EOF >> ${CARGO_HOME}/config
  88. [build]
  89. # Use out of tree build destination to avoid polluting the source tree
  90. target-dir = "${B}/target"
  91. EOF
  92. fi
  93. cat <<- EOF >> ${CARGO_HOME}/config
  94. [term]
  95. progress.when = 'always'
  96. progress.width = 80
  97. EOF
  98. }
  99. python cargo_common_do_patch_paths() {
  100. import shutil
  101. cargo_config = os.path.join(d.getVar("CARGO_HOME"), "config")
  102. if not os.path.exists(cargo_config):
  103. return
  104. src_uri = (d.getVar('SRC_URI') or "").split()
  105. if len(src_uri) == 0:
  106. return
  107. patches = dict()
  108. workdir = d.getVar('UNPACKDIR')
  109. fetcher = bb.fetch2.Fetch(src_uri, d)
  110. for url in fetcher.urls:
  111. ud = fetcher.ud[url]
  112. if ud.type == 'git':
  113. name = ud.parm.get('name')
  114. destsuffix = ud.parm.get('destsuffix')
  115. if name is not None and destsuffix is not None:
  116. if ud.user:
  117. repo = '%s://%s@%s%s' % (ud.proto, ud.user, ud.host, ud.path)
  118. else:
  119. repo = '%s://%s%s' % (ud.proto, ud.host, ud.path)
  120. path = '%s = { path = "%s" }' % (name, os.path.join(workdir, destsuffix))
  121. patches.setdefault(repo, []).append(path)
  122. with open(cargo_config, "a+") as config:
  123. for k, v in patches.items():
  124. print('\n[patch."%s"]' % k, file=config)
  125. for name in v:
  126. print(name, file=config)
  127. if not patches:
  128. return
  129. # Cargo.lock file is needed for to be sure that artifacts
  130. # downloaded by the fetch steps are those expected by the
  131. # project and that the possible patches are correctly applied.
  132. # Moreover since we do not want any modification
  133. # of this file (for reproducibility purpose), we prevent it by
  134. # using --frozen flag (in CARGO_BUILD_FLAGS) and raise a clear error
  135. # here is better than letting cargo tell (in case the file is missing)
  136. # "Cargo.lock should be modified but --frozen was given"
  137. lockfile = d.getVar("CARGO_LOCK_PATH", True)
  138. if not os.path.exists(lockfile):
  139. bb.fatal(f"{lockfile} file doesn't exist")
  140. # There are patched files and so Cargo.lock should be modified but we use
  141. # --frozen so let's handle that modifications here.
  142. #
  143. # Note that a "better" (more elegant ?) would have been to use cargo update for
  144. # patched packages:
  145. # cargo update --offline -p package_1 -p package_2
  146. # But this is not possible since it requires that cargo local git db
  147. # to be populated and this is not the case as we fetch git repo ourself.
  148. lockfile_orig = lockfile + ".orig"
  149. if not os.path.exists(lockfile_orig):
  150. shutil.copy(lockfile, lockfile_orig)
  151. newlines = []
  152. with open(lockfile_orig, "r") as f:
  153. for line in f.readlines():
  154. if not line.startswith("source = \"git"):
  155. newlines.append(line)
  156. with open(lockfile, "w") as f:
  157. f.writelines(newlines)
  158. }
  159. do_configure[postfuncs] += "cargo_common_do_patch_paths"
  160. do_compile:prepend () {
  161. oe_cargo_fix_env
  162. }
  163. oe_cargo_fix_env () {
  164. export CC="${RUST_TARGET_CC}"
  165. export CXX="${RUST_TARGET_CXX}"
  166. export CFLAGS="${CFLAGS}"
  167. export CXXFLAGS="${CXXFLAGS}"
  168. export AR="${AR}"
  169. export TARGET_CC="${RUST_TARGET_CC}"
  170. export TARGET_CXX="${RUST_TARGET_CXX}"
  171. export TARGET_CFLAGS="${CFLAGS}"
  172. export TARGET_CXXFLAGS="${CXXFLAGS}"
  173. export TARGET_AR="${AR}"
  174. export HOST_CC="${RUST_BUILD_CC}"
  175. export HOST_CXX="${RUST_BUILD_CXX}"
  176. export HOST_CFLAGS="${BUILD_CFLAGS}"
  177. export HOST_CXXFLAGS="${BUILD_CXXFLAGS}"
  178. export HOST_AR="${BUILD_AR}"
  179. }
  180. EXTRA_OECARGO_PATHS ??= ""
  181. EXPORT_FUNCTIONS do_configure
  182. # The culprit for this setting is the libc crate,
  183. # which as of Jun 2023 calls directly into 32 bit time functions in glibc,
  184. # bypassing all of glibc provisions to choose the right Y2038-safe functions. As
  185. # rust components statically link with that crate, pretty much everything
  186. # is affected, and so there's no point trying to have recipe-specific
  187. # INSANE_SKIP entries.
  188. #
  189. # Upstream ticket and PR:
  190. # https://github.com/rust-lang/libc/issues/3223
  191. # https://github.com/rust-lang/libc/pull/3175
  192. INSANE_SKIP:append = " 32bit-time"