blob: 29ac6d418fb3c5eb4bdd6f1a929056b8a0725706 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# This class should provide easy access to the different aspects of the
2# buildsystem such as layers, bitbake location, etc.
3import stat
4import shutil
5
6def _smart_copy(src, dest):
Brad Bishop37a0e4d2017-12-04 01:01:44 -05007 import subprocess
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008 # smart_copy will choose the correct function depending on whether the
9 # source is a file or a directory.
10 mode = os.stat(src).st_mode
11 if stat.S_ISDIR(mode):
Brad Bishop37a0e4d2017-12-04 01:01:44 -050012 bb.utils.mkdirhier(dest)
13 cmd = "tar --exclude='.git' --xattrs --xattrs-include='*' -chf - -C %s -p . \
14 | tar --xattrs --xattrs-include='*' -xf - -C %s" % (src, dest)
15 subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016 else:
17 shutil.copyfile(src, dest)
18 shutil.copymode(src, dest)
19
20class BuildSystem(object):
Patrick Williamsf1e5d692016-03-30 15:21:19 -050021 def __init__(self, context, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050022 self.d = d
Patrick Williamsf1e5d692016-03-30 15:21:19 -050023 self.context = context
Patrick Williamsc0f7c042017-02-23 20:41:17 -060024 self.layerdirs = [os.path.abspath(pth) for pth in d.getVar('BBLAYERS', True).split()]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050025 self.layers_exclude = (d.getVar('SDK_LAYERS_EXCLUDE', True) or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050027 def copy_bitbake_and_layers(self, destdir, workspace_name=None):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028 # Copy in all metadata layers + bitbake (as repositories)
29 layers_copied = []
30 bb.utils.mkdirhier(destdir)
31 layers = list(self.layerdirs)
32
Patrick Williamsc0f7c042017-02-23 20:41:17 -060033 corebase = os.path.abspath(self.d.getVar('COREBASE', True))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050034 layers.append(corebase)
35
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050036 # Exclude layers
37 for layer_exclude in self.layers_exclude:
38 if layer_exclude in layers:
39 layers.remove(layer_exclude)
40
41 workspace_newname = workspace_name
42 if workspace_newname:
43 layernames = [os.path.basename(layer) for layer in layers]
44 extranum = 0
45 while workspace_newname in layernames:
46 extranum += 1
47 workspace_newname = '%s-%d' % (workspace_name, extranum)
48
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049 corebase_files = self.d.getVar('COREBASE_FILES', True).split()
50 corebase_files = [corebase + '/' +x for x in corebase_files]
51 # Make sure bitbake goes in
52 bitbake_dir = bb.__file__.rsplit('/', 3)[0]
53 corebase_files.append(bitbake_dir)
54
55 for layer in layers:
56 layerconf = os.path.join(layer, 'conf', 'layer.conf')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050057 layernewname = os.path.basename(layer)
58 workspace = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 if os.path.exists(layerconf):
60 with open(layerconf, 'r') as f:
61 if f.readline().startswith("# ### workspace layer auto-generated by devtool ###"):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050062 if workspace_newname:
63 layernewname = workspace_newname
64 workspace = True
65 else:
66 bb.plain("NOTE: Excluding local workspace layer %s from %s" % (layer, self.context))
67 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068
69 # If the layer was already under corebase, leave it there
70 # since layers such as meta have issues when moved.
71 layerdestpath = destdir
72 if corebase == os.path.dirname(layer):
73 layerdestpath += '/' + os.path.basename(corebase)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050074 layerdestpath += '/' + layernewname
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075
76 layer_relative = os.path.relpath(layerdestpath,
77 destdir)
78 layers_copied.append(layer_relative)
79
80 # Treat corebase as special since it typically will contain
81 # build directories or other custom items.
82 if corebase == layer:
83 bb.utils.mkdirhier(layerdestpath)
84 for f in corebase_files:
85 f_basename = os.path.basename(f)
86 destname = os.path.join(layerdestpath, f_basename)
87 _smart_copy(f, destname)
88 else:
89 if os.path.exists(layerdestpath):
90 bb.note("Skipping layer %s, already handled" % layer)
91 else:
92 _smart_copy(layer, layerdestpath)
93
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050094 if workspace:
95 # Make some adjustments original workspace layer
96 # Drop sources (recipe tasks will be locked, so we don't need them)
97 srcdir = os.path.join(layerdestpath, 'sources')
98 if os.path.isdir(srcdir):
99 shutil.rmtree(srcdir)
100 # Drop all bbappends except the one for the image the SDK is being built for
101 # (because of externalsrc, the workspace bbappends will interfere with the
102 # locked signatures if present, and we don't need them anyway)
103 image_bbappend = os.path.splitext(os.path.basename(self.d.getVar('FILE', True)))[0] + '.bbappend'
104 appenddir = os.path.join(layerdestpath, 'appends')
105 if os.path.isdir(appenddir):
106 for fn in os.listdir(appenddir):
107 if fn == image_bbappend:
108 continue
109 else:
110 os.remove(os.path.join(appenddir, fn))
111 # Drop README
112 readme = os.path.join(layerdestpath, 'README')
113 if os.path.exists(readme):
114 os.remove(readme)
115 # Filter out comments in layer.conf and change layer name
116 layerconf = os.path.join(layerdestpath, 'conf', 'layer.conf')
117 with open(layerconf, 'r') as f:
118 origlines = f.readlines()
119 with open(layerconf, 'w') as f:
120 for line in origlines:
121 if line.startswith('#'):
122 continue
123 line = line.replace('workspacelayer', workspace_newname)
124 f.write(line)
125
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500126 return layers_copied
127
128def generate_locked_sigs(sigfile, d):
129 bb.utils.mkdirhier(os.path.dirname(sigfile))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500130 depd = d.getVar('BB_TASKDEPDATA', False)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600131 tasks = ['%s.%s' % (v[2], v[1]) for v in depd.values()]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132 bb.parse.siggen.dump_lockedsigs(sigfile, tasks)
133
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500134def prune_lockedsigs(excluded_tasks, excluded_targets, lockedsigs, pruned_output):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500135 with open(lockedsigs, 'r') as infile:
136 bb.utils.mkdirhier(os.path.dirname(pruned_output))
137 with open(pruned_output, 'w') as f:
138 invalue = False
139 for line in infile:
140 if invalue:
141 if line.endswith('\\\n'):
142 splitval = line.strip().split(':')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500143 if not splitval[1] in excluded_tasks and not splitval[0] in excluded_targets:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500144 f.write(line)
145 else:
146 f.write(line)
147 invalue = False
148 elif line.startswith('SIGGEN_LOCKEDSIGS'):
149 invalue = True
150 f.write(line)
151
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600152def merge_lockedsigs(copy_tasks, lockedsigs_main, lockedsigs_extra, merged_output, copy_output=None):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500153 merged = {}
154 arch_order = []
155 with open(lockedsigs_main, 'r') as f:
156 invalue = None
157 for line in f:
158 if invalue:
159 if line.endswith('\\\n'):
160 merged[invalue].append(line)
161 else:
162 invalue = None
163 elif line.startswith('SIGGEN_LOCKEDSIGS_t-'):
164 invalue = line[18:].split('=', 1)[0].rstrip()
165 merged[invalue] = []
166 arch_order.append(invalue)
167
168 with open(lockedsigs_extra, 'r') as f:
169 invalue = None
170 tocopy = {}
171 for line in f:
172 if invalue:
173 if line.endswith('\\\n'):
174 if not line in merged[invalue]:
175 target, task = line.strip().split(':')[:2]
176 if not copy_tasks or task in copy_tasks:
177 tocopy[invalue].append(line)
178 merged[invalue].append(line)
179 else:
180 invalue = None
181 elif line.startswith('SIGGEN_LOCKEDSIGS_t-'):
182 invalue = line[18:].split('=', 1)[0].rstrip()
183 if not invalue in merged:
184 merged[invalue] = []
185 arch_order.append(invalue)
186 tocopy[invalue] = []
187
188 def write_sigs_file(fn, types, sigs):
189 fulltypes = []
190 bb.utils.mkdirhier(os.path.dirname(fn))
191 with open(fn, 'w') as f:
192 for typename in types:
193 lines = sigs[typename]
194 if lines:
195 f.write('SIGGEN_LOCKEDSIGS_%s = "\\\n' % typename)
196 for line in lines:
197 f.write(line)
198 f.write(' "\n')
199 fulltypes.append(typename)
200 f.write('SIGGEN_LOCKEDSIGS_TYPES = "%s"\n' % ' '.join(fulltypes))
201
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600202 if copy_output:
203 write_sigs_file(copy_output, list(tocopy.keys()), tocopy)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500204 if merged_output:
205 write_sigs_file(merged_output, arch_order, merged)
206
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600207def create_locked_sstate_cache(lockedsigs, input_sstate_cache, output_sstate_cache, d, fixedlsbstring="", filterfile=None):
208 import shutil
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500209 bb.note('Generating sstate-cache...')
210
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500211 nativelsbstring = d.getVar('NATIVELSBSTRING', True)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600212 bb.process.run("gen-lockedsig-cache %s %s %s %s %s" % (lockedsigs, input_sstate_cache, output_sstate_cache, nativelsbstring, filterfile or ''))
213 if fixedlsbstring and nativelsbstring != fixedlsbstring:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500214 nativedir = output_sstate_cache + '/' + nativelsbstring
215 if os.path.isdir(nativedir):
216 destdir = os.path.join(output_sstate_cache, fixedlsbstring)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600217 for root, _, files in os.walk(nativedir):
218 for fn in files:
219 src = os.path.join(root, fn)
220 dest = os.path.join(destdir, os.path.relpath(src, nativedir))
221 if os.path.exists(dest):
222 # Already exists, and it'll be the same file, so just delete it
223 os.unlink(src)
224 else:
225 bb.utils.mkdirhier(os.path.dirname(dest))
226 shutil.move(src, dest)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500227
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600228def check_sstate_task_list(d, targets, filteroutfile, cmdprefix='', cwd=None, logfile=None):
229 import subprocess
230
231 bb.note('Generating sstate task list...')
232
233 if not cwd:
234 cwd = os.getcwd()
235 if logfile:
236 logparam = '-l %s' % logfile
237 else:
238 logparam = ''
239 cmd = "%sBB_SETSCENE_ENFORCE=1 PSEUDO_DISABLED=1 oe-check-sstate %s -s -o %s %s" % (cmdprefix, targets, filteroutfile, logparam)
240 env = dict(d.getVar('BB_ORIGENV', False))
241 env.pop('BUILDDIR', '')
242 pathitems = env['PATH'].split(':')
243 env['PATH'] = ':'.join([item for item in pathitems if not item.endswith('/bitbake/bin')])
244 bb.process.run(cmd, stderr=subprocess.STDOUT, env=env, cwd=cwd, executable='/bin/bash')