Skip to content

Commit

Permalink
layers/meta-opentrons: support pydantic v2 (#185)
Browse files Browse the repository at this point in the history
Add a bunch of rust deps from upstream meta-openembedded and openembedded-core so that we support
- rust, generally
- maturin, generally
- pydantic and deps, specifically

We're going to have to remove most of this when we update m-o and o-c.
  • Loading branch information
sfoster1 authored Dec 18, 2024
1 parent c0832a0 commit 26a877b
Show file tree
Hide file tree
Showing 84 changed files with 11,554 additions and 4 deletions.
81 changes: 81 additions & 0 deletions layers/meta-opentrons/classes/cargo-update-recipe-crates.bbclass
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#
# Copyright OpenEmbedded Contributors
#
# SPDX-License-Identifier: MIT
#

##
## Purpose:
## This class is used to update the list of crates in SRC_URI
## by reading Cargo.lock in the source tree.
##
## See meta/recipes-devtools/python/python3-bcrypt_*.bb for an example
##
## To perform the update: bitbake -c update_crates recipe-name

addtask do_update_crates after do_patch
do_update_crates[depends] = "python3-native:do_populate_sysroot"
do_update_crates[nostamp] = "1"
do_update_crates[doc] = "Update the recipe by reading Cargo.lock and write in ${THISDIR}/${BPN}-crates.inc"

RECIPE_UPDATE_EXTRA_TASKS += "do_update_crates"

# The directory where to search for Cargo.lock files
CARGO_LOCK_SRC_DIR ??= "${S}"

do_update_crates() {
TARGET_FILE="${THISDIR}/${BPN}-crates.inc"

nativepython3 - <<EOF

def get_crates(f):
import tomllib
c_list = '# from %s' % os.path.relpath(f, '${CARGO_LOCK_SRC_DIR}')
c_list += '\nSRC_URI += " \\\'
crates = tomllib.load(open(f, 'rb'))
# Build a list with crates info that have crates.io in the source
crates_candidates = list(filter(lambda c: 'crates.io' in c.get('source', ''), crates['package']))
if not crates_candidates:
raise ValueError("Unable to find any candidate crates that use crates.io")
# Update crates uri and their checksum, to avoid name clashing on the checksum
# we need to rename crates with name and version to have a unique key
cksum_list = ''
for c in crates_candidates:
rename = "%s-%s" % (c['name'], c['version'])
c_list += '\n crate://crates.io/%s/%s \\\' % (c['name'], c['version'])
if 'checksum' in c:
cksum_list += '\nSRC_URI[%s.sha256sum] = "%s"' % (rename, c['checksum'])

c_list += '\n"\n'
c_list += cksum_list
c_list += '\n'
return c_list

import os
crates = "# Autogenerated with 'bitbake -c update_crates ${PN}'\n\n"
found = False
for root, dirs, files in os.walk('${CARGO_LOCK_SRC_DIR}'):
# ignore git and patches directories
if root.startswith(os.path.join('${CARGO_LOCK_SRC_DIR}', '.pc')):
continue
if root.startswith(os.path.join('${CARGO_LOCK_SRC_DIR}', '.git')):
continue
for file in files:
if file == 'Cargo.lock':
try:
cargo_lock_path = os.path.join(root, file)
crates += get_crates(os.path.join(root, file))
except Exception as e:
raise ValueError("Cannot parse '%s'" % cargo_lock_path) from e
else:
found = True
if not found:
raise ValueError("Unable to find any Cargo.lock in ${CARGO_LOCK_SRC_DIR}")
open("${TARGET_FILE}", 'w').write(crates)
EOF

bbnote "Successfully update crates inside '${TARGET_FILE}'"
}
93 changes: 93 additions & 0 deletions layers/meta-opentrons/classes/cargo.bbclass
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#
# Copyright OpenEmbedded Contributors
#
# SPDX-License-Identifier: MIT
#

##
## Purpose:
## This class is used by any recipes that are built using
## Cargo.

inherit cargo_common
inherit rust-target-config

# the binary we will use
CARGO = "cargo"

# We need cargo to compile for the target
BASEDEPENDS:append = " cargo-native"

# Ensure we get the right rust variant
DEPENDS:append:class-target = " rust-native ${RUSTLIB_DEP}"
DEPENDS:append:class-nativesdk = " rust-native ${RUSTLIB_DEP}"
DEPENDS:append:class-native = " rust-native"

# Enable build separation
B = "${WORKDIR}/build"

# In case something fails in the build process, give a bit more feedback on
# where the issue occured
export RUST_BACKTRACE = "1"

RUSTFLAGS ??= ""
BUILD_MODE = "${@['--release', ''][d.getVar('DEBUG_BUILD') == '1']}"
# --frozen flag will prevent network access (which is required since only
# the do_fetch step is authorized to access network)
# and will require an up to date Cargo.lock file.
# This force the package being built to already ship a Cargo.lock, in the end
# this is what we want, at least, for reproducibility of the build.
CARGO_BUILD_FLAGS = "-v --frozen --target ${RUST_HOST_SYS} ${BUILD_MODE} --manifest-path=${CARGO_MANIFEST_PATH}"

# This is based on the content of CARGO_BUILD_FLAGS and generally will need to
# change if CARGO_BUILD_FLAGS changes.
BUILD_DIR = "${@['release', 'debug'][d.getVar('DEBUG_BUILD') == '1']}"
CARGO_TARGET_SUBDIR="${RUST_HOST_SYS}/${BUILD_DIR}"
oe_cargo_build () {
export RUSTFLAGS="${RUSTFLAGS}"
bbnote "Using rust targets from ${RUST_TARGET_PATH}"
bbnote "cargo = $(which ${CARGO})"
bbnote "${CARGO} build ${CARGO_BUILD_FLAGS} $@"
"${CARGO}" build ${CARGO_BUILD_FLAGS} "$@"
}

do_compile[progress] = "outof:\s+(\d+)/(\d+)"
cargo_do_compile () {
oe_cargo_build
}

cargo_do_install () {
local have_installed=false
for tgt in "${B}/target/${CARGO_TARGET_SUBDIR}/"*; do
case $tgt in
*.so|*.rlib)
install -d "${D}${rustlibdir}"
install -m755 "$tgt" "${D}${rustlibdir}"
have_installed=true
;;
*examples)
if [ -d "$tgt" ]; then
for example in "$tgt/"*; do
if [ -f "$example" ] && [ -x "$example" ]; then
install -d "${D}${bindir}"
install -m755 "$example" "${D}${bindir}"
have_installed=true
fi
done
fi
;;
*)
if [ -f "$tgt" ] && [ -x "$tgt" ]; then
install -d "${D}${bindir}"
install -m755 "$tgt" "${D}${bindir}"
have_installed=true
fi
;;
esac
done
if ! $have_installed; then
die "Did not find anything to install"
fi
}

