rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.65k stars 2.4k forks source link

What's the right way to install/build with lto? #7539

Closed elichai closed 4 years ago

elichai commented 4 years ago

Hi, I want to use LTO when i'm installing crates but doing the naive RUSTFLAGS=-Clto cargo install something doesn't quite work, it seems to fail on proc-macros

Example:

$ RUSTFLAGS=-Clto cargo install ripgrep
    Updating crates.io index
  Installing ripgrep v11.0.2
   Compiling byteorder v1.3.2
   Compiling proc-macro2 v1.0.6
   Compiling memchr v2.2.1
   Compiling encoding_rs v0.8.20
   Compiling libc v0.2.65
   Compiling unicode-xid v0.2.0
   Compiling lazy_static v1.4.0
   Compiling ryu v1.0.2
   Compiling serde v1.0.101
   Compiling syn v1.0.5
   Compiling bitflags v1.2.1
   Compiling log v0.4.8
   Compiling cfg-if v0.1.10
   Compiling unicode-width v0.1.6
   Compiling regex-syntax v0.6.12
   Compiling bytecount v0.5.1
   Compiling itoa v0.4.4
   Compiling strsim v0.8.0
   Compiling fnv v1.0.6
   Compiling termcolor v1.0.5
   Compiling same-file v1.0.5
   Compiling thread_local v0.3.6
   Compiling textwrap v0.11.0
   Compiling crossbeam-utils v0.6.6
   Compiling walkdir v2.2.9
   Compiling crossbeam-channel v0.3.9
   Compiling encoding_rs_io v0.1.6
   Compiling memmap v0.7.0
   Compiling atty v0.2.13
   Compiling num_cpus v1.10.1
   Compiling quote v1.0.2
   Compiling clap v2.33.0
   Compiling aho-corasick v0.7.6
   Compiling grep-matcher v0.1.3
   Compiling serde_json v1.0.41
   Compiling ripgrep v11.0.2
   Compiling regex v1.3.1
   Compiling grep-regex v0.1.5
   Compiling regex-automata v0.1.8
   Compiling base64 v0.10.1
   Compiling bstr v0.2.8
   Compiling serde_derive v1.0.101
   Compiling grep-searcher v0.1.6
   Compiling globset v0.4.4
   Compiling grep-cli v0.1.3
   Compiling ignore v0.4.10
error: cannot prefer dynamic linking when performing LTO

note: only 'staticlib', 'bin', and 'cdylib' outputs are supported with LTO

error: aborting due to previous error

error: Could not compile `serde_derive`.
warning: build failed, waiting for other jobs to finish...
error: failed to compile `ripgrep v11.0.2`, intermediate artifacts can be found at `/tmp/cargo-installKaYQ3b`

Caused by:
  build failed
ehuss commented 4 years ago

This is supported with the config profile feature:

CARGO_PROFILE_RELEASE_LTO=true cargo +nightly install ripgrep -Zconfig-profile

ehuss commented 4 years ago

I'm going to close this in favor of issues like #6375 and https://github.com/rust-lang/rust/issues/48683.

elichai commented 4 years ago

For future searchers this is the easiest method I've found on stable: RUSTFLAGS=-Clto cargo install ripgrep --target x86_64-unknown-linux-gnu

Thanks: https://github.com/rust-lang/cargo/issues/6375#issuecomment-444900324