| 1 |
# Copyright (c) Facebook, Inc. and its affiliates. |
| 2 |
|
| 3 |
include(FBCMakeParseArgs) |
| 4 |
|
| 5 |
# Generate a C++ library from a thrift file |
| 6 |
# |
| 7 |
# Parameters: |
| 8 |
# - SERVICES <svc1> [<svc2> ...] |
| 9 |
# The names of the services defined in the thrift file. |
| 10 |
# - DEPENDS <dep1> [<dep2> ...] |
| 11 |
# A list of other thrift C++ libraries that this library depends on. |
| 12 |
# - OPTIONS <opt1> [<opt2> ...] |
| 13 |
# A list of options to pass to the thrift compiler. |
| 14 |
# - INCLUDE_DIR <path> |
| 15 |
# The sub-directory where generated headers will be installed. |
| 16 |
# Defaults to "include" if not specified. The caller must still call |
| 17 |
# install() to install the thrift library if desired. |
| 18 |
# - THRIFT_INCLUDE_DIR <path> |
| 19 |
# The sub-directory where generated headers will be installed. |
| 20 |
# Defaults to "${INCLUDE_DIR}/thrift-files" if not specified. |
| 21 |
# The caller must still call install() to install the thrift library if |
| 22 |
# desired. |
| 23 |
function(add_fbthrift_cpp_library LIB_NAME THRIFT_FILE) |
| 24 |
# Parse the arguments |
| 25 |
set(one_value_args INCLUDE_DIR THRIFT_INCLUDE_DIR) |
| 26 |
set(multi_value_args SERVICES DEPENDS OPTIONS) |
| 27 |
fb_cmake_parse_args( |
| 28 |
ARG "" "${one_value_args}" "${multi_value_args}" "${ARGN}" |
| 29 |
) |
| 30 |
if(NOT DEFINED ARG_INCLUDE_DIR) |
| 31 |
set(ARG_INCLUDE_DIR "include") |
| 32 |
endif() |
| 33 |
if(NOT DEFINED ARG_THRIFT_INCLUDE_DIR) |
| 34 |
set(ARG_THRIFT_INCLUDE_DIR "${ARG_INCLUDE_DIR}/thrift-files") |
| 35 |
endif() |
| 36 |
|
| 37 |
get_filename_component(base ${THRIFT_FILE} NAME_WE) |
| 38 |
get_filename_component( |
| 39 |
output_dir |
| 40 |
${CMAKE_CURRENT_BINARY_DIR}/${THRIFT_FILE} |
| 41 |
DIRECTORY |
| 42 |
) |
| 43 |
|
| 44 |
# Generate relative paths in #includes |
| 45 |
file( |
| 46 |
RELATIVE_PATH include_prefix |
| 47 |
"${CMAKE_SOURCE_DIR}" |
| 48 |
"${CMAKE_CURRENT_SOURCE_DIR}/${THRIFT_FILE}" |
| 49 |
) |
| 50 |
get_filename_component(include_prefix ${include_prefix} DIRECTORY) |
| 51 |
|
| 52 |
if (NOT "${include_prefix}" STREQUAL "") |
| 53 |
list(APPEND ARG_OPTIONS "include_prefix=${include_prefix}") |
| 54 |
endif() |
| 55 |
# CMake 3.12 is finally getting a list(JOIN) function, but until then |
| 56 |
# treating the list as a string and replacing the semicolons is good enough. |
| 57 |
string(REPLACE ";" "," GEN_ARG_STR "${ARG_OPTIONS}") |
| 58 |
|
| 59 |
# Compute the list of generated files |
| 60 |
list(APPEND generated_headers |
| 61 |
"${output_dir}/gen-cpp2/${base}_constants.h" |
| 62 |
"${output_dir}/gen-cpp2/${base}_types.h" |
| 63 |
"${output_dir}/gen-cpp2/${base}_types.tcc" |
| 64 |
"${output_dir}/gen-cpp2/${base}_types_custom_protocol.h" |
| 65 |
"${output_dir}/gen-cpp2/${base}_metadata.h" |
| 66 |
) |
| 67 |
list(APPEND generated_sources |
| 68 |
"${output_dir}/gen-cpp2/${base}_constants.cpp" |
| 69 |
"${output_dir}/gen-cpp2/${base}_data.h" |
| 70 |
"${output_dir}/gen-cpp2/${base}_data.cpp" |
| 71 |
"${output_dir}/gen-cpp2/${base}_types.cpp" |
| 72 |
"${output_dir}/gen-cpp2/${base}_metadata.cpp" |
| 73 |
) |
| 74 |
foreach(service IN LISTS ARG_SERVICES) |
| 75 |
list(APPEND generated_headers |
| 76 |
"${output_dir}/gen-cpp2/${service}.h" |
| 77 |
"${output_dir}/gen-cpp2/${service}.tcc" |
| 78 |
"${output_dir}/gen-cpp2/${service}AsyncClient.h" |
| 79 |
"${output_dir}/gen-cpp2/${service}_custom_protocol.h" |
| 80 |
) |
| 81 |
list(APPEND generated_sources |
| 82 |
"${output_dir}/gen-cpp2/${service}.cpp" |
| 83 |
"${output_dir}/gen-cpp2/${service}AsyncClient.cpp" |
| 84 |
"${output_dir}/gen-cpp2/${service}_processmap_binary.cpp" |
| 85 |
"${output_dir}/gen-cpp2/${service}_processmap_compact.cpp" |
| 86 |
) |
| 87 |
endforeach() |
| 88 |
|
| 89 |
# This generator expression gets the list of include directories required |
| 90 |
# for all of our dependencies. |
| 91 |
# It requires using COMMAND_EXPAND_LISTS in the add_custom_command() call |
| 92 |
# below. COMMAND_EXPAND_LISTS is only available in CMake 3.8+ |
| 93 |
# If we really had to support older versions of CMake we would probably need |
| 94 |
# to use a wrapper script around the thrift compiler that could take the |
| 95 |
# include list as a single argument and split it up before invoking the |
| 96 |
# thrift compiler. |
| 97 |
if (NOT POLICY CMP0067) |
| 98 |
message(FATAL_ERROR "add_fbthrift_cpp_library() requires CMake 3.8+") |
| 99 |
endif() |
| 100 |
set( |
| 101 |
thrift_include_options |
| 102 |
"-I;$<JOIN:$<TARGET_PROPERTY:${LIB_NAME}.thrift_includes,INTERFACE_INCLUDE_DIRECTORIES>,;-I;>" |
| 103 |
) |
| 104 |
|
| 105 |
# Emit the rule to run the thrift compiler |
| 106 |
add_custom_command( |
| 107 |
OUTPUT |
| 108 |
${generated_headers} |
| 109 |
${generated_sources} |
| 110 |
COMMAND_EXPAND_LISTS |
| 111 |
COMMAND |
| 112 |
"${CMAKE_COMMAND}" -E make_directory "${output_dir}" |
| 113 |
COMMAND |
| 114 |
"${FBTHRIFT_COMPILER}" |
| 115 |
--legacy-strict |
| 116 |
--gen "mstch_cpp2:${GEN_ARG_STR}" |
| 117 |
"${thrift_include_options}" |
| 118 |
-I "${FBTHRIFT_INCLUDE_DIR}" |
| 119 |
-o "${output_dir}" |
| 120 |
"${CMAKE_CURRENT_SOURCE_DIR}/${THRIFT_FILE}" |
| 121 |
WORKING_DIRECTORY |
| 122 |
"${CMAKE_BINARY_DIR}" |
| 123 |
MAIN_DEPENDENCY |
| 124 |
"${THRIFT_FILE}" |
| 125 |
DEPENDS |
| 126 |
${ARG_DEPENDS} |
| 127 |
"${FBTHRIFT_COMPILER}" |
| 128 |
) |
| 129 |
|
| 130 |
# Now emit the library rule to compile the sources |
| 131 |
if (BUILD_SHARED_LIBS) |
| 132 |
set(LIB_TYPE SHARED) |
| 133 |
else () |
| 134 |
set(LIB_TYPE STATIC) |
| 135 |
endif () |
| 136 |
|
| 137 |
add_library( |
| 138 |
"${LIB_NAME}" ${LIB_TYPE} |
| 139 |
${generated_sources} |
| 140 |
) |
| 141 |
|
| 142 |
target_include_directories( |
| 143 |
"${LIB_NAME}" |
| 144 |
PUBLIC |
| 145 |
"$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>" |
| 146 |
"$<INSTALL_INTERFACE:${ARG_INCLUDE_DIR}>" |
| 147 |
) |
| 148 |
target_link_libraries( |
| 149 |
"${LIB_NAME}" |
| 150 |
PUBLIC |
| 151 |
${ARG_DEPENDS} |
| 152 |
FBThrift::thriftcpp2 |
| 153 |
Folly::folly |
| 154 |
mvfst::mvfst_server_async_tran |
| 155 |
mvfst::mvfst_server |
| 156 |
) |
| 157 |
|
| 158 |
# Add ${generated_headers} to the PUBLIC_HEADER property for ${LIB_NAME} |
| 159 |
# |
| 160 |
# This allows callers to install it using |
| 161 |
# "install(TARGETS ${LIB_NAME} PUBLIC_HEADER)" |
| 162 |
# However, note that CMake's PUBLIC_HEADER behavior is rather inflexible, |
| 163 |
# and does have any way to preserve header directory structure. Callers |
| 164 |
# must be careful to use the correct PUBLIC_HEADER DESTINATION parameter |
| 165 |
# when doing this, to put the files the correct directory themselves. |
| 166 |
# We define a HEADER_INSTALL_DIR property with the include directory prefix, |
| 167 |
# so typically callers should specify the PUBLIC_HEADER DESTINATION as |
| 168 |
# "$<TARGET_PROPERTY:${LIB_NAME},HEADER_INSTALL_DIR>" |
| 169 |
set_property( |
| 170 |
TARGET "${LIB_NAME}" |
| 171 |
PROPERTY PUBLIC_HEADER ${generated_headers} |
| 172 |
) |
| 173 |
|
| 174 |
# Define a dummy interface library to help propagate the thrift include |
| 175 |
# directories between dependencies. |
| 176 |
add_library("${LIB_NAME}.thrift_includes" INTERFACE) |
| 177 |
target_include_directories( |
| 178 |
"${LIB_NAME}.thrift_includes" |
| 179 |
INTERFACE |
| 180 |
"$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}>" |
| 181 |
"$<INSTALL_INTERFACE:${ARG_THRIFT_INCLUDE_DIR}>" |
| 182 |
) |
| 183 |
foreach(dep IN LISTS ARG_DEPENDS) |
| 184 |
target_link_libraries( |
| 185 |
"${LIB_NAME}.thrift_includes" |
| 186 |
INTERFACE "${dep}.thrift_includes" |
| 187 |
) |
| 188 |
endforeach() |
| 189 |
|
| 190 |
set_target_properties( |
| 191 |
"${LIB_NAME}" |
| 192 |
PROPERTIES |
| 193 |
EXPORT_PROPERTIES "THRIFT_INSTALL_DIR" |
| 194 |
THRIFT_INSTALL_DIR "${ARG_THRIFT_INCLUDE_DIR}/${include_prefix}" |
| 195 |
HEADER_INSTALL_DIR "${ARG_INCLUDE_DIR}/${include_prefix}/gen-cpp2" |
| 196 |
) |
| 197 |
endfunction() |