Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,21 @@ set(CMAKE_MODULE_PATH
# Find required dependencies.
find_package(Boost REQUIRED filesystem)
find_package(fmt CONFIG REQUIRED)
find_package(folly CONFIG REQUIRED)
find_package(OpenSSL REQUIRED)

if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
message(STATUS "setting C++ standard to C++${CMAKE_CXX_STANDARD}")
endif()
set(FBTHRIFT_CXX_OPTIONS "-std=c++${CMAKE_CXX_STANDARD}")
set(CMAKE_CXX_EXTENSIONS OFF)

# Explicitly enable coroutine support, since GCC does not enable it
# by default when targeting C++17.
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(FBTHRIFT_CXX_OPTIONS "${FBTHRIFT_CXX_OPTIONS} -fcoroutines")
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fcoroutines>)
endif()

Expand All @@ -94,8 +97,6 @@ option(thriftpy3
OFF
)

find_package(folly CONFIG REQUIRED)

# Find required dependencies for the Thrift compiler.
if (THRIFT_COMPILER_ONLY OR build_all)
include_directories(${OPENSSL_INCLUDE_DIR})
Expand Down
68 changes: 68 additions & 0 deletions build/fbcode_builder/CMake/FBPythonWrapper.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.

function(_write_manifest MANIFEST_FILE PY_TARGET EGG_NAME)
set(egg_glob "${CMAKE_INSTALL_PREFIX}/lib/python*/site-packages/${EGG_NAME}-*.egg")

add_dependencies("${PY_TARGET}" "${MANIFEST_FILE}")
set("${PY_TARGET}" INTERFACE_INCLUDE_DIRECTORIES ${MANIFEST_FILE})
set("${PY_TARGET}" INTERFACE_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib")

set(_install_code [[
file(GLOB _egg_root @egg_glob@)
file(GLOB_RECURSE _egg_files
LIST_DIRECTORIES false
"${_egg_root}/*"
)

set(_manifest_path "@MANIFEST_FILE@")

set(_manifest_contents "FBPY_MANIFEST 1\n")

foreach(_absfile IN LISTS _egg_files)
cmake_path(RELATIVE_PATH _absfile BASE_DIRECTORY ${_egg_root} OUTPUT_VARIABLE _egg_relative)

cmake_path(RELATIVE_PATH _absfile BASE_DIRECTORY ${CMAKE_INSTALL_PREFIX} OUTPUT_VARIABLE _manifest_relative)
set("${PY_TARGET}" INTERFACE_SOURCES $_absfile)
if(NOT ${_egg_relative} MATCHES "^EGG-INFO/")
string(APPEND _manifest_contents "${_manifest_relative} :: ${_egg_relative}\n")
endif()
endforeach()

file(WRITE ${_manifest_path} ${_manifest_contents})
]])
string(CONFIGURE "${_install_code}" _install_code @ONLY)
install(CODE "${_install_code}" DESTINATION ${MANIFEST_FILE} EXPORT)
endfunction()

function(wrap_non_fb_python_library TARGET EGG_NAME)
set(py_lib "${TARGET}.py_lib")
add_library("${py_lib}" INTERFACE)
install(TARGETS "${py_lib}" EXPORT)

set(manifest_filename "${TARGET}.manifest")
set(build_manifest "${CMAKE_CURRENT_BINARY_DIR}/${manifest_filename}")
set(install_manifest "${CMAKE_INSTALL_PREFIX}/${manifest_filename}")
target_include_directories(
"${py_lib}" INTERFACE
"$<BUILD_INTERFACE:${build_manifest}>"
"$<INSTALL_INTERFACE:${install_manifest}>"
)

_write_manifest("${install_manifest}" "${py_lib}" "${EGG_NAME}")

set(build_install_dir "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.lib_install")
set(build_install_manifest "${build_install_dir}/${manifest_filename}")
add_custom_command(
OUTPUT
"${build_install_manifest}"
COMMAND false
DEPENDS
"${install_manifest}"
)
add_custom_target(
"${TARGET}.py_lib_install"
DEPENDS "${build_install_manifest}"
)

# FIXME: do we need the logic from FBPythonBinary:543-553?
endfunction()
143 changes: 118 additions & 25 deletions thrift/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ if(thriftpy)
endif()

if(thriftpy3)
include(FBPythonWrapper)
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)

