Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collect crate metadata during 'prepare' phase of build #52

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions colcon_cargo/task/cargo/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,16 @@ async def build( # noqa: D102
elif build_dir.exists():
shutil.rmtree(build_dir)

# Invoke build step
if CARGO_EXECUTABLE is None:
raise RuntimeError("Could not find 'cargo' executable")

# Get package metadata
metadata = await self._get_metadata(env)

cargo_args = args.cargo_args
if cargo_args is None:
cargo_args = []
# Invoke build step
cmd = self._build_cmd(cargo_args)

self.progress('build')
Expand All @@ -85,7 +88,7 @@ async def build( # noqa: D102
# We also need to check if the package has any binaries, because if it
# has no binaries then cargo install will return an error.
cmd = self._install_cmd(cargo_args)
if cmd is not None and await self._has_binaries(env):
if cmd is not None and self._has_binaries(metadata):
self.progress('install')
rc = await run(
self.context, cmd, cwd=self.context.pkg.path, env=env)
Expand Down Expand Up @@ -141,15 +144,17 @@ def _install_cmd(self, cargo_args):
cmd += ['--profile', 'dev']
return cmd + cargo_args

# Identify if there are any binaries to install for the current package
async def _has_binaries(self, env):
async def _get_metadata(self, env):
cmd = [
CARGO_EXECUTABLE,
'metadata',
'--no-deps',
'--format-version', '1',
]

# TODO: The reported target_directory is wrong. Does it matter here?
# We could maybe override it with --config

rc = await run(
self.context,
cmd,
Expand All @@ -167,7 +172,11 @@ async def _has_binaries(self, env):
"Failed to capture stdout from 'cargo metadata'"
)

metadata = json.loads(rc.stdout)
return json.loads(rc.stdout)

# Identify if there are any binaries to install for the current package
@staticmethod
def _has_binaries(metadata):
for package in metadata.get('packages', {}):
for target in package.get('targets', {}):
for crate_type in target.get('crate_types', {}):
Expand Down
1 change: 1 addition & 0 deletions test/spell_check.words
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ rustfmt
scspell
setuptools
skipif
staticmethod
symlink
tempfile
testcase
Expand Down
Loading