cmake breaks llvm's cross compilation
Hi, I'm a developer of LLVM.
Recently, I'm cross building llvm from X86_64 to RISC-V.
However, the cross compilation is broken because of the code below.
https://gitlab.kitware.com/cmake/cmake/-/blob/v3.22.2/Modules/CMakeDetermineSystem.cmake#L156
if(CMAKE_SYSTEM_NAME)
if(NOT DEFINED CMAKE_CROSSCOMPILING)
set(CMAKE_CROSSCOMPILING TRUE)
endif()
set(PRESET_CMAKE_SYSTEM_NAME TRUE)
elseif(CMAKE_VS_WINCE_VERSION)
set(CMAKE_SYSTEM_NAME "WindowsCE")
set(CMAKE_SYSTEM_VERSION "${CMAKE_VS_WINCE_VERSION}")
set(CMAKE_SYSTEM_PROCESSOR "${MSVC_C_ARCHITECTURE_ID}")
set(CMAKE_CROSSCOMPILING TRUE)
set(PRESET_CMAKE_SYSTEM_NAME TRUE)
else()
set(CMAKE_SYSTEM_NAME "${CMAKE_HOST_SYSTEM_NAME}")
if(NOT DEFINED CMAKE_SYSTEM_VERSION)
set(CMAKE_SYSTEM_VERSION "${CMAKE_HOST_SYSTEM_VERSION}")
endif()
set(CMAKE_SYSTEM_PROCESSOR "${CMAKE_HOST_SYSTEM_PROCESSOR}")
set(CMAKE_CROSSCOMPILING FALSE)
set(PRESET_CMAKE_SYSTEM_NAME FALSE)
endif()
As I'm building for linux system on the linux machine. I wonder I don't need to set the
In LLVM project, CMAKE_CROSSCOMPILING
is used to determine whether to execute some tests on the host machine.
And apparently, RISC-V executable cannot run on the x86_64 machine without any other tool's help.
Then the build failed.
https://github.com/llvm/llvm-project/blob/5e27487fc992cece0d703f0bd83480381764b77b/clang/lib/Tooling/CMakeLists.txt#L26
if (NOT Python3_EXECUTABLE
OR APPLE
OR CMAKE_CROSSCOMPILING
OR GENERATOR_IS_MULTI_CONFIG
OR NOT LLVM_NATIVE_ARCH IN_LIST LLVM_TARGETS_TO_BUILD
)
configure_file(
EmptyNodeIntrospection.inc.in
${BINARY_INCLUDE_DIR}/NodeIntrospection.inc
COPYONLY
)
set(CLANG_TOOLING_BUILD_AST_INTROSPECTION "OFF" CACHE BOOL "")
else()
# The generation of ASTNodeAPI.json takes a long time in a
# Debug build due to parsing AST.h. Disable the processing
# but setting CLANG_TOOLING_BUILD_AST_INTROSPECTION as an
# internal hidden setting to override.
# When the processing is disabled, a trivial/empty JSON
# file is generated by clang-ast-dump and generate_cxx_src_locs.py
# generates the same API, but with a trivial implementation.
option(CLANG_TOOLING_BUILD_AST_INTROSPECTION "Enable AST introspection" TRUE)
set(skip_expensive_processing $<OR:$<CONFIG:Debug>,$<NOT:$<BOOL:${CLANG_TOOLING_BUILD_AST_INTROSPECTION}>>>)
set(implicitDirs)
foreach(implicitDir ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
list(APPEND implicitDirs -I ${implicitDir})
endforeach()
add_custom_command(
COMMENT Generate ASTNodeAPI.json
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/ASTNodeAPI.json
DEPENDS clang-ast-dump clang-resource-headers
COMMAND
$<TARGET_FILE:clang-ast-dump>
# Skip this in debug mode because parsing AST.h is too slow
--skip-processing=${skip_expensive_processing}
-I ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/include
-I ${CLANG_SOURCE_DIR}/include
-I ${LLVM_BINARY_DIR}/tools/clang/include
-I ${LLVM_BINARY_DIR}/include
-I ${LLVM_SOURCE_DIR}/include
${implicitDirs}
--json-output-path ${CMAKE_CURRENT_BINARY_DIR}/ASTNodeAPI.json
)
Could you please either update the document here or fix this issue?
However, this is only a part of this issue.
After define -DCMAKE_SYSTEM_NAME=Linux
We still have:
https://github.com/llvm/llvm-project/blob/7ad94bd74bb5e2c984f541bc1921a9eda73ed973/llvm/CMakeLists.txt#L49
if (CMAKE_GENERATOR STREQUAL "Xcode" AND NOT CMAKE_OSX_ARCHITECTURES)
set(CMAKE_OSX_ARCHITECTURES "x86_64")
endif()
# CMAKE_CROSSCOMPILING TRUE HERE
project(LLVM
VERSION ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}
LANGUAGES C CXX ASM)
# CMAKE_CROSSCOMPILING FALSE HERE
include(GNUInstallDirs)
Can someone explain this to me?
Reference: https://github.com/llvm/llvm-project/issues/52819 https://reviews.llvm.org/D93164