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-04-13 00:27:10 +03:00
|
|
|
from setuptools import setup, find_packages
|
2023-01-24 00:52:18 +03:00
|
|
|
from setuptools.command.build import build as _build
|
2021-09-25 10:40:12 +03:00
|
|
|
from setuptools.command.build_ext import build_ext as _build_ext
|
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
|
|
|
|
|
2021-09-25 10:40:12 +03:00
|
|
|
from textwrap import dedent
|
2021-01-12 00:44:17 +03:00
|
|
|
|
|
|
|
|
2021-09-25 10:40:12 +03:00
|
|
|
TOP_DIR = os.path.dirname(__file__) or os.getcwd()
|
|
|
|
PACKAGE_NAME = 'onnxruntime_extensions'
|
2022-11-29 20:54:49 +03:00
|
|
|
VSINSTALLDIR_NAME = 'VSINSTALLDIR'
|
2021-01-12 00:44:17 +03:00
|
|
|
|
|
|
|
|
2023-02-07 05:23:56 +03:00
|
|
|
def load_vsdevcmd():
|
|
|
|
if os.environ.get(VSINSTALLDIR_NAME) is None:
|
2021-01-12 00:44:17 +03:00
|
|
|
stdout, _ = subprocess.Popen([
|
2023-02-07 05:23:56 +03:00
|
|
|
'powershell', ' -noprofile', '-executionpolicy',
|
2023-03-03 01:12:31 +03:00
|
|
|
'bypass', '-f', TOP_DIR+'/tools/get_vsdevcmd.ps1', '-outputEnv', '1'],
|
2022-11-29 20:54:49 +03:00
|
|
|
stdout=subprocess.PIPE, shell=False, universal_newlines=True).communicate()
|
2021-01-12 00:44:17 +03:00
|
|
|
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, " +
|
2022-05-12 02:51:59 +03:00
|
|
|
"please install one or specify the environment variable VCVARS to the path of VS vcvars64.bat.")
|
2021-01-12 00:44:17 +03:00
|
|
|
|
|
|
|
|
2021-09-29 23:18:36 +03:00
|
|
|
def read_git_refs():
|
|
|
|
release_branch = False
|
2021-09-25 10:40:12 +03:00
|
|
|
stdout, _ = subprocess.Popen(
|
2021-09-29 23:18:36 +03:00
|
|
|
['git'] + ['log', '-1', '--format=%H'],
|
|
|
|
cwd=TOP_DIR,
|
|
|
|
stdout=subprocess.PIPE, universal_newlines=True).communicate()
|
|
|
|
HEAD = dedent(stdout.splitlines()[0]).strip('\n\r')
|
|
|
|
stdout, _ = subprocess.Popen(
|
|
|
|
['git'] + ['show-ref', '--head'],
|
2021-09-25 10:40:12 +03:00
|
|
|
cwd=TOP_DIR,
|
|
|
|
stdout=subprocess.PIPE, universal_newlines=True).communicate()
|
|
|
|
for _ln in stdout.splitlines():
|
|
|
|
_ln = dedent(_ln).strip('\n\r')
|
2021-09-29 23:18:36 +03:00
|
|
|
if _ln.startswith(HEAD):
|
|
|
|
_, _2 = _ln.split(' ')
|
2022-05-12 02:51:59 +03:00
|
|
|
if _2.startswith('refs/remotes/origin/rel-'):
|
2021-09-29 23:18:36 +03:00
|
|
|
release_branch = True
|
|
|
|
return release_branch, HEAD
|
2021-09-25 10:40:12 +03:00
|
|
|
|
|
|
|
|
2021-01-12 00:44:17 +03:00
|
|
|
class BuildCMakeExt(_build_ext):
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""
|
|
|
|
Perform build_cmake before doing the 'normal' stuff
|
|
|
|
"""
|
|
|
|
for extension in self.extensions:
|
2022-10-05 02:22:28 +03:00
|
|
|
if extension.name == 'onnxruntime_extensions._extensions_pydll':
|
2021-01-12 00:44:17 +03:00
|
|
|
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()),
|
2022-10-05 02:22:28 +03:00
|
|
|
'-DOCOS_BUILD_PYTHON=ON',
|
2022-05-12 02:51:59 +03:00
|
|
|
'-DOCOS_EXTENTION_NAME=' + ext_fullpath.name,
|
2021-01-12 00:44:17 +03:00
|
|
|
'-DCMAKE_BUILD_TYPE=' + config
|
|
|
|
]
|
2023-02-07 05:23:56 +03:00
|
|
|
if os.environ.get('OCOS_NO_OPENCV') == '1':
|
|
|
|
# Disabling openCV can drastically reduce the build time.
|
|
|
|
cmake_args += [
|
|
|
|
'-DOCOS_ENABLE_CTEST=OFF',
|
|
|
|
'-DOCOS_ENABLE_OPENCV_CODECS=OFF',
|
|
|
|
'-DOCOS_ENABLE_CV2=OFF',
|
|
|
|
'-DOCOS_ENABLE_VISION=OFF']
|
|
|
|
|
2021-09-29 23:18:36 +03:00
|
|
|
# overwrite the Python module info if the auto-detection doesn't work.
|
2021-04-13 00:27:10 +03:00
|
|
|
# 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
|
|
|
|
2022-05-12 02:51:59 +03:00
|
|
|
# the parallel build has to be limited on some Linux VM machine.
|
|
|
|
cpu_number = os.environ.get('CPU_NUMBER')
|
2021-01-12 00:44:17 +03:00
|
|
|
build_args = [
|
|
|
|
'--config', config,
|
2022-05-12 02:51:59 +03:00
|
|
|
'--parallel' + ('' if cpu_number is None else ' ' + cpu_number)
|
2021-01-12 00:44:17 +03:00
|
|
|
]
|
2022-11-29 20:54:49 +03:00
|
|
|
cmake_exe = 'cmake'
|
2023-02-07 05:23:56 +03:00
|
|
|
# unlike Linux/MacOS, cmake python package on Windows fails to build some 3rd party dependencies.
|
|
|
|
# so we have to use the cmake installed with Visual Studio.
|
2022-11-29 20:54:49 +03:00
|
|
|
if os.environ.get(VSINSTALLDIR_NAME):
|
2023-01-24 00:52:18 +03:00
|
|
|
cmake_exe = os.environ[VSINSTALLDIR_NAME] +\
|
|
|
|
'Common7\\IDE\\CommonExtensions\\Microsoft\\CMake\\CMake\\bin\\cmake.exe'
|
2021-01-12 00:44:17 +03:00
|
|
|
|
2022-11-29 20:54:49 +03:00
|
|
|
self.spawn([cmake_exe, '-S', str(project_dir), '-B', str(build_temp)] + cmake_args)
|
2021-09-25 10:40:12 +03:00
|
|
|
if not self.dry_run:
|
2022-11-29 20:54:49 +03:00
|
|
|
self.spawn([cmake_exe, '--build', str(build_temp)] + build_args)
|
2021-01-12 00:44:17 +03:00
|
|
|
|
|
|
|
if sys.platform == "win32":
|
2022-04-21 02:14:46 +03:00
|
|
|
config_dir = '.'
|
|
|
|
if not (build_temp / 'build.ninja').exists():
|
|
|
|
config_dir = config
|
2022-10-05 02:22:28 +03:00
|
|
|
self.copy_file(build_temp / 'bin' / config_dir / 'extensions_pydll.dll', ext_fullpath)
|
2022-05-12 02:51:59 +03:00
|
|
|
else:
|
|
|
|
self.copy_file(build_temp / 'lib' / ext_fullpath.name, ext_fullpath)
|
2021-09-29 23:18:36 +03:00
|
|
|
|
|
|
|
|
2023-01-24 00:52:18 +03:00
|
|
|
class Build(_build):
|
|
|
|
def finalize_options(self) -> None:
|
|
|
|
# There is a bug in setuptools that prevents the build get the right platform name from arguments.
|
|
|
|
# So, it cannot generate the correct wheel with the right arch in Official release pipeline.
|
|
|
|
# Force plat_name to be 'win-amd64' in Windows to fix that.
|
|
|
|
# Since extensions cmake is only available on x64 for Windows now, it is not a problem to hardcode it.
|
|
|
|
if sys.platform == "win32" and "arm" not in sys.version.lower():
|
|
|
|
self.plat_name = "win-amd64"
|
|
|
|
return super().finalize_options()
|
|
|
|
|
|
|
|
|
2021-01-12 00:44:17 +03:00
|
|
|
def read_requirements():
|
2022-11-29 20:54:49 +03:00
|
|
|
with open(os.path.join(TOP_DIR, "requirements.txt"), "r", encoding="utf-8") as f:
|
2021-09-25 10:40:12 +03:00
|
|
|
requirements = [_ for _ in [dedent(_) for _ in f.readlines()] if _ is not None]
|
2021-01-12 00:44:17 +03:00
|
|
|
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'
|
2023-01-13 01:28:32 +03:00
|
|
|
with (open(os.path.join(TOP_DIR, 'version.txt'), "r")) as f:
|
|
|
|
version_str = f.readline().strip()
|
2021-09-25 10:40:12 +03:00
|
|
|
|
2023-03-03 01:12:31 +03:00
|
|
|
# special handling for Onebranch building
|
|
|
|
if os.getenv('BUILD_SOURCEBRANCHNAME', "").startswith('rel-'):
|
|
|
|
return version_str
|
|
|
|
|
2021-09-29 23:18:36 +03:00
|
|
|
# is it a dev build or release?
|
2023-03-03 01:12:31 +03:00
|
|
|
rel_br, cid = read_git_refs() if os.path.isdir(
|
|
|
|
os.path.join(TOP_DIR, '.git')) else (True, None)
|
|
|
|
|
|
|
|
if rel_br:
|
|
|
|
return version_str
|
|
|
|
|
2022-08-25 00:39:33 +03:00
|
|
|
build_id = os.getenv('BUILD_BUILDID', None)
|
|
|
|
if build_id is not None:
|
|
|
|
version_str += '.{}'.format(build_id)
|
|
|
|
else:
|
2023-03-03 01:12:31 +03:00
|
|
|
version_str += '+' + cid[:7]
|
2021-01-12 00:44:17 +03:00
|
|
|
return version_str
|
|
|
|
|
2023-01-24 00:52:18 +03:00
|
|
|
|
2023-01-13 01:28:32 +03:00
|
|
|
def write_py_version(ortx_version):
|
2023-01-24 00:52:18 +03:00
|
|
|
text = ["# Generated by setup.py, DON'T MANUALLY UPDATE IT!\n",
|
2023-01-13 01:28:32 +03:00
|
|
|
"__version__ = \"{}\"\n".format(ortx_version)]
|
|
|
|
with (open(os.path.join(TOP_DIR, 'onnxruntime_extensions/_version.py'), "w")) as _f:
|
|
|
|
_f.writelines(text)
|
2021-01-12 00:44:17 +03:00
|
|
|
|
2023-01-24 00:52:18 +03:00
|
|
|
|
2021-01-12 00:44:17 +03:00
|
|
|
if sys.platform == "win32":
|
2023-02-07 05:23:56 +03:00
|
|
|
load_vsdevcmd()
|
2021-01-12 00:44:17 +03:00
|
|
|
|
|
|
|
ext_modules = [
|
|
|
|
setuptools.extension.Extension(
|
2022-10-05 02:22:28 +03:00
|
|
|
name=str('onnxruntime_extensions._extensions_pydll'),
|
2021-01-12 00:44:17 +03:00
|
|
|
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 = {
|
2021-09-29 23:18:36 +03:00
|
|
|
"onnxruntime_extensions": ["*.so", "*.pyd"],
|
2021-04-13 00:27:10 +03:00
|
|
|
}
|
2020-10-20 22:53:14 +03:00
|
|
|
|
2021-06-02 00:02:55 +03:00
|
|
|
long_description = ''
|
2022-11-29 20:54:49 +03:00
|
|
|
with open(os.path.join(TOP_DIR, "README.md"), 'r', encoding="utf-8") as _f:
|
2022-05-12 02:51:59 +03:00
|
|
|
long_description += _f.read()
|
2021-06-02 00:02:55 +03:00
|
|
|
start_pos = long_description.find('# Introduction')
|
|
|
|
start_pos = 0 if start_pos < 0 else start_pos
|
|
|
|
end_pos = long_description.find('# Contributing')
|
|
|
|
long_description = long_description[start_pos:end_pos]
|
2023-01-13 01:28:32 +03:00
|
|
|
ortx_version = read_version()
|
|
|
|
write_py_version(ortx_version)
|
2021-06-02 00:02:55 +03:00
|
|
|
|
2020-10-20 22:53:14 +03:00
|
|
|
setup(
|
2021-06-10 19:57:49 +03:00
|
|
|
name=PACKAGE_NAME,
|
2023-01-13 01:28:32 +03:00
|
|
|
version=ortx_version,
|
2021-04-13 00:27:10 +03:00
|
|
|
packages=packages,
|
|
|
|
package_dir=package_dir,
|
|
|
|
package_data=package_data,
|
2021-06-02 00:02:55 +03:00
|
|
|
description="ONNXRuntime Extensions",
|
|
|
|
long_description=long_description,
|
2020-10-20 22:53:14 +03:00
|
|
|
long_description_content_type='text/markdown',
|
|
|
|
license='MIT License',
|
|
|
|
author='Microsoft Corporation',
|
2023-01-24 00:52:18 +03:00
|
|
|
author_email='onnxruntime@microsoft.com',
|
2021-06-02 00:02:55 +03:00
|
|
|
url='https://github.com/microsoft/onnxruntime-extensions',
|
2021-01-12 00:44:17 +03:00
|
|
|
ext_modules=ext_modules,
|
2023-01-24 00:52:18 +03:00
|
|
|
cmdclass=dict(build_ext=BuildCMakeExt, build=Build),
|
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 :: Implementation :: CPython",
|
|
|
|
'License :: OSI Approved :: MIT License'
|
2022-05-12 02:51:59 +03:00
|
|
|
]
|
2020-10-20 22:53:14 +03:00
|
|
|
)
|