Skip to content

Commit

Permalink
ptxd_make_world_extract_cargo: improve workspace handling
Browse files Browse the repository at this point in the history
To handle all downloads manually, PTXdist basically fakes vendoring all
dependencies. This can cause problems with workspaces where packages
inherit from the workspace. 'cargo vendor' used to have the same
problem[1] and fixed it by rewriting the Cargo.toml.

Do the same thing here to fix this.

[1] rust-lang/cargo#11192

Signed-off-by: Michael Olbrich <m.olbrich@pengutronix.de>
  • Loading branch information
michaelolbrich committed Mar 11, 2024
1 parent 65f8db9 commit 51496ff
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
14 changes: 11 additions & 3 deletions scripts/lib/ptxd_make_world_extract.sh
Expand Up @@ -124,22 +124,30 @@ ptxd_make_world_extract_cargo_crate() {
name = gensub(/\//, "-", "g", dir)
print dir, name
}' "${pkg_cargo_home}/workspaces/${srcdir}/Cargo.toml" | while read dir name; do
ln -sf "${pkg_cargo_home}/workspaces/${srcdir}/${dir}" "${pkg_cargo_home}/source/${name}"
ln -sf "../workspaces/${srcdir}/${dir}" "${pkg_cargo_home}/source/${name}"
printf '{"files": {}, "package": null}' > "${pkg_cargo_home}/source/${name}/.cargo-checksum.json"
mv "${pkg_cargo_home}/workspaces/${srcdir}/${dir}/Cargo.toml" \
"${pkg_cargo_home}/workspaces/${srcdir}/${dir}/Cargo.toml.orig"
"${vendor_cargo_workspace_package}" \
--input "${pkg_cargo_home}/workspaces/${srcdir}/${dir}/Cargo.toml.orig" \
--output "${pkg_cargo_home}/workspaces/${srcdir}/${dir}/Cargo.toml" \
--workspace "${pkg_cargo_home}/workspaces/${srcdir}/Cargo.toml" || return
done
}
export -f ptxd_make_world_extract_cargo_crate

ptxd_make_world_extract_cargo() {
local src
local src vendor_cargo_workspace_package
echo "extract: cargo dependencies:"
ptxd_in_path PTXDIST_PATH_SCRIPTS vendor-cargo-workspace-package &&
vendor_cargo_workspace_package="${ptxd_reply}" &&
rm -rf "${pkg_cargo_home}" &&
mkdir -p "${pkg_cargo_home}/source" &&
mkdir -p "${pkg_cargo_home}/workspaces" &&
for src in ${pkg_srcs}; do
case "${src}" in
*.crate)
ptxd_make_world_extract_cargo_crate
ptxd_make_world_extract_cargo_crate || return
;;
*)
;;
Expand Down
55 changes: 55 additions & 0 deletions scripts/vendor-cargo-workspace-package
@@ -0,0 +1,55 @@
#!/usr/bin/env python3

import argparse
import toml
from copy import deepcopy
from os.path import dirname, join, relpath

parser = argparse.ArgumentParser(description='vendor cargo workspace package')
parser.add_argument('--input')
parser.add_argument('--output')
parser.add_argument('--workspace')
args = parser.parse_args()

pkg_input = toml.load(args.input)
pkg_output = deepcopy(pkg_input)
workspace = toml.load(args.workspace)

rel = relpath(dirname(args.workspace), dirname(args.input))

for section, data in pkg_input.items():
if not isinstance(data, dict):
continue
for key, value in data.items():
if not isinstance(value, dict):
continue
if not 'workspace' in value:
continue
if not value['workspace']:
continue
if not 'workspace' in workspace:
print(f'missing section "workspace" {args.workspace}')
exit(1)
if section in workspace['workspace']:
wsection = section
else:
wsection = section.removeprefix('dev-')
if not wsection in workspace['workspace']:
print(f'missing section "workspace.{section}" in {args.workspace} for {args.input}')
exit(1)
try:
copy = deepcopy(workspace['workspace'][wsection][key])
except KeyError:
print(f'missing "workspace.{section}.{key}" in {args.workspace} for {args.input}')
exit(1)
if isinstance(copy, dict):
if 'path' in copy:
copy['path'] = join(rel, copy['path'])
pkg_output[section][key].pop('workspace')
pkg_output[section][key].update(copy)
else:
pkg_output[section][key] = copy


with open(args.output, 'w') as f:
toml.dump(pkg_output, f)

0 comments on commit 51496ff

Please sign in to comment.