From 61cb0afd40ef8d6a5493cbd3bd60f49f61e8b34f Mon Sep 17 00:00:00 2001
From: Ross Burton <ross.burton@arm.com>
Date: Wed, 16 Jul 2025 23:24:21 +0100
Subject: [PATCH] allow external prepare_builtins

In standalone cross builds, libclc can't build a native prepare_builtins.

Hack the CMake to allow it to use an existing binary, which we build in the recipe.

Upstream-Status: Inappropriate [discussion for a proper fix in https://github.com/llvm/llvm-project/issues/149814]
Signed-off-by: Ross Burton <ross.burton@arm.com>
---
 libclc/CMakeLists.txt | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/libclc/CMakeLists.txt b/libclc/CMakeLists.txt
index c98e2043464d..151c5d2d20c6 100644
--- a/libclc/CMakeLists.txt
+++ b/libclc/CMakeLists.txt
@@ -178,17 +178,28 @@ set(LLVM_LINK_COMPONENTS
   IRReader
   Support
 )
+
 if( LIBCLC_STANDALONE_BUILD )
-  add_llvm_executable( prepare_builtins utils/prepare-builtins.cpp )
-  set( prepare_builtins_exe prepare_builtins )
-  set( prepare_builtins_target prepare_builtins )
+  message("cross ${CMAKE_CROSSCOMPILING}")
+  if(CMAKE_CROSSCOMPILING)
+    find_program(PREPARE_BUILTINS prepare_builtins REQUIRED)
+    message("found ${PREPARE_BUILTINS}")
+    set( prepare_builtins_exe ${PREPARE_BUILTINS} )
+  else()
+    add_llvm_executable( prepare_builtins utils/prepare-builtins.cpp )
+    set( prepare_builtins_exe prepare_builtins )
+    set( prepare_builtins_target prepare_builtins )
+    target_compile_definitions( prepare_builtins PRIVATE ${LLVM_VERSION_DEFINE} )
+    # These were not properly reported in early LLVM and we don't need them
+    target_compile_options( prepare_builtins PRIVATE -fno-rtti -fno-exceptions )
+  endif()
 else()
   add_llvm_utility( prepare_builtins utils/prepare-builtins.cpp )
   setup_host_tool( prepare_builtins PREPARE_BUILTINS prepare_builtins_exe prepare_builtins_target )
+  target_compile_definitions( prepare_builtins PRIVATE ${LLVM_VERSION_DEFINE} )
+  # These were not properly reported in early LLVM and we don't need them
+  target_compile_options( prepare_builtins PRIVATE -fno-rtti -fno-exceptions )
 endif()
-target_compile_definitions( prepare_builtins PRIVATE ${LLVM_VERSION_DEFINE} )
-# These were not properly reported in early LLVM and we don't need them
-target_compile_options( prepare_builtins PRIVATE -fno-rtti -fno-exceptions )
 
 # Setup arch devices
 set( r600--_devices cedar cypress barts cayman )
