[libunwind] Allow target flags to affect CMake configuration tests

Summary:
This patch changes the libunwind CMake so that it adds certain target flags like '-m32' or '--gcc-toolchain' before including config-ix.cmake.
Since these flags can affect things like check_library_exists([...]) they needed to be added before the tests are performed.

Additionally this patch adds LIBUNWIND_BUILD_32_BITS which defaults to LLVM_BUILD_32_BITS.

This patch fixes:

https://llvm.org/bugs/show_bug.cgi?id=27950
https://llvm.org/bugs/show_bug.cgi?id=27959

Reviewers: jroelofs, danalbert, bcraig, rmaprath, compnerd

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D20889

git-svn-id: https://llvm.org/svn/llvm-project/libunwind/trunk@271458 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7ed1f10..d5ad2f9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -99,14 +99,16 @@
 #===============================================================================
 
 # Define options.
+option(LIBUNWIND_BUILD_32_BITS "Build 32 bit libunwind" ${LLVM_BUILD_32_BITS})
 option(LIBUNWIND_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON)
 option(LIBUNWIND_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
 option(LIBUNWIND_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
 option(LIBUNWIND_ENABLE_SHARED "Build libunwind as a shared library." ON)
 option(LIBUNWIND_ENABLE_CROSS_UNWINDING "Enable cross-platform unwinding support." OFF)
 
-set(LIBUNWIND_GCC_TOOLCHAIN "" CACHE STRING "GCC toolchain for cross compiling.")
-set(LIBUNWIND_SYSROOT "" CACHE STRING "Sysroot for cross compiling.")
+set(LIBUNWIND_TARGET_TRIPLE "" CACHE STRING "Target triple for cross compiling.")
+set(LIBUNWIND_GCC_TOOLCHAIN "" CACHE PATH "GCC toolchain for cross compiling.")
+set(LIBUNWIND_SYSROOT "" CACHE PATH "Sysroot for cross compiling.")
 
 #===============================================================================
 # Configure System
@@ -117,17 +119,15 @@
     "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
     ${CMAKE_MODULE_PATH})
 
-# Configure compiler.
-include(config-ix)
-
 set(LIBUNWIND_COMPILER    ${CMAKE_CXX_COMPILER})
 set(LIBUNWIND_SOURCE_DIR  ${CMAKE_CURRENT_SOURCE_DIR})
 set(LIBUNWIND_BINARY_DIR  ${CMAKE_CURRENT_BINARY_DIR})
 set(LIBUNWIND_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
 
-#===============================================================================
-# Setup Compiler Flags
-#===============================================================================
+set(LIBUNWIND_C_FLAGS "")
+set(LIBUNWIND_CXX_FLAGS "")
+set(LIBUNWIND_COMPILE_FLAGS "")
+set(LIBUNWIND_LINK_FLAGS "")
 
 # Get required flags.
 macro(append_if list condition var)
@@ -136,10 +136,29 @@
   endif()
 endmacro()
 
-set(LIBUNWIND_C_FLAGS "")
-set(LIBUNWIND_CXX_FLAGS "")
-set(LIBUNWIND_COMPILE_FLAGS "")
-set(LIBUNWIND_LINK_FLAGS "")
+macro(add_target_flags_if condition var)
+  if (${condition})
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${var}")
+    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${var}")
+    list(APPEND LINUNWIND_COMPILE_FLAGS ${var})
+    list(APPEND LIBUNWIND_LINK_FLAGS ${var})
+  endif()
+endmacro()
+
+add_target_flags_if(LIBUNWIND_BUILD_32_BITS "-m32")
+add_target_flags_if(LIBUNWIND_TARGET_TRIPLE
+          "-target ${LIBUNWIND_TARGET_TRIPLE}")
+add_target_flags_if(LIBUNWIND_GCC_TOOLCHAIN
+          "-gcc-toolchain ${LIBUNWIND_GCC_TOOLCHAIN}")
+add_target_flags_if(LIBUNWIND_SYSROOT
+          "--sysroot=${LIBUNWIND_SYSROOT}")
+
+# Configure compiler.
+include(config-ix)
+
+#===============================================================================
+# Setup Compiler Flags
+#===============================================================================
 
 append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WERROR_FLAG -Werror=return-type)
 
@@ -213,13 +232,6 @@
   add_definitions(-D_CRT_SECURE_NO_WARNINGS)
 endif ()
 
-append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_TARGET_TRIPLE
-          "-target ${LIBUNWIND_TARGET_TRIPLE}")
-append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_GCC_TOOLCHAIN
-          "-gcc-toolchain ${LIBUNWIND_GCC_TOOLCHAIN}")
-append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_SYSROOT
-          "--sysroot=${LIBUNWIND_SYSROOT}")
-
 #===============================================================================
 # Setup Source Code
 #===============================================================================
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ee01e9a..53b114b 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -94,17 +94,20 @@
 
 string(REPLACE ";" " " LIBUNWIND_COMPILE_FLAGS "${LIBUNWIND_COMPILE_FLAGS}")
 string(REPLACE ";" " " LIBUNWIND_CXX_FLAGS "${LIBUNWIND_CXX_FLAGS}")
+string(REPLACE ";" " " LIBUNWIND_C_FLAGS "${LIBUNWIND_C_FLAGS}")
 string(REPLACE ";" " " LIBUNWIND_LINK_FLAGS "${LIBUNWIND_LINK_FLAGS}")
 
 set_target_properties(unwind
                       PROPERTIES
-                        COMPILE_FLAGS "${CMAKE_COMPILE_FLAGS} ${LIBUNWIND_COMPILE_FLAGS}"
-                        LINK_FLAGS    "${CMAKE_LINK_FLAGS} ${LIBUNWIND_LINK_FLAGS}"
+                        COMPILE_FLAGS "${LIBUNWIND_COMPILE_FLAGS}"
+                        LINK_FLAGS    "${LIBUNWIND_LINK_FLAGS}"
                         OUTPUT_NAME   "unwind"
                         VERSION       "1.0"
                         SOVERSION     "1")
 set_property(SOURCE ${LIBUNWIND_CXX_SOURCES}
-             APPEND_STRING PROPERTY COMPILE_FLAGS "${LIBUNWIND_CXX_FLAGS}")
+             APPEND_STRING PROPERTY COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${LIBUNWIND_CXX_FLAGS}")
+set_property(SOURCE ${LIBUNWIND_C_SOURCES}
+             APPEND_STRING PROPERTY COMPILE_FLAGS "${CMAKE_C_FLAGS} ${LIBUNWIND_C_FLAGS}")
 
 install(TARGETS unwind
         LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}