Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo build --out-dir #5203

Merged
merged 4 commits into from
Apr 3, 2018
Merged

cargo build --out-dir #5203

merged 4 commits into from
Apr 3, 2018

Conversation

matklad
Copy link
Member

@matklad matklad commented Mar 19, 2018

This is intended to fix #4875. Very much work in progress, just to figure out how exactly --out-dir should work :)

The path of least resistance for implementing out-dir would be to just link everything from target/debug to the out-dir. However, the problem with this approach is that we link too much to the target/debug. For example, if you run cargo build --bin foo, you'll find not only the foo itself there, but also rlibs from the same workspace.

I think it's rather important not to copy extra stuff to out-dir, so it is implemented as a completely new parameter, which is threaded through various config structs and applies only to the top-level units (i.e, to the stuff user explicitly requested to compile on the command line).

I also plan to add platform specific tests here, to check that we get .pdb on windows and .dSYM on macs for various crate-types.

Because, in general, a single target may produce multiple files, out-dir has to be a directory, you can't directly specify the output file.

Note that artifacts are still generated to the target dir as well.

Also cc @nirbheek, I hope that this might be useful for Meson integration, because --out-dir should be more straightforward to use than --message-format=json.

The end result, for cargo build --bin rustraytracer --out-dir out on windows looks like this:

image

Note how we have fewer files in the out :)

r? @alexcrichton

@alexcrichton
Copy link
Member

Looks good to me! Technically new features start unstable but I'd be ok having this start out as a stable option

@bors
Copy link
Collaborator

bors commented Mar 25, 2018

☔ The latest upstream changes (presumably #5232) made this pull request unmergeable. Please resolve the merge conflicts.

@matklad
Copy link
Member Author

matklad commented Mar 29, 2018

cc @sfackler, I think this should allow you to avoid --message-format=json.

@matklad matklad force-pushed the out-dir branch 2 times, most recently from 47d57fa to d23a020 Compare April 3, 2018 12:26
@matklad matklad changed the title WIP: cargo build --out-dir cargo build --out-dir Apr 3, 2018
@matklad matklad force-pushed the out-dir branch 2 times, most recently from 45f2c19 to 1b393f3 Compare April 3, 2018 12:51
@matklad
Copy link
Member Author

matklad commented Apr 3, 2018

Featuregated, cleanedup and added more tests! Should be ready for review now, although mac tests would probably fail because I don't have a mac machine nearby :-)

@matklad
Copy link
Member Author

matklad commented Apr 3, 2018

Hopefully the mac tests should be fixed now as well!

@alexcrichton
Copy link
Member

@bors: r+

Nice!

@bors
Copy link
Collaborator

bors commented Apr 3, 2018

📌 Commit c919118 has been approved by alexcrichton

@bors
Copy link
Collaborator

bors commented Apr 3, 2018

⌛ Testing commit c919118 with merge f082805...

bors added a commit that referenced this pull request Apr 3, 2018
cargo build --out-dir

This is intended to fix #4875. Very much work in progress, just to figure out how exactly `--out-dir` should work :)

The path of least resistance for implementing `out-dir` would be to just link everything from `target/debug` to the `out-dir`. However, the problem with this approach is that we link *too much* to the `target/debug`. For example, if you run `cargo build --bin foo`, you'll find not only the `foo` itself there, but also rlibs from the same workspace.

I think it's rather important not to copy extra stuff to out-dir, so it is implemented as a completely new parameter, which is threaded through various config structs and applies *only* to the top-level units (i.e, to the stuff user explicitly requested to compile on the command line).

I also plan to add platform specific tests here, to check that we get .pdb on windows and .dSYM on macs for various crate-types.

Because, in general, a single target may produce multiple files, `out-dir` has to be a directory, you can't directly specify the output *file*.

Note that artifacts are still generated to the `target` dir as well.

Also cc @nirbheek, I hope that this might be useful for Meson integration, because `--out-dir` should be more straightforward to use than `--message-format=json`.

The end result, for `cargo build --bin rustraytracer --out-dir out` on windows looks like this:

![image](https://user-images.githubusercontent.com/1711539/37605115-941c0b22-2ba3-11e8-9685-9756a10fdfac.png)

Note how we have fewer files in the `out` :)

r? @alexcrichton
@bors
Copy link
Collaborator

bors commented Apr 3, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: alexcrichton
Pushing f082805 to master...

@bors bors merged commit c919118 into rust-lang:master Apr 3, 2018
@matklad matklad added the relnotes Release-note worthy label Apr 3, 2018
@matklad matklad deleted the out-dir branch April 3, 2018 20:19
matklad added a commit to matklad/rust that referenced this pull request Apr 4, 2018
This includes at least two notable changes:

  * a regression is fixed where Cargo would update index on every
    operation rust-lang/cargo#5288
  * a new unstable `--out-dir` option is implemented
    rust-lang/cargo#5203
kennytm added a commit to kennytm/rust that referenced this pull request Apr 4, 2018
Update Cargo

This includes at least two notable changes:

  * a regression is fixed where Cargo would update index on every
    operation rust-lang/cargo#5288
  * a new unstable `--out-dir` option is implemented
    rust-lang/cargo#5203
@epage
Copy link
Contributor

epage commented Apr 11, 2018

Is this flag made available to build scripts so that we can get completions and other generated files into this directory? I'm coming at this from the packaging angle.

@matklad
Copy link
Member Author

matklad commented Apr 11, 2018

No, it is not exposed to build scripts. Moreover, it seems to me that it is wrong for build scripts to do anything packaging related, because packaging should happen after the code is build, and not before.

Regarding packaging in general, I think we need a more elaborate solution here. I expect that packaging is an open-ended task: you want completions for linux, you want to sign your binaries for mac and windows, and you might want to run some kind of assets managing pipeline for complex web/multimedia applications.

So, at this moment, I would say that packaging should be done completely outside of Cargo. I.e, a Makefile could be written, which calls cargo with --output-dir to build the raw binaries, and then does arbitrary stuff to "package" an application, whatever it means. If you don't want to write platform-specific makefiles/shell scripts, you kinda can use arbitrary rust code for that: create a package-app binary at src/bin/package-app.rs, write packaging logic in Rust, and invoke it as cargo run --bin package-app. The latter hack would probably become more official with workflows.

@epage
Copy link
Contributor

epage commented Apr 11, 2018

Sorry, I wasn't clear. Packaging is happening outside of cargo. I was more referring to making all build artifacts intended to be packaged in an easily identifiable location.

@matklad
Copy link
Member Author

matklad commented Apr 12, 2018

So, --output-dir is indeed intended to make all build artifacts, produced by Cargo, available to the outside world. However, the set of artifacts, produced by Cargo, is determined by the crate type and is not extensible.

In particular, it is not possible (in theory) to make a build script, which produces some additional artifacts, like man pages and completions, and puts them somewhere in the target/output dir. In practice of course you can do anything in the build scripts, it just may not work in some circumstances, and might break if we change implementation details of Cargo :-)

