Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build executor Cython extension #992

Closed
Closed
27 changes: 27 additions & 0 deletions CMake/FindCython.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
find_program(_cython
NAMES cython cython3)
unset(CYTHON_FOUND)
if (_cython)
execute_process(COMMAND ${_cython} --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}")
set(CYTHON_FOUND True)
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_FOUND
VERSION_VAR CYTHON_VERSION_STRING)

5 changes: 5 additions & 0 deletions CMake/folly-deps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ list(APPEND FOLLY_INCLUDE_DIRECTORIES ${LIBAIO_INCLUDE_DIRS})
list(APPEND FOLLY_LINK_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 (${PYTHON_EXTENSIONS})

set(FOLLY_USE_SYMBOLIZER OFF)
CHECK_INCLUDE_FILE_CXX(elf.h FOLLY_HAVE_ELF_H)
find_library(UNWIND_LIBRARIES NAMES unwind)
Expand Down
20 changes: 20 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ list(REMOVE_ITEM files
${FOLLY_DIR}/experimental/io/HugePageUtil.cpp
${FOLLY_DIR}/python/fibers.cpp
${FOLLY_DIR}/python/GILAwareManualExecutor.cpp
${FOLLY_DIR}/cybld/folly/executor.cpp
)
list(REMOVE_ITEM hfiles
${FOLLY_DIR}/python/fibers.h
Expand Down Expand Up @@ -218,6 +219,25 @@ target_compile_definitions(folly_base
$<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 (${PYTHON_EXTENSIONS})

add_library(folly
$<TARGET_OBJECTS:folly_base>
)
Expand Down
37 changes: 37 additions & 0 deletions folly/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,40 @@ install(
)

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 "
calebmarchent marked this conversation as resolved.
Show resolved Hide resolved
string(REGEX REPLACE \"^(..*)$\" \"--root=\\\\1\" PYROOT \"\$ENV{DESTDIR}\")
execute_process(COMMAND
python3 ${CMAKE_CURRENT_SOURCE_DIR}/setup.py install \${PYROOT}
WORKING_DIRECTORY ${_cybld})")
endif (${PYTHON_EXTENSIONS})
Empty file added folly/python/__init__.py
Empty file.
2 changes: 2 additions & 0 deletions folly/python/executor.pxd
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# distutils: language = c++

from libcpp.memory cimport unique_ptr
from folly cimport cFollyExecutor

Expand Down
25 changes: 25 additions & 0 deletions folly/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/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
import os

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,}))