Maybe fix generated-CMake build on Android and iOS.

Based on reports, these changes seem like good suspects for build
failures reported, while using the generated CMake build, on iOS and
Android. By checking this in, this change will appear in the
master-with-bazel branch, making it easier for consumers to test.

Change-Id: Icbcebe85f10fad76e3224333c769d0818ae45244
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/39545
Reviewed-by: David Benjamin <davidben@google.com>
diff --git a/util/generate_build_files.py b/util/generate_build_files.py
index af02ed0..92bc08e 100644
--- a/util/generate_build_files.py
+++ b/util/generate_build_files.py
@@ -528,6 +528,20 @@
   endif()
 endif()
 
+# CMake's iOS support uses Apple's multiple-architecture toolchain. It takes an
+# architecture list from CMAKE_OSX_ARCHITECTURES, leaves CMAKE_SYSTEM_PROCESSOR
+# alone, and expects all architecture-specific logic to be conditioned within
+# the source files rather than the build. This does not work for our assembly
+# files, so we fix CMAKE_SYSTEM_PROCESSOR and only support single-architecture
+# builds.
+if(NOT OPENSSL_NO_ASM AND CMAKE_OSX_ARCHITECTURES)
+  list(LENGTH CMAKE_OSX_ARCHITECTURES NUM_ARCHES)
+  if(NOT ${NUM_ARCHES} EQUAL 1)
+    message(FATAL_ERROR "Universal binaries not supported.")
+  endif()
+  list(GET CMAKE_OSX_ARCHITECTURES 0 CMAKE_SYSTEM_PROCESSOR)
+endif()
+
 include_directories(src/include)
 
 '''
@@ -584,8 +598,14 @@
       self.PrintLibrary(cmake, 'crypto',
           files['crypto'] + ['${CRYPTO_ARCH_SOURCES}'])
       self.PrintLibrary(cmake, 'ssl', files['ssl'])
-      self.PrintExe(cmake, 'bssl', files['tool'],
-          ['ssl', 'crypto', '-lpthread'])
+      self.PrintExe(cmake, 'bssl', files['tool'], ['ssl', 'crypto'])
+
+      cmake.write(
+R'''if(NOT MSVC AND NOT ANDROID)
+  target_link_libraries(crypto pthread)
+endif()
+
+''')
 
 def FindCMakeFiles(directory):
   """Returns list of all CMakeLists.txt files recursively in directory."""