Skip to content

Commit 55bd1da

Browse files
Nix - support - rebase #353 (#356)
* build cargo_driver: update embuild to support non-git `IDF_PATH` * Leave a TODO that we need to cleanup the code that deals with an activated ESP IDF environment --------- Co-authored-by: Denbeigh Stevens <[email protected]>
1 parent 45c2881 commit 55bd1da

File tree

2 files changed

+58
-44
lines changed

2 files changed

+58
-44
lines changed

build/common.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,7 @@ impl InstallDir {
257257
/// Get the install directory from the [`ESP_IDF_TOOLS_INSTALL_DIR_VAR`] env variable.
258258
///
259259
/// If this env variable is unset or empty uses `default_install_dir` instead.
260-
/// On success returns `(install_dir as InstallDir, is_default as bool)`.
261-
pub fn try_from(location: Option<&str>) -> Result<InstallDir> {
260+
pub fn try_from(location: Option<&str>) -> Result<Self> {
262261
let (location, path) = match &location {
263262
None => (crate::config::DEFAULT_TOOLS_INSTALL_DIR, None),
264263
Some(val) => {

build/native/cargo_driver.rs

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use config::{ESP_IDF_REPOSITORY_VAR, ESP_IDF_VERSION_VAR};
1010
use embuild::cargo::IntoWarning;
1111
use embuild::cmake::file_api::codemodel::Language;
1212
use embuild::cmake::file_api::ObjKind;
13-
use embuild::espidf::{EspIdfOrigin, EspIdfRemote, FromEnvError, DEFAULT_ESP_IDF_REPOSITORY};
13+
use embuild::espidf::{
14+
EspIdfOrigin, EspIdfRemote, FromEnvError, NotActivatedError, SourceTree,
15+
DEFAULT_ESP_IDF_REPOSITORY,
16+
};
1417
use embuild::fs::copy_file_if_different;
1518
use embuild::utils::{OsStrExt, PathExt};
1619
use embuild::{bindgen, build, cargo, cmake, espidf, git, kconfig, path_buf};
@@ -49,7 +52,7 @@ pub fn build() -> Result<EspIdfBuildOutput> {
4952
if let Ok(chip) = Chip::from_str(mcu) {
5053
if !supported_chips.iter().any(|sc| *sc == chip) {
5154
bail!(
52-
"Specified MCU '{chip}' is not amongst the MCUs ([{}]) supported by the build target ('{target}')",
55+
"Specified MCU '{chip}' is not amongst the MCUs ([{}]) supported by the build target ('{target}')",
5356
supported_chips.iter().map(|chip| format!("{chip}")).collect::<Vec<_>>().join(", ")
5457
);
5558
}
@@ -82,13 +85,13 @@ pub fn build() -> Result<EspIdfBuildOutput> {
8285
let cmake_generator = config.native.esp_idf_cmake_generator();
8386

8487
// A closure to specify which tools `idf-tools.py` should install.
85-
let make_tools = move |repo: &git::Repository,
88+
let make_tools = move |tree: &SourceTree,
8689
version: &Result<espidf::EspIdfVersion>|
8790
-> Result<Vec<espidf::Tools>> {
8891
eprintln!(
8992
"Using esp-idf {} at '{}'",
9093
espidf::EspIdfVersion::format(version),
91-
repo.worktree().display()
94+
tree.path().display(),
9295
);
9396

9497
let mut tools = vec![];
@@ -127,38 +130,37 @@ pub fn build() -> Result<EspIdfBuildOutput> {
127130
// we're using msys/cygwin.
128131
// But this variable is also present when using git-bash.
129132
env::remove_var("MSYSTEM");
130-
131133
// Install the esp-idf and its tools.
132134
let (idf, tools_install_dir) = {
133135
// Get the install dir location from the build config, or use
134136
// [`crate::config::DEFAULT_TOOLS_INSTALL_DIR`] if unset.
135-
let (install_dir, is_default_install_dir) = config.esp_idf_tools_install_dir()?;
137+
let (idf_tools_install_dir, is_default_install_dir) = config.esp_idf_tools_install_dir()?;
136138
// EspIdf must come from the environment if `esp_idf_tools_install_dir` == `fromenv`.
137-
let require_from_env = install_dir.is_from_env();
139+
let require_from_env = idf_tools_install_dir.is_from_env();
138140
let maybe_from_env = require_from_env || is_default_install_dir;
139141

140142
// Closure to install the esp-idf using `embuild::espidf::Installer`.
141143
let install = |esp_idf_origin: EspIdfOrigin| -> Result<(espidf::EspIdf, InstallDir)> {
142144
match &esp_idf_origin {
143145
EspIdfOrigin::Custom(repo) => {
144146
eprintln!(
145-
"Using custom user-supplied esp-idf repository at '{}' (detected from env variable `{}`)",
146-
repo.worktree().display(),
147-
espidf::IDF_PATH_VAR
148-
);
147+
"Using custom user-supplied esp-idf repository at '{}' (detected from env variable `{}`)",
148+
repo.path().display(),
149+
espidf::IDF_PATH_VAR
150+
);
149151
if let Some(custom_url) = &config.native.esp_idf_repository {
150152
cargo::print_warning(format_args!(
151-
"Ignoring configuration setting `{ESP_IDF_REPOSITORY_VAR}=\"{custom_url}\"`: \
152-
custom esp-idf repository detected via ${}",
153-
espidf::IDF_PATH_VAR
154-
));
153+
"Ignoring configuration setting `{ESP_IDF_REPOSITORY_VAR}=\"{custom_url}\"`: \
154+
custom esp-idf repository detected via ${}",
155+
espidf::IDF_PATH_VAR
156+
));
155157
}
156158
if let Some(custom_version) = &config.native.esp_idf_version {
157159
cargo::print_warning(format_args!(
158-
"Ignoring configuration setting `{ESP_IDF_VERSION_VAR}` ({custom_version}): \
159-
custom esp-idf repository detected via ${}",
160-
espidf::IDF_PATH_VAR
161-
));
160+
"Ignoring configuration setting `{ESP_IDF_VERSION_VAR}` ({custom_version}): \
161+
custom esp-idf repository detected via ${}",
162+
espidf::IDF_PATH_VAR
163+
));
162164
}
163165
}
164166
EspIdfOrigin::Managed(remote) => {
@@ -167,13 +169,21 @@ pub fn build() -> Result<EspIdfBuildOutput> {
167169
};
168170

169171
let idf = espidf::Installer::new(esp_idf_origin)
170-
.install_dir(install_dir.path().map(Into::into))
172+
.install_dir(idf_tools_install_dir.path().map(Into::into))
171173
.with_tools(make_tools)
172174
.install()
173175
.context("Could not install esp-idf")?;
174-
Ok((idf, install_dir.clone()))
176+
Ok((idf, idf_tools_install_dir.clone()))
175177
};
176178

179+
// TODO: This is a bit of a mess.
180+
// We should probably refactor this and the lines below to make it more readable.
181+
//
182+
// For one, it is still unclear to me when this path should be used and when not.
183+
// It seems an option is to also completely retire specifying the IDF PATH in the cargo-metadata config,
184+
// see: https://github.com/esp-rs/esp-idf-sys/pull/353#issuecomment-2543179482
185+
let idf_path = config.native.idf_path.as_deref();
186+
177187
// 1. Try to use the activated esp-idf environment if `esp_idf_tools_install_dir`
178188
// is `fromenv` or unset.
179189
// 2. Use a custom esp-idf repository specified by `$IDF_PATH`/`idf_path` if
@@ -185,29 +195,29 @@ pub fn build() -> Result<EspIdfBuildOutput> {
185195
eprintln!(
186196
"Using activated esp-idf {} environment at '{}'",
187197
espidf::EspIdfVersion::format(&idf.version),
188-
idf.repository.worktree().display()
198+
idf.esp_idf_dir.path().display()
189199
);
190200

191201
(idf, InstallDir::FromEnv)
192202
},
193203
(Ok(idf), false) => {
194-
cargo::print_warning(format_args!(
195-
"Ignoring activated esp-idf environment: {ESP_IDF_TOOLS_INSTALL_DIR_VAR} != {}", InstallDir::FromEnv
196-
));
197-
install(EspIdfOrigin::Custom(idf.repository))?
204+
cargo::print_warning(format_args!(
205+
"Ignoring activated esp-idf environment: {ESP_IDF_TOOLS_INSTALL_DIR_VAR} != {}", InstallDir::FromEnv
206+
));
207+
install(EspIdfOrigin::Custom(idf.esp_idf_dir))?
198208
},
199-
(Err(FromEnvError::NotActivated { source: err, .. }), true) |
209+
(Err(FromEnvError::NotActivated(NotActivatedError { source: err, .. })), true) |
200210
(Err(FromEnvError::NoRepo(err)), true) if require_from_env => {
201211
return Err(err.context(
202-
format!("activated esp-idf environment not found but required by {ESP_IDF_TOOLS_INSTALL_DIR_VAR} == {install_dir}")
212+
format!("activated esp-idf environment not found but required by {ESP_IDF_TOOLS_INSTALL_DIR_VAR} == {idf_tools_install_dir}")
203213
))
204214
}
205-
(Err(FromEnvError::NotActivated { esp_idf_repo, .. }), _) => {
206-
install(EspIdfOrigin::Custom(esp_idf_repo))?
215+
(Err(FromEnvError::NotActivated(NotActivatedError { esp_idf_dir, .. })), _) => {
216+
install(EspIdfOrigin::Custom(esp_idf_dir))?
207217
},
208218
(Err(FromEnvError::NoRepo(_)), _) => {
209-
let origin = match &config.native.idf_path {
210-
Some(idf_path) => EspIdfOrigin::Custom(git::Repository::open(idf_path)?),
219+
let origin = match idf_path {
220+
Some(idf_path) => EspIdfOrigin::Custom(SourceTree::Plain(idf_path.to_path_buf())),
211221
None => EspIdfOrigin::Managed(EspIdfRemote {
212222
git_ref: config.native.esp_idf_version(),
213223
repo_url: config.native.esp_idf_repository.clone()
@@ -284,9 +294,13 @@ pub fn build() -> Result<EspIdfBuildOutput> {
284294
let patch_set = match idf.version.as_ref().map(|v| (v.major, v.minor, v.patch)) {
285295
// master branch
286296
_ if {
287-
let default_branch = idf.repository.get_default_branch()?;
288-
let curr_branch = idf.repository.get_branch_name()?;
289-
default_branch == curr_branch && default_branch.is_some()
297+
if let SourceTree::Git(repository) = &idf.esp_idf_dir {
298+
let default_branch = repository.get_default_branch()?;
299+
let curr_branch = repository.get_branch_name()?;
300+
default_branch == curr_branch && default_branch.is_some()
301+
} else {
302+
false
303+
}
290304
} =>
291305
{
292306
cargo::print_warning(
@@ -313,10 +327,11 @@ pub fn build() -> Result<EspIdfBuildOutput> {
313327
}
314328
};
315329

316-
// Apply patches, only if the patches were not previously applied and if the esp-idf repo is managed.
330+
// Apply patches, only if the patches were not previously applied and if the esp-idf dir is a managed GIT repo.
317331
if idf.is_managed_espidf && !patch_set.is_empty() {
318-
idf.repository
319-
.apply_once(patch_set.iter().map(|p| manifest_dir.join(p)))?;
332+
if let SourceTree::Git(repository) = &idf.esp_idf_dir {
333+
repository.apply_once(patch_set.iter().map(|p| manifest_dir.join(p)))?;
334+
}
320335
}
321336

322337
// "PATH" is anyway passed to the CMake generator, but if we don't set it here, we get the following warnings from CMake:
@@ -427,7 +442,7 @@ pub fn build() -> Result<EspIdfBuildOutput> {
427442
)?;
428443

429444
let cmake_toolchain_file = path_buf![
430-
&idf.repository.worktree(),
445+
&idf.esp_idf_dir.path(),
431446
"tools",
432447
"cmake",
433448
chip.cmake_toolchain_file()
@@ -443,7 +458,7 @@ pub fn build() -> Result<EspIdfBuildOutput> {
443458
cmake::cmake(),
444459
"-P",
445460
extractor_script.as_ref().as_os_str();
446-
env=("IDF_PATH", &idf.repository.worktree().as_os_str()))
461+
env=("IDF_PATH", idf.esp_idf_dir.path()))
447462
.stdout()?;
448463

449464
let mut vars = cmake::process_script_variables_extractor_output(output)?;
@@ -484,7 +499,7 @@ pub fn build() -> Result<EspIdfBuildOutput> {
484499
.cxxflag(cxx_flags)
485500
.env("IDF_COMPONENT_MANAGER", idf_comp_manager)
486501
.env("EXTRA_COMPONENT_DIRS", extra_component_dirs)
487-
.env("IDF_PATH", idf.repository.worktree())
502+
.env("IDF_PATH", idf.esp_idf_dir.path())
488503
.env("PATH", &idf.exported_path)
489504
.env("SDKCONFIG_DEFAULTS", defaults_files)
490505
.env("IDF_TARGET", &chip_name)
@@ -540,7 +555,7 @@ pub fn build() -> Result<EspIdfBuildOutput> {
540555
.context("Could not determine the compiler from cmake")?;
541556

542557
let build_info = espidf::EspIdfBuildInfo {
543-
esp_idf_dir: idf.repository.worktree().to_owned(),
558+
esp_idf_dir: idf.esp_idf_dir.path().to_owned(),
544559
exported_path_var: idf.exported_path.try_to_str()?.to_owned(),
545560
venv_python: idf.venv_python,
546561
build_dir: cmake_build_dir.clone(),

0 commit comments

Comments
 (0)