Skip to content

Commit 52d1f7f

Browse files
committed
Use files for input and output for now until I can figure out Windows being dumb
1 parent 034ddad commit 52d1f7f

File tree

3 files changed

+27
-51
lines changed

3 files changed

+27
-51
lines changed

spirv-tools/src/assembler/tool.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,18 @@ impl Assembler for ToolAssembler {
1414
text: &str,
1515
options: super::AssemblerOptions,
1616
) -> Result<crate::binary::Binary, crate::error::Error> {
17-
use crate::cmd::CmdError;
18-
1917
let mut cmd = std::process::Command::new("spirv-as");
2018
cmd.arg("--target-env").arg(self.target_env.to_string());
2119

2220
if options.preserve_numeric_ids {
2321
cmd.arg("--preserve-numeric-ids");
2422
}
2523

26-
let temp_dir = tempfile::tempdir().map_err(CmdError::Io)?;
27-
let output_path = temp_dir.path().join("code.spv");
28-
cmd.arg("-o").arg(&output_path);
29-
30-
// Input file
31-
let input_path = temp_dir.path().join("code.txt");
32-
std::fs::write(&input_path, text).map_err(CmdError::Io)?;
33-
cmd.arg(&input_path);
34-
35-
let _cmd_output =
36-
crate::cmd::exec(cmd, Some(text.as_bytes()), crate::cmd::Output::Ignore)?;
37-
38-
let binary = std::fs::read(&output_path).map_err(CmdError::Io)?;
24+
let cmd_output =
25+
crate::cmd::exec(cmd, Some(text.as_bytes()), crate::cmd::Output::Retrieve)?;
3926

4027
use std::convert::TryFrom;
41-
crate::binary::Binary::try_from(binary)
28+
crate::binary::Binary::try_from(cmd_output.binary)
4229
}
4330
}
4431

spirv-tools/src/cmd.rs

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,27 @@ pub fn exec(
7777
input: Option<&[u8]>,
7878
retrieve_output: Output,
7979
) -> Result<CmdOutput, CmdError> {
80-
if input.is_some() {
81-
cmd.stdin(Stdio::piped());
82-
}
83-
8480
cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
8581

86-
let mut child = cmd.spawn().map_err(CmdError::BinaryNotFound)?;
82+
// Create a temp dir for the input and/or output of the tool
83+
let temp_dir = tempfile::tempdir().map_err(CmdError::Io)?;
84+
85+
// Output
86+
let output_path = temp_dir.path().join("output");
87+
if retrieve_output == Output::Retrieve {
88+
cmd.arg("-o").arg(&output_path);
89+
}
8790

91+
// Input
8892
if let Some(input) = input {
89-
use std::io::Write;
90-
91-
child
92-
.stdin
93-
.take()
94-
.unwrap()
95-
.write_all(input)
96-
.map_err(CmdError::Io)?;
93+
let input_path = temp_dir.path().join("input");
94+
std::fs::write(&input_path, input).map_err(CmdError::Io)?;
95+
96+
cmd.arg(&input_path);
9797
}
9898

99+
let child = cmd.spawn().map_err(CmdError::BinaryNotFound)?;
100+
99101
let output = child.wait_with_output().map_err(CmdError::Io)?;
100102

101103
let code = match output.status.code() {
@@ -163,35 +165,24 @@ pub fn exec(
163165
Split { haystack, needle }
164166
}
165167

166-
let retrieve_output = retrieve_output == Output::Retrieve;
168+
let binary = match retrieve_output {
169+
Output::Retrieve => std::fs::read(&output_path).map_err(CmdError::Io)?,
170+
Output::Ignore => Vec::new(),
171+
};
167172

168173
// Since we are retrieving the results via stdout, but it can also contain
169174
// diagnostic messages, we need to be careful
170175
let mut messages = Vec::new();
171-
let mut binary = Vec::with_capacity(if retrieve_output { 1024 } else { 0 });
172176

173-
let mut maybe_msg = true;
174177
for line in split(&output.stdout, b'\n') {
175-
if maybe_msg {
176-
if let Ok(s) = std::str::from_utf8(line) {
177-
if let Some(msg) = crate::error::Message::parse(s) {
178-
messages.push(msg);
179-
continue;
180-
}
181-
}
182-
}
183-
184-
if retrieve_output {
185-
// Handle case where there is a '\n' in the stream, but it's not the
186-
// end of an output message
187-
if !maybe_msg || messages.is_empty() && !binary.is_empty() {
188-
binary.push(b'\n');
178+
if let Ok(s) = std::str::from_utf8(line) {
179+
if let Some(msg) = crate::error::Message::parse(s) {
180+
messages.push(msg);
181+
continue;
189182
}
190-
191-
binary.extend_from_slice(line);
192183
}
193184

194-
maybe_msg = false;
185+
break;
195186
}
196187

197188
Ok(CmdOutput { binary, messages })

spirv-tools/src/opt/tool.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ impl Optimizer for ToolOptimizer {
6666
}
6767
}
6868

69-
cmd.arg("-o").arg("-");
70-
7169
let input = crate::util::from_binary(input);
7270

7371
let cmd_output = crate::cmd::exec(cmd, Some(input), crate::cmd::Output::Retrieve)?;

0 commit comments

Comments
 (0)