rust-playground icon indicating copy to clipboard operation
rust-playground copied to clipboard

Set `incremental = true` for builds that don't output LLVM IR

Open Kestrer opened this issue 4 years ago • 2 comments
trafficstars

@kangalioo, @danielhenrymantilla and I have been trying to figure out how to use the playground to run procedural macros, for a Discord bot. The code so far looks like this:

fn main(){let _=std::process::Command::new("/bin/bash").args(&["-c", r##"
set -euo pipefail
cat >>Cargo.toml <<EOF
    [lib]
    proc-macro = true
EOF
cat >src/lib.rs <<EOF
    use proc_macro::TokenStream;

    #[proc_macro]
    pub fn foo(input: TokenStream) -> TokenStream {
        quote::ToTokens::into_token_stream(syn::parse_macro_input!(input as syn::Item)).into()
    }
EOF
cat >src/main.rs <<EOF
    ::playground::foo!(fn main() {});
EOF

cargo run --offline
"##]).status();}

However, this doesn't work because all the dependencies used in the playground get rebuilt and the process gets timed out. This happens because incremental = false is enabled for all builds, a change made in #259 to fix the output of the "Show LLVM IR" command. If incremental = false was disabled for all builds except those "Show LLVM IR" ones, this code would work.

Kestrer avatar Apr 17 '21 18:04 Kestrer

After experimenting with cargo locally for a while, I believe the culprit is not incremental = false but codegen-units = 1.

In a test, I set codegen-units = 1 and incremental = true. Now, (after pre-compiling the dependencies like the playground does), when adding proc-macro = true, all dependencies are still rebuilt even though incremental build is enabled.

When the codegen-units = 1 settings is removed, adding proc-macro = true does not cause a recompilation of dependencies, regardless of whether incremental build is turned on or off.

kangalio avatar Apr 17 '21 20:04 kangalio

To clarify: the pattern works when not using dependencies. But indeed the killer is that if we want to feature the pervasive quote and syn crates with this pattern, then the profile settings that the Playground used to precompile the dependencies leads to them no longer being re-usable by a proc-macro = true crate.

I believe that even an intermediate solution of pre-compiling the deps with the default codegen-units and incremental and yet keeping the same settings that the Playground currently does when running user-facing code (so as to keep most of the properties the Playground has, regarding LLVM-IR generation, for instance) would still let us use this pattern, by simply unapplying those settings:

sed -i -e 's/^codegen-units/#/' Cargo.toml
sed -i -e 's/^incremental = false/incremental = true/' Cargo.toml

danielhenrymantilla avatar Apr 17 '21 20:04 danielhenrymantilla