Passing more flags to rustc
This is kind of a followup to #60. I've been eyeing cargo integration with Visual Studio and after asking on IRC I want to propose more flags for inclusion in Cargo.toml:
--cfg, it's a commonly used rustc feature--Cor at least--C link-args. It's required for passing -mwindows (compile as a Windows Desktop app, no console output) and for passing resources (icons, executable manifests) to the gcc.-W,-A,-D,-F, four lint control flags, projects will want to set stricter/more relaxed baselines across code-base.
As I mentioned in #60, it's unlikely that cargo build will support shipping along arbitrary compiler flags. That being said, here's what cargo build will likely support:
--cfgwill manifest itself with features-Cwill definitely be supported, but probably not-C link-args. The-mwindowsflag may be something that rustc should pass by default? What does it do?- These will likely not be supported as they're what the in-crate attributes will predominately support.
Also as mentioned in #60, the plan is have fine-grained subcommands which you can script yourself. The idea is that cargo build is just a high-level wrapper around these subcommands. With these you'll be able to pass whatever you want, they sadly just don't exist right now (still figuring out what exactly we want).
I'm unclear on the subcommands. Last comment in #60 mentions no arbitrary flags in the manifest/cargo build, but I assume that means "no arbitrary flags in build section but all kinds of arbitrary flags in rustc section", so something like
[rustc]
flags="-C link-args icon.res"
(or whatever syntax will be for subcommands) will be fine. Am I right? If so, I'm perfectly happy with subcommands and closing this issue.
As for the flags. No, -mwindows shouldn't be default. It's for desktop apps like firefox. It sets pe32 subsystem to gui, which means that app doesn't get console. -C link-args is needed for pasing resources: icon, app manifest (though I've just managed to emulate it with #[link(name=..., kind="static")] and empty extern, but I don't think that's very intuitive)
By subcommands, I mean where the cargo commands themselves are all built on a common set of primitive commands. You wouldn't personally use the subcommands that often as they'll be fairly restrictive and may not do much, but they're available to you whenever you need to configure your workflow.
So, for example, cargo build would not take arbitrary flags, but a possible cargo rustc may indeed take many flags to pass through. The downside would be that cargo rustc doesn't actually build or fetch dependencies, it just assumes that they're all in-place.
Does that make sense?
Yes, it does.
#595 is an example of a subcommand which may help here, I'm curious about opinions on it!
Slightly related to this is the issue of finding system libraries. As an example, my libcurl require a -L argument to be passed to rustc, so that rustc can find the library. At the moment there is no good way to tell Cargo to tell rustc where to find system libraries. This could be solved in this issue (by allowing me to explicitly pass a -L option). Or this might be solved in a more specific way (maybe having Cargo honor $LDFLAGS or something similar)
I'm curious about this as well since I'd like to sometimes pass the -g option to run the application through gdb.
Cargo passes -g in the dev profile now by default, which is used when you do a non-release cargo build.
If you wanted to include debugging symbols in a release build, you could set debug = true on the release profile.
http://doc.crates.io/manifest.html#the-[profile.*]-sections
I'd like to control optimization in Cargo.toml:
$ rustc src/lib.rs -O --test; ./xxhash --bench
test xxh32::bench_64k_oneshot ... bench: 17632 ns/iter (+/- 87) = 3716 MB/s
test xxh32::bench_long_str ... bench: 169 ns/iter (+/- 2) = 2639 MB/s
$ rustc src/lib.rs --opt-level=3 -C no-vectorize-slp --test; ./xxhash --bench
test xxh32::bench_64k_oneshot ... bench: 17691 ns/iter (+/- 104) = 3704 MB/s
test xxh32::bench_long_str ... bench: 172 ns/iter (+/- 3) = 2593 MB/s
$ cargo bench
test xxh32::bench_64k_oneshot ... bench: 41706 ns/iter (+/- 42) = 1571 MB/s
test xxh32::bench_long_str ... bench: 336 ns/iter (+/- 7) = 1327 MB/s
FYI, ShowWindow with SW_HIDE can be used as a workaround for the lack of "-mwindows".
Example: http://stackoverflow.com/a/29764309/257568
It seems that cargo either passes -g to rustc (when debug=true) or --cfg ndebug (when debug=false).
Personally, I'd really like my release build to have both -g and --cfg ndebug, because I really care about runtime speed (i.e. I don't want debug_assert!() macros to run in my release build) and I care about being able to debug my programs, but I don't care about the size of the resulting binary.
It seems that I cannot do this with the current cargo manifest format, but if I could control which options to pass to rustc, then I could easily do it.
Unless something like this is implemented, starting in beta it will probably become impossible to use Cargo to compile a program with -mwindows, at least without using the hide_window workaround described above. We really shouldn't have to use a workaround like that. Plus, that technique doesn't work for specifying icons. As far as I can tell, there's no alternative to link_args for that.
Currently you can use the #[link_args] attribute for this, but I'm pretty sure it'll stop working in beta (since it's behind a feature gate). Without Cargo support for link_args, you'll have to run rustc directly in order to use its -C link_args option. I don't think we should force people to run rustc directly if we don't have to.
@nstoddard using -C link-args isn't necessarily super stable and we would probably want a better solution for passing -mwindows to the linker regardless (e.g. what happens when it starts being linked with msvc instead of gcc?).
- --cfg will manifest itself with features
@alexcrichton could you expand on this? I would like to have different features compile with different --cfg options enabled, but it doesn't seem possible now
@deontologician for each activated feature foo Cargo will pass --cfg 'feature="foo"'. More information can also be found here: http://doc.crates.io/manifest.html#the-[features]-section
@alexcrichton you'd still want to pass other kinds of cfgs to e.g. disable logging with --cfg log_level="error"
Ah yes good point, features certainly don't cover all the use cases, just some :)
We have a few additional scenarios from Servo:
-
Debugging perf regressions in rustc, so we'd like to be able to add
-Z time-passesand-Z time-llvm-passesto all rustc invocations, no matter which of the various feature targets we're using. -
Probably being able to pass
-Fto forbid a certain class of lints / overrides, unconditionally, across all feature targets -
Tweaking level of debug info at every optimization level
Note that those are off the top of my head, just things we've wanted to do in the last couple of weeks. I would much rather see you prohibit the specific rustc flags that you don't want people to pass through rather than one-by-one enable subsets from the vast array of flags that do not jeapordize your platform-independent compilation goals.
cc me
and me