diff mbox series

[RFC,17/30] classes: add vendor class for cargo

Message ID 20250211150034.18696-17-stefan.herbrechtsmeier-oss@weidmueller.com
State New
Headers show
Series Add vendor support for go, npm and rust | expand

Commit Message

Stefan Herbrechtsmeier Feb. 11, 2025, 3 p.m. UTC
From: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>

Add a vendor class for cargo to resolve the dependency SRC_URIs from a
Cargo.lock file and populate the crate vendor folder.

Signed-off-by: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
---

 meta/classes-recipe/vendor_cargo.bbclass | 46 ++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
 create mode 100644 meta/classes-recipe/vendor_cargo.bbclass
diff mbox series

Patch

diff --git a/meta/classes-recipe/vendor_cargo.bbclass b/meta/classes-recipe/vendor_cargo.bbclass
new file mode 100644
index 0000000000..495f7e01ce
--- /dev/null
+++ b/meta/classes-recipe/vendor_cargo.bbclass
@@ -0,0 +1,46 @@ 
+# Copyright (C) 2025 Weidmueller Interface GmbH & Co. KG
+# Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
+#
+# SPDX-License-Identifier: MIT
+#
+
+# The name of the vendor directory
+CARGO_VENDOR_SUBDIR = "vendor"
+
+# The URL of the crate registry
+CARGO_REGISTRY ?= "https://crates.io"
+
+CARGO_SRC_PATH = "${S}/${CARGO_SRC_DIR}"
+CARGO_SRC_SUBDIR = "${@os.path.relpath(d.getVar('CARGO_SRC_PATH'), d.getVar('WORKDIR'))}"
+CARGO_SRC_URI_FILE = "${VENDOR_DIR}/cargo-source-uris.txt"
+SRC_URI_FILES:append = " ${CARGO_SRC_URI_FILE}"
+
+inherit vendor
+
+CARGO_VENDORING_DIRECTORY = "${CARGO_SRC_PATH}/${CARGO_VENDOR_SUBDIR}"
+
+python vendor_cargo_do_vendor_resolve() {
+    from oe.vendor import cargo
+
+    lock_file_dir = d.getVar("CARGO_LOCK_PATH")
+    lock_file_dir = get_early_source_dir(d, lock_file_dir)
+    registry = d.getVar("CARGO_REGISTRY")
+    src_subdir = d.getVar("CARGO_SRC_SUBDIR")
+    vendor_subdir = d.getVar("CARGO_VENDOR_SUBDIR")
+    uris = cargo.resolve_src_uris(lock_file_dir, registry, src_subdir,
+                                  vendor_subdir)
+    with open(d.getVar("CARGO_SRC_URI_FILE"), "w") as f:
+        oe.vendor.dump(f, uris)
+}
+
+python emulate_crate_vendor() {
+    from oe.vendor import cargo
+
+    lock_file_dir = d.getVar("CARGO_LOCK_PATH")
+    lock_file_dir = get_early_source_dir(d, lock_file_dir)
+    vendor_dir = d.getVar("CARGO_VENDORING_DIRECTORY")
+    cargo.populate_vendor(lock_file_dir, vendor_dir)
+}
+do_unpack[postfuncs] += "emulate_crate_vendor"
+
+EXPORT_FUNCTIONS do_vendor_resolve