From 8c46d9181ff3172427066d5ccde3584f707482b8 Mon Sep 17 00:00:00 2001
From: Peter Wu <peter@lekensteyn.nl>
Date: Mon, 15 Feb 2016 01:57:13 +0100
Subject: [PATCH] cmake: process tests directory

Split the nghttp2 library into objects and a shared library from those
objects. This is needed because of symbol visibility. An advantage over
the autotools build is that there are no worries about static versus
static library builds.

Test:

    cmake $srcdir
    make nghttpx-unittest main failmalloc
    make test
---
 CMakeLists.txt       |  6 +----
 lib/CMakeLists.txt   |  3 ++-
 src/CMakeLists.txt   | 43 ++++++++++++++++++++---------------
 tests/CMakeLists.txt | 53 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 81 insertions(+), 24 deletions(-)
 create mode 100644 tests/CMakeLists.txt

diff --git a/CMakeLists.txt b/CMakeLists.txt
index a669c4ba..938f0c2b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -140,8 +140,6 @@ cmake_pop_check_state()
 
 
 # Checks for libraries.
-# Additional libraries required for tests.
-set(TEST_LIBRARIES)
 # Additional libraries required for programs under src directory.
 set(APP_LIBRARIES)
 
@@ -163,6 +161,7 @@ endif()
 
 find_package(CUnit 2.1)
 if(CUNIT_FOUND)
+  enable_testing()
   set(HAVE_CUNIT 1)
 else()
   set(HAVE_CUNIT 0)
@@ -457,7 +456,6 @@ endif()
 # # propagate $enable_static to tests/Makefile.am
 # AM_CONDITIONAL([ENABLE_STATIC], [test "x$enable_static" = "xyes"])
 #
-# AC_SUBST([TESTLDADD])
 # AC_SUBST([APPLDFLAGS])
 
 add_definitions(-DHAVE_CONFIG_H)
@@ -519,10 +517,8 @@ add_subdirectory(src)
 #add_subdirectory(src/includes)
 add_subdirectory(examples)
 add_subdirectory(python)
-if(0)
 add_subdirectory(tests)
 #add_subdirectory(tests/testdata)
-endif()
 add_subdirectory(integration-tests)
 add_subdirectory(doc)
 add_subdirectory(contrib)
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 114749b1..a7520f5b 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -24,7 +24,8 @@ set(NGHTTP2_SOURCES
   nghttp2_http.c
 )
 
