From 3a955f4a6506cc28aa5c51524da7441839565a4a Mon Sep 17 00:00:00 2001 From: N3xed Date: Tue, 2 Aug 2022 22:03:18 +0200 Subject: [PATCH] Allow to specify which components are built 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 --- README.md | 13 +++++++++++++ build/config.rs | 19 ++++++++++--------- build/native/cargo_driver.rs | 7 +++++++ build/native/cargo_driver/config.rs | 14 ++++++++++++++ resources/cmake_project/CMakeLists.txt | 7 ++++++- 5 files changed, 50 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 3317fa99159..756b780bd06 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/build/config.rs b/build/config.rs index 126a5ebc02d..6b081599906 100644 --- a/build/config.rs +++ b/build/config.rs @@ -23,7 +23,7 @@ pub struct BuildConfig { esp_idf_sdkconfig: Option, /// 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>, /// The MCU (esp32, esp32s2, esp32s3, esp32c3, ...) to compile for if unset will be @@ -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>, D::Error> { - Option::>::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>, D::Error> + where + D: Deserializer<'d>, + T: for<'s> From<&'s str> + Deserialize<'d>, + { + Option::>::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), diff --git a/build/native/cargo_driver.rs b/build/native/cargo_driver.rs index c00eebaea15..fb8e777af93 100644 --- a/build/native/cargo_driver.rs +++ b/build/native/cargo_driver.rs @@ -317,6 +317,11 @@ pub fn build() -> Result { 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(); @@ -373,6 +378,8 @@ pub fn build() -> Result { }) .collect::>(); + 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])?, diff --git a/build/native/cargo_driver/config.rs b/build/native/cargo_driver/config.rs index ac2c4b3d429..62870bf6ab3 100644 --- a/build/native/cargo_driver/config.rs +++ b/build/native/cargo_driver/config.rs @@ -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, + + /// 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>, } impl NativeConfig { @@ -181,6 +192,7 @@ impl NativeConfig { esp_idf_cmake_generator, idf_path, extra_components, + esp_idf_components, }, } = EspIdfSys::deserialize(&root.metadata)?; @@ -188,6 +200,7 @@ impl NativeConfig { 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, @@ -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`]. diff --git a/resources/cmake_project/CMakeLists.txt b/resources/cmake_project/CMakeLists.txt index cb02dd545af..70e1c285db4 100644 --- a/resources/cmake_project/CMakeLists.txt +++ b/resources/cmake_project/CMakeLists.txt @@ -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)