-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmollybuild.py
More file actions
341 lines (294 loc) · 14 KB
/
Copy pathmollybuild.py
File metadata and controls
341 lines (294 loc) · 14 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import glob
import os
import platform
import random
import re
import shutil
import subprocess
import string
import sys
import sysconfig
import venv
from argparse import ArgumentParser, Namespace
from configparser import ConfigParser
from datetime import datetime
from pathlib import Path
from subprocess import PIPE
from typing import Any
from zipfile import ZipFile
def _get_dependencies(dependencies: list[str]):
pip_args = [ sys.executable, "-m", "pip", "install" ] + dependencies
pip_result = subprocess.run(pip_args)
pip_result.check_returncode()
def _get_mode_name(args_dict: dict[str, Any]): # pyright: ignore[reportExplicitAny]
mode_name: str | None = args_dict.get("mode", None)
return mode_name if mode_name != None else "debug"
def _get_toolchain_name(args_dict: dict[str, Any]): # pyright: ignore[reportExplicitAny]
default_toolchain: str | None
match sys.platform:
case "win32":
default_toolchain = "win32-msvc"
case "linux":
default_toolchain = "linux-gcc"
case _:
default_toolchain = None
print(
f"WARNING: Mollytime doesn't officially support your platform, '{sys.platform}'."
+ "\nMeson will try to find a working C++ toolchain, but it might fail.",
file = sys.stderr
)
toolchain_name: str | None = args_dict.get("toolchain", None)
return toolchain_name if toolchain_name != None else default_toolchain
# HACK: UGH: So, meson-python "helpfully" overrides some built-in Meson options:
# - `buildtype = release`
# - `b_ndebug = if-release`
# - `b_vscrt = md`
#
# See: https://mesonbuild.com/meson-python/explanations/default-options.html
#
# These overrides are passed in via the CLI. While meson-python ensures that our `setup-args`
# are passed in next, and thus theoretically given higher priority, that doesn't actually work
# in all cases: we define our build options using native files, which are always lower-priority
# than CLI args, regardless of their CLI position. That means our native file options are ignored.
# We have no recourse to prevent this. The meson-python CLI args are hardcoded.
# See: `mesonpy/__init__.py`, `_configure()`
#
# To work around this, see if our native files have any of the overridden values, and extract
# them to pass in as CLI overrides. The options we're interested in are close enough to .ini
# format that ConfigParser will suffice; we just need to strip the '' quotes from values.
def _HACK_extract_override_overrides(native_file: Path) -> list[str]:
args: list[str] = []
mode_config = ConfigParser()
_ = mode_config.read(native_file)
def _parse_arg(name: str):
arg = mode_config.get("built-in options", name, fallback = None)
if arg != None:
arg = arg.strip("'")
args.append(f"-Csetup-args=-D{name}={arg}")
_parse_arg("buildtype")
_parse_arg("b_ndebug")
_parse_arg("b_vscrt")
return args
def develop(modes: dict[str, Path], toolchains: dict[str, Path], args: Namespace):
# Grab dependencies.
_get_dependencies([ "meson", "meson-python", "ninja" ])
# Prepare to execute a local, editable `pip install`.
# This will have meson-python automatically run `meson setup`, and then add a launcher shim
# that automatically recompiles our extension module(s) when running the module locally.
args_dict = vars(args)
install_args = [ sys.executable, "-m", "pip", "install", "--no-build-isolation" ]
# This isn't that "verbose," it's just the normal `meson compile` output.
# We really need this, because it includes compile errors!
install_args += [ "-Ceditable-verbose=true" ]
# Gather mode config, if specified.
mode_name = _get_mode_name(args_dict)
mode_file = modes.get(mode_name, None)
if mode_file != None:
mode_arg = f"--native-file={mode_file.resolve()}"
install_args += [ f"-Csetup-args={mode_arg}" ]
install_args += _HACK_extract_override_overrides(mode_file)
# Gather toolchain config, if specified.
toolchain_name = _get_toolchain_name(args_dict)
if toolchain_name != None:
toolchain_file = toolchains.get(toolchain_name, None)
if toolchain_file != None:
toolchain_arg = f"--native-file={toolchain_file.resolve()}"
install_args += [ f"-Csetup-args={toolchain_arg}" ]
install_args += _HACK_extract_override_overrides(toolchain_file)
# Apply Meson options.
meson_options: list[str] = args_dict.get("meson_options", []) # pyright: ignore[reportAny]
for option in meson_options:
install_args += [f"-Csetup-args=-D{option}"]
# Go.
install_args += [ "-v", "--editable", "." ]
install_result = subprocess.run(install_args)
install_result.check_returncode()
# HACK: Meson-python isn't smart enough to expose subprojects' DLLs to an editable install.
# Meson, meanwhile, is too fussy to let us access and manipulate subproject output directly. (For our own good, of course.)
# Dig them out of the build folder, and put them where the program can find them.
major, minor, _ = platform.python_version().split(".")
build_dir = Path("build") / f"cp{major}{minor}"
subprojects_dir = build_dir / "subprojects"
for subproject_dir in subprojects_dir.iterdir():
for file in subproject_dir.iterdir():
if file.is_file() and file.suffix == ".dll":
output_path_expected = build_dir / file.name
output_path_actual = shutil.copy(file, output_path_expected)
print(f"Copied {file} to {output_path_actual}.")
def package(modes: dict[str, Path], toolchains: dict[str, Path], args: Namespace):
# Output here:
output_dir = Path("dist")
exe_archive_name: str = args.build_name # pyright: ignore[reportAny]
exe_archive_timestamp = datetime.today().strftime("%Y.%m.%d")
exe_archive_dir = output_dir / f"{exe_archive_name}-{exe_archive_timestamp}"
# Grab dependencies.
_get_dependencies([ "build" ])
# Prepare to execute `py(thon) -m build`.
args_dict = vars(args)
setup_args = [ sys.executable, "-m", "build", "--outdir", output_dir ]
# Always package in `release` mode.
mode_file = modes["release"]
mode_arg = f"--native-file={mode_file.resolve()}"
setup_args += [ f"-Csetup-args={mode_arg}" ]
# Gather toolchain config, if specified.
toolchain_name = _get_toolchain_name(args_dict)
if toolchain_name != None:
toolchain_file = toolchains.get(toolchain_name, None)
if toolchain_file != None:
toolchain_arg = f"--native-file={toolchain_file.resolve()}"
setup_args += [ f"-Csetup-args={toolchain_arg}" ]
# Apply Meson options.
meson_options: list[str] = args_dict.get("meson_options", []) # pyright: ignore[reportAny]
for option in meson_options:
setup_args += [f"-Csetup-args=-D{option}"]
# Build the source distribution & wheel.
package_result = subprocess.run(setup_args, stdout = PIPE)
try:
package_result.check_returncode()
except Exception as e:
print(package_result.stdout.decode())
raise e
# Dig out the name of the built wheel.
package_stdout = package_result.stdout.decode().strip()
package_report = package_stdout.splitlines()[-1]
package_report_pattern = re.compile(r"^Successfully built (.* and )?(?P<name>.+)")
if (match := package_report_pattern.match(package_report)):
wheel_name = match.group("name")
else:
print("Can't infer wheel name from package report. Scanning for any built wheels...")
maybe_wheel_names = glob.glob((output_dir / "*.whl").as_posix())
if len(maybe_wheel_names) < 1:
raise RuntimeError(f"Couldn't find any built *.whl files in {output_dir}.")
wheel_name = Path(maybe_wheel_names[0]).name
print(f"...Inferred {wheel_name}")
wheel_path = output_dir.resolve() / wheel_name
if not wheel_path.exists():
raise RuntimeError(f"I seem to have messed up finding the wheel. I think it's at: '{wheel_path}'")
print(f"Building a Pyinstaller executable from {wheel_path}...")
# Create a temporary directory to work in.
this_dir = Path(__file__).parent
temp_id = ''.join(random.choices(string.ascii_lowercase + string.digits, k = 8))
temp_dir = Path(f".mollybuild-package-{temp_id}")
try:
shutil.rmtree(temp_dir, ignore_errors = True)
os.makedirs(temp_dir)
# Create an isolated virtual environment.
venv_path = temp_dir / ".venv"
venv.EnvBuilder(with_pip = True).create(venv_path)
venv_config_vars = sysconfig.get_config_vars().copy()
venv_config_vars["base"] = venv_path.resolve()
venv_paths = sysconfig.get_paths(scheme = "venv", vars = venv_config_vars)
venv_scripts = Path(venv_paths["scripts"])
venv_python = (venv_scripts / "python.exe") if os.name == "nt" else "python"
# Install dependencies into the virtual environment.
venv_packages = [ wheel_path, "pyinstaller" ]
venv_pip_command = [ venv_python, "-Im", "pip", "install" ] + venv_packages
venv_pip_process = subprocess.run(venv_pip_command)
venv_pip_process.check_returncode()
# Unzip the wheel.
wheel_zip = ZipFile(wheel_path)
wheel_out_path = temp_dir / wheel_name
_ = wheel_zip.extractall(wheel_out_path)
# Copy the pyinstaller shim over, and build.
venv_pyinstaller_main = shutil.copy(this_dir / "pyinstaller_main.py", wheel_out_path / "pyinstaller_main.py")
venv_pyinstaller_command = [
(venv_scripts / "pyinstaller.exe") if os.name == "nt" else "pyinstaller",
"--distpath", exe_archive_dir,
"--specpath", temp_dir / ".pyinstaller",
"--workpath", temp_dir / ".pyinstaller" / "work",
"--onefile",
"--name", "mollytime",
"--icon", this_dir / "mollytime.ico",
"--copy-metadata", "mollytime",
"--collect-binaries", "mollytime",
"--collect-data", "mollytime",
venv_pyinstaller_main
]
venv_pyinstaller_process = subprocess.run(venv_pyinstaller_command)
venv_pyinstaller_process.check_returncode()
finally:
# Clean out the temporary work dir.
shutil.rmtree(temp_dir, ignore_errors = True)
print(f"Bundling Pyinstaller executable with supporting files...")
# Copy over supporting files.
for item in ( Path(item) for item in ("LICENSE.txt", "CONTRIBUTORS.txt", "examples") ):
if item.is_dir():
_ = shutil.copytree(this_dir / item, exe_archive_dir / item, dirs_exist_ok = True)
else:
_ = shutil.copyfile(this_dir / item, exe_archive_dir / item)
# Zip it up For Your Convenience.
archive = shutil.make_archive(str(output_dir / exe_archive_dir.name), "zip", exe_archive_dir)
print(f"Successfully packaged executable into '{Path(archive)}'.")
if __name__ == "__main__":
working_dir = Path(__file__).parent
os.chdir(working_dir)
# Parse config files, and map them to their identifying name.
config_dir = Path("build_native")
config_files = [ file for file in config_dir.iterdir() if file.suffix == ".ini" ]
modes = {
file.stem.removeprefix("mode-") : file
for file in config_files
if file.stem.startswith("mode-")
}
toolchains = {
file.stem.removeprefix("toolchain-") : file
for file in config_files
if file.stem.startswith("toolchain-")
}
# ---
# Main parser
parser = ArgumentParser(
prog = "mollybuild",
description = \
"Concise build helper." +
"\nFor an iterative development workflow, first run `develop`. Then, just run the project with `python -m mollytime`. C++ changes will be automatically recompiled when you run." +
"\nWhen you're ready to distribute, run `package` to build a wheel and Pyinstaller distribution.",
argument_default = "-h"
)
subparsers = parser.add_subparsers(title = "Commands")
# `develop` command
develop_parser = subparsers.add_parser("develop", help = "Build a working environment. Once complete, you can just run the project with `python -m mollytime`. C++ changes will be automatically recompiled when you run.")
develop_parser.set_defaults(command = develop)
_ = develop_parser.add_argument(
"--mode", "-m",
help = "Build mode. If unspecified, uses `debug`.",
choices = modes.keys(),
required = False
)
_ = develop_parser.add_argument(
"--toolchain", "-t",
help = "Toolchain to build with. If unspecified, uses `win32-msvc` on Windows, `linux-gcc` on Linux, and Meson's best guess on other platforms.",
choices = toolchains.keys(),
required = False
)
_ = develop_parser.add_argument(
"meson_options",
help = "Option overrides to pass to Meson, in the form `option=value`.",
nargs = "*"
)
# `package` command
package_parser = subparsers.add_parser("package", help = "Build a distributable Python package (sdist and wheel), and Pyinstaller executable.")
package_parser.set_defaults(command = package)
_ = package_parser.add_argument(
"--toolchain", "-t",
help = "Toolchain to build with. If unspecified, uses `win32-msvc` on Windows, `linux-gcc` on Linux, and Meson's best guess on other platforms.",
choices = toolchains.keys(),
required = False
)
_ = package_parser.add_argument(
"--build-name", "-n",
help = "Name for the executable archive. If unspecified, uses `mollytime`.",
default = "mollytime",
required = False
)
_ = package_parser.add_argument(
"meson_options",
help = "Option overrides to pass to Meson, in the form `option=value`.",
nargs = "*"
)
# Go
args = parser.parse_args()
if len(vars(args)) == 0:
parser.print_help()
exit(0)
args.command(modes, toolchains, args) # pyright: ignore[reportAny]