set(_cybld "${CMAKE_CURRENT_BINARY_DIR}/cybld")
file(MAKE_DIRECTORY "${_cybld}/thrift/python/")
file(MAKE_DIRECTORY "${_cybld}/thrift/python/client/")
file(MAKE_DIRECTORY "${_cybld}/thrift/python/server/")
file(MAKE_DIRECTORY "${_cybld}/thrift/python/server/flagged/")
file(MAKE_DIRECTORY "${_cybld}/thrift/python/server_impl/")
file(MAKE_DIRECTORY "${_cybld}/thrift/python/server_impl/interceptor/")
file(MAKE_DIRECTORY "${_cybld}/thrift/python/streaming/")
file(MAKE_DIRECTORY "${_cybld}/thrift/py3/")

# So that cython includes work correctly
Expand Down Expand Up @@ -110,12 +116,10 @@ if(thriftpy3)
${CMAKE_COMMAND} -E create_symlink "${_cybld}/thrift/python/server/server.pyx" "${_cybld}/thrift/python/server.pyx"
)

########################################################################
# Symlink files that get mapped directly
add_custom_target(create_binding_symlink_python ALL)
file(GLOB BindingFiles
"python/abstract_types.py"
"python/client/client_wrapper.py"
"python/client/__init__.py"
"python/metadata.py"
"python/client/async_client_factory.pxd"
"python/client/async_client.pxd"
"python/client/omni_client.pxd"
Expand All @@ -137,8 +141,13 @@ if(thriftpy3)
"python/server/server.pxd"
"python/std_libcpp.pxd"
"python/stream.pxd"
"python/streaming/py_promise.pxd"
"python/streaming/python_user_exception.pxd"
"python/streaming/sink.pxd"
"python/streaming/stream.pxd"
"python/types.pxd"
"python/util.pxd"

"python/adapter.pyx"
"python/client/async_client_factory.pyx"
"python/client/async_client.pyx"
Expand All @@ -159,20 +168,33 @@ if(thriftpy3)
"python/mutable_types.pyx"
"python/protocol.pyx"
"python/serializer.pyx"
"python/stream.pyx"
"python/server/request_context.pyx"
"python/server/server.pyx"
"python/stream.pyx"
"python/streaming/py_promise.pyx"
"python/streaming/python_user_exception.pyx"
"python/streaming/sink.pyx"
"python/streaming/stream.pyx"
"python/util.pyx"

"python/abstract_types.py"
"python/client/__init__.py"
"python/client/client_wrapper.py"
"python/metadata.py"

"python/flags.h"
"python/types.h"
"python/Serializer.h"
"python/types.h"

"python/server/PythonAsyncProcessor.cpp"
"python/server/PythonAsyncProcessorFactory.cpp"
"python/server/flagged/EnableResourcePoolsForPython.cpp"
"python/streaming/PythonUserException.cpp"
"python/streaming/Sink.cpp"
"python/streaming/StreamElementEncoder.cpp"
)

foreach(_src ${BindingFiles})
file(RELATIVE_PATH _filerelpath ${CMAKE_CURRENT_SOURCE_DIR} ${_src})
get_filename_component(_target_file "${_src}" NAME)

message(
STATUS
Expand All @@ -188,6 +210,40 @@ if(thriftpy3)
)
endforeach()


########################################################################
# Symlink files that get mapped server -> server_impl
file(GLOB BindingFiles
"python/server/async_processor.pxd"
"python/server/event_handler.pxd"
"python/server/interceptor/server_module.pxd"
"python/server/python_async_processor.pxd"
"python/server/request_context.pxd"

"python/server/python_async_processor.pyx"

"python/server/PythonAsyncProcessor.cpp"
"python/server/PythonAsyncProcessorFactory.cpp"
)
foreach(_src ${BindingFiles})
file(RELATIVE_PATH _filerelpath ${CMAKE_CURRENT_SOURCE_DIR}/python/server ${_src})

message(
STATUS
"Linking ${_src} to ${_cybld}/thrift/python/server_impl/${_filerelpath}"
)

add_custom_command(
TARGET
create_binding_symlink_python
PRE_BUILD
COMMAND
${CMAKE_COMMAND} -E create_symlink "${_src}" "${_cybld}/thrift/python/server_impl/${_filerelpath}"
)
endforeach()

########################################################################
# Symlink all the py3 files
add_custom_target(create_binding_symlink_py3 ALL)
file(GLOB BindingFiles
"${CMAKE_CURRENT_SOURCE_DIR}/py3/*.pxd"
Expand Down Expand Up @@ -223,16 +279,28 @@ if(thriftpy3)
thriftcpp2
WORKING_DIRECTORY ${_cybld}
)

