Skip to content

Commit 360e017

Browse files
bnavigatorisaevil
andauthored
Python bindings: Replace direct setup.py usage with PEP517 (pip) (#1941)
Co-authored-by: Ilya Isaev <ilya.isaev@intel.com>
1 parent 05387d3 commit 360e017

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

python/CMakeLists.txt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Copyright (c) 2020-2023 Intel Corporation
2+
# Copyright (c) 2026 UXL Foundation Contributors
23
#
34
# Licensed under the Apache License, Version 2.0 (the "License");
45
# you may not use this file except in compliance with the License.
@@ -16,18 +17,21 @@ find_package(PythonInterp 3.5 REQUIRED)
1617

1718
set(PYTHON_BUILD_WORK_DIR python_build)
1819

20+
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in ${CMAKE_CURRENT_SOURCE_DIR}/setup.py)
21+
1922
add_custom_target(
2023
python_copy
2124
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/tbb ${PYTHON_BUILD_WORK_DIR}/tbb
2225
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/TBB.py ${PYTHON_BUILD_WORK_DIR}
2326
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/setup.py ${PYTHON_BUILD_WORK_DIR}
24-
)
27+
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/pyproject.toml ${PYTHON_BUILD_WORK_DIR}
28+
)
2529

2630
# Python build requires path to TBB headers
2731
get_target_property(TBB_INCLUDES tbb INTERFACE_INCLUDE_DIRECTORIES)
2832
foreach(dir ${TBB_INCLUDES})
2933
if (${dir} MATCHES "<BUILD_INTERFACE:" OR TBB_FOUND)
30-
set(TBB4PY_INCLUDE_STRING "-I${dir} ${TBB4PY_INCLUDE_STRING}")
34+
set(TBB4PY_INCLUDE_STRING "${dir} ${TBB4PY_INCLUDE_STRING}")
3135
endif()
3236
endforeach()
3337
string(STRIP "${TBB4PY_INCLUDE_STRING}" TBB4PY_INCLUDE_STRING)
@@ -37,10 +41,13 @@ add_custom_target(
3741
ALL
3842
DEPENDS tbb python_copy
3943
COMMAND
40-
${PYTHON_EXECUTABLE} ${PYTHON_BUILD_WORK_DIR}/setup.py
41-
build -b${PYTHON_BUILD_WORK_DIR}
42-
build_ext ${TBB4PY_INCLUDE_STRING} -L$<TARGET_FILE_DIR:TBB::tbb>
43-
install --prefix build -f
44+
${CMAKE_COMMAND} -E env
45+
TBB_INCLUDEDIRS="${TBB4PY_INCLUDE_STRING}"
46+
TBB_LIBDIRS=$<TARGET_FILE_DIR:TBB::tbb>
47+
${PYTHON_EXECUTABLE} -m pip install
48+
--no-build-isolation
49+
--prefix build
50+
./${PYTHON_BUILD_WORK_DIR}
4451
COMMENT "Build and install to work directory the oneTBB Python module"
4552
)
4653

@@ -50,7 +57,7 @@ add_test(NAME python_test
5057
-DPYTHON_MODULE_BUILD_PATH=${PYTHON_BUILD_WORK_DIR}/build
5158
-P ${PROJECT_SOURCE_DIR}/cmake/python/test_launcher.cmake)
5259

53-
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${PYTHON_BUILD_WORK_DIR}/build/
60+
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/build/
5461
DESTINATION .
5562
COMPONENT tbb4py)
5663

