summaryrefslogtreecommitdiffstats
path: root/meta/classes-recipe/cargo_common.bbclass
diff options
context:
space:
mode:
Diffstat (limited to 'meta/classes-recipe/cargo_common.bbclass')
-rw-r--r--meta/classes-recipe/cargo_common.bbclass109
1 files changed, 104 insertions, 5 deletions
diff --git a/meta/classes-recipe/cargo_common.bbclass b/meta/classes-recipe/cargo_common.bbclass
index dea0fbe2f6..0fb443edbd 100644
--- a/meta/classes-recipe/cargo_common.bbclass
+++ b/meta/classes-recipe/cargo_common.bbclass
@@ -28,12 +28,22 @@ export PKG_CONFIG_ALLOW_CROSS = "1"
# Don't instruct cargo to use crates downloaded by bitbake. Some rust packages,
# for example the rust compiler itself, come with their own vendored sources.
# Specifying two [source.crates-io] will not work.
-CARGO_DISABLE_BITBAKE_VENDORING ?= "0"
+CARGO_DISABLE_BITBAKE_VENDORING ??= "0"
# Used by libstd-rs to point to the vendor dir included in rustc src
-CARGO_VENDORING_DIRECTORY ?= "${CARGO_HOME}/bitbake"
+CARGO_VENDORING_DIRECTORY ??= "${CARGO_HOME}/bitbake"
-CARGO_RUST_TARGET_CCLD ?= "${RUST_TARGET_CCLD}"
+# The directory of the Cargo.toml relative to the root directory, per default
+# assume there's a Cargo.toml directly in the root directory
+CARGO_SRC_DIR ??= ""
+
+# The actual path to the Cargo.toml
+CARGO_MANIFEST_PATH ??= "${S}/${CARGO_SRC_DIR}/Cargo.toml"
+
+# Path to Cargo.lock
+CARGO_LOCK_PATH ??= "${@ os.path.join(os.path.dirname(d.getVar('CARGO_MANIFEST_PATH', True)), 'Cargo.lock')}"
+
+CARGO_RUST_TARGET_CCLD ??= "${RUST_TARGET_CCLD}"
cargo_common_do_configure () {
mkdir -p ${CARGO_HOME}/bitbake
@@ -56,7 +66,7 @@ cargo_common_do_configure () {
[source.crates-io]
replace-with = "bitbake"
- local-registry = "/nonexistant"
+ local-registry = "/nonexistent"
EOF
fi
@@ -103,7 +113,7 @@ cargo_common_do_configure () {
cat <<- EOF >> ${CARGO_HOME}/config
[build]
- # Use out of tree build destination to avoid poluting the source tree
+ # Use out of tree build destination to avoid polluting the source tree
target-dir = "${B}/target"
EOF
fi
@@ -116,6 +126,83 @@ cargo_common_do_configure () {
EOF
}
+python cargo_common_do_patch_paths() {
+ import shutil
+
+ cargo_config = os.path.join(d.getVar("CARGO_HOME"), "config")
+ if not os.path.exists(cargo_config):
+ return
+
+ src_uri = (d.getVar('SRC_URI') or "").split()
+ if len(src_uri) == 0:
+ return
+
+ patches = dict()
+ workdir = d.getVar('WORKDIR')
+ fetcher = bb.fetch2.Fetch(src_uri, d)
+ for url in fetcher.urls:
+ ud = fetcher.ud[url]
+ if ud.type == 'git':
+ name = ud.parm.get('name')
+ destsuffix = ud.parm.get('destsuffix')
+ if name is not None and destsuffix is not None:
+ if ud.user:
+ repo = '%s://%s@%s%s' % (ud.proto, ud.user, ud.host, ud.path)
+ else:
+ repo = '%s://%s%s' % (ud.proto, ud.host, ud.path)
+ path = '%s = { path = "%s" }' % (name, os.path.join(workdir, destsuffix))
+ patches.setdefault(repo, []).append(path)
+
+ with open(cargo_config, "a+") as config:
+ for k, v in patches.items():
+ print('\n[patch."%s"]' % k, file=config)
+ for name in v:
+ print(name, file=config)
+
+ if not patches:
+ return
+
+ # Cargo.lock file is needed for to be sure that artifacts
+ # downloaded by the fetch steps are those expected by the
+ # project and that the possible patches are correctly applied.
+ # Moreover since we do not want any modification
+ # of this file (for reproducibility purpose), we prevent it by
+ # using --frozen flag (in CARGO_BUILD_FLAGS) and raise a clear error
+ # here is better than letting cargo tell (in case the file is missing)
+ # "Cargo.lock should be modified but --frozen was given"
+
+ lockfile = d.getVar("CARGO_LOCK_PATH", True)
+ if not os.path.exists(lockfile):
+ bb.fatal(f"{lockfile} file doesn't exist")
+
+ # There are patched files and so Cargo.lock should be modified but we use
+ # --frozen so let's handle that modifications here.
+ #
+ # Note that a "better" (more elegant ?) would have been to use cargo update for
+ # patched packages:
+ # cargo update --offline -p package_1 -p package_2
+ # But this is not possible since it requires that cargo local git db
+ # to be populated and this is not the case as we fetch git repo ourself.
+
+ lockfile_orig = lockfile + ".orig"
+ if not os.path.exists(lockfile_orig):
+ shutil.copy(lockfile, lockfile_orig)
+
+ newlines = []
+ with open(lockfile_orig, "r") as f:
+ for line in f.readlines():
+ if not line.startswith("source = \"git"):
+ newlines.append(line)
+
+ with open(lockfile, "w") as f:
+ f.writelines(newlines)
+}
+do_configure[postfuncs] += "cargo_common_do_patch_paths"
+
+do_compile:prepend () {
+ oe_cargo_fix_env
+}
+
oe_cargo_fix_env () {
export CC="${RUST_TARGET_CC}"
export CXX="${RUST_TARGET_CXX}"
@@ -137,3 +224,15 @@ oe_cargo_fix_env () {
EXTRA_OECARGO_PATHS ??= ""
EXPORT_FUNCTIONS do_configure
+
+# The culprit for this setting is the libc crate,
+# which as of Jun 2023 calls directly into 32 bit time functions in glibc,
+# bypassing all of glibc provisions to choose the right Y2038-safe functions. As
+# rust components statically link with that crate, pretty much everything
+# is affected, and so there's no point trying to have recipe-specific
+# INSANE_SKIP entries.
+#
+# Upstream ticket and PR:
+# https://github.com/rust-lang/libc/issues/3223
+# https://github.com/rust-lang/libc/pull/3175
+INSANE_SKIP:append = " 32bit-time"