Skip to content

Commit

Permalink
Build openvino wheel package from setup.py (#7091)
Browse files Browse the repository at this point in the history
* Added ability to build wheel package by executing `setup.py bdist_wheel`

* fix linter issues

* fix formating

* remove blank line
  • Loading branch information
slyubimt authored Aug 18, 2021
1 parent ed9b4ad commit 96732e3
Showing 1 changed file with 68 additions and 17 deletions.
85 changes: 68 additions & 17 deletions inference-engine/ie_bridges/python/wheel/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import errno
import subprocess # nosec
import typing
import multiprocessing
from fnmatch import fnmatchcase
from pathlib import Path
from shutil import copyfile, rmtree
from distutils.command.install import install
from distutils.command.build import build
from distutils.command.clean import clean
from distutils.errors import DistutilsSetupError
Expand All @@ -27,11 +27,11 @@

# The following variables can be defined in environment or .env file
CMAKE_BUILD_DIR = config('CMAKE_BUILD_DIR', '.')
CORE_LIBS_DIR = config('CORE_LIBS_DIR', '')
PLUGINS_LIBS_DIR = config('PLUGINS_LIBS_DIR', '')
NGRAPH_LIBS_DIR = config('NGRAPH_LIBS_DIR', '')
TBB_LIBS_DIR = config('TBB_LIBS_DIR', '')
PY_PACKAGES_DIR = config('PY_PACKAGES_DIR', '')
CORE_LIBS_DIR = config('CORE_LIBS_DIR', 'deployment_tools/inference_engine/lib/intel64')
PLUGINS_LIBS_DIR = config('PLUGINS_LIBS_DIR', 'deployment_tools/inference_engine/lib/intel64')
NGRAPH_LIBS_DIR = config('NGRAPH_LIBS_DIR', 'deployment_tools/ngraph/lib')
TBB_LIBS_DIR = config('TBB_LIBS_DIR', 'deployment_tools/inference_engine/external/tbb/lib')
PY_PACKAGES_DIR = config('PY_PACKAGES_DIR', f'python/{PYTHON_VERSION}')
LIBS_RPATH = '$ORIGIN' if sys.platform == 'linux' else '@loader_path'

LIB_INSTALL_CFG = {
Expand Down Expand Up @@ -118,7 +118,66 @@ def __init__(self, name, sources, *args, **kwargs):
class CustomBuild(build):
"""Custom implementation of build_clib"""

cmake_build_types = ['Release', 'Debug', 'RelWithDebInfo', 'MinSizeRel']
user_options = [
('config=', None, 'Build configuration [{types}].'.format(types='|'.join(cmake_build_types))),
('jobs=', None, 'Specifies the number of jobs to use with make.'),
('cmake-args=', None, 'Additional options to be passed to CMake.'),
]

def initialize_options(self):
"""Set default values for all the options that this command supports."""
super().initialize_options()
self.build_base = 'build'
self.config = None
self.jobs = None
self.cmake_args = None

def finalize_options(self):
"""Set final values for all the options that this command supports."""
super().finalize_options()

if not self.config:
if self.debug:
self.config = 'Debug'
else:
self.announce('Set default value for CMAKE_BUILD_TYPE = Release.', level=4)
self.config = 'Release'
else:
build_types = [item.lower() for item in self.cmake_build_types]
try:
i = build_types.index(str(self.config).lower())
self.config = self.cmake_build_types[i]
self.debug = True if 'Debug' == self.config else False
except ValueError:
self.announce('Unsupported CMAKE_BUILD_TYPE value: ' + self.config, level=4)
self.announce('Supported values: {types}'.format(types=', '.join(self.cmake_build_types)), level=4)
sys.exit(1)
if self.jobs is None and os.getenv('MAX_JOBS') is not None:
self.jobs = os.getenv('MAX_JOBS')
self.jobs = multiprocessing.cpu_count() if self.jobs is None else int(self.jobs)

def run(self):
global CMAKE_BUILD_DIR
self.jobs = multiprocessing.cpu_count()
plat_specifier = '.{0}-{1}.{2}'.format(self.plat_name, *sys.version_info[:2])
self.build_temp = os.path.join(self.build_base, 'temp' + plat_specifier, self.config)

# if setup.py is directly called use CMake to build product
if CMAKE_BUILD_DIR == '.':
openvino_root_dir = os.path.normpath(os.path.join(CMAKE_BUILD_DIR, '../../../../'))
self.announce('Configuring cmake project', level=3)

self.spawn(['cmake', '-H' + openvino_root_dir, '-B' + self.build_temp,
'-DCMAKE_BUILD_TYPE={type}'.format(type=self.config),
'-DENABLE_PYTHON=ON',
'-DNGRAPH_ONNX_FRONTEND_ENABLE=ON'])

self.announce('Building binaries', level=3)
self.spawn(['cmake', '--build', self.build_temp,
'--config', self.config, '-j', str(self.jobs)])
CMAKE_BUILD_DIR = self.build_temp

self.run_command('build_clib')
build.run(self)
# Copy extra package_data content filtered by find_packages
Expand All @@ -133,14 +192,6 @@ def run(self):
copyfile(path, dst / path_rel)


class CustomInstall(install):
"""Enable build_clib during the installation"""

def run(self):
self.run_command('build_clib')
install.run(self)


class PrepareLibs(build_clib):
"""Prepare prebuilt libraries"""

Expand Down Expand Up @@ -369,21 +420,21 @@ def get_package_dir(install_cfg):
packages = find_namespace_packages(get_package_dir(PY_INSTALL_CFG))
package_data: typing.Dict[str, list] = {}


setup(
version=config('WHEEL_VERSION', '0.0.0'),
author_email=config('WHEEL_AUTHOR_EMAIL', '[email protected]'),
name=config('WHEEL_PACKAGE_NAME', 'openvino'),
license=config('WHEEL_LICENCE_TYPE', 'OSI Approved :: Apache Software License'),
author=config('WHEEL_AUTHOR', 'Intel Corporation'),
description=config('WHEEL_DESC', 'Inference Engine Python* API'),
install_requires=get_dependencies(config('WHEEL_REQUIREMENTS', 'requirements.txt')),
long_description=get_description(config('WHEEL_OVERVIEW', 'pypi_overview.md')),
install_requires=get_dependencies(config('WHEEL_REQUIREMENTS', 'meta/openvino.requirements.txt')),
long_description=get_description(config('WHEEL_OVERVIEW', 'meta/pypi_overview.md')),
long_description_content_type='text/markdown',
download_url=config('WHEEL_DOWNLOAD_URL', 'https://github.com/openvinotoolkit/openvino/tags'),
url=config('WHEEL_URL', 'https://docs.openvinotoolkit.org/latest/index.html'),
cmdclass={
'build': CustomBuild,
'install': CustomInstall,
'build_clib': PrepareLibs,
'build_ext': CopyExt,
'clean': CustomClean,
Expand Down

0 comments on commit 96732e3

Please sign in to comment.