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 <frantisek.hrbata@espressif.com>
This commit is contained in:
Frantisek Hrbata 2023-03-28 11:18:04 +02:00
parent a32699c2a7
commit 9b731e5e35
2 changed files with 16 additions and 3 deletions

View File

@ -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) cache_cmdl = _parse_cmdl_cmakecache(args.define_cache_entry)
# Validate IDF_TARGET # 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 always_run_cmake or _new_cmakecache_entries(cache, cache_cmdl):
if args.generator is None: 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 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 Cross-check the three settings (sdkconfig, CMakeCache, environment) and if there is
mismatch, fail with instructions on how to fix this. 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 = cache.get('IDF_TARGET')
idf_target_from_cache_cmdl = cache_cmdl.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: if idf_target_from_env:
# Let's check that IDF_TARGET values are consistent # Let's check that IDF_TARGET values are consistent
if idf_target_from_sdkconfig and idf_target_from_sdkconfig != idf_target_from_env: if idf_target_from_sdkconfig and idf_target_from_sdkconfig != idf_target_from_env:

View File

@ -140,7 +140,7 @@ def test_target_using_settarget_parameter_alternative_name(idf_py: IdfPyFunc) ->
@pytest.mark.usefixtures('test_app_copy') @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') logging.info('Can set target using idf.py set-target')
idf_py('set-target', ESP32S2_TARGET) idf_py('set-target', ESP32S2_TARGET)
check_file_contains('sdkconfig', 'CONFIG_IDF_TARGET="{}"'.format(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('sdkconfig', 'CONFIG_IDF_TARGET="{}"'.format(ESP32S2_TARGET))
check_file_contains('build/CMakeCache.txt', 'IDF_TARGET:STRING={}'.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: def test_target_using_sdkconfig(idf_py: IdfPyFunc, test_app_copy: Path) -> None:
logging.info('Can set the default target using sdkconfig.defaults') logging.info('Can set the default target using sdkconfig.defaults')