set(_folly_site_packages "${CMAKE_INSTALL_PREFIX}/../folly/lib/python${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}/site-packages")
# FIXME: For some reason, FOLLY_VERSION is not defined
set(_folly_version "0.0.1")
set(_folly_PYTHONPATH "${_folly_site_packages}/folly-${_folly_version}-py${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}-linux-x86_64.egg")

add_custom_command(TARGET thrift_python_types_bindings
COMMAND ${CMAKE_COMMAND} -E env
"CFLAGS=${CMAKE_C_FLAGS}"
"CXXFLAGS=${CMAKE_CXX_FLAGS}"
"CFLAGS=${CMAKE_C_FLAGS} ${FBTHRIFT_CXX_OPTIONS}"
"CXXFLAGS=${CMAKE_CXX_FLAGS} ${FBTHRIFT_CXX_OPTIONS}"
"CXX=${CMAKE_CXX_COMPILER}"
python3 ${CMAKE_CURRENT_SOURCE_DIR}/setup.py -v --api-only
"PYTHONPATH=${_cybld}/lib:${_cybld}:${_folly_PYTHONPATH}:$ENV{PYTHONPATH}"
${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/setup.py -v --api-only
BYPRODUCTS
${_cybld}/thrift/python/types_api.h
${_cybld}/thrift/python/_types_api.h
${_cybld}/thrift/python/server/server_api.h
${_cybld}/thrift/python/util_api.h
${_cybld}/thrift/python/server_impl/python_async_processor_api.h
${_cybld}/thrift/python/streaming/sink_api.h
${_cybld}/thrift/python/_stream_api.h
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/setup.py
# FIXME: add .pxd/.pyx files here
WORKING_DIRECTORY ${_cybld}
)

Expand All @@ -243,6 +311,7 @@ if(thriftpy3)
WORKING_DIRECTORY ${_cybld})
file(MAKE_DIRECTORY "${_cybld}/thrift/lib/python/")
file(MAKE_DIRECTORY "${_cybld}/thrift/lib/python/server")
file(MAKE_DIRECTORY "${_cybld}/thrift/lib/python/streaming")
file(MAKE_DIRECTORY "${_cybld}/thrift/lib/py3/")

add_custom_command(
Expand All @@ -267,13 +336,13 @@ if(thriftpy3)
TARGET
create_cython_types_api_symlink
COMMAND
${CMAKE_COMMAND} -E create_symlink "${_cybld}/thrift/python/_util_api.h" "${_cybld}/thrift/lib/python/util_api.h"
${CMAKE_COMMAND} -E create_symlink "${_cybld}/thrift/python/server_impl/python_async_processor_api.h" "${_cybld}/thrift/lib/python/server/python_async_processor_api.h"
)
add_custom_command(
TARGET
create_cython_types_api_symlink
COMMAND
${CMAKE_COMMAND} -E create_symlink "${_cybld}/thrift/python/_util.h" "${_cybld}/thrift/lib/python/_util.h"
${CMAKE_COMMAND} -E create_symlink "${_cybld}/thrift/python/streaming/sink_api.h" "${_cybld}/thrift/lib/python/streaming/sink_api.h"
)
add_custom_command(
TARGET
Expand All @@ -292,8 +361,9 @@ if(thriftpy3)
python/client/OmniClient.cpp
python/client/RequestChannel.cpp
python/client/ssl.cpp
python/streaming/PythonUserException.cpp
python/streaming/StreamElementEncoder.cpp
python/types.cpp
python/util.cpp
python/Serializer.cpp
py3/stream.cpp
)
Expand All @@ -302,22 +372,34 @@ if(thriftpy3)
target_compile_definitions(thrift_python_cpp PRIVATE BOOST_NO_AUTO_PTR)
target_compile_definitions(thrift_python_cpp PRIVATE THRIFT_NO_HTTP_CLIENT_CHANNEL)
target_include_directories(thrift_python_cpp PRIVATE "${_cybld}")
target_include_directories(thrift_python_cpp PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
target_include_directories(thrift_python_cpp PRIVATE Folly::folly_python_cpp)
target_link_libraries(
thrift_python_cpp
PUBLIC
thriftcpp2
Folly::folly
Folly::folly_python_cpp
PRIVATE
Python3::Python
)
install(
TARGETS thrift_python_cpp
EXPORT thrift
EXPORT fbthrift-exports
# FIXME: Consider DESTINATION ${LIB_INSTALL_DIR}
)
install(
TARGETS thrift_python_cpp
DESTINATION ${py_install_dir}
)

wrap_non_fb_python_library(thrift_python_cpp thrift)
install(
TARGETS thrift_python_cpp.py_lib
EXPORT fbthrift-exports
DESTINATION ${LIB_INSTALL_DIR}
)

#####
# Now build everything else in lib/python, including all remaining Cython
# modules that depend on types_api.h and thrift_python_cpp.
Expand All @@ -328,8 +410,10 @@ if(thriftpy3)
generate_apache_thrift_metadata_python
create_binding_symlink_python
create_binding_symlink_py3
Folly::folly_python_cpp
thriftcpp2
thrift_python_cpp
thrift_python_types_bindings
WORKING_DIRECTORY ${_cybld}
)

