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