Honor the standard BUILD_SHARED_LIBS cmake variable.

The variable switches the default type for add_library from STATIC to SHARED.
We can condition additional stuff on that for convenience. (tabtest still
doesn't build.)

BoringSSL as any kind of stable system shared library is still very much
unsupported, but this is probably handy for making sure we don't forget all
those pesky OPENSSL_EXPORTs.

Change-Id: I66ab80bcddbf3724e03e85384141fdf4f4acbc2e
Reviewed-on: https://boringssl-review.googlesource.com/3092
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/BUILDING b/BUILDING
index 830f58b..18ddde1 100644
--- a/BUILDING
+++ b/BUILDING
@@ -40,7 +40,7 @@
 because the build scripts will detect changes to them and rebuild themselves
 automatically.
 
-Note that the default build flags in the top-leve CMakeLists.txt are for
+Note that the default build flags in the top-level CMakeLists.txt are for
 debugging - optimisation isn't enabled.
 
 If you want to cross-compile then there are example toolchain files for 32-bit
@@ -49,12 +49,10 @@
 
   cmake -DCMAKE_TOOLCHAIN_FILE=../util/arm-toolchain.cmake -GNinja ..
 
-If you want to build as a shared library you need to tweak the STATIC tags in
-the CMakeLists.txts and also define BORINGSSL_SHARED_LIBRARY and
-BORINGSSL_IMPLEMENTATION. On Windows, where functions need to be tagged with
-"dllimport" when coming from a shared library, you need just
-BORINGSSL_SHARED_LIBRARY defined in the code which #includes the BoringSSL
-headers.
+If you want to build as a shared library, pass -DBUILD_SHARED_LIBS=1. On
+Windows, where functions need to be tagged with "dllimport" when coming from a
+shared library, define BORINGSSL_SHARED_LIBRARY in any code which #includes the
+BoringSSL headers.
 
 Known Limitations on Windows:
 
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 199d44d..6d2c39f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -19,6 +19,13 @@
 
 add_definitions(-DBORINGSSL_IMPLEMENTATION)
 
+if (BUILD_SHARED_LIBS)
+	add_definitions(-DBORINGSSL_SHARED_LIBRARY)
+	# Enable position-independent code globally. This is needed because
+	# some library targets are OBJECT libraries.
+	set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
+endif()
+
 if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
 	set(ARCH "x86_64")
 elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
diff --git a/crypto/CMakeLists.txt b/crypto/CMakeLists.txt
index 6fa0a26..691f79e 100644
--- a/crypto/CMakeLists.txt
+++ b/crypto/CMakeLists.txt
@@ -134,7 +134,6 @@
 
 add_library(
 	crypto
-	STATIC
 
 	crypto.c
 	crypto_error.c
diff --git a/ssl/CMakeLists.txt b/ssl/CMakeLists.txt
index 954418b..a7292db 100644
--- a/ssl/CMakeLists.txt
+++ b/ssl/CMakeLists.txt
@@ -4,7 +4,6 @@
 
 add_library(
 	ssl
-	STATIC
 
 	d1_both.c
 	d1_clnt.c