4

I have a workspace project with multiple packages. The two important ones are:

  • flowc - which is a lib and a binary
  • flowstdlib

flowc is a kind of compiler that I build as part of the project.

flowstdlib has a build script, that uses the flowc built binary to build that package (generate "code", files etc), so I need the flowc compiler ready when flowstdlib is to be built.

In cargo.toml of flowstdlib I define flowc as a build dependency:

[build-dependencies]
flowc = {path = "../flowc", version = "0.31.0" }`

(I've tried making it also a dependency, but no change)

in the build.rs of flowstdlib I look for it in the path, and if not found in the ../target/debug/flowc location:

    let flowc = if Path::new(env!("CARGO_MANIFEST_DIR")).join("../target/debug/flowc").exists() {
        "../target/debug/flowc"
    } else if Simpath::new("PATH").find_type("flowc", FileType::File).is_ok() {
        "flowc"
    } else {
        ""
    };

When I run the build, it looks like it's trying to build multiple packages at the same time in parallel:

   Compiling flowstdlib v0.31.0 (/Users/andrew/workspace/flow/flowstdlib)
   Compiling flowsamples v0.31.1 (/Users/andrew/workspace/flow/samples)
warning: Could not find `flowc` in $PATH or `target/debug`, so cannot build flowstdlib
error: failed to run custom build command for `flowsamples v0.31.1 (/Users/andrew/workspace/flow/samples)`

and the flowstdlib build fails as flowc binary is not built yet.

Since the build continues and eventually finishes building flowc, if I re-run the build, it will work the second time around (as flowc binary is now found).

So:

  • it looks like a build-dependency does wait for the depended-on binary to be built (maybe it waits for the library to be built, hard to tell)

Question

How I can make the build of flowstdlib wait for the completion of the flowc binary? (without forcing a non-parallel build)

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Andrew Mackenzie
  • 5,477
  • 5
  • 48
  • 70
  • I don't think there's an easy way to depend on binaries in Cargo. Have you considered refactoring the core logic of `flowc` into a library, and depending on that? – Lambda Fairy Dec 11 '20 at 00:26
  • There's an RFC posted just last week about adding support for depending on binaries to Cargo: https://github.com/rust-lang/rfcs/pull/3028 – Kevin Reid Dec 11 '20 at 00:34
  • flowc already has the core in a library. I would need to then invoke it from build.rs without calling the binary - could be done but not ideal. Thanks for the idea @LambdaFairy – Andrew Mackenzie Dec 11 '20 at 09:26
  • Thanks @KevinReid I will check out that PR and see if it might land in a reasonable time – Andrew Mackenzie Dec 11 '20 at 09:27

1 Answers1

3

Pending the RFC landing, my workaround is to split the build into two commands (I'm using a Makefile to invoke them currently):

* cargo build -p flowc   # will complete the build of the flowc binary
* cargo build            # will build the entire workspace, including flowstdlib
Andrew Mackenzie
  • 5,477
  • 5
  • 48
  • 70