blob: d64af6a9c9ad15df59ebda9f918593d7f7b1e179 [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')
Patrick Williamsc0f7c042017-02-23 20:41:17 -060032
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 Bishop6e60e8b2018-02-01 10:27:11 -050037 bpn = d.getVar('BPN')
38 if bpn == d.getVar('PN'):
39 classextend = (d.getVar('BBCLASSEXTEND') or '').split()
Patrick Williamsc0f7c042017-02-23 20:41:17 -060040 if (externalsrc or
41 ('native' in classextend and
Brad Bishop6e60e8b2018-02-01 10:27:11 -050042 d.getVar('EXTERNALSRC_pn-%s-native' % bpn)) or
Patrick Williamsc0f7c042017-02-23 20:41:17 -060043 ('nativesdk' in classextend and
Brad Bishop6e60e8b2018-02-01 10:27:11 -050044 d.getVar('EXTERNALSRC_pn-nativesdk-%s' % bpn)) or
Patrick Williamsc0f7c042017-02-23 20:41:17 -060045 ('cross' in classextend and
Brad Bishop6e60e8b2018-02-01 10:27:11 -050046 d.getVar('EXTERNALSRC_pn-%s-cross' % bpn))):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060047 d.setVar('BB_DONT_CACHE', '1')
48
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049 if externalsrc:
50 d.setVar('S', externalsrc)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050051 externalsrcbuild = d.getVar('EXTERNALSRC_BUILD')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052 if externalsrcbuild:
53 d.setVar('B', externalsrcbuild)
54 else:
55 d.setVar('B', '${WORKDIR}/${BPN}-${PV}/')
56
57 local_srcuri = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -050058 fetch = bb.fetch2.Fetch((d.getVar('SRC_URI') or '').split(), d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 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 Bishop6e60e8b2018-02-01 10:27:11 -050072 tasks = filter(lambda k: d.getVarFlag(k, "task"), d.keys())
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073
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 Williamsf1e5d692016-03-30 15:21:19 -050094 # 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 Williamsc124f4f2015-09-15 14:41:29 -050096
Brad Bishop6e60e8b2018-02-01 10:27:11 -050097 for task in d.getVar("SRCTREECOVEREDTASKS").split():
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098 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 Williamsd8c66bc2016-06-20 12:57:21 -0500103 d.prependVarFlag('do_configure', 'prefuncs', "externalsrc_configure_prefunc ")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500105 d.setVarFlag('do_compile', 'file-checksums', '${@srctree_hash_files(d)}')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600106 d.setVarFlag('do_configure', 'file-checksums', '${@srctree_configure_hash_files(d)}')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500107
108 # We don't want the workdir to go away
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500109 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 Williamsd8c66bc2016-06-20 12:57:21 -0500114
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 Bishop6e60e8b2018-02-01 10:27:11 -0500119 if d.getVar('S') == d.getVar('B'):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500120 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 Bishop6e60e8b2018-02-01 10:27:11 -0500123 d.setVar('STAMPCLEAN', '${STAMPS_DIR}/work-shared/${PN}/*-*')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500124}
125
126python externalsrc_configure_prefunc() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500127 s_dir = d.getVar('S')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500128 # Create desired symlinks
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500129 symlinks = (d.getVar('EXTERNALSRC_SYMLINKS') or '').split()
130 newlinks = []
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500131 for symlink in symlinks:
132 symsplit = symlink.split(':', 1)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500133 lnkfile = os.path.join(s_dir, symsplit[0])
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500134 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 Bishop6e60e8b2018-02-01 10:27:11 -0500145 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 Williamsc124f4f2015-09-15 14:41:29 -0500158}
159
160python externalsrc_compile_prefunc() {
161 # Make it obvious that this is happening, since forgetting about it could lead to much confusion
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500162 bb.plain('NOTE: %s: compiling from external source tree %s' % (d.getVar('PN'), d.getVar('EXTERNALSRC')))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500163}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500164
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500165do_buildclean[dirs] = "${S} ${B}"
166do_buildclean[nostamp] = "1"
167do_buildclean[doc] = "Call 'make clean' or equivalent in ${B}"
168externalsrc_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
176def srctree_hash_files(d, srcdir=None):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500177 import shutil
178 import subprocess
179 import tempfile
180
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500181 s_dir = srcdir or d.getVar('EXTERNALSRC')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500182 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 Bishop6e60e8b2018-02-01 10:27:11 -0500193 subprocess.check_output(['git', 'add', '-A', '.'], cwd=s_dir, env=env)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600194 sha1 = subprocess.check_output(['git', 'write-tree'], cwd=s_dir, env=env).decode("utf-8")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500195 with open(oe_hash_file, 'w') as fobj:
196 fobj.write(sha1)
197 ret = oe_hash_file + ':True'
198 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500199 ret = s_dir + '/*:True'
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500200 return ret
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600201
202def 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 Bishop6e60e8b2018-02-01 10:27:11 -0500207 in_files = (d.getVar('CONFIGURE_FILES') or '').split()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600208 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 Bishop6e60e8b2018-02-01 10:27:11 -0500216 s_dir = d.getVar('EXTERNALSRC')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600217 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 Bishop6e60e8b2018-02-01 10:27:11 -0500222
223EXPORT_FUNCTIONS do_buildclean