From f6a852e1cf8c9bbb7e581bd2f4c5941be8e133e5 Mon Sep 17 00:00:00 2001 From: gojimmypi Date: Thu, 20 Jun 2024 14:32:38 +0200 Subject: [PATCH] fix(cmake): check for missing component_target Closes https://github.com/espressif/esp-idf/pull/14036 [Frantisek Hrbata: fixed spelling not related to this change so spellcheck succeeds] [Frantisek Hrbata: modified the error message to be recognized by the existing hint] [Frantisek Hrbata: added also check in idf_component_set_property and simple tests] Signed-off-by: Frantisek Hrbata --- tools/cmake/component.cmake | 15 +++++++++++---- tools/test_build_system/test_components.py | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/tools/cmake/component.cmake b/tools/cmake/component.cmake index 1046364aca..18bab3b386 100644 --- a/tools/cmake/component.cmake +++ b/tools/cmake/component.cmake @@ -44,7 +44,7 @@ function(__component_get_target var name_or_alias) idf_build_get_property(component_targets __COMPONENT_TARGETS) - # Assume first that the paramters is an alias. + # Assume first that the parameters is an alias. string(REPLACE "::" "_" name_or_alias "${name_or_alias}") set(component_target ___${name_or_alias}) @@ -376,10 +376,14 @@ endmacro() function(idf_component_get_property var component property) cmake_parse_arguments(_ "GENERATOR_EXPRESSION" "" "" ${ARGN}) __component_get_target(component_target ${component}) - if(__GENERATOR_EXPRESSION) - set(val "$") + if("${component_target}" STREQUAL "") + message(FATAL_ERROR "Failed to resolve component '${component}'") else() - __component_get_property(val ${component_target} ${property}) + if(__GENERATOR_EXPRESSION) + set(val "$") + else() + __component_get_property(val ${component_target} ${property}) + endif() endif() set(${var} "${val}" PARENT_SCOPE) endfunction() @@ -398,6 +402,9 @@ endfunction() function(idf_component_set_property component property val) cmake_parse_arguments(_ "APPEND" "" "" ${ARGN}) __component_get_target(component_target ${component}) + if(NOT component_target) + message(FATAL_ERROR "Failed to resolve component '${component}'") + endif() if(__APPEND) __component_set_property(${component_target} ${property} "${val}" APPEND) diff --git a/tools/test_build_system/test_components.py b/tools/test_build_system/test_components.py index 79bea5bda6..2afc5c5288 100644 --- a/tools/test_build_system/test_components.py +++ b/tools/test_build_system/test_components.py @@ -87,6 +87,24 @@ def test_component_properties_are_set(idf_py: IdfPyFunc, test_app_copy: Path) -> assert 'SRCS:{}'.format((test_app_copy / 'main' / 'build_test_app.c').as_posix()) in ret.stdout, 'Component properties should be set' +def test_get_property_for_unknown_component(idf_py: IdfPyFunc, test_app_copy: Path) -> None: + logging.info('Getting property of unknown component fails gracefully') + append_to_file(test_app_copy / 'CMakeLists.txt', '\n'.join(['', + 'idf_component_get_property(VAR UNKNOWN PROP)'])) + ret = idf_py('reconfigure', check=False) + assert "Failed to resolve component 'UNKNOWN'" in ret.stderr, ('idf_component_get_property ' + 'for unknown component should fail gracefully') + + +def test_set_property_for_unknown_component(idf_py: IdfPyFunc, test_app_copy: Path) -> None: + logging.info('Setting property of unknown component fails gracefully') + append_to_file(test_app_copy / 'CMakeLists.txt', '\n'.join(['', + 'idf_component_set_property(UNKNOWN PROP VAL)'])) + ret = idf_py('reconfigure', check=False) + assert "Failed to resolve component 'UNKNOWN'" in ret.stderr, ('idf_component_set_property ' + 'for unknown component should fail gracefully') + + def test_component_overridden_dir(idf_py: IdfPyFunc, test_app_copy: Path, default_idf_env: EnvDict) -> None: logging.info('Getting component overridden dir') (test_app_copy / 'components' / 'hal').mkdir(parents=True)