-
Notifications
You must be signed in to change notification settings - Fork 630
Expand file tree
/
Copy pathThriftLibrary.cmake
More file actions
374 lines (364 loc) · 13.1 KB
/
ThriftLibrary.cmake
File metadata and controls
374 lines (364 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Requirements:
# Please provide the following two variables before using these macros:
# ${THRIFT1} - path/to/bin/thrift1
# ${THRIFTCPP2} - path/to/lib/thriftcpp2
#
#
# thrift_object
# This creates a object that will contain the source files and all the proper
# dependencies to generate and compile thrift generated files
#
# Params:
# @file_name - The name of the thrift file
# @services - A list of services that are declared in the thrift file
# @language - The generator to use (cpp, cpp2, or python)
# @options - Extra options to pass to the generator
# @file_path - The directory where the thrift file lives
# @output_path - The directory where the thrift objects will be built
# @include_prefix - The last part of output_path, relative include prefix
#
# Output:
# A object file named `${file-name}-${language}-obj` to include into your
# project's library
#
# Notes:
# If any of the fields is empty, it is still required to provide an empty string
#
# Usage:
# thrift_object(
# #file_name
# #services
# #language
# #options
# #file_path
# #output_path
# #include_prefix
# )
# add_library(somelib $<TARGET_OBJECTS:${file_name}-${language}-obj> ...)
#
macro(thrift_object
file_name
services
language
options
file_path
output_path
include_prefix
)
thrift_generate(
"${file_name}"
"${services}"
"${language}"
"${options}"
"${file_path}"
"${output_path}"
"${include_prefix}"
${ARGN}
)
if("${language}" STREQUAL "python")
# Python: generated .py files are the artifacts
message("Thrift will generate Python files for : ${file_name}-${language}")
else()
bypass_source_check(${${file_name}-${language}-SOURCES})
add_library(
"${file_name}-${language}-obj"
OBJECT
${${file_name}-${language}-SOURCES}
)
add_dependencies(
"${file_name}-${language}-obj"
"${file_name}-${language}-target"
)
message("Thrift will create the Object file : ${file_name}-${language}-obj")
endif()
endmacro()
# thrift_library
# Same as thrift object in terms of usage but creates the library instead of
# object so that you can use to link against your library instead of including
# all symbols into your library
#
# Params:
# @file_name - The name of the thrift file
# @services - A list of services that are declared in the thrift file
# @language - The generator to use (cpp, cpp2, or python)
# @options - Extra options to pass to the generator
# @file_path - The directory where the thrift file lives
# @output_path - The directory where the thrift objects will be built
# @include_prefix - The last part of output_path, relative include prefix
#
# Output:
# A library file named `${file-name}-${language}` to link against your
# project's library
#
# Notes:
# If any of the fields is empty, it is still required to provide an empty string
#
# Usage:
# thrift_library(
# #file_name
# #services
# #language
# #options
# #file_path
# #output_path
# #include_prefix
# )
# add_library(somelib ...)
# target_link_libraries(somelib ${file_name}-${language} ...)
#
macro(thrift_library
file_name
services
language
options
file_path
output_path
include_prefix
)
thrift_object(
"${file_name}"
"${services}"
"${language}"
"${options}"
"${file_path}"
"${output_path}"
"${include_prefix}"
${ARGN}
)
if("${language}" STREQUAL "python")
# Python: create an alias target so users can depend on <name>-python
add_custom_target("${file_name}-${language}" ALL)
add_dependencies("${file_name}-${language}" "${file_name}-${language}-target")
message("Thrift will create the Python library : ${file_name}-${language}")
else()
add_library(
"${file_name}-${language}"
$<TARGET_OBJECTS:${file_name}-${language}-obj>
)
target_link_libraries("${file_name}-${language}" ${THRIFTCPP2})
message("Thrift will create the Library file : ${file_name}-${language}")
endif()
endmacro()
#
# bypass_source_check
# This tells cmake to ignore if it doesn't see the following sources in
# the library that will be installed. Thrift files are generated at compile
# time so they do not exist at source check time
#
# Params:
# @sources - The list of files to ignore in source check
#
macro(bypass_source_check sources)
set_source_files_properties(
${sources}
PROPERTIES GENERATED TRUE
)
endmacro()
#
# thrift_generate
# This is used to codegen thrift files using the thrift compiler
# Supports library names that differ from the file name (to handle two libraries
# with the same filename on disk (in different folders))
# Params:
# @file_name - Input file name. Will be used for naming the CMake
# target if TARGET_NAME_BASE is not specified.
# @services - A list of services that are declared in the thrift file
# @language - The generator to use (cpp, cpp2, py3, or python)
# @options - Extra options to pass to the generator
# @output_path - The directory where the thrift file lives
# @include_prefix - Prefix to use for thrift includes in generated sources
# @TARGET_NAME_BASE (optional) - name used for target instead of real filename
# @THRIFT_INCLUDE_DIRECTORIES (optional) path to thrift include directories
# @NAMESPACE (optional) - Must match the "namespace py3" directive in the
# .thrift file. Dot-separated (e.g. "example.sum") maps to output
# directory structure (example/sum/<file_name>/).
#
# Output:
# file-language-target - A custom target to add a dependency
# ${file-language-HEADERS} - The generated Header Files.
# ${file-language-SOURCES} - The generated Source Files.
#
# Notes:
# If any of the fields is empty, it is still required to provide an empty string
#
# When using file_language-SOURCES it should always call:
# bypass_source_check(${file_language-SOURCES})
# This will prevent cmake from complaining about missing source files
#
macro(thrift_generate
file_name
services
language
options
file_path
output_path
include_prefix
)
cmake_parse_arguments(THRIFT_GENERATE # Prefix
"" # Options
"TARGET_NAME_BASE;NAMESPACE" # One Value args
"THRIFT_INCLUDE_DIRECTORIES" # Multi-value args
"${ARGN}")
set(source_file_name ${file_name})
set(target_file_name ${file_name})
set(thrift_include_directories)
foreach(dir ${THRIFT_GENERATE_THRIFT_INCLUDE_DIRECTORIES})
list(APPEND thrift_include_directories "-I" "${dir}")
endforeach()
if(DEFINED THRIFT_GENERATE_TARGET_NAME_BASE
AND NOT THRIFT_GENERATE_TARGET_NAME_BASE STREQUAL "")
set(target_file_name ${THRIFT_GENERATE_TARGET_NAME_BASE})
endif()
set("${target_file_name}-${language}-HEADERS"
${output_path}/gen-${language}/${source_file_name}_constants.h
${output_path}/gen-${language}/${source_file_name}_data.h
${output_path}/gen-${language}/${source_file_name}_metadata.h
${output_path}/gen-${language}/${source_file_name}_types.h
${output_path}/gen-${language}/${source_file_name}_types.tcch
${output_path}/gen-${language}/${source_file_name}_types_custom_protocol.h
)
set("${target_file_name}-${language}-SOURCES"
${output_path}/gen-${language}/${source_file_name}_constants.cpp
${output_path}/gen-${language}/${source_file_name}_data.cpp
${output_path}/gen-${language}/${source_file_name}_types.cpp
${output_path}/gen-${language}/${source_file_name}_types_binary.cpp
${output_path}/gen-${language}/${source_file_name}_types_compact.cpp
${output_path}/gen-${language}/${source_file_name}_types_serialization.cpp
)
if("${options}" MATCHES "layouts")
set("${target_file_name}-${language}-SOURCES"
${${target_file_name}-${language}-SOURCES}
${output_path}/gen-${language}/${source_file_name}_layouts.cpp
)
endif()
if(NOT "${options}" MATCHES "no_metadata")
set("${target_file_name}-${language}-SOURCES"
${${target_file_name}-${language}-SOURCES}
${output_path}/gen-${language}/${source_file_name}_metadata.cpp
)
endif()
foreach(service ${services})
set("${target_file_name}-${language}-HEADERS"
${${source_file_name}-${language}-HEADERS}
${output_path}/gen-${language}/${service}.h
${output_path}/gen-${language}/${service}.tcc
${output_path}/gen-${language}/${service}AsyncClient.h
${output_path}/gen-${language}/${service}_custom_protocol.h
)
set("${target_file_name}-${language}-SOURCES"
${${source_file_name}-${language}-SOURCES}
${output_path}/gen-${language}/${service}.cpp
${output_path}/gen-${language}/${service}AsyncClient.cpp
)
endforeach()
if("${include_prefix}" STREQUAL "")
set(include_prefix_text "")
else()
set(include_prefix_text "include_prefix=${include_prefix}")
if(NOT "${options}" STREQUAL "")
set(include_prefix_text ",${include_prefix_text}")
endif()
endif()
set(gen_language ${language})
if("${language}" STREQUAL "cpp2")
set(gen_language "mstch_cpp2")
elseif("${language}" STREQUAL "py3")
set(gen_language "mstch_py3")
file(WRITE "${output_path}/gen-${language}/${source_file_name}/__init__.py")
elseif("${language}" STREQUAL "python")
set(gen_language "mstch_python")
# thrift-python uses PEP 420 namespace packages — no __init__.py.
# NAMESPACE must match "namespace py3" in the .thrift file.
# Dot-separated value maps to directory: "example.sum" -> example/sum/<file_name>/
if(DEFINED THRIFT_GENERATE_NAMESPACE
AND NOT THRIFT_GENERATE_NAMESPACE STREQUAL "")
string(REPLACE "." "/" _namespace_dir "${THRIFT_GENERATE_NAMESPACE}")
set(_python_output_subdir "${_namespace_dir}/${source_file_name}")
else()
set(_python_output_subdir "${source_file_name}")
endif()
# Override C++ HEADERS/SOURCES with Python output files
set("${target_file_name}-${language}-HEADERS" "")
set("${target_file_name}-${language}-SOURCES"
${output_path}/gen-python/${_python_output_subdir}/thrift_types.py
${output_path}/gen-python/${_python_output_subdir}/thrift_types.pyi
${output_path}/gen-python/${_python_output_subdir}/thrift_metadata.py
${output_path}/gen-python/${_python_output_subdir}/thrift_enums.py
${output_path}/gen-python/${_python_output_subdir}/thrift_abstract_types.py
${output_path}/gen-python/${_python_output_subdir}/thrift_mutable_types.py
${output_path}/gen-python/${_python_output_subdir}/thrift_mutable_types.pyi
)
if(NOT "${services}" STREQUAL "")
list(APPEND "${target_file_name}-${language}-SOURCES"
${output_path}/gen-python/${_python_output_subdir}/thrift_services.py
${output_path}/gen-python/${_python_output_subdir}/thrift_clients.py
${output_path}/gen-python/${_python_output_subdir}/thrift_mutable_services.py
${output_path}/gen-python/${_python_output_subdir}/thrift_mutable_clients.py
)
endif()
# Python options format differs from C++: no include_prefix
if(NOT "${options}" STREQUAL "")
set(_python_gen_options ":${options}")
else()
set(_python_gen_options "")
endif()
add_custom_command(
OUTPUT ${${target_file_name}-${language}-SOURCES}
COMMAND ${THRIFT1}
--gen "${gen_language}${_python_gen_options}"
-o ${output_path}
${thrift_include_directories}
"${file_path}/${source_file_name}.thrift"
DEPENDS
${THRIFT1}
"${file_path}/${source_file_name}.thrift"
COMMENT "Generating ${target_file_name} thrift-python files. Output: ${output_path}"
)
add_custom_target(
${target_file_name}-${language}-target ALL
DEPENDS ${${target_file_name}-${language}-SOURCES}
)
endif()
if(NOT "${language}" STREQUAL "python")
add_custom_command(
OUTPUT ${${target_file_name}-${language}-HEADERS}
${${target_file_name}-${language}-SOURCES}
COMMAND ${THRIFT1}
--gen "${gen_language}:${options}${include_prefix_text}"
-o ${output_path}
${thrift_include_directories}
"${file_path}/${source_file_name}.thrift"
DEPENDS
${THRIFT1}
"${file_path}/${source_file_name}.thrift"
COMMENT "Generating ${target_file_name} files. Output: ${output_path}"
)
add_custom_target(
${target_file_name}-${language}-target ALL
DEPENDS ${${language}-${language}-HEADERS}
${${target_file_name}-${language}-SOURCES}
)
install(
DIRECTORY gen-${language}
DESTINATION include/${include_prefix}
FILES_MATCHING PATTERN "*.h")
install(
DIRECTORY gen-${language}
DESTINATION include/${include_prefix}
FILES_MATCHING PATTERN "*.tcc")
endif()
endmacro()