feat(cmakev2): Added conditional_component example for cmakev2

This commit adds a new example at
examples/build_system/cmakev2/features/conditional_components to
demonstrate the cmakev2 ability to conditionally include components in
the build based on Kconfig options.
This commit is contained in:
Sudeep Mohanty
2026-02-24 11:21:20 +01:00
parent 249f3b167f
commit 390ce6a832
12 changed files with 314 additions and 0 deletions
@@ -0,0 +1,21 @@
# This component provides the main function and conditionally
# includes logging_util and math_util based on the Kconfig options.
# Provide the main component as a static library
add_library(${COMPONENT_TARGET} STATIC main.c)
if(CONFIG_EXAMPLE_ENABLE_LOGGING)
idf_component_include(logging_util)
target_link_libraries(${COMPONENT_TARGET} PRIVATE idf::logging_util)
idf_msg("Conditional component 'logging_util' is ENABLED")
else()
idf_msg("Conditional component 'logging_util' is DISABLED")
endif()
if(CONFIG_EXAMPLE_ENABLE_MATH)
idf_component_include(math_util)
target_link_libraries(${COMPONENT_TARGET} PRIVATE idf::math_util)
idf_msg("Conditional component 'math_util' is ENABLED")
else()
idf_msg("Conditional component 'math_util' is DISABLED")
endif()
@@ -0,0 +1,17 @@
menu "Conditional Component Example Configuration"
config EXAMPLE_ENABLE_LOGGING
bool "Enable Logging Utility Component"
default y
help
Enable the logging_util component to demonstrate conditional
component inclusion in Build System v2.
config EXAMPLE_ENABLE_MATH
bool "Enable Math Utility Component"
default y
help
Enable the math_util component to demonstrate conditional
component inclusion in Build System v2.
endmenu
@@ -0,0 +1,49 @@
/*
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include "sdkconfig.h"
#ifdef CONFIG_EXAMPLE_ENABLE_LOGGING
#include "logging_util.h"
#endif
#ifdef CONFIG_EXAMPLE_ENABLE_MATH
#include "math_util.h"
#endif
void app_main(void)
{
printf("Conditional Component Example\n");
printf("==============================\n\n");
printf("This example demonstrates Build System v2's idf_component_include()\n");
printf("function for conditional component inclusion.\n\n");
#ifdef CONFIG_EXAMPLE_ENABLE_LOGGING
printf("Logging component is ENABLED\n");
log_info("This is an info message from logging_util");
log_warn("This is a warning message from logging_util");
#else
printf("Logging component is DISABLED\n");
#endif
printf("\n");
#ifdef CONFIG_EXAMPLE_ENABLE_MATH
printf("Math component is ENABLED\n");
int a = 10, b = 5;
printf(" add(%d, %d) = %d\n", a, b, math_add(a, b));
printf(" subtract(%d, %d) = %d\n", a, b, math_subtract(a, b));
printf(" multiply(%d, %d) = %d\n", a, b, math_multiply(a, b));
#else
printf("Math component is DISABLED\n");
#endif
printf("\n");
printf("You can change the component configuration via menuconfig:\n");
printf(" idf.py menuconfig -> Conditional Component Example Configuration\n");
}