From 9b731e5e356f6e453df5d80a4ee2843c7464fe05 Mon Sep 17 00:00:00 2001 From: Frantisek Hrbata Date: Tue, 28 Mar 2023 11:18:04 +0200 Subject: [PATCH] tools: fix set-target if IDF_TARGET env is set and stale sdkconfig exists Currently if the IDF_TARGET env is set, and old sdkconfig exists with different target value in CONFIG_IDF_TARGET, the set-target action fails complaining about the IDF_TARGET env and value in sdkconfig being different. We should ignore IDF_TARGET value from sdkconfig, because we are actually setting new target and the old sdkconfig is renamed in cmake. This can be easily reproduced with ---8<--- $ IDF_TARGET=esp32 idf.py set-target esp32 $ IDF_TARGET=esp32s3 idf.py set-target esp32s3 Project sdkconfig '/home/fhrbata/work/hello_world/sdkconfig' was generated for target 'esp32s3', but environment variable IDF_TARGET is set to 'esp32'. Run 'idf.py set-target esp32' to generate new sdkconfig file for target esp32. ---8<--- This also adds test for this use case to test_non_default_target.py. Signed-off-by: Frantisek Hrbata --- tools/idf_py_actions/tools.py | 11 +++++++++-- tools/test_build_system/test_non_default_target.py | 8 +++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/tools/idf_py_actions/tools.py b/tools/idf_py_actions/tools.py index f2c546f8fa..773c540c11 100644 --- a/tools/idf_py_actions/tools.py +++ b/tools/idf_py_actions/tools.py @@ -459,7 +459,7 @@ def ensure_build_directory(args: 'PropertyDict', prog_name: str, always_run_cmak cache_cmdl = _parse_cmdl_cmakecache(args.define_cache_entry) # Validate IDF_TARGET - _check_idf_target(args, prog_name, cache, cache_cmdl) + _check_idf_target(args, prog_name, cache, cache_cmdl, env) if always_run_cmake or _new_cmakecache_entries(cache, cache_cmdl): if args.generator is None: @@ -582,7 +582,8 @@ def is_target_supported(project_path: str, supported_targets: List) -> bool: return get_target(project_path) in supported_targets -def _check_idf_target(args: 'PropertyDict', prog_name: str, cache: Dict, cache_cmdl: Dict) -> None: +def _check_idf_target(args: 'PropertyDict', prog_name: str, cache: Dict, + cache_cmdl: Dict, env: Dict=None) -> None: """ Cross-check the three settings (sdkconfig, CMakeCache, environment) and if there is mismatch, fail with instructions on how to fix this. @@ -593,6 +594,12 @@ def _check_idf_target(args: 'PropertyDict', prog_name: str, cache: Dict, cache_c idf_target_from_cache = cache.get('IDF_TARGET') idf_target_from_cache_cmdl = cache_cmdl.get('IDF_TARGET') + # Called from set-target action. The original sdkconfig will be renamed + # in cmake, so ignore any CONFIG_IDF_TARGET which may be defined in + # stale sdkconfig. + if env and env.get('_IDF_PY_SET_TARGET_ACTION') == '1': + idf_target_from_sdkconfig = None + if idf_target_from_env: # Let's check that IDF_TARGET values are consistent if idf_target_from_sdkconfig and idf_target_from_sdkconfig != idf_target_from_env: diff --git a/tools/test_build_system/test_non_default_target.py b/tools/test_build_system/test_non_default_target.py index d7a3988b88..2655c65f20 100644 --- a/tools/test_build_system/test_non_default_target.py +++ b/tools/test_build_system/test_non_default_target.py @@ -140,7 +140,7 @@ def test_target_using_settarget_parameter_alternative_name(idf_py: IdfPyFunc) -> @pytest.mark.usefixtures('test_app_copy') -def test_target_using_settarget_parameter(idf_py: IdfPyFunc) -> None: +def test_target_using_settarget_parameter(idf_py: IdfPyFunc, default_idf_env: EnvDict) -> None: logging.info('Can set target using idf.py set-target') idf_py('set-target', ESP32S2_TARGET) check_file_contains('sdkconfig', 'CONFIG_IDF_TARGET="{}"'.format(ESP32S2_TARGET)) @@ -152,6 +152,12 @@ def test_target_using_settarget_parameter(idf_py: IdfPyFunc) -> None: check_file_contains('sdkconfig', 'CONFIG_IDF_TARGET="{}"'.format(ESP32S2_TARGET)) check_file_contains('build/CMakeCache.txt', 'IDF_TARGET:STRING={}'.format(ESP32S2_TARGET)) + logging.info('Can set target if IDF_TARGET env is set and old sdkconfig exists') + default_idf_env.update({'IDF_TARGET': ESP32_TARGET}) + idf_py('set-target', ESP32_TARGET) + default_idf_env.pop('IDF_TARGET') + check_file_contains('sdkconfig', 'CONFIG_IDF_TARGET="{}"'.format(ESP32_TARGET)) + def test_target_using_sdkconfig(idf_py: IdfPyFunc, test_app_copy: Path) -> None: logging.info('Can set the default target using sdkconfig.defaults')