Skip to content

Commit d1d08ea

Browse files
committed
unix: move most logic from build-*.py scripts to a new build-main.py
This allows us to consolidate logic for the UNIX build environment.
1 parent cf6426d commit d1d08ea

File tree

3 files changed

+121
-106
lines changed

3 files changed

+121
-106
lines changed

build-linux.py

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
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
7-
import datetime
86
import os
97
import pathlib
108
import subprocess
@@ -14,7 +12,6 @@
1412

1513
ROOT = pathlib.Path(os.path.abspath(__file__)).parent
1614
BUILD = ROOT / "build"
17-
DIST = ROOT / "dist"
1815
VENV = BUILD / "venv.linux"
1916
PIP = VENV / "bin" / "pip"
2017
PYTHON = VENV / "bin" / "python"
@@ -23,19 +20,7 @@
2320

2421

2522
def bootstrap():
26-
parser = argparse.ArgumentParser()
27-
parser.add_argument("--debug", action="store_true")
28-
parser.add_argument("--libressl", action="store_true")
29-
parser.add_argument("--musl", action="store_true")
30-
parser.add_argument("--optimized", action="store_true")
31-
parser.add_argument(
32-
"--python", default="cpython-3.7", help="name of Python to build"
33-
)
34-
35-
args = parser.parse_args()
36-
3723
BUILD.mkdir(exist_ok=True)
38-
DIST.mkdir(exist_ok=True)
3924

4025
venv.create(VENV, with_pip=True)
4126

@@ -45,57 +30,22 @@ def bootstrap():
4530
os.environ["PATH"] = "%s:%s" % (str(VENV / "bin"), os.environ["PATH"])
4631
os.environ["PYTHONPATH"] = str(ROOT)
4732

48-
if args.debug:
49-
os.environ["PYBUILD_DEBUG"] = "1"
50-
if args.libressl:
51-
os.environ["PYBUILD_LIBRESSL"] = "1"
52-
if args.musl:
53-
os.environ["PYBUILD_MUSL"] = "1"
54-
if args.optimized:
55-
os.environ["PYBUILD_OPTIMIZED"] = "1"
56-
os.environ["PYBUILD_PYTHON"] = args.python
57-
os.environ["PYBUILD_UNIX_PLATFORM"] = "linux64"
33+
args = [str(PYTHON), __file__, *sys.argv[1:]]
5834

59-
subprocess.run([str(PYTHON), __file__], check=True)
35+
subprocess.run(args, check=True)
6036

6137

6238
def run():
63-
from pythonbuild.downloads import DOWNLOADS
64-
from pythonbuild.utils import compress_python_archive
65-
66-
now = datetime.datetime.utcnow()
67-
6839
env = dict(os.environ)
6940
env["PYTHONUNBUFFERED"] = "1"
7041

71-
entry = DOWNLOADS[os.environ["PYBUILD_PYTHON"]]
72-
env["PYBUILD_PYTHON_VERSION"] = entry["version"]
73-
env["PYBUILD_PYTHON_MAJOR_VERSION"] = ".".join(entry["version"].split(".")[0:2])
74-
75-
subprocess.run(["make"], cwd=str(MAKE_DIR), env=env, check=True)
76-
77-
basename = "cpython-%s-linux64" % entry["version"]
78-
extra = ""
79-
80-
if "PYBUILD_MUSL" in os.environ:
81-
basename += "-musl"
82-
extra = "-musl"
83-
if "PYBUILD_DEBUG" in os.environ:
84-
basename += "-debug"
85-
extra += "-debug"
86-
if "PYBUILD_OPTIMIZED" in os.environ:
87-
basename += "-pgo"
88-
extra = "-pgo"
89-
90-
basename += ".tar"
91-
92-
source_path = BUILD / basename
93-
compress_python_archive(
94-
source_path,
95-
DIST,
96-
"cpython-%s-linux64%s-%s"
97-
% (entry["version"], extra, now.strftime("%Y%m%dT%H%M")),
98-
)
42+
args = [
43+
str(PYTHON),
44+
"build-main.py",
45+
*sys.argv[1:],
46+
]
47+
48+
subprocess.run(args, cwd=str(MAKE_DIR), env=env, check=True)
9949

10050

10151
if __name__ == "__main__":

build-macos.py

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
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
7-
import datetime
86
import os
97
import pathlib
108
import subprocess
@@ -14,7 +12,6 @@
1412

1513
ROOT = pathlib.Path(os.path.abspath(__file__)).parent
1614
BUILD = ROOT / "build"
17-
DIST = ROOT / "dist"
1815
VENV = BUILD / "venv.macos"
1916
PIP = VENV / "bin" / "pip"
2017
PYTHON = VENV / "bin" / "python"
@@ -23,16 +20,7 @@
2320

2421

2522
def bootstrap():
26-
parser = argparse.ArgumentParser()
27-
parser.add_argument("--optimized", action="store_true")
28-
parser.add_argument(
29-
"--python", default="cpython-3.7", help="name of Python to build"
30-
)
31-
32-
args = parser.parse_args()
33-
3423
BUILD.mkdir(exist_ok=True)
35-
DIST.mkdir(exist_ok=True)
3624

3725
venv.create(VENV, with_pip=True)
3826

@@ -42,49 +30,22 @@ def bootstrap():
4230
os.environ["PATH"] = "%s:%s" % (str(VENV / "bin"), os.environ["PATH"])
4331
os.environ["PYTHONPATH"] = str(ROOT)
4432

