Simplify CMake rules in HandleLLVMOptions module.
authorAlexey Samsonov <samsonov@google.com>
Wed, 13 Mar 2013 20:50:23 +0000 (20:50 +0000)
committerAlexey Samsonov <samsonov@google.com>
Wed, 13 Mar 2013 20:50:23 +0000 (20:50 +0000)
Summary: No functionality change.

Reviewers: Bigcheese

Reviewed By: Bigcheese

CC: llvm-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D535

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@176973 91177308-0d34-0410-b5e6-96231b3b80d8

cmake/modules/HandleLLVMOptions.cmake

index a83be41b228c7b15fccd11052ed75c5bda3499b8..5c37ebd05e076b1b1feba0656a8e8722ecb655dc 100644 (file)
@@ -59,6 +59,24 @@ else(WIN32)
   endif(UNIX)
 endif(WIN32)
 
+function(add_flag_or_print_warning flag)
+  check_c_compiler_flag(${flag} C_SUPPORTS_${flag})
+  check_cxx_compiler_flag(${flag} CXX_SUPPORTS_${flag})
+  if (C_SUPPORTS_${flag} AND CXX_SUPPORTS_${flag})
+    message(STATUS "Building with ${flag}")
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE)
+    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE)
+  else()
+    message(WARNING "${flag} is not supported.")
+  endif()
+endfunction()
+
+function(append_if variable value condition)
+  if (${condition})
+    set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
+  endif()
+endfunction()
+
 if( LLVM_ENABLE_PIC )
   if( XCODE )
     # Xcode has -mdynamic-no-pic on by default, which overrides -fPIC. I don't
@@ -67,23 +85,14 @@ if( LLVM_ENABLE_PIC )
   elseif( WIN32 OR CYGWIN)
     # On Windows all code is PIC. MinGW warns if -fPIC is used.
   else()
-    check_cxx_compiler_flag("-fPIC" SUPPORTS_FPIC_FLAG)
-    if( SUPPORTS_FPIC_FLAG )
-      message(STATUS "Building with -fPIC")
-      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
-      set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
-    else( SUPPORTS_FPIC_FLAG )
-      message(WARNING "-fPIC not supported.")
-    endif()
+    add_flag_or_print_warning("-fPIC")
 
     if( WIN32 OR CYGWIN)
       # MinGW warns if -fvisibility-inlines-hidden is used.
     else()
       check_cxx_compiler_flag("-fvisibility-inlines-hidden" SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG)
-      if( SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG )
-        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden")
-      endif()
-     endif()
+      append_if(CMAKE_CXX_FLAGS "-fvisibility-inlines-hidden" SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG)
+    endif()
   endif()
 endif()
 
@@ -194,24 +203,14 @@ elseif( LLVM_COMPILER_IS_GCC_COMPATIBLE )
     if (LLVM_ENABLE_PEDANTIC)
       add_llvm_definitions( -pedantic -Wno-long-long )
       check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
-      if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
-        set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
-      endif()
+      append_if(CMAKE_CXX_FLAGS "-Wno-nested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
     endif (LLVM_ENABLE_PEDANTIC)
     check_cxx_compiler_flag("-Werror -Wcovered-switch-default" CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG)
-    if( CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG )
-      set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wcovered-switch-default" )
-    endif()
+    append_if(CMAKE_CXX_FLAGS "-Wcovered-switch-default" CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG)
     check_c_compiler_flag("-Werror -Wcovered-switch-default" C_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG)
-    if( C_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG )
-      set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wcovered-switch-default" )
-    endif()
-    if (USE_NO_UNINITIALIZED)
-      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-uninitialized")
-    endif()
-    if (USE_NO_MAYBE_UNINITIALIZED)
-      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-maybe-uninitialized")
-    endif()
+    append_if(CMAKE_C_FLAGS "-Wcovered-switch-default" C_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG)
+    append_if(CMAKE_CXX_FLAGS "-Wno-uninitialized" USE_NO_UNINITIALIZED)
+    append_if(CMAKE_CXX_FLAGS "-Wno-maybe-uninitialized" USE_NO_MAYBE_UNINITIALIZED)
   endif (LLVM_ENABLE_WARNINGS)
   if (LLVM_ENABLE_WERROR)
     add_llvm_definitions( -Werror )