Skip to content

Commit

Permalink
tools: move cmake executable check into function uses it
Browse files Browse the repository at this point in the history
Before this change idf.py could exit with reason cmake does not exist in PATH
even cmake will not be executed by idf.py (e.g., 'idf.py gdb').
  • Loading branch information
Lapshin committed Sep 21, 2022
1 parent 0fd4b09 commit c17a7f4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
35 changes: 3 additions & 32 deletions tools/idf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from importlib import import_module
from pkgutil import iter_modules
from types import FrameType
from typing import Any, Callable, Dict, List, Optional, TextIO, Union
from typing import Any, Callable, Dict, List, Optional, Union

# pyc files remain in the filesystem when switching between branches which might raise errors for incompatible
# idf.py extensions. Therefore, pyc file generation is turned off:
Expand All @@ -37,8 +37,8 @@

try:
from idf_py_actions.errors import FatalError # noqa: E402
from idf_py_actions.tools import (PropertyDict, executable_exists, get_target, idf_version, # noqa: E402
merge_action_lists, realpath)
from idf_py_actions.tools import (PROG, SHELL_COMPLETE_RUN, SHELL_COMPLETE_VAR, PropertyDict, # noqa: E402
debug_print_idf_version, get_target, merge_action_lists, print_warning, realpath)
if os.getenv('IDF_COMPONENT_MANAGER') != '0':
from idf_component_manager import idf_extensions
except ImportError:
Expand All @@ -53,23 +53,6 @@
# you have to pass env=os.environ explicitly anywhere that we create a process
os.environ['PYTHON'] = sys.executable

# Name of the program, normally 'idf.py'.
# Can be overridden from idf.bat using IDF_PY_PROGRAM_NAME
PROG = os.getenv('IDF_PY_PROGRAM_NAME', 'idf.py')

# environment variable used during click shell completion run
SHELL_COMPLETE_VAR = '_IDF.PY_COMPLETE'

# was shell completion invoked?
SHELL_COMPLETE_RUN = SHELL_COMPLETE_VAR in os.environ


# function prints warning when autocompletion is not being performed
# set argument stream to sys.stderr for errors and exceptions
def print_warning(message: str, stream: TextIO=None) -> None:
if not SHELL_COMPLETE_RUN:
print(message, file=stream or sys.stderr)


def check_environment() -> List:
"""
Expand All @@ -79,10 +62,6 @@ def check_environment() -> List:
"""
checks_output = []

if not executable_exists(['cmake', '--version']):
debug_print_idf_version()
raise FatalError("'cmake' must be available on the PATH to use %s" % PROG)

# verify that IDF_PATH env variable is set
# find the directory idf.py is in, then the parent directory of this, and assume this is IDF_PATH
detected_idf_path = realpath(os.path.join(os.path.dirname(__file__), '..'))
Expand Down Expand Up @@ -137,14 +116,6 @@ def _safe_relpath(path: str, start: Optional[str]=None) -> str:
return os.path.abspath(path)


def debug_print_idf_version() -> None:
version = idf_version()
if version:
print_warning('ESP-IDF %s' % version)
else:
print_warning('ESP-IDF version unknown')


def init_cli(verbose_output: List=None) -> Any:
# Click is imported here to run it after check_environment()
import click
Expand Down
26 changes: 26 additions & 0 deletions tools/idf_py_actions/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
from .constants import GENERATORS
from .errors import FatalError

# Name of the program, normally 'idf.py'.
# Can be overridden from idf.bat using IDF_PY_PROGRAM_NAME
PROG = os.getenv('IDF_PY_PROGRAM_NAME', 'idf.py')

# environment variable used during click shell completion run
SHELL_COMPLETE_VAR = '_IDF.PY_COMPLETE'

# was shell completion invoked?
SHELL_COMPLETE_RUN = SHELL_COMPLETE_VAR in os.environ


def executable_exists(args: List) -> bool:
try:
Expand Down Expand Up @@ -78,6 +88,13 @@ def idf_version() -> Optional[str]:
return version


# function prints warning when autocompletion is not being performed
# set argument stream to sys.stderr for errors and exceptions
def print_warning(message: str, stream: TextIO=None) -> None:
if not SHELL_COMPLETE_RUN:
print(message, file=stream or sys.stderr)


def color_print(message: str, color: str, newline: Optional[str]='\n') -> None:
""" Print a message to stderr with colored highlighting """
ansi_normal = '\033[0m'
Expand All @@ -95,6 +112,10 @@ def red_print(message: str, newline: Optional[str]='\n') -> None:
color_print(message, ansi_red, newline)


def debug_print_idf_version() -> None:
print_warning(f'ESP-IDF {idf_version() or "version unknown"}')


def generate_hints(*filenames: str) -> Generator:
"""Getting output files and printing hints on how to resolve errors based on the output."""
with open(os.path.join(os.path.dirname(__file__), 'hints.yml'), 'r') as file:
Expand Down Expand Up @@ -383,6 +404,11 @@ def ensure_build_directory(args: 'PropertyDict', prog_name: str, always_run_cmak
the build directory, an error is raised. If the parameter is None, this function will set it to
an auto-detected default generator or to the value already configured in the build directory.
"""

if not executable_exists(['cmake', '--version']):
debug_print_idf_version()
raise FatalError(f'"cmake" must be available on the PATH to use {PROG}')

project_dir = args.project_dir
# Verify the project directory
if not os.path.isdir(project_dir):
Expand Down

0 comments on commit c17a7f4

Please sign in to comment.