| 1 |
from setuptools import setup, find_packages |
| 2 |
from setuptools.extension import Extension |
| 3 |
from os import path, environ |
| 4 |
import sys |
| 5 |
import sysconfig |
| 6 |
from pathlib import Path |
| 7 |
|
| 8 |
# needed for isolated environment |
| 9 |
sys.path.insert(0, str(Path(__file__).parent.resolve())) |
| 10 |
from scalene.scalene_config import scalene_version |
| 11 |
|
| 12 |
sys.path.pop(0) |
| 13 |
|
| 14 |
|
| 15 |
if sys.platform == "darwin": |
| 16 |
import sysconfig |
| 17 |
|
| 18 |
mdt = "MACOSX_DEPLOYMENT_TARGET" |
| 19 |
target = environ[mdt] if mdt in environ else sysconfig.get_config_var(mdt) |
| 20 |
# target >= 10.9 is required for gcc/clang to find libstdc++ headers |
| 21 |
if [int(n) for n in target.split(".")] < [10, 9]: |
| 22 |
from os import execve |
| 23 |
|
| 24 |
newenv = environ.copy() |
| 25 |
newenv[mdt] = "10.9" |
| 26 |
execve(sys.executable, [sys.executable] + sys.argv, newenv) |
| 27 |
|
| 28 |
|
| 29 |
def compiler_archs(compiler: str): |
| 30 |
"""Discovers what platforms the given compiler supports; intended for MacOS use""" |
| 31 |
import tempfile |
| 32 |
import subprocess |
| 33 |
|
| 34 |
print(f"Compiler: {compiler}") |
| 35 |
arch_flags = [] |
| 36 |
|
| 37 |
# see also the architectures tested for in .github/workflows/build-and-upload.yml |
| 38 |
for arch in ["x86_64", "arm64", "arm64e"]: |
| 39 |
with tempfile.TemporaryDirectory() as tmpdir: |
| 40 |
cpp = Path(tmpdir) / "test.cxx" |
| 41 |
cpp.write_text("int main() {return 0;}\n") |
| 42 |
out = Path(tmpdir) / "a.out" |
| 43 |
p = subprocess.run( |
| 44 |
[compiler, "-arch", arch, str(cpp), "-o", str(out)], capture_output=True |
| 45 |
) |
| 46 |
if p.returncode == 0: |
| 47 |
arch_flags += ["-arch", arch] |
| 48 |
|
| 49 |
print(f"Discovered {compiler} arch flags: {arch_flags}") |
| 50 |
return arch_flags |
| 51 |
|
| 52 |
|
| 53 |
def extra_compile_args(): |
| 54 |
"""Returns extra compiler args for platform.""" |
| 55 |
if sys.platform == "win32": |
| 56 |
return ["/std:c++14"] # for Visual Studio C++ |
| 57 |
|
| 58 |
return ["-std=c++14"] |
| 59 |
|
| 60 |
|
| 61 |
def make_command(): |
| 62 |
# return 'nmake' if sys.platform == 'win32' else 'make' # 'nmake' isn't found on github actions' VM |
| 63 |
return "make" |
| 64 |
|
| 65 |
|
| 66 |
def dll_suffix(): |
| 67 |
"""Returns the file suffix ("extension") of a DLL""" |
| 68 |
if sys.platform == "win32": |
| 69 |
return ".dll" |
| 70 |
if sys.platform == "darwin": |
| 71 |
return ".dylib" |
| 72 |
return ".so" |
| 73 |
|
| 74 |
|
| 75 |
def read_file(name): |
| 76 |
"""Returns a file's contents""" |
| 77 |
with open(path.join(path.dirname(__file__), name), encoding="utf-8") as f: |
| 78 |
return f.read() |
| 79 |
|
| 80 |
|
| 81 |
import setuptools.command.egg_info |
| 82 |
|
| 83 |
|
| 84 |
class EggInfoCommand(setuptools.command.egg_info.egg_info): |
| 85 |
"""Custom command to download vendor libs before creating the egg_info.""" |
| 86 |
|
| 87 |
def run(self): |
| 88 |
if sys.platform != "win32": |
| 89 |
self.spawn([make_command(), "vendor-deps"]) |
| 90 |
super().run() |
| 91 |
|
| 92 |
|
| 93 |
# Force building platform-specific wheel to avoid the Windows wheel |
| 94 |
# (which doesn't include libscalene, and thus would be considered "pure") |
| 95 |
# being used for other platforms. |
| 96 |
try: |
| 97 |
from wheel.bdist_wheel import bdist_wheel |
| 98 |
|
| 99 |
class BdistWheelCommand(bdist_wheel): |
| 100 |
def finalize_options(self): |
| 101 |
super().finalize_options() |
| 102 |
self.root_is_pure = False |
| 103 |
|
| 104 |
except (ImportError, ModuleNotFoundError): |
| 105 |
# Disable wheel if `wheel` not installed. |
| 106 |
print( |
| 107 |
"If this installation does not work, run `pip install setuptools wheel` and try again." |
| 108 |
) |
| 109 |
BdistWheelCommand = None |
| 110 |
|
| 111 |
import setuptools.command.build_ext |
| 112 |
|
| 113 |
|
| 114 |
class BuildExtCommand(setuptools.command.build_ext.build_ext): |
| 115 |
"""Custom command that runs 'make' to generate libscalene, and also does MacOS |
| 116 |
supported --arch flag discovery.""" |
| 117 |
|
| 118 |
def build_extensions(self): |
| 119 |
arch_flags = [] |
| 120 |
if sys.platform == "darwin": |
| 121 |
# The only sure way to tell which compiler build_ext is going to use |
| 122 |
# seems to be to customize a build_ext and look at its internal flags :( |
| 123 |
# Also, note that self.plat_name here isn't "...-universal2" even if that |
| 124 |
# is what we're building; that's only in bdist_wheel.plat_name. |
| 125 |
arch_flags += compiler_archs(self.compiler.compiler_cxx[0]) |
| 126 |
for ext in self.extensions: |
| 127 |
# While the flags _could_ be different between the programs used for |
| 128 |
# C and C++ compilation and linking, we have no way to adapt them here, |
| 129 |
# so it seems best to just use them and let it error out if not recognized. |
| 130 |
ext.extra_compile_args += arch_flags |
| 131 |
ext.extra_link_args += arch_flags |
| 132 |
|
| 133 |
super().build_extensions() |
| 134 |
|
| 135 |
# No build of DLL for Windows currently. |
| 136 |
if sys.platform != "win32": |
| 137 |
self.build_libscalene(arch_flags) # XXX should we pass compiler_cxx here? |
| 138 |
|
| 139 |
def build_libscalene(self, arch_flags): |
| 140 |
scalene_temp = path.join(self.build_temp, "scalene") |
| 141 |
scalene_lib = path.join(self.build_lib, "scalene") |
| 142 |
libscalene = "libscalene" + dll_suffix() |
| 143 |
self.mkpath(scalene_temp) |
| 144 |
self.mkpath(scalene_lib) |
| 145 |
self.spawn( |
| 146 |
[make_command(), "OUTDIR=" + scalene_temp, "ARCH=" + " ".join(arch_flags)] |
| 147 |
) |
| 148 |
self.copy_file( |
| 149 |
path.join(scalene_temp, libscalene), path.join(scalene_lib, libscalene) |
| 150 |
) |
| 151 |
|
| 152 |
def copy_extensions_to_source(self): |
| 153 |
# self.inplace is temporarily overridden while running build_extensions, |
| 154 |
# so inplace copying (for pip install -e, setup.py develop) must be done here. |
| 155 |
|
| 156 |
super().copy_extensions_to_source() |
| 157 |
|
| 158 |
if sys.platform != "win32": |
| 159 |
scalene_lib = path.join(self.build_lib, "scalene") |
| 160 |
inplace_dir = self.get_finalized_command("build_py").get_package_dir( |
| 161 |
"scalene" |
| 162 |
) |
| 163 |
libscalene = "libscalene" + dll_suffix() |
| 164 |
self.copy_file( |
| 165 |
path.join(scalene_lib, libscalene), path.join(inplace_dir, libscalene) |
| 166 |
) |
| 167 |
|
| 168 |
|
| 169 |
get_line_atomic = Extension( |
| 170 |
"scalene.get_line_atomic", |
| 171 |
include_dirs=[".", "vendor/Heap-Layers", "vendor/Heap-Layers/utility"], |
| 172 |
sources=["src/source/get_line_atomic.cpp"], |
| 173 |
extra_compile_args=extra_compile_args(), |
| 174 |
py_limited_api=True, # for binary compatibility |
| 175 |
language="c++", |
| 176 |
) |
| 177 |
|
| 178 |
pywhere = Extension( |
| 179 |
"scalene.pywhere", |
| 180 |
include_dirs=[".", "src", "src/include"], |
| 181 |
depends=["src/include/traceconfig.hpp"], |
| 182 |
sources=["src/source/pywhere.cpp", "src/source/traceconfig.cpp"], |
| 183 |
extra_compile_args=extra_compile_args(), |
| 184 |
py_limited_api=False, |
| 185 |
language="c++", |
| 186 |
) |
| 187 |
|
| 188 |
# If we're testing packaging, build using a ".devN" suffix in the version number, |
| 189 |
# so that we can upload new files (as testpypi/pypi don't allow re-uploading files with |
| 190 |
# the same name as previously uploaded). |
| 191 |
# Numbering scheme: https://www.python.org/dev/peps/pep-0440 |
| 192 |
dev_build = (".dev" + environ["DEV_BUILD"]) if "DEV_BUILD" in environ else "" |
| 193 |
|
| 194 |
|
| 195 |
def bdist_wheel_options(): |
| 196 |
if sys.platform == "darwin": |
| 197 |
# Build universal wheels on MacOS. |
| 198 |
# --- |
| 199 |
# On MacOS >= 11, all builds are compatible within a major MacOS version, so Python "floors" |
| 200 |
# all minor versions to 0, leading to tags like like "macosx_11_0_universal2". If you use |
| 201 |
# the actual (non-0) minor name in the build platform, it isn't recognized. |
| 202 |
# --- |
| 203 |
# It would be nice to check whether we're actually building multi-architecture, |
| 204 |
# but that depends on the platforms supported by the compiler build_ext wants to use, |
| 205 |
# which is hard to obtain (see BuildExtCommand above). |
| 206 |
import platform |
| 207 |
|
| 208 |
v = platform.mac_ver()[0] |
| 209 |
major = int(v.split(".")[0]) |
| 210 |
if major >= 11: |
| 211 |
v = f"{major}.0" |
| 212 |
return {"plat_name": f"macosx-{v}-universal2"} |
| 213 |
|
| 214 |
return {} |
| 215 |
|
| 216 |
|
| 217 |
setup( |
| 218 |
version=scalene_version + dev_build, |
| 219 |
packages=find_packages(), |
| 220 |
cmdclass={ |
| 221 |
"bdist_wheel": BdistWheelCommand, |
| 222 |
"egg_info": EggInfoCommand, |
| 223 |
"build_ext": BuildExtCommand, |
| 224 |
}, |
| 225 |
ext_modules=([get_line_atomic, pywhere] if sys.platform != "win32" else []), |
| 226 |
include_package_data=True, |
| 227 |
options={"bdist_wheel": bdist_wheel_options()}, |
| 228 |
) |