Debian Codegraph @

thirdparty/folly/build/fbcode_builder/CMake/FBThriftPyLibrary.cmake

raw
1 # Copyright (c) Facebook, Inc. and its affiliates.
2
3 include(FBCMakeParseArgs)
4 include(FBPythonBinary)
5
6 # Generate a Python library from a thrift file
7 function(add_fbthrift_py_library LIB_NAME THRIFT_FILE)
8 # Parse the arguments
9 set(one_value_args NAMESPACE THRIFT_INCLUDE_DIR)
10 set(multi_value_args SERVICES DEPENDS OPTIONS)
11 fb_cmake_parse_args(
12 ARG "" "${one_value_args}" "${multi_value_args}" "${ARGN}"
13 )
14
15 if(NOT DEFINED ARG_THRIFT_INCLUDE_DIR)
16 set(ARG_THRIFT_INCLUDE_DIR "include/thrift-files")
17 endif()
18
19 get_filename_component(base ${THRIFT_FILE} NAME_WE)
20 set(output_dir "${CMAKE_CURRENT_BINARY_DIR}/${THRIFT_FILE}-py")
21
22 # Parse the namespace value
23 if (NOT DEFINED ARG_NAMESPACE)
24 set(ARG_NAMESPACE "${base}")
25 endif()
26
27 string(REPLACE "." "/" namespace_dir "${ARG_NAMESPACE}")
28 set(py_output_dir "${output_dir}/gen-py/${namespace_dir}")
29 list(APPEND generated_sources
30 "${py_output_dir}/__init__.py"
31 "${py_output_dir}/ttypes.py"
32 "${py_output_dir}/constants.py"
33 )
34 foreach(service IN LISTS ARG_SERVICES)
35 list(APPEND generated_sources
36 ${py_output_dir}/${service}.py
37 )
38 endforeach()
39
40 # Define a dummy interface library to help propagate the thrift include
41 # directories between dependencies.
42 add_library("${LIB_NAME}.thrift_includes" INTERFACE)
43 target_include_directories(
44 "${LIB_NAME}.thrift_includes"
45 INTERFACE
46 "$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}>"
47 "$<INSTALL_INTERFACE:${ARG_THRIFT_INCLUDE_DIR}>"
48 )
49 foreach(dep IN LISTS ARG_DEPENDS)
50 target_link_libraries(
51 "${LIB_NAME}.thrift_includes"
52 INTERFACE "${dep}.thrift_includes"
53 )
54 endforeach()
55
56 # This generator expression gets the list of include directories required
57 # for all of our dependencies.
58 # It requires using COMMAND_EXPAND_LISTS in the add_custom_command() call
59 # below. COMMAND_EXPAND_LISTS is only available in CMake 3.8+
60 # If we really had to support older versions of CMake we would probably need
61 # to use a wrapper script around the thrift compiler that could take the
62 # include list as a single argument and split it up before invoking the
63 # thrift compiler.
64 if (NOT POLICY CMP0067)
65 message(FATAL_ERROR "add_fbthrift_py_library() requires CMake 3.8+")
66 endif()
67 set(
68 thrift_include_options
69 "-I;$<JOIN:$<TARGET_PROPERTY:${LIB_NAME}.thrift_includes,INTERFACE_INCLUDE_DIRECTORIES>,;-I;>"
70 )
71
72 # Always force generation of "new-style" python classes for Python 2
73 list(APPEND ARG_OPTIONS "new_style")
74 # CMake 3.12 is finally getting a list(JOIN) function, but until then
75 # treating the list as a string and replacing the semicolons is good enough.
76 string(REPLACE ";" "," GEN_ARG_STR "${ARG_OPTIONS}")
77
78 # Emit the rule to run the thrift compiler
79 add_custom_command(
80 OUTPUT
81 ${generated_sources}
82 COMMAND_EXPAND_LISTS
83 COMMAND
84 "${CMAKE_COMMAND}" -E make_directory "${output_dir}"
85 COMMAND
86 "${FBTHRIFT_COMPILER}"
87 --legacy-strict
88 --gen "py:${GEN_ARG_STR}"
89 "${thrift_include_options}"
90 -o "${output_dir}"
91 "${CMAKE_CURRENT_SOURCE_DIR}/${THRIFT_FILE}"
92 WORKING_DIRECTORY
93 "${CMAKE_BINARY_DIR}"
94 MAIN_DEPENDENCY
95 "${THRIFT_FILE}"
96 DEPENDS
97 "${FBTHRIFT_COMPILER}"
98 )
99
100 # We always want to pass the namespace as "" to this call:
101 # thrift will already emit the files with the desired namespace prefix under
102 # gen-py. We don't want add_fb_python_library() to prepend the namespace a
103 # second time.
104 add_fb_python_library(
105 "${LIB_NAME}"
106 BASE_DIR "${output_dir}/gen-py"
107 NAMESPACE ""
108 SOURCES ${generated_sources}
109 DEPENDS ${ARG_DEPENDS} FBThrift::thrift_py
110 )
111 endfunction()