[ORC-RT] Re-apply "Initial ORC Runtime directories and build..." with fixes.

This reapplies 1e1d75b190c, which was reverted in ce1a4d53231 due to build
failures.

The unconditional dependencies on clang and llvm-jitlink in
compiler-rt/test/orc/CMakeLists.txt have been removed -- they don't appear to
be necessary, and I suspect they're the cause of the build failures seen
earlier.

GitOrigin-RevId: 5e537ea1d7a937f848ff1c0fc691d5a5afdf2459
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2d5fd9c..c9939ac 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -45,6 +45,8 @@
 mark_as_advanced(COMPILER_RT_BUILD_MEMPROF)
 option(COMPILER_RT_BUILD_XRAY_NO_PREINIT "Build xray with no preinit patching" OFF)
 mark_as_advanced(COMPILER_RT_BUILD_XRAY_NO_PREINIT)
+option(COMPILER_RT_BUILD_ORC "Build ORC runtime" ON)
+mark_as_advanced(COMPILER_RT_BUILD_ORC)
 
 set(COMPILER_RT_ASAN_SHADOW_SCALE ""
     CACHE STRING "Override the shadow scale to be used in ASan runtime")
diff --git a/cmake/config-ix.cmake b/cmake/config-ix.cmake
index 47039a3..317c97e 100644
--- a/cmake/config-ix.cmake
+++ b/cmake/config-ix.cmake
@@ -343,6 +343,10 @@
 endif()
 set(ALL_SHADOWCALLSTACK_SUPPORTED_ARCH ${ARM64})
 
+if (APPLE)
+set(ALL_ORC_SUPPORTED_ARCH ${X86_64})
+endif()
+
 if(APPLE)
   include(CompilerRTDarwinUtils)
 
@@ -399,6 +403,7 @@
   set(TSAN_SUPPORTED_OS osx)
   set(XRAY_SUPPORTED_OS osx)
   set(FUZZER_SUPPORTED_OS osx)
+  set(ORC_SUPPORTED_OS osx)
 
   # Note: In order to target x86_64h on OS X the minimum deployment target must
   # be 10.8 or higher.
@@ -587,6 +592,9 @@
   list_intersect(SHADOWCALLSTACK_SUPPORTED_ARCH
     ALL_SHADOWCALLSTACK_SUPPORTED_ARCH
     SANITIZER_COMMON_SUPPORTED_ARCH)
+  list_intersect(ORC_SUPPORTED_ARCH
+    ALL_ORC_SUPPORTED_ARCH
+    SANITIZER_COMMON_SUPPORTED_ARCH)
 
 else()
   filter_available_targets(CRT_SUPPORTED_ARCH ${ALL_CRT_SUPPORTED_ARCH})
@@ -618,6 +626,7 @@
   filter_available_targets(SHADOWCALLSTACK_SUPPORTED_ARCH
     ${ALL_SHADOWCALLSTACK_SUPPORTED_ARCH})
   filter_available_targets(GWP_ASAN_SUPPORTED_ARCH ${ALL_GWP_ASAN_SUPPORTED_ARCH})
+  filter_available_targets(ORC_SUPPORTED_ARCH ${ALL_ORC_SUPPORTED_ARCH})
 endif()
 
 if (MSVC)
@@ -778,6 +787,12 @@
   set(COMPILER_RT_HAS_XRAY FALSE)
 endif()
 
+if (ORC_SUPPORTED_ARCH)
+  set(COMPILER_RT_HAS_ORC TRUE)
+else()
+  set(COMPILER_RT_HAS_ORC FALSE)
+endif()
+
 if (COMPILER_RT_HAS_SANITIZER_COMMON AND FUZZER_SUPPORTED_ARCH AND
     OS_NAME MATCHES "Android|Darwin|Linux|NetBSD|FreeBSD|Fuchsia|Windows")
   set(COMPILER_RT_HAS_FUZZER TRUE)
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index c1c6880..1437e37 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -66,6 +66,10 @@
   compiler_rt_build_runtime(memprof)
 endif()
 
+if(COMPILER_RT_BUILD_ORC)
+  compiler_rt_build_runtime(orc)
+endif()
+
 # It doesn't normally make sense to build runtimes when a sanitizer is enabled,
 # so we don't add_subdirectory the runtimes in that case. However, the opposite
 # is true for fuzzers that exercise parts of the runtime. So we add the fuzzer
