-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
79 lines (64 loc) · 2.36 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
# Copyright 2023, popatam, bjia56, tfuxu <https://github.com/tfuxu>
# SPDX-License-Identifier: GPL-3.0-or-later
import json
import os
import shutil
import subprocess
import sys
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
if sys.platform == 'darwin':
# PYTHON_BINARY_PATH is setting explicitly for 310 and 311, see build_wheel.yml
# on macos PYTHON_BINARY_PATH must be python bin installed from python.org
PYTHON_BINARY = os.getenv("PYTHON_BINARY_PATH", sys.executable)
else:
# windown & linux
PYTHON_BINARY = sys.executable
def _generate_path_with_gopath() -> str:
go_path = subprocess.check_output(["go", "env", "GOPATH"]).decode("utf-8").strip()
path_val = f'{os.getenv("PATH")}:{go_path}/bin'
return path_val
class CustomBuildExt(build_ext):
def build_extension(self, ext: Extension):
bin_path = _generate_path_with_gopath()
go_env = json.loads(subprocess.check_output(["go", "env", "-json"]).decode("utf-8").strip())
destination = os.path.dirname(os.path.abspath(self.get_ext_fullpath(ext.name))) + "/dither_go/bindings"
if os.path.isdir(destination):
# clean up destination in case it has existing build artifacts
shutil.rmtree(destination)
env = {
"PATH": bin_path,
**go_env,
"CGO_LDFLAGS_ALLOW": ".*",
}
# https://stackoverflow.com/a/64706392
if sys.platform == "win32":
env["SYSTEMROOT"] = os.environ.get("SYSTEMROOT", "")
if sys.platform == "darwin":
min_ver = os.environ.get("MACOSX_DEPLOYMENT_TARGET", "")
env["MACOSX_DEPLOYMENT_TARGET"] = min_ver
env["CGO_LDFLAGS"] = "-mmacosx-version-min=" + min_ver
env["CGO_CFLAGS"] = "-mmacosx-version-min=" + min_ver
subprocess.check_call(
[
"gopy",
"build",
"-no-make",
"-dynamic-link=True",
"-symbols=False",
"-output",
destination,
"-vm",
PYTHON_BINARY,
*ext.sources,
],
env=env,
)
setup(
cmdclass={
"build_ext": CustomBuildExt,
},
ext_modules=[
Extension("dither_go", sources=["./dither_go", "github.com/tfuxu/dither-gopy"])
],
)