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