Commit 2bacf890 authored by Caleb Marchent's avatar Caleb Marchent Committed by Facebook Github Bot

Build executor Cython extension (#992)

Summary:
setup.py to create folly Cython bindings

Minimum required to get fbthrift to compile in OSS, so only includes executor

Cython requires that source files (pyx and pxd) are in a directory matching the extension name, to provide this a tree of symbolic links is created and the Cython compile happens in that directory.

  - Python3 > 3.6 available?
  - Cython installed?
Pull Request resolved: https://github.com/facebook/folly/pull/992

Reviewed By: simpkins

Differential Revision: D13716102

Pulled By: calebmarchent

fbshipit-source-id: 5fb285b0b43e8b6d1774fa4b6f2525c327cbcc7e
parent 96f51113
# Find Cython
#
# This module sets the following variables:
# - Cython_FOUND
# - CYTHON_EXE
# - CYTHON_VERSION_STRING
#
find_program(CYTHON_EXE
NAMES cython cython3)
if (CYTHON_EXE)
execute_process(COMMAND ${CYTHON_EXE} --version
RESULT_VARIABLE _cython_retcode
OUTPUT_VARIABLE _cython_output
ERROR_VARIABLE _cython_output
OUTPUT_STRIP_TRAILING_WHITESPACE)
if (${_cython_retcode} EQUAL 0)
separate_arguments(_cython_output)
list(GET _cython_output -1 CYTHON_VERSION_STRING)
message(STATUS "Found Cython Version ${CYTHON_VERSION_STRING}")
else ()
message(STATUS "Failed to get Cython version")
endif ()
else ()
message(STATUS "Cython not found")
endif ()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
Cython
REQUIRED_VARS CYTHON_EXE CYTHON_VERSION_STRING
VERSION_VAR CYTHON_VERSION_STRING
)
...@@ -131,6 +131,11 @@ list(APPEND FOLLY_INCLUDE_DIRECTORIES ${LIBAIO_INCLUDE_DIRS}) ...@@ -131,6 +131,11 @@ list(APPEND FOLLY_INCLUDE_DIRECTORIES ${LIBAIO_INCLUDE_DIRS})
list(APPEND FOLLY_LINK_LIBRARIES ${CMAKE_DL_LIBS}) list(APPEND FOLLY_LINK_LIBRARIES ${CMAKE_DL_LIBS})
list(APPEND CMAKE_REQUIRED_LIBRARIES ${CMAKE_DL_LIBS}) list(APPEND CMAKE_REQUIRED_LIBRARIES ${CMAKE_DL_LIBS})
if (PYTHON_EXTENSIONS)
find_package(PythonInterp 3.6 REQUIRED)
find_package(Cython 0.26 REQUIRED)
endif ()
set(FOLLY_USE_SYMBOLIZER OFF) set(FOLLY_USE_SYMBOLIZER OFF)
CHECK_INCLUDE_FILE_CXX(elf.h FOLLY_HAVE_ELF_H) CHECK_INCLUDE_FILE_CXX(elf.h FOLLY_HAVE_ELF_H)
find_library(UNWIND_LIBRARIES NAMES unwind) find_library(UNWIND_LIBRARIES NAMES unwind)
......
...@@ -117,6 +117,7 @@ list(REMOVE_ITEM files ...@@ -117,6 +117,7 @@ list(REMOVE_ITEM files
${FOLLY_DIR}/experimental/io/HugePageUtil.cpp ${FOLLY_DIR}/experimental/io/HugePageUtil.cpp
${FOLLY_DIR}/python/fibers.cpp ${FOLLY_DIR}/python/fibers.cpp
${FOLLY_DIR}/python/GILAwareManualExecutor.cpp ${FOLLY_DIR}/python/GILAwareManualExecutor.cpp
${FOLLY_DIR}/cybld/folly/executor.cpp
) )
list(REMOVE_ITEM hfiles list(REMOVE_ITEM hfiles
${FOLLY_DIR}/python/fibers.h ${FOLLY_DIR}/python/fibers.h
...@@ -218,6 +219,25 @@ target_compile_definitions(folly_base ...@@ -218,6 +219,25 @@ target_compile_definitions(folly_base
$<TARGET_PROPERTY:folly_deps,INTERFACE_COMPILE_DEFINITIONS> $<TARGET_PROPERTY:folly_deps,INTERFACE_COMPILE_DEFINITIONS>
) )
option(PYTHON_EXTENSIONS "Build Python Bindings for Folly, requires Cython" OFF)
if (PYTHON_EXTENSIONS)
# Compile folly such that it can be linked in to a shared library
# 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)
endif ()
add_library(folly add_library(folly
$<TARGET_OBJECTS:folly_base> $<TARGET_OBJECTS:folly_base>
) )
......
...@@ -13,3 +13,50 @@ install( ...@@ -13,3 +13,50 @@ install(
) )
add_subdirectory(experimental/exception_tracer) add_subdirectory(experimental/exception_tracer)
if (PYTHON_EXTENSIONS)
# Create tree of symbolic links in structure required for successful
# compliation by Cython.
# - must be in path named same as extension
set(_cybld "${CMAKE_CURRENT_BINARY_DIR}/cybld")
file(MAKE_DIRECTORY "${_cybld}/folly")
foreach(_src
"executor.pxd"
"executor.pyx"
"futures.pxd"
"__init__.pxd"
"AsyncioExecutor.h")
message(
STATUS
"Linking ${CMAKE_CURRENT_SOURCE_DIR}/python/${_src} "
"to ${_cybld}/folly/${_src}"
)
add_custom_target("create_folly_link_${_src}" ALL
COMMAND
${CMAKE_COMMAND} -E create_symlink
${CMAKE_CURRENT_SOURCE_DIR}/python/${_src}
"${_cybld}/folly/${_src}"
)
endforeach()
# Tell setup.py where to find includes and libfolly_pic.a
set(prop "$<TARGET_PROPERTY:folly_base,INCLUDE_DIRECTORIES>")
set(incs "$<$<BOOL:${prop}>:-I$<JOIN:${prop},:>>")
set(libs "-L${CMAKE_BINARY_DIR}")
add_custom_target(folly_python_bindings ALL
COMMAND
python3 ${CMAKE_CURRENT_SOURCE_DIR}/setup.py
build_ext -f ${incs} ${libs}
DEPENDS folly_pic
WORKING_DIRECTORY ${_cybld})
# Install Folly Python Bindings
install(CODE "
string(REGEX REPLACE \"^(..*)$\" \"--root=\\\\1\" PYROOT \"\$ENV{DESTDIR}\")
execute_process(COMMAND
python3 ${CMAKE_CURRENT_SOURCE_DIR}/setup.py install \${PYROOT}
WORKING_DIRECTORY ${_cybld})")
endif ()
# distutils: language = c++
from libcpp.memory cimport unique_ptr from libcpp.memory cimport unique_ptr
from folly cimport cFollyExecutor from folly cimport cFollyExecutor
......
#!/usr/bin/env python3
# Do not call directly, use cmake
#
# Cython requires source files in a specific structure, the structure is
# created as tree of links to the real source files.
from setuptools import setup, Extension
from Cython.Build import cythonize
from Cython.Compiler import Options
Options.fast_fail = True
ext = Extension("folly.executor",
sources=['folly/executor.pyx'],
libraries=['folly_pic', 'glog', 'double-conversion', 'iberty'])
setup(name="folly",
version='0.0.1',
packages=['folly'],
package_data={"": ['*.pxd', '*.h']},
zip_safe=False,
ext_modules=cythonize([ext],
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