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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
From 52b71d6edc8ea90d29a5844191d7b386fed67c46 Mon Sep 17 00:00:00 2001
From: Xiretza <xiretza@xiretza.xyz>
Date: Sat, 4 Jun 2022 11:33:46 +0200
Subject: [PATCH 6/7] Use cmake directly instead of letting setup.py try to
handle it
---
setup.py | 226 +------------------------------------------------------
1 file changed, 1 insertion(+), 225 deletions(-)
diff --git a/setup.py b/setup.py
index cfbaa55..8c0b3ee 100644
--- a/setup.py
+++ b/setup.py
@@ -18,20 +18,10 @@
# SPDX-License-Identifier: Apache-2.0
import os
-import re
import setuptools
-import shutil
import subprocess
-import sys
-import traceback
from Cython.Build import cythonize
-from distutils.command.build import build
-from distutils.version import LooseVersion
-from setuptools import Extension
-from setuptools.command.build_ext import build_ext
-from setuptools.command.develop import develop
-from setuptools.command.install import install
__dir__ = os.path.dirname(os.path.abspath(__file__))
@@ -56,212 +46,6 @@ assert version_value[0] == '"', version_value
assert version_value[-1] == '"', version_value
version = version_value[1:-1]
-
-# Based on: https://www.benjack.io/2018/02/02/python-cpp-revisited.html
-# GitHub: https://github.com/benjaminjack/python_cpp_example
-class CMakeExtension(Extension):
- def __init__(self, name, sourcedir='', prefix=''):
- Extension.__init__(self, name, sources=[])
- self.sourcedir = os.path.abspath(sourcedir)
- self.prefix = prefix
-
-
-# Used to share options between two classes.
-class SharedOptions():
- ANTLR_RUNTIMES = ['static', 'shared']
- options = [
- (
- 'antlr-runtime=', None,
- "Whether to use a 'static' or 'shared' ANTLR runtime.")
- ]
-
- def __init__(self):
- self.antlr_runtime = 'static'
-
- def initialize(self, other):
- other.antlr_runtime = None
-
- def load(self, other):
- if other.antlr_runtime is not None:
- self.antlr_runtime = other.antlr_runtime
- assert self.antlr_runtime in SharedOptions.ANTLR_RUNTIMES, \
- 'Invalid antlr_runtime {}, expected one of {}'.format(
- self.antlr_runtime, SharedOptions.ANTLR_RUNTIMES)
-
-
-# Global to allow sharing options.
-shared_options = SharedOptions()
-
-
-class AntlrCMakeBuild(build_ext):
- user_options = SharedOptions.options
-
- def copy_extensions_to_source(self):
- original_extensions = list(self.extensions)
- self.extensions = [
- ext for ext in self.extensions
- if not isinstance(ext, CMakeExtension)
- ]
- super().copy_extensions_to_source()
- self.extensions = original_extensions
-
- def run(self):
- shared_options.load(self)
- try:
- super().run()
-
- try:
- out = subprocess.check_output(['cmake', '--version'])
- except OSError:
- raise RuntimeError(
- "CMake must be installed to build "
- "the following extensions: " + ", ".join(
- e.name for e in self.extensions))
-
- cmake_version = LooseVersion(
- re.search(r'version\s*([\d.]+)', out.decode()).group(1))
- if cmake_version < '3.7.0':
- raise RuntimeError("CMake >= 3.7.0 is required.")
-
- for ext in self.extensions:
- self.build_extension(ext)
-
- except BaseException as e:
- print(
- "Failed to build ANTLR parser, "
- "falling back on slower textX parser. Error:\n", e)
- traceback.print_exc()
-
- # FIXME: Remove this function
- # see: https://github.com/chipsalliance/fasm/issues/50
- def add_flags(self):
- if sys.platform.startswith('win'):
- return
-
- for flag in ["CFLAGS", "CXXFLAGS"]:
- flags = [os.environ.get(flag, "")]
- if not flags[0]:
- flags.pop(0)
-
- if shared_options.antlr_runtime == 'static':
- # When linking the ANTLR runtime statically, -fPIC is
- # still necessary because libparse_fasm will be a
- # shared library.
- flags.append("-fPIC")
-
- # FIXME: These should be in the cmake config file?
- # Disable excessive warnings currently in ANTLR runtime.
- # warning: type attributes ignored after type is already defined
- # `class ANTLR4CPP_PUBLIC ATN;`
- flags.append('-Wno-attributes')
-
- # Lots of implicit fallthroughs.
- # flags.append('-Wimplicit-fallthrough=0')
-
- if flags:
- os.environ[flag] = " ".join(flags)
-
- def build_extension(self, ext):
- if isinstance(ext, CMakeExtension):
- extdir = os.path.join(
- os.path.abspath(
- os.path.dirname(self.get_ext_fullpath(ext.name))),
- ext.prefix)
- cmake_args = [
- '-DCMAKE_INSTALL_PREFIX=' + extdir,
- '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
- '-DPYTHON_EXECUTABLE=' + sys.executable,
- '-DANTLR_RUNTIME_TYPE=' + shared_options.antlr_runtime
- ]
-
- cfg = 'Debug' if self.debug else 'Release'
- build_args = ['--config', cfg]
- cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
- if not sys.platform.startswith('win') and (
- os.environ.get("CMAKE_BUILD_PARALLEL_LEVEL") is None):
- build_args += ['--', '-j']
-
- env = os.environ.copy()
- env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(
- env.get('CXXFLAGS', ''), self.distribution.get_version())
-
- # Remove the existing build_temp directory if it already exists.
- if os.path.exists(self.build_temp):
- shutil.rmtree(self.build_temp, ignore_errors=True)
- os.makedirs(self.build_temp, exist_ok=True)
-
- self.add_flags()
-
- subprocess.check_call(
- ['cmake', ext.sourcedir] + cmake_args,
- cwd=self.build_temp,
- env=env)
- subprocess.check_call(
- ['cmake', '--build', '.'] + build_args, cwd=self.build_temp)
- subprocess.check_call(
- ['cmake', '--install', '.'], cwd=self.build_temp)
- subprocess.check_call(['ctest'], cwd=self.build_temp)
- print() # Add an empty line for cleaner output
- else:
- super().build_extension(ext)
-
- def initialize_options(self):
- super().initialize_options()
- shared_options.initialize(self)
-
- def finalize_options(self):
- super().finalize_options()
- shared_options.load(self)
-
-
-class BuildCommand(build):
- user_options = build.user_options + SharedOptions.options
-
- def initialize_options(self):
- super().initialize_options()
- shared_options.initialize(self)
-
- def finalize_options(self):
- super().finalize_options()
- shared_options.load(self)
-
- def run(self):
- shared_options.load(self)
- super().run()
-
-
-class InstallCommand(install):
- user_options = install.user_options + SharedOptions.options
-
- def initialize_options(self):
- super().initialize_options()
- shared_options.initialize(self)
-
- def finalize_options(self):
- super().finalize_options()
- shared_options.load(self)
-
- def run(self):
- shared_options.load(self)
- super().run()
-
-
-class DevelopCommand(develop):
- user_options = develop.user_options + SharedOptions.options
-
- def initialize_options(self):
- super().initialize_options()
- shared_options.initialize(self)
-
- def finalize_options(self):
- super().finalize_options()
- shared_options.load(self)
-
- def run(self):
- shared_options.load(self)
- super().run()
-
-
setuptools.setup(
name="fasm",
version=version,
@@ -282,13 +66,5 @@ setuptools.setup(
entry_points={
'console_scripts': ['fasm=fasm.tool:main'],
},
- ext_modules=[
- CMakeExtension('parse_fasm', sourcedir='src', prefix='fasm/parser')
- ] + cythonize("fasm/parser/antlr_to_tuple.pyx"),
- cmdclass={
- 'build_ext': AntlrCMakeBuild,
- 'build': BuildCommand,
- 'develop': DevelopCommand,
- 'install': InstallCommand,
- },
+ ext_modules=cythonize("fasm/parser/antlr_to_tuple.pyx"),
)
--
2.36.1
|