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 | # |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 7 | # externalsrc.bbclass enables use of an existing source tree, usually external to |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 () { |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 31 | externalsrc = d.getVar('EXTERNALSRC') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 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") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 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. |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 43 | bpn = d.getVar('BPN') |
| 44 | if bpn == d.getVar('PN'): |
| 45 | classextend = (d.getVar('BBCLASSEXTEND') or '').split() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 46 | if (externalsrc or |
| 47 | ('native' in classextend and |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 48 | d.getVar('EXTERNALSRC_pn-%s-native' % bpn)) or |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 49 | ('nativesdk' in classextend and |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 50 | d.getVar('EXTERNALSRC_pn-nativesdk-%s' % bpn)) or |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 51 | ('cross' in classextend and |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 52 | d.getVar('EXTERNALSRC_pn-%s-cross' % bpn))): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 53 | d.setVar('BB_DONT_CACHE', '1') |
| 54 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 55 | if externalsrc: |
| 56 | d.setVar('S', externalsrc) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | if externalsrcbuild: |
| 58 | d.setVar('B', externalsrcbuild) |
| 59 | else: |
| 60 | d.setVar('B', '${WORKDIR}/${BPN}-${PV}/') |
| 61 | |
| 62 | local_srcuri = [] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 63 | fetch = bb.fetch2.Fetch((d.getVar('SRC_URI') or '').split(), d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 | for url in fetch.urls: |
| 65 | url_data = fetch.ud[url] |
| 66 | parm = url_data.parm |
| 67 | if (url_data.type == 'file' or |
| 68 | 'type' in parm and parm['type'] == 'kmeta'): |
| 69 | local_srcuri.append(url) |
| 70 | |
| 71 | d.setVar('SRC_URI', ' '.join(local_srcuri)) |
| 72 | |
| 73 | if '{SRCPV}' in d.getVar('PV', False): |
| 74 | # Dummy value because the default function can't be called with blank SRC_URI |
| 75 | d.setVar('SRCPV', '999') |
| 76 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 77 | tasks = filter(lambda k: d.getVarFlag(k, "task"), d.keys()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 78 | |
| 79 | for task in tasks: |
| 80 | if task.endswith("_setscene"): |
| 81 | # sstate is never going to work for external source trees, disable it |
| 82 | bb.build.deltask(task, d) |
| 83 | else: |
| 84 | # Since configure will likely touch ${S}, ensure only we lock so one task has access at a time |
| 85 | d.appendVarFlag(task, "lockfiles", " ${S}/singletask.lock") |
| 86 | |
| 87 | # We do not want our source to be wiped out, ever (kernel.bbclass does this for do_clean) |
| 88 | cleandirs = (d.getVarFlag(task, 'cleandirs', False) or '').split() |
| 89 | setvalue = False |
| 90 | for cleandir in cleandirs[:]: |
| 91 | if d.expand(cleandir) == externalsrc: |
| 92 | cleandirs.remove(cleandir) |
| 93 | setvalue = True |
| 94 | if setvalue: |
| 95 | d.setVarFlag(task, 'cleandirs', ' '.join(cleandirs)) |
| 96 | |
| 97 | fetch_tasks = ['do_fetch', 'do_unpack'] |
| 98 | # 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] | 99 | # Note that we cannot use d.appendVarFlag() here because deps is expected to be a list object, not a string |
| 100 | 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] | 101 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 102 | for task in d.getVar("SRCTREECOVEREDTASKS").split(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | if local_srcuri and task in fetch_tasks: |
| 104 | continue |
| 105 | bb.build.deltask(task, d) |
| 106 | |
| 107 | d.prependVarFlag('do_compile', 'prefuncs', "externalsrc_compile_prefunc ") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 108 | d.prependVarFlag('do_configure', 'prefuncs', "externalsrc_configure_prefunc ") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 109 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 110 | d.setVarFlag('do_compile', 'file-checksums', '${@srctree_hash_files(d)}') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 111 | d.setVarFlag('do_configure', 'file-checksums', '${@srctree_configure_hash_files(d)}') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 112 | |
| 113 | # We don't want the workdir to go away |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 114 | d.appendVar('RM_WORK_EXCLUDE', ' ' + d.getVar('PN')) |
| 115 | |
| 116 | bb.build.addtask('do_buildclean', |
| 117 | 'do_clean' if d.getVar('S') == d.getVar('B') else None, |
| 118 | None, d) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 119 | |
| 120 | # If B=S the same builddir is used even for different architectures. |
| 121 | # Thus, use a shared CONFIGURESTAMPFILE and STAMP directory so that |
| 122 | # change of do_configure task hash is correctly detected and stamps are |
| 123 | # invalidated if e.g. MACHINE changes. |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 124 | if d.getVar('S') == d.getVar('B'): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 125 | configstamp = '${TMPDIR}/work-shared/${PN}/${EXTENDPE}${PV}-${PR}/configure.sstate' |
| 126 | d.setVar('CONFIGURESTAMPFILE', configstamp) |
| 127 | d.setVar('STAMP', '${STAMPS_DIR}/work-shared/${PN}/${EXTENDPE}${PV}-${PR}') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 128 | d.setVar('STAMPCLEAN', '${STAMPS_DIR}/work-shared/${PN}/*-*') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | python externalsrc_configure_prefunc() { |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 132 | s_dir = d.getVar('S') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 133 | # Create desired symlinks |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 134 | symlinks = (d.getVar('EXTERNALSRC_SYMLINKS') or '').split() |
| 135 | newlinks = [] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 136 | for symlink in symlinks: |
| 137 | symsplit = symlink.split(':', 1) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 138 | lnkfile = os.path.join(s_dir, symsplit[0]) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 139 | target = d.expand(symsplit[1]) |
| 140 | if len(symsplit) > 1: |
| 141 | if os.path.islink(lnkfile): |
| 142 | # Link already exists, leave it if it points to the right location already |
| 143 | if os.readlink(lnkfile) == target: |
| 144 | continue |
| 145 | os.unlink(lnkfile) |
| 146 | elif os.path.exists(lnkfile): |
| 147 | # File/dir exists with same name as link, just leave it alone |
| 148 | continue |
| 149 | os.symlink(target, lnkfile) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 150 | newlinks.append(symsplit[0]) |
| 151 | # Hide the symlinks from git |
| 152 | try: |
| 153 | git_exclude_file = os.path.join(s_dir, '.git/info/exclude') |
| 154 | if os.path.exists(git_exclude_file): |
| 155 | with open(git_exclude_file, 'r+') as efile: |
| 156 | elines = efile.readlines() |
| 157 | for link in newlinks: |
| 158 | if link in elines or '/'+link in elines: |
| 159 | continue |
| 160 | efile.write('/' + link + '\n') |
| 161 | except IOError as ioe: |
| 162 | bb.note('Failed to hide EXTERNALSRC_SYMLINKS from git') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | python externalsrc_compile_prefunc() { |
| 166 | # Make it obvious that this is happening, since forgetting about it could lead to much confusion |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 167 | bb.plain('NOTE: %s: compiling from external source tree %s' % (d.getVar('PN'), d.getVar('EXTERNALSRC'))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 168 | } |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 169 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 170 | do_buildclean[dirs] = "${S} ${B}" |
| 171 | do_buildclean[nostamp] = "1" |
| 172 | do_buildclean[doc] = "Call 'make clean' or equivalent in ${B}" |
| 173 | externalsrc_do_buildclean() { |
| 174 | if [ -e Makefile -o -e makefile -o -e GNUmakefile ]; then |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 175 | rm -f ${@' '.join([x.split(':')[0] for x in (d.getVar('EXTERNALSRC_SYMLINKS') or '').split()])} |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 176 | oe_runmake clean || die "make failed" |
| 177 | else |
| 178 | bbnote "nothing to do - no makefile found" |
| 179 | fi |
| 180 | } |
| 181 | |
| 182 | def srctree_hash_files(d, srcdir=None): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 183 | import shutil |
| 184 | import subprocess |
| 185 | import tempfile |
| 186 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 187 | s_dir = srcdir or d.getVar('EXTERNALSRC') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 188 | git_dir = None |
| 189 | |
| 190 | try: |
| 191 | git_dir = os.path.join(s_dir, |
| 192 | subprocess.check_output(['git', '-C', s_dir, 'rev-parse', '--git-dir']).decode("utf-8").rstrip()) |
| 193 | except subprocess.CalledProcessError: |
| 194 | pass |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 195 | |
| 196 | ret = " " |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 197 | if git_dir is not None: |
| 198 | oe_hash_file = os.path.join(git_dir, 'oe-devtool-tree-sha1') |
| 199 | with tempfile.NamedTemporaryFile(prefix='oe-devtool-index') as tmp_index: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 200 | # Clone index |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 201 | shutil.copyfile(os.path.join(git_dir, 'index'), tmp_index.name) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 202 | # Update our custom index |
| 203 | env = os.environ.copy() |
| 204 | env['GIT_INDEX_FILE'] = tmp_index.name |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 205 | subprocess.check_output(['git', 'add', '-A', '.'], cwd=s_dir, env=env) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 206 | 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] | 207 | with open(oe_hash_file, 'w') as fobj: |
| 208 | fobj.write(sha1) |
| 209 | ret = oe_hash_file + ':True' |
| 210 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 211 | ret = s_dir + '/*:True' |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 212 | return ret |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 213 | |
| 214 | def srctree_configure_hash_files(d): |
| 215 | """ |
| 216 | Get the list of files that should trigger do_configure to re-execute, |
| 217 | based on the value of CONFIGURE_FILES |
| 218 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 219 | in_files = (d.getVar('CONFIGURE_FILES') or '').split() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 220 | out_items = [] |
| 221 | search_files = [] |
| 222 | for entry in in_files: |
| 223 | if entry.startswith('/'): |
| 224 | out_items.append('%s:%s' % (entry, os.path.exists(entry))) |
| 225 | else: |
| 226 | search_files.append(entry) |
| 227 | if search_files: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 228 | s_dir = d.getVar('EXTERNALSRC') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 229 | for root, _, files in os.walk(s_dir): |
| 230 | for f in files: |
| 231 | if f in search_files: |
| 232 | out_items.append('%s:True' % os.path.join(root, f)) |
| 233 | return ' '.join(out_items) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 234 | |
| 235 | EXPORT_FUNCTIONS do_buildclean |