Skip to content

Commit 6fd28cf

Browse files
rascaniclaude
andauthored
Don't link libdl when cross-compiling (pytorch#20522)
### Summary The top-level CMakeLists.txt links libdl into executorch_core when EXECUTORCH_USE_DL is on (the default): find_library(DL_LIBRARY_EXISTS NAMES dl) if(DL_LIBRARY_EXISTS): target_link_libraries(executorch_core PRIVATE dl) When cross-compiling for a bare-metal target (arm-none-eabi, esp, zephyr), find_library still resolves the *host* libdl, so -ldl is linked into executorch_core and propagates through the exported target interface, failing the bare-metal executor-runner link ("cannot find -ldl"). dladdr() (the only reason for the link) is compiled out on those targets anyway (it's gated by the ET_USE_LIBDL macro, which the CMake build never defines). Two layers: - Guard the link with `NOT CMAKE_CROSSCOMPILING` in CMakeLists.txt, so any cross-compiled target is covered even without a preset. - Set EXECUTORCH_USE_DL OFF explicitly in the arm_baremetal / esp_baremetal / zephyr presets, documenting the intent for those bare-metal builds. ### Test plan Confirmed against the cortex-m FVP runner: with -ldl removed the arm_executor_runner links and runs on Corstone-300. Co-authored-by: Claude <noreply@anthropic.com>
1 parent 9e23c9c commit 6fd28cf

4 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎CMakeLists.txt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,11 @@ target_link_libraries(executorch_core PRIVATE program_schema)
519519
if(ANDROID)
520520
target_link_libraries(executorch_core PUBLIC log)
521521
endif()
522-
if(EXECUTORCH_USE_DL)
522+
# Skip when cross-compiling: find_library() resolves the host libdl even for a
523+
# bare-metal target (e.g. arm-none-eabi), which then links -ldl into the
524+
# baremetal runner and fails ("cannot find -ldl"). dladdr() isn't used on those
525+
# targets anyway. Per-target presets also set EXECUTORCH_USE_DL OFF explicitly.
526+
if(EXECUTORCH_USE_DL AND NOT CMAKE_CROSSCOMPILING)
523527
# Check if dl exists for this toolchain and only then link it.
524528
find_library(DL_LIBRARY_EXISTS NAMES dl)
525529
# Check if the library was found

‎tools/cmake/preset/arm_baremetal.cmake‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ set_overridable_option(EXECUTORCH_BUILD_EXECUTOR_RUNNER OFF)
2929
set_overridable_option(EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR OFF)
3030
set_overridable_option(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER OFF)
3131
set_overridable_option(EXECUTORCH_BUILD_ARM_BAREMETAL ON)
32+
# Bare-metal has no libdl (see CMakeLists.txt EXECUTORCH_USE_DL block).
33+
set_overridable_option(EXECUTORCH_USE_DL OFF)
3234
set_overridable_option(EXECUTORCH_BUILD_KERNELS_QUANTIZED ON)
3335
set_overridable_option(EXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL ON)
3436
set_overridable_option(EXECUTORCH_BUILD_CORTEX_M ON)

‎tools/cmake/preset/esp_baremetal.cmake‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}")
77
set_overridable_option(EXECUTORCH_BUILD_EXECUTOR_RUNNER OFF)
88
set_overridable_option(EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR OFF)
99
set_overridable_option(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER OFF)
10+
# Bare-metal has no libdl (see CMakeLists.txt EXECUTORCH_USE_DL block).
11+
set_overridable_option(EXECUTORCH_USE_DL OFF)
1012
set_overridable_option(EXECUTORCH_BUILD_KERNELS_QUANTIZED ON)
1113
set_overridable_option(EXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL ON)
1214
set_overridable_option(EXECUTORCH_ENABLE_LOGGING ON)

‎tools/cmake/preset/zephyr.cmake‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
set_overridable_option(EXECUTORCH_BUILD_COREML OFF)
88
set_overridable_option(EXECUTORCH_ENABLE_EVENT_TRACER OFF)
9+
# Bare-metal has no libdl (see CMakeLists.txt EXECUTORCH_USE_DL block).
10+
set_overridable_option(EXECUTORCH_USE_DL OFF)
911
set_overridable_option(EXECUTORCH_BUILD_KERNELS_LLM OFF)
1012
set_overridable_option(EXECUTORCH_BUILD_KERNELS_LLM_AOT OFF)
1113
set_overridable_option(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER OFF)

0 commit comments

Comments
 (0)