Add cmake option ENABLE_BITCODE for iOS builds

Change-Id: Ib56c711c626044754c1c3a397d7c4648c4ec089d
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f24f173..b247c71 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -153,6 +153,8 @@
 if (APPLE)
   option(ACCELERATESPARSE
     "Enable use of sparse solvers in Apple's Accelerate framework." ON)
+  option(ENABLE_BITCODE
+    "Enable bitcode for iOS builds (disables inline optimizations for Eigen)." OFF)
 endif()
 option(CUDA "Enable use of CUDA linear algebra solvers." ON)
 option(LAPACK "Enable use of LAPACK directly within Ceres." ON)
@@ -631,14 +633,20 @@
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CERES_STRICT_CXX_FLAGS}")
 endif (UNIX)
 
-# Use a larger inlining threshold for Clang, since it hobbles Eigen,
-# resulting in an unreasonably slow version of the blas routines. The
-# -Qunused-arguments is needed because CMake passes the inline
-# threshold to the linker and clang complains about it and dies.
 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") # Matches Clang & AppleClang.
-  set(CMAKE_CXX_FLAGS
-    "${CMAKE_CXX_FLAGS} -Qunused-arguments -mllvm -inline-threshold=600")
-
+  # Optimize for Eigen OR enable bitcode; you cannot do both since bitcode is an
+  # intermediate representation.
+  if (ENABLE_BITCODE)
+    set(CMAKE_CXX_FLAGS
+      "${CMAKE_CXX_FLAGS} -fembed-bitcode")
+  else ()
+    # Use a larger inlining threshold for Clang, since it hobbles Eigen,
+    # resulting in an unreasonably slow version of the blas routines. The
+    # -Qunused-arguments is needed because CMake passes the inline
+    # threshold to the linker and clang complains about it and dies.
+    set(CMAKE_CXX_FLAGS
+      "${CMAKE_CXX_FLAGS} -Qunused-arguments -mllvm -inline-threshold=600")
+  endif ()
   # Older versions of Clang (<= 2.9) do not support the 'return-type-c-linkage'
   # option, so check for its presence before adding it to the default flags set.
   include(CheckCXXCompilerFlag)