Subject: [bug#35155] [PATCH] build-system/cargo: refactor phases to successfully build

From: Chris Marusich <[email-masked]>
Date: Sat, 06 Apr 2019 16:27:14 -0700

Hi Ivan!

Ivan Petkov <[email-masked]> writes:

> This patch refactors the cargo-build-system builder code to correctly build
> imported Rust crates.

This is great!  Thank you for taking the time to work on it.  I have
only some high level questions and minor comments.  It seems fine to me
in general.

Since no packages currently use the cargo-build-system, I think we can
basically just commit this on master and keep moving forward.  Danny, do
you agree?

> * Do not rely on a Cargo.lock presence to determine installation. Cargo will
> automatically create the file at its first invocation, so instead we check
> the manifest for any executable targets before attempting an installation

Sounds good.

> * Do not attempt to modify the Cargo.toml file. There are many system
> specific package in crates.io (e.g. for Windows, Redox, Fuschia, WASM,
> etc.) and attempting to keep up with what crates must be patched out
> is futile.

That seems reasonable, too.

> * The build phases will honor a skip-build? flag which allows for
> short-circuiting for optional packages which require nightly features
> or cannot be built for the current platform.

Can you elaborate on the motivation for this?  Are there situations in
which we need to build an optional package, but we aren't actually going
to use its build output anywhere?

If I'm understanding this correctly, it seems like this new flag would
allow us to write a package definition that doesn't actually build a
package.  If that's the case, I'm not sure why we would bother writing
the package definition in the first place.

> Changes which still need to be done:
> * Update the host-side code to expand transitive inputs: cargo requires that
> all transitive crate dependencies are present in its (vendored) index, but
> doing so by hand in the package definitions will become unwieldy.
> * Update the host-side code to detect any "circular" dependencies which can
> result from a naive import

I agree with that plan.  If I've been following along correctly, once we
figure out how to properly make all the transitive crate dependencies
(in source form) available in the build environment (and resolve the
circular dependencies), it should open the door to importing many Rust
packages.

> * guix/build-system/cargo.scm (%cargo-build-system-modules):
> Add (json parser)

Nitpick: You're missing a period here, and in a few more sentences in
the ChangeLog entry.

> Add #:cargo-tset-flags and pass it to cargo

Nitpick: The word "tset" should be "test".

> (install): Factor source logic to install-source.
> Define #:skip-build? flag and use it.
> Only install if executable targets are present.
> (install-source): Copy entire crate directory not just src.

I'm not as familiar with Rust packaging as you are, so the correctness
of this part is not as clear to me.

My understanding is that a Cargo package that is a library needs to
install its source (so that other Cargo libraries/applications can use
it) but not any executables.  On the other hand, a Cargo package that is
an application needs to install executables (so that a user can run it),
but not its source.  Is that right?  What about Cargo packages that are
both libraries and applications?  Do those even exist?

If you could help me understand (or point me to docs that will help me
understand) the model that Rust/Cargo follows here, it would be helpful.

> +(define* (configure #:key inputs
> +                    (vendor-dir "guix-vendor")
> +                    #:allow-other-keys)
>    "Replace Cargo.toml [dependencies] section with guix inputs."

Is this docstring still accurate after these changes?

> +  ;; Lift restriction on any lints: a crate author may have decided to opt
> +  ;; into stricter lints (e.g. #![deny(warnings)]) during their own builds
> +  ;; but we don't want any build failures that could be caused later by
> +  ;; upgrading the compiler for example.
> +  (setenv "RUSTFLAGS" "--cap-lints allow")

Is this necessary?  The docs seem to suggest that Cargo always sets it
to "allow" anyway:

https://doc.rust-lang.org/rustc/lints/levels.html

"This feature is used heavily by Cargo; it will pass --cap-lints allow
when compiling your dependencies, so that if they have any warnings,
they do not pollute the output of your build."

> @@ -122,22 +131,34 @@ directory = '" port)
>      ;; Until this changes we are working around this by
>      ;; distributing crates as source and replacing
>      ;; references in Cargo.toml with store paths.

Is this comment still accurate?

> -    (generate-checksums rsrc src)
> +    (generate-checksums rsrc "/dev/null")

This probably deserves a short comment to clarify the intent.

Really nice stuff - thank you for sharing it!

-- 
Chris