blob: da7eb4781c51a4a0cf44ef5f7c30777971fcb0ac [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#
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
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 () {
31 externalsrc = d.getVar('EXTERNALSRC', True)
32 if externalsrc:
33 d.setVar('S', externalsrc)
34 externalsrcbuild = d.getVar('EXTERNALSRC_BUILD', True)
35 if externalsrcbuild:
36 d.setVar('B', externalsrcbuild)
37 else:
38 d.setVar('B', '${WORKDIR}/${BPN}-${PV}/')
39
40 local_srcuri = []
41 fetch = bb.fetch2.Fetch((d.getVar('SRC_URI', True) or '').split(), d)
42 for url in fetch.urls:
43 url_data = fetch.ud[url]
44 parm = url_data.parm
45 if (url_data.type == 'file' or
46 'type' in parm and parm['type'] == 'kmeta'):
47 local_srcuri.append(url)
48
49 d.setVar('SRC_URI', ' '.join(local_srcuri))
50
51 if '{SRCPV}' in d.getVar('PV', False):
52 # Dummy value because the default function can't be called with blank SRC_URI
53 d.setVar('SRCPV', '999')
54
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050055 tasks = filter(lambda k: d.getVarFlag(k, "task", True), d.keys())
Patrick Williamsc124f4f2015-09-15 14:41:29 -050056
57 for task in tasks:
58 if task.endswith("_setscene"):
59 # sstate is never going to work for external source trees, disable it
60 bb.build.deltask(task, d)
61 else:
62 # Since configure will likely touch ${S}, ensure only we lock so one task has access at a time
63 d.appendVarFlag(task, "lockfiles", " ${S}/singletask.lock")
64
65 # We do not want our source to be wiped out, ever (kernel.bbclass does this for do_clean)
66 cleandirs = (d.getVarFlag(task, 'cleandirs', False) or '').split()
67 setvalue = False
68 for cleandir in cleandirs[:]:
69 if d.expand(cleandir) == externalsrc:
70 cleandirs.remove(cleandir)
71 setvalue = True
72 if setvalue:
73 d.setVarFlag(task, 'cleandirs', ' '.join(cleandirs))
74
75 fetch_tasks = ['do_fetch', 'do_unpack']
76 # 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 -050077 # Note that we cannot use d.appendVarFlag() here because deps is expected to be a list object, not a string
78 d.setVarFlag('do_configure', 'deps', (d.getVarFlag('do_configure', 'deps', False) or []) + ['do_unpack'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079
80 for task in d.getVar("SRCTREECOVEREDTASKS", True).split():
81 if local_srcuri and task in fetch_tasks:
82 continue
83 bb.build.deltask(task, d)
84
85 d.prependVarFlag('do_compile', 'prefuncs', "externalsrc_compile_prefunc ")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050086 d.prependVarFlag('do_configure', 'prefuncs', "externalsrc_configure_prefunc ")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050088 # Force the recipe to be always re-parsed so that the file_checksums
89 # function is run every time
90 d.setVar('BB_DONT_CACHE', '1')
91 d.setVarFlag('do_compile', 'file-checksums', '${@srctree_hash_files(d)}')
92
93 # We don't want the workdir to go away
94 d.appendVar('RM_WORK_EXCLUDE', ' ' + d.getVar('PN', True))
95
96 # If B=S the same builddir is used even for different architectures.
97 # Thus, use a shared CONFIGURESTAMPFILE and STAMP directory so that
98 # change of do_configure task hash is correctly detected and stamps are
99 # invalidated if e.g. MACHINE changes.
100 if d.getVar('S', True) == d.getVar('B', True):
101 configstamp = '${TMPDIR}/work-shared/${PN}/${EXTENDPE}${PV}-${PR}/configure.sstate'
102 d.setVar('CONFIGURESTAMPFILE', configstamp)
103 d.setVar('STAMP', '${STAMPS_DIR}/work-shared/${PN}/${EXTENDPE}${PV}-${PR}')
104}
105
106python externalsrc_configure_prefunc() {
107 # Create desired symlinks
108 symlinks = (d.getVar('EXTERNALSRC_SYMLINKS', True) or '').split()
109 for symlink in symlinks:
110 symsplit = symlink.split(':', 1)
111 lnkfile = os.path.join(d.getVar('S', True), symsplit[0])
112 target = d.expand(symsplit[1])
113 if len(symsplit) > 1:
114 if os.path.islink(lnkfile):
115 # Link already exists, leave it if it points to the right location already
116 if os.readlink(lnkfile) == target:
117 continue
118 os.unlink(lnkfile)
119 elif os.path.exists(lnkfile):
120 # File/dir exists with same name as link, just leave it alone
121 continue
122 os.symlink(target, lnkfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500123}
124
125python externalsrc_compile_prefunc() {
126 # Make it obvious that this is happening, since forgetting about it could lead to much confusion
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500127 bb.plain('NOTE: %s: compiling from external source tree %s' % (d.getVar('PN', True), d.getVar('EXTERNALSRC', True)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500128}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500129
130def srctree_hash_files(d):
131 import shutil
132 import subprocess
133 import tempfile
134
135 s_dir = d.getVar('EXTERNALSRC', True)
136 git_dir = os.path.join(s_dir, '.git')
137 oe_hash_file = os.path.join(git_dir, 'oe-devtool-tree-sha1')
138
139 ret = " "
140 if os.path.exists(git_dir):
141 with tempfile.NamedTemporaryFile(dir=git_dir, prefix='oe-devtool-index') as tmp_index:
142 # Clone index
143 shutil.copy2(os.path.join(git_dir, 'index'), tmp_index.name)
144 # Update our custom index
145 env = os.environ.copy()
146 env['GIT_INDEX_FILE'] = tmp_index.name
147 subprocess.check_output(['git', 'add', '.'], cwd=s_dir, env=env)
148 sha1 = subprocess.check_output(['git', 'write-tree'], cwd=s_dir, env=env)
149 with open(oe_hash_file, 'w') as fobj:
150 fobj.write(sha1)
151 ret = oe_hash_file + ':True'
152 else:
153 ret = d.getVar('EXTERNALSRC', True) + '/*:True'
154 return ret