1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-12 21:58:14 +00:00

feat(core): Add support for <...> includes (#8132)

This commit is contained in:
Rodrigo Martín 2025-02-02 21:34:38 +01:00 committed by GitHub
parent 051fa3a49f
commit 03e2701bd0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 5 deletions

View File

@ -689,7 +689,7 @@ class EsphomeCore:
_LOGGER.debug("Adding: %s", expression) _LOGGER.debug("Adding: %s", expression)
return expression return expression
def add_global(self, expression): def add_global(self, expression, prepend=False):
from esphome.cpp_generator import Expression, Statement, statement from esphome.cpp_generator import Expression, Statement, statement
if isinstance(expression, Expression): if isinstance(expression, Expression):
@ -698,7 +698,10 @@ class EsphomeCore:
raise ValueError( raise ValueError(
f"Add '{expression}' must be expression or statement, not {type(expression)}" f"Add '{expression}' must be expression or statement, not {type(expression)}"
) )
self.global_statements.append(expression) if prepend:
self.global_statements.insert(0, expression)
else:
self.global_statements.append(expression)
_LOGGER.debug("Adding global: %s", expression) _LOGGER.debug("Adding global: %s", expression)
return expression return expression

View File

@ -72,6 +72,9 @@ def validate_hostname(config):
def valid_include(value): def valid_include(value):
# Look for "<...>" includes
if value.startswith("<") and value.endswith(">"):
return value
try: try:
return cv.directory(value) return cv.directory(value)
except cv.Invalid: except cv.Invalid:
@ -360,7 +363,19 @@ async def to_code(config):
CORE.add_job(add_arduino_global_workaround) CORE.add_job(add_arduino_global_workaround)
if config[CONF_INCLUDES]: if config[CONF_INCLUDES]:
CORE.add_job(add_includes, config[CONF_INCLUDES]) # Get the <...> includes
system_includes = []
other_includes = []
for include in config[CONF_INCLUDES]:
if include.startswith("<") and include.endswith(">"):
system_includes.append(include)
else:
other_includes.append(include)
# <...> includes should be at the start
for include in system_includes:
cg.add_global(cg.RawStatement(f"#include {include}"), prepend=True)
# Other includes should be at the end
CORE.add_job(add_includes, other_includes)
if project_conf := config.get(CONF_PROJECT): if project_conf := config.get(CONF_PROJECT):
cg.add_define("ESPHOME_PROJECT_NAME", project_conf[CONF_NAME]) cg.add_define("ESPHOME_PROJECT_NAME", project_conf[CONF_NAME])

View File

@ -588,9 +588,9 @@ def add(expression: Union[Expression, Statement]):
CORE.add(expression) CORE.add(expression)
def add_global(expression: Union[SafeExpType, Statement]): def add_global(expression: Union[SafeExpType, Statement], prepend: bool = False):
"""Add an expression to the codegen global storage (above setup()).""" """Add an expression to the codegen global storage (above setup())."""
CORE.add_global(expression) CORE.add_global(expression, prepend)
def add_library(name: str, version: Optional[str], repository: Optional[str] = None): def add_library(name: str, version: Optional[str], repository: Optional[str] = None):