CMakeLists.txt (6795B)
1 cmake_minimum_required(VERSION 3.4) 2 project("Cap'n Proto" CXX) 3 set(VERSION 0.10-dev) 4 5 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") 6 7 include(CheckIncludeFileCXX) 8 include(GNUInstallDirs) 9 if(MSVC) 10 check_include_file_cxx(initializer_list HAS_CXX14) 11 else() 12 check_include_file_cxx(initializer_list HAS_CXX14 "-std=gnu++1y") 13 endif() 14 if(NOT HAS_CXX14) 15 message(SEND_ERROR "Requires a C++14 compiler and standard library.") 16 endif() 17 18 # these arguments are passed to all install(TARGETS) calls 19 set(INSTALL_TARGETS_DEFAULT_ARGS 20 EXPORT CapnProtoTargets 21 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" 22 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" 23 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" 24 ) 25 26 # Options ====================================================================== 27 28 option(BUILD_TESTING "Build unit tests and enable CTest 'check' target." ON) 29 option(EXTERNAL_CAPNP "Use the system capnp binary, or the one specified in $CAPNP, instead of using the compiled one." OFF) 30 option(CAPNP_LITE "Compile Cap'n Proto in 'lite mode', in which all reflection APIs (schema.h, dynamic.h, etc.) are not included. Produces a smaller library at the cost of features. All programs built against the library must be compiled with -DCAPNP_LITE. Requires EXTERNAL_CAPNP." OFF) 31 32 # Check for invalid combinations of build options 33 if(CAPNP_LITE AND BUILD_TESTING AND NOT EXTERNAL_CAPNP) 34 message(SEND_ERROR "You must set EXTERNAL_CAPNP when using CAPNP_LITE and BUILD_TESTING.") 35 endif() 36 37 if(CAPNP_LITE) 38 set(CAPNP_LITE_FLAG "-DCAPNP_LITE") 39 # This flag is attached as PUBLIC target_compile_definition to kj target 40 else() 41 set(CAPNP_LITE_FLAG) 42 endif() 43 44 set(WITH_OPENSSL "AUTO" CACHE STRING 45 "Whether or not to build libkj-tls by linking against openssl") 46 # define list of values GUI will offer for the variable 47 set_property(CACHE WITH_OPENSSL PROPERTY STRINGS AUTO ON OFF) 48 49 # shadow cache variable original value with ON/OFF, 50 # so from now on OpenSSL-specific code just has to check: 51 # if (WITH_OPENSSL) 52 # ... 53 # endif() 54 if (CAPNP_LITE) 55 set(WITH_OPENSSL OFF) 56 elseif (WITH_OPENSSL STREQUAL "AUTO") 57 find_package(OpenSSL COMPONENTS Crypto SSL) 58 if (OPENSSL_FOUND) 59 set(WITH_OPENSSL ON) 60 else() 61 set(WITH_OPENSSL OFF) 62 endif() 63 elseif (WITH_OPENSSL) 64 find_package(OpenSSL REQUIRED COMPONENTS Crypto SSL) 65 endif() 66 67 if(MSVC) 68 # TODO(cleanup): Enable higher warning level in MSVC, but make sure to test 69 # build with that warning level and clean out false positives. 70 71 add_compile_options(/wo4503) 72 # Only warn once on truncated decorated names. The maximum symbol length MSVC 73 # supports is 4k characters, which the parser framework regularly blows. The 74 # compiler likes to print out the entire type that went over the limit along 75 # with this warning, which gets unbearably spammy. That said, we don't want to 76 # just ignore it, so I'm letting it trigger once until we find some places to 77 # inject ParserRefs. 78 else() 79 # Note that it's important to add new CXXFLAGS before ones specified by the 80 # user, so that the user's flags override them. This is particularly 81 # important if -Werror was enabled and then certain warnings need to be 82 # disabled, as is done in super-test.sh. 83 # 84 # We enable a lot of warnings, but then disable some: 85 # * strict-aliasing: We use type-punning in known-safe ways that GCC doesn't 86 # recognize as safe. 87 # * sign-compare: Low S/N ratio. 88 # * unused-parameter: Low S/N ratio. 89 add_compile_options(-Wall -Wextra -Wno-strict-aliasing -Wno-sign-compare -Wno-unused-parameter) 90 91 if(DEFINED CMAKE_CXX_EXTENSIONS AND NOT CMAKE_CXX_EXTENSIONS) 92 message(SEND_ERROR "Cap'n Proto requires compiler-specific extensions (e.g., -std=gnu++14). Please leave CMAKE_CXX_EXTENSIONS undefined or ON.") 93 endif() 94 95 if (NOT ANDROID) 96 add_compile_options(-pthread) 97 endif() 98 endif() 99 100 # Source ======================================================================= 101 include(CapnProtoMacros) 102 add_subdirectory(src) 103 104 # Install ====================================================================== 105 106 include(CMakePackageConfigHelpers) 107 108 # We used to use write_basic_package_version_file(), but since the autotools build needs to install 109 # a config version script as well, I copied the AnyNewerVersion template from my CMake Modules 110 # directory to Cap'n Proto's cmake/ directory (alternatively, we could make the autotools build 111 # depend on CMake). 112 # 113 # We might as well use the local copy of the template. In the future we can modify the project's 114 # version compatibility policy just by changing that file. 115 set(PACKAGE_VERSION ${VERSION}) 116 configure_file(cmake/CapnProtoConfigVersion.cmake.in cmake/CapnProtoConfigVersion.cmake @ONLY) 117 118 set(CONFIG_PACKAGE_LOCATION ${CMAKE_INSTALL_LIBDIR}/cmake/CapnProto) 119 120 configure_package_config_file(cmake/CapnProtoConfig.cmake.in 121 ${CMAKE_CURRENT_BINARY_DIR}/cmake/CapnProtoConfig.cmake 122 INSTALL_DESTINATION ${CONFIG_PACKAGE_LOCATION} 123 PATH_VARS CMAKE_INSTALL_FULL_INCLUDEDIR 124 ) 125 export(EXPORT CapnProtoTargets 126 FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/CapnProtoTargets.cmake" 127 NAMESPACE CapnProto:: 128 ) 129 install(EXPORT CapnProtoTargets 130 FILE CapnProtoTargets.cmake 131 NAMESPACE CapnProto:: 132 DESTINATION ${CONFIG_PACKAGE_LOCATION} 133 ) 134 install(FILES 135 cmake/CapnProtoMacros.cmake 136 ${CMAKE_CURRENT_BINARY_DIR}/cmake/CapnProtoConfig.cmake 137 ${CMAKE_CURRENT_BINARY_DIR}/cmake/CapnProtoConfigVersion.cmake 138 DESTINATION ${CONFIG_PACKAGE_LOCATION} 139 ) 140 #install CapnProtoMacros for CapnProtoConfig.cmake build directory consumers 141 configure_file(cmake/CapnProtoMacros.cmake cmake/CapnProtoMacros.cmake COPYONLY) 142 143 if(NOT MSVC) # Don't install pkg-config files when building with MSVC 144 # Variables for pkg-config files 145 set(prefix "${CMAKE_INSTALL_PREFIX}") 146 set(exec_prefix "") # not needed since we use absolute paths in libdir and includedir 147 set(libdir "${CMAKE_INSTALL_FULL_LIBDIR}") 148 set(includedir "${CMAKE_INSTALL_FULL_INCLUDEDIR}") 149 set(PTHREAD_CFLAGS "-pthread") 150 set(STDLIB_FLAG) # TODO: Unsupported 151 152 set(CAPNP_PKG_CONFIG_FILES 153 pkgconfig/kj.pc 154 pkgconfig/capnp.pc 155 pkgconfig/capnpc.pc 156 ) 157 158 if(NOT CAPNP_LITE) 159 list(APPEND CAPNP_PKG_CONFIG_FILES 160 pkgconfig/kj-async.pc 161 pkgconfig/kj-gzip.pc 162 pkgconfig/kj-http.pc 163 pkgconfig/kj-test.pc 164 pkgconfig/kj-tls.pc 165 pkgconfig/capnp-rpc.pc 166 pkgconfig/capnp-websocket.pc 167 pkgconfig/capnp-json.pc 168 ) 169 endif() 170 171 foreach(pcfile ${CAPNP_PKG_CONFIG_FILES}) 172 configure_file(${pcfile}.in "${CMAKE_CURRENT_BINARY_DIR}/${pcfile}" @ONLY) 173 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${pcfile}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") 174 endforeach() 175 176 unset(STDLIB_FLAG) 177 unset(PTHREAD_CFLAGS) 178 unset(includedir) 179 unset(libdir) 180 unset(exec_prefix) 181 unset(prefix) 182 endif()