From 4c210fdf6943c0c40b5fe0f66800c7b9c7ca84d3 Mon Sep 17 00:00:00 2001
From: Yiyang Wu <xgreenlandforwyy@gmail.com>
Date: Tue, 14 Jun 2022 20:21:22 +0800
Subject: [PATCH] Find CLANG_RESOURCE_DIR using clang -print-resource-dir

Suggested-By: https://reviews.llvm.org/D49486
Signed-off-by: Yiyang Wu <xgreenlandforwyy@gmail.com>
---
 lib/comgr/cmake/opencl_pch.cmake | 24 +++---------------------
 1 file changed, 3 insertions(+), 21 deletions(-)

diff --git a/lib/comgr/cmake/opencl_pch.cmake b/lib/comgr/cmake/opencl_pch.cmake
index 95311fc..71050c8 100644
--- a/cmake/opencl_pch.cmake
+++ b/cmake/opencl_pch.cmake
@@ -1,26 +1,8 @@
 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
   find_package(Clang REQUIRED CONFIG)
-
-  # FIXME: CLANG_CMAKE_DIR seems like the most stable way to find this, but
-  # really there is no way to reliably discover this header.
-  #
-  # We effectively back up to the Clang output directory (for the case of a build
-  # tree) or install prefix (for the case of an installed copy), and then search
-  # for a file named opencl-c.h anywhere below that. We take the first result in
-  # the case where there are multiple (e.g. if there is an installed copy nested
-  # in a build directory). This is a bit imprecise, but it covers cases like MSVC
-  # adding some additional configuration-specific subdirectories to the build
-  # tree but not to an installed copy.
-  file(GLOB_RECURSE OPENCL_C_H_LIST "${CLANG_CMAKE_DIR}/../../../*/opencl-c.h")
-
-  list(GET OPENCL_C_H_LIST 0 OPENCL_C_H)
-
-  if (NOT EXISTS "${OPENCL_C_H}" OR IS_DIRECTORY "${OPENCL_C_H}")
-    message(FATAL_ERROR "Unable to locate opencl-c.h from the supplied Clang. The path '${CLANG_CMAKE_DIR}/../../../*' was searched.")
-  endif()
-else()
-  get_target_property(clang_build_header_dir clang-resource-headers RUNTIME_OUTPUT_DIRECTORY)
-  set(OPENCL_C_H "${clang_build_header_dir}/opencl-c.h")
+  execute_process(COMMAND "${CLANG_CMAKE_DIR}/../../../bin/clang" -print-resource-dir OUTPUT_VARIABLE CLANG_RESOURCE_DIR)
+  string(STRIP ${CLANG_RESOURCE_DIR} CLANG_RESOURCE_DIR)
+  set(OPENCL_C_H "${CLANG_RESOURCE_DIR}/include/opencl-c.h")
 endif()
 
 # Macro to create and install a custom target for generating PCH for given
-- 
2.39.0

