6

With Cargo, I can set a project's development settings to use parallel code generation:

[profile.dev]
codegen-units = 8

According to the documentation, it should be possible to put this into ~/.cargo/config to apply this setting to all projects. This doesn't work for me: it seems that the .cargo/config file isn't used at all. Is there any way to apply such configuration to every project I compile?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
George Hilliard
  • 15,402
  • 9
  • 58
  • 96
  • The documentation you linked to doesn't list any of the profiles as applicable global configuration; perhaps that is the problem? Since profiles seem pretty project-specific, it seems likely that these files wouldn't even be checked. Have you tried any of the other possible paths? – Shepmaster Aug 16 '15 at 02:31
  • Ohh, I hadn't considered that profiles might not be globally configurable. I haven't tried any other paths. – George Hilliard Aug 16 '15 at 14:59

2 Answers2

4

You can set rustflags for all builds or per target in your .cargo/config file.

[build] # or [target.$triple]
rustflags = ["-Ccodegen-units=4"]

To be clear, this will set the codegen-units for all your projects (covered by this .cargo/config) regardless of profile.

To make sure it's actually set, you can also set verbose output in the same file. This will show each rustc command with flags that cargo invokes.

[term]
verbose = true
Zoomulator
  • 20,774
  • 7
  • 28
  • 32
  • Your answer *almost* answers OPs question, but it's unclear how you'd set this option for the `dev` profile, as requested. – Shepmaster Jun 04 '17 at 01:27
  • @Shepmaster The question is a bit ambiguous though. He states you can set it per profile in one project, but then asks how to enable it for 'every project [you] compile'. It's unclear if he wants to use codegen-units for all projects compiling with the dev profile, or just all projects. I'll clarify that this affects all projects regardless of profile. – Zoomulator Jun 05 '17 at 16:16
2

A workaround is to create a script to be called instead of cargo

#!/bin/bash

if [[ $* != *--release*  ]]; then
    # dev build
    export RUSTFLAGS="-C codegen-units=8"
fi

cargo "$@"

If you use the full path to cargo on the script, you can create a alias

alias cargo=/path/to/script

and just call cargo.

rofrol
  • 14,438
  • 7
  • 79
  • 77
malbarbo
  • 10,717
  • 1
  • 42
  • 57