Commit cb482fe3 authored by Harris Hancock's avatar Harris Hancock

cmake: capnp_generate_cpp() should import src-prefix

capnp_generate_cpp() needs to import two directories by default: the src-prefix directory which houses the .capnp files being compiled (under their canonical paths), and the capnp include directory which houses capnp/rpc.capnp, capnp/schema.capnp, etc. (This latter path will necessarily be different for build-tree usage versus install-tree usage.) Before this commit, instead of importing the src-prefix, we were importing CMAKE_CURRENT_SOURCE_DIR.

Note that for capnproto's own compilation of test.capnp and friends, the src-prefix and capnp include directory will end up being the exact same directory. It turns out to be surprisingly difficult to de-duplicate them, however, because the capnp include directory is actually a CMake generator expression in order to deal with the build-tree versus install-tree issue mentioned above. So, for now, we pass two -I flags to the same directory.

Alongside the fix in capnp_generate_cpp(), I fixed the usage of it in c++/src/capnp/CMakeLists.txt by setting CAPNPC_SRC_PREFIX correctly and removing the /capnp suffix hack from CAPNPC_OUTPUT_DIR.
parent ac10f36b
......@@ -15,7 +15,7 @@
# Directory to place compiled schema sources (default: CMAKE_CURRENT_BINARY_DIR).
# CAPNPC_IMPORT_DIRS
# List of additional include directories for the schema compiler.
# (CMAKE_CURRENT_SOURCE_DIR and CAPNP_INCLUDE_DIRECTORY are always included.)
# (CAPNPC_SRC_PREFIX and CAPNP_INCLUDE_DIRECTORY are always included.)
# CAPNPC_SRC_PREFIX
# Schema file source prefix (default: CMAKE_CURRENT_SOURCE_DIR).
# CAPNPC_FLAGS
......@@ -45,17 +45,6 @@ function(CAPNP_GENERATE_CPP SOURCES HEADERS)
message(SEND_ERROR "Could not locate capnp header files (CAPNP_INCLUDE_DIRECTORY).")
endif()
# Default compiler includes
set(include_path -I ${CMAKE_CURRENT_SOURCE_DIR} -I ${CAPNP_INCLUDE_DIRECTORY})
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)
list(APPEND include_path -I ${absolute_path})
endforeach()
endif()
if(DEFINED CAPNPC_OUTPUT_DIR)
# Prepend a ':' to get the format for the '-o' flag right
set(output_dir ":${CAPNPC_OUTPUT_DIR}")
......@@ -68,6 +57,22 @@ function(CAPNP_GENERATE_CPP SOURCES HEADERS)
endif()
get_filename_component(CAPNPC_SRC_PREFIX "${CAPNPC_SRC_PREFIX}" ABSOLUTE)
# Default compiler includes. Note that in capnp's own test usage of capnp_generate_cpp(), these
# two variables will end up evaluating to the same directory. However, it's difficult to
# deduplicate them because if CAPNP_INCLUDE_DIRECTORY came from the capnp_tool target property,
# then it must be a generator expression in order to handle usages in both the build tree and the
# install tree. This vastly overcomplicates duplication detection, so the duplication doesn't seem
# worth fixing.
set(include_path -I "${CAPNPC_SRC_PREFIX}" -I "${CAPNP_INCLUDE_DIRECTORY}")
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)
list(APPEND include_path -I "${absolute_path}")
endforeach()
endif()
set(${SOURCES})
set(${HEADERS})
foreach(schema_file ${ARGN})
......
......@@ -194,10 +194,13 @@ if(BUILD_TESTING)
test-import2.capnp
)
# 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"
set(CAPNPC_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/test_capnp")
include_directories("${CAPNPC_OUTPUT_DIR}")
file(MAKE_DIRECTORY "${CAPNPC_OUTPUT_DIR}")
# Tell capnp_generate_cpp to set --src-prefix to our parent directory. This allows us to pass our
# .capnp files relative to this directory, but have their canonical name end up as
# capnp/test.capnp, capnp/test-import.capnp, etc.
get_filename_component(CAPNPC_SRC_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}" DIRECTORY)
capnp_generate_cpp(test_capnp_cpp_files test_capnp_h_files ${test_capnp_files})
......
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