Write to stdout if `-` is given as output file by pjhades · Pull Request #111626...
source link: https://github.com/rust-lang/rust/pull/111626
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Write to stdout if -
is given as output file
#111626
Merged
Conversation
Contributor
With this PR, if -o -
or --emit KIND=-
is provided, output will be written to stdout instead. Binary output (those of type obj
, llvm-bc
, link
and metadata
) being written this way will result in an error unless stdout is not a tty. Multiple output types going to stdout will trigger an error too, as they will all be mixded together.
This implements rust-lang/compiler-team#431
The idea behind the changes is to introduce an OutFileName
enum that represents the output - be it a real path or stdout - and to use this enum along the code paths that handle different output types.
Collaborator
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @b-naber (or someone else) soon. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
labels
Collaborator
These commits modify the If this was unintentional then you should revert the changes before this PR is merged. Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
This comment has been minimized.
Collaborator
These commits modify the If this was unintentional then you should revert the changes before this PR is merged. Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Contributor
The latest upstream changes (presumably #111641) made this pull request unmergeable. Please resolve the merge conflicts. |
Contributor
The latest upstream changes (presumably #111858) made this pull request unmergeable. Please resolve the merge conflicts. |
Sorry for the late review. This looks good to me, but some nits.
compiler/rustc_codegen_ssa/messages.ftl
Outdated Show resolved
sess, |
||
crate_type, |
||
outputs, |
||
codegen_results.crate_info.local_crate_name, |
||
); |
||
let out_filename = match output { |
Contributor
This could be included in a OutputFileName::New
function.
Contributor
Author
Do you mean defining something like this?
impl OutFileName {
pub fn temp_path(&self, flavor: OutputType, codegen_unit_name: Option<&str>) -> PathBuf {
match *self {
OutFileName::Real(ref path) => path.clone(),
OutFileName::Stdout => outputs.temp_path(flavor, codegen_unit_name),
}
}
}
Then use it here:
let out_filename = output.temp_path();
Contributor
I read this wrong initially. But I think something like this should work (didn't compile this):
impl OutFileName {
pub fn process<'a>(&self, sess: &'a Session, tempfiles_for_stdout_output: &mut Vec<PathBuf>, outputs: &OutputFilenames) {
match self {
Self::Real(_) => {/* Don't think we need to do anything here */ }
Self::Stdout => {
let crate_name = format!("{}", codegen_results.crate_info.local_crate_name);
let temp_filename =
outputs.temp_path(OutputType::Exe, Some(crate_name.as_str()));
if self.is_tty() {
sess.emit_err(errors::BinaryOutputToTty {
shorthand: OutputType::Exe.shorthand(),
});
} else if let Err(e) = copy_to_stdout(&temp_filename) {
sess.emit_err(errors::CopyPath::new(&temp_filename, self.as_path(), e));
}
tempfiles_for_stdout_output.push(temp_filename);
}
}
}
}
and then use out_filename(..).process(...)
Which seems better to me than including all of this logic in link_binary
.
Contributor
Author
Which seems better to me than including all of this logic in
link_binary
.
Yeah, that's a fair point. I thought about this.
Here basically I wanted to do two things, before and after linking:
- Before linking, generate a filename for the file that would hold the result of linking
- Link as usual
- After linking, check if we need to copy the content of that file to stdout
It's a bit hard to put the two things in one function. I think it makes more sense to just abstract the before-linking part, like I commented previously. Regarding the after-linking part, it doesn't feel natural to me if I put it in a function like the following, because it feels like the self
here is just a thin layer for the convenience of calling a function, and the function does a bunch of things somewhat unrelated to self
:
impl OutFileName {
pub fn copy_file_to_stdout<'a>(&self, flavor: OutputType, file: &PathBuf, sess: &'a Session) {
match *self {
OutFileName::Real(_) => ();
OutFileName::Stdout => {
if self.is_tty() {
sess.emit_err(errors::BinaryOutputToTty {
shorthand: flavor.shorthand(),
});
} else if let Err(e) = copy_to_stdout(file) {
sess.emit_err(errors::CopyPath::new(file, self.as_path(), e));
}
}
}
}
}
What do you think?
Contributor
Seems good. For the part before linking you could also have a method like get_filename
on OutFileName
, can't you?
Contributor
Author
Yes I can. I added two helpers to OutFileName
and simplified the part before linking. IMHO, I'm leaning towards keeping the after-linking part as is, because - in addition to my point above - the logic is one of the steps of link_binary
when the output is stdout: the content of the linked binary should be written to the desired destination.
compiler/rustc_codegen_ssa/src/back/link.rs
Outdated Show resolved
compiler/rustc_driver_impl/src/lib.rs
Show resolved
compiler/rustc_interface/src/passes.rs
Outdated Show resolved
compiler/rustc_interface/src/passes.rs
Show resolved
compiler/rustc_interface/src/util.rs
Outdated Show resolved
compiler/rustc_mir_transform/src/dump_mir.rs
Show resolved
src/doc/rustc/src/command-line-arguments.md
Outdated Show resolved
Contributor
LGTM. Thanks for the PR. @bors r+ |
added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
labels
This comment has been minimized.
Contributor
Test failed - checks-actions |
added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
labels
This comment has been minimized.
This comment has been minimized.
Contributor
Sorry that I can't help you here, I'm not really familiar with that test suite. Have your tried asking for help on zulip? |
Contributor
Author
@b-naber Thanks for checking back. I haven't asked on Zulip, but I found that the There's another issue of missing
The check would fail if I run for example the I'll run a windows build to see if any error happens. If it still complains about missing |
This comment has been minimized.
Contributor
Author
@rustbot review I fixed the original test and also another bug that would show up in x86-msvc jobs. |
added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
labels
Contributor
Can you explain what exactly you did? What other bug do you think you have found? Also can you please not squash all new changes into old commits? This makes it really tedious to review, because the reviewer needs to go through all changes again. |
Contributor
Author
@b-naber My bad, I restored the history. Hopefully it should be easier to read. For the original error
It was because my test program After the fix build passed, but CI job
This was because the config option Finally I ran CI job
This happened because I tried to remove the temporary metadata file but I didn't have the permission in the test environment somehow. But actually I don't have to manually delete the temporary metadata file because it is managed by |
Contributor
@bors r+ rollup=iffy |
added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
labels
Contributor
Test successful - checks-actions |
Collaborator
Finished benchmarking commit (343ad6f): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)Results CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 648.999s -> 647.29s (-0.26%) |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Successfully merging this pull request may close these issues.
None yet
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK