Commit a62e4935 authored by Robert Bradshaw's avatar Robert Bradshaw

Add bazel build support.

Cython is still built and installed with the standard distutils
setup.py mechanisms, but this allows other bazel projects to
reference and use Cython as part of their build process.
parent f7a7bc6b
# Bazel build file for inclusion as external dependency.
#
# Most useful is the pyx_library rule from //Tools:rules.bzl
# which mirrors py_library but compiles .pyx files.
py_library(
name = "cython_lib",
srcs = glob(
["Cython/**/*.py"],
exclude = [
"**/Tests/*.py",
],
) + ["cython.py"],
data = glob([
"Cython/**/*.pyx",
"Cython/Utility/*.*",
"Cython/Includes/**/*.pxd",
]),
visibility = ["//visibility:public"],
)
py_binary(
name = "cythonize",
srcs = ["cythonize.py"],
visibility = ["//visibility:public"],
deps = ["cython_lib"],
)
py_binary(
name = "cython",
srcs = ["cython.py"],
visibility = ["//visibility:public"],
deps = ["cython_lib"],
)
"""
Defines a pyx_library() macros corresponding to py_library.
Uses Cython to compile a .pyx files (and .py files with corresponding .pxd
files) to Python extension modules.
Example:
pyx_library(name = 'mylib',
srcs = ['a.pyx', 'a.pxd', 'b.py', 'pkg/c.pyx'],
py_deps = ['//py_library/dep'],
data = ['//other/data'],
)
Make sure to include the __init__.py files in your srcs list so that Cython
can resolve cimports using the package layout.
"""
def pyx_library(
name,
deps=[],
srcs=[],
cython_directives=[],
cython_options=[]):
# First filter out files that should be run compiled vs. passed through.
py_srcs = []
pyx_srcs = []
pxd_srcs = []
for src in srcs:
if src.endswith('.pyx') or (src.endwith('.py')
and src[:-3] + '.pxd' in srcs):
pyx_srcs.append(src)
elif src.endswith('.py'):
py_srcs.append(src)
else:
pxd_srcs.append(src)
if src.endswith('__init__.py'):
pxd_srcs.append(src)
# Invoke cythonize to produce the shared object libraries.
outs = [src.split('.')[0] + '.so' for src in pyx_srcs]
extra_flags = " ".join(["-X '%s=%s'" % x for x in cython_directives] +
["-s '%s=%s'" % x for x in cython_options])
native.genrule(
name = name + "_cythonize",
srcs = pyx_srcs,
outs = outs,
cmd = "PYTHONHASHSEED=0 $(location @cython//:cythonize) -k -i %s $(SRCS)" % extra_flags
# Rename outputs to expected location.
# TODO(robertwb): Add an option to cythonize itself to do this.
+ """ && python -c 'import os, sys; n = len(sys.argv); [os.rename(src.split(".")[0] + ".so", dst) for src, dst in zip(sys.argv[1:], sys.argv[1+n//2:])]' $(SRCS) $(OUTS)""",
tools = ["@cython//:cythonize"] + pxd_srcs,
)
# Now create a py_library with these shared objects as data.
native.py_library(
name=name,
srcs=py_srcs,
deps=deps,
data=outs + pyx_srcs + pxd_srcs
)
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