[OpenMP][libomp] Detect if test compiler has omp.h

omp50_taskdep_depobj.c relies on the test compiler's omp.h file.
If the test compiler does not have an omp.h file, then use the one
within the build tree.

Fixes: https://github.com/llvm/llvm-project/issues/56820
Differential Revision: https://reviews.llvm.org/D131000
GitOrigin-RevId: 9cf6511bff97007401238f6cff6bf80cb9af04a5
diff --git a/cmake/DetectTestCompiler/CMakeLists.txt b/cmake/DetectTestCompiler/CMakeLists.txt
index c8afd47..dc709f5 100644
--- a/cmake/DetectTestCompiler/CMakeLists.txt
+++ b/cmake/DetectTestCompiler/CMakeLists.txt
@@ -3,6 +3,8 @@
 
 include(CheckCCompilerFlag)
 include(CheckCXXCompilerFlag)
+include(CheckIncludeFile)
+include(CheckIncludeFileCXX)
 
 function(write_compiler_information lang)
   set(information "${CMAKE_${lang}_COMPILER}")
@@ -11,6 +13,7 @@
   set(information "${information}\\;${${lang}_FLAGS}")
   set(information "${information}\\;${${lang}_HAS_TSAN_FLAG}")
   set(information "${information}\\;${${lang}_HAS_OMIT_FRAME_POINTER}")
+  set(information "${information}\\;${${lang}_HAS_OMP_H}")
   file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${lang}CompilerInformation.txt ${information})
 endfunction(write_compiler_information)
 
@@ -44,9 +47,15 @@
 check_c_compiler_flag("-fno-omit-frame-pointer" C_HAS_OMIT_FRAME_POINTER)
 check_cxx_compiler_flag("-fno-omit-frame-pointer" CXX_HAS_OMIT_FRAME_POINTER)
 
-SET(CMAKE_REQUIRED_FLAGS "-fsanitize=thread")
+set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
+set(CMAKE_REQUIRED_FLAGS "-fsanitize=thread")
 check_c_compiler_flag("" C_HAS_TSAN_FLAG)
 check_cxx_compiler_flag("" CXX_HAS_TSAN_FLAG)
+set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
+
+# Check if omp.h header exists for the test compiler
+check_include_file_cxx(omp.h CXX_HAS_OMP_H)
+check_include_file(omp.h C_HAS_OMP_H)
 
 write_compiler_information(C)
 write_compiler_information(CXX)
diff --git a/cmake/OpenMPTesting.cmake b/cmake/OpenMPTesting.cmake
index 0370b5f..1a9e0de 100644
--- a/cmake/OpenMPTesting.cmake
+++ b/cmake/OpenMPTesting.cmake
@@ -77,6 +77,7 @@
   list(GET information 3 openmp_flags)
   list(GET information 4 has_tsan_flags)
   list(GET information 5 has_omit_frame_pointer_flags)
+  list(GET information 6 has_omp_h)
 
   set(OPENMP_TEST_${lang}_COMPILER_PATH ${path})
   set(OPENMP_TEST_${lang}_COMPILER_ID ${id})
@@ -84,6 +85,7 @@
   set(OPENMP_TEST_${lang}_COMPILER_OPENMP_FLAGS ${openmp_flags})
   set(OPENMP_TEST_${lang}_COMPILER_HAS_TSAN_FLAGS ${has_tsan_flags})
   set(OPENMP_TEST_${lang}_COMPILER_HAS_OMIT_FRAME_POINTER_FLAGS ${has_omit_frame_pointer_flags})
+  set(OPENMP_TEST_${lang}_COMPILER_HAS_OMP_H ${has_omp_h})
 endmacro()
 
 # Function to set variables with information about the test compiler.
