[CMake] Enhance llvm_update_compile_flags(name sources) to handle LLVM_REQUIRES_EH...
[oota-llvm.git] / cmake / modules / AddLLVM.cmake
index 5bc3c22cf893404f3ef9d56e941a4fec7454f8ca..6996c388b3edeaa986b89a56c0a9e69327a054a0 100644 (file)
@@ -3,20 +3,45 @@ include(LLVMProcessSources)
 include(LLVM-Config)
 
 function(llvm_update_compile_flags name)
-  get_property(target_compile_flags TARGET ${name} PROPERTY COMPILE_FLAGS)
-  if(NOT "${LLVM_COMPILE_FLAGS}" STREQUAL "")
-    set(target_compile_flags "${target_compile_flags} ${LLVM_COMPILE_FLAGS}")
+  set(ALL_SOURCES ${ARGN})
+  if("${ALL_SOURCES}" MATCHES "\\.c(;|$)")
+    set(update_src_props ON)
   endif()
-  if(LLVM_NO_RTTI)
+
+  if(LLVM_REQUIRES_EH)
+    set(LLVM_REQUIRES_RTTI ON)
+  else()
+    if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
+      set(target_compile_flags "${target_compile_flags} -fno-exceptions")
+    elseif(MSVC)
+      list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
+      set(target_compile_flags "${target_compile_flags} /EHs-c-")
+    endif()
+  endif()
+
+  if(NOT LLVM_REQUIRES_RTTI)
     list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
     if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
       set(target_compile_flags "${target_compile_flags} -fno-rtti")
     elseif (MSVC)
-      llvm_replace_compiler_option(target_compile_flags "/GR" "/GR-")
+      set(target_compile_flags "${target_compile_flags} /GR-")
     endif ()
   endif()
 
-  set_property(TARGET ${name} PROPERTY COMPILE_FLAGS "${target_compile_flags}")
+  if(update_src_props)
+    foreach(fn ${ALL_SOURCES})
+      get_filename_component(suf ${fn} EXT)
+      if("${suf}" STREQUAL ".cpp")
+       set_property(SOURCE ${fn} APPEND_STRING PROPERTY
+         COMPILE_FLAGS "${target_compile_flags}")
+      endif()
+    endforeach()
+  else()
+    # Update target props, since all sources are C++.
+    set_property(TARGET ${name} APPEND_STRING PROPERTY
+      COMPILE_FLAGS "${target_compile_flags}")
+  endif()
+
   set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
 endfunction()
 
@@ -137,6 +162,7 @@ macro(add_llvm_library name)
   add_library( ${name} ${ALL_FILES} )
   set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
   set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
+  llvm_update_compile_flags(${name} ${ALL_FILES})
   add_dead_strip( ${name} )
   if( LLVM_COMMON_DEPENDS )
     add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
@@ -196,6 +222,7 @@ ${name} ignored.")
     add_library( ${name} ${libkind} ${ALL_FILES} )
     set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
     set_target_properties( ${name} PROPERTIES PREFIX "" )
+    llvm_update_compile_flags(${name} ${ALL_FILES})
     add_dead_strip( ${name} )
 
     if (LLVM_EXPORTED_SYMBOL_FILE)
@@ -237,6 +264,7 @@ macro(add_llvm_executable name)
   else()
     add_executable(${name} ${ALL_FILES})
   endif()
+  llvm_update_compile_flags(${name} ${ALL_FILES})
   add_dead_strip( ${name} )
 
   if (LLVM_EXPORTED_SYMBOL_FILE)
@@ -374,7 +402,7 @@ function(add_unittest test_suite test_name)
     set(LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
   endif ()
 
-  set(LLVM_NO_RTTI ON)
+  set(LLVM_REQUIRES_RTTI OFF)
 
   add_llvm_executable(${test_name} ${ARGN})
   set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
@@ -390,7 +418,6 @@ function(add_unittest test_suite test_name)
   if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
     set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
   endif ()
-  llvm_update_compile_flags(${test_name})
 endfunction()
 
 # This function provides an automatic way to 'configure'-like generate a file