45-
if args.optimized:
46-
os.environ["PYBUILD_OPTIMIZED"] = "1"
47-
48-
os.environ["PYBUILD_PYTHON"] = args.python
33+
args = [str(PYTHON), __file__, *sys.argv[1:]]
4934

50-
subprocess.run([str(PYTHON), __file__], check=True)
35+
subprocess.run(args, check=True)
5136

5237

5338
def run():
54-
from pythonbuild.downloads import DOWNLOADS
55-
from pythonbuild.utils import compress_python_archive
56-
57-
now = datetime.datetime.utcnow()
58-
5939
env = dict(os.environ)
6040
env["PYTHONUNBUFFERED"] = "1"
6141

62-
entry = DOWNLOADS[os.environ["PYBUILD_PYTHON"]]
63-
env["PYBUILD_PYTHON_VERSION"] = entry["version"]
64-
env["PYBUILD_PYTHON_MAJOR_VERSION"] = ".".join(entry["version"].split(".")[0:2])
65-
66-
env["PYBUILD_NO_DOCKER"] = "1"
67-
env["PYBUILD_UNIX_PLATFORM"] = "macos"
68-
69-
subprocess.run(["make"], cwd=str(MAKE_DIR), env=env, check=True)
70-
71-
basename = "cpython-%s-macos" % entry["version"]
72-
extra = ""
73-
74-
if "PYBUILD_OPTIMIZED" in os.environ:
75-
basename += "-pgo"
76-
extra = "-pgo"
77-
78-
basename += ".tar"
79-
80-
source_path = BUILD / basename
42+
args = [
43+
str(PYTHON),
44+
"build-main.py",
45+
*sys.argv[1:],
46+
]
8147

82-
compress_python_archive(
83-
source_path,
84-
DIST,
85-
"cpython-%s-macos%s-%s"
86-
% (entry["version"], extra, now.strftime("%Y%m%dT%H%M")),
87-
)
48+
subprocess.run(args, cwd=str(MAKE_DIR), env=env, check=True)
8849

8950

9051
if __name__ == "__main__":

cpython-unix/build-main.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env python3
2+
# This Source Code Form is subject to the terms of the Mozilla Public
3+
# License, v. 2.0. If a copy of the MPL was not distributed with this
4+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
6+
import argparse
7+
import datetime
8+
import os
9+
import pathlib
10+
import subprocess
11+
import sys
12+
13+
from pythonbuild.downloads import DOWNLOADS
14+
from pythonbuild.utils import compress_python_archive
15+
16+
ROOT = pathlib.Path(os.path.abspath(__file__)).parent.parent
17+
BUILD = ROOT / "build"
18+
DIST = ROOT / "dist"
19+
20+
21+
def main():
22+
parser = argparse.ArgumentParser()
23+
parser.add_argument("--debug", action="store_true", help="Produce a debug build")
24+
parser.add_argument(
25+
"--libressl", action="store_true", help="Build LibreSSL instead of OpenSSL"
26+
)
27+
parser.add_argument(
28+
"--musl", action="store_true", help="Build against musl libc (Linux only)"
29+
)
30+
parser.add_argument(
31+
"--optimized", action="store_true", help="Build an optimized build"
32+
)
33+
parser.add_argument(
34+
"--python",
35+
choices={"cpython-3.7", "cpython-3.8"},
36+
default="cpython-3.7",
37+
help="Python distribution to build",
38+
)
39+
parser.add_argument(
40+
"--no-docker",
41+
action="store_true",
42+
default=True if sys.platform == "darwin" else False,
43+
help="Disable building in Docker",
44+
)
45+
46+
args = parser.parse_args()
47+
48+
env = dict(os.environ)
49+
50+
if args.debug:
51+
env["PYBUILD_DEBUG"] = "1"
52+
if args.libressl:
53+
env["PYBUILD_LIBRESSL"] = "1"
54+
if args.musl:
55+
env["PYBUILD_MUSL"] = "1"
56+
if args.optimized:
57+
env["PYBUILD_OPTIMIZED"] = "1"
58+
if args.no_docker:
59+
env["PYBUILD_NO_DOCKER"] = "1"
60+
61+
if sys.platform == "linux":
62+
platform = "linux64"
63+
elif sys.platform == "darwin":
64+
platform = "macos"
65+
else:
66+
raise Exception("unhandled platform")
67+
68+
env["PYBUILD_UNIX_PLATFORM"] = platform
69+
70+
entry = DOWNLOADS[args.python]
71+
env["PYBUILD_PYTHON_VERSION"] = entry["version"]
72+
env["PYBUILD_PYTHON_MAJOR_VERSION"] = ".".join(entry["version"].split(".")[0:2])
73+
74+
now = datetime.datetime.utcnow()
75+
76+
subprocess.run(["make"], env=env, check=True)
77+
78+
basename = "cpython-%s-%s" % (entry["version"], platform)
79+
extra = ""
80+
81+
if args.musl:
82+
basename += "-musl"
83+
extra = "-musl"
84+
if args.debug:
85+
basename += "-debug"
86+
extra += "-debug"
87+
if args.optimized:
88+
basename += "-pgo"
89+
extra += "-pgo"
90+
91+
basename += ".tar"
92+
93+
DIST.mkdir(exist_ok=True)
94+
95+
compress_python_archive(
96+
BUILD / basename,
97+
DIST,
98+
"cpython-%s-%s%s-%s"
99+
% (entry["version"], platform, extra, now.strftime("%Y%m%dT%H:%M")),
100+
)
101+
102+
103+
if __name__ == "__main__":
104+
sys.exit(main())

0 commit comments

Comments
 (0)