@@ -101,6 +103,7 @@
     set(OPENMP_TEST_COMPILER_OPENMP_FLAGS "${OPENMP_TEST_C_COMPILER_OPENMP_FLAGS}" PARENT_SCOPE)
     set(OPENMP_TEST_COMPILER_HAS_TSAN_FLAGS "${OPENMP_TEST_C_COMPILER_HAS_TSAN_FLAGS}" PARENT_SCOPE)
     set(OPENMP_TEST_COMPILER_HAS_OMIT_FRAME_POINTER_FLAGS "${OPENMP_TEST_C_COMPILER_HAS_OMIT_FRAME_POINTER_FLAGS}" PARENT_SCOPE)
+    set(OPENMP_TEST_COMPILER_HAS_OMP_H "${OPENMP_TEST_C_COMPILER_HAS_OMP_H}" PARENT_SCOPE)
 
     # Determine major version.
     string(REGEX MATCH "[0-9]+" major "${OPENMP_TEST_C_COMPILER_VERSION}")
@@ -150,6 +153,7 @@
   else()
     set(OPENMP_TEST_COMPILER_HAS_TSAN_FLAGS 0)
   endif()
+  set(OPENMP_TEST_COMPILER_HAS_OMP_H 1)
   # TODO: Implement blockaddress in GlobalISel and remove this flag!
   set(OPENMP_TEST_COMPILER_OPENMP_FLAGS "-fopenmp ${OPENMP_TEST_COMPILER_THREAD_FLAGS} -fno-experimental-isel")
   set(OPENMP_TEST_COMPILER_HAS_OMIT_FRAME_POINTER_FLAGS 1)
diff --git a/runtime/test/CMakeLists.txt b/runtime/test/CMakeLists.txt
index aa9a373..71680a3 100644
--- a/runtime/test/CMakeLists.txt
+++ b/runtime/test/CMakeLists.txt
@@ -31,6 +31,7 @@
 pythonize_bool(LIBOMP_HAVE_LIBATOMIC)
 pythonize_bool(OPENMP_STANDALONE_BUILD)
 pythonize_bool(OPENMP_TEST_COMPILER_HAS_OMIT_FRAME_POINTER_FLAGS)
+pythonize_bool(OPENMP_TEST_COMPILER_HAS_OMP_H)
 
 add_library(ompt-print-callback INTERFACE)
 target_include_directories(ompt-print-callback INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/ompt)
diff --git a/runtime/test/lit.cfg b/runtime/test/lit.cfg
index b000787..fe1886b 100644
--- a/runtime/test/lit.cfg
+++ b/runtime/test/lit.cfg
@@ -140,7 +140,7 @@
 config.substitutions.append(("%openmp_flags", config.test_openmp_flags))
 # %flags-use-compiler-omp-h allows us to use the test compiler's omp.h file which
 # may have different definitions of structures than our omp.h file.
-if config.is_standalone_build:
+if config.is_standalone_build and config.test_compiler_has_omp_h:
     config.substitutions.append(("%flags-use-compiler-omp-h",
         config.test_flags_use_compiler_omp_h))
 else:
diff --git a/runtime/test/lit.site.cfg.in b/runtime/test/lit.site.cfg.in
index 4d8235d..e84a2d3 100644
--- a/runtime/test/lit.site.cfg.in
+++ b/runtime/test/lit.site.cfg.in
@@ -3,6 +3,7 @@
 config.test_c_compiler = "@OPENMP_TEST_C_COMPILER@"
 config.test_cxx_compiler = "@OPENMP_TEST_CXX_COMPILER@"
 config.test_compiler_features = @OPENMP_TEST_COMPILER_FEATURES@
+config.test_compiler_has_omp_h = @OPENMP_TEST_COMPILER_HAS_OMP_H@
 config.test_filecheck = "@OPENMP_FILECHECK_EXECUTABLE@"
 config.test_not = "@OPENMP_NOT_EXECUTABLE@"
 config.test_openmp_flags = "@OPENMP_TEST_OPENMP_FLAGS@"