EXPORT_FUNCTIONS do_compile do_install
41 changes: 41 additions & 0 deletions layers/meta-opentrons/classes/cargo_c.bbclass
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# Copyright OpenEmbedded Contributors
#
# SPDX-License-Identifier: MIT
#

##
## Purpose:
## This class is used by any recipes that want to compile a C ABI compatible
## library with header and pkg config file

inherit cargo pkgconfig

# the binaries we will use
CARGO_C_BUILD = "cargo-cbuild"
CARGO_C_INSTALL = "cargo-cinstall"

# We need cargo-c to compile for the target
BASEDEPENDS:append = " cargo-c-native"

do_compile[progress] = "outof:\s+(\d+)/(\d+)"
cargo_c_do_compile() {
oe_cargo_fix_env
export RUSTFLAGS="${RUSTFLAGS}"
bbnote "Using rust targets from ${RUST_TARGET_PATH}"
bbnote "cargo-cbuild = $(which ${CARGO_C_BUILD})"
bbnote "${CARGO_C_BUILD} cbuild ${CARGO_BUILD_FLAGS}"
"${CARGO_C_BUILD}" cbuild ${CARGO_BUILD_FLAGS}
}

cargo_c_do_install() {
oe_cargo_fix_env
export RUSTFLAGS="${RUSTFLAGS}"
bbnote "cargo-cinstall = $(which ${CARGO_C_INSTALL})"
"${CARGO_C_INSTALL}" cinstall ${CARGO_BUILD_FLAGS} \
--destdir ${D} \
--prefix ${prefix} \
--library-type cdylib
}

EXPORT_FUNCTIONS do_compile do_install
Loading

0 comments on commit 26a877b

Please sign in to comment.