1 # nghttp2 - HTTP/2 C Library
3 # Copyright (c) 2012, 2013, 2014, 2015 Tatsuhiro Tsujikawa
4 # Copyright (c) 2016 Peter Wu <peter@lekensteyn.nl>
6 # Permission is hereby granted, free of charge, to any person obtaining
7 # a copy of this software and associated documentation files (the
8 # "Software"), to deal in the Software without restriction, including
9 # without limitation the rights to use, copy, modify, merge, publish,
10 # distribute, sublicense, and/or sell copies of the Software, and to
11 # permit persons to whom the Software is furnished to do so, subject to
12 # the following conditions:
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 cmake_minimum_required(VERSION 3.0)
26 # XXX using 1.8.90 instead of 1.9.0-DEV
27 project(nghttp2 VERSION 1.46.0)
29 # See versioning rule:
30 # https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
35 set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
38 math(EXPR LT_SOVERSION "${LT_CURRENT} - ${LT_AGE}")
39 set(LT_VERSION "${LT_SOVERSION}.${LT_AGE}.${LT_REVISION}")
40 set(PACKAGE_VERSION "${PROJECT_VERSION}")
41 HexVersion(PACKAGE_VERSION_NUM ${PROJECT_VERSION_MAJOR} ${PROJECT_VERSION_MINOR} ${PROJECT_VERSION_PATCH})
43 if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
44 set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the build type" FORCE)
46 # Include "None" as option to disable any additional (optimization) flags,
47 # relying on just CMAKE_C_FLAGS and CMAKE_CXX_FLAGS (which are empty by
48 # default). These strings are presented in cmake-gui.
49 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
50 "None" "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
53 include(GNUInstallDirs)
55 # For Python bindings and documentation
56 # (Must be called before PythonLibs for matching versions.)
57 find_package(PythonInterp)
59 # Auto-detection of features that can be toggled
60 find_package(OpenSSL 1.0.1)
61 find_package(Libev 4.11)
62 find_package(Libcares 1.7.5)
63 find_package(ZLIB 1.2.3)
64 find_package(Libngtcp2 0.0.0)
65 find_package(Libngtcp2_crypto_openssl 0.0.0)
66 if(LIBNGTCP2_CRYPTO_OPENSSL_FOUND)
67 set(HAVE_LIBNGTCP2_CRYPTO_OPENSSL 1)
69 find_package(Libnghttp3 0.0.0)
70 find_package(Libbpf 0.4.0)
71 if(OPENSSL_FOUND AND LIBEV_FOUND AND ZLIB_FOUND)
72 set(ENABLE_APP_DEFAULT ON)
74 set(ENABLE_APP_DEFAULT OFF)
76 find_package(Systemd 209)
77 find_package(Jansson 2.5)
78 set(ENABLE_HPACK_TOOLS_DEFAULT ${JANSSON_FOUND})
79 # 2.0.8 is required because we use evconnlistener_set_error_cb()
80 find_package(Libevent 2.0.8 COMPONENTS libevent openssl)
81 set(ENABLE_EXAMPLES_DEFAULT ${LIBEVENT_OPENSSL_FOUND})
83 find_package(PythonLibs)
84 if(CYTHON_FOUND AND PYTHONLIBS_FOUND)
85 set(ENABLE_PYTHON_BINDINGS_DEFAULT ON)
87 set(ENABLE_PYTHON_BINDINGS_DEFAULT OFF)
90 find_package(LibXml2 2.6.26)
91 set(WITH_LIBXML2_DEFAULT ${LIBXML2_FOUND})
92 find_package(Jemalloc)
93 set(WITH_JEMALLOC_DEFAULT ${JEMALLOC_FOUND})
95 include(CMakeOptions.txt)
97 if(ENABLE_LIB_ONLY AND (ENABLE_APP OR ENABLE_HPACK_TOOLS OR ENABLE_EXAMPLES OR
98 ENABLE_PYTHON_BINDINGS))
99 # Remember when disabled options are disabled for later diagnostics.
100 set(ENABLE_LIB_ONLY_DISABLED_OTHERS 1)
102 set(ENABLE_LIB_ONLY_DISABLED_OTHERS 0)
106 set(ENABLE_HPACK_TOOLS OFF)
107 set(ENABLE_EXAMPLES OFF)
108 set(ENABLE_PYTHON_BINDINGS OFF)
111 # Do not disable assertions based on CMAKE_BUILD_TYPE.
112 foreach(_build_type "Release" "MinSizeRel" "RelWithDebInfo")
114 string(TOUPPER "CMAKE_${_lang}_FLAGS_${_build_type}" _var)
115 string(REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " " ${_var} "${${_var}}")
119 if(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang")
120 set(HINT_NORETURN "__attribute__((noreturn))")
125 include(ExtractValidFlags)
126 foreach(_cxx1x_flag -std=c++14)
127 extract_valid_cxx_flags(_cxx1x_flag_supported ${_cxx1x_flag})
128 if(_cxx1x_flag_supported)
129 set(CXX1XCXXFLAGS ${_cxx1x_flag})
134 include(CMakePushCheckState)
135 include(CheckCXXSourceCompiles)
136 cmake_push_check_state()
137 set(CMAKE_REQUIRED_DEFINITIONS "${CXX1XCXXFLAGS}")
138 # Check that std::future is available.
139 check_cxx_source_compiles("
142 int main() { std::vector<std::future<int>> v; }" HAVE_STD_FUTURE)
143 # Check that std::map::emplace is available for g++-4.7.
144 check_cxx_source_compiles("
146 int main() { std::map<int, int>().emplace(1, 2); }" HAVE_STD_MAP_EMPLACE)
147 cmake_pop_check_state()
150 # Checks for libraries.
151 # Additional libraries required for programs under src directory.
154 if(ENABLE_PYTHON_BINDINGS)
155 if(NOT (CYTHON_FOUND AND PYTHONLIBS_FOUND))
156 message(FATAL_ERROR "python bindings were requested "
157 "(ENABLE_PYTHON_BINDINGS=1) but dependencies are not met.")
159 if(NOT PYTHON_VERSION_STRING STREQUAL PYTHONLIBS_VERSION_STRING)
161 "Python executable and library must have the same version!"
162 " Found Python ${PYTHON_VERSION_STRING} and"
163 " PythonLibs ${PYTHONLIBS_VERSION_STRING}"
168 set(CMAKE_THREAD_PREFER_PTHREAD 1)
169 find_package(Threads)
170 if(CMAKE_USE_PTHREADS_INIT)
171 list(APPEND APP_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
173 # XXX android and C++, is this still needed in cmake?
177 # # android does not need -pthread, but needs followng 3 libs for C++
178 # APPLDFLAGS="$APPLDFLAGS -lstdc++ -latomic -lsupc++"
180 # dl: openssl requires libdl when it is statically linked.
181 # XXX shouldn't ${CMAKE_DL_LIBS} be appended to OPENSSL_LIBRARIES instead of
182 # APP_LIBRARIES if it is really specific to OpenSSL?
184 find_package(CUnit 2.1)
186 set(HAVE_CUNIT ${CUNIT_FOUND})
188 add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND})
192 include(CheckSymbolExists)
193 set(HAVE_OPENSSL ${OPENSSL_FOUND})
195 set(OPENSSL_INCLUDE_DIRS ${OPENSSL_INCLUDE_DIR})
196 cmake_push_check_state()
197 set(CMAKE_REQUIRED_INCLUDES "${OPENSSL_INCLUDE_DIR}")
198 set(CMAKE_REQUIRED_LIBRARIES "${OPENSSL_LIBRARIES}")
199 check_symbol_exists(SSL_is_quic "openssl/ssl.h" HAVE_SSL_IS_QUIC)
200 if(NOT HAVE_SSL_IS_QUIC)
201 message(WARNING "OpenSSL in ${OPENSSL_LIBRARIES} dose not have SSL_is_quic. HTTP/3 support cannot be enabled")
203 cmake_pop_check_state()
205 set(OPENSSL_INCLUDE_DIRS "")
206 set(OPENSSL_LIBRARIES "")
209 set(HAVE_LIBEV ${LIBEV_FOUND})
210 set(HAVE_ZLIB ${ZLIB_FOUND})
211 set(HAVE_SYSTEMD ${SYSTEMD_FOUND})
212 set(HAVE_LIBEVENT_OPENSSL ${LIBEVENT_FOUND})
214 # Must both link the core and openssl libraries.
215 set(LIBEVENT_OPENSSL_LIBRARIES ${LIBEVENT_LIBRARIES})
217 # libc-ares (for src)
218 set(HAVE_LIBCARES ${LIBCARES_FOUND})
220 set(LIBCARES_INCLUDE_DIRS ${LIBCARES_INCLUDE_DIR})
222 set(LIBCARES_INCLUDE_DIRS "")
223 set(LIBCARES_LIBRARIES "")
225 # jansson (for src/nghttp, src/deflatehd and src/inflatehd)
226 set(HAVE_JANSSON ${JANSSON_FOUND})
227 # libxml2 (for src/nghttp)
228 set(HAVE_LIBXML2 ${LIBXML2_FOUND})
230 set(LIBXML2_INCLUDE_DIRS ${LIBXML2_INCLUDE_DIR})
232 set(LIBXML2_INCLUDE_DIRS "")
233 set(LIBXML2_LIBRARIES "")
236 set(HAVE_JEMALLOC ${JEMALLOC_FOUND})
239 find_package(Boost 1.54.0 REQUIRED system thread)
243 set(HAVE_LIBBPF ${LIBBPF_FOUND})
245 set(BPFCFLAGS -Wall -O2 -g)
246 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
248 set(EXTRABPFCFLAGS -I/usr/include/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu)
251 check_c_source_compiles("
252 #include <linux/bpf.h>
253 int main() { enum bpf_stats_type foo; (void)foo; }" HAVE_BPF_STATS_TYPE)
256 # The nghttp, nghttpd and nghttpx under src depend on zlib, OpenSSL and libev
257 if(ENABLE_APP AND NOT (ZLIB_FOUND AND OPENSSL_FOUND AND LIBEV_FOUND))
258 message(FATAL_ERROR "Applications were requested (ENABLE_APP=1) but dependencies are not met.")
261 # HTTP/3 requires quictls/openssl, libngtcp2, libngtcp2_crypto_openssl
263 if(ENABLE_HTTP3 AND NOT (HAVE_SSL_IS_QUIC AND LIBNGTCP2_FOUND AND LIBNGTCP2_CRYPTO_OPENSSL_FOUND AND LIBNGHTTP3_FOUND))
264 message(FATAL_ERROR "HTTP/3 was requested (ENABLE_HTTP3=1) but dependencies are not met.")
267 # HPACK tools requires jansson
268 if(ENABLE_HPACK_TOOLS AND NOT HAVE_JANSSON)
269 message(FATAL_ERROR "HPACK tools were requested (ENABLE_HPACK_TOOLS=1) but dependencies are not met.")
272 # C++ library libnghttp2_asio
273 if(ENABLE_EXAMPLES AND NOT (OPENSSL_FOUND AND LIBEVENT_OPENSSL_FOUND))
274 message(FATAL_ERROR "examples were requested (ENABLE_EXAMPLES=1) but dependencies are not met.")
277 # third-party http-parser only be built when needed
278 if(ENABLE_EXAMPLES OR ENABLE_APP OR ENABLE_HPACK_TOOLS OR ENABLE_ASIO_LIB)
279 set(ENABLE_THIRD_PARTY 1)
280 # mruby (for src/nghttpx)
281 set(HAVE_MRUBY ${WITH_MRUBY})
282 set(HAVE_NEVERBLEED ${WITH_NEVERBLEED})
285 set(HAVE_NEVERBLEED 0)
288 # Checks for header files.
289 include(CheckIncludeFile)
290 check_include_file("arpa/inet.h" HAVE_ARPA_INET_H)
291 check_include_file("fcntl.h" HAVE_FCNTL_H)
292 check_include_file("inttypes.h" HAVE_INTTYPES_H)
293 check_include_file("limits.h" HAVE_LIMITS_H)
294 check_include_file("netdb.h" HAVE_NETDB_H)
295 check_include_file("netinet/in.h" HAVE_NETINET_IN_H)
296 check_include_file("pwd.h" HAVE_PWD_H)
297 check_include_file("sys/socket.h" HAVE_SYS_SOCKET_H)
298 check_include_file("sys/time.h" HAVE_SYS_TIME_H)
299 check_include_file("syslog.h" HAVE_SYSLOG_H)
300 check_include_file("time.h" HAVE_TIME_H)
301 check_include_file("unistd.h" HAVE_UNISTD_H)
303 include(CheckTypeSize)
304 # Checks for typedefs, structures, and compiler characteristics.
306 check_type_size("ssize_t" SIZEOF_SSIZE_T)
307 if(SIZEOF_SSIZE_T STREQUAL "")
308 # ssize_t is a signed type in POSIX storing at least -1.
309 # Set it to "int" to match the behavior of AC_TYPE_SSIZE_T (autotools).
323 # XXX To support inline for crappy compilers, see https://cmake.org/Wiki/CMakeTestInline
325 # XXX is AC_SYS_LARGEFILE still needed for modern systems?
326 # add_definitions(-D_FILE_OFFSET_BITS=64)
328 include(CheckStructHasMember)
329 check_struct_has_member("struct tm" tm_gmtoff time.h HAVE_STRUCT_TM_TM_GMTOFF)
331 # Check size of pointer to decide we need 8 bytes alignment adjustment.
332 check_type_size("int *" SIZEOF_INT_P)
333 check_type_size("time_t" SIZEOF_TIME_T)
335 # Checks for library functions.
336 include(CheckFunctionExists)
337 check_function_exists(_Exit HAVE__EXIT)
338 check_function_exists(accept4 HAVE_ACCEPT4)
339 check_function_exists(mkostemp HAVE_MKOSTEMP)
341 include(CheckSymbolExists)
342 # XXX does this correctly detect initgroups (un)availability on cygwin?
343 check_symbol_exists(initgroups grp.h HAVE_DECL_INITGROUPS)
344 if(NOT HAVE_DECL_INITGROUPS AND HAVE_UNISTD_H)
345 # FreeBSD declares initgroups() in unistd.h
346 check_symbol_exists(initgroups unistd.h HAVE_DECL_INITGROUPS2)
347 if(HAVE_DECL_INITGROUPS2)
348 set(HAVE_DECL_INITGROUPS 1)
354 if(CMAKE_C_COMPILER_ID MATCHES "MSVC")
357 set(WARNCXXFLAGS /WX)
361 extract_valid_c_flags(WARNCFLAGS -Werror)
362 extract_valid_c_flags(WARNCXXFLAGS -Werror)
366 extract_valid_c_flags(WARNCFLAGS
371 -Wmissing-declarations
373 -Wdeclaration-after-statement
396 -Wlanguage-extension-token
397 -Wmissing-field-initializers
399 -Wmissing-variable-declarations
400 # Not used because we cannot change public structs
403 # Not used because this basically disallows default case
405 -Wunreachable-code-break
409 # Only work with Clang for the moment
411 # This is required because we pass format string as "const char*.
412 -Wno-format-nonliteral
415 extract_valid_cxx_flags(WARNCXXFLAGS
422 if(ENABLE_STATIC_CRT)
424 foreach(suffix "" _DEBUG _MINSIZEREL _RELEASE _RELWITHDEBINFO)
425 set(var "CMAKE_${lang}_FLAGS${suffix}")
426 string(REPLACE "/MD" "/MT" ${var} "${${var}}")
435 # Some platform does not have working std::future. We disable
436 # threading for those platforms.
437 if(NOT ENABLE_THREADS OR NOT HAVE_STD_FUTURE)
441 add_definitions(-DHAVE_CONFIG_H)
442 configure_file(cmakeconfig.h.in config.h)
443 # autotools-compatible names
444 # Sphinx expects relative paths in the .rst files. Use the fact that the files
445 # below are all one directory level deep.
446 file(RELATIVE_PATH top_srcdir "${CMAKE_CURRENT_BINARY_DIR}/dir" "${CMAKE_CURRENT_SOURCE_DIR}")
447 file(RELATIVE_PATH top_builddir "${CMAKE_CURRENT_BINARY_DIR}/dir" "${CMAKE_CURRENT_BINARY_DIR}")
448 set(abs_top_srcdir "${CMAKE_CURRENT_SOURCE_DIR}")
449 set(abs_top_builddir "${CMAKE_CURRENT_BINARY_DIR}")
450 # libnghttp2.pc (pkg-config file)
451 set(prefix "${CMAKE_INSTALL_PREFIX}")
452 set(exec_prefix "${CMAKE_INSTALL_PREFIX}")
453 set(libdir "${CMAKE_INSTALL_FULL_LIBDIR}")
454 set(includedir "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
455 set(VERSION "${PACKAGE_VERSION}")
456 # For init scripts and systemd service file (in contrib/)
457 set(bindir "${CMAKE_INSTALL_FULL_BINDIR}")
458 set(sbindir "${CMAKE_INSTALL_FULL_SBINDIR}")
461 lib/includes/nghttp2/nghttp2ver.h
462 src/libnghttp2_asio.pc
464 integration-tests/config.go
465 integration-tests/setenv
468 doc/package_README.rst
469 doc/tutorial-client.rst
470 doc/tutorial-server.rst
471 doc/tutorial-hpack.rst
472 doc/nghttpx-howto.rst
474 doc/libnghttp2_asio.rst
475 doc/python-apiref.rst
476 doc/building-android-binary.rst
480 doc/asio_http2_server.h.rst
481 doc/asio_http2_client.h.rst
484 configure_file("${name}.in" "${name}" @ONLY)
488 add_definitions(-D__APPLE_USE_RFC_3542)
492 "${CMAKE_CURRENT_BINARY_DIR}" # for config.h
494 # For use in src/CMakeLists.txt
495 set(PKGDATADIR "${CMAKE_INSTALL_FULL_DATADIR}/${CMAKE_PROJECT_NAME}")
496 set(PKGLIBDIR "${CMAKE_INSTALL_FULL_LIBDIR}/${CMAKE_PROJECT_NAME}")
498 install(FILES README.rst DESTINATION "${CMAKE_INSTALL_DOCDIR}")
500 add_subdirectory(lib)
501 #add_subdirectory(lib/includes)
502 add_subdirectory(third-party)
503 add_subdirectory(src)
504 #add_subdirectory(src/includes)
505 add_subdirectory(examples)
506 add_subdirectory(python)
507 add_subdirectory(tests)
508 #add_subdirectory(tests/testdata)
509 add_subdirectory(integration-tests)
510 add_subdirectory(doc)
511 add_subdirectory(contrib)
512 add_subdirectory(script)
513 add_subdirectory(bpf)
516 string(TOUPPER "${CMAKE_BUILD_TYPE}" _build_type)
517 message(STATUS "summary of build options:
519 Package version: ${VERSION}
520 Library version: ${LT_CURRENT}:${LT_REVISION}:${LT_AGE}
521 Install prefix: ${CMAKE_INSTALL_PREFIX}
522 Target system: ${CMAKE_SYSTEM_NAME}
524 Build type: ${CMAKE_BUILD_TYPE}
525 C compiler: ${CMAKE_C_COMPILER}
526 CFLAGS: ${CMAKE_C_FLAGS_${_build_type}} ${CMAKE_C_FLAGS}
527 C++ compiler: ${CMAKE_CXX_COMPILER}
528 CXXFLAGS: ${CMAKE_CXX_FLAGS_${_build_type}} ${CMAKE_CXX_FLAGS}
529 WARNCFLAGS: ${WARNCFLAGS}
530 CXX1XCXXFLAGS: ${CXX1XCXXFLAGS}
532 Python: ${PYTHON_EXECUTABLE}
533 PYTHON_VERSION: ${PYTHON_VERSION_STRING}
534 Library version:${PYTHONLIBS_VERSION_STRING}
535 Cython: ${CYTHON_EXECUTABLE}
537 CUnit: ${HAVE_CUNIT} (LIBS='${CUNIT_LIBRARIES}')
538 Failmalloc: ${ENABLE_FAILMALLOC}
540 OpenSSL: ${HAVE_OPENSSL} (LIBS='${OPENSSL_LIBRARIES}')
541 Libxml2: ${HAVE_LIBXML2} (LIBS='${LIBXML2_LIBRARIES}')
542 Libev: ${HAVE_LIBEV} (LIBS='${LIBEV_LIBRARIES}')
543 Libc-ares: ${HAVE_LIBCARES} (LIBS='${LIBCARES_LIBRARIES}')
544 Libngtcp2: ${HAVE_LIBNGTCP2} (LIBS='${LIBNGTCP2_LIBRARIES}')
545 Libngtcp2_crypto_openssl: ${HAVE_LIBNGTCP2_CRYPTO_OPENSSL} (LIBS='${LIBNGTCP2_CRYPTO_OPENSSL_LIBRARIES}')
546 Libnghttp3: ${HAVE_LIBNGHTTP3} (LIBS='${LIBNGHTTP3_LIBRARIES}')
547 Libbpf: ${HAVE_LIBBPF} (LIBS='${LIBBPF_LIBRARIES}')
548 Libevent(SSL): ${HAVE_LIBEVENT_OPENSSL} (LIBS='${LIBEVENT_OPENSSL_LIBRARIES}')
549 Jansson: ${HAVE_JANSSON} (LIBS='${JANSSON_LIBRARIES}')
550 Jemalloc: ${HAVE_JEMALLOC} (LIBS='${JEMALLOC_LIBRARIES}')
551 Zlib: ${HAVE_ZLIB} (LIBS='${ZLIB_LIBRARIES}')
552 Systemd: ${HAVE_SYSTEMD} (LIBS='${SYSTEMD_LIBRARIES}')
553 Boost::System: ${Boost_SYSTEM_LIBRARY}
554 Boost::Thread: ${Boost_THREAD_LIBRARY}
556 http-parser: ${ENABLE_THIRD_PARTY}
558 Neverbleed: ${HAVE_NEVERBLEED}
560 Applications: ${ENABLE_APP}
561 HPACK tools: ${ENABLE_HPACK_TOOLS}
562 Libnghttp2_asio:${ENABLE_ASIO_LIB}
563 Examples: ${ENABLE_EXAMPLES}
564 Python bindings:${ENABLE_PYTHON_BINDINGS}
565 Threading: ${ENABLE_THREADS}
566 HTTP/3(EXPERIMENTAL): ${ENABLE_HTTP3}
568 if(ENABLE_LIB_ONLY_DISABLED_OTHERS)
569 message("Only the library will be built. To build other components "
570 "(such as applications and examples), set ENABLE_LIB_ONLY=OFF.")