python/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ For more information and examples, please refer to [forum discussion](https://co
2020
- **tbb** - The folder contains Python module sources.
2121

2222
## Files
23-
- **setup.py** - Standard Python setup script.
23+
- **setup.py** - Standard Python setuptools script. Calling it directly is deprecated since setuptools version 58.3.0.
2424
- **TBB.py** - Alternative entry point for Python module.
2525

2626
## CMake predefined targets
@@ -33,8 +33,7 @@ For more information and examples, please refer to [forum discussion](https://co
3333
- `pydoc tbb` - Read built-in documentation for Python interfaces.
3434
- `python3 -m tbb your_script.py` - Run your_script.py in context of `with tbb.Monkey():` when oneTBB is enabled. By default only multi-threading will be covered.
3535
- `python3 -m tbb --ipc your_script.py` - Run your_script.py in context of `with tbb.Monkey():` when oneTBB enabled in both multi-threading and multi-processing modes.
36-
- `python3 setup.py build -b<output_directory_path> -f check` - Build oneTBB module for Python. (Prerequisites: built and sourced oneTBB and IRML libraries)
37-
- `python3 setup.py build -b<output_directory_path> build_ext -I<path_to_tbb_includes> -L<path_to_prebuilt_libraries> install -f <additional_flags> ` - Build and install oneTBB module for Python. (Prerequisites: built oneTBB and IRML libraries)
36+
- `TBB_INCLUDEDIRS=<path_to_tbb_includes> TBB_LIBDIRS=<path_to_prebuilt_libraries> python3 -m pip install --no-build-isolation --prefix <output_directory_path>` - Build and install oneTBB module for Python. (Prerequisites: built oneTBB and IRML libraries)
3837
- `python3 -m TBB test` - run test for oneTBB module for Python.
3938
- `python3 -m tbb test` - run test for oneTBB module for Python.
4039

python/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["setuptools>=61"]
3+
build-backend = "setuptools.build_meta"

python/setup.py renamed to python/setup.py.in

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Copyright (c) 2016-2023 Intel Corporation
2+
# Copyright (c) 2026 UXL Foundation Contributors
23
#
34
# Licensed under the Apache License, Version 2.0 (the "License");
45
# you may not use this file except in compliance with the License.
@@ -17,8 +18,12 @@
1718
import platform
1819
import os
1920

20-
from distutils.core import *
21-
from distutils.command.build import build
21+
try:
22+
from setuptools import setup, Extension
23+
from setuptools.command.build import build
24+
except ImportError:
25+
from distutils.core import setup, Extension
26+
from distutils.command.build import build
2227

2328
rundir = os.getcwd()
2429
os.chdir(os.path.abspath(os.path.dirname(__file__)))
@@ -57,19 +62,31 @@
5762
compile_flags = ['-std=c++11', '-Wno-unused-variable']
5863
tbb_lib_name = 'tbb'
5964

65+
tbb_includedirs = os.getenv("TBB_INCLUDEDIRS", "")
66+
tbb_libdirs = os.getenv("TBB_LIBDIRS", "")
67+
68+
if use_compiler_tbb and (tbb_includedirs or tbb_libdirs):
69+
print("Warning: Ignoring TBB_INCLUDEDIRS=%s and TBB_LIBDIRS=%s "
70+
"because using oneTBB from Intel (R) C++ Compiler is set"
71+
% (tbb_includedirs, tbb_libdirs))
72+
6073
_tbb = Extension("tbb._api", ["tbb/api.i"],
61-
include_dirs=[os.path.join(tbb_root, 'include')] if not use_compiler_tbb else [],
74+
include_dirs=([os.path.join(tbb_root, 'include')]
75+
+ tbb_includedirs.split()
76+
) if not use_compiler_tbb else [],
6277
swig_opts =['-c++', '-O', '-threads'] + ( # add '-builtin' later
6378
['-I' + os.path.join(tbb_root, 'include')] if not use_compiler_tbb else []),
6479
extra_compile_args=compile_flags + tbb_flag,
6580
extra_link_args=tbb_flag,
6681
libraries =([tbb_lib_name] if not use_compiler_tbb else []) +
6782
(['irml'] if platform.system() == "Linux" else []),
68-
library_dirs=[ rundir, # for custom-builds
83+
library_dirs=([rundir, # for custom-builds
6984
os.path.join(tbb_root, 'lib', 'intel64', 'gcc4.8'), # for Linux
7085
os.path.join(tbb_root, 'lib'), # for MacOS
7186
os.path.join(tbb_root, 'lib', 'intel64', 'vc_mt'), # for Windows
72-
] if not use_compiler_tbb else [],
87+
]
88+
+ tbb_libdirs.split()
89+
) if not use_compiler_tbb else [],
7390
language ='c++',
7491
)
7592

@@ -89,7 +106,7 @@ class TBBBuild(build):
89106
author ="Intel Corporation",
90107
author_email="inteltbbdevelopers@intel.com",
91108
license ="Dual license: Apache or Proprietary",
92-
version ="0.2",
109+
version ="${CMAKE_PROJECT_VERSION}",
93110
classifiers =[
94111
'Development Status :: 4 - Beta',
95112
'Environment :: Console',
@@ -98,7 +115,6 @@ class TBBBuild(build):
98115
'Intended Audience :: System Administrators',
99116
'Intended Audience :: Other Audience',
100117
'Intended Audience :: Science/Research',
101-
'License :: OSI Approved :: Apache Software License',
102118
'Operating System :: MacOS :: MacOS X',
103119
'Operating System :: Microsoft :: Windows',
104120
'Operating System :: POSIX :: Linux',

0 commit comments

Comments
 (0)