Add a run_tests target to run all tests.

It's very annoying having to remember the right incant every time I want
to switch around between my build, build-release, build-asan, etc.,
output directories.

Unfortunately, this target is pretty unfriendly without CMake 3.2+ (and
Ninja 1.5+). This combination gives a USES_TERMINAL flag to
add_custom_target which uses Ninja's "console" pool, otherwise the
output buffering gets in the way. Ubuntu LTS is still on an older CMake,
so do a version check in the meantime.

CMake also has its own test mechanism (CTest), but this doesn't use it.
It seems to prefer knowing what all the tests are and then tries to do
its own output management and parallelizing and such. We already have
our own runners. all_tests.go could actually be converted tidily, but
generate_build_files.py also needs to read it, and runner.go has very
specific needs.

Naming the target ninja -C build test would be nice, but CTest squats
that name and CMake grumps when you use a reserved name, so I've gone
with run_tests.

Change-Id: Ibd20ebd50febe1b4e91bb19921f3bbbd9fbcf66c
Reviewed-on: https://boringssl-review.googlesource.com/6270
Reviewed-by: Adam Langley <alangley@gmail.com>
diff --git a/BUILDING.md b/BUILDING.md
index 99adbfd..cc0ee0e 100644
--- a/BUILDING.md
+++ b/BUILDING.md
@@ -122,6 +122,9 @@
 util/all_tests.go`. The latter have to be run separately by running `go test`
 from within `ssl/test/runner`.
 
+Both sets of tests may also be run with `ninja -C build run_tests`, but CMake
+3.2 or later is required to avoid Ninja's output buffering.
+
 
  [1]: http://www.cmake.org/download/
  [2]: http://strawberryperl.com/
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 38dfe45..d0ccd6b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -137,8 +137,27 @@
   set(ARCH "generic")
 endif()
 
+# Declare a dummy target to build all unit tests. Test targets should inject
+# themselves as dependencies next to the target definition.
+add_custom_target(all_tests)
+
 add_subdirectory(crypto)
 add_subdirectory(ssl)
 add_subdirectory(ssl/test)
 add_subdirectory(tool)
 add_subdirectory(decrepit)
+
+if (NOT ${CMAKE_VERSION} VERSION_LESS "3.2")
+  # USES_TERMINAL is only available in CMake 3.2 or later.
+  set(MAYBE_USES_TERMINAL USES_TERMINAL)
+endif()
+
+add_custom_target(
+    run_tests
+    COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
+            ${CMAKE_BINARY_DIR}
+    COMMAND cd ssl/test/runner
+    COMMAND ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
+    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+    DEPENDS all_tests bssl_shim
+    ${MAYBE_USES_TERMINAL})
diff --git a/crypto/CMakeLists.txt b/crypto/CMakeLists.txt
index 3115279..89f4ce5 100644
--- a/crypto/CMakeLists.txt
+++ b/crypto/CMakeLists.txt
@@ -210,6 +210,7 @@
 )
 
 target_link_libraries(constant_time_test crypto)
+add_dependencies(all_tests constant_time_test)
 
 add_executable(
   thread_test
@@ -220,6 +221,7 @@
 )
 
 target_link_libraries(thread_test crypto)
+add_dependencies(all_tests thread_test)
 
 add_executable(
   refcount_test
@@ -228,3 +230,4 @@
 )
 
 target_link_libraries(refcount_test crypto)
+add_dependencies(all_tests refcount_test)
diff --git a/crypto/aes/CMakeLists.txt b/crypto/aes/CMakeLists.txt
index c82d99a..0566e39 100644
--- a/crypto/aes/CMakeLists.txt
+++ b/crypto/aes/CMakeLists.txt
@@ -69,3 +69,4 @@
 )
 
 target_link_libraries(aes_test crypto)
+add_dependencies(all_tests aes_test)
diff --git a/crypto/base64/CMakeLists.txt b/crypto/base64/CMakeLists.txt
index f1dba6c..15ee691 100644
--- a/crypto/base64/CMakeLists.txt
+++ b/crypto/base64/CMakeLists.txt
@@ -17,3 +17,4 @@
 )
 
 target_link_libraries(base64_test crypto)
+add_dependencies(all_tests base64_test)
diff --git a/crypto/bio/CMakeLists.txt b/crypto/bio/CMakeLists.txt
index 8de090a..7859b58 100644
--- a/crypto/bio/CMakeLists.txt
+++ b/crypto/bio/CMakeLists.txt
@@ -30,3 +30,4 @@
 if (WIN32)
   target_link_libraries(bio_test ws2_32)
 endif()
+add_dependencies(all_tests bio_test)
diff --git a/crypto/bn/CMakeLists.txt b/crypto/bn/CMakeLists.txt
index 232e40a..b7130d7 100644
--- a/crypto/bn/CMakeLists.txt
+++ b/crypto/bn/CMakeLists.txt
@@ -76,3 +76,4 @@
 )
 
 target_link_libraries(bn_test crypto)
+add_dependencies(all_tests bn_test)
diff --git a/crypto/bytestring/CMakeLists.txt b/crypto/bytestring/CMakeLists.txt
index 3462aee..33d3c21 100644
--- a/crypto/bytestring/CMakeLists.txt
+++ b/crypto/bytestring/CMakeLists.txt
@@ -19,3 +19,4 @@
 )
 
 target_link_libraries(bytestring_test crypto)
+add_dependencies(all_tests bytestring_test)
diff --git a/crypto/cipher/CMakeLists.txt b/crypto/cipher/CMakeLists.txt
index 6b4c729..52b87b6 100644
--- a/crypto/cipher/CMakeLists.txt
+++ b/crypto/cipher/CMakeLists.txt
@@ -37,3 +37,4 @@
 
 target_link_libraries(cipher_test crypto)
 target_link_libraries(aead_test crypto)
+add_dependencies(all_tests cipher_test aead_test)
diff --git a/crypto/cmac/CMakeLists.txt b/crypto/cmac/CMakeLists.txt
index bb3abc3..a346b24 100644
--- a/crypto/cmac/CMakeLists.txt
+++ b/crypto/cmac/CMakeLists.txt
@@ -17,3 +17,4 @@
 )
 
 target_link_libraries(cmac_test crypto)
+add_dependencies(all_tests cmac_test)
diff --git a/crypto/dh/CMakeLists.txt b/crypto/dh/CMakeLists.txt
index 1a46512..8ddf03d 100644
--- a/crypto/dh/CMakeLists.txt
+++ b/crypto/dh/CMakeLists.txt
@@ -21,3 +21,4 @@
 )
 
 target_link_libraries(dh_test crypto)
+add_dependencies(all_tests dh_test)
diff --git a/crypto/digest/CMakeLists.txt b/crypto/digest/CMakeLists.txt
index 856e45a..7a68f6f 100644
--- a/crypto/digest/CMakeLists.txt
+++ b/crypto/digest/CMakeLists.txt
@@ -18,3 +18,4 @@
 )
 
 target_link_libraries(digest_test crypto)
+add_dependencies(all_tests digest_test)
diff --git a/crypto/dsa/CMakeLists.txt b/crypto/dsa/CMakeLists.txt
index e8b7793..654f18c 100644
--- a/crypto/dsa/CMakeLists.txt
+++ b/crypto/dsa/CMakeLists.txt
@@ -19,3 +19,4 @@
 )
 
 target_link_libraries(dsa_test crypto)
+add_dependencies(all_tests dsa_test)
diff --git a/crypto/ec/CMakeLists.txt b/crypto/ec/CMakeLists.txt
index 2b76759..9808cd5 100644
--- a/crypto/ec/CMakeLists.txt
+++ b/crypto/ec/CMakeLists.txt
@@ -35,3 +35,4 @@
 
 target_link_libraries(example_mul crypto)
 target_link_libraries(ec_test crypto)
+add_dependencies(all_tests example_mul ec_test)
diff --git a/crypto/ecdsa/CMakeLists.txt b/crypto/ecdsa/CMakeLists.txt
index e7581be..0cc672e 100644
--- a/crypto/ecdsa/CMakeLists.txt
+++ b/crypto/ecdsa/CMakeLists.txt
@@ -19,3 +19,4 @@
 )
 
 target_link_libraries(ecdsa_test crypto)
+add_dependencies(all_tests ecdsa_test)
diff --git a/crypto/err/CMakeLists.txt b/crypto/err/CMakeLists.txt
index 8519e51..a095b54 100644
--- a/crypto/err/CMakeLists.txt
+++ b/crypto/err/CMakeLists.txt
@@ -47,3 +47,4 @@
 )
 
 target_link_libraries(err_test crypto)
+add_dependencies(all_tests err_test)
diff --git a/crypto/evp/CMakeLists.txt b/crypto/evp/CMakeLists.txt
index 5d2e918..000f076 100644
--- a/crypto/evp/CMakeLists.txt
+++ b/crypto/evp/CMakeLists.txt
@@ -47,3 +47,4 @@
 target_link_libraries(evp_extra_test crypto)
 target_link_libraries(evp_test crypto)
 target_link_libraries(pbkdf_test crypto)
+add_dependencies(all_tests evp_extra_test evp_test pbkdf_test)
diff --git a/crypto/hkdf/CMakeLists.txt b/crypto/hkdf/CMakeLists.txt
index 53bf558..d9db933 100644
--- a/crypto/hkdf/CMakeLists.txt
+++ b/crypto/hkdf/CMakeLists.txt
@@ -17,3 +17,4 @@
 )
 
 target_link_libraries(hkdf_test crypto)
+add_dependencies(all_tests hkdf_test)
diff --git a/crypto/hmac/CMakeLists.txt b/crypto/hmac/CMakeLists.txt
index 392ce01..179a53b 100644
--- a/crypto/hmac/CMakeLists.txt
+++ b/crypto/hmac/CMakeLists.txt
@@ -18,3 +18,4 @@
 )
 
 target_link_libraries(hmac_test crypto)
+add_dependencies(all_tests hmac_test)
diff --git a/crypto/lhash/CMakeLists.txt b/crypto/lhash/CMakeLists.txt
index ce785eb..4f4dea7 100644
--- a/crypto/lhash/CMakeLists.txt
+++ b/crypto/lhash/CMakeLists.txt
@@ -17,3 +17,4 @@
 )
 
 target_link_libraries(lhash_test crypto)
+add_dependencies(all_tests lhash_test)
diff --git a/crypto/modes/CMakeLists.txt b/crypto/modes/CMakeLists.txt
index 6da5207..41f71d7 100644
--- a/crypto/modes/CMakeLists.txt
+++ b/crypto/modes/CMakeLists.txt
@@ -63,3 +63,4 @@
 )
 
 target_link_libraries(gcm_test crypto)
+add_dependencies(all_tests gcm_test)
diff --git a/crypto/pkcs8/CMakeLists.txt b/crypto/pkcs8/CMakeLists.txt
index ce5bce1..9550109 100644
--- a/crypto/pkcs8/CMakeLists.txt
+++ b/crypto/pkcs8/CMakeLists.txt
@@ -27,3 +27,4 @@
 
 target_link_libraries(pkcs8_test crypto)
 target_link_libraries(pkcs12_test crypto)
+add_dependencies(all_tests pkcs8_test pkcs12_test)
diff --git a/crypto/poly1305/CMakeLists.txt b/crypto/poly1305/CMakeLists.txt
index 674d9f6..1b6a804 100644
--- a/crypto/poly1305/CMakeLists.txt
+++ b/crypto/poly1305/CMakeLists.txt
@@ -28,3 +28,4 @@
 )
 
 target_link_libraries(poly1305_test crypto)
+add_dependencies(all_tests poly1305_test)
diff --git a/crypto/rsa/CMakeLists.txt b/crypto/rsa/CMakeLists.txt
index bd8ad3b..969b753 100644
--- a/crypto/rsa/CMakeLists.txt
+++ b/crypto/rsa/CMakeLists.txt
@@ -21,3 +21,4 @@
 )
 
 target_link_libraries(rsa_test crypto)
+add_dependencies(all_tests rsa_test)
\ No newline at end of file
diff --git a/crypto/x509/CMakeLists.txt b/crypto/x509/CMakeLists.txt
index 258c263..8ffeaa0 100644
--- a/crypto/x509/CMakeLists.txt
+++ b/crypto/x509/CMakeLists.txt
@@ -65,3 +65,4 @@
 )
 
 target_link_libraries(pkcs7_test crypto)
+add_dependencies(all_tests pkcs7_test)
diff --git a/crypto/x509v3/CMakeLists.txt b/crypto/x509v3/CMakeLists.txt
index 5cc1b49..cf2474a 100644
--- a/crypto/x509v3/CMakeLists.txt
+++ b/crypto/x509v3/CMakeLists.txt
@@ -52,6 +52,7 @@
 )
 
 target_link_libraries(v3name_test crypto)
+add_dependencies(all_tests v3name_test)
 
 add_executable(
   tab_test
@@ -62,3 +63,4 @@
 )
 
 target_link_libraries(tab_test crypto)
+add_dependencies(all_tests tab_test)
diff --git a/ssl/CMakeLists.txt b/ssl/CMakeLists.txt
index 3c55e9d..8617639 100644
--- a/ssl/CMakeLists.txt
+++ b/ssl/CMakeLists.txt
@@ -47,3 +47,4 @@
 )
 
 target_link_libraries(ssl_test ssl crypto)
+add_dependencies(all_tests ssl_test)
diff --git a/ssl/pqueue/CMakeLists.txt b/ssl/pqueue/CMakeLists.txt
index 53d2a8b..3a8b82b 100644
--- a/ssl/pqueue/CMakeLists.txt
+++ b/ssl/pqueue/CMakeLists.txt
@@ -17,3 +17,4 @@
 )
 
 target_link_libraries(pqueue_test ssl crypto)
+add_dependencies(all_tests pqueue_test)