Skip to content

Commit 1bdcd38

Browse files
ihb2032Ubospica
andauthored
fix(build): Disable LTO on riscv64 to fix linker error (#458)
### Description This PR fixes a build failure encountered when cross-compiling for the `riscv64` architecture. When LTO is enabled (via `-flto=auto`), the build fails during the final linking stage with the following error: ``` /data/hebo/toolchain/gcc-15.1/lib/gcc/riscv64-unknown-linux-gnu/15.1.0/../../../../riscv64-unknown-linux-gnu/bin/ld: ... hidden symbol \`\_ZN8nanobind11chain\_errorENS\_6handleEPKcz' isn't defined /data/hebo/toolchain/gcc-15.1/lib/gcc/riscv64-unknown-linux-gnu/15.1.0/../../../../riscv64-unknown-linux-gnu/bin/ld: final link failed: bad value ``` This appears to be caused by the RISC-V toolchain's LTO implementation incorrectly discarding the `nanobind::chain_error` symbol, which is required by the bindings. This patch resolves the issue by modifying the CMake build logic to conditionally add the `-flto=auto` flag to `CMAKE_CXX_FLAGS` *only* when the target architecture (`CMAKE_SYSTEM_PROCESSOR`) is not `riscv64`. ### How This Was Tested The fix has been successfully tested in the following environments: 1. RISC-V 64-bit (Problem Environment): * Platform: SG2044 * Toolchain: GCC 15.1 * Python: 3.11 * Result: Build SUCCESS, previously FAILED 2. x86_64: * Platform: x86_64 * Toolchain: GCC 14 * Python: 3.12 * Result: Build SUCCESS, LTO remains enabled and functional Signed-off-by: lyd1992 <[email protected]> Signed-off-by: ihb2032 <[email protected]> Co-authored-by: Yixin Dong <[email protected]>
1 parent de241aa commit 1bdcd38

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

CMakeLists.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,16 @@ else()
5151
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
5252
set(CMAKE_CXX_FLAGS "-O3 ${CMAKE_CXX_FLAGS}")
5353
endif()
54-
55-
set(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror -Wno-pedantic -Wno-unused-parameter \
54+
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
55+
message(STATUS "RISC-V 64-bit target detected, disabling LTO to prevent linker errors.")
56+
set(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror -Wno-pedantic -Wno-unused-parameter \
57+
-Woverloaded-virtual ${CMAKE_CXX_FLAGS}"
58+
)
59+
else()
60+
set(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror -Wno-pedantic -Wno-unused-parameter \
5661
-Woverloaded-virtual -flto=auto ${CMAKE_CXX_FLAGS}"
57-
)
62+
)
63+
endif()
5864
endif()
5965

6066
set(XGRAMMAR_INCLUDE_PATH ${PROJECT_SOURCE_DIR}/3rdparty/picojson

0 commit comments

Comments
 (0)