1# IceCream distributed compiling support
2#
3# Stages directories with symlinks from gcc/g++ to icecc, for both
4# native and cross compilers. Depending on each configure or compile,
5# the directories are added at the head of the PATH list and ICECC_CXX
6# and ICEC_CC are set.
7#
8# For the cross compiler, creates a tar.gz of our toolchain and sets
9# ICECC_VERSION accordingly.
10#
11# The class now handles all 3 different compile 'stages' (i.e native ,cross-kernel and target) creating the
12# necessary environment tar.gz file to be used by the remote machines.
13# It also supports meta-toolchain generation
14#
15# If ICECC_PATH is not set in local.conf then the class will try to locate it using 'bb.utils.which'
16# but nothing is sure ;)
17#
18# If ICECC_ENV_EXEC is set in local.conf, then it should point to the icecc-create-env script provided by the user
19# or the default one provided by icecc-create-env.bb will be used
20# (NOTE that this is a modified version of the script need it and *not the one that comes with icecc*
21#
22# User can specify if specific packages or packages belonging to class should not use icecc to distribute
23# compile jobs to remote machines, but handled locally, by defining ICECC_USER_CLASS_BL and ICECC_USER_PACKAGE_BL
24# with the appropriate values in local.conf. In addition the user can force to enable icecc for packages
25# which set an empty PARALLEL_MAKE variable by defining ICECC_USER_PACKAGE_WL.
26#
27#########################################################################################
28#Error checking is kept to minimum so double check any parameters you pass to the class
29###########################################################################################
30
31BB_HASHBASE_WHITELIST += "ICECC_PARALLEL_MAKE ICECC_DISABLED ICECC_USER_PACKAGE_BL \
32    ICECC_USER_CLASS_BL ICECC_USER_PACKAGE_WL ICECC_PATH ICECC_ENV_EXEC \
33    ICECC_CARET_WORKAROUND ICECC_CFLAGS ICECC_ENV_VERSION \
34    ICECC_DEBUG ICECC_LOGFILE ICECC_REPEAT_RATE ICECC_PREFERRED_HOST \
35    ICECC_CLANG_REMOTE_CPP ICECC_IGNORE_UNVERIFIED ICECC_TEST_SOCKET \
36    ICECC_ENV_DEBUG ICECC_SYSTEM_PACKAGE_BL ICECC_SYSTEM_CLASS_BL \
37    ICECC_REMOTE_CPP \
38    "
39
40ICECC_ENV_EXEC ?= "${STAGING_BINDIR_NATIVE}/icecc-create-env"
41
42HOSTTOOLS_NONFATAL += "icecc patchelf"
43
44# This version can be incremented when changes are made to the environment that
45# invalidate the version on the compile nodes. Changing it will cause a new
46# environment to be created.
47#
48# A useful thing to do for testing Icecream changes locally is to add a
49# subversion in local.conf:
50#  ICECC_ENV_VERSION_append = "-my-ver-1"
51ICECC_ENV_VERSION = "2"
52
53# Default to disabling the caret workaround, If set to "1" in local.conf, icecc
54# will locally recompile any files that have warnings, which can adversely
55# affect performance.
56#
57# See: https://github.com/icecc/icecream/issues/190
58export ICECC_CARET_WORKAROUND ??= "0"
59
60export ICECC_REMOTE_CPP ??= "0"
61
62ICECC_CFLAGS = ""
63CFLAGS += "${ICECC_CFLAGS}"
64CXXFLAGS += "${ICECC_CFLAGS}"
65
66# Debug flags when generating environments
67ICECC_ENV_DEBUG ??= ""
68
69# "system" recipe blacklist contains a list of packages that can not distribute
70# compile tasks for one reason or the other. When adding new entry, please
71# document why (how it failed) so that we can re-evaluate it later e.g. when
72# there is new version
73#
74# libgcc-initial - fails with CPP sanity check error if host sysroot contains
75#                  cross gcc built for another target tune/variant
76# target-sdk-provides-dummy - ${HOST_PREFIX} is empty which triggers the "NULL
77#                             prefix" error.
78ICECC_SYSTEM_PACKAGE_BL += "\
79    libgcc-initial \
80    target-sdk-provides-dummy \
81    "
82
83# "system" classes that should be blacklisted. When adding new entry, please
84# document why (how it failed) so that we can re-evaluate it later
85#
86# image - Image aren't compiling, but the testing framework for images captures
87#         PARALLEL_MAKE as part of the test environment. Many tests won't use
88#         icecream, but leaving the high level of parallelism can cause them to
89#         consume an unnecessary amount of resources.
90ICECC_SYSTEM_CLASS_BL += "\
91    image \
92    "
93
94def icecc_dep_prepend(d):
95    # INHIBIT_DEFAULT_DEPS doesn't apply to the patch command.  Whether or  not
96    # we need that built is the responsibility of the patch function / class, not
97    # the application.
98    if not d.getVar('INHIBIT_DEFAULT_DEPS'):
99        return "icecc-create-env-native"
100    return ""
101
102DEPENDS_prepend += "${@icecc_dep_prepend(d)} "
103
104get_cross_kernel_cc[vardepsexclude] += "KERNEL_CC"
105def get_cross_kernel_cc(bb,d):
106    if not icecc_is_kernel(bb, d):
107        return None
108
109    # evaluate the expression by the shell if necessary
110    kernel_cc = d.getVar('KERNEL_CC')
111    if '`' in kernel_cc or '$(' in kernel_cc:
112        import subprocess
113        kernel_cc = subprocess.check_output("echo %s" % kernel_cc, shell=True).decode("utf-8")[:-1]
114
115    kernel_cc = kernel_cc.replace('ccache', '').strip()
116    kernel_cc = kernel_cc.split(' ')[0]
117    kernel_cc = kernel_cc.strip()
118    return kernel_cc
119
120def get_icecc(d):
121    return d.getVar('ICECC_PATH') or bb.utils.which(os.getenv("PATH"), "icecc")
122
123def use_icecc(bb,d):
124    if d.getVar('ICECC_DISABLED') == "1":
125        # don't even try it, when explicitly disabled
126        return "no"
127
128    # allarch recipes don't use compiler
129    if icecc_is_allarch(bb, d):
130        return "no"
131
132    if icecc_is_cross_canadian(bb, d):
133        return "no"
134
135    pn = d.getVar('PN')
136    bpn = d.getVar('BPN')
137
138    # Blacklist/whitelist checks are made against BPN, because there is a good
139    # chance that if icecc should be skipped for a recipe, it should be skipped
140    # for all the variants of that recipe. PN is still checked in case a user
141    # specified a more specific recipe.
142    check_pn = set([pn, bpn])
143
144    system_class_blacklist = (d.getVar('ICECC_SYSTEM_CLASS_BL') or "").split()
145    user_class_blacklist = (d.getVar('ICECC_USER_CLASS_BL') or "none").split()
146    package_class_blacklist = system_class_blacklist + user_class_blacklist
147
148    for black in package_class_blacklist:
149        if bb.data.inherits_class(black, d):
150            bb.debug(1, "%s: class %s found in blacklist, disable icecc" % (pn, black))
151            return "no"
152
153    system_package_blacklist = (d.getVar('ICECC_SYSTEM_PACKAGE_BL') or "").split()
154    user_package_blacklist = (d.getVar('ICECC_USER_PACKAGE_BL') or "").split()
155    user_package_whitelist = (d.getVar('ICECC_USER_PACKAGE_WL') or "").split()
156    package_blacklist = system_package_blacklist + user_package_blacklist
157
158    if check_pn & set(package_blacklist):
159        bb.debug(1, "%s: found in blacklist, disable icecc" % pn)
160        return "no"
161
162    if check_pn & set(user_package_whitelist):
163        bb.debug(1, "%s: found in whitelist, enable icecc" % pn)
164        return "yes"
165
166    if d.getVar('PARALLEL_MAKE') == "":
167        bb.debug(1, "%s: has empty PARALLEL_MAKE, disable icecc" % pn)
168        return "no"
169
170    return "yes"
171
172def icecc_is_allarch(bb, d):
173    return d.getVar("PACKAGE_ARCH") == "all"
174
175def icecc_is_kernel(bb, d):
176    return \
177        bb.data.inherits_class("kernel", d);
178
179def icecc_is_native(bb, d):
180    return \
181        bb.data.inherits_class("cross", d) or \
182        bb.data.inherits_class("native", d);
183
184def icecc_is_cross_canadian(bb, d):
185    return bb.data.inherits_class("cross-canadian", d)
186
187def icecc_dir(bb, d):
188    return d.expand('${TMPDIR}/work-shared/ice')
189
190# Don't pollute allarch signatures with TARGET_FPU
191icecc_version[vardepsexclude] += "TARGET_FPU"
192def icecc_version(bb, d):
193    if use_icecc(bb, d) == "no":
194        return ""
195
196    parallel = d.getVar('ICECC_PARALLEL_MAKE') or ""
197    if not d.getVar('PARALLEL_MAKE') == "" and parallel:
198        d.setVar("PARALLEL_MAKE", parallel)
199
200    # Disable showing the caret in the GCC compiler output if the workaround is
201    # disabled
202    if d.getVar('ICECC_CARET_WORKAROUND') == '0':
203        d.setVar('ICECC_CFLAGS', '-fno-diagnostics-show-caret')
204
205    if icecc_is_native(bb, d):
206        archive_name = "local-host-env"
207    elif d.expand('${HOST_PREFIX}') == "":
208        bb.fatal(d.expand("${PN}"), " NULL prefix")
209    else:
210        prefix = d.expand('${HOST_PREFIX}' )
211        distro = d.expand('${DISTRO}')
212        target_sys = d.expand('${TARGET_SYS}')
213        float = d.getVar('TARGET_FPU') or "hard"
214        archive_name = prefix + distro + "-"        + target_sys + "-" + float
215        if icecc_is_kernel(bb, d):
216            archive_name += "-kernel"
217
218    import socket
219    ice_dir = icecc_dir(bb, d)
220    tar_file = os.path.join(ice_dir, "{archive}-{version}-@VERSION@-{hostname}.tar.gz".format(
221        archive=archive_name,
222        version=d.getVar('ICECC_ENV_VERSION'),
223        hostname=socket.gethostname()
224        ))
225
226    return tar_file
227
228def icecc_path(bb,d):
229    if use_icecc(bb, d) == "no":
230        # don't create unnecessary directories when icecc is disabled
231        return
232
233    staging = os.path.join(d.expand('${STAGING_BINDIR}'), "ice")
234    if icecc_is_kernel(bb, d):
235        staging += "-kernel"
236
237    return staging
238
239def icecc_get_external_tool(bb, d, tool):
240    external_toolchain_bindir = d.expand('${EXTERNAL_TOOLCHAIN}${bindir_cross}')
241    target_prefix = d.expand('${TARGET_PREFIX}')
242    return os.path.join(external_toolchain_bindir, '%s%s' % (target_prefix, tool))
243
244def icecc_get_tool_link(tool, d):
245    import subprocess
246    return subprocess.check_output("readlink -f %s" % tool, shell=True).decode("utf-8")[:-1]
247
248def icecc_get_path_tool(tool, d):
249    # This is a little ugly, but we want to make sure we add an actual
250    # compiler to the toolchain, not ccache. Some distros (e.g. Fedora)
251    # have ccache enabled by default using symlinks PATH, meaning ccache
252    # would be found first when looking for the compiler.
253    paths = os.getenv("PATH").split(':')
254    while True:
255        p, hist = bb.utils.which(':'.join(paths), tool, history=True)
256        if not p or os.path.basename(icecc_get_tool_link(p, d)) != 'ccache':
257            return p
258        paths = paths[len(hist):]
259
260    return ""
261
262# Don't pollute native signatures with target TUNE_PKGARCH through STAGING_BINDIR_TOOLCHAIN
263icecc_get_tool[vardepsexclude] += "STAGING_BINDIR_TOOLCHAIN"
264def icecc_get_tool(bb, d, tool):
265    if icecc_is_native(bb, d):
266        return icecc_get_path_tool(tool, d)
267    elif icecc_is_kernel(bb, d):
268        return icecc_get_path_tool(get_cross_kernel_cc(bb, d), d)
269    else:
270        ice_dir = d.expand('${STAGING_BINDIR_TOOLCHAIN}')
271        target_sys = d.expand('${TARGET_SYS}')
272        for p in ice_dir.split(':'):
273            tool_bin = os.path.join(p, "%s-%s" % (target_sys, tool))
274            if os.path.isfile(tool_bin):
275                return tool_bin
276        external_tool_bin = icecc_get_external_tool(bb, d, tool)
277        if os.path.isfile(external_tool_bin):
278            return external_tool_bin
279        return ""
280
281def icecc_get_and_check_tool(bb, d, tool):
282    # Check that g++ or gcc is not a symbolic link to icecc binary in
283    # PATH or icecc-create-env script will silently create an invalid
284    # compiler environment package.
285    t = icecc_get_tool(bb, d, tool)
286    if t:
287        link_path = icecc_get_tool_link(t, d)
288        if link_path == get_icecc(d):
289            bb.error("%s is a symlink to %s in PATH and this prevents icecc from working" % (t, link_path))
290            return ""
291        else:
292            return t
293    else:
294        return t
295
296wait_for_file() {
297    local TIME_ELAPSED=0
298    local FILE_TO_TEST=$1
299    local TIMEOUT=$2
300    until [ -f "$FILE_TO_TEST" ]
301    do
302        TIME_ELAPSED=`expr $TIME_ELAPSED + 1`
303        if [ $TIME_ELAPSED -gt $TIMEOUT ]
304        then
305            return 1
306        fi
307        sleep 1
308    done
309}
310
311def set_icecc_env():
312    # dummy python version of set_icecc_env
313    return
314
315set_icecc_env[vardepsexclude] += "KERNEL_CC"
316set_icecc_env() {
317    if [ "${@use_icecc(bb, d)}" = "no" ]
318    then
319        return
320    fi
321    ICECC_VERSION="${@icecc_version(bb, d)}"
322    if [ "x${ICECC_VERSION}" = "x" ]
323    then
324        bbwarn "Cannot use icecc: could not get ICECC_VERSION"
325        return
326    fi
327
328    ICE_PATH="${@icecc_path(bb, d)}"
329    if [ "x${ICE_PATH}" = "x" ]
330    then
331        bbwarn "Cannot use icecc: could not get ICE_PATH"
332        return
333    fi
334
335    ICECC_BIN="${@get_icecc(d)}"
336    if [ -z "${ICECC_BIN}" ]; then
337        bbwarn "Cannot use icecc: icecc binary not found"
338        return
339    fi
340    if [ -z "$(which patchelf patchelf-uninative)" ]; then
341        bbwarn "Cannot use icecc: patchelf not found"
342        return
343    fi
344
345    # Create symlinks to icecc in the recipe-sysroot directory
346    mkdir -p ${ICE_PATH}
347    if [ -n "${KERNEL_CC}" ]; then
348        compilers="${@get_cross_kernel_cc(bb,d)}"
349    else
350        compilers="${HOST_PREFIX}gcc ${HOST_PREFIX}g++"
351    fi
352    for compiler in $compilers; do
353        ln -sf ${ICECC_BIN} ${ICE_PATH}/$compiler
354    done
355
356    ICECC_CC="${@icecc_get_and_check_tool(bb, d, "gcc")}"
357    ICECC_CXX="${@icecc_get_and_check_tool(bb, d, "g++")}"
358    # cannot use icecc_get_and_check_tool here because it assumes as without target_sys prefix
359    ICECC_WHICH_AS="${@bb.utils.which(os.getenv('PATH'), 'as')}"
360    if [ ! -x "${ICECC_CC}" -o ! -x "${ICECC_CXX}" ]
361    then
362        bbwarn "Cannot use icecc: could not get ICECC_CC or ICECC_CXX"
363        return
364    fi
365
366    ICE_VERSION=`$ICECC_CC -dumpversion`
367    ICECC_VERSION=`echo ${ICECC_VERSION} | sed -e "s/@VERSION@/$ICE_VERSION/g"`
368    if [ ! -x "${ICECC_ENV_EXEC}" ]
369    then
370        bbwarn "Cannot use icecc: invalid ICECC_ENV_EXEC"
371        return
372    fi
373
374    ICECC_AS="`${ICECC_CC} -print-prog-name=as`"
375    # for target recipes should return something like:
376    # /OE/tmp-eglibc/sysroots/x86_64-linux/usr/libexec/arm920tt-oe-linux-gnueabi/gcc/arm-oe-linux-gnueabi/4.8.2/as
377    # and just "as" for native, if it returns "as" in current directory (for whatever reason) use "as" from PATH
378    if [ "`dirname "${ICECC_AS}"`" = "." ]
379    then
380        ICECC_AS="${ICECC_WHICH_AS}"
381    fi
382
383    if [ ! -f "${ICECC_VERSION}.done" ]
384    then
385        mkdir -p "`dirname "${ICECC_VERSION}"`"
386
387        # the ICECC_VERSION generation step must be locked by a mutex
388        # in order to prevent race conditions
389        if flock -n "${ICECC_VERSION}.lock" \
390            ${ICECC_ENV_EXEC} ${ICECC_ENV_DEBUG} "${ICECC_CC}" "${ICECC_CXX}" "${ICECC_AS}" "${ICECC_VERSION}"
391        then
392            touch "${ICECC_VERSION}.done"
393        elif ! wait_for_file "${ICECC_VERSION}.done" 30
394        then
395            # locking failed so wait for ${ICECC_VERSION}.done to appear
396            bbwarn "Timeout waiting for ${ICECC_VERSION}.done"
397            return
398        fi
399    fi
400
401    # Don't let ccache find the icecream compiler links that have been created, otherwise
402    # it can end up invoking icecream recursively.
403    export CCACHE_PATH="$PATH"
404    export CCACHE_DISABLE="1"
405
406    export ICECC_VERSION ICECC_CC ICECC_CXX
407    export PATH="$ICE_PATH:$PATH"
408
409    bbnote "Using icecc path: $ICE_PATH"
410    bbnote "Using icecc tarball: $ICECC_VERSION"
411}
412
413do_configure_prepend() {
414    set_icecc_env
415}
416
417do_compile_prepend() {
418    set_icecc_env
419}
420
421do_compile_kernelmodules_prepend() {
422    set_icecc_env
423}
424
425do_install_prepend() {
426    set_icecc_env
427}
428
429# IceCream is not (currently) supported in the extensible SDK
430ICECC_SDK_HOST_TASK = "nativesdk-icecc-toolchain"
431ICECC_SDK_HOST_TASK_task-populate-sdk-ext = ""
432
433# Don't include IceCream in uninative tarball
434ICECC_SDK_HOST_TASK_pn-uninative-tarball = ""
435
436# Add the toolchain scripts to the SDK
437TOOLCHAIN_HOST_TASK_append = " ${ICECC_SDK_HOST_TASK}"
438