CMakeLists.txt 5.85 KB
Newer Older
1
cmake_minimum_required(VERSION 3.1)
2
project("Cap'n Proto" CXX)
3
set(VERSION 0.8-dev)
4 5 6 7

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

include(CheckIncludeFileCXX)
8
include(GNUInstallDirs)
9
if(MSVC)
Kenton Varda's avatar
Kenton Varda committed
10
  check_include_file_cxx(initializer_list HAS_CXX14)
11
else()
Kenton Varda's avatar
Kenton Varda committed
12
  check_include_file_cxx(initializer_list HAS_CXX14 "-std=gnu++1y")
13
endif()
Kenton Varda's avatar
Kenton Varda committed
14 15
if(NOT HAS_CXX14)
  message(SEND_ERROR "Requires a C++14 compiler and standard library.")
16 17
endif()

18 19 20
# these arguments are passed to all install(TARGETS) calls
set(INSTALL_TARGETS_DEFAULT_ARGS
  EXPORT CapnProtoTargets
21 22 23
  ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
  LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
  RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
24 25
)

26 27
# Options ======================================================================

28 29
option(BUILD_TESTING "Build unit tests and enable CTest 'check' target." ON)
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 34
if(CAPNP_LITE AND BUILD_TESTING AND NOT EXTERNAL_CAPNP)
  message(SEND_ERROR "You must set EXTERNAL_CAPNP when using CAPNP_LITE and BUILD_TESTING.")
35 36
endif()

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 41
else()
  set(CAPNP_LITE_FLAG)
42 43
endif()

44
if(MSVC)
45 46
  # TODO(cleanup): Enable higher warning level in MSVC, but make sure to test
  #   build with that warning level and clean out false positives.
47 48 49 50 51 52 53 54

  add_compile_options(/wo4503)
  # Only warn once on truncated decorated names. The maximum symbol length MSVC
  # supports is 4k characters, which the parser framework regularly blows. The
  # compiler likes to print out the entire type that went over the limit along
  # with this warning, which gets unbearably spammy. That said, we don't want to
  # just ignore it, so I'm letting it trigger once until we find some places to
  # inject ParserRefs.
55
else()
56 57 58 59 60 61 62 63 64 65
  # Note that it's important to add new CXXFLAGS before ones specified by the
  # user, so that the user's flags override them. This is particularly
  # important if -Werror was enabled and then certain warnings need to be
  # disabled, as is done in super-test.sh.
  #
  # We enable a lot of warnings, but then disable some:
  # * strict-aliasing: We use type-punning in known-safe ways that GCC doesn't
  #   recognize as safe.
  # * sign-compare: Low S/N ratio.
  # * unused-parameter: Low S/N ratio.
66 67 68
  add_compile_options(-Wall -Wextra -Wno-strict-aliasing -Wno-sign-compare -Wno-unused-parameter)

  if(DEFINED CMAKE_CXX_EXTENSIONS AND NOT CMAKE_CXX_EXTENSIONS)
Kenton Varda's avatar
Kenton Varda committed
69
    message(SEND_ERROR "Cap'n Proto requires compiler-specific extensions (e.g., -std=gnu++14). Please leave CMAKE_CXX_EXTENSIONS undefined or ON.")
70
  endif()
71 72 73 74

  if (NOT ANDROID)
    add_compile_options(-pthread)
  endif()
75
endif()
76 77

# Source =======================================================================
78
include(CapnProtoMacros)
Ahmed Charles's avatar
Ahmed Charles committed
79
add_subdirectory(src)
80 81 82

# Install ======================================================================

83
include(CMakePackageConfigHelpers)
84 85 86 87 88 89 90 91 92 93 94

# We used to use write_basic_package_version_file(), but since the autotools build needs to install
# a config version script as well, I copied the AnyNewerVersion template from my CMake Modules
# directory to Cap'n Proto's cmake/ directory (alternatively, we could make the autotools build
# depend on CMake).
#
# We might as well use the local copy of the template. In the future we can modify the project's
# version compatibility policy just by changing that file.
set(PACKAGE_VERSION ${VERSION})
configure_file(cmake/CapnProtoConfigVersion.cmake.in cmake/CapnProtoConfigVersion.cmake @ONLY)

95
set(CONFIG_PACKAGE_LOCATION ${CMAKE_INSTALL_LIBDIR}/cmake/CapnProto)
96 97

configure_package_config_file(cmake/CapnProtoConfig.cmake.in
98
  ${CMAKE_CURRENT_BINARY_DIR}/cmake/CapnProtoConfig.cmake
99
  INSTALL_DESTINATION ${CONFIG_PACKAGE_LOCATION}
100
  PATH_VARS CMAKE_INSTALL_FULL_INCLUDEDIR
101 102
)
export(EXPORT CapnProtoTargets
103
  FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/CapnProtoTargets.cmake"
104 105 106 107 108 109 110 111 112
  NAMESPACE CapnProto::
)
install(EXPORT CapnProtoTargets
  FILE CapnProtoTargets.cmake
  NAMESPACE CapnProto::
  DESTINATION ${CONFIG_PACKAGE_LOCATION}
)
install(FILES
  cmake/CapnProtoMacros.cmake
113 114
  ${CMAKE_CURRENT_BINARY_DIR}/cmake/CapnProtoConfig.cmake
  ${CMAKE_CURRENT_BINARY_DIR}/cmake/CapnProtoConfigVersion.cmake
115 116
  DESTINATION ${CONFIG_PACKAGE_LOCATION}
)
117
#install CapnProtoMacros for CapnProtoConfig.cmake build directory consumers
118
configure_file(cmake/CapnProtoMacros.cmake cmake/CapnProtoMacros.cmake COPYONLY)
119

120 121 122
if(NOT MSVC)  # Don't install pkg-config files when building with MSVC
  # Variables for pkg-config files
  set(prefix "${CMAKE_INSTALL_PREFIX}")
123 124 125
  set(exec_prefix "") # not needed since we use absolute paths in libdir and includedir
  set(libdir "${CMAKE_INSTALL_FULL_LIBDIR}")
  set(includedir "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
126 127
  set(PTHREAD_CFLAGS "-pthread")
  set(STDLIB_FLAG)  # TODO: Unsupported
128

129 130 131 132
  set(CAPNP_PKG_CONFIG_FILES
    pkgconfig/kj.pc
    pkgconfig/capnp.pc
  )
133

134
  if(NOT CAPNP_LITE)
135 136 137 138 139 140 141
    list(APPEND CAPNP_PKG_CONFIG_FILES
      pkgconfig/kj-async.pc
      pkgconfig/kj-http.pc
      pkgconfig/kj-test.pc
      pkgconfig/capnp-rpc.pc
      pkgconfig/capnp-json.pc
    )
142
  endif()
143

144 145 146 147 148
  foreach(pcfile ${CAPNP_PKG_CONFIG_FILES})
    configure_file(${pcfile}.in "${CMAKE_CURRENT_BINARY_DIR}/${pcfile}" @ONLY)
    install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${pcfile}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
  endforeach()

149 150 151 152 153 154 155
  unset(STDLIB_FLAG)
  unset(PTHREAD_CFLAGS)
  unset(includedir)
  unset(libdir)
  unset(exec_prefix)
  unset(prefix)
endif()