Merge to upstream r225300.

Change-Id: I2b23715db9ac129ff80aa78ad5824db0a4d6fbb3
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f12c525..99a4a33 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -27,7 +27,15 @@
  )
 
 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
+  set(LIBCXX_LIBDIR_SUFFIX "" CACHE STRING
+      "Define suffix of library directory name (32/64)")
+
+  set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LIBCXX_LIBDIR_SUFFIX})
+  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LIBCXX_LIBDIR_SUFFIX})
+
   set(LIBCXX_BUILT_STANDALONE 1)
+else()
+  set(LIBCXX_LIBDIR_SUFFIX ${LLVM_LIBDIR_SUFFIX})
 endif()
 
 #===============================================================================
@@ -42,6 +50,12 @@
 option(LIBCXX_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
 option(LIBCXX_ENABLE_CXX1Y "Enable -std=c++1y and use of c++1y language features if the compiler supports it." OFF)
 option(LIBCXX_ENABLE_SHARED "Build libc++ as a shared library." ON)
+option(LIBCXX_ENABLE_THREADS "Build libc++ with support for threads." ON)
+option(LIBCXX_BUILD_32_BITS "Build 32 bit libc++" OFF)
+option(LIBCXX_ENABLE_MONOTONIC_CLOCK
+  "Build libc++ with support for a monotonic clock.
+   This option may only be used when LIBCXX_ENABLE_THREADS=OFF." ON)
+option(LIBCXX_INSTALL_HEADERS "Install the libc++ headers." ON)
 option(LIBCXX_INSTALL_SUPPORT_HEADERS "Install libc++ support headers." ON)
 if (LIBCXX_BUILT_STANDALONE)
   set(LLVM_USE_SANITIZER "" CACHE STRING
@@ -84,6 +98,10 @@
   )
 set(LIBCXX_TARGET_TRIPLE ${LIBCXX_TARGET_TRIPLE} CACHE STRING "Target triple.")
 
+set(LIBCXX_COMPILER    ${CMAKE_CXX_COMPILER})
+set(LIBCXX_SOURCE_DIR  ${CMAKE_CURRENT_SOURCE_DIR})
+set(LIBCXX_BINARY_DIR  ${CMAKE_CURRENT_BINARY_DIR})
+set(LIBCXX_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBCXX_LIBDIR_SUFFIX})
 
 # Declare libc++ configuration variables.
 # They are intended for use as follows:
@@ -201,11 +219,31 @@
   list(APPEND LIBCXX_COMPILE_FLAGS -D_LIBCPP_BUILD_STATIC)
 endif()
 
+if (CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32)
+  if (LIBCXX_BUILD_32_BITS)
+    message(STATUS "Building 32 bits executables and libraries.")
+    list(APPEND LIBCXX_CXX_FLAGS "-m32")
+  endif()
+elseif(LIBCXX_BUILD_32_BITS)
+  message(FATAL_ERROR "LIBCXX_BUILD_32_BITS=ON is not supported on this platform.")
+endif()
 # This is the _ONLY_ place where add_definitions is called.
 if (MSVC)
   add_definitions(-D_CRT_SECURE_NO_WARNINGS)
 endif()
 
+# LIBCXX_ENABLE_THREADS configuration
+if (NOT LIBCXX_ENABLE_THREADS)
+  add_definitions(-D_LIBCPP_HAS_NO_THREADS)
+  if (NOT LIBCXX_ENABLE_MONOTONIC_CLOCK)
+    add_definitions(-D_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
+  endif()
+# Ensure LIBCXX_ENABLE_MONOTONIC_CLOCK is set to ON.
+elseif(NOT LIBCXX_ENABLE_MONOTONIC_CLOCK)
+  message(FATAL_ERROR "LIBCXX_ENABLE_MONOTONIC_CLOCK can only be set to OFF"
+                      " when LIBCXX_ENABLE_THREADS is also set to OFF.")
+endif()
+
 # Configure for sanitizers. If LIBCXX_BUILT_STANDALONE then we have to do
 # the flag translation ourselves. Othewise LLVM's CMakeList.txt will handle it.
 if (LIBCXX_BUILT_STANDALONE)