blob: 56882a41d84869e892ba96a4413b404f5e019177 [file] [log] [blame]
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001# Development tool - source extraction helper class
2#
3# NOTE: this class is intended for use by devtool and should not be
4# inherited manually.
5#
6# Copyright (C) 2014-2017 Intel Corporation
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License version 2 as
10# published by the Free Software Foundation.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License along
18# with this program; if not, write to the Free Software Foundation, Inc.,
19# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21
22DEVTOOL_TEMPDIR ?= ""
23DEVTOOL_PATCH_SRCDIR = "${DEVTOOL_TEMPDIR}/patchworkdir"
24
25
26python() {
27 tempdir = d.getVar('DEVTOOL_TEMPDIR')
28
29 if not tempdir:
30 bb.fatal('devtool-source class is for internal use by devtool only')
31
32 # Make a subdir so we guard against WORKDIR==S
33 workdir = os.path.join(tempdir, 'workdir')
34 d.setVar('WORKDIR', workdir)
35 if not d.getVar('S').startswith(workdir):
36 # Usually a shared workdir recipe (kernel, gcc)
37 # Try to set a reasonable default
38 if bb.data.inherits_class('kernel', d):
39 d.setVar('S', '${WORKDIR}/source')
40 else:
41 d.setVar('S', '${WORKDIR}/%s' % os.path.basename(d.getVar('S')))
42 if bb.data.inherits_class('kernel', d):
43 # We don't want to move the source to STAGING_KERNEL_DIR here
44 d.setVar('STAGING_KERNEL_DIR', '${S}')
45
46 d.setVar('STAMPS_DIR', os.path.join(tempdir, 'stamps'))
47 d.setVar('T', os.path.join(tempdir, 'temp'))
48
49 # Hook in pre/postfuncs
50 is_kernel_yocto = bb.data.inherits_class('kernel-yocto', d)
51 if is_kernel_yocto:
52 unpacktask = 'do_kernel_checkout'
53 d.appendVarFlag('do_configure', 'postfuncs', ' devtool_post_configure')
54 else:
55 unpacktask = 'do_unpack'
56 d.appendVarFlag(unpacktask, 'postfuncs', ' devtool_post_unpack')
57 d.prependVarFlag('do_patch', 'prefuncs', ' devtool_pre_patch')
58 d.appendVarFlag('do_patch', 'postfuncs', ' devtool_post_patch')
59
60 # NOTE: in order for the patch stuff to be fully functional,
61 # PATCHTOOL and PATCH_COMMIT_FUNCTIONS need to be set; we can't
62 # do that here because we can't guarantee the order of the anonymous
63 # functions, so it gets done in the bbappend we create.
64}
65
66
67python devtool_post_unpack() {
68 import oe.recipeutils
69 import shutil
70 sys.path.insert(0, os.path.join(d.getVar('COREBASE'), 'scripts', 'lib'))
71 import scriptutils
72 from devtool import setup_git_repo
73
74 tempdir = d.getVar('DEVTOOL_TEMPDIR')
75 workdir = d.getVar('WORKDIR')
76 srcsubdir = d.getVar('S')
77
78 def _move_file(src, dst):
79 """Move a file. Creates all the directory components of destination path."""
80 dst_d = os.path.dirname(dst)
81 if dst_d:
82 bb.utils.mkdirhier(dst_d)
83 shutil.move(src, dst)
84
85 def _ls_tree(directory):
86 """Recursive listing of files in a directory"""
87 ret = []
88 for root, dirs, files in os.walk(directory):
89 ret.extend([os.path.relpath(os.path.join(root, fname), directory) for
90 fname in files])
91 return ret
92
93 # Move local source files into separate subdir
94 recipe_patches = [os.path.basename(patch) for patch in
95 oe.recipeutils.get_recipe_patches(d)]
96 local_files = oe.recipeutils.get_recipe_local_files(d)
97
98 # Ignore local files with subdir={BP}
99 srcabspath = os.path.abspath(srcsubdir)
100 local_files = [fname for fname in local_files if
101 os.path.exists(os.path.join(workdir, fname)) and
102 (srcabspath == workdir or not
103 os.path.join(workdir, fname).startswith(srcabspath +
104 os.sep))]
105 if local_files:
106 for fname in local_files:
107 _move_file(os.path.join(workdir, fname),
108 os.path.join(tempdir, 'oe-local-files', fname))
109 with open(os.path.join(tempdir, 'oe-local-files', '.gitignore'),
110 'w') as f:
111 f.write('# Ignore local files, by default. Remove this file '
112 'if you want to commit the directory to Git\n*\n')
113
114 if srcsubdir == workdir:
115 # Find non-patch non-local sources that were "unpacked" to srctree
116 # directory
117 src_files = [fname for fname in _ls_tree(workdir) if
118 os.path.basename(fname) not in recipe_patches]
119 srcsubdir = d.getVar('DEVTOOL_PATCH_SRCDIR')
120 # Move source files to S
121 for path in src_files:
122 _move_file(os.path.join(workdir, path),
123 os.path.join(srcsubdir, path))
124 elif os.path.dirname(srcsubdir) != workdir:
125 # Handle if S is set to a subdirectory of the source
126 srcsubdir = os.path.join(workdir, os.path.relpath(srcsubdir, workdir).split(os.sep)[0])
127
128 scriptutils.git_convert_standalone_clone(srcsubdir)
129
130 # Make sure that srcsubdir exists
131 bb.utils.mkdirhier(srcsubdir)
132 if not os.listdir(srcsubdir):
133 bb.warn("No source unpacked to S - either the %s recipe "
134 "doesn't use any source or the correct source "
135 "directory could not be determined" % d.getVar('PN'))
136
137 devbranch = d.getVar('DEVTOOL_DEVBRANCH')
138 setup_git_repo(srcsubdir, d.getVar('PV'), devbranch, d=d)
139
140 (stdout, _) = bb.process.run('git rev-parse HEAD', cwd=srcsubdir)
141 initial_rev = stdout.rstrip()
142 with open(os.path.join(tempdir, 'initial_rev'), 'w') as f:
143 f.write(initial_rev)
144
145 with open(os.path.join(tempdir, 'srcsubdir'), 'w') as f:
146 f.write(srcsubdir)
147}
148
149python devtool_pre_patch() {
150 if d.getVar('S') == d.getVar('WORKDIR'):
151 d.setVar('S', '${DEVTOOL_PATCH_SRCDIR}')
152}
153
154python devtool_post_patch() {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400155 import shutil
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500156 tempdir = d.getVar('DEVTOOL_TEMPDIR')
157 with open(os.path.join(tempdir, 'srcsubdir'), 'r') as f:
158 srcsubdir = f.read()
Brad Bishop316dfdd2018-06-25 12:45:53 -0400159 with open(os.path.join(tempdir, 'initial_rev'), 'r') as f:
160 initial_rev = f.read()
161
162 def rm_patches():
163 patches_dir = os.path.join(srcsubdir, 'patches')
164 if os.path.exists(patches_dir):
165 shutil.rmtree(patches_dir)
166 # Restore any "patches" directory that was actually part of the source tree
167 try:
168 bb.process.run('git checkout -- patches', cwd=srcsubdir)
169 except bb.process.ExecutionError:
170 pass
171
172 extra_overrides = d.getVar('DEVTOOL_EXTRA_OVERRIDES')
173 if extra_overrides:
174 extra_override_list = extra_overrides.split(':')
175 devbranch = d.getVar('DEVTOOL_DEVBRANCH')
176 default_overrides = d.getVar('OVERRIDES').split(':')
177 no_overrides = []
178 # First, we may have some overrides that are referred to in the recipe set in
179 # our configuration, so we need to make a branch that excludes those
180 for override in default_overrides:
181 if override not in extra_override_list:
182 no_overrides.append(override)
183 if default_overrides != no_overrides:
184 # Some overrides are active in the current configuration, so
185 # we need to create a branch where none of the overrides are active
186 bb.process.run('git checkout %s -b devtool-no-overrides' % initial_rev, cwd=srcsubdir)
187 # Run do_patch function with the override applied
188 localdata = bb.data.createCopy(d)
189 localdata.setVar('OVERRIDES', ':'.join(no_overrides))
190 bb.build.exec_func('do_patch', localdata)
191 rm_patches()
192 # Now we need to reconcile the dev branch with the no-overrides one
193 # (otherwise we'd likely be left with identical commits that have different hashes)
194 bb.process.run('git checkout %s' % devbranch, cwd=srcsubdir)
195 bb.process.run('git rebase devtool-no-overrides', cwd=srcsubdir)
196 else:
197 bb.process.run('git checkout %s -b devtool-no-overrides' % devbranch, cwd=srcsubdir)
198
199 for override in extra_override_list:
200 localdata = bb.data.createCopy(d)
201 if override in default_overrides:
202 bb.process.run('git branch devtool-override-%s %s' % (override, devbranch), cwd=srcsubdir)
203 else:
204 # Reset back to the initial commit on a new branch
205 bb.process.run('git checkout %s -b devtool-override-%s' % (initial_rev, override), cwd=srcsubdir)
206 # Run do_patch function with the override applied
207 localdata.appendVar('OVERRIDES', ':%s' % override)
208 bb.build.exec_func('do_patch', localdata)
209 rm_patches()
210 # Now we need to reconcile the new branch with the no-overrides one
211 # (otherwise we'd likely be left with identical commits that have different hashes)
212 bb.process.run('git rebase devtool-no-overrides', cwd=srcsubdir)
213 bb.process.run('git checkout %s' % devbranch, cwd=srcsubdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500214 bb.process.run('git tag -f devtool-patched', cwd=srcsubdir)
215}
216
217python devtool_post_configure() {
218 import shutil
219 tempdir = d.getVar('DEVTOOL_TEMPDIR')
220 shutil.copy2(os.path.join(d.getVar('B'), '.config'), tempdir)
221}