So, sure, --output-dir is intended for packaging as well, but generating completions via build.rs is probably not what you want to do, and it is orthogonal to --output-dir I would think.

@ehuss ehuss added this to the 1.27.0 milestone Feb 6, 2022
bors added a commit that referenced this pull request Jun 7, 2024
Rename --out-dir to --artifact-dir

Progress towards unblocking #6790. Renames the experimental `--out-dir` argument to `--artifact-dir`, both to reflect that it's where the final build *artifacts* will be copied to, and to avoid confusion with the `OUT_DIR` environment variable which serves an entirely different purpose.

For transition purposes, `--out-dir` argument and `out-dir` config key will still work with a deprecation message encouraging the use of the new arg and config key.

### Rationale

A lot of people seem to be confused by the naming of the `--out-dir` argument, and are misled into thinking it serves the same purpose as the `OUT_DIR` environment variable:

> [However, it doesn't seem that OUT_DIR environment variable is set to the value of --out-dir when build.rs is executed.](#6790 (comment))

> [I understand that the worry is that there could be confusion between --out-dir for cargo and the environment variable OUT_DIR for build.rs, but doesn't it mean exactly the same in both cases?](#6790 (comment))

> [--out-dir: Things will be built into $PWD/target as normal, but copies some of the artifacts into the directory specified by out-dir (not a profile specific subdirectory). Unstable flag, added in March 2018. cargo build --out-dir #5203 Ability to specify output artifact name #4875. **Mimicks the behavior of OUT_DIR.**](#6100 (comment))

> [I recently had a couple of people express an interest in --out-dir being stabilized and from my initial digging it seems like what they may actually want is to switch to OUT_DIR, which is already stable.](#6100 (comment))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes Release-note worthy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to specify output artifact name
5 participants