Skip to content

Commit cf740ab

Browse files
committed
pythonbuild: support reading environment variables from a file
This enables you to define persisted environment variables between builds. I'm now using this to make use of an S3 bucket for sccache storage. This speeds up my local builds dramatically. While I was here, I also documented how to configure sccache to speed up builds because it is a game changer.
1 parent 964718c commit cf740ab

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

docs/building.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,40 @@ If building CPython 3.8+, you will need to specify the path to a
109109

110110
To build a 32-bit x86 binary, simply use an ``x86 Native Tools
111111
Command Prompt`` instead of ``x64``.
112+
113+
Using sccache to Speed up Builds
114+
================================
115+
116+
Builds can take a long time. The bulk of the CPU time is bootstrapping a modern
117+
Clang toolchain, which requires building a modern GCC then potentially doing
118+
a 3 stage bootstrap of Clang.
119+
120+
python-build-standalone can automatically detect and use the
121+
`sccache <https://github.com/mozilla/sccache>`_ compiler cache to speed
122+
up subsequent builds on UNIX-like platforms. ``sccache`` can shave dozens
123+
of minutes from fresh builds, even on a 16 core CPU!
124+
125+
If there is an executable ``sccache`` in the source directory, it will
126+
automatically be copied into the build environment and used. For non-container
127+
builds, an ``sccache`` executable is also searched for on ``PATH``.
128+
129+
The ``~/.python-build-standalone-env`` file is read if it exists (the format is
130+
``key=value`` pairs) and variables are added to the build environment.
131+
132+
In addition, environment variables ``AWS_ACCESS_KEY_ID``,
133+
``AWS_SECRET_ACCESS_KEY``, and any variable beginning with ``SCCACHE_`` are
134+
automatically added to the build environment.
135+
136+
The environment variable support enables you to define remote build caches
137+
(such as S3 buckets) to provide a persistent, shared cache across builds and
138+
machines.
139+
140+
Keep in mind that when performing builds in containers in Linux (the default
141+
behavior), the local filesystem is local to the container and does not survive
142+
the build of a single package. So sccache is practically meaningless unless
143+
configured to use an external store (such as S3).
144+
145+
When using remote stores (such as S3), ``sccache`` can be constrained on
146+
network I/O. We recommend having at least a 100mbps network connection to
147+
a remote store and employing a network store with as little latency as possible
148+
for best results.

pythonbuild/utils.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,29 @@ def add_env_common(env):
391391
if "CI" in os.environ:
392392
env["CI"] = "1"
393393

394-
for k in (
395-
# Proxy variables used for sccache remote cache.
396-
"AWS_ACCESS_KEY_ID",
397-
"AWS_SECRET_ACCESS_KEY",
398-
"SCCACHE_BUCKET",
399-
"SCCACHE_S3_USE_SSL",
400-
):
394+
env_path = os.path.expanduser("~/.python-build-standalone-env")
395+
try:
396+
with open(env_path, "r") as fh:
397+
for line in fh:
398+
line = line.strip()
399+
if line.startswith("#"):
400+
continue
401+
402+
key, value = line.split("=", 1)
403+
404+
print("adding %s from %s" % (key, env_path))
405+
env[key] = value
406+
except FileNotFoundError:
407+
pass
408+
409+
# Proxy sccache settings.
410+
for k, v in os.environ.items():
411+
if k.startswith("SCCACHE_"):
412+
env[k] = v
413+
414+
# Proxy cloud provider credentials variables to enable sccache to
415+
# use stores in those providers.
416+
for k in ("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY"):
401417
if k in os.environ:
402418
env[k] = os.environ[k]
403419

0 commit comments

Comments
 (0)