Skip to content

Commit

Permalink
Fix generating bindings for extra components with pio build
Browse files Browse the repository at this point in the history
  • Loading branch information
N3xed committed Aug 2, 2022
1 parent 456f986 commit 235e507
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 35 deletions.
12 changes: 7 additions & 5 deletions build/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#[cfg(not(any(feature = "pio", feature = "native")))]
compile_error!("One of the features `pio` or `native` must be selected.");

use std::fs;
use std::io::{BufWriter, Write};
use std::iter::once;

use anyhow::*;
Expand Down Expand Up @@ -130,9 +127,11 @@ fn main() -> anyhow::Result<()> {
bindings_file.display()
)
};

#[allow(unused_mut)]
let mut headers = vec![header_file];

#[cfg(feature = "native")]
#[cfg(all(feature = "native", not(feature = "pio")))]
// Add additional headers from extra components.
headers.extend(
build_output
Expand All @@ -151,8 +150,11 @@ fn main() -> anyhow::Result<()> {
.with_context(bindgen_err)?;

// Generate bindings separately for each unique module name.
#[cfg(feature = "native")]
#[cfg(all(feature = "native", not(feature = "pio")))]
(|| {
use std::fs;
use std::io::{BufWriter, Write};

let mut output_file =
BufWriter::new(fs::File::options().append(true).open(&bindings_file)?);

Expand Down
27 changes: 1 addition & 26 deletions build/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl EspIdfComponents {
Ok(Self::new(components))
}

#[allow(dead_code)]
pub fn from(enabled: impl IntoIterator<Item = impl Into<String>>) -> Self {
// NOTE: The components which are always enabled by ESP-IDF's CMake build (for ESP-IDF V4.4) are as follows:
// cxx; newlib; freertos; esp_hw_support; heap; log; lwip; soc; hal; esp_rom; esp_common; esp_system;
Expand Down Expand Up @@ -339,29 +340,3 @@ pub fn manifest_dir() -> Result<PathBuf> {
})
.map(PathBuf::from)
}

/// Create a cmake list (`;`-separated strings), escape all `;` and on Windows make sure
/// paths don't contain `\`.
pub fn to_cmake_path_list(iter: impl IntoIterator<Item = impl AsRef<OsStr>>) -> Result<String> {
let mut accu = String::new();
for p in iter {
let p: &str = p.as_ref().try_to_str()?;
if !accu.is_empty() {
accu.push(';');
}

// Escape all `;` since cmake uses them as separators.
let p = p.replace(';', "\\;");

accu.push_str(
// Windows uses `\` as directory separators which cmake can't deal with, so we
// convert all back-slashes to forward-slashes here.
&if cfg!(windows) {
p.replace('\\', "/")
} else {
p
},
);
}
Ok(accu)
}
4 changes: 2 additions & 2 deletions build/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl BuildConfig {
pub fn try_from_env() -> Result<BuildConfig> {
let cfg: BuildConfig = utils::parse_from_env(&[])?;

#[cfg(feature = "native")]
#[cfg(all(feature = "native", not(feature = "pio")))]
let cfg = {
use crate::native::cargo_driver::config::NativeConfig;
BuildConfig {
Expand Down Expand Up @@ -142,7 +142,7 @@ impl BuildConfig {
);
utils::set_when_none(&mut self.mcu, mcu);

#[cfg(feature = "native")]
#[cfg(all(feature = "native", not(feature = "pio")))]
self.native.with_cargo_metadata(root_package, &metadata)?;

Ok(())
Expand Down
31 changes: 29 additions & 2 deletions build/native/cargo_driver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::convert::TryFrom;
use std::ffi::OsStr;
use std::path::Path;
use std::str::FromStr;
use std::{env, fs};
Expand All @@ -15,8 +16,8 @@ use embuild::{bindgen, build, cargo, cmake, espidf, git, kconfig, path_buf};

use self::chip::Chip;
use crate::common::{
self, list_specific_sdkconfigs, manifest_dir, to_cmake_path_list, workspace_dir,
EspIdfBuildOutput, EspIdfComponents, InstallDir, V_4_3_2_PATCHES,
self, list_specific_sdkconfigs, manifest_dir, workspace_dir, EspIdfBuildOutput,
EspIdfComponents, InstallDir, V_4_3_2_PATCHES,
};
use crate::config::{BuildConfig, ESP_IDF_GLOB_VAR_PREFIX, ESP_IDF_TOOLS_INSTALL_DIR_VAR};

Expand Down Expand Up @@ -431,3 +432,29 @@ fn generate_sdkconfig_defaults() -> Result<String> {
.map(|(i, s)| format!("{}={}\n", s, if i == opt_index { 'y' } else { 'n' }))
.collect::<String>())
}

/// Create a cmake list (`;`-separated strings), escape all `;` and on Windows make sure
/// paths don't contain `\`.
pub fn to_cmake_path_list(iter: impl IntoIterator<Item = impl AsRef<OsStr>>) -> Result<String> {
let mut accu = String::new();
for p in iter {
let p: &str = p.as_ref().try_to_str()?;
if !accu.is_empty() {
accu.push(';');
}

// Escape all `;` since cmake uses them as separators.
let p = p.replace(';', "\\;");

accu.push_str(
// Windows uses `\` as directory separators which cmake can't deal with, so we
// convert all back-slashes to forward-slashes here.
&if cfg!(windows) {
p.replace('\\', "/")
} else {
p
},
);
}
Ok(accu)
}

0 comments on commit 235e507

Please sign in to comment.