|
| 1 | + |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | +import platform |
| 5 | +import shutil |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +from typing import Any, List, Dict |
| 9 | + |
| 10 | + |
| 11 | +PACKAGE_DIR = Path(__file__).parent / "libjpeg" |
| 12 | +LIBJPEG_SRC = PACKAGE_DIR / 'src' / 'libjpeg' |
| 13 | +INTERFACE_SRC = PACKAGE_DIR / 'src' / 'interface' |
| 14 | + |
| 15 | + |
| 16 | +def build(setup_kwargs: Any) -> Any: |
| 17 | + from setuptools import Extension |
| 18 | + from setuptools.dist import Distribution |
| 19 | + import Cython.Compiler.Options |
| 20 | + from Cython.Build import build_ext, cythonize |
| 21 | + import numpy |
| 22 | + |
| 23 | + # Compiler and linker arguments |
| 24 | + extra_compile_args = [] |
| 25 | + extra_link_args = [] |
| 26 | + if platform.system() == 'Windows': |
| 27 | + os.environ['LIB'] = os.path.abspath( |
| 28 | + os.path.join(sys.executable, '../', 'libs') |
| 29 | + ) |
| 30 | + extra_compile_args = get_mscv_args() |
| 31 | + elif platform.system() in ['Darwin', 'Linux']: |
| 32 | + # Skip configuration if running with `sdist` |
| 33 | + if 'sdist' not in sys.argv: |
| 34 | + opts = get_gcc_args() |
| 35 | + extra_compile_args += opts['ADDOPTS'] |
| 36 | + extra_link_args += opts['EXTRA_LIBS'] |
| 37 | + |
| 38 | + ext = Extension( |
| 39 | + '_libjpeg', |
| 40 | + [os.fspath(p) for p in get_source_files()], |
| 41 | + language='c++', |
| 42 | + include_dirs=[ |
| 43 | + os.fspath(LIBJPEG_SRC), |
| 44 | + os.fspath(INTERFACE_SRC), |
| 45 | + numpy.get_include(), |
| 46 | + ], |
| 47 | + extra_compile_args=extra_compile_args, |
| 48 | + extra_link_args=extra_link_args, |
| 49 | + ) |
| 50 | + |
| 51 | + ext_modules = cythonize( |
| 52 | + [ext], |
| 53 | + include_path=ext.include_dirs, |
| 54 | + language_level=3, |
| 55 | + ) |
| 56 | + |
| 57 | + dist = Distribution({"ext_modules": ext_modules}) |
| 58 | + cmd = build_ext(dist) |
| 59 | + cmd.ensure_finalized() |
| 60 | + cmd.run() |
| 61 | + |
| 62 | + for output in cmd.get_outputs(): |
| 63 | + output = Path(output) |
| 64 | + relative_ext = output.relative_to(cmd.build_lib) |
| 65 | + shutil.copyfile(output, relative_ext) |
| 66 | + |
| 67 | + return setup_kwargs |
| 68 | + |
| 69 | + |
| 70 | +def get_mscv_args() -> List[str]: |
| 71 | + """Return a list of compiler args for MSVC++'s compiler.""" |
| 72 | + flags = [ |
| 73 | + '/GS', # Buffer security check |
| 74 | + '/W3', # Warning level |
| 75 | + '/Zc:wchar_t', # Use windows char type |
| 76 | + '/Zc:inline', # Remove unreferenced function or data (...) |
| 77 | + '/Zc:forScope', |
| 78 | + '/Od', # Disable optimisation |
| 79 | + '/Oy-', # (x86 only) don't omit frame pointer |
| 80 | + '/openmp-', # Disable #pragma omp directive |
| 81 | + '/FC', # Display full path of source code files |
| 82 | + '/fp:precise', # Floating-point behaviour |
| 83 | + '/Gd', # (x86 only) use __cdecl calling convention |
| 84 | + '/GF-', # Disable string pooling |
| 85 | + '/GR', # Enable run-time type info |
| 86 | + '/RTC1', # Enable run-time error checking |
| 87 | + '/MT', # Create multithreading executable |
| 88 | + # /D defines constants and macros |
| 89 | + '/D_UNICODE', |
| 90 | + '/DUNICODE', |
| 91 | + ] |
| 92 | + |
| 93 | + # Set the architecture based on system architecture and Python |
| 94 | + is_x64 = platform.architecture()[0] == '64bit' |
| 95 | + if is_x64 and sys.maxsize > 2**32: |
| 96 | + flags.append('/DWIN64=1') |
| 97 | + else: |
| 98 | + # Architecture is 32-bit, or Python is 32-bit |
| 99 | + flags.append('/DWIN32=1') |
| 100 | + |
| 101 | + return flags |
| 102 | + |
| 103 | + |
| 104 | +def get_gcc_args() -> Dict[str, str]: |
| 105 | + """Return a list of compiler and linker args for GCC/clang. |
| 106 | +
|
| 107 | + The args are determined by running the src/libjpeg/configure script then |
| 108 | + parsing src/libjpeg/automakefile for the relevant values. |
| 109 | +
|
| 110 | + Returns |
| 111 | + ------- |
| 112 | + dict |
| 113 | + A dict with keys COMPILER_CMD, CC_ONLY, SETTINGS, PREFIX, |
| 114 | + PTHREADCFLAGS, PTHREADLDFLAGS, PTHREADLIBS, HWTYPE, HAVE_ADDONS, |
| 115 | + BITSIZE, ADDOPTS, LIB_OPTS, EXTRA_LIBS, CPU, TUNE. |
| 116 | + """ |
| 117 | + # Run configure script once |
| 118 | + # Using GCC or clang, run `configure` bash script once |
| 119 | + if 'config.log' not in os.listdir(LIBJPEG_SRC): |
| 120 | + # Needs to be determined before changing the working dir |
| 121 | + fpath = os.path.abspath(LIBJPEG_SRC) |
| 122 | + # Needs to be run from within the src/libjpeg directory |
| 123 | + current_dir = os.getcwd() |
| 124 | + os.chdir(LIBJPEG_SRC) |
| 125 | + subprocess.call([os.path.join(fpath, 'configure')]) |
| 126 | + os.chdir(current_dir) |
| 127 | + |
| 128 | + # Get compilation options |
| 129 | + with open(os.path.join(LIBJPEG_SRC, 'automakefile')) as fp: |
| 130 | + lines = fp.readlines() |
| 131 | + |
| 132 | + lines = [ll for ll in lines if not ll.startswith('#')] |
| 133 | + opts = [ll.split('=', 1) for ll in lines] |
| 134 | + opts = {vv[0].strip():list(vv[1].strip().split(' ')) for vv in opts} |
| 135 | + |
| 136 | + return opts |
| 137 | + |
| 138 | + |
| 139 | +def get_source_files() -> List[Path]: |
| 140 | + """Return a list of paths to the source files to be compiled.""" |
| 141 | + source_files = [ |
| 142 | + PACKAGE_DIR / '_libjpeg.pyx', |
| 143 | + INTERFACE_SRC /'decode.cpp', |
| 144 | + INTERFACE_SRC /'streamhook.cpp', |
| 145 | + ] |
| 146 | + for p in LIBJPEG_SRC.glob('*/*'): |
| 147 | + if p.suffix == '.cpp': |
| 148 | + source_files.append(p) |
| 149 | + |
| 150 | + # Source files must always be relative to the setup.py directory |
| 151 | + source_files = [p.relative_to(PACKAGE_DIR.parent) for p in source_files] |
| 152 | + |
| 153 | + return source_files |
0 commit comments