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') |
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. |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 37 | bpn = d.getVar('BPN') |
| 38 | if bpn == d.getVar('PN'): |
| 39 | classextend = (d.getVar('BBCLASSEXTEND') or '').split() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 40 | if (externalsrc or |
| 41 | ('native' in classextend and |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 42 | d.getVar('EXTERNALSRC_pn-%s-native' % bpn)) or |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 43 | ('nativesdk' in classextend and |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 44 | d.getVar('EXTERNALSRC_pn-nativesdk-%s' % bpn)) or |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 45 | ('cross' in classextend and |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 46 | d.getVar('EXTERNALSRC_pn-%s-cross' % bpn))): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 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) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 51 | externalsrcbuild = d.getVar('EXTERNALSRC_BUILD') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | if externalsrcbuild: |
| 53 | d.setVar('B', externalsrcbuild) |
| 54 | else: |
| 55 | d.setVar('B', '${WORKDIR}/${BPN}-${PV}/') |
| 56 | |
| 57 | local_srcuri = [] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 58 | fetch = bb.fetch2.Fetch((d.getVar('SRC_URI') or '').split(), d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 72 | tasks = filter(lambda k: d.getVarFlag(k, "task"), 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 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 97 | for task in d.getVar("SRCTREECOVEREDTASKS").split(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 109 | d.appendVar('RM_WORK_EXCLUDE', ' ' + d.getVar('PN')) |
| 110 | |
| 111 | bb.build.addtask('do_buildclean', |
| 112 | 'do_clean' if d.getVar('S') == d.getVar('B') else None, |
| 113 | None, d) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 114 | |
| 115 | # If B=S the same builddir is used even for different architectures. |
| 116 | # Thus, use a shared CONFIGURESTAMPFILE and STAMP directory so that |
| 117 | # change of do_configure task hash is correctly detected and stamps are |
| 118 | # invalidated if e.g. MACHINE changes. |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 119 | if d.getVar('S') == d.getVar('B'): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 120 | configstamp = '${TMPDIR}/work-shared/${PN}/${EXTENDPE}${PV}-${PR}/configure.sstate' |
| 121 | d.setVar('CONFIGURESTAMPFILE', configstamp) |
| 122 | d.setVar('STAMP', '${STAMPS_DIR}/work-shared/${PN}/${EXTENDPE}${PV}-${PR}') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 123 | d.setVar('STAMPCLEAN', '${STAMPS_DIR}/work-shared/${PN}/*-*') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | python externalsrc_configure_prefunc() { |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 127 | s_dir = d.getVar('S') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 128 | # Create desired symlinks |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 129 | symlinks = (d.getVar('EXTERNALSRC_SYMLINKS') or '').split() |
| 130 | newlinks = [] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 131 | for symlink in symlinks: |
| 132 | symsplit = symlink.split(':', 1) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 133 | lnkfile = os.path.join(s_dir, symsplit[0]) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 134 | target = d.expand(symsplit[1]) |
| 135 | if len(symsplit) > 1: |
| 136 | if os.path.islink(lnkfile): |
| 137 | # Link already exists, leave it if it points to the right location already |
| 138 | if os.readlink(lnkfile) == target: |
| 139 | continue |
| 140 | os.unlink(lnkfile) |
| 141 | elif os.path.exists(lnkfile): |
| 142 | # File/dir exists with same name as link, just leave it alone |
| 143 | continue |
| 144 | os.symlink(target, lnkfile) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 145 | newlinks.append(symsplit[0]) |
| 146 | # Hide the symlinks from git |
| 147 | try: |
| 148 | git_exclude_file = os.path.join(s_dir, '.git/info/exclude') |
| 149 | if os.path.exists(git_exclude_file): |
| 150 | with open(git_exclude_file, 'r+') as efile: |
| 151 | elines = efile.readlines() |
| 152 | for link in newlinks: |
| 153 | if link in elines or '/'+link in elines: |
| 154 | continue |
| 155 | efile.write('/' + link + '\n') |
| 156 | except IOError as ioe: |
| 157 | bb.note('Failed to hide EXTERNALSRC_SYMLINKS from git') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | python externalsrc_compile_prefunc() { |
| 161 | # 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^] | 162 | 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] | 163 | } |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 164 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 165 | do_buildclean[dirs] = "${S} ${B}" |
| 166 | do_buildclean[nostamp] = "1" |
| 167 | do_buildclean[doc] = "Call 'make clean' or equivalent in ${B}" |
| 168 | externalsrc_do_buildclean() { |
| 169 | if [ -e Makefile -o -e makefile -o -e GNUmakefile ]; then |
| 170 | oe_runmake clean || die "make failed" |
| 171 | else |
| 172 | bbnote "nothing to do - no makefile found" |
| 173 | fi |
| 174 | } |
| 175 | |
| 176 | def srctree_hash_files(d, srcdir=None): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 177 | import shutil |
| 178 | import subprocess |
| 179 | import tempfile |
| 180 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 181 | s_dir = srcdir or d.getVar('EXTERNALSRC') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 182 | git_dir = os.path.join(s_dir, '.git') |
| 183 | oe_hash_file = os.path.join(git_dir, 'oe-devtool-tree-sha1') |
| 184 | |
| 185 | ret = " " |
| 186 | if os.path.exists(git_dir): |
| 187 | with tempfile.NamedTemporaryFile(dir=git_dir, prefix='oe-devtool-index') as tmp_index: |
| 188 | # Clone index |
| 189 | shutil.copy2(os.path.join(git_dir, 'index'), tmp_index.name) |
| 190 | # Update our custom index |
| 191 | env = os.environ.copy() |
| 192 | env['GIT_INDEX_FILE'] = tmp_index.name |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 193 | subprocess.check_output(['git', 'add', '-A', '.'], cwd=s_dir, env=env) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 194 | 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] | 195 | with open(oe_hash_file, 'w') as fobj: |
| 196 | fobj.write(sha1) |
| 197 | ret = oe_hash_file + ':True' |
| 198 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 199 | ret = s_dir + '/*:True' |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 200 | return ret |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 201 | |
| 202 | def srctree_configure_hash_files(d): |
| 203 | """ |
| 204 | Get the list of files that should trigger do_configure to re-execute, |
| 205 | based on the value of CONFIGURE_FILES |
| 206 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 207 | in_files = (d.getVar('CONFIGURE_FILES') or '').split() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 208 | out_items = [] |
| 209 | search_files = [] |
| 210 | for entry in in_files: |
| 211 | if entry.startswith('/'): |
| 212 | out_items.append('%s:%s' % (entry, os.path.exists(entry))) |
| 213 | else: |
| 214 | search_files.append(entry) |
| 215 | if search_files: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 216 | s_dir = d.getVar('EXTERNALSRC') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 217 | for root, _, files in os.walk(s_dir): |
| 218 | for f in files: |
| 219 | if f in search_files: |
| 220 | out_items.append('%s:True' % os.path.join(root, f)) |
| 221 | return ' '.join(out_items) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 222 | |
| 223 | EXPORT_FUNCTIONS do_buildclean |