Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
184 lines (158 loc) · 6.16 KB
/
Copy pathsetup.py
File metadata and controls
184 lines (158 loc) · 6.16 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import os
import pathlib
import platform
import re
import subprocess
import sys
from importlib import metadata
from setuptools import Extension, find_packages, setup
from setuptools.command.build_ext import build_ext
PLATFORM = platform.system()
ROOT_DIR = pathlib.Path(__file__).parent.resolve()
PACKAGE_NAME = 'tun2socks'
BINDING_NAME = 'tun2socks'
PACKAGE_VERSION = (ROOT_DIR / 'VERSION').read_text(encoding='utf-8').strip()
UPSTREAM_VERSION = (ROOT_DIR / 'UPSTREAM_VERSION').read_text(encoding='utf-8').strip()
UPSTREAM_COMMIT = (ROOT_DIR / 'UPSTREAM_COMMIT').read_text(encoding='utf-8').strip()
def run_command(command, **kwargs):
subprocess.run(command, check=True, **kwargs)
def macos_architectures():
return re.findall(r'-arch\s+(\S+)', os.environ.get('ARCHFLAGS', ''))
def pybind11_cmake_dir():
# Resolve the PEP 517 build dependency by distribution metadata. Importing
# pybind11 here would be ambiguous in a checkout that still contains the
# project's older vendored pybind11/ source directory.
distribution = metadata.distribution('pybind11')
return distribution.locate_file('pybind11/share/cmake/pybind11')
def build_tun2socks(output_dir):
output_dir.mkdir(parents=True, exist_ok=True)
output = f'{BINDING_NAME}.lib' if PLATFORM == 'Windows' else f'{BINDING_NAME}.a'
environment = os.environ.copy()
# cibuildwheel sets ARCHFLAGS for its requested macOS architecture. Go's
# architecture names differ from Apple's, so keep the Go archive aligned
# with the CMake extension when CPython itself runs under Rosetta (3.8).
if PLATFORM == 'Darwin':
architectures = macos_architectures()
if len(architectures) == 1:
environment['GOARCH'] = {'x86_64': 'amd64', 'arm64': 'arm64'}[
architectures[0]
]
run_command(
[
'go',
'build',
'-C',
str(ROOT_DIR / 'tun2socks-go'),
'-o',
str(output_dir / output),
'-buildmode=c-archive',
'-trimpath',
'-ldflags',
'-s -w -buildid=',
'./python-binding',
],
env=environment,
)
class CMakeExtension(Extension):
def __init__(self, name):
# The sources are built by CMake, but declaring an Extension is what
# tells setuptools and wheel that this distribution is not pure Python.
super().__init__(name, sources=[])
class BuildTun2socks(build_ext):
def build_extension(self, extension):
extension_path = pathlib.Path(self.get_ext_fullpath(extension.name)).resolve()
extension_dir = extension_path.parent
build_root = pathlib.Path(self.build_temp).resolve() / extension.name.replace(
'.', '_'
)
go_build_dir = build_root / 'go'
cmake_build_dir = build_root / 'cmake'
extension_dir.mkdir(parents=True, exist_ok=True)
cmake_build_dir.mkdir(parents=True, exist_ok=True)
build_tun2socks(go_build_dir)
configure_command = [
'cmake',
'-S',
str(ROOT_DIR),
'-B',
str(cmake_build_dir),
'-DCMAKE_BUILD_TYPE=Release',
f'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extension_dir}',
f'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE={extension_dir}',
'-DPYBIND11_FINDPYTHON=ON',
f'-DPython_EXECUTABLE={sys.executable}',
f'-Dpybind11_DIR={pybind11_cmake_dir()}',
f'-DTUN2SOCKS_GO_BUILD_DIR={go_build_dir}',
f'-DTUN2SOCKS_PYTHON_VERSION={PACKAGE_VERSION}',
f'-DTUN2SOCKS_UPSTREAM_VERSION={UPSTREAM_VERSION}',
f'-DTUN2SOCKS_UPSTREAM_COMMIT={UPSTREAM_COMMIT}',
]
if PLATFORM == 'Windows':
configure_command += [
'-G',
'MinGW Makefiles',
'-DCMAKE_C_COMPILER=gcc',
'-DCMAKE_CXX_COMPILER=g++',
]
elif PLATFORM == 'Darwin':
architectures = macos_architectures()
if architectures:
architecture_list = ';'.join(architectures)
configure_command.append(
f'-DCMAKE_OSX_ARCHITECTURES={architecture_list}'
)
run_command(configure_command)
run_command(
[
'cmake',
'--build',
str(cmake_build_dir),
'--config',
'Release',
'--target',
BINDING_NAME,
]
)
if not extension_path.is_file():
raise RuntimeError(
'CMake did not create the extension at the path expected by '
f'setuptools: {extension_path}'
)
with open(ROOT_DIR / 'README.md', 'r', encoding='utf-8') as file:
long_description = file.read()
setup(
name=PACKAGE_NAME,
version=PACKAGE_VERSION,
license='MIT',
description='Python bindings for go tun2socks.',
long_description=long_description,
long_description_content_type='text/markdown',
author='Loren Eteval',
author_email='loren.eteval@proton.me',
url='https://github.com/LorenEteval/tun2socks-python',
cmdclass={'build_ext': BuildTun2socks},
ext_modules=[CMakeExtension('tun2socks.tun2socks')],
packages=find_packages(),
include_package_data=True,
python_requires='>=3.8',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Programming Language :: C++',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3 :: Only',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
'Programming Language :: Python :: 3.13',
'Programming Language :: Python :: 3.14',
'Operating System :: MacOS',
'Operating System :: Microsoft :: Windows',
'Operating System :: POSIX :: Linux',
'Topic :: Internet',
'Topic :: Internet :: Proxy Servers',
],
zip_safe=False,
)