Build cython extensions in common setup.py (#281)

* build packer and parser with same setup

* fix build

* remove packer setup

* extra compile args
This commit is contained in:
Adeeb Shihadeh 2020-07-08 14:05:16 -07:00 committed by GitHub
parent 3cfb5c7778
commit 2265c9c3dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 88 deletions

2
.gitignore vendored
View File

@ -12,4 +12,6 @@ can/build/
can/obj/
can/packer_pyx.cpp
can/parser_pyx.cpp
can/packer_pyx.html
can/parser_pyx.html
can/packer_impl.cpp

View File

@ -16,12 +16,8 @@ for x in sorted(os.listdir('../')):
libdbc = env.SharedLibrary('libdbc', ["dbc.cc", "parser.cc", "packer.cc", "common.cc"]+dbcs, LIBS=["capnp", "kj"])
# packer
env.Command(['packer_pyx.so'],
[libdbc, 'packer_pyx.pyx', 'packer_pyx_setup.py'],
"cd opendbc/can && python3 packer_pyx_setup.py build_ext --inplace")
# Build packer and parser
# parser
env.Command(['parser_pyx.so'],
[libdbc, cereal, 'parser_pyx_setup.py', 'parser_pyx.pyx', 'common.pxd'],
"cd opendbc/can && python3 parser_pyx_setup.py build_ext --inplace")
env.Command(['packer_pyx.so', 'parser_pyx.so'],
[libdbc, cereal, 'common_pyx_setup.py', 'packer_pyx.pyx', 'parser_pyx.pyx', 'common.pxd'],
"cd opendbc/can && python3 common_pyx_setup.py build_ext --inplace")

View File

@ -1,12 +1,14 @@
import os
import sysconfig
import subprocess
import sysconfig
import platform
from distutils.core import Extension, setup # pylint: disable=import-error,no-name-in-module
from Cython.Build import cythonize
from Cython.Distutils import build_ext
ANNOTATE = os.getenv('ANNOTATE') is not None
BASEDIR = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../"))
@ -32,10 +34,8 @@ class BuildExtWithoutPlatformSuffix(build_ext):
return get_ext_filename_without_platform_suffix(filename)
sourcefiles = ['packer_pyx.pyx']
extra_compile_args = ["-std=c++14"]
ARCH = subprocess.check_output(["uname", "-m"], encoding='utf8').rstrip() # pylint: disable=unexpected-keyword-arg
if ARCH == "aarch64":
extra_compile_args += ["-Wno-deprecated-register"]
@ -44,22 +44,48 @@ if platform.system() == "Darwin":
else:
libdbc = "libdbc.so"
extra_link_args = [os.path.join(BASEDIR, 'opendbc', 'can', libdbc)]
include_dirs = [
BASEDIR,
os.path.join(BASEDIR, 'phonelibs'),
]
# Build CAN Parser
setup(name='CAN parser',
cmdclass={'build_ext': BuildExtWithoutPlatformSuffix},
ext_modules=cythonize(
Extension(
"parser_pyx",
language="c++",
sources=['parser_pyx.pyx'],
extra_compile_args=extra_compile_args,
include_dirs=include_dirs,
extra_link_args=extra_link_args,
),
annotate=ANNOTATE
),
nthreads=4,
)
if platform.system() == "Darwin":
os.system("install_name_tool -change opendbc/can/libdbc.dylib " + BASEDIR + "/opendbc/can/libdbc.dylib parser_pyx.so")
# Build CAN Packer
setup(name='CAN packer',
cmdclass={'build_ext': BuildExtWithoutPlatformSuffix},
ext_modules=cythonize(
Extension(
"packer_pyx",
language="c++",
sources=sourcefiles,
sources=['packer_pyx.pyx'],
extra_compile_args=extra_compile_args,
include_dirs=[
BASEDIR,
os.path.join(BASEDIR, 'phonelibs'),
],
extra_link_args=[
os.path.join(BASEDIR, 'opendbc', 'can', libdbc),
],
)
include_dirs=include_dirs,
extra_link_args=extra_link_args,
),
annotate=ANNOTATE
),
nthreads=4,
)

View File

@ -1,68 +0,0 @@
import os
import subprocess
import sysconfig
import platform
from distutils.core import Extension, setup # pylint: disable=import-error,no-name-in-module
from Cython.Build import cythonize
from Cython.Distutils import build_ext
BASEDIR = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../"))
def get_ext_filename_without_platform_suffix(filename):
name, ext = os.path.splitext(filename)
ext_suffix = sysconfig.get_config_var('EXT_SUFFIX')
if ext_suffix == ext:
return filename
ext_suffix = ext_suffix.replace(ext, '')
idx = name.find(ext_suffix)
if idx == -1:
return filename
else:
return name[:idx] + ext
class BuildExtWithoutPlatformSuffix(build_ext):
def get_ext_filename(self, ext_name):
filename = super().get_ext_filename(ext_name)
return get_ext_filename_without_platform_suffix(filename)
sourcefiles = ['parser_pyx.pyx']
extra_compile_args = ["-std=c++14"]
ARCH = subprocess.check_output(["uname", "-m"], encoding='utf8').rstrip() # pylint: disable=unexpected-keyword-arg
if ARCH == "aarch64":
extra_compile_args += ["-Wno-deprecated-register"]
if platform.system() == "Darwin":
libdbc = "libdbc.dylib"
else:
libdbc = "libdbc.so"
setup(name='CAN parser',
cmdclass={'build_ext': BuildExtWithoutPlatformSuffix},
ext_modules=cythonize(
Extension(
"parser_pyx",
language="c++",
sources=sourcefiles,
extra_compile_args=extra_compile_args,
include_dirs=[
BASEDIR,
os.path.join(BASEDIR, 'phonelibs'),
],
extra_link_args=[
os.path.join(BASEDIR, 'opendbc', 'can', libdbc),
],
)
),
nthreads=4,
)
if platform.system() == "Darwin":
os.system("install_name_tool -change opendbc/can/libdbc.dylib " + BASEDIR + "/opendbc/can/libdbc.dylib parser_pyx.so")