Skip to content

Commit

Permalink
fix(rust_extractor): process env vars and compiler args passed to wra…
Browse files Browse the repository at this point in the history
…pper (#5014)

* fix(rust_extractor): process env vars and compiler args passed to wrapper

* chore: fix typo

* fix: optimize functions

* fix: simplify further

Co-authored-by: Shahms King <shahms@google.com>
  • Loading branch information
wcalandro and shahms committed Aug 3, 2021
1 parent 4654ff6 commit d315b5b
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion kythe/rust/extractor/src/bin/save_analysis.rs
Expand Up @@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use anyhow::Result;
use anyhow::{Context, Result};
use std::fs::File;
use std::io::{self, BufRead};
use std::path::{Path, PathBuf};

/// Generates a save analysis in `output_dir`
Expand Down Expand Up @@ -55,5 +57,47 @@ fn generate_arguments(arguments: Vec<String>, output_dir: &Path) -> Result<Vec<S
output_dir.to_str().ok_or_else(|| anyhow!("Couldn't convert temporary path to string"))?;
rustc_arguments[outdir_position] = format!("--out-dir={}", outdir_str);

// Check if there is a file containing environment variables
let env_file_position = arguments.iter().position(|arg| arg == "--env-file");
if let Some(position) = env_file_position {
let env_file = &arguments[position + 1];
let path = Path::new(env_file);
process_env_file(path).context("Failed to process wrapper environment file")?;
}

// Check if there is a file containing extra compiler arguments
let arg_file_position = arguments.iter().position(|arg| arg == "--arg-file");
if let Some(position) = arg_file_position {
let arg_file = &arguments[position + 1];
let path = Path::new(arg_file);
let new_arguments =
process_arg_file(path).context("Failed to process wrapper argument file")?;
rustc_arguments.extend(new_arguments);
}

Ok(rustc_arguments)
}

/// Process an environment variable file passed to the process wrapper
///
/// * `path` - The path of the environment variable file
fn process_env_file(path: &Path) -> Result<()> {
let pwd = std::env::current_dir().context("Couldn't determine pwd")?;
for line in io::BufReader::new(File::open(path)?).lines() {
let line_string = line.context("Failed to read line of env file")?;
let split: Vec<&str> = line_string.trim().split('=').collect();
let value = split[1].replace("${pwd}", pwd.to_str().unwrap());
std::env::set_var(split[0], value);
}
Ok(())
}

/// Process a compiler argument file passed to the process wrapper
///
/// * `path` - The path of the compiler argument file
fn process_arg_file(path: &Path) -> Result<Vec<String>> {
io::BufReader::new(File::open(path)?)
.lines()
.map(|line| line.context("Failed to read line of arg file"))
.collect()
}

0 comments on commit d315b5b

Please sign in to comment.