diff --git a/lib/orc/CMakeLists.txt b/lib/orc/CMakeLists.txt
new file mode 100644
index 0000000..4007a46
--- /dev/null
+++ b/lib/orc/CMakeLists.txt
@@ -0,0 +1,84 @@
+# Build for all components of the ORC runtime support library.
+
+# ORC runtime library implementation files.
+set(ORC_SOURCES
+  placeholder.cpp
+  )
+
+# Implementation files for all ORC architectures.
+set(x86_64_SOURCES
+# x86-64 specific assembly files will go here.
+)
+
+set(ORC_IMPL_HEADERS
+# Implementation headers will go here.
+)
+
+# Create list of all source files for
+# consumption by tests.
+set(ORC_ALL_SOURCE_FILES
+  ${ORC_SOURCES}
+  ${x86_64_SOURCES}
+  ${ORC_IMPL_HEADERS}
+  )
+
+list(REMOVE_DUPLICATES ORC_ALL_SOURCE_FILES)
+
+# Now put it all together...
+include_directories(..)
+include_directories(../../include)
+
+set(ORC_CFLAGS ${COMPILER_RT_COMMON_CFLAGS})
+
+# Allow the ORC runtime to reference LLVM headers.
+foreach (DIR ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR})
+  list(APPEND ORC_CFLAGS -I${DIR})
+endforeach()
+
+add_compiler_rt_component(orc)
+
+# ORC uses C++ standard library headers.
+if (TARGET cxx-headers OR HAVE_LIBCXX)
+  set(ORC_DEPS cxx-headers)
+endif()
+
+if (APPLE)
+  add_compiler_rt_object_libraries(RTOrc
+    OS ${ORC_SUPPORTED_OS}
+    ARCHS ${ORC_SUPPORTED_ARCH}
+    SOURCES ${ORC_SOURCES} ${x86_64_SOURCES}
+    ADDITIONAL_HEADERS ${ORC_IMPL_HEADERS}
+    CFLAGS ${ORC_CFLAGS}
+    DEPS ${ORC_DEPS})
+
+  # We only support running on osx for now.
+  add_compiler_rt_runtime(clang_rt.orc
+    STATIC
+    OS ${ORC_SUPPORTED_OS}
+    ARCHS ${ORC_SUPPORTED_ARCH}
+    OBJECT_LIBS RTOrc
+    CFLAGS ${ORC_CFLAGS}
+    LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS} ${WEAK_SYMBOL_LINK_FLAGS}
+    LINK_LIBS ${ORC_LINK_LIBS}
+    PARENT_TARGET orc)
+else() # not Apple
+  foreach(arch ${ORC_SUPPORTED_ARCH})
+    if(NOT CAN_TARGET_${arch})
+      continue()
+    endif()
+    add_compiler_rt_object_libraries(RTOrc
+      ARCHS ${arch}
+      SOURCES ${ORC_SOURCES} ${${arch}_SOURCES}
+      ADDITIONAL_HEADERS ${ORC_IMPL_HEADERS}
+      CFLAGS $ORC_CFLAGS}
+      DEPS ${ORC_DEPS})
+
+    # Common ORC archive for instrumented binaries.
+    add_compiler_rt_runtime(clang_rt.orc
+     STATIC
+     ARCHS ${arch}
+     CFLAGS ${ORC_CFLAGS}
+     OBJECT_LIBS ${ORC_COMMON_RUNTIME_OBJECT_LIBS} RTOrc
+     PARENT_TARGET orc)
+  endforeach()
+endif() # not Apple
diff --git a/lib/orc/placeholder.cpp b/lib/orc/placeholder.cpp
new file mode 100644
index 0000000..c832f60
--- /dev/null
+++ b/lib/orc/placeholder.cpp
@@ -0,0 +1 @@
+void placeholder(void) {}
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 1d123ed..1020cc7 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -78,6 +78,9 @@
   if(COMPILER_RT_BUILD_XRAY)
     compiler_rt_test_runtime(xray)
   endif()
+  if(COMPILER_RT_BUILD_ORC)
+    compiler_rt_Test_runtime(orc)
+  endif()
   if(COMPILER_RT_BUILD_CRT AND COMPILER_RT_HAS_CRT)
     add_subdirectory(crt)
   endif()