Expand All @@ -353,29 +437,38 @@ if(thriftpy3)

add_custom_command(TARGET thrift_python_and_py3_bindings
COMMAND ${CMAKE_COMMAND} -E env
"CFLAGS=${CMAKE_C_FLAGS}"
"CXXFLAGS=${CMAKE_CXX_FLAGS}"
"CFLAGS=${CMAKE_C_FLAGS} ${FBTHRIFT_CXX_OPTIONS}"
"CXXFLAGS=${CMAKE_CXX_FLAGS} ${FBTHRIFT_CXX_OPTIONS}"
"CXX=${CMAKE_CXX_COMPILER}"
"PYTHONPATH=${_cybld}/lib:${_cybld}:${_folly_PYTHONPATH}:$ENV{PYTHONPATH}"
python3 ${CMAKE_CURRENT_SOURCE_DIR}/setup.py -v
build_ext -f ${incs} ${libs} --libpython ${python_libname}
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/setup.py
WORKING_DIRECTORY ${_cybld}
)
add_custom_command(TARGET thrift_python_and_py3_bindings
COMMAND ${CMAKE_COMMAND} -E env
"CFLAGS=${CMAKE_C_FLAGS}"
"CXXFLAGS=${CMAKE_CXX_FLAGS}"
"CFLAGS=${CMAKE_C_FLAGS} ${FBTHRIFT_CXX_OPTIONS}"
"CXXFLAGS=${CMAKE_CXX_FLAGS} ${FBTHRIFT_CXX_OPTIONS}"
"CXX=${CMAKE_CXX_COMPILER}"
"PYTHONPATH=${_cybld}/lib:${_cybld}:${_folly_PYTHONPATH}:$ENV{PYTHONPATH}"
python3 ${CMAKE_CURRENT_SOURCE_DIR}/setup.py -v
bdist_wheel --libpython ${python_libname}
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/setup.py
WORKING_DIRECTORY ${_cybld}
)
add_custom_command(TARGET thrift_python_and_py3_bindings POST_BUILD
COMMAND ${CMAKE_COMMAND} -E env
"CFLAGS=${CMAKE_C_FLAGS}"
"CXXFLAGS=${CMAKE_CXX_FLAGS}"
"CFLAGS=${CMAKE_C_FLAGS} ${FBTHRIFT_CXX_OPTIONS}"
"CXXFLAGS=${CMAKE_CXX_FLAGS} ${FBTHRIFT_CXX_OPTIONS}"
"CXX=${CMAKE_CXX_COMPILER}"
"PYTHONPATH=${_cybld}/lib:${_cybld}:${_folly_PYTHONPATH}:$ENV{PYTHONPATH}"
python3 ${CMAKE_CURRENT_SOURCE_DIR}/setup.py -v
install --prefix ${CMAKE_INSTALL_PREFIX} --libpython ${python_libname}
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/setup.py
WORKING_DIRECTORY ${_cybld}
)

Expand All @@ -386,8 +479,8 @@ if(thriftpy3)
install(CODE "
execute_process(
COMMAND ${CMAKE_COMMAND} -E env
\"CFLAGS=${CMAKE_C_FLAGS}\"
\"CXXFLAGS=${CMAKE_CXX_FLAGS}\"
\"CFLAGS=${CMAKE_C_FLAGS} ${FBTHRIFT_CXX_OPTIONS}\"
\"CXXFLAGS=${CMAKE_CXX_FLAGS} ${FBTHRIFT_CXX_OPTIONS}\"
python3 ${CMAKE_CURRENT_SOURCE_DIR}/setup.py install -v
--prefix ${py_install_dir} --libpython ${python_libname}
COMMAND_ECHO STDOUT
Expand Down
Loading
Loading