blob: 78a08c80ad6f143eefb0f021b7b5c6a48c0fa04b [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# 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 Bishop6e60e8b2018-02-01 10:27:11 -05007# externalsrc.bbclass enables use of an existing source tree, usually external to
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008# 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"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050028EXTERNALSRC_SYMLINKS ?= "oe-workdir:${WORKDIR} oe-logs:${T}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050029
30python () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050031 externalsrc = d.getVar('EXTERNALSRC')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050032 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 Williamsc0f7c042017-02-23 20:41:17 -060038
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 Bishop6e60e8b2018-02-01 10:27:11 -050043 bpn = d.getVar('BPN')
Brad Bishop004d4992018-10-02 23:54:45 +020044 classextend = (d.getVar('BBCLASSEXTEND') or '').split()
45 if bpn == d.getVar('PN') or not classextend:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060046 if (externalsrc or
47 ('native' in classextend and
Brad Bishop6e60e8b2018-02-01 10:27:11 -050048 d.getVar('EXTERNALSRC_pn-%s-native' % bpn)) or
Patrick Williamsc0f7c042017-02-23 20:41:17 -060049 ('nativesdk' in classextend and
Brad Bishop6e60e8b2018-02-01 10:27:11 -050050 d.getVar('EXTERNALSRC_pn-nativesdk-%s' % bpn)) or
Patrick Williamsc0f7c042017-02-23 20:41:17 -060051 ('cross' in classextend and
Brad Bishop6e60e8b2018-02-01 10:27:11 -050052 d.getVar('EXTERNALSRC_pn-%s-cross' % bpn))):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060053 d.setVar('BB_DONT_CACHE', '1')
54
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055 if externalsrc:
Brad Bishop316dfdd2018-06-25 12:45:53 -040056 import oe.recipeutils
57 import oe.path
58
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 d.setVar('S', externalsrc)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050060 if externalsrcbuild:
61 d.setVar('B', externalsrcbuild)
62 else:
63 d.setVar('B', '${WORKDIR}/${BPN}-${PV}/')
64
65 local_srcuri = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -050066 fetch = bb.fetch2.Fetch((d.getVar('SRC_URI') or '').split(), d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050067 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 if '{SRCPV}' in d.getVar('PV', False):
77 # Dummy value because the default function can't be called with blank SRC_URI
78 d.setVar('SRCPV', '999')
79
Brad Bishop6e60e8b2018-02-01 10:27:11 -050080 tasks = filter(lambda k: d.getVarFlag(k, "task"), d.keys())
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081
82 for task in tasks:
83 if task.endswith("_setscene"):
84 # sstate is never going to work for external source trees, disable it
85 bb.build.deltask(task, d)
86 else:
87 # Since configure will likely touch ${S}, ensure only we lock so one task has access at a time
88 d.appendVarFlag(task, "lockfiles", " ${S}/singletask.lock")
89
90 # We do not want our source to be wiped out, ever (kernel.bbclass does this for do_clean)
Brad Bishop316dfdd2018-06-25 12:45:53 -040091 cleandirs = oe.recipeutils.split_var_value(d.getVarFlag(task, 'cleandirs', False) or '')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092 setvalue = False
93 for cleandir in cleandirs[:]:
Brad Bishop316dfdd2018-06-25 12:45:53 -040094 if oe.path.is_path_parent(externalsrc, d.expand(cleandir)):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050095 cleandirs.remove(cleandir)
96 setvalue = True
97 if setvalue:
98 d.setVarFlag(task, 'cleandirs', ' '.join(cleandirs))
99
100 fetch_tasks = ['do_fetch', 'do_unpack']
101 # If we deltask do_patch, there's no dependency to ensure do_unpack gets run, so add one
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500102 # Note that we cannot use d.appendVarFlag() here because deps is expected to be a list object, not a string
103 d.setVarFlag('do_configure', 'deps', (d.getVarFlag('do_configure', 'deps', False) or []) + ['do_unpack'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500105 for task in d.getVar("SRCTREECOVEREDTASKS").split():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106 if local_srcuri and task in fetch_tasks:
107 continue
108 bb.build.deltask(task, d)
109
110 d.prependVarFlag('do_compile', 'prefuncs', "externalsrc_compile_prefunc ")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500111 d.prependVarFlag('do_configure', 'prefuncs', "externalsrc_configure_prefunc ")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500113 d.setVarFlag('do_compile', 'file-checksums', '${@srctree_hash_files(d)}')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600114 d.setVarFlag('do_configure', 'file-checksums', '${@srctree_configure_hash_files(d)}')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500115
116 # We don't want the workdir to go away
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500117 d.appendVar('RM_WORK_EXCLUDE', ' ' + d.getVar('PN'))
118
119 bb.build.addtask('do_buildclean',
120 'do_clean' if d.getVar('S') == d.getVar('B') else None,
121 None, d)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500122
123 # If B=S the same builddir is used even for different architectures.
124 # Thus, use a shared CONFIGURESTAMPFILE and STAMP directory so that
125 # change of do_configure task hash is correctly detected and stamps are
126 # invalidated if e.g. MACHINE changes.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500127 if d.getVar('S') == d.getVar('B'):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500128 configstamp = '${TMPDIR}/work-shared/${PN}/${EXTENDPE}${PV}-${PR}/configure.sstate'
129 d.setVar('CONFIGURESTAMPFILE', configstamp)
130 d.setVar('STAMP', '${STAMPS_DIR}/work-shared/${PN}/${EXTENDPE}${PV}-${PR}')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500131 d.setVar('STAMPCLEAN', '${STAMPS_DIR}/work-shared/${PN}/*-*')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500132}
133
134python externalsrc_configure_prefunc() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500135 s_dir = d.getVar('S')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500136 # Create desired symlinks
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500137 symlinks = (d.getVar('EXTERNALSRC_SYMLINKS') or '').split()
138 newlinks = []
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500139 for symlink in symlinks:
140 symsplit = symlink.split(':', 1)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500141 lnkfile = os.path.join(s_dir, symsplit[0])
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500142 target = d.expand(symsplit[1])
143 if len(symsplit) > 1:
144 if os.path.islink(lnkfile):
145 # Link already exists, leave it if it points to the right location already
146 if os.readlink(lnkfile) == target:
147 continue
148 os.unlink(lnkfile)
149 elif os.path.exists(lnkfile):
150 # File/dir exists with same name as link, just leave it alone
151 continue
152 os.symlink(target, lnkfile)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500153 newlinks.append(symsplit[0])
154 # Hide the symlinks from git
155 try:
156 git_exclude_file = os.path.join(s_dir, '.git/info/exclude')
157 if os.path.exists(git_exclude_file):
158 with open(git_exclude_file, 'r+') as efile:
159 elines = efile.readlines()
160 for link in newlinks:
161 if link in elines or '/'+link in elines:
162 continue
163 efile.write('/' + link + '\n')
164 except IOError as ioe:
165 bb.note('Failed to hide EXTERNALSRC_SYMLINKS from git')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500166}
167
168python externalsrc_compile_prefunc() {
169 # Make it obvious that this is happening, since forgetting about it could lead to much confusion
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500170 bb.plain('NOTE: %s: compiling from external source tree %s' % (d.getVar('PN'), d.getVar('EXTERNALSRC')))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500171}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500172
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500173do_buildclean[dirs] = "${S} ${B}"
174do_buildclean[nostamp] = "1"
175do_buildclean[doc] = "Call 'make clean' or equivalent in ${B}"
176externalsrc_do_buildclean() {
177 if [ -e Makefile -o -e makefile -o -e GNUmakefile ]; then
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500178 rm -f ${@' '.join([x.split(':')[0] for x in (d.getVar('EXTERNALSRC_SYMLINKS') or '').split()])}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400179 if [ "${CLEANBROKEN}" != "1" ]; then
180 oe_runmake clean || die "make failed"
181 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500182 else
183 bbnote "nothing to do - no makefile found"
184 fi
185}
186
187def srctree_hash_files(d, srcdir=None):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500188 import shutil
189 import subprocess
190 import tempfile
191
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500192 s_dir = srcdir or d.getVar('EXTERNALSRC')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500193 git_dir = None
194
195 try:
196 git_dir = os.path.join(s_dir,
Brad Bishop316dfdd2018-06-25 12:45:53 -0400197 subprocess.check_output(['git', '-C', s_dir, 'rev-parse', '--git-dir'], stderr=subprocess.DEVNULL).decode("utf-8").rstrip())
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500198 except subprocess.CalledProcessError:
199 pass
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500200
201 ret = " "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500202 if git_dir is not None:
203 oe_hash_file = os.path.join(git_dir, 'oe-devtool-tree-sha1')
204 with tempfile.NamedTemporaryFile(prefix='oe-devtool-index') as tmp_index:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500205 # Clone index
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500206 shutil.copyfile(os.path.join(git_dir, 'index'), tmp_index.name)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500207 # Update our custom index
208 env = os.environ.copy()
209 env['GIT_INDEX_FILE'] = tmp_index.name
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500210 subprocess.check_output(['git', 'add', '-A', '.'], cwd=s_dir, env=env)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600211 sha1 = subprocess.check_output(['git', 'write-tree'], cwd=s_dir, env=env).decode("utf-8")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500212 with open(oe_hash_file, 'w') as fobj:
213 fobj.write(sha1)
214 ret = oe_hash_file + ':True'
215 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500216 ret = s_dir + '/*:True'
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500217 return ret
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600218
219def srctree_configure_hash_files(d):
220 """
221 Get the list of files that should trigger do_configure to re-execute,
222 based on the value of CONFIGURE_FILES
223 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500224 in_files = (d.getVar('CONFIGURE_FILES') or '').split()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600225 out_items = []
226 search_files = []
227 for entry in in_files:
228 if entry.startswith('/'):
229 out_items.append('%s:%s' % (entry, os.path.exists(entry)))
230 else:
231 search_files.append(entry)
232 if search_files:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500233 s_dir = d.getVar('EXTERNALSRC')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600234 for root, _, files in os.walk(s_dir):
235 for f in files:
236 if f in search_files:
237 out_items.append('%s:True' % os.path.join(root, f))
238 return ' '.join(out_items)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500239
240EXPORT_FUNCTIONS do_buildclean