diff --git a/test/orc/CMakeLists.txt b/test/orc/CMakeLists.txt
new file mode 100644
index 0000000..b2d31dc
--- /dev/null
+++ b/test/orc/CMakeLists.txt
@@ -0,0 +1,25 @@
+set(ORC_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
+
+set(ORC_TESTSUITES)
+set(ORC_TEST_DEPS ${SANITIZER_COMMON_LIT_TEST_DEPS})
+
+set(ORC_TEST_ARCH ${ORC_SUPPORTED_ARCH})
+if (COMPILER_RT_BUILD_ORC AND COMPILER_RT_HAS_ORC)
+  foreach(arch ${ORC_TEST_ARCH})
+    set(ORC_TEST_TARGET_ARCH ${arch})
+    string(TOLOWER "-${arch}-${OS_NAME}" ORC_TEST_CONFIG_SUFFIX)
+    get_test_cc_for_arch(${arch} ORC_TEST_TARGET_CC ORC_TEST_TARGET_CFLAGS)
+    string(TOUPPER ${arch} ARCH_UPPER_CASE)
+    set(CONFIG_NAME ${ARCH_UPPER_CASE}${OS_NAME}Config)
+
+    configure_lit_site_cfg(
+      ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
+      ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg.py)
+    list(APPEND ORC_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME})
+  endforeach()
+endif()
+
+add_lit_testsuite(check-orc "Running the ORC runtime tests"
+  ${ORC_TESTSUITES}
+  DEPENDS ${ORC_TEST_DEPS})
+set_target_properties(check-orc PROPERTIES FOLDER "Compiler-RT Misc")
diff --git a/test/orc/TestCases/Darwin/lit.local.cfg.py b/test/orc/TestCases/Darwin/lit.local.cfg.py
new file mode 100644
index 0000000..ffa8fd8
--- /dev/null
+++ b/test/orc/TestCases/Darwin/lit.local.cfg.py
@@ -0,0 +1,2 @@
+if config.root.host_os != 'Darwin':
+  config.unsupported = True
diff --git a/test/orc/TestCases/Darwin/x86-64/lit.local.cfg.py b/test/orc/TestCases/Darwin/x86-64/lit.local.cfg.py
new file mode 100644
index 0000000..1b14fb6
--- /dev/null
+++ b/test/orc/TestCases/Darwin/x86-64/lit.local.cfg.py
@@ -0,0 +1,2 @@
+if config.root.host_arch != 'x86_64':
+  config.unsupported = True
diff --git a/test/orc/TestCases/Darwin/x86-64/placeholder_test.S b/test/orc/TestCases/Darwin/x86-64/placeholder_test.S
new file mode 100644
index 0000000..a794e10
--- /dev/null
+++ b/test/orc/TestCases/Darwin/x86-64/placeholder_test.S
@@ -0,0 +1,13 @@
+// RUN: %clang -c -o %t %s
+// RUN: %llvm_jitlink %t
+
+	.section	__TEXT,__text,regular,pure_instructions
+	.build_version macos, 11, 0	sdk_version 11, 3
+
+	.globl	_main
+	.p2align	4, 0x90
+_main:
+	xorl	%eax, %eax
+	retq
+
+.subsections_via_symbols
diff --git a/test/orc/lit.cfg.py b/test/orc/lit.cfg.py
new file mode 100644
index 0000000..4e39523
--- /dev/null
+++ b/test/orc/lit.cfg.py
@@ -0,0 +1,33 @@
+# -*- Python -*-
+
+import os
+
+# Setup config name.
+config.name = 'ORC' + config.name_suffix
+
+# Setup source root.
+config.test_source_root = os.path.dirname(__file__)
+
+def build_invocation(compile_flags):
+  return ' ' + ' '.join([config.clang] + compile_flags) + ' '
+
+# Assume that llvm-jitlink is in the config.llvm_tools_dir.
+llvm_jitlink = os.path.join(config.llvm_tools_dir, 'llvm-jitlink')
+
+config.substitutions.append(
+    ('%clang ', build_invocation([config.target_cflags])))
+config.substitutions.append(
+    ('%clangxx ',
+     build_invocation(config.cxx_mode_flags + [config.target_cflags])))
+config.substitutions.append(
+    ('%llvm_jitlink', llvm_jitlink))
+config.substitutions.append(
+    ('%orc_rt',
+        ('-L%s -lclang_rt.orc%s')
+        % (config.compiler_rt_libdir, config.target_suffix)))
+
+# Default test suffixes.
+config.suffixes = ['.c', '.cpp', '.S']
+
+if config.host_os not in ['Darwin']:
+  config.unsupported = True
diff --git a/test/orc/lit.site.cfg.py.in b/test/orc/lit.site.cfg.py.in
new file mode 100644
index 0000000..d5bb51c
--- /dev/null
+++ b/test/orc/lit.site.cfg.py.in
@@ -0,0 +1,14 @@
+@LIT_SITE_CFG_IN_HEADER@
+
+# Tool-specific config options.
+config.name_suffix = "@ORC_TEST_CONFIG_SUFFIX@"
+config.orc_lit_source_dir = "@ORC_LIT_SOURCE_DIR@"
+config.target_cflags = "@ORC_TEST_TARGET_CFLAGS@"
+config.target_arch = "@ORC_TEST_TARGET_ARCH@"
+config.built_with_llvm = ("@COMPILER_RT_STANDALONE_BUILD@" != "TRUE")
+
+# Load common config for all compiler-rt lit tests
+lit_config.load_config(config, "@COMPILER_RT_BINARY_DIR@/test/lit.common.configured")
+
+# Load tool-specific config that would do the real work.
+lit_config.load_config(config, "@CMAKE_CURRENT_SOURCE_DIR@/lit.cfg.py")