1# Copyright (C) 2012 Linux Foundation 2# Author: Richard Purdie 3# Some code and influence taken from srctree.bbclass: 4# Copyright (C) 2009 Chris Larson <clarson@kergoth.com> 5# Released under the MIT license (see COPYING.MIT for the terms) 6# 7# externalsrc.bbclass enables use of an existing source tree, usually external to 8# the build system to build a piece of software rather than the usual fetch/unpack/patch 9# process. 10# 11# To use, add externalsrc to the global inherit and set EXTERNALSRC to point at the 12# directory you want to use containing the sources e.g. from local.conf for a recipe 13# called "myrecipe" you would do: 14# 15# INHERIT += "externalsrc" 16# EXTERNALSRC_pn-myrecipe = "/path/to/my/source/tree" 17# 18# In order to make this class work for both target and native versions (or with 19# multilibs/cross or other BBCLASSEXTEND variants), B is set to point to a separate 20# directory under the work directory (split source and build directories). This is 21# the default, but the build directory can be set to the source directory if 22# circumstances dictate by setting EXTERNALSRC_BUILD to the same value, e.g.: 23# 24# EXTERNALSRC_BUILD_pn-myrecipe = "/path/to/my/source/tree" 25# 26 27SRCTREECOVEREDTASKS ?= "do_patch do_unpack do_fetch" 28EXTERNALSRC_SYMLINKS ?= "oe-workdir:${WORKDIR} oe-logs:${T}" 29 30python () { 31 externalsrc = d.getVar('EXTERNALSRC') 32 externalsrcbuild = d.getVar('EXTERNALSRC_BUILD') 33 34 if externalsrc and not externalsrc.startswith("/"): 35 bb.error("EXTERNALSRC must be an absolute path") 36 if externalsrcbuild and not externalsrcbuild.startswith("/"): 37 bb.error("EXTERNALSRC_BUILD must be an absolute path") 38 39 # If this is the base recipe and EXTERNALSRC is set for it or any of its 40 # derivatives, then enable BB_DONT_CACHE to force the recipe to always be 41 # re-parsed so that the file-checksums function for do_compile is run every 42 # time. 43 bpn = d.getVar('BPN') 44 classextend = (d.getVar('BBCLASSEXTEND') or '').split() 45 if bpn == d.getVar('PN') or not classextend: 46 if (externalsrc or 47 ('native' in classextend and 48 d.getVar('EXTERNALSRC_pn-%s-native' % bpn)) or 49 ('nativesdk' in classextend and 50 d.getVar('EXTERNALSRC_pn-nativesdk-%s' % bpn)) or 51 ('cross' in classextend and 52 d.getVar('EXTERNALSRC_pn-%s-cross' % bpn))): 53 d.setVar('BB_DONT_CACHE', '1') 54 55 if externalsrc: 56 import oe.recipeutils 57 import oe.path 58 59 d.setVar('S', externalsrc) 60 if externalsrcbuild: 61 d.setVar('B', externalsrcbuild) 62 else: 63 d.setVar('B', '${WORKDIR}/${BPN}-${PV}/') 64 65 local_srcuri = [] 66 fetch = bb.fetch2.Fetch((d.getVar('SRC_URI') or '').split(), d) 67 for url in fetch.urls: 68 url_data = fetch.ud[url] 69 parm = url_data.parm 70 if (url_data.type == 'file' or 71 'type' in parm and parm['type'] == 'kmeta'): 72 local_srcuri.append(url) 73 74 d.setVar('SRC_URI', ' '.join(local_srcuri)) 75 76 # Dummy value because the default function can't be called with blank SRC_URI 77 d.setVar('SRCPV', '999') 78 79 if d.getVar('CONFIGUREOPT_DEPTRACK') == '--disable-dependency-tracking': 80 d.setVar('CONFIGUREOPT_DEPTRACK', '') 81 82 tasks = filter(lambda k: d.getVarFlag(k, "task"), d.keys()) 83 84 for task in tasks: 85 if task.endswith("_setscene"): 86 # sstate is never going to work for external source trees, disable it 87 bb.build.deltask(task, d) 88 else: 89 # Since configure will likely touch ${S}, ensure only we lock so one task has access at a time 90 d.appendVarFlag(task, "lockfiles", " ${S}/singletask.lock") 91 92 # We do not want our source to be wiped out, ever (kernel.bbclass does this for do_clean) 93 cleandirs = oe.recipeutils.split_var_value(d.getVarFlag(task, 'cleandirs', False) or '') 94 setvalue = False 95 for cleandir in cleandirs[:]: 96 if oe.path.is_path_parent(externalsrc, d.expand(cleandir)): 97 cleandirs.remove(cleandir) 98 setvalue = True 99 if setvalue: 100 d.setVarFlag(task, 'cleandirs', ' '.join(cleandirs)) 101 102 fetch_tasks = ['do_fetch', 'do_unpack'] 103 # If we deltask do_patch, there's no dependency to ensure do_unpack gets run, so add one 104 # Note that we cannot use d.appendVarFlag() here because deps is expected to be a list object, not a string 105 d.setVarFlag('do_configure', 'deps', (d.getVarFlag('do_configure', 'deps', False) or []) + ['do_unpack']) 106 107 for task in d.getVar("SRCTREECOVEREDTASKS").split(): 108 if local_srcuri and task in fetch_tasks: 109 continue 110 bb.build.deltask(task, d) 111 112 d.prependVarFlag('do_compile', 'prefuncs', "externalsrc_compile_prefunc ") 113 d.prependVarFlag('do_configure', 'prefuncs', "externalsrc_configure_prefunc ") 114 115 d.setVarFlag('do_compile', 'file-checksums', '${@srctree_hash_files(d)}') 116 d.setVarFlag('do_configure', 'file-checksums', '${@srctree_configure_hash_files(d)}') 117 118 # We don't want the workdir to go away 119 d.appendVar('RM_WORK_EXCLUDE', ' ' + d.getVar('PN')) 120 121 bb.build.addtask('do_buildclean', 122 'do_clean' if d.getVar('S') == d.getVar('B') else None, 123 None, d) 124 125 # If B=S the same builddir is used even for different architectures. 126 # Thus, use a shared CONFIGURESTAMPFILE and STAMP directory so that 127 # change of do_configure task hash is correctly detected and stamps are 128 # invalidated if e.g. MACHINE changes. 129 if d.getVar('S') == d.getVar('B'): 130 configstamp = '${TMPDIR}/work-shared/${PN}/${EXTENDPE}${PV}-${PR}/configure.sstate' 131 d.setVar('CONFIGURESTAMPFILE', configstamp) 132 d.setVar('STAMP', '${STAMPS_DIR}/work-shared/${PN}/${EXTENDPE}${PV}-${PR}') 133 d.setVar('STAMPCLEAN', '${STAMPS_DIR}/work-shared/${PN}/*-*') 134} 135 136python externalsrc_configure_prefunc() { 137 s_dir = d.getVar('S') 138 # Create desired symlinks 139 symlinks = (d.getVar('EXTERNALSRC_SYMLINKS') or '').split() 140 newlinks = [] 141 for symlink in symlinks: 142 symsplit = symlink.split(':', 1) 143 lnkfile = os.path.join(s_dir, symsplit[0]) 144 target = d.expand(symsplit[1]) 145 if len(symsplit) > 1: 146 if os.path.islink(lnkfile): 147 # Link already exists, leave it if it points to the right location already 148 if os.readlink(lnkfile) == target: 149 continue 150 os.unlink(lnkfile) 151 elif os.path.exists(lnkfile): 152 # File/dir exists with same name as link, just leave it alone 153 continue 154 os.symlink(target, lnkfile) 155 newlinks.append(symsplit[0]) 156 # Hide the symlinks from git 157 try: 158 git_exclude_file = os.path.join(s_dir, '.git/info/exclude') 159 if os.path.exists(git_exclude_file): 160 with open(git_exclude_file, 'r+') as efile: 161 elines = efile.readlines() 162 for link in newlinks: 163 if link in elines or '/'+link in elines: 164 continue 165 efile.write('/' + link + '\n') 166 except IOError as ioe: 167 bb.note('Failed to hide EXTERNALSRC_SYMLINKS from git') 168} 169 170python externalsrc_compile_prefunc() { 171 # Make it obvious that this is happening, since forgetting about it could lead to much confusion 172 bb.plain('NOTE: %s: compiling from external source tree %s' % (d.getVar('PN'), d.getVar('EXTERNALSRC'))) 173} 174 175do_buildclean[dirs] = "${S} ${B}" 176do_buildclean[nostamp] = "1" 177do_buildclean[doc] = "Call 'make clean' or equivalent in ${B}" 178externalsrc_do_buildclean() { 179 if [ -e Makefile -o -e makefile -o -e GNUmakefile ]; then 180 rm -f ${@' '.join([x.split(':')[0] for x in (d.getVar('EXTERNALSRC_SYMLINKS') or '').split()])} 181 if [ "${CLEANBROKEN}" != "1" ]; then 182 oe_runmake clean || die "make failed" 183 fi 184 else 185 bbnote "nothing to do - no makefile found" 186 fi 187} 188 189def srctree_hash_files(d, srcdir=None): 190 import shutil 191 import subprocess 192 import tempfile 193 194 s_dir = srcdir or d.getVar('EXTERNALSRC') 195 git_dir = None 196 197 try: 198 git_dir = os.path.join(s_dir, 199 subprocess.check_output(['git', '-C', s_dir, 'rev-parse', '--git-dir'], stderr=subprocess.DEVNULL).decode("utf-8").rstrip()) 200 except subprocess.CalledProcessError: 201 pass 202 203 ret = " " 204 if git_dir is not None: 205 oe_hash_file = os.path.join(git_dir, 'oe-devtool-tree-sha1-%s' % d.getVar('PN')) 206 with tempfile.NamedTemporaryFile(prefix='oe-devtool-index') as tmp_index: 207 # Clone index 208 shutil.copyfile(os.path.join(git_dir, 'index'), tmp_index.name) 209 # Update our custom index 210 env = os.environ.copy() 211 env['GIT_INDEX_FILE'] = tmp_index.name 212 subprocess.check_output(['git', 'add', '-A', '.'], cwd=s_dir, env=env) 213 sha1 = subprocess.check_output(['git', 'write-tree'], cwd=s_dir, env=env).decode("utf-8") 214 with open(oe_hash_file, 'w') as fobj: 215 fobj.write(sha1) 216 ret = oe_hash_file + ':True' 217 else: 218 ret = s_dir + '/*:True' 219 return ret 220 221def srctree_configure_hash_files(d): 222 """ 223 Get the list of files that should trigger do_configure to re-execute, 224 based on the value of CONFIGURE_FILES 225 """ 226 in_files = (d.getVar('CONFIGURE_FILES') or '').split() 227 out_items = [] 228 search_files = [] 229 for entry in in_files: 230 if entry.startswith('/'): 231 out_items.append('%s:%s' % (entry, os.path.exists(entry))) 232 else: 233 search_files.append(entry) 234 if search_files: 235 s_dir = d.getVar('EXTERNALSRC') 236 for root, _, files in os.walk(s_dir): 237 for f in files: 238 if f in search_files: 239 out_items.append('%s:True' % os.path.join(root, f)) 240 return ' '.join(out_items) 241 242EXPORT_FUNCTIONS do_buildclean 243