2020-10-20 22:53:14 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
###########################################################################
|
|
|
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
# Licensed under the MIT License. See License.txt in the project root for
|
|
|
|
# license information.
|
|
|
|
###########################################################################
|
|
|
|
|
2021-01-12 00:44:17 +03:00
|
|
|
from setuptools.command.build_ext import build_ext as _build_ext
|
|
|
|
from setuptools.command.develop import develop as _develop
|
|
|
|
from setuptools.command.build_py import build_py as _build_py
|
|
|
|
from contextlib import contextmanager
|
2021-04-13 00:27:10 +03:00
|
|
|
from setuptools import setup, find_packages
|
2021-01-12 00:44:17 +03:00
|
|
|
|
2020-10-20 22:53:14 +03:00
|
|
|
import os
|
2021-01-12 00:44:17 +03:00
|
|
|
import sys
|
|
|
|
import setuptools
|
|
|
|
import pathlib
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
TOP_DIR = os.path.dirname(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def chdir(path):
|
|
|
|
orig_path = os.getcwd()
|
|
|
|
os.chdir(str(path))
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
os.chdir(orig_path)
|
|
|
|
|
|
|
|
|
|
|
|
def load_msvcvar():
|
|
|
|
if os.environ.get('vcvars'):
|
|
|
|
stdout, _ = subprocess.Popen([
|
|
|
|
'cmd', '/q', '/c', '(%vcvars% & set)'],
|
|
|
|
stdout=subprocess.PIPE, shell=True, universal_newlines=True).communicate()
|
|
|
|
for line in stdout.splitlines():
|
|
|
|
kv_pair = line.split('=')
|
|
|
|
if len(kv_pair) == 2:
|
|
|
|
os.environ[kv_pair[0]] = kv_pair[1]
|
|
|
|
else:
|
|
|
|
import shutil
|
|
|
|
if shutil.which('cmake') is None:
|
|
|
|
raise SystemExit(
|
|
|
|
"Cannot find cmake in the executable path, " +
|
|
|
|
"please install one or specify the environement variable VCVARS to the path of VS vcvars64.bat.")
|
|
|
|
|
|
|
|
|
|
|
|
class BuildCMakeExt(_build_ext):
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""
|
|
|
|
Perform build_cmake before doing the 'normal' stuff
|
|
|
|
"""
|
|
|
|
for extension in self.extensions:
|
|
|
|
if extension.name == 'onnxruntime_customops._ortcustomops':
|
|
|
|
self.build_cmake(extension)
|
|
|
|
|
|
|
|
def build_cmake(self, extension):
|
|
|
|
project_dir = pathlib.Path().absolute()
|
|
|
|
build_temp = pathlib.Path(self.build_temp)
|
|
|
|
build_temp.mkdir(parents=True, exist_ok=True)
|
|
|
|
ext_fullpath = pathlib.Path(self.get_ext_fullpath(extension.name))
|
|
|
|
|
|
|
|
config = 'RelWithDebInfo' if self.debug else 'Release'
|
|
|
|
cmake_args = [
|
|
|
|
'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + str(ext_fullpath.parent.absolute()),
|
2021-02-03 22:17:35 +03:00
|
|
|
'-DOCOS_ENABLE_PYTHON=ON',
|
2021-01-12 00:44:17 +03:00
|
|
|
'-DOCOS_EXTENTION_NAME=' + pathlib.Path(self.get_ext_filename(extension.name)).name,
|
|
|
|
'-DCMAKE_BUILD_TYPE=' + config
|
|
|
|
]
|
2021-04-13 00:27:10 +03:00
|
|
|
# Uses to overwrite
|
|
|
|
# export Python3_INCLUDE_DIRS=/opt/python/cp38-cp38
|
|
|
|
# export Python3_LIBRARIES=/opt/python/cp38-cp38
|
|
|
|
for env in ['Python3_INCLUDE_DIRS', 'Python3_LIBRARIES']:
|
|
|
|
if env in os.environ:
|
|
|
|
cmake_args.append("-D%s=%s" % (env, os.environ[env]))
|
2021-01-12 00:44:17 +03:00
|
|
|
|
|
|
|
if self.debug:
|
|
|
|
cmake_args += ['-DCC_OPTIMIZE=OFF']
|
2020-10-20 22:53:14 +03:00
|
|
|
|
2021-01-12 00:44:17 +03:00
|
|
|
build_args = [
|
|
|
|
'--config', config,
|
|
|
|
'--parallel'
|
|
|
|
]
|
|
|
|
|
|
|
|
with chdir(build_temp):
|
|
|
|
self.spawn(['cmake', str(project_dir)] + cmake_args)
|
|
|
|
if not self.dry_run:
|
|
|
|
self.spawn(['cmake', '--build', '.'] + build_args)
|
|
|
|
|
|
|
|
if sys.platform == "win32":
|
2021-01-28 01:55:50 +03:00
|
|
|
self.copy_file(build_temp / config / 'ortcustomops.dll',
|
|
|
|
self.get_ext_filename(extension.name))
|
2021-01-12 00:44:17 +03:00
|
|
|
|
|
|
|
|
|
|
|
class BuildPy(_build_py):
|
|
|
|
def run(self):
|
|
|
|
super().run()
|
|
|
|
|
|
|
|
|
|
|
|
class BuildDevelop(_develop):
|
|
|
|
def run(self):
|
|
|
|
super().run()
|
|
|
|
|
|
|
|
|
|
|
|
def read_requirements():
|
|
|
|
with open(os.path.join(TOP_DIR, "requirements.txt"), "r") as f:
|
|
|
|
requirements = [_ for _ in [_.strip("\r\n ")
|
|
|
|
for _ in f.readlines()] if _ is not None]
|
|
|
|
return requirements
|
2020-10-20 22:53:14 +03:00
|
|
|
|
|
|
|
|
|
|
|
# read version from the package file.
|
2021-01-12 00:44:17 +03:00
|
|
|
def read_version():
|
|
|
|
version_str = '1.0.0'
|
|
|
|
with (open(os.path.join(TOP_DIR, 'onnxruntime_customops/__init__.py'), "r")) as f:
|
|
|
|
line = [_ for _ in [_.strip("\r\n ")
|
|
|
|
for _ in f.readlines()] if _.startswith("__version__")]
|
|
|
|
if len(line) > 0:
|
|
|
|
version_str = line[0].split('=')[1].strip('" ')
|
|
|
|
return version_str
|
|
|
|
|
|
|
|
|
|
|
|
if sys.platform == "win32":
|
|
|
|
load_msvcvar()
|
|
|
|
|
|
|
|
|
|
|
|
ext_modules = [
|
|
|
|
setuptools.extension.Extension(
|
|
|
|
name=str('onnxruntime_customops._ortcustomops'),
|
|
|
|
sources=[])
|
|
|
|
]
|
2020-10-20 22:53:14 +03:00
|
|
|
|
2021-04-13 00:27:10 +03:00
|
|
|
packages = find_packages()
|
|
|
|
package_dir = {k: os.path.join('.', k.replace(".", "/")) for k in packages}
|
|
|
|
package_data = {
|
|
|
|
"onnxruntime_customops": ["*.dll", "*.so", "*.pyd"],
|
|
|
|
}
|
2020-10-20 22:53:14 +03:00
|
|
|
|
|
|
|
setup(
|
2020-12-23 07:16:30 +03:00
|
|
|
name='onnxruntime_customops',
|
2021-01-12 00:44:17 +03:00
|
|
|
version=read_version(),
|
2021-04-13 00:27:10 +03:00
|
|
|
packages=packages,
|
|
|
|
package_dir=package_dir,
|
|
|
|
package_data=package_data,
|
2020-10-20 22:53:14 +03:00
|
|
|
description="ONNXRuntime Custom Operator Library",
|
2021-01-12 00:44:17 +03:00
|
|
|
long_description=open(os.path.join(os.getcwd(), "README.md"), 'r').read(),
|
2020-10-20 22:53:14 +03:00
|
|
|
long_description_content_type='text/markdown',
|
|
|
|
license='MIT License',
|
|
|
|
author='Microsoft Corporation',
|
|
|
|
author_email='onnx@microsoft.com',
|
|
|
|
url='https://github.com/microsoft/ortcustomops',
|
2021-01-12 00:44:17 +03:00
|
|
|
ext_modules=ext_modules,
|
|
|
|
cmdclass=dict(
|
|
|
|
build_ext=BuildCMakeExt,
|
|
|
|
build_py=BuildPy,
|
|
|
|
develop=BuildDevelop
|
|
|
|
),
|
2020-10-20 22:53:14 +03:00
|
|
|
include_package_data=True,
|
2021-01-12 00:44:17 +03:00
|
|
|
install_requires=read_requirements(),
|
2020-10-20 22:53:14 +03:00
|
|
|
classifiers=[
|
2021-04-13 00:27:10 +03:00
|
|
|
'Development Status :: 4 - Beta',
|
2020-10-20 22:53:14 +03:00
|
|
|
'Environment :: Console',
|
|
|
|
'Intended Audience :: Developers',
|
|
|
|
'Operating System :: MacOS :: MacOS X',
|
|
|
|
'Operating System :: Microsoft :: Windows',
|
2021-04-13 00:27:10 +03:00
|
|
|
'Operating System :: POSIX :: Linux',
|
2021-01-12 00:44:17 +03:00
|
|
|
"Programming Language :: C++",
|
2020-10-20 22:53:14 +03:00
|
|
|
'Programming Language :: Python',
|
2021-01-12 00:44:17 +03:00
|
|
|
'Programming Language :: Python :: 3.7',
|
2021-04-13 00:27:10 +03:00
|
|
|
'Programming Language :: Python :: 3.8',
|
|
|
|
'Programming Language :: Python :: 3.9',
|
2021-01-12 00:44:17 +03:00
|
|
|
"Programming Language :: Python :: Implementation :: CPython",
|
|
|
|
'License :: OSI Approved :: MIT License'
|
|
|
|
],
|
2020-10-20 22:53:14 +03:00
|
|
|
)
|