Skip to content

Commit

Permalink
[libc] Build native libc-hdrgen when crosscompiling (#77848)
Browse files Browse the repository at this point in the history
When crosscompiling tools for a different architecture, we need to build
native libc-hdrgen which can be achieved using the existing CMake
support for crosscompiling tablegen tools.
  • Loading branch information
petrhosek committed Jan 12, 2024
1 parent 792fa23 commit b348126
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 2 additions & 0 deletions libc/utils/HdrGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ target_include_directories(libc-hdrgen PRIVATE ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_I
target_link_libraries(libc-hdrgen PRIVATE LibcTableGenUtil)

add_subdirectory(PrototypeTestGen)

setup_host_tool(libc-hdrgen LIBC_HDRGEN libc_hdrgen_exe libc_hdrgen_target)
8 changes: 7 additions & 1 deletion llvm/cmake/modules/CrossCompile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ function(llvm_create_cross_target project_name target_name toolchain buildtype)
"-DLLVM_EXTERNAL_${name}_SOURCE_DIR=${LLVM_EXTERNAL_${name}_SOURCE_DIR}")
endforeach()

if("libc" IN_LIST LLVM_ENABLE_PROJECTS)
if(LLVM_LIBC_FULL_BUILD AND NOT LIBC_HDRGEN_EXE)
set(libc_flags -DLLVM_LIBC_FULL_BUILD=ON -DLIBC_HDRGEN_ONLY=ON)
endif()
endif()

add_custom_command(OUTPUT ${${project_name}_${target_name}_BUILD}/CMakeCache.txt
COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}"
-DCMAKE_MAKE_PROGRAM="${CMAKE_MAKE_PROGRAM}"
Expand All @@ -86,7 +92,7 @@ function(llvm_create_cross_target project_name target_name toolchain buildtype)
-DLLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN="${LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN}"
-DLLVM_INCLUDE_BENCHMARKS=OFF
-DLLVM_INCLUDE_TESTS=OFF
${build_type_flags} ${linker_flag} ${external_clang_dir}
${build_type_flags} ${linker_flag} ${external_clang_dir} ${libc_flags}
${ARGN}
WORKING_DIRECTORY ${${project_name}_${target_name}_BUILD}
DEPENDS CREATE_${project_name}_${target_name}
Expand Down
18 changes: 7 additions & 11 deletions llvm/runtimes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -425,18 +425,14 @@ if(runtimes)
endif()
if("libc" IN_LIST LLVM_ENABLE_PROJECTS AND
(LLVM_LIBC_FULL_BUILD OR LIBC_GPU_BUILD OR LIBC_GPU_ARCHITECTURES))
if(TARGET libc-hdrgen)
set(libc_tools libc-hdrgen)
set(libc_cmake_args "-DLIBC_HDRGEN_EXE=$<TARGET_FILE:libc-hdrgen>"
"-DLLVM_LIBC_FULL_BUILD=ON")
list(APPEND extra_deps ${libc_tools})
else()
# We want to build the libc build tools before we can build the libc
# itself. So, the libc project should be included in LLVM_ENABLE_PROJECTS.
# This should have been done in llvm/CMakeLists.txt automatically when
# "libc" is detected in LLVM_ENABLE_RUNTIMES.
message(FATAL_ERROR "libc-hdrgen target missing unexpectedly")
get_host_tool_path(libc-hdrgen LIBC_HDRGEN libc_hdrgen_exe libc_hdrgen_target)
if(NOT libc_hdrgen_exe)
message(FATAL_ERROR "libc-hdrgen executable missing")
endif()
set(libc_cmake_args "-DLIBC_HDRGEN_EXE=${libc_hdrgen_exe}"
"-DLLVM_LIBC_FULL_BUILD=ON")
set(libc_tools libc_hdrgen_target)
list(APPEND extra_deps ${libc_hdrgen_target})
if(LIBC_GPU_BUILD OR LIBC_GPU_ARCHITECTURES)
foreach(dep clang-offload-packager nvptx-arch amdgpu-arch)
if(TARGET ${dep})
Expand Down

1 comment on commit b348126

@jhuber6
Copy link
Contributor

@jhuber6 jhuber6 commented on b348126 Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to have broken my GPU build.

CMake Error at /usr/share/cmake/Modules/ExternalProject.cmake:3675 (get_property):
  get_property could not find TARGET
  /home/jhuber/Documents/llvm/llvm-project/build/NATIVE/bin/libc-hdrgen.
  Perhaps it has not yet been created.
Call Stack (most recent call first):
  /usr/share/cmake/Modules/ExternalProject.cmake:3857 (_ep_get_file_deps)
  /usr/share/cmake/Modules/ExternalProject.cmake:4425 (_ep_add_configure_command)
  cmake/modules/LLVMExternalProjectUtils.cmake:326 (ExternalProject_Add)
  runtimes/CMakeLists.txt:240 (llvm_ExternalProject_Add)
  runtimes/CMakeLists.txt:445 (runtime_default_target)

The GPU build is a RUNTIMES build. if I revert this commit the above error goes away.

Please sign in to comment.