|
| 1 | + |
| 2 | +import os |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | +import platform |
| 6 | +import setuptools |
| 7 | +from setuptools import setup, find_packages |
| 8 | +from setuptools.extension import Extension |
| 9 | +import subprocess |
| 10 | +from distutils.command.build import build as build_orig |
| 11 | +import distutils.sysconfig |
| 12 | + |
| 13 | + |
| 14 | +RLE_SRC = os.path.join('rle', 'src', 'rle') |
| 15 | + |
| 16 | + |
| 17 | +# Workaround for needing cython and numpy |
| 18 | +# Solution from: https://stackoverflow.com/a/54128391/12606901 |
| 19 | +class build(build_orig): |
| 20 | + def finalize_options(self): |
| 21 | + super().finalize_options() |
| 22 | + __builtins__.__NUMPY_SETUP__ = False |
| 23 | + |
| 24 | + import numpy |
| 25 | + for ext in self.distribution.ext_modules: |
| 26 | + if ext in extensions: |
| 27 | + ext.include_dirs.append(numpy.get_include()) |
| 28 | + |
| 29 | + |
| 30 | +def get_mscv_args(): |
| 31 | + """Return a list of compiler args for MSVC++'s compiler.""" |
| 32 | + flags = [ |
| 33 | + '/GS', # Buffer security check |
| 34 | + '/W3', # Warning level |
| 35 | + '/wd"4335"', # Ignore warning 4335 |
| 36 | + '/Zc:wchar_t', # Use windows char type |
| 37 | + '/Zc:inline', # Remove unreferenced function or data (...) |
| 38 | + '/Zc:forScope', |
| 39 | + '/Od', # Disable optimisation |
| 40 | + '/Oy-', # (x86 only) don't omit frame pointer |
| 41 | + '/openmp-', # Disable #pragma omp directive |
| 42 | + '/FC', # Display full path of source code files |
| 43 | + '/fp:precise', # Floating-point behaviour |
| 44 | + '/Gd', # (x86 only) use __cdecl calling convention |
| 45 | + '/GF-', # Disable string pooling |
| 46 | + '/GR', # Enable run-time type info |
| 47 | + '/RTC1', # Enable run-time error checking |
| 48 | + '/MT', # Create multithreading executable |
| 49 | + # /D defines constants and macros |
| 50 | + '/D_UNICODE', |
| 51 | + '/DUNICODE', |
| 52 | + ] |
| 53 | + # Set the architecture based on system architecture and Python |
| 54 | + is_x64 = platform.architecture()[0] == '64bit' |
| 55 | + if is_x64 and sys.maxsize > 2**32: |
| 56 | + flags.append('/DWIN64=1') |
| 57 | + else: |
| 58 | + # Architecture is 32-bit, or Python is 32-bit |
| 59 | + flags.append('/DWIN32=1') |
| 60 | + |
| 61 | + return flags |
| 62 | + |
| 63 | + |
| 64 | +def get_source_files(): |
| 65 | + """Return a list of paths to the source files to be compiled.""" |
| 66 | + source_files = [ |
| 67 | + 'rle/_libjpeg.pyx', |
| 68 | + os.path.join(RLE_SRC, 'decode.cpp'), |
| 69 | + ] |
| 70 | + #for fname in Path(RLE_SRC).glob('*/*'): |
| 71 | + # if '.cpp' in str(fname): |
| 72 | + # source_files.append(str(fname)) |
| 73 | + |
| 74 | + return source_files |
| 75 | + |
| 76 | + |
| 77 | +# Compiler and linker arguments |
| 78 | +extra_compile_args = [] |
| 79 | +extra_link_args = [] |
| 80 | +if platform.system() == 'Windows': |
| 81 | + os.environ['LIB'] = os.path.abspath( |
| 82 | + os.path.join(sys.executable, '../', 'libs') |
| 83 | + ) |
| 84 | + extra_compile_args = get_mscv_args() |
| 85 | + |
| 86 | + |
| 87 | +extensions = [ |
| 88 | + Extension( |
| 89 | + '_rle', |
| 90 | + get_source_files(), |
| 91 | + language='c++', |
| 92 | + include_dirs=[ |
| 93 | + RLE_SRC, |
| 94 | + distutils.sysconfig.get_python_inc(), |
| 95 | + # Numpy includes get added by the `build` subclass |
| 96 | + ], |
| 97 | + extra_compile_args=extra_compile_args, |
| 98 | + extra_link_args=extra_link_args, |
| 99 | + ) |
| 100 | +] |
| 101 | + |
| 102 | +VERSION_FILE = os.path.join('rle', '_version.py') |
| 103 | +with open(VERSION_FILE) as fp: |
| 104 | + exec(fp.read()) |
| 105 | + |
| 106 | +with open('README.md', 'r') as fp: |
| 107 | + long_description = fp.read() |
| 108 | + |
| 109 | +setup( |
| 110 | + name = 'pylibjpeg-rle', |
| 111 | + description = ( |
| 112 | + "A Python wrapper for libjpeg, with a focus on use as a plugin for " |
| 113 | + "for pylibjpeg" |
| 114 | + ), |
| 115 | + long_description = long_description, |
| 116 | + long_description_content_type = 'text/markdown', |
| 117 | + version = __version__, |
| 118 | + author = "scaramallion", |
| 119 | + author_email = "[email protected]", |
| 120 | + url = "https://github.com/pydicom/pylibjpeg-rle", |
| 121 | + license = "MIT", |
| 122 | + keywords = ( |
| 123 | + "dicom pydicom python medicalimaging radiotherapy oncology imaging " |
| 124 | + "radiology nuclearmedicine rle pylibjpeg" |
| 125 | + ), |
| 126 | + classifiers = [ |
| 127 | + "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", |
| 128 | + "Intended Audience :: Developers", |
| 129 | + "Intended Audience :: Healthcare Industry", |
| 130 | + "Intended Audience :: Science/Research", |
| 131 | + "Development Status :: 3 - Alpha", |
| 132 | + #"Development Status :: 4 - Beta", |
| 133 | + #"Development Status :: 5 - Production/Stable", |
| 134 | + "Natural Language :: English", |
| 135 | + "Programming Language :: C++", |
| 136 | + "Programming Language :: Python :: 3.6", |
| 137 | + "Programming Language :: Python :: 3.7", |
| 138 | + "Programming Language :: Python :: 3.8", |
| 139 | + "Operating System :: MacOS :: MacOS X", |
| 140 | + "Operating System :: POSIX :: Linux", |
| 141 | + "Operating System :: Microsoft :: Windows", |
| 142 | + "Topic :: Scientific/Engineering :: Medical Science Apps.", |
| 143 | + "Topic :: Software Development :: Libraries", |
| 144 | + ], |
| 145 | + packages = find_packages(), |
| 146 | + package_data = {'': ['*.txt', '*.cpp', '*.h', '*.hpp', '*.pyx']}, |
| 147 | + include_package_data = True, |
| 148 | + zip_safe = False, |
| 149 | + python_requires = ">=3.6", |
| 150 | + setup_requires = ['setuptools>=18.0', 'cython', 'numpy>=1.16.0'], |
| 151 | + install_requires = ["numpy>=1.16.0"], |
| 152 | + cmdclass = {'build': build}, |
| 153 | + ext_modules = extensions, |
| 154 | + # Plugin registrations |
| 155 | + entry_points={ |
| 156 | + 'pylibjpeg.pixel_data_decoders': [ |
| 157 | + "1.2.840.10008.1.2.5 = rle:decode_pixel_data", |
| 158 | + ], |
| 159 | + }, |
| 160 | +) |
0 commit comments