Skip to content

Instantly share code, notes, and snippets.

@fsantini
Created October 31, 2024 14:44
Show Gist options
  • Save fsantini/4002b4a1599a0f8eec30d1256edeb36b to your computer and use it in GitHub Desktop.
Save fsantini/4002b4a1599a0f8eec30d1256edeb36b to your computer and use it in GitHub Desktop.
SimpleElastix Python Build Mac
#!/bin/bash
PYTHON_VER=$1
if [ "$PYTHON_VER" = "" ]
then
echo "Please specify python version"
exit -1
fi
PYTHON_MAJOR_VER=`expr "$PYTHON_VER" : '\(3\.[0-9][0-9]*\)'`
echo $PYTHON_MAJOR_VER
PYTHON_BASE_DIR="$HOME/.pyenv/versions/$PYTHON_VER"
PYTHON_INCLUDE="$PYTHON_BASE_DIR/include/python$PYTHON_MAJOR_VER"
PYTHON_BIN="$PYTHON_BASE_DIR/bin/python3"
PIP_BIN="$PYTHON_BASE_DIR/bin/pip"
PYTHON_LIB="$PYTHON_BASE_DIR/lib/libpython$PYTHON_MAJOR_VER.dylib"
SELX_DIR="$HOME/SimpleITK"
SELX_HELP_DIR="$HOME/selx_helper"
mkdir -p $SELX_DIR/build
cd $SELX_DIR/build
rm -rf SimpleITK-build/Wrapping/Python/SimpleITK
cmake ../SuperBuild
cmake -DBUILD_EXAMPLES:BOOL=OFF \
-DBUILD_TESTING:BOOL=OFF \
-DWRAP_CSHARP:BOOL=OFF \
-DWRAP_JAVA:BOOL=OFF \
-DWRAP_LUA:BOOL=OFF \
-DWRAP_R:BOOL=OFF \
-DWRAP_RUBY:BOOL=OFF \
-DWRAP_TCL:BOOL=OFF \
-DWRAP_PYTHON:BOOL=ON \
-DPython_EXECUTABLE:STRING=$PYTHON_BIN \
-DPython_INCLUDE_DIR:STRING=$PYTHON_INCLUDE \
-DSimpleITK_USE_ELASTIX=ON .
make -j4
cp $SELX_HELP_DIR/setup.py SimpleITK-build/Wrapping/Python/
cd SimpleITK-build/Wrapping/Python/
$PIP_BIN install wheel setuptools
$PYTHON_BIN setup.py bdist_wheel
import sys
import os
try:
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext as _build_ext
except ImportError:
from distutils.core import setup, Extension
from distutils.command.build_ext import build_ext as _build_ext
import re
import os.path as osp
from SimpleITK._version import __version__
def to_package_relpath(filename, pkg_name='SimpleITK'):
return osp.relpath(filename, osp.join(os.curdir, pkg_name))
doc_files = [to_package_relpath(f) for f in [r'/Users/francesco/SimpleITK/build/SimpleITK-build/Wrapping/Python/SimpleITK/docs/LICENSE',r'/Users/francesco/SimpleElastix/build/SimpleITK-build/Wrapping/Python/SimpleITK/docs/NOTICE',r'/Users/francesco/SimpleElastix/build/SimpleITK-build/Wrapping/Python/SimpleITK/docs/README.md',r'/Users/francesco/SimpleElastix/build/SimpleITK-build/Wrapping/Python/SimpleITK/docs/ITK-5.2-NOTICE',r'/Users/francesco/SimpleElastix/build/SimpleITK-build/Wrapping/Python/SimpleITK/docs/ITK-5.2-README.md']]
with open('/Users/francesco/selx_helper/Readme_unofficial.md', encoding='utf-8') as f:
long_description = f.read()
class build_ext(_build_ext):
""" Override standard command class to build an extension, to
simply copy an existing compiled library into the packaging
directory structure.
"""
def build_extension(self, ext):
"""
"""
from distutils.errors import DistutilsSetupError
sources = ext.sources
if sources is None or not len(sources) == 1:
raise DistutilsSetupError( "Expected only one compiled library." )
expected_ext_filename = os.path.split(self.get_ext_filename(ext.name))[1]
ext_file = self.get_ext_fullpath(ext.name)
abs_sources = list( map(os.path.abspath, sources) )
self.copy_file(abs_sources[0], ext_file)
version = __version__
version=version[:version.find('+')]
setup(
name = 'SimpleITK-SimpleElastix',
version = version,
author = 'Insight Software Consortium',
author_email = '[email protected]',
ext_modules=[Extension('SimpleITK._SimpleITK', [r'SimpleITK/_SimpleITK.abi3.so'])],
packages= ['SimpleITK'],
package_data = {"SimpleITK": doc_files},
platforms = [],
description = r'SimpleITK is a simplified interface to the Insight Toolkit (ITK) for image registration and segmentation',
long_description = long_description,
long_description_content_type='text/markdown',
classifiers=[
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python",
"Programming Language :: C++",
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Education",
"Intended Audience :: Healthcare Industry",
"Intended Audience :: Science/Research",
"Topic :: Scientific/Engineering",
"Topic :: Scientific/Engineering :: Medical Science Apps.",
"Topic :: Scientific/Engineering :: Information Analysis",
"Topic :: Software Development :: Libraries",
"Operating System :: Microsoft :: Windows",
"Operating System :: POSIX",
"Operating System :: Unix",
"Operating System :: MacOS"
],
license='Apache',
keywords = 'SimpleITK ITK InsightToolkit segmentation registration',
url = r'http://simpleitk.org/',
project_urls={
"Bug Tracker": "https://github.com/SimpleITK/SimpleITK/issues",
"Documentation": "https://simpleitk.readthedocs.io/en/release/",
"Source Code": "https://github.com/SimpleITK/SimpleITK",
},
cmdclass={'build_ext':build_ext}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment