Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
| 27 | SRCTREECOVEREDTASKS ?= "do_patch do_unpack do_fetch" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 28 | EXTERNALSRC_SYMLINKS ?= "oe-workdir:${WORKDIR} oe-logs:${T}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 29 | |
| 30 | python () { |
| 31 | externalsrc = d.getVar('EXTERNALSRC', True) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 32 | |
| 33 | # If this is the base recipe and EXTERNALSRC is set for it or any of its |
| 34 | # derivatives, then enable BB_DONT_CACHE to force the recipe to always be |
| 35 | # re-parsed so that the file-checksums function for do_compile is run every |
| 36 | # time. |
| 37 | bpn = d.getVar('BPN', True) |
| 38 | if bpn == d.getVar('PN', True): |
| 39 | classextend = (d.getVar('BBCLASSEXTEND', True) or '').split() |
| 40 | if (externalsrc or |
| 41 | ('native' in classextend and |
| 42 | d.getVar('EXTERNALSRC_pn-%s-native' % bpn, True)) or |
| 43 | ('nativesdk' in classextend and |
| 44 | d.getVar('EXTERNALSRC_pn-nativesdk-%s' % bpn, True)) or |
| 45 | ('cross' in classextend and |
| 46 | d.getVar('EXTERNALSRC_pn-%s-cross' % bpn, True))): |
| 47 | d.setVar('BB_DONT_CACHE', '1') |
| 48 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 49 | if externalsrc: |
| 50 | d.setVar('S', externalsrc) |
| 51 | externalsrcbuild = d.getVar('EXTERNALSRC_BUILD', True) |
| 52 | if externalsrcbuild: |
| 53 | d.setVar('B', externalsrcbuild) |
| 54 | else: |
| 55 | d.setVar('B', '${WORKDIR}/${BPN}-${PV}/') |
| 56 | |
| 57 | local_srcuri = [] |
| 58 | fetch = bb.fetch2.Fetch((d.getVar('SRC_URI', True) or '').split(), d) |
| 59 | for url in fetch.urls: |
| 60 | url_data = fetch.ud[url] |
| 61 | parm = url_data.parm |
| 62 | if (url_data.type == 'file' or |
| 63 | 'type' in parm and parm['type'] == 'kmeta'): |
| 64 | local_srcuri.append(url) |
| 65 | |
| 66 | d.setVar('SRC_URI', ' '.join(local_srcuri)) |
| 67 | |
| 68 | if '{SRCPV}' in d.getVar('PV', False): |
| 69 | # Dummy value because the default function can't be called with blank SRC_URI |
| 70 | d.setVar('SRCPV', '999') |
| 71 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 72 | tasks = filter(lambda k: d.getVarFlag(k, "task", True), d.keys()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | |
| 74 | for task in tasks: |
| 75 | if task.endswith("_setscene"): |
| 76 | # sstate is never going to work for external source trees, disable it |
| 77 | bb.build.deltask(task, d) |
| 78 | else: |
| 79 | # Since configure will likely touch ${S}, ensure only we lock so one task has access at a time |
| 80 | d.appendVarFlag(task, "lockfiles", " ${S}/singletask.lock") |
| 81 | |
| 82 | # We do not want our source to be wiped out, ever (kernel.bbclass does this for do_clean) |
| 83 | cleandirs = (d.getVarFlag(task, 'cleandirs', False) or '').split() |
| 84 | setvalue = False |
| 85 | for cleandir in cleandirs[:]: |
| 86 | if d.expand(cleandir) == externalsrc: |
| 87 | cleandirs.remove(cleandir) |
| 88 | setvalue = True |
| 89 | if setvalue: |
| 90 | d.setVarFlag(task, 'cleandirs', ' '.join(cleandirs)) |
| 91 | |
| 92 | fetch_tasks = ['do_fetch', 'do_unpack'] |
| 93 | # If we deltask do_patch, there's no dependency to ensure do_unpack gets run, so add one |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 94 | # Note that we cannot use d.appendVarFlag() here because deps is expected to be a list object, not a string |
| 95 | d.setVarFlag('do_configure', 'deps', (d.getVarFlag('do_configure', 'deps', False) or []) + ['do_unpack']) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 96 | |
| 97 | for task in d.getVar("SRCTREECOVEREDTASKS", True).split(): |
| 98 | if local_srcuri and task in fetch_tasks: |
| 99 | continue |
| 100 | bb.build.deltask(task, d) |
| 101 | |
| 102 | d.prependVarFlag('do_compile', 'prefuncs', "externalsrc_compile_prefunc ") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 103 | d.prependVarFlag('do_configure', 'prefuncs', "externalsrc_configure_prefunc ") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 104 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 105 | d.setVarFlag('do_compile', 'file-checksums', '${@srctree_hash_files(d)}') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 106 | d.setVarFlag('do_configure', 'file-checksums', '${@srctree_configure_hash_files(d)}') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 107 | |
| 108 | # We don't want the workdir to go away |
| 109 | d.appendVar('RM_WORK_EXCLUDE', ' ' + d.getVar('PN', True)) |
| 110 | |
| 111 | # If B=S the same builddir is used even for different architectures. |
| 112 | # Thus, use a shared CONFIGURESTAMPFILE and STAMP directory so that |
| 113 | # change of do_configure task hash is correctly detected and stamps are |
| 114 | # invalidated if e.g. MACHINE changes. |
| 115 | if d.getVar('S', True) == d.getVar('B', True): |
| 116 | configstamp = '${TMPDIR}/work-shared/${PN}/${EXTENDPE}${PV}-${PR}/configure.sstate' |
| 117 | d.setVar('CONFIGURESTAMPFILE', configstamp) |
| 118 | d.setVar('STAMP', '${STAMPS_DIR}/work-shared/${PN}/${EXTENDPE}${PV}-${PR}') |
| 119 | } |
| 120 | |
| 121 | python externalsrc_configure_prefunc() { |
| 122 | # Create desired symlinks |
| 123 | symlinks = (d.getVar('EXTERNALSRC_SYMLINKS', True) or '').split() |
| 124 | for symlink in symlinks: |
| 125 | symsplit = symlink.split(':', 1) |
| 126 | lnkfile = os.path.join(d.getVar('S', True), symsplit[0]) |
| 127 | target = d.expand(symsplit[1]) |
| 128 | if len(symsplit) > 1: |
| 129 | if os.path.islink(lnkfile): |
| 130 | # Link already exists, leave it if it points to the right location already |
| 131 | if os.readlink(lnkfile) == target: |
| 132 | continue |
| 133 | os.unlink(lnkfile) |
| 134 | elif os.path.exists(lnkfile): |
| 135 | # File/dir exists with same name as link, just leave it alone |
| 136 | continue |
| 137 | os.symlink(target, lnkfile) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | python externalsrc_compile_prefunc() { |
| 141 | # Make it obvious that this is happening, since forgetting about it could lead to much confusion |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 142 | bb.plain('NOTE: %s: compiling from external source tree %s' % (d.getVar('PN', True), d.getVar('EXTERNALSRC', True))) |
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 | |
| 145 | def srctree_hash_files(d): |
| 146 | import shutil |
| 147 | import subprocess |
| 148 | import tempfile |
| 149 | |
| 150 | s_dir = d.getVar('EXTERNALSRC', True) |
| 151 | git_dir = os.path.join(s_dir, '.git') |
| 152 | oe_hash_file = os.path.join(git_dir, 'oe-devtool-tree-sha1') |
| 153 | |
| 154 | ret = " " |
| 155 | if os.path.exists(git_dir): |
| 156 | with tempfile.NamedTemporaryFile(dir=git_dir, prefix='oe-devtool-index') as tmp_index: |
| 157 | # Clone index |
| 158 | shutil.copy2(os.path.join(git_dir, 'index'), tmp_index.name) |
| 159 | # Update our custom index |
| 160 | env = os.environ.copy() |
| 161 | env['GIT_INDEX_FILE'] = tmp_index.name |
| 162 | subprocess.check_output(['git', 'add', '.'], cwd=s_dir, env=env) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 163 | sha1 = subprocess.check_output(['git', 'write-tree'], cwd=s_dir, env=env).decode("utf-8") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 164 | with open(oe_hash_file, 'w') as fobj: |
| 165 | fobj.write(sha1) |
| 166 | ret = oe_hash_file + ':True' |
| 167 | else: |
| 168 | ret = d.getVar('EXTERNALSRC', True) + '/*:True' |
| 169 | return ret |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 170 | |
| 171 | def srctree_configure_hash_files(d): |
| 172 | """ |
| 173 | Get the list of files that should trigger do_configure to re-execute, |
| 174 | based on the value of CONFIGURE_FILES |
| 175 | """ |
| 176 | in_files = (d.getVar('CONFIGURE_FILES', True) or '').split() |
| 177 | out_items = [] |
| 178 | search_files = [] |
| 179 | for entry in in_files: |
| 180 | if entry.startswith('/'): |
| 181 | out_items.append('%s:%s' % (entry, os.path.exists(entry))) |
| 182 | else: |
| 183 | search_files.append(entry) |
| 184 | if search_files: |
| 185 | s_dir = d.getVar('EXTERNALSRC', True) |
| 186 | for root, _, files in os.walk(s_dir): |
| 187 | for f in files: |
| 188 | if f in search_files: |
| 189 | out_items.append('%s:True' % os.path.join(root, f)) |
| 190 | return ' '.join(out_items) |