Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
| 31 | BB_HASHBASE_WHITELIST += "ICECC_PARALLEL_MAKE ICECC_DISABLED ICECC_USER_PACKAGE_BL ICECC_USER_CLASS_BL ICECC_USER_PACKAGE_WL ICECC_PATH ICECC_ENV_EXEC" |
| 32 | |
| 33 | ICECC_ENV_EXEC ?= "${STAGING_BINDIR_NATIVE}/icecc-create-env" |
| 34 | |
| 35 | def icecc_dep_prepend(d): |
| 36 | # INHIBIT_DEFAULT_DEPS doesn't apply to the patch command. Whether or not |
| 37 | # we need that built is the responsibility of the patch function / class, not |
| 38 | # the application. |
| 39 | if not d.getVar('INHIBIT_DEFAULT_DEPS', False): |
| 40 | return "icecc-create-env-native" |
| 41 | return "" |
| 42 | |
| 43 | DEPENDS_prepend += "${@icecc_dep_prepend(d)} " |
| 44 | |
| 45 | def get_cross_kernel_cc(bb,d): |
| 46 | kernel_cc = d.getVar('KERNEL_CC', False) |
| 47 | |
| 48 | # evaluate the expression by the shell if necessary |
| 49 | if '`' in kernel_cc or '$(' in kernel_cc: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 50 | import subprocess |
| 51 | kernel_cc = subprocess.check_output("echo %s" % kernel_cc, shell=True).decode("utf-8")[:-1] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | |
| 53 | kernel_cc = d.expand(kernel_cc) |
| 54 | kernel_cc = kernel_cc.replace('ccache', '').strip() |
| 55 | kernel_cc = kernel_cc.split(' ')[0] |
| 56 | kernel_cc = kernel_cc.strip() |
| 57 | return kernel_cc |
| 58 | |
| 59 | def get_icecc(d): |
| 60 | return d.getVar('ICECC_PATH', False) or bb.utils.which(os.getenv("PATH"), "icecc") |
| 61 | |
| 62 | def create_path(compilers, bb, d): |
| 63 | """ |
| 64 | Create Symlinks for the icecc in the staging directory |
| 65 | """ |
| 66 | staging = os.path.join(d.expand('${STAGING_BINDIR}'), "ice") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 67 | if icecc_is_kernel(bb, d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 68 | staging += "-kernel" |
| 69 | |
| 70 | #check if the icecc path is set by the user |
| 71 | icecc = get_icecc(d) |
| 72 | |
| 73 | # Create the dir if necessary |
| 74 | try: |
| 75 | os.stat(staging) |
| 76 | except: |
| 77 | try: |
| 78 | os.makedirs(staging) |
| 79 | except: |
| 80 | pass |
| 81 | |
| 82 | for compiler in compilers: |
| 83 | gcc_path = os.path.join(staging, compiler) |
| 84 | try: |
| 85 | os.stat(gcc_path) |
| 86 | except: |
| 87 | try: |
| 88 | os.symlink(icecc, gcc_path) |
| 89 | except: |
| 90 | pass |
| 91 | |
| 92 | return staging |
| 93 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 94 | def use_icecc(bb,d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 95 | if d.getVar('ICECC_DISABLED', False) == "1": |
| 96 | # don't even try it, when explicitly disabled |
| 97 | return "no" |
| 98 | |
| 99 | # allarch recipes don't use compiler |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 100 | if icecc_is_allarch(bb, d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 101 | return "no" |
| 102 | |
| 103 | pn = d.getVar('PN', True) |
| 104 | |
| 105 | system_class_blacklist = [] |
| 106 | user_class_blacklist = (d.getVar('ICECC_USER_CLASS_BL', False) or "none").split() |
| 107 | package_class_blacklist = system_class_blacklist + user_class_blacklist |
| 108 | |
| 109 | for black in package_class_blacklist: |
| 110 | if bb.data.inherits_class(black, d): |
| 111 | bb.debug(1, "%s: class %s found in blacklist, disable icecc" % (pn, black)) |
| 112 | return "no" |
| 113 | |
| 114 | # "system" recipe blacklist contains a list of packages that can not distribute compile tasks |
| 115 | # for one reason or the other |
| 116 | # this is the old list (which doesn't seem to be valid anymore, because I was able to build |
| 117 | # all these with icecc enabled) |
| 118 | # system_package_blacklist = [ "uclibc", "glibc", "gcc", "bind", "u-boot", "dhcp-forwarder", "enchant", "connman", "orbit2" ] |
| 119 | # when adding new entry, please document why (how it failed) so that we can re-evaluate it later |
| 120 | # e.g. when there is new version |
| 121 | # building libgcc-initial with icecc fails with CPP sanity check error if host sysroot contains cross gcc built for another target tune/variant |
| 122 | system_package_blacklist = ["libgcc-initial"] |
| 123 | user_package_blacklist = (d.getVar('ICECC_USER_PACKAGE_BL', False) or "").split() |
| 124 | user_package_whitelist = (d.getVar('ICECC_USER_PACKAGE_WL', False) or "").split() |
| 125 | package_blacklist = system_package_blacklist + user_package_blacklist |
| 126 | |
| 127 | if pn in package_blacklist: |
| 128 | bb.debug(1, "%s: found in blacklist, disable icecc" % pn) |
| 129 | return "no" |
| 130 | |
| 131 | if pn in user_package_whitelist: |
| 132 | bb.debug(1, "%s: found in whitelist, enable icecc" % pn) |
| 133 | return "yes" |
| 134 | |
| 135 | if d.getVar('PARALLEL_MAKE', False) == "": |
| 136 | bb.debug(1, "%s: has empty PARALLEL_MAKE, disable icecc" % pn) |
| 137 | return "no" |
| 138 | |
| 139 | return "yes" |
| 140 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 141 | def icecc_is_allarch(bb, d): |
| 142 | return d.getVar("PACKAGE_ARCH", True) == "all" or bb.data.inherits_class('allarch', d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 143 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 144 | def icecc_is_kernel(bb, d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 145 | return \ |
| 146 | bb.data.inherits_class("kernel", d); |
| 147 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 148 | def icecc_is_native(bb, d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 149 | return \ |
| 150 | bb.data.inherits_class("cross", d) or \ |
| 151 | bb.data.inherits_class("native", d); |
| 152 | |
| 153 | # Don't pollute allarch signatures with TARGET_FPU |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 154 | icecc_version[vardepsexclude] += "TARGET_FPU" |
| 155 | def icecc_version(bb, d): |
| 156 | if use_icecc(bb, d) == "no": |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 157 | return "" |
| 158 | |
| 159 | parallel = d.getVar('ICECC_PARALLEL_MAKE', False) or "" |
| 160 | if not d.getVar('PARALLEL_MAKE', False) == "" and parallel: |
| 161 | d.setVar("PARALLEL_MAKE", parallel) |
| 162 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 163 | if icecc_is_native(bb, d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 164 | archive_name = "local-host-env" |
| 165 | elif d.expand('${HOST_PREFIX}') == "": |
| 166 | bb.fatal(d.expand("${PN}"), " NULL prefix") |
| 167 | else: |
| 168 | prefix = d.expand('${HOST_PREFIX}' ) |
| 169 | distro = d.expand('${DISTRO}') |
| 170 | target_sys = d.expand('${TARGET_SYS}') |
| 171 | float = d.getVar('TARGET_FPU', False) or "hard" |
| 172 | archive_name = prefix + distro + "-" + target_sys + "-" + float |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 173 | if icecc_is_kernel(bb, d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 174 | archive_name += "-kernel" |
| 175 | |
| 176 | import socket |
| 177 | ice_dir = d.expand('${STAGING_DIR_NATIVE}${prefix_native}') |
| 178 | tar_file = os.path.join(ice_dir, 'ice', archive_name + "-@VERSION@-" + socket.gethostname() + '.tar.gz') |
| 179 | |
| 180 | return tar_file |
| 181 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 182 | def icecc_path(bb,d): |
| 183 | if use_icecc(bb, d) == "no": |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 184 | # don't create unnecessary directories when icecc is disabled |
| 185 | return |
| 186 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 187 | if icecc_is_kernel(bb, d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 188 | return create_path( [get_cross_kernel_cc(bb,d), ], bb, d) |
| 189 | |
| 190 | else: |
| 191 | prefix = d.expand('${HOST_PREFIX}') |
| 192 | return create_path( [prefix+"gcc", prefix+"g++"], bb, d) |
| 193 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 194 | def icecc_get_external_tool(bb, d, tool): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 195 | external_toolchain_bindir = d.expand('${EXTERNAL_TOOLCHAIN}${bindir_cross}') |
| 196 | target_prefix = d.expand('${TARGET_PREFIX}') |
| 197 | return os.path.join(external_toolchain_bindir, '%s%s' % (target_prefix, tool)) |
| 198 | |
| 199 | # Don't pollute native signatures with target TUNE_PKGARCH through STAGING_BINDIR_TOOLCHAIN |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 200 | icecc_get_tool[vardepsexclude] += "STAGING_BINDIR_TOOLCHAIN" |
| 201 | def icecc_get_tool(bb, d, tool): |
| 202 | if icecc_is_native(bb, d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 203 | return bb.utils.which(os.getenv("PATH"), tool) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 204 | elif icecc_is_kernel(bb, d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 205 | return bb.utils.which(os.getenv("PATH"), get_cross_kernel_cc(bb, d)) |
| 206 | else: |
| 207 | ice_dir = d.expand('${STAGING_BINDIR_TOOLCHAIN}') |
| 208 | target_sys = d.expand('${TARGET_SYS}') |
| 209 | tool_bin = os.path.join(ice_dir, "%s-%s" % (target_sys, tool)) |
| 210 | if os.path.isfile(tool_bin): |
| 211 | return tool_bin |
| 212 | else: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 213 | external_tool_bin = icecc_get_external_tool(bb, d, tool) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 214 | if os.path.isfile(external_tool_bin): |
| 215 | return external_tool_bin |
| 216 | else: |
| 217 | return "" |
| 218 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 219 | def icecc_get_and_check_tool(bb, d, tool): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 220 | # Check that g++ or gcc is not a symbolic link to icecc binary in |
| 221 | # PATH or icecc-create-env script will silently create an invalid |
| 222 | # compiler environment package. |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 223 | t = icecc_get_tool(bb, d, tool) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 224 | if t: |
| 225 | import subprocess |
| 226 | link_path = subprocess.check_output("readlink -f %s" % t, shell=True).decode("utf-8")[:-1] |
| 227 | if link_path == get_icecc(d): |
| 228 | bb.error("%s is a symlink to %s in PATH and this prevents icecc from working" % (t, get_icecc(d))) |
| 229 | return "" |
| 230 | else: |
| 231 | return t |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 232 | else: |
| 233 | return t |
| 234 | |
| 235 | wait_for_file() { |
| 236 | local TIME_ELAPSED=0 |
| 237 | local FILE_TO_TEST=$1 |
| 238 | local TIMEOUT=$2 |
| 239 | until [ -f "$FILE_TO_TEST" ] |
| 240 | do |
| 241 | TIME_ELAPSED=`expr $TIME_ELAPSED + 1` |
| 242 | if [ $TIME_ELAPSED -gt $TIMEOUT ] |
| 243 | then |
| 244 | return 1 |
| 245 | fi |
| 246 | sleep 1 |
| 247 | done |
| 248 | } |
| 249 | |
| 250 | def set_icecc_env(): |
| 251 | # dummy python version of set_icecc_env |
| 252 | return |
| 253 | |
| 254 | set_icecc_env() { |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 255 | if [ "${@use_icecc(bb, d)}" = "no" ] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 256 | then |
| 257 | return |
| 258 | fi |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 259 | ICECC_VERSION="${@icecc_version(bb, d)}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 260 | if [ "x${ICECC_VERSION}" = "x" ] |
| 261 | then |
| 262 | bbwarn "Cannot use icecc: could not get ICECC_VERSION" |
| 263 | return |
| 264 | fi |
| 265 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 266 | ICE_PATH="${@icecc_path(bb, d)}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 267 | if [ "x${ICE_PATH}" = "x" ] |
| 268 | then |
| 269 | bbwarn "Cannot use icecc: could not get ICE_PATH" |
| 270 | return |
| 271 | fi |
| 272 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 273 | ICECC_CC="${@icecc_get_and_check_tool(bb, d, "gcc")}" |
| 274 | ICECC_CXX="${@icecc_get_and_check_tool(bb, d, "g++")}" |
| 275 | # cannot use icecc_get_and_check_tool here because it assumes as without target_sys prefix |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 276 | ICECC_WHICH_AS="${@bb.utils.which(os.getenv('PATH'), 'as')}" |
| 277 | if [ ! -x "${ICECC_CC}" -o ! -x "${ICECC_CXX}" ] |
| 278 | then |
| 279 | bbwarn "Cannot use icecc: could not get ICECC_CC or ICECC_CXX" |
| 280 | return |
| 281 | fi |
| 282 | |
| 283 | ICE_VERSION=`$ICECC_CC -dumpversion` |
| 284 | ICECC_VERSION=`echo ${ICECC_VERSION} | sed -e "s/@VERSION@/$ICE_VERSION/g"` |
| 285 | if [ ! -x "${ICECC_ENV_EXEC}" ] |
| 286 | then |
| 287 | bbwarn "Cannot use icecc: invalid ICECC_ENV_EXEC" |
| 288 | return |
| 289 | fi |
| 290 | |
| 291 | ICECC_AS="`${ICECC_CC} -print-prog-name=as`" |
| 292 | # for target recipes should return something like: |
| 293 | # /OE/tmp-eglibc/sysroots/x86_64-linux/usr/libexec/arm920tt-oe-linux-gnueabi/gcc/arm-oe-linux-gnueabi/4.8.2/as |
| 294 | # and just "as" for native, if it returns "as" in current directory (for whatever reason) use "as" from PATH |
| 295 | if [ "`dirname "${ICECC_AS}"`" = "." ] |
| 296 | then |
| 297 | ICECC_AS="${ICECC_WHICH_AS}" |
| 298 | fi |
| 299 | |
| 300 | if [ ! -f "${ICECC_VERSION}.done" ] |
| 301 | then |
| 302 | mkdir -p "`dirname "${ICECC_VERSION}"`" |
| 303 | |
| 304 | # the ICECC_VERSION generation step must be locked by a mutex |
| 305 | # in order to prevent race conditions |
| 306 | if flock -n "${ICECC_VERSION}.lock" \ |
| 307 | ${ICECC_ENV_EXEC} "${ICECC_CC}" "${ICECC_CXX}" "${ICECC_AS}" "${ICECC_VERSION}" |
| 308 | then |
| 309 | touch "${ICECC_VERSION}.done" |
| 310 | elif [ ! wait_for_file "${ICECC_VERSION}.done" 30 ] |
| 311 | then |
| 312 | # locking failed so wait for ${ICECC_VERSION}.done to appear |
| 313 | bbwarn "Timeout waiting for ${ICECC_VERSION}.done" |
| 314 | return |
| 315 | fi |
| 316 | fi |
| 317 | |
| 318 | export ICECC_VERSION ICECC_CC ICECC_CXX |
| 319 | export PATH="$ICE_PATH:$PATH" |
| 320 | export CCACHE_PATH="$PATH" |
| 321 | |
| 322 | bbnote "Using icecc" |
| 323 | } |
| 324 | |
| 325 | do_configure_prepend() { |
| 326 | set_icecc_env |
| 327 | } |
| 328 | |
| 329 | do_compile_prepend() { |
| 330 | set_icecc_env |
| 331 | } |
| 332 | |
| 333 | do_compile_kernelmodules_prepend() { |
| 334 | set_icecc_env |
| 335 | } |
| 336 | |
| 337 | do_install_prepend() { |
| 338 | set_icecc_env |
| 339 | } |