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

Compile a crate from its source archive directly #128884

Open
fenollp opened this issue Aug 8, 2024 · 6 comments
Open

Compile a crate from its source archive directly #128884

fenollp opened this issue Aug 8, 2024 · 6 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@fenollp
Copy link

fenollp commented Aug 8, 2024

Problem

Today, crates provided by e.g. crates.io are downloaded in a (somewhat loosely defined?) archive format, with a .crate extension. Example: https://static.crates.io/crates/clap/clap-3.0.6.crate

clap-3.0.6.crate: gzip compressed data, was "clap-3.0.6.crate", max compression, original size modulo 2^32 1110528

This gets stored as a local cache and is also extracted in a nearby folder, which then gets fed to cargo / rustc for compilation.

I (and surely others) am developing a $RUSTC_WRAPPER that has to deal with these archives and thus have to replicate the handling of said .crates. Given these open issues, and for optimization reasons, I would like for cargo (or rustc) to be able to take in these archives directly, without prior extraction:

Proposed Solution

I am not familiar with the inner workings of either cargo nor rustc so this is a high level take.

Basically, I'm asking that today's call to rustc be seamlessly interchangeable from:

rustc --crate-name clap ...... $HOME/.cargo/registry/src/index.crates.io-6f17d22bba15001f/clap-3.0.6/src/lib.rs ....

to

rustc --crate-name clap ...... $HOME/.cargo/registry/cache/index.crates.io-6f17d22bba15001f/clap-3.0.6.crate ...

thus moving the crate archive handling code to within the boundary of (I guess) rustc.

This in turn

  • encapsulates the archive format
  • allows me (and others) to just pass archives down from crates.io straight to rustc

Notes

I realize this issue may need to move to rustc. But hey let's have a small discussion first :)

@fenollp fenollp added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Aug 8, 2024
@epage
Copy link
Contributor

epage commented Aug 8, 2024

https://internals.rust-lang.org/t/jar-for-rust-single-file-crate-support-for-rustc/19576 is the last conversation I saw for compiling a .crate without extracting it.

I (and surely others) am developing a $RUSTC_WRAPPER that has to deal with these archives and thus have to replicate the handling of said .crates

This part isn't making sense to me. If you are passing a $RUSTC_WRAPPER to cargo, why are you needing to build .crate files as cargo would have extracted it for you and told you where the source is.

allows me (and others) to just pass archives down from crates.io straight to rustc

Sounds like you aren't creating a $RUSTC_WRAPPER but creating your own build tool on top of rustc, is that right?

Then this is purely a rustc concern and not a Cargo concern, though designing this in a way for Cargo to take advantage of would be useful.

@workingjubilee
Copy link
Member

I feel like it would be better to first librarify the relevant code in cargo and then discuss adding it to rustc?

@epage
Copy link
Contributor

epage commented Aug 8, 2024

A lot of that depends on what rustc would want.

For example, one idea that came up in that thread is if we could abstract this in a way for cargo to define the filesystem so we could read from git databases directly, rather than checking out files. Would we want that in scope? What would it look like to support something like that?

@fenollp
Copy link
Author

fenollp commented Aug 9, 2024

cargo would have extracted it for you
Sounds like you aren't creating a $RUSTC_WRAPPER

Well you're right, this issue should probably live in rustc's repo :)
I am indeed creating a wrapper but I am also handling fetching and extracting crates.io deps as an optimization. See e.g.

FROM scratch AS cratesio-ring-0.17.8-index.crates.io-6f17d22bba15001f
ADD --chmod=0664 --checksum=sha256:c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d \
  https://static.crates.io/crates/ring/ring-0.17.8.crate /crate
SHELL ["/usr/bin/dash", "-c"]
RUN \
  --mount=from=rust-base,src=/lib,dst=/lib \
  --mount=from=rust-base,src=/lib64,dst=/lib64 \
  --mount=from=rust-base,src=/usr,dst=/usr \
    set -eux \
 && mkdir -p /extracted \
 && tar zxvf /crate --strip-components=1 -C /extracted

I use this to fetch and extract a .crate. Passing the .crate directly to rustc would allow me to drop everything after the ADD :)


WRT git I'd say it'd enlarge the scope of this issue. But I quite like the idea as well!

@weihanglo
Copy link
Member

we could abstract this in a way for cargo to define the filesystem so we could read from git databases directly

The performance might be a concern for reading from git versus from file system.

@weihanglo
Copy link
Member

squashfs could be another possibility.

@epage epage transferred this issue from rust-lang/cargo Aug 9, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 9, 2024
@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants