Skip to content

Commit

Permalink
refactor(setup.py): refactor build system
Browse files Browse the repository at this point in the history
  • Loading branch information
XuehaiPan committed Apr 21, 2024
1 parent 9f4f568 commit 47f20b7
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import contextlib
import os
import pathlib
import platform
import re
import shutil
import sys
import sysconfig
from importlib.util import module_from_spec, spec_from_file_location

from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext


HERE = pathlib.Path(__file__).absolute().parent
VERSION_FILE = HERE / 'optree' / 'version.py'

sys.path.insert(0, str(VERSION_FILE.parent))
import version # noqa


class CMakeExtension(Extension):
Expand Down Expand Up @@ -72,39 +70,56 @@ def build_extension(self, ext):

build_args.extend(['--target', ext.target, '--'])

cwd = os.getcwd()
try:
os.chdir(build_temp)
self.spawn([cmake, ext.source_dir, *cmake_args])
if not self.dry_run:
self.spawn([cmake, '--build', '.', *build_args])
finally:
os.chdir(HERE)
os.chdir(cwd)


@contextlib.contextmanager
def vcs_version(name, path):
path = pathlib.Path(path).absolute()
assert path.is_file()
module_spec = spec_from_file_location(name=name, location=path)
assert module_spec is not None
assert module_spec.loader is not None
module = module_from_spec(module_spec)
module_spec.loader.exec_module(module)

VERSION_CONTENT = None
if module.__release__:
yield module
return

try:
if not version.__release__:
content = None
try:
try:
VERSION_CONTENT = VERSION_FILE.read_text(encoding='utf-8')
VERSION_FILE.write_text(
content = path.read_text(encoding='utf-8')
path.write_text(
data=re.sub(
r"""__version__\s*=\s*('[^']+'|"[^"]+")""",
f'__version__ = {version.__version__!r}',
string=VERSION_CONTENT,
f'__version__ = {module.__version__!r}',
string=content,
),
encoding='utf-8',
)
except OSError:
VERSION_CONTENT = None
content = None

yield module
finally:
if content is not None:
with path.open(mode='wt', encoding='utf-8', newline='') as file:
file.write(content)


with vcs_version(name='optree.version', path=HERE.joinpath('optree', 'version.py')) as version:
setup(
name='optree',
version=version.__version__,
cmdclass={'build_ext': cmake_build_ext},
ext_modules=[CMakeExtension('optree._C', source_dir=HERE)],
)
finally:
if VERSION_CONTENT is not None:
with VERSION_FILE.open(mode='wt', encoding='utf-8', newline='') as file:
file.write(VERSION_CONTENT)

0 comments on commit 47f20b7

Please sign in to comment.