-add_library(nghttp2 SHARED ${NGHTTP2_SOURCES})
+add_library(nghttp2-obj OBJECT ${NGHTTP2_SOURCES})
+add_library(nghttp2 SHARED $<TARGET_OBJECTS:nghttp2-obj>)
 #target_link_libraries(nghttp2 ...)
 set_target_properties(nghttp2 PROPERTIES
   COMPILE_FLAGS "${WARNCFLAGS} ${EXTRACFLAG}"
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 81c2cb59..4057ac86 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -155,23 +155,30 @@ if(ENABLE_APP)
 
   if(HAVE_CUNIT)
   # check_PROGRAMS += nghttpx-unittest
-  # nghttpx_unittest_SOURCES = shrpx-unittest.cc
-  #   shrpx_ssl_test.cc
-  #   shrpx_downstream_test.cc
-  #   shrpx_config_test.cc
-  #   shrpx_http_test.cc
-  #   http2_test.cc
-  #   util_test.cc
-  #   nghttp2_gzip_test.c
-  #   nghttp2_gzip.c
-  #   buffer_test.cc
-  #   memchunk_test.cc
-  #   template_test.cc
-  #   base64_test.cc
-  # nghttpx_unittest_CPPFLAGS = ${AM_CPPFLAGS}
-  #   -DNGHTTP2_TESTS_DIR=\"$(top_srcdir)/tests\"
-  # nghttpx_unittest_LDADD = libnghttpx.a ${LDADD} @CUNIT_LIBS@ @TESTLDADD@
-  # 
+    set(NGHTTPX_UNITTEST_SOURCES
+      shrpx-unittest.cc
+      shrpx_ssl_test.cc
+      shrpx_downstream_test.cc
+      shrpx_config_test.cc
+      shrpx_http_test.cc
+      http2_test.cc
+      util_test.cc
+      nghttp2_gzip_test.c
+      nghttp2_gzip.c
+      buffer_test.cc
+      memchunk_test.cc
+      template_test.cc
+      base64_test.cc
+    )
+    add_executable(nghttpx-unittest EXCLUDE_FROM_ALL
+      ${NGHTTPX_UNITTEST_SOURCES}
+      $<TARGET_OBJECTS:http-parser>
+    )
+    target_include_directories(nghttpx-unittest PRIVATE ${CUNIT_INCLUDE_DIRS})
+    target_compile_definitions(nghttpx-unittest
+      PRIVATE "-DNGHTTP2_TESTS_DIR=\"$(CMAKE_SOURCE_DIR)/tests\""
+    )
+    target_link_libraries(nghttpx-unittest nghttpx ${CUNIT_LIBRARIES})
   # if HAVE_MRUBY
   # nghttpx_unittest_CPPFLAGS +=
   #   -I${top_srcdir}/third-party/mruby/include @LIBMRUBY_CFLAGS@
@@ -184,7 +191,7 @@ if(ENABLE_APP)
   # nghttpx_unittest_LDADD += ${top_builddir}/third-party/libneverbleed.la
   # endif # HAVE_NEVERBLEED
   # 
-  # TESTS += nghttpx-unittest
+    add_test(nghttpx-unittest nghttpx-unittest)
   endif()
 
   add_executable(nghttp   ${NGHTTP_SOURCES}   $<TARGET_OBJECTS:http-parser>)
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
new file mode 100644
index 00000000..1ffa16c7
--- /dev/null
+++ b/tests/CMakeLists.txt
@@ -0,0 +1,53 @@
+# XXX testdata/: EXTRA_DIST = cacert.pem  index.html  privkey.pem
+if(HAVE_CUNIT)
+  # XXX replace this by lists (WARNCFLAGS_list)
+  string(REPLACE " " ";" c_flags "${WARNCFLAGS}")
+  add_compile_options(${c_flags})
+
+  include_directories(
+    "${CMAKE_SOURCE_DIR}/lib/includes"
+    "${CMAKE_SOURCE_DIR}/lib"
+    "${CMAKE_SOURCE_DIR}/src/includes"
+    "${CMAKE_BINARY_DIR}/lib/includes"
+    ${CUNIT_INCLUDE_DIRS}
+  )
+
+  set(MAIN_SOURCES
+    main.c nghttp2_pq_test.c nghttp2_map_test.c nghttp2_queue_test.c
+    nghttp2_test_helper.c
+    nghttp2_frame_test.c
+    nghttp2_stream_test.c
+    nghttp2_session_test.c
+    nghttp2_hd_test.c
+    nghttp2_npn_test.c
+    nghttp2_helper_test.c
+    nghttp2_buf_test.c
+  )
+
+  add_executable(main EXCLUDE_FROM_ALL
+    ${MAIN_SOURCES}
+    $<TARGET_OBJECTS:nghttp2-obj>
+  )
+  target_include_directories(main PRIVATE ${CUNIT_INCLUDE_DIRS})
+  target_link_libraries(main ${CUNIT_LIBRARIES})
+  add_test(main main)
+
+  if(ENABLE_FAILMALLOC)
+    set(FAILMALLOC_SOURCES
+      failmalloc.c failmalloc_test.c
+      malloc_wrapper.c
+      nghttp2_test_helper.c
+    )
+    add_executable(failmalloc EXCLUDE_FROM_ALL
+      ${FAILMALLOC_SOURCES}
+      $<TARGET_OBJECTS:nghttp2-obj>
+    )
+    target_link_libraries(failmalloc ${CUNIT_LIBRARIES})
+    add_test(failmalloc failmalloc)
+  endif()
+
+  if(ENABLE_APP)
+    # EXTRA_DIST = end_to_end.py
+    # TESTS += end_to_end.py
+  endif()
+endif()
-- 
2.26.2