Skip to content

Commit 8cb609d

Browse files
committed
linux: support building debug builds of Python
Debug builds can be useful for debugging built binaries.
1 parent 6545c75 commit 8cb609d

File tree

5 files changed

+45
-7
lines changed

5 files changed

+45
-7
lines changed

build-linux.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
def bootstrap():
2626
parser = argparse.ArgumentParser()
27+
parser.add_argument('--debug', action='store_true')
2728
parser.add_argument('--musl', action='store_true')
2829
parser.add_argument('--optimized', action='store_true')
2930

@@ -41,6 +42,8 @@ def bootstrap():
4142
os.environ['PATH'] = '%s:%s' % (str(VENV / 'bin'), os.environ['PATH'])
4243
os.environ['PYTHONPATH'] = str(ROOT)
4344

45+
if args.debug:
46+
os.environ['PYBUILD_DEBUG'] = '1'
4447
if args.musl:
4548
os.environ['PYBUILD_MUSL'] = '1'
4649
if args.optimized:
@@ -64,6 +67,9 @@ def run():
6467
if 'PYBUILD_MUSL' in os.environ:
6568
basename += '-musl'
6669
extra = '-musl'
70+
if 'PYBUILD_DEBUG' in os.environ:
71+
basename += '-debug'
72+
extra += '-debug'
6773
if 'PYBUILD_OPTIMIZED' in os.environ:
6874
basename += '-pgo'
6975
extra = '-pgo'

cpython-linux/Makefile

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ ifdef PYBUILD_MUSL
1515
PLATFORM := $(PLATFORM)-musl
1616
endif
1717

18+
ifdef PYBUILD_DEBUG
19+
DEFAULT_EXTRA := -debug
20+
else
21+
ifdef PYBUILD_OPTIMIZED
22+
DEFAULT_EXTRA := -pgo
23+
else
24+
DEFAULT_EXTRA :=
25+
endif
26+
endif
27+
1828
BASE_TOOLCHAIN_DEPENDS := \
1929
$(OUTDIR)/binutils-linux64.tar \
2030
$(OUTDIR)/gcc-linux64.tar \
@@ -31,7 +41,7 @@ PYTHON_DEP_DEPENDS := \
3141
$(TOOLCHAIN_DEPENDS) \
3242
$(NULL)
3343

34-
default: $(OUTDIR)/cpython-$(PLATFORM)$(if $(PYBUILD_OPTIMIZED),-pgo,).tar
44+
default: $(OUTDIR)/cpython-$(PLATFORM)$(DEFAULT_EXTRA).tar
3545

3646
$(OUTDIR)/image-%.tar: $(HERE)/%.Dockerfile $(COMMON_DEPENDS)
3747
$(BUILD) image-$*
@@ -106,5 +116,8 @@ PYTHON_DEPENDS := \
106116
$(OUTDIR)/cpython-$(PLATFORM).tar: $(PYTHON_DEP_DEPENDS) $(HERE)/build-cpython.sh $(PYTHON_DEPENDS)
107117
$(BUILD) --platform $(PLATFORM) cpython
108118

119+
$(OUTDIR)/cpython-$(PLATFORM)-debug.tar: $(PYTHON_DEP_DEPENDS) $(HERE)/build-cpython.sh $(PYTHON_DEPENDS)
120+
$(BUILD) --platform $(PLATFORM) --debug cpython
121+
109122
$(OUTDIR)/cpython-$(PLATFORM)-pgo.tar: $(PYTHON_DEP_DEPENDS) $(HERE)/build-cpython.sh $(PYTHON_DEPENDS)
110123
$(BUILD) --platform $(PLATFORM) --optimized cpython

cpython-linux/build-cpython.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ fi
7575

7676
CONFIGURE_FLAGS="--prefix=/install --with-openssl=/tools/deps --without-ensurepip"
7777

78+
if [ -n "${CPYTHON_DEBUG}" ]; then
79+
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} --with-pydebug"
80+
fi
81+
7882
# TODO support --with-lto
7983
# --with-lto will produce .o files that are LLVM bitcode and aren't compatible
8084
# with downstream consumers that can't handle them.

cpython-linux/build.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ def process_setup_line(line, variant=None):
629629
return bi
630630

631631

