2019-07-18 23:50:34 +02:00
|
|
|
# Copyright 2019 MongoDB Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import math
|
|
|
|
import os
|
2019-12-26 21:49:40 +01:00
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
|
2019-07-18 23:50:34 +02:00
|
|
|
import SCons
|
2019-12-26 21:49:40 +01:00
|
|
|
from pkg_resources import parse_version
|
|
|
|
|
|
|
|
# This is the oldest version of ccache that offers support for -gsplit-dwarf
|
2020-01-06 18:03:45 +01:00
|
|
|
_ccache_version_min = parse_version("3.2.3")
|
2019-12-26 21:49:40 +01:00
|
|
|
_ccache_version_found = None
|
2019-07-18 23:50:34 +02:00
|
|
|
|
2020-01-06 18:03:45 +01:00
|
|
|
|
2019-07-18 23:50:34 +02:00
|
|
|
def exists(env):
|
2019-12-26 21:49:40 +01:00
|
|
|
"""Look for a viable ccache implementation that meets our version requirements."""
|
|
|
|
|
|
|
|
# If we already generated, we definitely exist
|
2020-01-06 18:03:45 +01:00
|
|
|
if "CCACHE_VERSION" in env:
|
2019-12-26 21:49:40 +01:00
|
|
|
return True
|
|
|
|
|
2020-01-06 18:03:45 +01:00
|
|
|
ccache = env.get("CCACHE", False)
|
2019-12-26 21:49:40 +01:00
|
|
|
if not ccache:
|
|
|
|
return False
|
|
|
|
|
|
|
|
ccache = env.WhereIs(ccache)
|
|
|
|
if not ccache:
|
|
|
|
return False
|
|
|
|
|
2020-01-06 18:03:45 +01:00
|
|
|
pipe = SCons.Action._subproc(
|
|
|
|
env,
|
|
|
|
SCons.Util.CLVar(ccache) + ["--version"],
|
|
|
|
stdin="devnull",
|
|
|
|
stderr="devnull",
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
)
|
2019-12-26 21:49:40 +01:00
|
|
|
|
|
|
|
if pipe.wait() != 0:
|
|
|
|
return False
|
2019-07-18 23:50:34 +02:00
|
|
|
|
2019-12-26 21:49:40 +01:00
|
|
|
validated = False
|
|
|
|
for line in pipe.stdout:
|
2020-01-06 18:03:45 +01:00
|
|
|
line = line.decode("utf-8")
|
2019-12-26 21:49:40 +01:00
|
|
|
if validated:
|
|
|
|
continue # consume all data
|
2020-01-06 18:03:45 +01:00
|
|
|
version_banner = re.search(r"^ccache version", line)
|
2019-12-26 21:49:40 +01:00
|
|
|
if not version_banner:
|
|
|
|
continue
|
2020-01-06 18:03:45 +01:00
|
|
|
ccache_version = re.split("ccache version (.+)", line)
|
2019-12-26 21:49:40 +01:00
|
|
|
if len(ccache_version) < 2:
|
|
|
|
continue
|
|
|
|
global _ccache_version_found
|
|
|
|
_ccache_version_found = parse_version(ccache_version[1])
|
|
|
|
if _ccache_version_found >= _ccache_version_min:
|
|
|
|
validated = True
|
|
|
|
|
|
|
|
return validated
|
2019-07-18 23:50:34 +02:00
|
|
|
|
2020-01-06 18:03:45 +01:00
|
|
|
|
2019-07-18 23:50:34 +02:00
|
|
|
def generate(env):
|
|
|
|
"""Add ccache support."""
|
2019-12-26 21:49:40 +01:00
|
|
|
|
|
|
|
# If we have already generated the tool, don't generate it again.
|
2020-01-06 18:03:45 +01:00
|
|
|
if "CCACHE_VERSION" in env:
|
2019-12-26 21:49:40 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
# If we can't find ccache, or it is too old a version, don't
|
|
|
|
# generate.
|
|
|
|
if not exists(env):
|
|
|
|
return
|
|
|
|
|
|
|
|
# Record our found CCACHE_VERSION. Other tools that need to know
|
|
|
|
# about ccache (like iecc) should query this variable to determine
|
|
|
|
# if ccache is active. Looking at the CCACHE variable in the
|
|
|
|
# environment is not sufficient, since the user may have set it,
|
|
|
|
# but it doesn't work or is out of date.
|
2020-01-06 18:03:45 +01:00
|
|
|
env["CCACHE_VERSION"] = _ccache_version_found
|
2019-12-26 21:49:40 +01:00
|
|
|
|
2019-07-18 23:50:34 +02:00
|
|
|
# ccache does not support response files so force scons to always
|
|
|
|
# use the full command
|
|
|
|
#
|
|
|
|
# Note: This only works for Python versions >= 3.5
|
2020-01-06 18:03:45 +01:00
|
|
|
env["MAXLINELENGTH"] = math.inf
|
2019-07-18 23:50:34 +02:00
|
|
|
|
2019-12-26 21:49:40 +01:00
|
|
|
# Add ccache to the relevant command lines. Wrap the reference to
|
|
|
|
# ccache in the $( $) pattern so that turning ccache on or off
|
|
|
|
# doesn't invalidate your build.
|
2020-01-06 18:03:45 +01:00
|
|
|
env["CCCOM"] = "$( $CCACHE $)" + env["CCCOM"]
|
|
|
|
env["CXXCOM"] = "$( $CCACHE $)" + env["CXXCOM"]
|
|
|
|
env["SHCCCOM"] = "$( $CCACHE $)" + env["SHCCCOM"]
|
|
|
|
env["SHCXXCOM"] = "$( $CCACHE $)" + env["SHCXXCOM"]
|