Skip to content

Commit

Permalink
espidf: Improve msys workaround (#22)
Browse files Browse the repository at this point in the history
* espidf: Improve msys workaround
Windows: Fixes compile errors when cygpath is in the PATH while not using msys or cygwin.
* embuild: Bump minor version
  • Loading branch information
N3xed authored Oct 30, 2021
1 parent c18d2a6 commit 78298cc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = ["cargo-pio", "ldproxy"]

[package]
name = "embuild"
version = "0.25.0"
version = "0.25.1"
authors = ["Ivan Markov <ivan.markov@gmail.com>", "Dominik Gschwind <dominik.gschwind99@gmail.com>"]
edition = "2018"
categories = ["embedded", "development-tools::build-utils"]
Expand Down
37 changes: 16 additions & 21 deletions src/espidf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,21 +273,7 @@ impl Installer {
});
self.clone_esp_idf(&mut esp_idf)?;

// This is a workaround for msys or even git bash.
// When using them `idf_tools.py` prints unix paths (ex. `/c/user/` instead of
// `C:\user\`), so we correct this with an invocation of `cygpath` which converts the
// path to the windows representation.
let cygpath_works = cfg!(windows) && cmd_output!("cygpath", "--version").is_ok();
let to_win_path = if cygpath_works {
|p: String| cmd_output!("cygpath", "-w", p).unwrap()
} else {
|p: String| p
};
let path_var_sep = if cygpath_works || cfg!(not(windows)) {
':'
} else {
';'
};
let path_var_sep = if cfg!(not(windows)) { ':' } else { ';' };

// Create python virtualenv or use a previously installed one.

Expand All @@ -298,7 +284,7 @@ impl Installer {

let get_python_env_dir = || -> Result<String> {
Ok(cmd_output!(PYTHON, &idf_tools_py, "--idf-path", esp_idf.worktree(), "--quiet", "export", "--format=key-value";
ignore_exitcode, env=("IDF_TOOLS_PATH", &install_dir))?
ignore_exitcode, env=("IDF_TOOLS_PATH", &install_dir), env_remove=("MSYSTEM"))?
.lines()
.find(|s| s.trim_start().starts_with("IDF_PYTHON_ENV_PATH="))
.ok_or_else(|| anyhow!("`idf_tools.py export` result contains no `IDF_PYTHON_ENV_PATH` item"))?
Expand All @@ -307,13 +293,12 @@ impl Installer {
.to_string())
};

let python_env_dir = get_python_env_dir().map(&to_win_path);
let python_env_dir: PathBuf = match python_env_dir {
let python_env_dir: PathBuf = match get_python_env_dir() {
Ok(dir) if Path::new(&dir).exists() => dir,
_ => {
cmd!(PYTHON, &idf_tools_py, "--idf-path", esp_idf.worktree(), "--quiet", "--non-interactive", "install-python-env";
env=("IDF_TOOLS_PATH", &install_dir))?;
to_win_path(get_python_env_dir()?)
get_python_env_dir()?
}
}.into();

Expand Down Expand Up @@ -344,9 +329,17 @@ impl Installer {
env=("IDF_TOOLS_PATH", &install_dir), args=(tool.tools))?;

// Get the paths to the tools.
//
// Note: `idf_tools.py` queries the environment
// variable `MSYSTEM` to determine if it should convert the paths to its shell
// equivalent on windows
// (https://github.com/espressif/esp-idf/blob/bcbef9a8db54d2deef83402f6e4403ccf298803a/tools/idf_tools.py#L243)
// (for example to unix paths when using msys or cygwin), but we need Windows
// native paths in rust. So we remove that environment variable when calling
// idf_tools.py.
exported_paths.extend(
cmd_output!(python, &idf_tools_py, "--idf-path", esp_idf.worktree(), @tools_json, "--quiet", "export", "--format=key-value";
ignore_exitcode, env=("IDF_TOOLS_PATH", &install_dir))?
ignore_exitcode, env=("IDF_TOOLS_PATH", &install_dir), env_remove=("MSYSTEM"))?
.lines()
.find(|s| s.trim_start().starts_with("PATH="))
.expect("`idf_tools.py export` result contains no `PATH` item").trim()
Expand All @@ -360,10 +353,12 @@ impl Installer {
let paths = env::join_paths(
exported_paths
.into_iter()
.map(|s| PathBuf::from(to_win_path(s)))
.map(PathBuf::from)
.chain(env::split_paths(&env::var_os("PATH").unwrap_or_default())),
)?;

log::debug!("Using PATH='{}'", &paths.to_string_lossy());

Ok(EspIdf {
install_dir,
esp_idf,
Expand Down

0 comments on commit 78298cc

Please sign in to comment.