632-
def build_cpython(client, image, platform, optimized=False, musl=False):
632+
def build_cpython(client, image, platform, debug=False, optimized=False, musl=False):
633633
"""Build CPython in a Docker image'"""
634634
python_archive = download_entry('cpython-3.7', BUILD)
635635
setuptools_archive = download_entry('setuptools', BUILD)
@@ -639,7 +639,7 @@ def build_cpython(client, image, platform, optimized=False, musl=False):
639639
static_modules_lines = [l.rstrip() for l in fh if not l.startswith(b'#')]
640640

641641
setup = derive_setup_local(static_modules_lines, python_archive,
642-
musl=musl)
642+
musl=musl, debug=debug)
643643

644644
config_c_in = parse_config_c(setup['config_c_in'].decode('utf-8'))
645645
setup_dist_content = setup['setup_dist']
@@ -706,21 +706,27 @@ def build_cpython(client, image, platform, optimized=False, musl=False):
706706
if musl:
707707
env['CC'] = 'musl-clang'
708708

709+
if debug:
710+
env['CPYTHON_DEBUG'] = '1'
709711
if optimized:
710712
env['CPYTHON_OPTIMIZED'] = '1'
711713

712714
container_exec(container, '/build/build-cpython.sh',
713715
environment=env)
714716

717+
fully_qualified_name = 'python%s%sm' % (
718+
'3.7', 'd' if debug else ''
719+
)
720+
715721
# Create PYTHON.json file describing this distribution.
716722
python_info = {
717723
'version': '2',
718724
'os': 'linux',
719725
'arch': 'x86_64',
720726
'python_flavor': 'cpython',
721727
'python_version': DOWNLOADS['cpython-3.7']['version'],
722-
'python_exe': 'install/bin/python3.7',
723-
'python_include': 'install/include/python3.7m',
728+
'python_exe': 'install/bin/%s' % fully_qualified_name,
729+
'python_include': 'install/include/%s' % fully_qualified_name,
724730
'python_stdlib': 'install/lib/python3.7',
725731
'build_info': python_build_info(container, config_c_in,
726732
setup_dist_content, setup_local_content),
@@ -740,6 +746,8 @@ def build_cpython(client, image, platform, optimized=False, musl=False):
740746

741747
if musl:
742748
basename += '-musl'
749+
if debug:
750+
basename += '-debug'
743751
if optimized:
744752
basename += '-pgo'
745753

@@ -764,6 +772,7 @@ def main():
764772
return 1
765773

766774
parser = argparse.ArgumentParser()
775+
parser.add_argument('--debug', action='store_true')
767776
parser.add_argument('--platform')
768777
parser.add_argument('--optimized', action='store_true')
769778
parser.add_argument('action')
@@ -775,6 +784,8 @@ def main():
775784
name = action
776785
if args.platform:
777786
name += '-%s' % args.platform
787+
if args.debug:
788+
name += '-debug'
778789
if args.optimized:
779790
name += '-pgo'
780791

@@ -823,7 +834,7 @@ def main():
823834

824835
elif action == 'cpython':
825836
build_cpython(client, get_image(client, 'build'), platform=platform,
826-
musl=musl, optimized=args.optimized)
837+
musl=musl, debug=args.debug, optimized=args.optimized)
827838

828839
else:
829840
print('unknown build action: %s' % action)

pythonbuild/cpython.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def parse_setup_line(line: bytes, variant: str):
116116

117117

118118
def derive_setup_local(static_modules_lines, cpython_source_archive, disabled=None,
119-
musl=False):
119+
musl=False, debug=False):
120120
"""Derive the content of the Modules/Setup.local file."""
121121
python_version = DOWNLOADS['cpython-3.7']['version']
122122

@@ -134,6 +134,10 @@ def derive_setup_local(static_modules_lines, cpython_source_archive, disabled=No
134134
disabled.add(b'nis')
135135
disabled.add(b'ossaudiodev')
136136

137+
if debug:
138+
# Doesn't work in debug builds.
139+
disabled.add(b'xxlimited')
140+
137141
with tarfile.open(str(cpython_source_archive)) as tf:
138142
ifh = tf.extractfile('Python-%s/Modules/Setup.dist' % python_version)
139143
source_lines = ifh.readlines()

0 commit comments

Comments
 (0)