Commit b1211e4c authored by Ahmed Soliman's avatar Ahmed Soliman Committed by Facebook Github Bot

Fixing fbthrift and folly cython bindings build

Summary:
This diff addresses a collection of issues with our thrift-py3 build story in open source.
- Cython 0.29 bug https://github.com/cython/cython/issues/2985 prevents us from loading the generated binary python extensions. I've tested 0.28.6 and it works perfectly.
- Adds ssl to the extensions (since we need SSLContext in get_client, we must have access to the module)
- No need to special case folly_pic. There is really no easy way to get the python bindings to work without using shared library libfolly.so. So, In our build (logdevice) we will build folly as a shared library, this also reduces the complexity everywhere.
- We also need iobuf as extension for thrift to work, added that to setup.py
- Refactored some of the CMake code that had hardcoded file names to use globbing to future-proof this as much as possible.
- Moved `python/lang/cast.pxd` to match the python module name `folly.cast`, so now it lives in `python/cast.pxd`. This simplifies the globbing as well.
- Moved `setup.py` to live in the `/python` directory.
- Added `*.py` in setup.py to pickup any raw python code (including `__init__.py`)

Reviewed By: yfeldblum

Differential Revision: D18685009

fbshipit-source-id: 749b30942a3e5e9e314b5248cc0aa90c6ac1581f
parent 9657632b
...@@ -343,25 +343,10 @@ target_compile_definitions(folly_base ...@@ -343,25 +343,10 @@ target_compile_definitions(folly_base
set(FOLLY_INSTALL_TARGETS folly folly_deps) set(FOLLY_INSTALL_TARGETS folly folly_deps)
option(PYTHON_EXTENSIONS "Build Python Bindings for Folly, requires Cython" OFF) option(PYTHON_EXTENSIONS
if (PYTHON_EXTENSIONS) "Build Python Bindings for Folly, requires Cython and (BUILD_SHARED_LIBS=ON)"
# Compile folly such that it can be linked in to a shared library OFF
# required for linking to python extensions )
add_library(folly_pic
$<TARGET_PROPERTY:folly_base,SOURCES>
)
apply_folly_compile_options_to_target(folly_pic)
target_include_directories(folly_pic
PUBLIC
$<TARGET_PROPERTY:folly_deps,INTERFACE_INCLUDE_DIRECTORIES>
)
target_compile_definitions(folly_pic
PUBLIC
$<TARGET_PROPERTY:folly_deps,INTERFACE_COMPILE_DEFINITIONS>
)
set_target_properties(folly_pic PROPERTIES POSITION_INDEPENDENT_CODE True)
list(APPEND FOLLY_INSTALL_TARGETS folly_pic)
endif ()
add_library(folly add_library(folly
$<TARGET_OBJECTS:folly_base> $<TARGET_OBJECTS:folly_base>
......
...@@ -35,53 +35,47 @@ if (PYTHON_EXTENSIONS) ...@@ -35,53 +35,47 @@ if (PYTHON_EXTENSIONS)
set(_cybld "${CMAKE_CURRENT_BINARY_DIR}/cybld") set(_cybld "${CMAKE_CURRENT_BINARY_DIR}/cybld")
file(MAKE_DIRECTORY "${_cybld}/folly") add_custom_target(create_binding_symlink ALL)
foreach(_src file(GLOB BindingFiles
"executor.pxd" "${CMAKE_CURRENT_SOURCE_DIR}/python/*.pyx"
"executor.pyx" "${CMAKE_CURRENT_SOURCE_DIR}/python/*.pxd"
"futures.pxd" "${CMAKE_CURRENT_SOURCE_DIR}/python/*.h"
"range.pxd" "${CMAKE_CURRENT_SOURCE_DIR}/python/*.cpp"
"iobuf.pxd"
"iobuf.pyx"
"optional.pyx"
"__init__.pxd"
"AsyncioExecutor.h")
message(
STATUS
"Linking ${CMAKE_CURRENT_SOURCE_DIR}/python/lang/cast.pxd "
"to ${_cybld}/folly/cast.pxd"
)
add_custom_target("create_folly_link_cast.pxd" ALL
COMMAND
${CMAKE_COMMAND} -E create_symlink
${CMAKE_CURRENT_SOURCE_DIR}/python/lang/cast.pxd
"${_cybld}/folly/cast.pxd"
) )
file(MAKE_DIRECTORY "${_cybld}/folly/")
foreach(_src ${BindingFiles})
get_filename_component(_target_file "${_src}" NAME)
message( message(
STATUS STATUS
"Linking ${CMAKE_CURRENT_SOURCE_DIR}/python/${_src} " "Linking ${_src} "
"to ${_cybld}/folly/${_src}" "to ${_cybld}/folly/${_target_file}"
) )
add_custom_target("create_folly_link_${_src}" ALL add_custom_command(TARGET create_binding_symlink PRE_BUILD
COMMAND COMMAND
${CMAKE_COMMAND} -E create_symlink ${CMAKE_COMMAND} -E create_symlink
${CMAKE_CURRENT_SOURCE_DIR}/python/${_src} "${_src}"
"${_cybld}/folly/${_src}" "${_cybld}/folly/${_target_file}"
) )
endforeach() endforeach()
# Tell setup.py where to find includes and libfolly_pic.a # Tell setup.py where to find includes and libfolly.so
set(prop "$<TARGET_PROPERTY:folly_base,INCLUDE_DIRECTORIES>") set(prop "$<TARGET_PROPERTY:folly_base,INCLUDE_DIRECTORIES>")
set(incs "$<$<BOOL:${prop}>:-I$<JOIN:${prop},:>>") set(incs "$<$<BOOL:${prop}>:-I$<JOIN:${prop},:>>")
set(libs "-L${CMAKE_BINARY_DIR}") set(libs "-L${CMAKE_BINARY_DIR}")
add_custom_target(folly_python_bindings ALL add_custom_target(folly_python_bindings ALL
DEPENDS folly create_binding_symlink
WORKING_DIRECTORY ${_cybld})
add_custom_command(TARGET folly_python_bindings POST_BUILD
COMMAND COMMAND
python3 ${CMAKE_CURRENT_SOURCE_DIR}/setup.py python3 ${CMAKE_CURRENT_SOURCE_DIR}/python/setup.py
build_ext -f ${incs} ${libs} build_ext -f ${incs} ${libs}
BYPRODUCTS ${_cybld}/folly/executor_api.h BYPRODUCTS ${_cybld}/folly/executor_api.h
DEPENDS folly_pic WORKING_DIRECTORY ${_cybld}
WORKING_DIRECTORY ${_cybld}) )
install( install(
FILES ${_cybld}/folly/executor_api.h FILES ${_cybld}/folly/executor_api.h
...@@ -91,8 +85,7 @@ if (PYTHON_EXTENSIONS) ...@@ -91,8 +85,7 @@ if (PYTHON_EXTENSIONS)
# Install Folly Python Bindings # Install Folly Python Bindings
install(CODE " install(CODE "
string(REGEX REPLACE \"^(..*)$\" \"--root=\\\\1\" PYROOT \"\$ENV{DESTDIR}\")
execute_process(COMMAND execute_process(COMMAND
python3 ${CMAKE_CURRENT_SOURCE_DIR}/setup.py install \${PYROOT} python3 ${CMAKE_CURRENT_SOURCE_DIR}/python/setup.py install
WORKING_DIRECTORY ${_cybld})") WORKING_DIRECTORY ${_cybld})")
endif () endif ()
...@@ -25,17 +25,25 @@ from setuptools import Extension, setup ...@@ -25,17 +25,25 @@ from setuptools import Extension, setup
Options.fast_fail = True Options.fast_fail = True
ext = Extension( exts = [
Extension(
"folly.executor", "folly.executor",
sources=["folly/executor.pyx", "folly/iobuf.pyx"], sources=["folly/executor.pyx"],
libraries=["folly_pic", "glog", "double-conversion", "iberty"], libraries=["folly", "glog", "double-conversion", "iberty"],
) ),
Extension(
"folly.iobuf",
sources=["folly/iobuf.pyx"],
libraries=["folly", "glog", "double-conversion", "iberty"],
),
]
setup( setup(
name="folly", name="folly",
version="0.0.1", version="0.0.1",
packages=["folly"], packages=["folly"],
package_data={"": ["*.pxd", "*.h"]}, package_data={"": ["*.pxd", "*.h"]},
setup_requires=["cython"],
zip_safe=False, zip_safe=False,
ext_modules=cythonize([ext], compiler_directives={"language_level": 3}), ext_modules=cythonize(exts, compiler_directives={"language_level": 3}),
) )
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment