Commit a6d898a2 authored by Kenton Varda's avatar Kenton Varda

Merge pull request #137 from pqu/cmake

More CMake Improvements
parents 46c48dfe d9b8455a
project("Cap'n Proto" CXX)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(CheckIncludeFileCXX)
check_include_file_cxx(initializer_list HAS_CXX11)
if(NOT HAS_CXX11)
message(SEND_ERROR "Requires a C++11 compiler and standard library.")
endif()
# Set installation paths; these can be overridden in the cache.
set(EXEC_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}"
CACHE PATH "Base installation path for executables."
)
set(BIN_INSTALL_DIR "${EXEC_INSTALL_PREFIX}/bin"
CACHE PATH "Installation directory for binaries (default: prefix/bin)."
)
set(LIB_INSTALL_DIR "${EXEC_INSTALL_PREFIX}/lib"
CACHE PATH "Installation directory for libraries (default: prefix/lib)."
)
set(INCLUDE_INSTALL_DIR "${EXEC_INSTALL_PREFIX}/include"
CACHE PATH "Installation directory for header files (default: prefix/include)."
)
add_subdirectory(c++)
# Options ======================================================================
option(BUILD_TOOLS "Build command-line tools and compiler." ON)
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)
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 to build the tests." OFF)
# Check for invalid combinations of build options
if(NOT BUILD_TOOLS AND BUILD_TESTING AND NOT EXTERNAL_CAPNP)
# Not *all* of the tests require the capnp compiler, and those that do could be excluded
# when not building the tools, but it's easier to just have a blanket rule.
message(SEND_ERROR "Tests (BUILD_TESTING) cannot be build without either BUILD_TOOLS or EXTERNAL_CAPNP.")
endif()
if(CAPNP_LITE AND BUILD_TESTING AND NOT EXTERNAL_CAPNP)
# As above, we could exclude only the tests that depend on the compiler.
message(SEND_ERROR "CAPNP_LITE with BUILD_TESTING requires EXTERNAL_CAPNP.")
endif()
if(CAPNP_LITE AND BUILD_TOOLS)
message(WARNING "Command-line tools will not be built with CAPNP_LITE.")
endif()
if (NOT BUILD_TOOLS AND BUILD_TESTING AND NOT EXTERNAL_CAPNP)
message(WARNING "Forcing BUILD_TOOLS to ON; required by BUILD_TESTING without EXTERNAL_CAPNP.")
set(BUILD_TOOLS TRUE)
if(CAPNP_LITE)
set(CAPNP_LITE_FLAG "-DCAPNP_LITE")
else()
set(CAPNP_LITE_FLAG)
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-unused-parameter -std=c++11 -pthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-unused-parameter -std=c++11 -pthread ${CAPNP_LITE_FLAG}")
# Source =======================================================================
add_subdirectory(src)
# Install ======================================================================
# Variables for pkg-config files
set(prefix "${CMAKE_INSTALL_PREFIX}")
set(exec_prefix "${EXEC_INSTALL_PREFIX}")
set(libdir "${LIB_INSTALL_DIR}")
set(includedir "${INCLUDE_INSTALL_DIR}")
set(PTHREAD_CFLAGS "-pthread")
set(STDLIB_FLAG) # TODO: Unsupported
configure_file(capnp.pc.in "${CMAKE_CURRENT_BINARY_DIR}/capnp.pc" @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/capnp.pc" DESTINATION "${LIB_INSTALL_DIR}/pkgconfig")
if(NOT CAPNP_LITE)
configure_file(capnp-rpc.pc.in "${CMAKE_CURRENT_BINARY_DIR}/capnp-rpc.pc" @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/capnp-rpc.pc" DESTINATION "${LIB_INSTALL_DIR}/pkgconfig")
endif()
unset(STDLIB_FLAG)
unset(PTHREAD_CFLAGS)
unset(includedir)
unset(libdir)
unset(exec_prefix)
unset(prefix)
This diff is collapsed.
# capnp ========================================================================
set(capnp_sources_lite
c++.capnp.c++
blob.c++
arena.c++
layout.c++
list.c++
any.c++
message.c++
schema.capnp.c++
serialize.c++
serialize-packed.c++
)
set(capnp_sources_heavy
schema.c++
schema-loader.c++
dynamic.c++
stringify.c++
)
if(NOT CAPNP_LITE)
set(capnp_sources ${capnp_sources_lite} ${capnp_sources_heavy})
else()
set(capnp_sources ${capnp_sources_lite})
endif()
set(capnp_headers
c++.capnp.h
common.h
blob.h
endian.h
layout.h
orphan.h
list.h
any.h
message.h
capability.h
dynamic.h
schema.h
schema.capnp.h
schema-lite.h
schema-loader.h
schema-parser.h
pretty-print.h
serialize.h
serialize-async.h
serialize-packed.h
pointer-helpers.h
generated-header-support.h
)
add_library(capnp ${capnp_sources})
target_link_libraries(capnp kj)
install(TARGETS capnp ARCHIVE DESTINATION "${LIB_INSTALL_DIR}")
install(FILES ${capnp_headers} DESTINATION "${INCLUDE_INSTALL_DIR}/capnp")
set(capnp-rpc_sources
serialize-async.c++
capability.c++
dynamic-capability.c++
rpc.c++
rpc.capnp.c++
rpc-twoparty.c++
rpc-twoparty.capnp.c++
ez-rpc.c++
)
set(capnp-rpc_headers
rpc-prelude.h
rpc.h
rpc-twoparty.h
rpc.capnp.h
rpc-twoparty.capnp.h
ez-rpc.h
)
if(NOT CAPNP_LITE)
add_library(capnp-rpc ${capnp-rpc_sources})
target_link_libraries(capnp-rpc kj-async kj)
install(TARGETS capnp-rpc ARCHIVE DESTINATION "${LIB_INSTALL_DIR}")
install(FILES ${capnp-rpc_headers} DESTINATION "${INCLUDE_INSTALL_DIR}/capnp")
endif()
# Tools/Compilers ==============================================================
set(capnpc_sources
compiler/md5.c++
compiler/error-reporter.c++
compiler/lexer.capnp.c++
compiler/lexer.c++
compiler/grammar.capnp.c++
compiler/parser.c++
compiler/node-translator.c++
compiler/compiler.c++
schema-parser.c++
)
set(capnpc_headers
c++.capnp
schema.capnp
rpc.capnp
rpc-twoparty.capnp
)
if(NOT CAPNP_LITE)
add_library(capnpc ${capnpc_sources})
target_link_libraries(capnpc capnp kj)
install(TARGETS capnpc ARCHIVE DESTINATION "${LIB_INSTALL_DIR}")
install(FILES ${capnpc_headers} DESTINATION "${INCLUDE_INSTALL_DIR}/capnp")
endif()
if(BUILD_TOOLS AND NOT CAPNP_LITE)
add_executable(capnp_tool
compiler/module-loader.c++
compiler/capnp.c++
)
target_link_libraries(capnp_tool capnpc capnp kj)
set_target_properties(capnp_tool PROPERTIES OUTPUT_NAME capnp)
add_executable(capnpc_cpp
compiler/capnpc-c++.c++
)
target_link_libraries(capnpc_cpp capnp kj)
set_target_properties(capnpc_cpp PROPERTIES OUTPUT_NAME capnpc-c++)
add_executable(capnpc_capnp
compiler/capnpc-capnp.c++
)
target_link_libraries(capnpc_capnp capnp kj)
set_target_properties(capnpc_capnp PROPERTIES OUTPUT_NAME capnpc-capnp)
install(TARGETS capnp_tool capnpc_cpp capnpc_capnp RUNTIME DESTINATION "${BIN_INSTALL_DIR}")
# Symlink capnpc -> capnp
install(CODE "execute_process(COMMAND \"${CMAKE_COMMAND}\" -E create_symlink capnp \"${BIN_INSTALL_DIR}/capnpc\")")
endif() # BUILD_TOOLS AND NOT CAPNP_LITE
# Tests ========================================================================
if(BUILD_TESTING)
set(test_capnp_files
test.capnp
test-import.capnp
test-import2.capnp
)
# Setup paths to the schema compiler for generating ${test_capnp_files}
if(NOT EXTERNAL_CAPNP)
set(CAPNP_EXECUTABLE $<TARGET_FILE:capnp_tool>)
set(CAPNPC_CXX_EXECUTABLE $<TARGET_FILE:capnpc_cpp>)
else()
# Allow paths to tools to be set with one of: (1) the CMake variables from the
# the FindCapnProto module; (2) environment variables; or (3) find_program()
if (NOT CAPNP_EXECUTABLE)
if (DEFINED ENV{CAPNP})
set(CAPNP_EXECUTABLE "$ENV{CAPNP}")
else()
find_program(CAPNP_EXECUTABLE capnp)
endif()
endif()
if(NOT CAPNPC_CXX_EXECUTABLE)
if (DEFINED ENV{CAPNPC_CXX})
set(CAPNPC_CXX_EXECUTABLE "$ENV{CAPNPC_CXX}")
else()
# Also search in the same directory that `capnp` was found in
get_filename_component(capnp_dir "${CAPNP_EXECUTABLE}" DIRECTORY)
find_program(CAPNPC_CXX_EXECUTABLE capnpc-c++ HINTS "${capnp_dir}")
endif()
endif()
endif()
# Add "/capnp" to match the path used to import the files in the test sources
set(CAPNPC_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/test_capnp/capnp")
include_directories("${CMAKE_CURRENT_BINARY_DIR}/test_capnp") # Note: no "/capnp"
file(MAKE_DIRECTORY "${CAPNPC_OUTPUT_DIR}")
capnp_generate_cpp(test_capnp_cpp_files test_capnp_h_files ${test_capnp_files})
if(CAPNP_LITE)
set(test_libraries capnp kj gtest gtest_main)
else()
set(test_libraries capnp capnp-rpc capnpc kj kj-async gtest gtest_main)
endif()
add_executable(capnp-tests
common-test.c++
blob-test.c++
endian-test.c++
endian-fallback-test.c++
endian-reverse-test.c++
layout-test.c++
any-test.c++
message-test.c++
encoding-test.c++
orphan-test.c++
serialize-test.c++
serialize-packed-test.c++
test-util.c++
${test_capnp_cpp_files}
${test_capnp_h_files}
)
target_link_libraries(capnp-tests ${test_libraries})
add_dependencies(check capnp-tests)
add_test(NAME capnp-tests-run COMMAND capnp-tests)
if(NOT CAPNP_LITE)
add_executable(capnp-heavy-tests
capability-test.c++
schema-test.c++
schema-loader-test.c++
dynamic-test.c++
stringify-test.c++
serialize-async-test.c++
rpc-test.c++
rpc-twoparty-test.c++
ez-rpc-test.c++
compiler/lexer-test.c++
compiler/md5-test.c++
test-util.c++
${test_capnp_cpp_files}
${test_capnp_h_files}
)
target_link_libraries(capnp-heavy-tests ${test_libraries})
add_dependencies(check capnp-heavy-tests)
add_test(NAME capnp-heavy-tests-run COMMAND capnp-heavy-tests)
add_executable(capnp-evolution-tests compiler/evolution-test.c++)
target_link_libraries(capnp-evolution-tests capnpc capnp kj)
add_dependencies(check capnp-evolution-tests)
add_test(NAME capnp-evolution-tests-run COMMAND capnp-evolution-tests)
endif() # NOT CAPNP_LITE
endif() # BUILD_TESTING
# kj ===========================================================================
set(kj_sources
common.c++
units.c++
memory.c++
refcount.c++
array.c++
string.c++
string-tree.c++
exception.c++
debug.c++
arena.c++
io.c++
mutex.c++
thread.c++
main.c++
parse/char.c++
)
set(kj_headers
common.h
units.h
memory.h
refcount.h
array.h
vector.h
string.h
string-tree.h
exception.h
debug.h
arena.h
io.h
tuple.h
one-of.h
function.h
mutex.h
thread.h
threadlocal.h
time.h
main.h
windows-sanity.h
)
set(kj-parse_headers
parse/common.h
parse/char.h
)
add_library(kj ${kj_sources})
install(TARGETS kj ARCHIVE DESTINATION "${LIB_INSTALL_DIR}")
install(FILES ${kj_headers} DESTINATION "${INCLUDE_INSTALL_DIR}/kj")
install(FILES ${kj-parse_headers} DESTINATION "${INCLUDE_INSTALL_DIR}/kj/parse")
set(kj-async_sources
async.c++
async-unix.c++
async-io.c++
)
set(kj-async_headers
async-prelude.h
async.h
async-inl.h
async-unix.h
async-io.h
)
if(NOT CAPNP_LITE)
add_library(kj-async ${kj-async_sources})
target_link_libraries(kj-async kj)
install(TARGETS kj-async ARCHIVE DESTINATION "${LIB_INSTALL_DIR}")
install(FILES ${kj-async_headers} DESTINATION "${INCLUDE_INSTALL_DIR}/kj")
endif()
# Tests ========================================================================
if(BUILD_TESTING)
add_executable(kj-tests
common-test.c++
memory-test.c++
refcount-test.c++
array-test.c++
string-test.c++
string-tree-test.c++
exception-test.c++
debug-test.c++
arena-test.c++
units-test.c++
tuple-test.c++
one-of-test.c++
function-test.c++
io-test.c++
mutex-test.c++
threadlocal-test.c++
threadlocal-pthread-test.c++
)
# TODO: Link with librt on Solaris for sched_yield
target_link_libraries(kj-tests kj gtest gtest_main)
add_dependencies(check kj-tests)
add_test(NAME kj-tests-run COMMAND kj-tests)
if(NOT CAPNP_LITE)
add_executable(kj-heavy-tests
async-test.c++
async-unix-test.c++
async-io-test.c++
parse/common-test.c++
parse/char-test.c++
)
target_link_libraries(kj-heavy-tests kj kj-async gtest gtest_main)
add_dependencies(check kj-heavy-tests)
add_test(NAME kj-heavy-tests-run COMMAND kj-heavy-tests)
endif() # NOT CAPNP_LITE
endif() # BUILD_TESTING
......@@ -19,8 +19,12 @@
# Path to the `capnpc-c++` tool (can be set to override).
# CAPNP_INCLUDE_DIRS
# Include directories for the library's headers (can be set to override).
# CAPNP_LIBRARIES
# The necessary library paths to link with.
# CANP_LIBRARIES
# The Cap'n Proto library paths.
# CAPNP_LIBRARIES_LITE
# Paths to only the 'lite' libraries.
# CAPNP_DEFINITIONS
# Compiler definitions required for building with the library.
# CAPNP_FOUND
# Set if the libraries have been located.
#
......@@ -28,6 +32,7 @@
#
# find_package(CapnProto REQUIRED)
# include_directories(${CAPNP_INCLUDE_DIRS})
# add_definitions(${CAPNP_DEFINITIONS})
#
# capnp_generate_cpp(CAPNP_SRCS CAPNP_HDRS schema.capnp)
# add_executable(a a.cc ${CAPNP_SRCS} ${CAPNP_HDRS})
......@@ -40,6 +45,8 @@
# capnp_generate_cpp(...)
#
# CAPNP_GENERATE_CPP ===========================================================
function(CAPNP_GENERATE_CPP SOURCES HEADERS)
if(NOT ARGN)
message(SEND_ERROR "CAPNP_GENERATE_CPP() called without any source files.")
......@@ -60,7 +67,7 @@ function(CAPNP_GENERATE_CPP SOURCES HEADERS)
if(DEFINED CAPNPC_IMPORT_DIRS)
# Append each directory as a series of '-I' flags in ${include_path}
foreach(directory ${CAPNPC_IMPORT_DIRS})
get_filename_component(absolute_path ${directory} ABSOLUTE)
get_filename_component(absolute_path "${directory}" ABSOLUTE)
list(APPEND include_path -I ${absolute_path})
endforeach()
endif()
......@@ -73,42 +80,42 @@ function(CAPNP_GENERATE_CPP SOURCES HEADERS)
endif()
if(NOT DEFINED CAPNPC_SRC_PREFIX)
set(CAPNPC_SRC_PREFIX ${CMAKE_CURRENT_SOURCE_DIR})
set(CAPNPC_SRC_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}")
endif()
get_filename_component(CAPNPC_SRC_PREFIX ${CAPNPC_SRC_PREFIX} ABSOLUTE)
get_filename_component(CAPNPC_SRC_PREFIX "${CAPNPC_SRC_PREFIX}" ABSOLUTE)
set(${SOURCES})
set(${HEADERS})
foreach(schema_file ${ARGN})
get_filename_component(file_path ${schema_file} ABSOLUTE)
get_filename_component(file_dir ${file_path} DIRECTORY)
get_filename_component(file_path "${schema_file}" ABSOLUTE)
get_filename_component(file_dir "${file_path}" PATH)
# Figure out where the output files will go
if (NOT DEFINED CAPNPC_OUTPUT_DIR)
set(output_base ${file_path})
set(output_base "${file_path}")
else()
# Output files are placed in CAPNPC_OUTPUT_DIR, at a location as if they were
# relative to CAPNPC_SRC_PREFIX.
string(LENGTH ${CAPNPC_SRC_PREFIX} prefix_len)
string(SUBSTRING ${file_path} 0 ${prefix_len} output_prefix)
if(NOT ${CAPNPC_SRC_PREFIX} STREQUAL ${output_prefix})
string(LENGTH "${CAPNPC_SRC_PREFIX}" prefix_len)
string(SUBSTRING "${file_path}" 0 ${prefix_len} output_prefix)
if(NOT "${CAPNPC_SRC_PREFIX}" STREQUAL "${output_prefix}")
message(SEND_ERROR "Could not determine output path for '${schema_file}' ('${file_path}') with source prefix '${CAPNPC_SRC_PREFIX}' into '${CAPNPC_OUTPUT_DIR}'.")
endif()
string(SUBSTRING ${file_path} ${prefix_len} -1 output_path)
set(output_base ${CAPNPC_OUTPUT_DIR}${output_path})
string(SUBSTRING "${file_path}" ${prefix_len} -1 output_path)
set(output_base "${CAPNPC_OUTPUT_DIR}${output_path}")
endif()
add_custom_command(
OUTPUT "${output_base}.c++" "${output_base}.h"
COMMAND ${CAPNP_EXECUTABLE}
COMMAND "${CAPNP_EXECUTABLE}"
ARGS compile
-o ${CAPNPC_CXX_EXECUTABLE}${output_dir}
--src-prefix ${CAPNPC_SRC_PREFIX}
${include_path}
${CAPNPC_FLAGS}
${file_path}
DEPENDS ${schema_file}
DEPENDS "${schema_file}"
COMMENT "Compiling Cap'n Proto schema ${schema_file}"
VERBATIM
)
......@@ -121,29 +128,64 @@ function(CAPNP_GENERATE_CPP SOURCES HEADERS)
set(${HEADERS} ${${HEADERS}} PARENT_SCOPE)
endfunction()
find_library(CAPNP_LIB_KJ kj)
find_library(CAPNP_LIB_KJ-ASYNC kj-async)
find_library(CAPNP_LIB_CAPNP capnp)
find_library(CAPNP_LIB_CAPNP-RPC capnp-rpc)
find_library(CAPNP_LIB_CAPNPC capnpc)
mark_as_advanced(CAPNP_LIB_KJ CAPNP_LIB_KJ-ASYNC CAPNP_LIB_CAPNP CAPNP_LIB_CAPNP-RPC CAPNP_LIB_CAPNPC)
set(CAPNP_LIBRARIES
# Find Libraries/Paths =========================================================
# Use pkg-config to get path hints and definitions
find_package(PkgConfig QUIET)
pkg_check_modules(PKGCONFIG_CAPNP capnp)
pkg_check_modules(PKGCONFIG_CAPNP_RPC capnp-rpc QUIET)
find_library(CAPNP_LIB_KJ kj
HINTS "${PKGCONFIG_CAPNP_LIBDIR}" ${PKGCONFIG_CAPNP_LIBRARY_DIRS}
)
find_library(CAPNP_LIB_KJ-ASYNC kj-async
HINTS "${PKGCONFIG_CAPNP_RPC_LIBDIR}" ${PKGCONFIG_CAPNP_RPC_LIBRARY_DIRS}
)
find_library(CAPNP_LIB_CAPNP capnp
HINTS "${PKGCONFIG_CAPNP_LIBDIR}" ${PKGCONFIG_CAPNP_LIBRARY_DIRS}
)
find_library(CAPNP_LIB_CAPNP-RPC capnp-rpc
HINTS "${PKGCONFIG_CAPNP_RPC_LIBDIR}" ${PKGCONFIG_CAPNP_RPC_LIBRARY_DIRS}
)
mark_as_advanced(CAPNP_LIB_KJ CAPNP_LIB_KJ-ASYNC CAPNP_LIB_CAPNP CAPNP_LIB_CAPNP-RPC)
set(CAPNP_LIBRARIES_LITE
${CAPNP_LIB_KJ}
${CAPNP_LIB_KJ-ASYNC}
${CAPNP_LIB_CAPNP}
)
set(CAPNP_LIBRARIES
${CAPNP_LIBRARIES_LITE}
${CAPNP_LIB_KJ-ASYNC}
${CAPNP_LIB_CAPNP-RPC}
${CAPNP_LIB_CAPNPC}
)
find_path(CAPNP_INCLUDE_DIRS capnp/generated-header-support.h)
# Was only the 'lite' library found?
if(CAPNP_LIB_CAPNP AND NOT CAPNP_LIB_CAPNP-RPC)
set(CAPNP_DEFINITIONS -DCAPNP_LITE)
else()
set(CAPNP_DEFINITIONS)
endif()
find_path(CAPNP_INCLUDE_DIRS capnp/generated-header-support.h
HINTS "${PKGCONFIG_CAPNP_INCLUDEDIR}" ${PKGCONFIG_CAPNP_INCLUDE_DIRS}
)
find_program(CAPNP_EXECUTABLE
NAMES capnp
DOC "Cap'n Proto Command-line Tool"
HINTS "${PKGCONFIG_CAPNP_PREFIX}/bin"
)
find_program(CAPNPC_CXX_EXECUTABLE
NAMES capnpc-c++
DOC "Capn'n Proto C++ Compiler"
HINTS "${PKGCONFIG_CAPNP_PREFIX}/bin"
)
# Only *require* the include directory, libkj, and libcapnp. If compiling with
# CAPNP_LITE, nothing else will be found.
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(CAPNP DEFAULT_MSG
CAPNP_INCLUDE_DIRS
CAPNP_LIB_KJ
CAPNP_LIB_CAPNP
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment