Improve python_wheel CMake target (#2688)

This commit is contained in:
Michał Karzyński
2020-10-18 17:12:25 +02:00
committed by GitHub
parent 2b5ed2e9eb
commit cc2bfcf1d7
3 changed files with 45 additions and 105 deletions

View File

@@ -23,37 +23,23 @@ include(ExternalProject)
ExternalProject_Add(
pybind11
GIT_REPOSITORY "https://github.com/pybind/pybind11.git"
GIT_TAG "v2.4.3"
GIT_TAG "v2.5.0"
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/pybind11"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
)
if (WIN32)
set(BUILD_PY_IN "${CMAKE_CURRENT_SOURCE_DIR}/build_wheel.py.in")
set(BUILD_PY "${CMAKE_CURRENT_BINARY_DIR}/build_wheel.py")
configure_file(${BUILD_PY_IN} ${BUILD_PY} @ONLY)
set(BUILD_PY_IN "${CMAKE_CURRENT_SOURCE_DIR}/build_wheel.py.in")
set(BUILD_PY "${CMAKE_CURRENT_BINARY_DIR}/build_wheel.py")
configure_file(${BUILD_PY_IN} ${BUILD_PY} @ONLY)
add_custom_command(
DEPENDS pybind11
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/dist/
POST_BUILD
WORKING_DIR ${CMAKE_CURRENT_BINARY_DIR}
COMMAND python ${BUILD_PY}
)
else()
set(BUILD_SH_IN "${CMAKE_CURRENT_SOURCE_DIR}/build_wheel.sh.in")
set(BUILD_SH "${CMAKE_CURRENT_BINARY_DIR}/build_wheel.sh")
configure_file(${BUILD_SH_IN} ${BUILD_SH} @ONLY)
add_custom_command(
DEPENDS pybind11
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/dist/
POST_BUILD
WORKING_DIR ${CMAKE_CURRENT_BINARY_DIR}
COMMAND ${CMAKE_BUILD_TOOL} -C ../ DESTDIR=python/_install install && bash build_wheel.sh
)
endif()
add_custom_command(
DEPENDS pybind11
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/dist/
POST_BUILD
WORKING_DIR ${CMAKE_CURRENT_BINARY_DIR}
COMMAND python ${BUILD_PY}
)
add_custom_target(python_wheel DEPENDS ngraph ${CMAKE_CURRENT_BINARY_DIR}/dist/)

View File

@@ -17,57 +17,65 @@
import os
import subprocess
import sys
import venv
print('Building ngraph wheel for Python {}'.format(sys.version_info.major))
print("Building ngraph wheel for Python {}".format(sys.version_info.major))
PYBIND_HEADERS_PATH="@CMAKE_CURRENT_BINARY_DIR@/pybind11"
NGRAPH_CPP_BUILD_PATH="@CMAKE_INSTALL_PREFIX@/@NGRAPH_COMPONENT_PREFIX@"
NGRAPH_ONNX_IMPORT_ENABLE="@NGRAPH_ONNX_IMPORT_ENABLE@"
NGRAPH_VERSION="@NGRAPH_WHEEL_VERSION@"
PYTHON_API_SOURCE_DIR="@CMAKE_CURRENT_SOURCE_DIR@"
BUILD_DIR="@CMAKE_CURRENT_BINARY_DIR@"
PYBIND_HEADERS_PATH = "@CMAKE_CURRENT_BINARY_DIR@/pybind11"
NGRAPH_CPP_BUILD_PATH = "@CMAKE_INSTALL_PREFIX@/@NGRAPH_COMPONENT_PREFIX@"
NGRAPH_ONNX_IMPORT_ENABLE = "@NGRAPH_ONNX_IMPORT_ENABLE@"
NGRAPH_VERSION = "@NGRAPH_WHEEL_VERSION@"
PYTHON_API_SOURCE_DIR = "@CMAKE_CURRENT_SOURCE_DIR@"
BUILD_DIR = "@CMAKE_CURRENT_BINARY_DIR@"
BUILD_DEPS = ['setuptools', 'wheel']
BUILD_DEPS = ["setuptools", "wheel", "pip"]
try:
venv_dir = os.path.join(os.path.curdir, 'whl_build_venv')
venv_dir = os.path.join(os.path.curdir, "whl_build_venv")
print("Creating a virtualenv to build the wheel in: ", os.path.abspath(venv_dir))
subprocess.check_call(['virtualenv', venv_dir])
venv.create(venv_dir, with_pip=True)
venv_activator = os.path.abspath(os.path.join(venv_dir, "Scripts", "activate.bat"))
print("Activating the virtualenv...")
os.startfile(venv_activator)
venv_python = (
os.path.abspath(os.path.join(venv_dir, "Scripts", "python"))
if os.name == "nt"
else os.path.abspath(os.path.join(venv_dir, "bin", "python"))
)
print("Installing build dependencies...")
pip_install_cmd = ['pip', 'install', '-U']
pip_install_cmd = [venv_python, "-m", "pip", "install", "-U"]
pip_install_cmd.extend(BUILD_DEPS)
subprocess.check_call(pip_install_cmd)
build_env_variables = {"PYBIND_HEADERS_PATH": PYBIND_HEADERS_PATH,
"NGRAPH_CPP_BUILD_PATH": NGRAPH_CPP_BUILD_PATH,
"NGRAPH_ONNX_IMPORT_ENABLE": NGRAPH_ONNX_IMPORT_ENABLE,
"NGRAPH_VERSION": NGRAPH_VERSION}
build_env_variables = {
"PYBIND_HEADERS_PATH": PYBIND_HEADERS_PATH,
"NGRAPH_CPP_BUILD_PATH": NGRAPH_CPP_BUILD_PATH,
"NGRAPH_ONNX_IMPORT_ENABLE": NGRAPH_ONNX_IMPORT_ENABLE,
"NGRAPH_VERSION": NGRAPH_VERSION,
}
env = os.environ
env.update(build_env_variables)
print("Running setup.py bdist_wheel")
build_log = subprocess.Popen(['python', os.path.join(PYTHON_API_SOURCE_DIR, 'setup.py'), 'bdist_wheel'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env, universal_newlines=True)
build_log = subprocess.Popen(
[venv_python, os.path.join(PYTHON_API_SOURCE_DIR, "setup.py"), "bdist_wheel"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env, universal_newlines=True,
)
for line in build_log.stdout:
sys.stdout.write(line)
print("Running setup.py sdist")
subprocess.check_call(['python', os.path.join(PYTHON_API_SOURCE_DIR, 'setup.py'), 'sdist'])
subprocess.check_call([venv_python, os.path.join(PYTHON_API_SOURCE_DIR, "setup.py"), "sdist"])
output_dir = os.path.join(PYTHON_API_SOURCE_DIR, "dist")
print("\n>>> NOTE: nGraph Python packages created in ", output_dir)
print("\n".join(os.listdir(output_dir)))
print("Deactivating the temporary build virtualenv")
venv_deactivator = os.path.abspath(os.path.join(venv_dir, "Scripts", "deactivate.bat"))
os.startfile(venv_deactivator)
except subprocess.CalledProcessError as err:
print("Could not complete the wheel building process")
print("Command that failed: ", err.cmd)
if err.stdout is not None:
print("Command std output: ", err.stdout.decode('utf-8'))
print("Command std output: ", err.stdout.decode("utf-8"))
if err.stderr is not None:
print("Command err output: ", err.stderr.decode('utf-8'))
print("Command err output: ", err.stderr.decode("utf-8"))
sys.exit(1)

View File

@@ -1,54 +0,0 @@
#!/usr/bin/env bash
# ******************************************************************************
# Copyright 2017-2020 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ******************************************************************************
set -e
export PYBIND_HEADERS_PATH=@CMAKE_CURRENT_BINARY_DIR@/pybind11
export NGRAPH_CPP_BUILD_PATH=@CMAKE_CURRENT_BINARY_DIR@/_install/@CMAKE_INSTALL_PREFIX@/@NGRAPH_COMPONENT_PREFIX@
export NGRAPH_ONNX_IMPORT_ENABLE=@NGRAPH_ONNX_IMPORT_ENABLE@
export NGRAPH_VERSION=@NGRAPH_WHEEL_VERSION@
SOURCE_DIR=@CMAKE_CURRENT_SOURCE_DIR@
BUILD_DIR=@CMAKE_CURRENT_BINARY_DIR@
! PYTHON2_DETECTED=$(($(python -c 'import sys; print(sys.version_info.major)' 2> /dev/null) == 2))
! PYTHON3_DETECTED=$(($(python3 -c 'import sys; print(sys.version_info.major)' 2> /dev/null) == 3))
mkdir -p build
if [ "${PYTHON2_DETECTED}" == 1 ]; then
echo "Building wheel for Python 2"
python --version
cd ${BUILD_DIR}
virtualenv -p "$(command -v python)" build/venv2
source build/venv2/bin/activate
pip install --upgrade setuptools pip wheel
python ${SOURCE_DIR}/setup.py bdist_wheel
deactivate
fi
if [ "${PYTHON3_DETECTED}" == 1 ]; then
echo "Building wheel for Python 3"
python3 --version
cd ${BUILD_DIR}
virtualenv -p "$(command -v python3)" build/venv3
source build/venv3/bin/activate
pip install --upgrade setuptools pip wheel
python ${SOURCE_DIR}/setup.py bdist_wheel
python ${SOURCE_DIR}/setup.py sdist
deactivate
fi