-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
105 lines (90 loc) · 3.13 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import glob
import os
import platform
import shutil
from setuptools import setup, Extension, Command
from setuptools.command.build_ext import build_ext
IS_WINDOWS = os.name == "nt"
IS_MACOS = platform.system() == "Darwin"
if IS_WINDOWS:
EXTRA_COMPILE_ARG = [
"/O2", "/Ob2", "/GL", "/W4", "/std:c++20", "/WX",
"/wd4068", # ignore unknown pragma error
"/EHsc"
]
EXTRA_LINK_ARG = ["/LTCG"]
elif IS_MACOS:
EXTRA_COMPILE_ARG = [
"-O3", "-flto", "-fPIC",
"-Wall", "-fvisibility=hidden",
"-Wno-error=unknown-pragmas",
"-mavx", "-mavx2", "-mavx512f", "-mavx512bw", "-mavx512dq", "-mavx512vl",
"-fno-tree-vectorize", "-faligned-allocation"
]
EXTRA_LINK_ARG = ["-flto"]
os.environ["CFLAGS"] = "-std=c2x"
os.environ["CXXFLAGS"] = "-std=c++20"
else:
EXTRA_COMPILE_ARG = [
"-O3", "-flto", "-fPIC",
"-std=c++20", "-Wall", "-fvisibility=hidden",
"-Wno-error=unknown-pragmas",
"-mavx", "-mavx2", "-mavx512f", "-mavx512bw", "-mavx512dq", "-mavx512vl",
"-fno-tree-vectorize"
]
EXTRA_LINK_ARG = ["-flto", "-fpermissive"]
class CleanCommand(Command):
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
shutil.rmtree("./build", ignore_errors=True)
shutil.rmtree("./dist", ignore_errors=True)
shutil.rmtree("./__pycache__", ignore_errors=True)
shutil.rmtree("./pyfastutil.egg-info", ignore_errors=True)
print("Cleaned up build directories.")
class CustomBuildExtCommand(build_ext):
def run(self):
build_ext.run(self)
build_lib = self.build_lib
pattern = os.path.join(build_lib, "pyfastutil", "__pyfastutil.*")
outFiles = glob.glob(pattern)
if outFiles:
source_file = outFiles[0]
target_filename = "__pyfastutil" + (".pyd" if IS_WINDOWS else ".so")
target_file = os.path.join("./pyfastutil", target_filename)
shutil.copy(source_file, target_file)
shutil.move(source_file, os.path.join(build_lib, "pyfastutil", target_filename))
print(f"File copied and renamed from {source_file} to {target_file}")
else:
print("No files found matching the pattern")
if __name__ == "__main__":
sources = []
for root, dirs, files in os.walk("./pyfastutil/src"):
for file in files:
if file.endswith(".cpp") or file.endswith(".c"):
sources.append(os.path.join(root, file))
modules = [
Extension(
name="pyfastutil.__pyfastutil",
sources=sources,
language="c++",
extra_compile_args=EXTRA_COMPILE_ARG,
extra_link_args=EXTRA_LINK_ARG,
include_dirs=["./pyfastutil/src"]
)
]
setup(
name="__pyfastutil",
description="C++ implementation of PyFastUtil.",
ext_modules=modules,
cmdclass={
"clean": CleanCommand,
"build_ext": CustomBuildExtCommand,
},
package_data={
"pyfastutil": ["src/**/*.h"],
}
)