tools: add target consistency checks for target specified with -D option

Extend existing target consistency checks for the two following cases.
1. Target does not match currently used toolchain
   $ IDF_TARGET=esp32s2 idf.py reconfigure
   $ idf.py -DIDF_TARGET=esp32c3 build

2. Target is ambiguous, because it's specified also as env. var.
   IDF_TARGET=esp32s3 idf.py set-target esp32c2

Signed-off-by: Frantisek Hrbata <frantisek.hrbata@espressif.com>
This commit is contained in:
Frantisek Hrbata 2023-03-04 10:39:32 +01:00
parent 0d859f2786
commit 8e912faad1
2 changed files with 37 additions and 8 deletions

View File

@ -590,6 +590,7 @@ def _check_idf_target(args: 'PropertyDict', prog_name: str, cache: Dict, cache_c
idf_target_from_sdkconfig = get_sdkconfig_value(sdkconfig, 'CONFIG_IDF_TARGET')
idf_target_from_env = os.environ.get('IDF_TARGET')
idf_target_from_cache = cache.get('IDF_TARGET')
idf_target_from_cache_cmdl = cache_cmdl.get('IDF_TARGET')
if idf_target_from_env:
# Let's check that IDF_TARGET values are consistent
@ -603,12 +604,25 @@ def _check_idf_target(args: 'PropertyDict', prog_name: str, cache: Dict, cache_c
"Run '{prog} fullclean' to start again."
.format(t_env=idf_target_from_env, t_cache=idf_target_from_cache, prog=prog_name))
elif idf_target_from_cache and idf_target_from_sdkconfig and idf_target_from_cache != idf_target_from_sdkconfig:
if idf_target_from_cache_cmdl and idf_target_from_cache_cmdl != idf_target_from_env:
raise FatalError("Target '{t_cmdl}' specified on command line is not consistent with "
"target '{t_env}' in the environment."
.format(t_cmdl=idf_target_from_cache_cmdl, t_env=idf_target_from_env))
elif idf_target_from_cache_cmdl:
# Check if -DIDF_TARGET is consistent with target in CMakeCache.txt
if idf_target_from_cache and idf_target_from_cache != idf_target_from_cache_cmdl:
raise FatalError("Target '{t_cmdl}' specified on command line is not consistent with "
"target '{t_cache}' in CMakeCache.txt. Run '{prog} set-target {t_cmdl}' to re-generate "
'CMakeCache.txt.'
.format(t_cache=idf_target_from_cache, t_cmdl=idf_target_from_cache_cmdl, prog=prog_name))
elif idf_target_from_cache:
# This shouldn't happen, unless the user manually edits CMakeCache.txt or sdkconfig, but let's check anyway.
raise FatalError("Project sdkconfig '{cfg}' was generated for target '{t_conf}', but CMakeCache.txt contains '{t_cache}'. "
"To keep the setting in sdkconfig ({t_conf}) and re-generate CMakeCache.txt, run '{prog} fullclean'. "
"To re-generate sdkconfig for '{t_cache}' target, run '{prog} set-target {t_cache}'."
.format(cfg=sdkconfig, t_conf=idf_target_from_sdkconfig, t_cache=idf_target_from_cache, prog=prog_name))
if idf_target_from_sdkconfig and idf_target_from_cache != idf_target_from_sdkconfig:
raise FatalError("Project sdkconfig '{cfg}' was generated for target '{t_conf}', but CMakeCache.txt contains '{t_cache}'. "
"To keep the setting in sdkconfig ({t_conf}) and re-generate CMakeCache.txt, run '{prog} fullclean'. "
"To re-generate sdkconfig for '{t_cache}' target, run '{prog} set-target {t_cache}'."
.format(cfg=sdkconfig, t_conf=idf_target_from_sdkconfig, t_cache=idf_target_from_cache, prog=prog_name))
class TargetChoice(click.Choice):

View File

@ -4,6 +4,7 @@
import logging
import shutil
from pathlib import Path
from typing import List, Optional
import pytest
from test_build_system_helpers import EnvDict, IdfPyFunc, check_file_contains, run_cmake
@ -31,15 +32,16 @@ def test_target_from_environment_cmake(default_idf_env: EnvDict) -> None:
def test_target_from_environment_idf_py(idf_py: IdfPyFunc, default_idf_env: EnvDict, test_app_copy: Path) -> None:
def reconfigure_and_check_return_values(errmsg: str) -> None:
ret = idf_py('reconfigure', check=False)
def reconfigure_and_check_return_values(errmsg: str, opts: Optional[List[str]] = None) -> None:
opts = opts or []
ret = idf_py(*opts, 'reconfigure', check=False)
assert ret.returncode == 2
assert errmsg in ret.stderr
idf_py('set-target', ESP32S2_TARGET)
default_idf_env.update({'IDF_TARGET': ESP32_TARGET})
cfg_path = test_app_copy.joinpath('sdkconfig')
cfg_path = (test_app_copy / 'sdkconfig')
logging.info("idf.py fails if IDF_TARGET settings don't match the environment")
reconfigure_and_check_return_values("Project sdkconfig '{}' was generated for target '{}', but environment "
@ -56,6 +58,19 @@ def test_target_from_environment_idf_py(idf_py: IdfPyFunc, default_idf_env: EnvD
reconfigure_and_check_return_values("Project sdkconfig '{}' was generated for target '{}', but CMakeCache.txt "
"contains '{}'.".format(cfg_path, ESP32_TARGET, ESP32S2_TARGET))
logging.info('idf.py fails if IDF_TARGET is set differently in environment and with -D option')
(test_app_copy / 'sdkconfig').write_text('CONFIG_IDF_TARGET="{}"'.format(ESP32S2_TARGET))
default_idf_env.update({'IDF_TARGET': ESP32S2_TARGET})
reconfigure_and_check_return_values("Target '{}' specified on command line is not consistent with target '{}' "
'in the environment.'.format(ESP32_TARGET, ESP32S2_TARGET),
['-D', 'IDF_TARGET={}'.format(ESP32_TARGET)])
logging.info('idf.py fails if IDF_TARGET is set differently in CMakeCache.txt and with -D option')
default_idf_env.pop('IDF_TARGET')
reconfigure_and_check_return_values("Target '{}' specified on command line is not consistent with "
"target '{}' in CMakeCache.txt.".format(ESP32_TARGET, ESP32S2_TARGET),
['-D', 'IDF_TARGET={}'.format(ESP32_TARGET)])
def test_target_precedence(idf_py: IdfPyFunc, default_idf_env: EnvDict, test_app_copy: Path) -> None:
logging.info('IDF_TARGET takes precedence over the value of CONFIG_IDF_TARGET in sdkconfig.defaults')