| 1 |
# Copyright (c) Facebook, Inc. and its affiliates. |
| 2 |
|
| 3 |
include(FBCMakeParseArgs) |
| 4 |
include(FBThriftPyLibrary) |
| 5 |
include(FBThriftCppLibrary) |
| 6 |
|
| 7 |
# |
| 8 |
# add_fbthrift_library() |
| 9 |
# |
| 10 |
# This is a convenience function that generates thrift libraries for multiple |
| 11 |
# languages. |
| 12 |
# |
| 13 |
# For example: |
| 14 |
# add_fbthrift_library( |
| 15 |
# foo foo.thrift |
| 16 |
# LANGUAGES cpp py |
| 17 |
# SERVICES Foo |
| 18 |
# DEPENDS bar) |
| 19 |
# |
| 20 |
# will be expanded into two separate calls: |
| 21 |
# |
| 22 |
# add_fbthrift_cpp_library(foo_cpp foo.thrift SERVICES Foo DEPENDS bar_cpp) |
| 23 |
# add_fbthrift_py_library(foo_py foo.thrift SERVICES Foo DEPENDS bar_py) |
| 24 |
# |
| 25 |
function(add_fbthrift_library LIB_NAME THRIFT_FILE) |
| 26 |
# Parse the arguments |
| 27 |
set(one_value_args PY_NAMESPACE INCLUDE_DIR THRIFT_INCLUDE_DIR) |
| 28 |
set(multi_value_args SERVICES DEPENDS LANGUAGES CPP_OPTIONS PY_OPTIONS) |
| 29 |
fb_cmake_parse_args( |
| 30 |
ARG "" "${one_value_args}" "${multi_value_args}" "${ARGN}" |
| 31 |
) |
| 32 |
|
| 33 |
if(NOT DEFINED ARG_INCLUDE_DIR) |
| 34 |
set(ARG_INCLUDE_DIR "include") |
| 35 |
endif() |
| 36 |
if(NOT DEFINED ARG_THRIFT_INCLUDE_DIR) |
| 37 |
set(ARG_THRIFT_INCLUDE_DIR "${ARG_INCLUDE_DIR}/thrift-files") |
| 38 |
endif() |
| 39 |
|
| 40 |
# CMake 3.12+ adds list(TRANSFORM) which would be nice to use here, but for |
| 41 |
# now we still want to support older versions of CMake. |
| 42 |
set(CPP_DEPENDS) |
| 43 |
set(PY_DEPENDS) |
| 44 |
foreach(dep IN LISTS ARG_DEPENDS) |
| 45 |
list(APPEND CPP_DEPENDS "${dep}_cpp") |
| 46 |
list(APPEND PY_DEPENDS "${dep}_py") |
| 47 |
endforeach() |
| 48 |
|
| 49 |
foreach(lang IN LISTS ARG_LANGUAGES) |
| 50 |
if ("${lang}" STREQUAL "cpp") |
| 51 |
add_fbthrift_cpp_library( |
| 52 |
"${LIB_NAME}_cpp" "${THRIFT_FILE}" |
| 53 |
SERVICES ${ARG_SERVICES} |
| 54 |
DEPENDS ${CPP_DEPENDS} |
| 55 |
OPTIONS ${ARG_CPP_OPTIONS} |
| 56 |
INCLUDE_DIR "${ARG_INCLUDE_DIR}" |
| 57 |
THRIFT_INCLUDE_DIR "${ARG_THRIFT_INCLUDE_DIR}" |
| 58 |
) |
| 59 |
elseif ("${lang}" STREQUAL "py" OR "${lang}" STREQUAL "python") |
| 60 |
if (DEFINED ARG_PY_NAMESPACE) |
| 61 |
set(namespace_args NAMESPACE "${ARG_PY_NAMESPACE}") |
| 62 |
endif() |
| 63 |
add_fbthrift_py_library( |
| 64 |
"${LIB_NAME}_py" "${THRIFT_FILE}" |
| 65 |
SERVICES ${ARG_SERVICES} |
| 66 |
${namespace_args} |
| 67 |
DEPENDS ${PY_DEPENDS} |
| 68 |
OPTIONS ${ARG_PY_OPTIONS} |
| 69 |
THRIFT_INCLUDE_DIR "${ARG_THRIFT_INCLUDE_DIR}" |
| 70 |
) |
| 71 |
else() |
| 72 |
message( |
| 73 |
FATAL_ERROR "unknown language for thrift library ${LIB_NAME}: ${lang}" |
| 74 |
) |
| 75 |
endif() |
| 76 |
endforeach() |
| 77 |
endfunction() |