Skip to content

Commit 457dae1

Browse files
committed
linux: control PGO via command line argument
This is much more convenient.
1 parent 62a1f29 commit 457dae1

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Instructions
4747
To build a Python distribution for Linux x64::
4848

4949
$ ./build-linux.py
50+
# With profile-guided optimizations (generated code should be faster):
51+
$ ./build-linux.py --optimized
5052

5153
To build a Python distribution for macOS::
5254

build-linux.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# License, v. 2.0. If a copy of the MPL was not distributed with this
44
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
55

6+
import argparse
67
import datetime
78
import os
89
import pathlib
@@ -22,6 +23,11 @@
2223

2324

2425
def bootstrap():
26+
parser = argparse.ArgumentParser()
27+
parser.add_argument('--optimized', action='store_true')
28+
29+
args = parser.parse_args()
30+
2531
BUILD.mkdir(exist_ok=True)
2632
DIST.mkdir(exist_ok=True)
2733

@@ -33,6 +39,10 @@ def bootstrap():
3339
os.environ['PYBUILD_BOOTSTRAPPED'] = '1'
3440
os.environ['PATH'] = '%s:%s' % (str(VENV / 'bin'), os.environ['PATH'])
3541
os.environ['PYTHONPATH'] = str(ROOT)
42+
43+
if args.optimized:
44+
os.environ['PYBUILD_OPTIMIZED'] = '1'
45+
3646
subprocess.run([str(PYTHON), __file__], check=True)
3747

3848

@@ -46,9 +56,19 @@ def run():
4656
subprocess.run(['make'],
4757
cwd=str(MAKE_DIR), check=True)
4858

49-
source_path = BUILD / 'cpython-linux64.tar'
50-
dest_path = DIST / ('cpython-%s-linux64-%s.tar.zst' % (
51-
DOWNLOADS['cpython-3.7']['version'], now.strftime('%Y%m%dT%H%M')))
59+
basename = 'cpython-linux64'
60+
extra = ''
61+
62+
if 'PYBUILD_OPTIMIZED' in os.environ:
63+
basename += '-pgo'
64+
extra = '-pgo'
65+
66+
basename += '.tar'
67+
68+
source_path = BUILD / basename
69+
dest_path = DIST / ('cpython-%s-linux64%s-%s.tar.zst' % (
70+
DOWNLOADS['cpython-3.7']['version'], extra,
71+
now.strftime('%Y%m%dT%H%M')))
5272

5373
print('compressing Python archive to %s' % dest_path)
5474
with source_path.open('rb') as ifh, dest_path.open('wb') as ofh:

cpython-linux/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ TOOLCHAIN_DEPENDS := \
1717
$(OUTDIR)/clang-linux64.tar \
1818
$(NULL)
1919

20-
default: $(OUTDIR)/cpython-linux64.tar
20+
default: $(OUTDIR)/cpython-linux64$(if $(PYBUILD_OPTIMIZED),-pgo,).tar
2121

2222
$(OUTDIR)/image-%.tar: $(HERE)/%.Dockerfile $(COMMON_DEPENDS)
2323
$(BUILD) image-$*
@@ -88,3 +88,6 @@ PYTHON_DEPENDS := \
8888

8989
$(OUTDIR)/cpython-$(PLATFORM).tar: $(TOOLCHAIN_DEPENDS) $(HERE)/build-cpython.sh $(PYTHON_DEPENDS)
9090
$(BUILD) --platform $(PLATFORM) cpython
91+
92+
$(OUTDIR)/cpython-$(PLATFORM)-pgo.tar: $(TOOLCHAIN_DEPENDS) $(HERE)/build-cpython.sh $(PYTHON_DEPENDS)
93+
$(BUILD) --platform $(PLATFORM) --optimized cpython

cpython-linux/build.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ def process_setup_line(line):
532532
return bi
533533

534534

535-
def build_cpython(client, image, platform):
535+
def build_cpython(client, image, platform, optimized=False):
536536
"""Build CPythin in a Docker image'"""
537537
python_archive = download_entry('cpython-3.7', BUILD)
538538

@@ -587,10 +587,12 @@ def build_cpython(client, image, platform):
587587
archive_path='Makefile.extra')
588588

589589
env = {
590-
'CPYTHON_OPTIMIZED': '1',
591590
'PYTHON_VERSION': DOWNLOADS['cpython-3.7']['version'],
592591
}
593592

593+
if optimized:
594+
env['CPYTHON_OPTIMIZED'] = '1'
595+
594596
container_exec(container, '/build/build-cpython.sh',
595597
environment=env)
596598

@@ -617,7 +619,14 @@ def build_cpython(client, image, platform):
617619
'/build/out/python',
618620
archive_path='PYTHON.json')
619621

620-
dest_path = BUILD / ('cpython-%s.tar' % platform)
622+
basename = 'cpython-%s' % platform
623+
624+
if optimized:
625+
basename += '-pgo'
626+
627+
basename += '.tar'
628+
629+
dest_path = BUILD / basename
621630
data, stat = container.get_archive('/build/out/python')
622631

623632
with dest_path.open('wb') as fh:
@@ -637,17 +646,21 @@ def main():
637646

638647
parser = argparse.ArgumentParser()
639648
parser.add_argument('--platform')
649+
parser.add_argument('--optimized', action='store_true')
640650
parser.add_argument('action')
641651

642652
args = parser.parse_args()
643653

644654
action = args.action
645655

646-
log_path = BUILD / ('build.%s.log' % action)
647-
LOG_PREFIX[0] = action
656+
name = action
648657
if args.platform:
649-
log_path = BUILD / ('build.%s-%s.log' % (action, args.platform))
650-
LOG_PREFIX[0] = '%s-%s' % (action, args.platform)
658+
name += '-%s' % args.platform
659+
if args.optimized:
660+
name += '-pgo'
661+
662+
log_path = BUILD / ('build.%s.log' % name)
663+
LOG_PREFIX[0] = name
651664

652665
with log_path.open('wb') as log_fh:
653666
LOG_FH[0] = log_fh
@@ -676,7 +689,8 @@ def main():
676689
build_tcltk(client, get_image(client, 'build'), platform=args.platform)
677690

678691
elif action == 'cpython':
679-
build_cpython(client, get_image(client, 'build'), platform=args.platform)
692+
build_cpython(client, get_image(client, 'build'), platform=args.platform,
693+
optimized=args.optimized)
680694

681695
else:
682696
print('unknown build action: %s' % action)

0 commit comments

Comments
 (0)