Skip to content

Commit

Permalink
fix(cmake): check for missing component_target
Browse files Browse the repository at this point in the history
Closes #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 <frantisek.hrbata@espressif.com>
  • Loading branch information
gojimmypi authored and fhrbata committed Jul 11, 2024
1 parent 5448703 commit f6a852e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
15 changes: 11 additions & 4 deletions tools/cmake/component.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down Expand Up @@ -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 "$<TARGET_PROPERTY:${component_target},${property}>")
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 "$<TARGET_PROPERTY:${component_target},${property}>")
else()
__component_get_property(val ${component_target} ${property})
endif()
endif()
set(${var} "${val}" PARENT_SCOPE)
endfunction()
Expand All @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions tools/test_build_system/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f6a852e

Please sign in to comment.