|
| 1 | +import sys |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import shutil |
| 5 | +import time |
| 6 | +import platform |
| 7 | +import distutils.dir_util |
| 8 | +from wheel.bdist_wheel import bdist_wheel |
| 9 | + |
| 10 | + |
| 11 | +''' |
| 12 | +BDistWheel forces python and abi wheel tags for binary distributions |
| 13 | +''' |
| 14 | + |
| 15 | + |
| 16 | +class BDistWheel(bdist_wheel): |
| 17 | + |
| 18 | + def finalize_options(self): |
| 19 | + bdist_wheel.finalize_options(self) |
| 20 | + self.root_is_pure = ("--universal" in sys.argv) |
| 21 | + |
| 22 | + |
| 23 | +''' |
| 24 | +Sort out the platform library for binary distribution and add to the package directory. |
| 25 | +Place all libraries onto the package directory for source distribution. |
| 26 | +Place the XML doc file onto the package directory. |
| 27 | +''' |
| 28 | + |
| 29 | + |
| 30 | +class PackageDataBuilder(): |
| 31 | + |
| 32 | + def __init__(self): |
| 33 | + self.pkg_dir = os.path.join(os.curdir, "delphifmx") |
| 34 | + self.plat_name = None |
| 35 | + for arg in sys.argv: |
| 36 | + if arg.startswith("--plat-name=") and (len(arg.split("=")) == 2): |
| 37 | + self.plat_name = arg.split("=")[1] |
| 38 | + |
| 39 | + ''' |
| 40 | + Must run "python setup.py clean --all" between builds. |
| 41 | + ''' |
| 42 | + |
| 43 | + def clean_up(self): |
| 44 | + lib_dirs = [os.path.join(self.pkg_dir, "Win32"), |
| 45 | + os.path.join(self.pkg_dir, "Win64"), |
| 46 | + os.path.join(self.pkg_dir, "Linux64"), |
| 47 | + os.path.join(self.pkg_dir, "OSX64"), |
| 48 | + os.path.join(self.pkg_dir, "OSXARM64")] |
| 49 | + |
| 50 | + for lib_dir in lib_dirs: |
| 51 | + if os.path.isdir(lib_dir): |
| 52 | + shutil.rmtree(lib_dir) |
| 53 | + |
| 54 | + # Wait until the OS remove all dirs |
| 55 | + for lib_dir in lib_dirs: |
| 56 | + for attempts in range(3): |
| 57 | + if not os.path.isdir(lib_dir): |
| 58 | + break |
| 59 | + else: |
| 60 | + time.sleep(1) |
| 61 | + |
| 62 | + ''' |
| 63 | + Cross-compiling wheel. |
| 64 | + This generates a wheel following the cross platform set on --plat-name. |
| 65 | + See: https://docs.python.org/3/distutils/builtdist.html#cross-compiling-on-windows |
| 66 | + ''' |
| 67 | + |
| 68 | + def __check_cross_compiling(self): |
| 69 | + is_cross_compiling = False |
| 70 | + lib_dir = None |
| 71 | + |
| 72 | + if self.plat_name: |
| 73 | + is_cross_compiling = True |
| 74 | + if self.plat_name == "win32": |
| 75 | + lib_dir = "Win32" |
| 76 | + elif self.plat_name == "win_amd64": |
| 77 | + lib_dir = "Win64" |
| 78 | + elif self.plat_name == "manylinux1_x86_64": |
| 79 | + lib_dir = "Linux64" |
| 80 | + # macosx_10_9_x86_64 |
| 81 | + elif self.plat_name.startswith("macosx") and self.plat_name.endswith("x86_64"): |
| 82 | + lib_dir = "OSX64" |
| 83 | + # macosx_11_0_arm64 |
| 84 | + elif self.plat_name.startswith("macosx") and self.plat_name.endswith("arm64"): |
| 85 | + lib_dir = "OSXARM64" |
| 86 | + else: |
| 87 | + is_cross_compiling = False |
| 88 | + |
| 89 | + return (is_cross_compiling, lib_dir) |
| 90 | + |
| 91 | + ''' |
| 92 | + Copy the FMX extension module(s) to the package data folder. |
| 93 | + Source distributions and Universal binary distributions will deliver |
| 94 | + all library versions. The right one will be loaded by __init__.py. |
| 95 | + Platform distributions will deliver only the library that matches the runner. |
| 96 | + ''' |
| 97 | + |
| 98 | + def __pick_and_copy_libraries(self): |
| 99 | + if ("sdist" in sys.argv) or (("bdist_wheel" in sys.argv) and ("--universal" in sys.argv)): |
| 100 | + # sdist/bdist[--universal] deploy all extension modules |
| 101 | + distutils.dir_util.copy_tree("lib", self.pkg_dir) |
| 102 | + else: |
| 103 | + # Deploys the current platform extension module only |
| 104 | + is_cross_compiling, lib_dir = self.__check_cross_compiling() |
| 105 | + if not is_cross_compiling: |
| 106 | + plat_sys = platform.system() |
| 107 | + plat_mac = platform.machine() |
| 108 | + if plat_sys == "Windows": |
| 109 | + if (sys.maxsize > 2**32): |
| 110 | + # Win x64 |
| 111 | + lib_dir = "Win64" |
| 112 | + else: |
| 113 | + # Win x86 |
| 114 | + lib_dir = "Win32" |
| 115 | + elif plat_sys == "Linux": |
| 116 | + if plat_mac == "x86_64": |
| 117 | + # Linux x86_64 |
| 118 | + lib_dir = "Linux64" |
| 119 | + elif plat_sys == "Darwin": |
| 120 | + if plat_mac == "x86_64": |
| 121 | + # macOS x86_64 |
| 122 | + lib_dir = "OSX64" |
| 123 | + elif plat_mac == "arm64": |
| 124 | + # macOS arm64 |
| 125 | + lib_dir = "OSXARM64" |
| 126 | + |
| 127 | + if lib_dir: |
| 128 | + distutils.dir_util.copy_tree(os.path.join( |
| 129 | + "lib", lib_dir), os.path.join(self.pkg_dir, lib_dir)) |
| 130 | + else: |
| 131 | + raise ValueError("Unsupported platform.") |
| 132 | + |
| 133 | + ''' |
| 134 | + Copy the XML doc file to the package data folder. |
| 135 | + The docs.xml file is the result of the compilation of all xml doc files. |
| 136 | + ''' |
| 137 | + |
| 138 | + def __pick_and_copy_docs(self): |
| 139 | + # Copy the doc files to the package folder into the doc subfolder |
| 140 | + docs_file = os.path.join("docs", "xml", "docs.xml") |
| 141 | + if os.path.exists(docs_file): |
| 142 | + pkg_doc_dir = os.path.join(self.pkg_dir, "doc") |
| 143 | + if not os.path.exists(pkg_doc_dir): |
| 144 | + os.mkdir(pkg_doc_dir) |
| 145 | + distutils.file_util.copy_file( |
| 146 | + docs_file, os.path.join(pkg_doc_dir, "docs.xml")) |
| 147 | + |
| 148 | + def build_package_data(self): |
| 149 | + self.__pick_and_copy_libraries() |
| 150 | + self.__pick_and_copy_docs() |
| 151 | + |
| 152 | + |
| 153 | +def setup(): |
| 154 | + builder = PackageDataBuilder() |
| 155 | + builder.clean_up() |
| 156 | + builder.build_package_data() |
0 commit comments