Skip to content

Commit

Permalink
building platform tagged wheels
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedh committed Mar 8, 2024
1 parent 9a494c6 commit 9d5b97a
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "OpenCTM"]
path = upstream/OpenCTM
url = https://github.com/Danny02/OpenCTM.git
File renamed without changes.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# openctm
100 changes: 100 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""
build-ctypes
For a ctypes binding it is compiled on a specific platform
but does not need to be recompiled for every version of Python.
`cibuildwheel` isn't set up for this necessarily so we will
do some level of manual wangling here.
# Steps
- build the wheel with `pip wheel`
- build the library for the current platform using subprocess
- inject the library into the wheel
- fix the wheel tags to be py3
"""
import os
import zipfile
import tempfile
import platform
import subprocess

cwd = os.path.abspath(os.path.dirname(__file__))
ctm_lib = os.path.join(cwd, 'upstream', 'OpenCTM', 'lib')

def build_manylinux(image: str):
"""
Build OpenCTM on a manylinux image.
"""

# remove any prior built artifacts from the library
subprocess.check_call(['git', 'clean', '-xdf'],
cwd=ctm_lib)

# compose a docker command to run
command = ['docker', 'run',
'-v', f'{ctm_lib}:/ctmlib',
'-w', '/ctmlib',
image,
'make -f Makefile.linux']
print(' '.join(command))

# build in the manylinux image
# not sure why this doesn't work as a non-shell command
subprocess.check_call(' '.join(command), shell=True)

libname = 'libopenctm.so'
with open(os.path.join(ctm_lib, libname), 'rb') as f:
return {libname: f.read()}




def to_wheel(libs: dict):

with tempfile.TemporaryDirectory() as tmp:
subprocess.check_call(['pip', 'wheel', '.', f'--wheel-dir={tmp}'])

# there should only be one file in this temp directory
wheel_name = os.listdir(tmp)[-1]
file_name = os.path.join(tmp, wheel_name)
assert file_name.endswith('.whl')

# append the requested libraries to the zip archive
with zipfile.ZipFile(file_name, 'a') as zipped:
for lib, raw in libs.items():
zipped.writestr(f'openctm./{lib}', raw)

with open(file_name, 'rb') as f:
return {wheel_name: f.read()}

def main():

wheelhouse = os.path.join(cwd, 'wheelhouse')
os.makedirs(wheelhouse, exist_ok=True)

if platform.system() == 'Linux':
# build on specific manylinux tag images
manylinux = ['quay.io/pypa/manylinux2014_x86_64',
'quay.io/pypa/manylinux2014_i686']
for image in manylinux:
tag_platform = image.rsplit('/', 1)[-1]
wheel = to_wheel(build_manylinux(image))
tag_none = next(iter(wheel.keys()))
# keep the `none` ABI
# replace the `any` platform with manylinux
tag_new = tag_none.replace('any', tag_platform)

with open(os.path.join(wheelhouse, tag_new), 'wb') as f:
f.write(wheel[tag_none])

elif platform.system() == 'Windows':
build_windows()


if __name__ == '__main__':

main()

58 changes: 58 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[build-system]
build-backend = "setuptools.build_meta"
requires = ["setuptools >= 61.0", "wheel"]

[project]
name = "openctm"
version = "0.0.1"
requires-python = ">=3.7"
authors = [{name = "Michael Dawson-Haggerty", email = "[email protected]"}]
license = {file = "LICENSE.md"}
description = "Provide a loader for OpenCTM files"
keywords = ["graphics", "OpenCTM"]
classifiers = [
"Programming Language :: Python",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Natural Language :: English",
"Topic :: Scientific/Engineering",
"Topic :: Multimedia :: Graphics",
"Topic :: Multimedia :: Graphics :: 3D Modeling"
]
urls = {Homepage = "https://github.com/trimesh/cascadio"}

[project.readme]
file = "README.md"
content-type = "text/markdown"

[tool.setuptools]
packages = [
"openctm",
]

[project.optional-dependencies]
tests = ["pytest", "trimesh"]

[tool.cibuildwheel]
# `yum` would need to be replaced in the `before-all`
# script before the musl linux can build OpenCASCADE
skip = "*musl*"

[tool.cibuildwheel.linux]
# build OCCT on linux from source on the manylinux image
before-all = [
'cd OpenCTM',
'git clean -xdf',
'cd lib',
'make -f Makefile.linux'
]

test-extras = "tests"
test-command = "pytest {project}/tests"

# point auditwheel at the OCCT libraries
repair-wheel-command = "LD_LIBRARY_PATH=/project/OpenCTM/lib auditwheel repair --lib-sdir . -w {dest_dir} {wheel}"
1 change: 1 addition & 0 deletions upstream/OpenCTM
Submodule OpenCTM added at 91b3b7

0 comments on commit 9d5b97a

Please sign in to comment.