Commit da5c228a authored by Adam Simpkins's avatar Adam Simpkins Committed by Facebook Github Bot

fbcode_builder: support installing thrift generated header files

Summary:
Update add_thrift_cpp2_library() to add a PUBLIC_HEADER property to the
generated library target, so that the generated headers can be installed using
an `install()` call.  Also add a `HEADER_INSTALL_DIR` property which indicates
the directory they should be installed into.  A `INCLUDE_DIR` argument was
added to customize the include installation path, should anyone care to do so.

This also removes code that was previously incorrectly installing the
generated headers into the source tree.

Reviewed By: wez

Differential Revision: D16853404

fbshipit-source-id: 6f640b2bd347c99804e074fca0209dda37c8a9cf
parent 74f346e7
...@@ -4,10 +4,26 @@ ...@@ -4,10 +4,26 @@
include(FBCMakeParseArgs) include(FBCMakeParseArgs)
# Generate a C++ library from a thrift file
#
# Parameters:
# - SERVICES <svc1> [<svc2> ...]
# The names of the services defined in the thrift file.
# - DEPENDS <dep1> [<dep2> ...]
# A list of other thrift C++ libraries that this library depends on.
# - OPTIONS <opt1> [<opt2> ...]
# A list of options to pass to the thrift compiler.
# - INCLUDE_DIR <path>
# The sub-directory where generated headers will be installed.
# Defaults to "include" if not specified. The caller must still call
# install() to install the thrift library if desired.
function(add_thrift_cpp2_library LIB_NAME THRIFT_FILE) function(add_thrift_cpp2_library LIB_NAME THRIFT_FILE)
# Parse the arguments # Parse the arguments
set(one_value_args INCLUDE_DIR)
set(multi_value_args SERVICES DEPENDS OPTIONS) set(multi_value_args SERVICES DEPENDS OPTIONS)
fb_cmake_parse_args(ARG "" "" "${multi_value_args}" "${ARGN}") fb_cmake_parse_args(
ARG "" "${one_value_args}" "${multi_value_args}" "${ARGN}"
)
get_filename_component(base ${THRIFT_FILE} NAME_WE) get_filename_component(base ${THRIFT_FILE} NAME_WE)
get_filename_component( get_filename_component(
...@@ -23,6 +39,9 @@ function(add_thrift_cpp2_library LIB_NAME THRIFT_FILE) ...@@ -23,6 +39,9 @@ function(add_thrift_cpp2_library LIB_NAME THRIFT_FILE)
"${CMAKE_CURRENT_SOURCE_DIR}/${THRIFT_FILE}" "${CMAKE_CURRENT_SOURCE_DIR}/${THRIFT_FILE}"
) )
get_filename_component(include_prefix ${include_prefix} DIRECTORY) get_filename_component(include_prefix ${include_prefix} DIRECTORY)
if(NOT DEFINED ARG_INCLUDE_DIR)
set(ARG_INCLUDE_DIR "include")
endif()
if (NOT "${include_prefix}" STREQUAL "") if (NOT "${include_prefix}" STREQUAL "")
list(APPEND ARG_OPTIONS "include_prefix=${include_prefix}") list(APPEND ARG_OPTIONS "include_prefix=${include_prefix}")
...@@ -110,19 +129,11 @@ function(add_thrift_cpp2_library LIB_NAME THRIFT_FILE) ...@@ -110,19 +129,11 @@ function(add_thrift_cpp2_library LIB_NAME THRIFT_FILE)
${generated_sources} ${generated_sources}
) )
install(
FILES ${generated_headers}
DESTINATION $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/gen-cpp2
)
target_include_directories( target_include_directories(
"${LIB_NAME}" "${LIB_NAME}"
PRIVATE
${CMAKE_SOURCE_DIR}
${CMAKE_BINARY_DIR}
PUBLIC PUBLIC
${FOLLY_INCLUDE_DIR} "$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>"
${FBTHRIFT_INCLUDE_DIR} "$<INSTALL_INTERFACE:${ARG_INCLUDE_DIR}>"
) )
target_link_libraries( target_link_libraries(
"${LIB_NAME}" "${LIB_NAME}"
...@@ -132,14 +143,27 @@ function(add_thrift_cpp2_library LIB_NAME THRIFT_FILE) ...@@ -132,14 +143,27 @@ function(add_thrift_cpp2_library LIB_NAME THRIFT_FILE)
Folly::folly Folly::folly
) )
# Add ${generated_headers} to the PUBLIC_HEADER property for ${LIB_NAME}
#
# This allows callers to install it using
# "install(TARGETS ${LIB_NAME} PUBLIC_HEADER)"
# However, note that CMake's PUBLIC_HEADER behavior is rather inflexible,
# and does have any way to preserve header directory structure. Callers
# must be careful to use the correct PUBLIC_HEADER DESTINATION parameter
# when doing this, to put the files the correct directory themselves.
# We define a HEADER_INSTALL_DIR property with the include directory prefix,
# so typically callers should specify the PUBLIC_HEADER DESTINATION as
# "$<TARGET_PROPERTY:${LIB_NAME},HEADER_INSTALL_DIR>"
set_property(
TARGET "${LIB_NAME}"
PROPERTY PUBLIC_HEADER ${generated_headers}
)
set_target_properties( set_target_properties(
"${LIB_NAME}" "${LIB_NAME}"
PROPERTIES PROPERTIES
EXPORT_PROPERTIES "THRIFT_INCLUDE_DIRECTORY" EXPORT_PROPERTIES "THRIFT_INCLUDE_DIRECTORY"
THRIFT_INCLUDE_DIRECTORY "${CMAKE_SOURCE_DIR}" THRIFT_INCLUDE_DIRECTORY "${CMAKE_SOURCE_DIR}"
HEADER_INSTALL_DIR "${ARG_INCLUDE_DIR}/${include_prefix}/gen-cpp2"
) )
get_property(thrift_include_directory
TARGET "${LIB_NAME}"
PROPERTY THRIFT_INCLUDE_DIRECTORY)
endfunction() endfunction()
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