Skip to content

Commit

Permalink
Allow to specify which components are built
Browse files Browse the repository at this point in the history
Add `$ESP_IDF_COMPONENTS` and `esp_idf_components` configuration option that allow
to specify a list of esp-idf components to build. This list can be used to trim down the esp-idf
build and reduce compile time.
Only implemented for the native build driver.
Fixes #83
  • Loading branch information
N3xed committed Aug 2, 2022
1 parent 62b655f commit 3a955f4
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,19 @@ The following configuration options are available:
> ⚠️
> [Older ESP-IDF versions might not support all MCUs from above.](https://github.com/espressif/esp-idf#esp-idf-release-and-soc-compatibility)
- ### *`esp_idf_components`*, `$ESP_IDF_COMPONENTS` (*native* builder only)

The list of esp-idf components (names) that should be built. This list is used to
trim the esp-idf build. Any component that is a dependency of a component in this
list will also automatically be built.

Defaults to all components being built.

> 🛈 **Note**
> Some components must be explicitly enabled in the sdkconfig.
> [Extra components](#extra-esp-idf-components) must also be added to this list if
> they are to be built.
### Example

An example of the `[package.metadata.esp-idf-sys]` section of the `Cargo.toml`.
Expand Down
19 changes: 10 additions & 9 deletions build/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct BuildConfig {
esp_idf_sdkconfig: Option<PathBuf>,

/// One or more paths to sdkconfig.defaults files used by the esp-idf.
#[serde(deserialize_with = "parse::sdkconfig_defaults")]
#[serde(deserialize_with = "parse::list")]
esp_idf_sdkconfig_defaults: Option<Vec<PathBuf>>,

/// The MCU (esp32, esp32s2, esp32s3, esp32c3, ...) to compile for if unset will be
Expand Down Expand Up @@ -173,21 +173,22 @@ where
}
}

mod parse {
use std::path::PathBuf;

pub mod parse {
use serde::{Deserialize, Deserializer};

use super::utils::ValueOrVec;

pub fn sdkconfig_defaults<'d, D: Deserializer<'d>>(
de: D,
) -> Result<Option<Vec<PathBuf>>, D::Error> {
Option::<ValueOrVec<String, PathBuf>>::deserialize(de).map(|val| match val {
/// Parse a string into a `;`-separated list of `T`s or parse a list of `T`s directly.
pub fn list<'d, T, D>(de: D) -> Result<Option<Vec<T>>, D::Error>
where
D: Deserializer<'d>,
T: for<'s> From<&'s str> + Deserialize<'d>,
{
Option::<ValueOrVec<String, T>>::deserialize(de).map(|val| match val {
Some(ValueOrVec::Val(s)) => Some(
s.split(';')
.filter(|s| !s.is_empty())
.map(PathBuf::from)
.map(Into::into)
.collect(),
),
Some(ValueOrVec::Vec(v)) => Some(v),
Expand Down
7 changes: 7 additions & 0 deletions build/native/cargo_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ pub fn build() -> Result<EspIdfBuildOutput> {
cmake_config.env("IDF_TOOLS_PATH", install_dir);
}

// specify the components that should be built
if let Some(components) = &config.native.esp_idf_components {
cmake_config.env("COMPONENTS", components.join(";"));
}

// Build the esp-idf.
cmake_config.build();

Expand Down Expand Up @@ -373,6 +378,8 @@ pub fn build() -> Result<EspIdfBuildOutput> {
})
.collect::<Vec<_>>();

eprintln!("Built components: {}", components.join(", "));

let sdkconfig_json = path_buf![&cmake_build_dir, "config", "sdkconfig.json"];
let build_output = EspIdfBuildOutput {
cincl_args: build::CInclArgs::try_from(&target.compile_groups[0])?,
Expand Down
14 changes: 14 additions & 0 deletions build/native/cargo_driver/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,19 @@ pub struct NativeConfig {
///
/// Can be specified in the root crate's `package.metadata.esp-idf-sys` and all direct
/// dependencies'.
///
/// This option is not available as an environment variable.
#[serde(alias = "extra-components")]
pub extra_components: Vec<ExtraComponent>,

/// A list of esp-idf components (names) that should be built. This list is used to
/// trim the esp-idf build. Any component that is a dependency of a component in this
/// list will also automatically be built.
///
/// If this option is not specified, all components will be built. Note though that
/// some components must be explicitly enabled in the sdkconfig.
#[serde(default, deserialize_with = "parse::list")]
pub esp_idf_components: Option<Vec<String>>,
}

impl NativeConfig {
Expand Down Expand Up @@ -181,13 +192,15 @@ impl NativeConfig {
esp_idf_cmake_generator,
idf_path,
extra_components,
esp_idf_components,
},
} = EspIdfSys::deserialize(&root.metadata)?;

set_when_none(&mut self.esp_idf_version, esp_idf_version);
set_when_none(&mut self.esp_idf_repository, esp_idf_repository);
set_when_none(&mut self.esp_idf_cmake_generator, esp_idf_cmake_generator);
set_when_none(&mut self.idf_path, idf_path);
set_when_none(&mut self.esp_idf_components, esp_idf_components);

fn make_processor(
package: &Package,
Expand Down Expand Up @@ -317,6 +330,7 @@ mod parse {
use strum::IntoEnumIterator;

use super::*;
pub use crate::config::parse::*;
use crate::config::utils::ValueOrVec;

/// Parse a cmake generator, either `default` or one of [`cmake::Generator`].
Expand Down
7 changes: 6 additions & 1 deletion resources/cmake_project/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ foreach(component_dir IN ITEMS $ENV{EXTRA_COMPONENT_DIRS})
idf_build_component(${component_dir})
endforeach()

idf_build_process($ENV{IDF_TARGET} SDKCONFIG $ENV{SDKCONFIG} SDKCONFIG_DEFAULTS $ENV{SDKCONFIG_DEFAULTS})
if(DEFINED ENV{COMPONENTS})
idf_build_process($ENV{IDF_TARGET} SDKCONFIG $ENV{SDKCONFIG} SDKCONFIG_DEFAULTS $ENV{SDKCONFIG_DEFAULTS} COMPONENTS $ENV{COMPONENTS})
else()
idf_build_process($ENV{IDF_TARGET} SDKCONFIG $ENV{SDKCONFIG} SDKCONFIG_DEFAULTS $ENV{SDKCONFIG_DEFAULTS})
endif()

idf_build_get_property(aliases BUILD_COMPONENT_ALIASES)

add_executable(libespidf.elf main.c)
Expand Down

0 comments on commit 3a955f4

Please sign in to comment.