Skip to content

Commit af258c5

Browse files
committed
enable gdb python scripting
1 parent aa9c710 commit af258c5

15 files changed

Lines changed: 653 additions & 7 deletions

File tree

doc/wsl-buildenv.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ The build script requires root privileges, so AN EXCLUSIVE DISTRO is highly reco
2323
```bash
2424
apk update
2525
apk add \
26-
autoconf automake bison build-base flex libtool rsync texinfo \
27-
ca-certificates curl file gawk libarchive-tools py3-packaging python3 zstd
26+
autoconf automake bison build-base flex libtool rsync texinfo xmake \
27+
7zip ca-certificates curl file gawk libarchive-tools py3-packaging python3 zstd
2828
```
2929

3030
## Clone the Repository

main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# B = x86_64-w64-mingw32
1919
# C = {aarch64,x86_64}-linux-gnu
2020
# XYZ: build = X, host = Y, target = Z
21-
from module.AAA import build_AAA_make, build_AAA_library
21+
from module.AAA import build_AAA_make, build_AAA_library, build_AAA_python
2222
from module.AAB import build_AAB_compiler, build_AAB_library
2323
from module.AAC import build_AAC_compiler, build_AAC_library
2424
from module.ABB import build_ABB_toolchain
@@ -97,6 +97,7 @@ def main():
9797
os.environ['PATH'] = f'{paths.x_prefix}/bin:{os.environ["PATH"]}'
9898
if not config.no_cross:
9999
build_AAA_library(ver, paths, config)
100+
build_AAA_python(ver, paths, config)
100101
build_AAC_compiler('x86_64', ver, paths, config)
101102
build_AAC_library('x86_64', ver, paths, config)
102103
build_AAC_compiler('aarch64', ver, paths, config)

module/AAA.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,45 @@ def _mpc(ver: BranchProfile, paths: ProjectPaths, config: argparse.Namespace):
8181
make_default('mpc', build_dir, config.jobs)
8282
make_install('mpc', build_dir)
8383

84+
def _python_z(ver: BranchProfile, paths: ProjectPaths, config: argparse.Namespace):
85+
build_dir = paths.python_z / 'build-AAA'
86+
ensure(build_dir)
87+
configure('zlib for python', build_dir, [
88+
'--prefix=',
89+
'--static',
90+
])
91+
make_default('zlib for python', build_dir, config.jobs)
92+
make_destdir_install('zlib for python', build_dir, paths.x_dep)
93+
8494
def build_AAA_library(ver: BranchProfile, paths: ProjectPaths, config: argparse.Namespace):
8595
_gmp(ver.gmp, paths, config)
8696

8797
_mpfr(ver.mpfr, paths, config)
8898

8999
_mpc(ver.mpc, paths, config)
100+
101+
if ver.python:
102+
_python_z(ver, paths, config)
103+
104+
def _python(ver: BranchProfile, paths: ProjectPaths, config: argparse.Namespace):
105+
build_dir = paths.python / 'build-AAA'
106+
ensure(build_dir)
107+
configure('python', build_dir, [
108+
f'--prefix={paths.x_prefix}',
109+
# static
110+
'--disable-shared',
111+
'MODULE_BUILDTYPE=static',
112+
# features
113+
'--disable-test-modules',
114+
# packages
115+
'--without-static-libpython',
116+
f'ZLIB_CFLAGS=-I{paths.x_dep}/include',
117+
f'ZLIB_LIBS=-L{paths.x_dep}/lib -lz',
118+
*cflags_A(ld_extra = ['-static']),
119+
])
120+
make_custom('python', build_dir, ['LDFLAGS=-static', 'LINKFORSHARED= '], config.jobs)
121+
make_install('python', build_dir)
122+
123+
def build_AAA_python(ver: BranchProfile, paths: ProjectPaths, config: argparse.Namespace):
124+
if ver.python:
125+
_python(ver, paths, config)

module/AAB.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import argparse
22
from packaging.version import Version
3+
import shutil
4+
import subprocess
35

