diff --git a/bsp/ESP/ESP32_C3/SConstruct b/bsp/ESP/ESP32_C3/SConstruct index 4b507b7b6a74..57b34386ce5b 100644 --- a/bsp/ESP/ESP32_C3/SConstruct +++ b/bsp/ESP/ESP32_C3/SConstruct @@ -68,6 +68,10 @@ Export('rtconfig') # prepare building environment objs = PrepareBuilding(env, RTT_ROOT, remove_components = ['libc']) +sys.path = sys.path + [os.path.join(os.path.dirname(SDK_ROOT), 'tools')] +from freertos_include import fix_freertos_include_order +fix_freertos_include_order(env) + objs.extend(SConscript(os.path.join(libraries_path_prefix, 'drivers', 'SConscript'), variant_dir='build/libraries/drivers', duplicate=0)) # make a building DoBuilding(TARGET, objs) diff --git a/bsp/ESP/ESP32_C6/SConstruct b/bsp/ESP/ESP32_C6/SConstruct index 2d877aebd133..6375e3a55033 100644 --- a/bsp/ESP/ESP32_C6/SConstruct +++ b/bsp/ESP/ESP32_C6/SConstruct @@ -164,6 +164,10 @@ _match_idf_spi_lock_stub_to_spi_config() objs = PrepareBuilding(env, RTT_ROOT, remove_components = ['libc']) +sys.path = sys.path + [os.path.join(os.path.dirname(SDK_ROOT), 'tools')] +from freertos_include import fix_freertos_include_order +fix_freertos_include_order(env) + objs.extend(SConscript(os.path.join(libraries_path_prefix, 'drivers', 'SConscript'), variant_dir='build/libraries/drivers', duplicate=0)) DoBuilding(TARGET, objs) diff --git a/bsp/ESP/tools/freertos_include.py b/bsp/ESP/tools/freertos_include.py new file mode 100644 index 000000000000..a30bafbfcaf7 --- /dev/null +++ b/bsp/ESP/tools/freertos_include.py @@ -0,0 +1,57 @@ +"""Build helpers shared by the ESP32 BSPs.""" + +import os + + +def _norm(path): + return os.path.normcase(os.path.abspath(str(path))).replace('\\', '/').lower() + + +def fix_freertos_include_order(env): + """Keep the RT-Thread FreeRTOS-Wrapper first in the include search order. + + ESP-IDF's scons shim (components/rtthread/scons/include/freertos/FreeRTOS.h) + forwards with #include_next, so the Wrapper include + directories must be searched after the shim but before ESP-IDF's own + FreeRTOS-Kernel copy. Package SConscripts are evaluated in directory + order, which is not stable, so enforce the order after PrepareBuilding(). + """ + cpp = list(env.get('CPPPATH', [])) + shim = None + wrapper = [] + front = [] + front_keys = set() + + for path in cpp: + norm = _norm(path) + if norm.endswith('/components/rtthread/scons/include'): + if shim is None: + shim = path + continue + + if '/freertos_wrapper-' in norm or '/freertos-wrapper-' in norm: + if norm not in front_keys: + front_keys.add(norm) + wrapper.append(path) + front.append(path) + elif norm.endswith('/idf_port/include') or \ + norm.endswith('/idf_port/include/freertos'): + # The BSP's FreeRTOSConfig.h must win over the ESP-IDF copy that + # Wi-Fi/BLE configurations add to the include path. + if norm not in front_keys: + front_keys.add(norm) + front.append(path) + + if shim is None or not wrapper: + return + + # Drop the Wrapper and BSP port config entries first so that they can be + # inserted right after the shim, whatever order the SConscripts ran in. + rest = [p for p in cpp if _norm(p) not in front_keys] + + shim_key = _norm(shim) + for index, path in enumerate(rest): + if _norm(path) == shim_key: + rest[index + 1:index + 1] = front + env.Replace(CPPPATH=rest) + return