2020-04-22 17:38:25 +02:00
|
|
|
# Copyright 2020 MongoDB Inc.
|
2017-07-16 00:23:12 +02:00
|
|
|
#
|
2020-04-22 17:38:25 +02:00
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
# a copy of this software and associated documentation files (the
|
|
|
|
# "Software"), to deal in the Software without restriction, including
|
|
|
|
# without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
# permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
# the following conditions:
|
2017-07-16 00:23:12 +02:00
|
|
|
#
|
2020-04-22 17:38:25 +02:00
|
|
|
# The above copyright notice and this permission notice shall be included
|
|
|
|
# in all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
|
|
|
|
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
|
|
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2017-07-16 00:23:12 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
import SCons
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
from pkg_resources import parse_version
|
|
|
|
|
2020-01-07 19:48:42 +01:00
|
|
|
_icecream_version_min = parse_version("1.1rc2")
|
|
|
|
_ccache_nocpp2_version = parse_version("3.4.1")
|
2019-12-26 21:49:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
# I'd prefer to use value here, but amazingly, its __str__ returns the
|
|
|
|
# *initial* value of the Value and not the built value, if
|
|
|
|
# available. That seems like a bug. In the meantime, make our own very
|
|
|
|
# sinmple Substition thing.
|
|
|
|
class _BoundSubstitution:
|
|
|
|
def __init__(self, env, expression):
|
|
|
|
self.env = env
|
|
|
|
self.expression = expression
|
|
|
|
self.result = None
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if self.result is None:
|
|
|
|
self.result = self.env.subst(self.expression)
|
|
|
|
return self.result
|
2017-07-16 00:23:12 +02:00
|
|
|
|
2020-01-07 19:48:42 +01:00
|
|
|
|
2020-02-20 16:01:40 +01:00
|
|
|
def icecc_create_env(env, target, source, for_signature):
|
|
|
|
# Safe to assume unix here because icecream only works on Unix
|
|
|
|
mkdir = "mkdir -p ${ICECC_VERSION.Dir('').abspath}"
|
|
|
|
|
|
|
|
# Create the env, use awk to get just the tarball name and we store it in
|
|
|
|
# the shell variable $ICECC_VERSION_TMP so the subsequent mv command and
|
2020-07-10 04:55:13 +02:00
|
|
|
# store it in a known location. Add any files requested from the user environment.
|
|
|
|
create_env = "ICECC_VERSION_TMP=$$($ICECC_CREATE_ENV --$ICECC_COMPILER_TYPE $CC $CXX"
|
|
|
|
for addfile in env.get('ICECC_CREATE_ENV_ADDFILES', []):
|
|
|
|
if (type(addfile) == tuple
|
|
|
|
and len(addfile) == 2):
|
|
|
|
if env['ICECREAM_VERSION'] > parse_version('1.1'):
|
|
|
|
raise Exception("This version of icecream does not support addfile remapping.")
|
|
|
|
create_env += " --addfile {}={}".format(
|
|
|
|
env.File(addfile[0]).srcnode().abspath,
|
|
|
|
env.File(addfile[1]).srcnode().abspath)
|
|
|
|
env.Depends('$ICECC_VERSION', addfile[1])
|
|
|
|
elif type(addfile) == str:
|
|
|
|
create_env += " --addfile {}".format(env.File(addfile).srcnode().abspath)
|
|
|
|
env.Depends('$ICECC_VERSION', addfile)
|
|
|
|
else:
|
|
|
|
# NOTE: abspath is required by icecream because of
|
|
|
|
# this line in icecc-create-env:
|
|
|
|
# https://github.com/icecc/icecream/blob/10b9468f5bd30a0fdb058901e91e7a29f1bfbd42/client/icecc-create-env.in#L534
|
|
|
|
# which cuts out the two files based off the equals sign and
|
|
|
|
# starting slash of the second file
|
|
|
|
raise Exception("Found incorrect icecream addfile format: {}" +
|
|
|
|
"\nicecream addfiles must be a single path or tuple path format: " +
|
|
|
|
"('chroot dest path', 'source file path')".format(
|
|
|
|
str(addfile)))
|
|
|
|
create_env += " | awk '/^creating .*\\.tar\\.gz/ { print $$2 }')"
|
2020-02-20 16:01:40 +01:00
|
|
|
|
|
|
|
# Simply move our tarball to the expected locale.
|
|
|
|
mv = "mv $$ICECC_VERSION_TMP $TARGET"
|
|
|
|
|
|
|
|
# Daisy chain the commands and then let SCons Subst in the rest.
|
|
|
|
cmdline = f"{mkdir} && {create_env} && {mv}"
|
|
|
|
return cmdline
|
|
|
|
|
|
|
|
|
2017-07-16 00:23:12 +02:00
|
|
|
def generate(env):
|
|
|
|
|
|
|
|
if not exists(env):
|
|
|
|
return
|
|
|
|
|
2020-07-10 04:55:13 +02:00
|
|
|
# icecc lower then 1.1 supports addfile remapping accidentally
|
|
|
|
# and above it adds an empty cpuinfo so handle cpuinfo issues for icecream
|
|
|
|
# below version 1.1
|
|
|
|
if (env['ICECREAM_VERSION'] <= parse_version('1.1')
|
|
|
|
and env.ToolchainIs("clang")
|
|
|
|
and os.path.exists('/proc/cpuinfo')):
|
|
|
|
env.AppendUnique(ICECC_CREATE_ENV_ADDFILES=[('/proc/cpuinfo', '/dev/null')])
|
|
|
|
|
2020-02-20 16:01:40 +01:00
|
|
|
env["ICECCENVCOMSTR"] = env.get("ICECCENVCOMSTR", "Generating environment: $TARGET")
|
2020-05-01 21:28:00 +02:00
|
|
|
env["ICECC_COMPILER_TYPE"] = env.get(
|
2020-02-20 16:01:40 +01:00
|
|
|
"ICECC_COMPILER_TYPE", os.path.basename(env.WhereIs("${CC}"))
|
|
|
|
)
|
|
|
|
env.Append(
|
|
|
|
BUILDERS={
|
|
|
|
"IcecreamEnv": SCons.Builder.Builder(
|
|
|
|
action=SCons.Action.CommandGeneratorAction(
|
|
|
|
icecc_create_env, {"comstr": "$ICECCENVCOMSTR"},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-12-26 21:49:40 +01:00
|
|
|
# If we are going to load the ccache tool, but we haven't done so
|
|
|
|
# yet, then explicitly do it now. We need the ccache tool to be in
|
|
|
|
# place before we setup icecream because we need to do things a
|
|
|
|
# little differently if ccache is in play. If you don't use the
|
|
|
|
# TOOLS variable to configure your tools, you should explicitly
|
|
|
|
# load the ccache tool before you load icecream.
|
2020-01-07 19:48:42 +01:00
|
|
|
ccache_enabled = "CCACHE_VERSION" in env
|
2020-02-20 16:01:40 +01:00
|
|
|
if "ccache" in env["TOOLS"] and not ccache_enabled:
|
|
|
|
env.Tool("ccache")
|
2019-12-26 21:49:40 +01:00
|
|
|
|
2017-07-16 00:23:12 +02:00
|
|
|
# Absoluteify, so we can derive ICERUN
|
2020-01-07 19:48:42 +01:00
|
|
|
env["ICECC"] = env.WhereIs("$ICECC")
|
2017-07-16 00:23:12 +02:00
|
|
|
|
2020-01-07 19:48:42 +01:00
|
|
|
if not "ICERUN" in env:
|
|
|
|
env["ICERUN"] = env.File("$ICECC").File("icerun")
|
2017-07-16 00:23:12 +02:00
|
|
|
|
|
|
|
# Absoluteify, for parity with ICECC
|
2020-01-07 19:48:42 +01:00
|
|
|
env["ICERUN"] = env.WhereIs("$ICERUN")
|
2017-07-16 00:23:12 +02:00
|
|
|
|
2020-02-20 16:01:40 +01:00
|
|
|
env["ICECC_CREATE_ENV"] = env.WhereIs(
|
|
|
|
env.get("ICECC_CREATE_ENV", "icecc-create-env")
|
|
|
|
)
|
|
|
|
|
2017-07-16 00:23:12 +02:00
|
|
|
# We can't handle sanitizer blacklist files, so disable icecc then, and just flow through
|
|
|
|
# icerun to prevent slamming the local system with a huge -j value.
|
2019-02-19 16:50:57 +01:00
|
|
|
if any(
|
2020-01-07 19:48:42 +01:00
|
|
|
f.startswith("-fsanitize-blacklist=")
|
|
|
|
for fs in ["CCFLAGS", "CFLAGS", "CXXFLAGS"]
|
|
|
|
for f in env[fs]
|
|
|
|
):
|
|
|
|
env["ICECC"] = "$ICERUN"
|
2017-07-16 00:23:12 +02:00
|
|
|
|
|
|
|
# Make CC and CXX absolute paths too. It is better for icecc.
|
2020-01-07 19:48:42 +01:00
|
|
|
env["CC"] = env.WhereIs("$CC")
|
|
|
|
env["CXX"] = env.WhereIs("$CXX")
|
2017-07-16 00:23:12 +02:00
|
|
|
|
2020-05-01 21:28:00 +02:00
|
|
|
have_explicit_icecc_version = 'ICECC_VERSION' in env and bool(env['ICECC_VERSION'])
|
|
|
|
have_icecc_version_url = have_explicit_icecc_version and env["ICECC_VERSION"].startswith("http")
|
|
|
|
|
|
|
|
if have_explicit_icecc_version and not have_icecc_version_url:
|
|
|
|
icecc_version_file = env.File('$ICECC_VERSION')
|
|
|
|
if not icecc_version_file.exists():
|
2020-02-20 16:01:40 +01:00
|
|
|
raise Exception(
|
2020-05-01 21:28:00 +02:00
|
|
|
'The ICECC_VERSION variable set set to {}, but this file does not exist'.format(icecc_version_file)
|
2019-04-24 14:55:35 +02:00
|
|
|
)
|
2020-05-01 21:28:00 +02:00
|
|
|
env['ICECC_VERSION'] = icecc_version_file
|
2020-02-20 16:01:40 +01:00
|
|
|
else:
|
2020-05-01 21:28:00 +02:00
|
|
|
# Generate the deterministic name for our tarball
|
|
|
|
icecc_version_target_filename = env.subst("${CC}${CXX}.tar.gz").replace("/", "_")[
|
|
|
|
1:
|
|
|
|
]
|
|
|
|
icecc_version_dir = env.Dir("$BUILD_ROOT/scons/icecc")
|
|
|
|
icecc_known_version = icecc_version_dir.File(icecc_version_target_filename)
|
|
|
|
|
|
|
|
if have_icecc_version_url:
|
|
|
|
# We do the above weaker validation as opposed to
|
|
|
|
# urllib.urlparse (or similar). We really only support http
|
|
|
|
# URLs here and any other validation either requires a third
|
|
|
|
# party module or accepts things we don't.
|
|
|
|
env["ICECC_VERSION_URL"] = env["ICECC_VERSION"]
|
|
|
|
env["ICECC_VERSION"] = icecc_known_version
|
|
|
|
|
|
|
|
# Use curl / wget to download the toolchain because SCons (and ninja)
|
|
|
|
# are better at running shell commands than Python functions.
|
|
|
|
curl = env.WhereIs("curl")
|
|
|
|
wget = env.WhereIs("wget")
|
|
|
|
|
|
|
|
if curl:
|
|
|
|
cmdstr = "curl -L"
|
|
|
|
elif wget:
|
|
|
|
cmdstr = "wget"
|
|
|
|
else:
|
|
|
|
raise Exception(
|
|
|
|
"You have specified an ICECC_VERSION that is a URL but you have neither wget nor curl installed."
|
|
|
|
)
|
|
|
|
|
|
|
|
env.Command(
|
|
|
|
target="$ICECC_VERSION",
|
|
|
|
source=["$CC", "$CXX"],
|
|
|
|
action=[
|
|
|
|
cmdstr + " -o $TARGET $ICECC_VERSION_URL",
|
|
|
|
],
|
2020-02-20 16:01:40 +01:00
|
|
|
)
|
2020-05-01 21:28:00 +02:00
|
|
|
else:
|
|
|
|
# Make a predictable name for the toolchain
|
|
|
|
env["ICECC_VERSION"] = env.File(icecc_known_version)
|
|
|
|
env.IcecreamEnv(
|
|
|
|
target="$ICECC_VERSION",
|
|
|
|
source=["$ICECC_CREATE_ENV", "$CC", "$CXX"],
|
|
|
|
)
|
|
|
|
|
|
|
|
# Our ICECC_VERSION isn't just a file, so we need to make
|
|
|
|
# things depend on it to ensure that it comes into being at
|
|
|
|
# the right time. Don't do that for conftests though: we never
|
|
|
|
# want to run them remote.
|
|
|
|
def icecc_toolchain_dependency_emitter(target, source, env):
|
|
|
|
if "conftest" not in str(target[0]):
|
|
|
|
env.Requires(target, "$ICECC_VERSION")
|
|
|
|
return target, source
|
|
|
|
|
|
|
|
# Cribbed from Tool/cc.py and Tool/c++.py. It would be better if
|
|
|
|
# we could obtain this from SCons.
|
|
|
|
_CSuffixes = [".c"]
|
|
|
|
if not SCons.Util.case_sensitive_suffixes(".c", ".C"):
|
|
|
|
_CSuffixes.append(".C")
|
|
|
|
|
|
|
|
_CXXSuffixes = [".cpp", ".cc", ".cxx", ".c++", ".C++"]
|
|
|
|
if SCons.Util.case_sensitive_suffixes(".c", ".C"):
|
|
|
|
_CXXSuffixes.append(".C")
|
|
|
|
|
|
|
|
suffixes = _CSuffixes + _CXXSuffixes
|
|
|
|
for object_builder in SCons.Tool.createObjBuilders(env):
|
|
|
|
emitterdict = object_builder.builder.emitter
|
|
|
|
for suffix in emitterdict.keys():
|
|
|
|
if not suffix in suffixes:
|
|
|
|
continue
|
|
|
|
base = emitterdict[suffix]
|
|
|
|
emitterdict[suffix] = SCons.Builder.ListEmitter(
|
|
|
|
[base, icecc_toolchain_dependency_emitter]
|
|
|
|
)
|
2019-04-24 14:55:35 +02:00
|
|
|
|
2020-01-07 19:48:42 +01:00
|
|
|
if env.ToolchainIs("clang"):
|
|
|
|
env["ENV"]["ICECC_CLANG_REMOTE_CPP"] = 1
|
2019-12-26 21:49:40 +01:00
|
|
|
|
2020-01-07 19:48:42 +01:00
|
|
|
if ccache_enabled and env["CCACHE_VERSION"] >= _ccache_nocpp2_version:
|
|
|
|
env.AppendUnique(CCFLAGS=["-frewrite-includes"])
|
|
|
|
env["ENV"]["CCACHE_NOCPP2"] = 1
|
2017-07-16 00:23:12 +02:00
|
|
|
else:
|
2020-01-07 19:48:42 +01:00
|
|
|
env.AppendUnique(CCFLAGS=["-fdirectives-only"])
|
2019-12-26 21:49:40 +01:00
|
|
|
if ccache_enabled:
|
2020-01-07 19:48:42 +01:00
|
|
|
env["ENV"]["CCACHE_NOCPP2"] = 1
|
2017-07-16 00:23:12 +02:00
|
|
|
|
2020-01-07 19:48:42 +01:00
|
|
|
if "ICECC_SCHEDULER" in env:
|
|
|
|
env["ENV"]["USE_SCHEDULER"] = env["ICECC_SCHEDULER"]
|
2017-07-16 00:23:12 +02:00
|
|
|
|
2019-12-26 21:49:40 +01:00
|
|
|
# Build up the string we will set in the environment to tell icecream
|
|
|
|
# about the compiler package.
|
2020-02-20 16:01:40 +01:00
|
|
|
icecc_version_string = "${ICECC_VERSION.abspath}"
|
2020-01-07 19:48:42 +01:00
|
|
|
if "ICECC_VERSION_ARCH" in env:
|
|
|
|
icecc_version_string = "${ICECC_VERSION_ARCH}:" + icecc_version_string
|
2019-12-17 21:09:33 +01:00
|
|
|
|
2020-02-20 16:01:40 +01:00
|
|
|
# Use our BoundSubstitition class to put ICECC_VERSION into env['ENV'] with
|
|
|
|
# substitution in play. This avoids an early subst which can behave
|
|
|
|
# strangely.
|
2020-01-07 19:48:42 +01:00
|
|
|
env["ENV"]["ICECC_VERSION"] = _BoundSubstitution(env, icecc_version_string)
|
2019-12-26 21:49:40 +01:00
|
|
|
|
|
|
|
# If ccache is in play we actually want the icecc binary in the
|
|
|
|
# CCACHE_PREFIX environment variable, not on the command line, per
|
|
|
|
# the ccache documentation on compiler wrappers. Otherwise, just
|
|
|
|
# put $ICECC on the command line. We wrap it in the magic "don't
|
|
|
|
# consider this part of the build signature" sigils in the hope
|
|
|
|
# that enabling and disabling icecream won't cause rebuilds. This
|
|
|
|
# is unlikely to really work, since above we have maybe changed
|
|
|
|
# compiler flags (things like -fdirectives-only), but we still try
|
|
|
|
# to do the right thing.
|
|
|
|
if ccache_enabled:
|
2020-01-07 19:48:42 +01:00
|
|
|
env["ENV"]["CCACHE_PREFIX"] = _BoundSubstitution(env, "$ICECC")
|
2019-12-26 21:49:40 +01:00
|
|
|
else:
|
2020-05-01 21:25:10 +02:00
|
|
|
# Make a generator to expand to ICECC in the case where we are
|
|
|
|
# not a conftest. We never want to run conftests
|
|
|
|
# remotely. Ideally, we would do this for the CCACHE_PREFIX
|
|
|
|
# case above, but unfortunately if we did we would never
|
|
|
|
# actually see the conftests, because the BoundSubst means
|
|
|
|
# that we will never have a meaningful `target` variable when
|
|
|
|
# we are in ENV. Instead, rely on the ccache.py tool to do
|
|
|
|
# it's own filtering out of conftests.
|
|
|
|
def icecc_generator(target, source, env, for_signature):
|
|
|
|
if "conftest" not in str(target[0]):
|
|
|
|
return '$ICECC'
|
|
|
|
return ''
|
|
|
|
env['ICECC_GENERATOR'] = icecc_generator
|
|
|
|
|
|
|
|
icecc_string = "$( $ICECC_GENERATOR $)"
|
2020-01-07 19:48:42 +01:00
|
|
|
env["CCCOM"] = " ".join([icecc_string, env["CCCOM"]])
|
|
|
|
env["CXXCOM"] = " ".join([icecc_string, env["CXXCOM"]])
|
|
|
|
env["SHCCCOM"] = " ".join([icecc_string, env["SHCCCOM"]])
|
|
|
|
env["SHCXXCOM"] = " ".join([icecc_string, env["SHCXXCOM"]])
|
2017-07-16 00:23:12 +02:00
|
|
|
|
2020-05-01 21:26:09 +02:00
|
|
|
# Make common non-compile jobs flow through icerun so we don't
|
|
|
|
# kill the local machine. It would be nice to plumb ICERUN in via
|
|
|
|
# SPAWN or SHELL but it is too much. You end up running `icerun
|
|
|
|
# icecc ...`, and icecream doesn't handle that. We could try to
|
|
|
|
# filter and only apply icerun if icecc wasn't present but that
|
|
|
|
# seems fragile. If you find your local machine being overrun by
|
|
|
|
# jobs, figure out what sort they are and extend this part of the
|
|
|
|
# setup.
|
|
|
|
icerun_commands = [
|
|
|
|
"ARCOM",
|
|
|
|
"LINKCOM",
|
|
|
|
"PYTHON",
|
|
|
|
"SHLINKCOM",
|
|
|
|
]
|
|
|
|
|
|
|
|
for command in icerun_commands:
|
|
|
|
if command in env:
|
|
|
|
env[command] = " ".join(["$( $ICERUN $)", env[command]])
|
2017-07-16 00:23:12 +02:00
|
|
|
|
|
|
|
# Uncomment these to debug your icecc integration
|
|
|
|
# env['ENV']['ICECC_DEBUG'] = 'debug'
|
|
|
|
# env['ENV']['ICECC_LOGFILE'] = 'icecc.log'
|
|
|
|
|
2019-02-19 16:50:57 +01:00
|
|
|
|
2017-07-16 00:23:12 +02:00
|
|
|
def exists(env):
|
|
|
|
|
2020-01-07 19:48:42 +01:00
|
|
|
icecc = env.get("ICECC", False)
|
2017-07-16 00:23:12 +02:00
|
|
|
if not icecc:
|
|
|
|
return False
|
|
|
|
icecc = env.WhereIs(icecc)
|
2019-12-26 21:49:40 +01:00
|
|
|
if not icecc:
|
|
|
|
return False
|
2017-07-16 00:23:12 +02:00
|
|
|
|
2020-01-07 19:48:42 +01:00
|
|
|
pipe = SCons.Action._subproc(
|
|
|
|
env,
|
|
|
|
SCons.Util.CLVar(icecc) + ["--version"],
|
|
|
|
stdin="devnull",
|
|
|
|
stderr="devnull",
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
)
|
2017-07-16 00:23:12 +02:00
|
|
|
|
|
|
|
if pipe.wait() != 0:
|
|
|
|
return False
|
|
|
|
|
|
|
|
validated = False
|
|
|
|
for line in pipe.stdout:
|
2020-01-07 19:48:42 +01:00
|
|
|
line = line.decode("utf-8")
|
2017-07-16 00:23:12 +02:00
|
|
|
if validated:
|
|
|
|
continue # consume all data
|
2020-01-07 19:48:42 +01:00
|
|
|
version_banner = re.search(r"^ICECC ", line)
|
2017-07-16 00:23:12 +02:00
|
|
|
if not version_banner:
|
|
|
|
continue
|
2020-01-07 19:48:42 +01:00
|
|
|
icecc_version = re.split("ICECC (.+)", line)
|
2017-07-16 00:23:12 +02:00
|
|
|
if len(icecc_version) < 2:
|
|
|
|
continue
|
|
|
|
icecc_version = parse_version(icecc_version[1])
|
2019-12-26 21:49:40 +01:00
|
|
|
if icecc_version >= _icecream_version_min:
|
2017-07-16 00:23:12 +02:00
|
|
|
validated = True
|
|
|
|
|
2020-05-01 21:19:59 +02:00
|
|
|
if validated:
|
|
|
|
env['ICECREAM_VERSION'] = icecc_version
|
|
|
|
|
2017-07-16 00:23:12 +02:00
|
|
|
return validated
|