[OpenMP] Add extra flags to libomptarget and plugin builds (#74520)

GitOrigin-RevId: 12cbccc3125725fad00022a9b3a52ed9be69c3a3
diff --git a/libomptarget/CMakeLists.txt b/libomptarget/CMakeLists.txt
index e64a175..66925cc 100644
--- a/libomptarget/CMakeLists.txt
+++ b/libomptarget/CMakeLists.txt
@@ -75,6 +75,25 @@
   add_definitions(-DOMPTARGET_DEBUG)
 endif()
 
+# No exceptions and no RTTI, except if requested.
+set(offload_compile_flags -fno-exceptions)
+if(NOT LLVM_ENABLE_RTTI)
+  set(offload_compile_flags ${offload_compile_flags} -fno-rtti)
+endif()
+
+# If LTO is not explicitly disabled we check if we can enable it and do so.
+set(LIBOMPTARGET_USE_LTO TRUE CACHE BOOL "Use LTO for the offload runtimes if available")
+if (LIBOMPTARGET_USE_LTO)
+  include(CheckIPOSupported)
+  check_ipo_supported(RESULT use_lto OUTPUT output)
+  if(use_lto)
+    set(offload_compile_flags ${offload_compile_flags} -flto)
+    set(offload_link_flags ${offload_link_flags} -flto)
+  else()
+    message(WARNING "LTO is not supported: ${output}")
+  endif()
+endif()
+
 # OMPT support for libomptarget
 # Follow host OMPT support and check if host support has been requested.
 # LIBOMP_HAVE_OMPT_SUPPORT indicates whether host OMPT support has been implemented.
diff --git a/libomptarget/plugins-nextgen/common/CMakeLists.txt b/libomptarget/plugins-nextgen/common/CMakeLists.txt
index 5b332ed..8ae3ff2 100644
--- a/libomptarget/plugins-nextgen/common/CMakeLists.txt
+++ b/libomptarget/plugins-nextgen/common/CMakeLists.txt
@@ -88,6 +88,9 @@
   DEBUG_PREFIX="PluginInterface"
 )
 
+target_compile_options(PluginCommon PUBLIC ${offload_compile_flags})
+target_link_options(PluginCommon PUBLIC ${offload_link_flags})
+
 target_include_directories(PluginCommon
   PRIVATE
   ${LIBOMPTARGET_INCLUDE_DIR}
diff --git a/libomptarget/src/CMakeLists.txt b/libomptarget/src/CMakeLists.txt
index 7c07c61..429a670 100644
--- a/libomptarget/src/CMakeLists.txt
+++ b/libomptarget/src/CMakeLists.txt
@@ -55,6 +55,9 @@
   DEBUG_PREFIX="omptarget"
 )
 
+target_compile_options(omptarget PUBLIC ${offload_compile_flags})
+target_link_options(omptarget PUBLIC ${offload_link_flags})
+
 macro(check_plugin_target target)
 if (TARGET omptarget.rtl.${target})
 	list(APPEND LIBOMPTARGET_PLUGINS_TO_LOAD ${target})