46
from module.debug import shell_here
57
from module.path import ProjectPaths
@@ -247,6 +249,49 @@ def _gettext(ver: BranchProfile, paths: ProjectPaths, config: argparse.Namespace
247249
make_default('gettext', build_dir, config.jobs)
248250
make_destdir_install('gettext', build_dir, paths.x_prefix / 'x86_64-w64-mingw32')
249251

252+
def _python(ver: BranchProfile, paths: ProjectPaths, config: argparse.Namespace):
253+
res = subprocess.run([
254+
'xmake', 'config', '--root',
255+
'-p', 'mingw',
256+
'-a', 'x86_64',
257+
f'--mingw={paths.x_prefix}',
258+
f'--cross=x86_64-w64-mingw32-',
259+
], cwd = paths.python)
260+
if res.returncode != 0:
261+
raise Exception('xmake config failed')
262+
res = subprocess.run([
263+
'xmake', 'build', '--root',
264+
'-j', str(config.jobs),
265+
], cwd = paths.python)
266+
if res.returncode != 0:
267+
raise Exception('xmake build failed')
268+
res = subprocess.run([
269+
'xmake', 'install', '--root',
270+
'-o', paths.x_prefix / 'x86_64-w64-mingw32',
271+
], cwd = paths.python)
272+
if res.returncode != 0:
273+
raise Exception('xmake install failed')
274+
275+
def _python_packages(ver: BranchProfile, paths: ProjectPaths, config: argparse.Namespace):
276+
x_prefix_mingw = paths.x_prefix / 'x86_64-w64-mingw32'
277+
python_lib = x_prefix_mingw / 'Lib'
278+
python_lib_zip = x_prefix_mingw / 'lib' / 'python.zip'
279+
shutil.copytree(paths.x_prefix / 'share' / f'gcc-{config.branch}' / 'python', python_lib, dirs_exist_ok = True)
280+
subprocess.run([
281+
'python3', '-m', 'compileall',
282+
'-b',
283+
'-o', '2',
284+
'.',
285+
], check = True, cwd = python_lib)
286+
if python_lib_zip.exists():
287+
python_lib_zip.unlink()
288+
subprocess.run([
289+
'7z', 'a', '-tzip',
290+
'-mx0', # no compression, reduce final size
291+
python_lib_zip,
292+
'*', '-xr!__pycache__', '-xr!*.py',
293+
], check = True, cwd = python_lib)
294+
250295
def build_AAB_library(ver: BranchProfile, paths: ProjectPaths, config: argparse.Namespace):
251296
_gmp(ver, paths, config)
252297

@@ -256,3 +301,7 @@ def build_AAB_library(ver: BranchProfile, paths: ProjectPaths, config: argparse.
256301

257302
if ver.gettext:
258303
_gettext(ver, paths, config)
304+
305+
if ver.python:
306+
_python(ver, paths, config)
307+
_python_packages(ver, paths, config)

module/ABB.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,12 @@ def _gdb(ver: BranchProfile, paths: ProjectPaths, config: argparse.Namespace):
146146
build_dir = paths.gdb / 'build-ABB'
147147
ensure(build_dir)
148148

149+
python_flags = []
149150
c_extra = []
150151

152+
if ver.python:
153+
python_flags.append(f'--with-python={paths.x_prefix}/x86_64-w64-mingw32/python-config.sh')
154+
151155
# GCC 15 defaults to C23, in which `foo()` means `foo(void)` instead of `foo(...)`.
152156
if v_gcc.major >= 15 and v < Version('16.3'):
153157
c_extra.append('-std=gnu11')
@@ -161,6 +165,7 @@ def _gdb(ver: BranchProfile, paths: ProjectPaths, config: argparse.Namespace):
161165
'--disable-tui',
162166
# packages
163167
f'--with-system-gdbinit=/share/gdb/gdbinit',
168+
*python_flags,
164169
*cflags_B(
165170
common_extra = ['-DPDC_WIDE'],
166171
c_extra = c_extra
@@ -169,6 +174,16 @@ def _gdb(ver: BranchProfile, paths: ProjectPaths, config: argparse.Namespace):
169174
make_default('gdb', build_dir, config.jobs)
170175
make_destdir_install('gdb', build_dir, paths.mingw_prefix)
171176

177+
if ver.python:
178+
shutil.copy(paths.x_prefix / 'x86_64-w64-mingw32' / 'lib' / 'python.zip', paths.mingw_prefix / 'lib' / 'python.zip')
179+
with open(paths.mingw_prefix / 'bin' / 'gdb._pth', 'w') as f:
180+
f.write('../lib/python.zip\n')
181+
with open(paths.mingw_prefix / 'share' / 'gdb' / 'gdbinit', 'w') as f:
182+
f.write('python\n')
183+
f.write('from libstdcxx.v6.printers import register_libstdcxx_printers\n')
184+
f.write('register_libstdcxx_printers(None)\n')
185+
f.write('end\n')
186+
172187
def _gmake(ver: BranchProfile, paths: ProjectPaths, config: argparse.Namespace):
173188
v = Version(ver.make)
174189
v_gcc = Version(ver.gcc)

module/ABC.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import argparse
22
import glob
33
import os
4-
import shutil
54
from packaging.version import Version
5+
import shutil
6+
import subprocess
67

78
from module.debug import shell_here
89
from module.path import ProjectPaths
@@ -148,8 +149,12 @@ def _gdb(arch: str, ver: BranchProfile, paths: ProjectPaths, config: argparse.Na
148149
build_dir = paths.gdb / f'build-ABC-{arch}'
149150
ensure(build_dir)
150151

152+
python_flags = []
151153
c_extra = []
152154

155+
if ver.python:
156+
python_flags.append(f'--with-python={paths.x_prefix}/x86_64-w64-mingw32/python-config.sh')
157+
153158
# GCC 15 defaults to C23, in which `foo()` means `foo(void)` instead of `foo(...)`.
154159
if v_gcc.major >= 15 and v < Version('16.3'):
155160
c_extra.append('-std=gnu11')
@@ -168,6 +173,8 @@ def _gdb(arch: str, ver: BranchProfile, paths: ProjectPaths, config: argparse.Na
168173
'--disable-tui',
169174
# packages
170175
'--without-gdbserver',
176+
'--with-system-gdbinit=/share/gdb/gdbinit',
177+
*python_flags,
171178
# libtool eats `-static`
172179
*cflags_B(
173180
c_extra = c_extra,
@@ -177,6 +184,16 @@ def _gdb(arch: str, ver: BranchProfile, paths: ProjectPaths, config: argparse.Na
177184
make_default('gdb', build_dir, config.jobs)
178185
make_destdir_install('gdb', build_dir, paths.linux_prefix(arch))
179186

187+
if ver.python:
188+
shutil.copy(paths.x_prefix / 'x86_64-w64-mingw32' / 'lib' / 'python.zip', paths.linux_prefix(arch) / 'lib' / 'python.zip')
189+
with open(paths.linux_prefix(arch) / 'bin' / 'gdb._pth', 'w') as f:
190+
f.write('../lib/python.zip\n')
191+
with open(paths.linux_prefix(arch) / 'share' / 'gdb' / 'gdbinit', 'w') as f:
192+
f.write('python\n')
193+
f.write('from libstdcxx.v6.printers import register_libstdcxx_printers\n')
194+
f.write('register_libstdcxx_printers(None)\n')
195+
f.write('end\n')
196+
180197
def _gmake(arch: str, ver: BranchProfile, paths: ProjectPaths, config: argparse.Namespace):
181198
v = Version(ver.make)
182199
v_gcc = Version(ver.gcc)

module/checksum.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from typing import Dict
22

33
CHECKSUMS: Dict[str, str] = {
4+
'Python-3.13.2.tar.xz': 'd984bcc57cd67caab26f7def42e523b1c015bbc5dc07836cf4f0b63fa159eb56',
5+
46
'binutils-2.24.tar.bz2': 'e5e8c5be9664e7f7f96e0d09919110ab5ad597794f5b1809871177a0f0f14137',
57
'binutils-2.25.1.tar.bz2': 'b5b14added7d78a8d1ca70b5cb75fef57ce2197264f4f5835326b0df22ac9f22',
68
'binutils-2.27.tar.bz2': '369737ce51587f92466041a97ab7d2358c6d9e1b6490b3940eb09fb0a9a6ac88',
@@ -105,4 +107,6 @@
105107
'mpfr-4.2.1.tar.xz': '277807353a6726978996945af13e52829e3abd7a9a5b7fb2793894e18f1fcbb2',
106108

107109
'xmake-v2.9.8.win64.zip': '5ba90e491f911b86dfc53c13cd9c849ea777567075d655641ed6028fcb956e35',
110+
111+
'zlib-1.3.1.tar.gz': '9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23',
108112
}

module/path.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class ProjectPaths:
3636
mingw: Path
3737
mpc: Path
3838
mpfr: Path
39+
python: Path
40+
python_z: Path
3941

4042
binutils_arx: Path
4143
gcc_arx: Path
@@ -48,6 +50,8 @@ class ProjectPaths:
4850
mingw_arx: Path
4951
mpc_arx: Path
5052
mpfr_arx: Path
53+
python_arx: Path
54+
python_z_arx: Path
5155

5256
# test phase
5357

@@ -160,6 +164,19 @@ def __init__(
160164
self.mpfr = self.build / mpfr
161165
self.mpfr_arx = self.assets / f'{mpfr}.tar.xz'
162166

167+
if ver.python:
168+
python = f'Python-{ver.python}'
169+
python_z = f'zlib-{ver.python_z}'
170+
self.python = self.build / python
171+
self.python_arx = self.assets / f'{python}.tar.xz'
172+
self.python_z = self.build / python / python_z
173+
self.python_z_arx = self.assets / f'{python_z}.tar.gz'
174+
else:
175+
self.python = None
176+
self.python_arx = None
177+
self.python_z = None
178+
self.python_z_arx = None
179+
163180
# test phase
164181

165182
self.test = Path(f'/tmp/test/gcc-{config.branch}')

module/prepare_source.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
from packaging.version import Version
44
from pathlib import Path
5-
from shutil import copyfile
5+
import shutil
66
import subprocess
77

88
from module.fetch import validate_and_download, check_and_extract
@@ -223,6 +223,10 @@ def _gdb(ver: BranchProfile, paths: ProjectPaths):
223223
elif v == Version('7.6.2'):
224224
_patch(paths.gdb, paths.patch / 'gdb' / 'backport-stub-termcap_7.6.2.patch')
225225

226+
# Fix pythondir
227+
if ver.python:
228+
_patch(paths.gdb, paths.patch / 'gdb' / 'fix-pythondir.patch')
229+
226230
_patch_done(paths.gdb)
227231

228232
def _gettext(ver: BranchProfile, paths: ProjectPaths):
@@ -302,6 +306,19 @@ def _mpfr(ver: BranchProfile, paths: ProjectPaths):
302306
check_and_extract(paths.mpfr, paths.mpfr_arx)
303307
_patch_done(paths.mpfr)
304308

309+
def _python(ver: BranchProfile, paths: ProjectPaths):
310+
url = f'https://www.python.org/ftp/python/{ver.python}/{paths.python_arx.name}'
311+
z_url = f'https://zlib.net/fossils/{paths.python_z_arx.name}'
312+
validate_and_download(paths.python_arx, url)
313+
validate_and_download(paths.python_z_arx, z_url)
314+
if check_and_extract(paths.python, paths.python_arx):
315+
check_and_extract(paths.python_z, paths.python_z_arx)
316+
os.symlink(paths.python_z, paths.python / 'zlib', target_is_directory = True)
317+
shutil.copy(paths.patch / 'python' / 'xmake.lua', paths.python / 'xmake.lua')
318+
shutil.copy(paths.patch / 'python' / 'python-config.sh', paths.python / 'python-config.sh')
319+
_patch(paths.python, paths.patch / 'python' / 'fix-mingw-build.patch')
320+
_patch_done(paths.python)
321+
305322
def prepare_source(ver: BranchProfile, paths: ProjectPaths):
306323
_binutils(ver, paths)
307324
_gcc(ver, paths)
@@ -315,3 +332,5 @@ def prepare_source(ver: BranchProfile, paths: ProjectPaths):
315332
_mingw(ver, paths)
316333
_mpc(ver, paths)
317334
_mpfr(ver, paths)
335+
if ver.python:
336+
_python(ver, paths)

0 commit comments

Comments
 (0)