diff options
Diffstat (limited to 'bitbake/bin/bitbake-setup')
-rwxr-xr-x | bitbake/bin/bitbake-setup | 860 |
1 files changed, 860 insertions, 0 deletions
diff --git a/bitbake/bin/bitbake-setup b/bitbake/bin/bitbake-setup new file mode 100755 index 0000000000..38bb8099fd --- /dev/null +++ b/bitbake/bin/bitbake-setup | |||
@@ -0,0 +1,860 @@ | |||
1 | #!/usr/bin/env python3 | ||
2 | |||
3 | # | ||
4 | # SPDX-License-Identifier: GPL-2.0-only | ||
5 | # | ||
6 | |||
7 | import logging | ||
8 | import os | ||
9 | import sys | ||
10 | import argparse | ||
11 | import warnings | ||
12 | import json | ||
13 | import shutil | ||
14 | import time | ||
15 | import stat | ||
16 | import tempfile | ||
17 | import configparser | ||
18 | import datetime | ||
19 | import glob | ||
20 | import subprocess | ||
21 | |||
22 | default_registry = os.path.normpath(os.path.dirname(__file__) + "/../default-registry") | ||
23 | |||
24 | bindir = os.path.abspath(os.path.dirname(__file__)) | ||
25 | sys.path[0:0] = [os.path.join(os.path.dirname(bindir), 'lib')] | ||
26 | |||
27 | import bb.msg | ||
28 | import bb.process | ||
29 | |||
30 | logger = bb.msg.logger_create('bitbake-setup', sys.stdout) | ||
31 | |||
32 | def cache_dir(top_dir): | ||
33 | return os.path.join(top_dir, '.bitbake-setup-cache') | ||
34 | |||
35 | def init_bb_cache(top_dir, settings, args): | ||
36 | dldir = settings["default"]["dl-dir"] | ||
37 | bb_cachedir = os.path.join(cache_dir(top_dir), 'bitbake-cache') | ||
38 | |||
39 | d = bb.data.init() | ||
40 | d.setVar("DL_DIR", dldir) | ||
41 | d.setVar("BB_CACHEDIR", bb_cachedir) | ||
42 | d.setVar("__BBSRCREV_SEEN", "1") | ||
43 | if args.no_network: | ||
44 | d.setVar("BB_SRCREV_POLICY", "cache") | ||
45 | bb.fetch.fetcher_init(d) | ||
46 | return d | ||
47 | |||
48 | def save_bb_cache(): | ||
49 | bb.fetch2.fetcher_parse_save() | ||
50 | bb.fetch2.fetcher_parse_done() | ||
51 | |||
52 | def get_config_name(config): | ||
53 | suffix = '.conf.json' | ||
54 | config_file = os.path.basename(config) | ||
55 | if config_file.endswith(suffix): | ||
56 | return config_file[:-len(suffix)] | ||
57 | else: | ||
58 | raise Exception("Config file {} does not end with {}, please rename the file.".format(config, suffix)) | ||
59 | |||
60 | def write_config(config, config_dir): | ||
61 | with open(os.path.join(config_dir, "config-upstream.json"),'w') as s: | ||
62 | json.dump(config, s, sort_keys=True, indent=4) | ||
63 | |||
64 | def commit_config(config_dir): | ||
65 | bb.process.run("git -C {} add .".format(config_dir)) | ||
66 | bb.process.run("git -C {} commit --no-verify -a -m 'Configuration at {}'".format(config_dir, time.asctime())) | ||
67 | |||
68 | def _write_layer_list(dest, repodirs): | ||
69 | layers = [] | ||
70 | for r in repodirs: | ||
71 | for root, dirs, files in os.walk(os.path.join(dest,r)): | ||
72 | if os.path.basename(root) == 'conf' and 'layer.conf' in files: | ||
73 | layers.append(os.path.relpath(os.path.dirname(root), dest)) | ||
74 | layers_f = os.path.join(dest, ".oe-layers.json") | ||
75 | with open(layers_f, 'w') as f: | ||
76 | json.dump({"version":"1.0","layers":layers}, f, sort_keys=True, indent=4) | ||
77 | |||
78 | def checkout_layers(layers, layerdir, d): | ||
79 | repodirs = [] | ||
80 | oesetupbuild = None | ||
81 | print("Fetching layer/tool repositories into {}".format(layerdir)) | ||
82 | for r_name in layers: | ||
83 | r_data = layers[r_name] | ||
84 | repodir = r_data["path"] | ||
85 | repodirs.append(repodir) | ||
86 | |||
87 | r_remote = r_data['git-remote'] | ||
88 | rev = r_remote['rev'] | ||
89 | branch = r_remote.get('branch', None) | ||
90 | remotes = r_remote['remotes'] | ||
91 | |||
92 | for remote in remotes: | ||
93 | type,host,path,user,pswd,params = bb.fetch.decodeurl(remotes[remote]["uri"]) | ||
94 | fetchuri = bb.fetch.encodeurl(('git',host,path,user,pswd,params)) | ||
95 | print(" {}".format(r_name)) | ||
96 | if branch: | ||
97 | fetcher = bb.fetch.Fetch(["{};protocol={};rev={};branch={};destsuffix={}".format(fetchuri,type,rev,branch,repodir)], d) | ||
98 | else: | ||
99 | fetcher = bb.fetch.Fetch(["{};protocol={};rev={};nobranch=1;destsuffix={}".format(fetchuri,type,rev,repodir)], d) | ||
100 | do_fetch(fetcher, layerdir) | ||
101 | |||
102 | if os.path.exists(os.path.join(layerdir, repodir, 'scripts/oe-setup-build')): | ||
103 | oesetupbuild = os.path.join(layerdir, repodir, 'scripts/oe-setup-build') | ||
104 | oeinitbuildenvdir = os.path.join(layerdir, repodir) | ||
105 | |||
106 | print(" ") | ||
107 | _write_layer_list(layerdir, repodirs) | ||
108 | |||
109 | if oesetupbuild: | ||
110 | links = {'setup-build': oesetupbuild, 'oe-scripts': os.path.dirname(oesetupbuild), 'oe-init-build-env-dir': oeinitbuildenvdir} | ||
111 | for l,t in links.items(): | ||
112 | symlink = os.path.join(layerdir, l) | ||
113 | if os.path.lexists(symlink): | ||
114 | os.remove(symlink) | ||
115 | os.symlink(os.path.relpath(t,layerdir),symlink) | ||
116 | |||
117 | def setup_bitbake_build(bitbake_config, layerdir, builddir, thisdir): | ||
118 | def _setup_build_conf(layers, build_conf_dir): | ||
119 | os.makedirs(build_conf_dir) | ||
120 | layers_s = [] | ||
121 | for l in layers: | ||
122 | if l.startswith("{THISDIR}/"): | ||
123 | if thisdir: | ||
124 | l = l.format(THISDIR=thisdir) | ||
125 | else: | ||
126 | raise Exception("Configuration is using {THISDIR} to specify " \ | ||
127 | "a layer path relative to itself. This can be done only " \ | ||
128 | "when the configuration is specified by its path on local " \ | ||
129 | "disk, not when it's in a registry or is fetched over http.") | ||
130 | if not os.path.isabs(l): | ||
131 | l = os.path.join(layerdir, l) | ||
132 | layers_s.append(" {} \\".format(l)) | ||
133 | layers_s = "\n".join(layers_s) | ||
134 | bblayers_conf = """BBLAYERS ?= " \\ | ||
135 | {} | ||
136 | " | ||
137 | """.format(layers_s) | ||
138 | with open(os.path.join(build_conf_dir, "bblayers.conf"), 'w') as f: | ||
139 | f.write(bblayers_conf) | ||
140 | |||
141 | local_conf = """# | ||
142 | # This file is intended for local configuration tweaks. | ||
143 | # | ||
144 | # If you would like to publish and share changes made to this file, | ||
145 | # it is recommended to put them into a distro config, or to create | ||
146 | # layer fragments from changes made here. | ||
147 | # | ||
148 | """ | ||
149 | with open(os.path.join(build_conf_dir, "local.conf"), 'w') as f: | ||
150 | f.write(local_conf) | ||
151 | |||
152 | with open(os.path.join(build_conf_dir, "templateconf.cfg"), 'w') as f: | ||
153 | f.write("") | ||
154 | |||
155 | with open(os.path.join(build_conf_dir, "conf-summary.txt"), 'w') as f: | ||
156 | f.write(bitbake_config["description"] + "\n") | ||
157 | |||
158 | with open(os.path.join(build_conf_dir, "conf-notes.txt"), 'w') as f: | ||
159 | f.write("") | ||
160 | |||
161 | def _make_init_build_env(builddir, oeinitbuildenvdir): | ||
162 | builddir = os.path.realpath(builddir) | ||
163 | cmd = "cd {}\nset {}\n. ./oe-init-build-env\n".format(oeinitbuildenvdir, builddir) | ||
164 | initbuild_in_builddir = os.path.join(builddir, 'init-build-env') | ||
165 | |||
166 | with open(initbuild_in_builddir, 'w') as f: | ||
167 | f.write("# init-build-env wrapper created by bitbake-setup\n") | ||
168 | f.write(cmd + '\n') | ||
169 | |||
170 | def _prepend_passthrough_to_init_build_env(builddir): | ||
171 | env = bitbake_config.get("bb-env-passthrough-additions") | ||
172 | if not env: | ||
173 | return | ||
174 | |||
175 | initbuild_in_builddir = os.path.join(builddir, 'init-build-env') | ||
176 | with open(initbuild_in_builddir) as f: | ||
177 | content = f.read() | ||
178 | |||
179 | joined = " \\\n".join(env) | ||
180 | env = "export BB_ENV_PASSTHROUGH_ADDITIONS=\" \\\n" | ||
181 | env += "${BB_ENV_PASSTHROUGH_ADDITIONS} \\\n" | ||
182 | env += joined | ||
183 | env += '"' | ||
184 | |||
185 | with open(initbuild_in_builddir, 'w') as f: | ||
186 | f.write("# environment passthrough added by bitbake-setup\n") | ||
187 | f.write(env + '\n') | ||
188 | f.write('\n') | ||
189 | f.write(content) | ||
190 | |||
191 | bitbake_builddir = os.path.join(builddir, "build") | ||
192 | print("Setting up bitbake configuration in\n {}\n".format(bitbake_builddir)) | ||
193 | |||
194 | template = bitbake_config.get("oe-template") | ||
195 | layers = bitbake_config.get("bb-layers") | ||
196 | if not template and not layers: | ||
197 | print("Bitbake configuration does not contain a reference to an OpenEmbedded build template via 'oe-template' or a list of layers via 'bb-layers'; please use oe-setup-build, oe-init-build-env or another mechanism manually to complete the setup.") | ||
198 | return | ||
199 | oesetupbuild = os.path.join(layerdir, 'setup-build') | ||
200 | if template and not os.path.exists(oesetupbuild): | ||
201 | raise Exception("Cannot complete setting up a bitbake build directory from OpenEmbedded template '{}' as oe-setup-build was not found in any layers; please use oe-init-build-env manually.".format(template)) | ||
202 | |||
203 | bitbake_confdir = os.path.join(bitbake_builddir, 'conf') | ||
204 | backup_bitbake_confdir = bitbake_confdir + "-backup.{}".format(time.strftime("%Y%m%d%H%M%S")) | ||
205 | if os.path.exists(bitbake_confdir): | ||
206 | os.rename(bitbake_confdir, backup_bitbake_confdir) | ||
207 | |||
208 | if layers: | ||
209 | _setup_build_conf(layers, bitbake_confdir) | ||
210 | |||
211 | if template: | ||
212 | bb.process.run("{} setup -c {} -b {} --no-shell".format(oesetupbuild, template, bitbake_builddir)) | ||
213 | else: | ||
214 | oeinitbuildenvdir = os.path.join(layerdir, 'oe-init-build-env-dir') | ||
215 | if not os.path.exists(os.path.join(oeinitbuildenvdir, "oe-init-build-env")): | ||
216 | print("Could not find oe-init-build-env in any of the layers; please use another mechanism to initialize the bitbake environment") | ||
217 | return | ||
218 | _make_init_build_env(bitbake_builddir, os.path.realpath(oeinitbuildenvdir)) | ||
219 | |||
220 | _prepend_passthrough_to_init_build_env(bitbake_builddir) | ||
221 | |||
222 | siteconf_symlink = os.path.join(bitbake_confdir, "site.conf") | ||
223 | siteconf = os.path.normpath(os.path.join(builddir, '..', "site.conf")) | ||
224 | if os.path.lexists(siteconf_symlink): | ||
225 | os.remove(symlink) | ||
226 | os.symlink(os.path.relpath(siteconf, bitbake_confdir) ,siteconf_symlink) | ||
227 | |||
228 | |||
229 | init_script = os.path.join(bitbake_builddir, "init-build-env") | ||
230 | shell = "bash" | ||
231 | fragments = bitbake_config.get("oe-fragments", []) + sorted(bitbake_config.get("oe-fragment-choices",{}).values()) | ||
232 | if fragments: | ||
233 | bb.process.run("{} -c '. {} && bitbake-config-build enable-fragment {}'".format(shell, init_script, " ".join(fragments))) | ||
234 | |||
235 | if os.path.exists(backup_bitbake_confdir): | ||
236 | bitbake_config_diff = get_diff(backup_bitbake_confdir, bitbake_confdir) | ||
237 | if bitbake_config_diff: | ||
238 | print("Existing bitbake configuration directory renamed to {}".format(backup_bitbake_confdir)) | ||
239 | print("The bitbake configuration has changed:") | ||
240 | print(bitbake_config_diff) | ||
241 | else: | ||
242 | shutil.rmtree(backup_bitbake_confdir) | ||
243 | |||
244 | print("This bitbake configuration provides:\n {}\n".format(bitbake_config["description"])) | ||
245 | |||
246 | readme = """{}\n\nAdditional information is in {} and {}\n | ||
247 | Source the environment using '. {}' to run builds from the command line. | ||
248 | The bitbake configuration files (local.conf, bblayers.conf and more) can be found in {}/conf | ||
249 | """.format( | ||
250 | bitbake_config["description"], | ||
251 | os.path.join(bitbake_builddir,'conf/conf-summary.txt'), | ||
252 | os.path.join(bitbake_builddir,'conf/conf-notes.txt'), | ||
253 | init_script, | ||
254 | bitbake_builddir | ||
255 | ) | ||
256 | readme_file = os.path.join(bitbake_builddir, "README") | ||
257 | with open(readme_file, 'w') as f: | ||
258 | f.write(readme) | ||
259 | print("Usage instructions and additional information are in\n {}\n".format(readme_file)) | ||
260 | print("The bitbake configuration files (local.conf, bblayers.conf and more) can be found in\n {}/conf\n".format(bitbake_builddir)) | ||
261 | print("To run builds, source the environment using\n . {}".format(init_script)) | ||
262 | |||
263 | def get_registry_config(registry_path, id): | ||
264 | for root, dirs, files in os.walk(registry_path): | ||
265 | for f in files: | ||
266 | if f.endswith('.conf.json') and id == get_config_name(f): | ||
267 | return os.path.join(root, f) | ||
268 | raise Exception("Unable to find {} in available configurations; use 'list' sub-command to see what is available".format(id)) | ||
269 | |||
270 | def update_build(config, confdir, builddir, layerdir, d): | ||
271 | layer_config = config["data"]["sources"] | ||
272 | layer_overrides = config["source-overrides"]["sources"] | ||
273 | for k,v in layer_overrides.items(): | ||
274 | if k in layer_config: | ||
275 | layer_config[k]["git-remote"] = v["git-remote"] | ||
276 | checkout_layers(layer_config, layerdir, d) | ||
277 | bitbake_config = config["bitbake-config"] | ||
278 | thisdir = os.path.dirname(config["path"]) if config["type"] == 'local' else None | ||
279 | setup_bitbake_build(bitbake_config, layerdir, builddir, thisdir) | ||
280 | |||
281 | def int_input(allowed_values): | ||
282 | n = None | ||
283 | while n is None: | ||
284 | try: | ||
285 | n = int(input()) | ||
286 | except ValueError: | ||
287 | print('Not a valid number, please try again:') | ||
288 | continue | ||
289 | if n not in allowed_values: | ||
290 | print('Number {} not one of {}, please try again:'.format(n, allowed_values)) | ||
291 | n = None | ||
292 | return n | ||
293 | |||
294 | def flatten_bitbake_configs(configs): | ||
295 | def merge_configs(c1,c2): | ||
296 | c_merged = {} | ||
297 | for k,v in c2.items(): | ||
298 | if k not in c1.keys(): | ||
299 | c_merged[k] = v | ||
300 | for k,v in c1.items(): | ||
301 | if k not in c2.keys(): | ||
302 | c_merged[k] = v | ||
303 | else: | ||
304 | c_merged[k] = c1[k] + c2[k] | ||
305 | del c_merged['configurations'] | ||
306 | return c_merged | ||
307 | |||
308 | flattened_configs = [] | ||
309 | for c in configs: | ||
310 | if 'configurations' not in c: | ||
311 | flattened_configs.append(c) | ||
312 | else: | ||
313 | for sub_c in flatten_bitbake_configs(c['configurations']): | ||
314 | flattened_configs.append(merge_configs(c, sub_c)) | ||
315 | return flattened_configs | ||
316 | |||
317 | def choose_bitbake_config(configs, parameters, non_interactive): | ||
318 | flattened_configs = flatten_bitbake_configs(configs) | ||
319 | configs_dict = {i["name"]:i for i in flattened_configs} | ||
320 | |||
321 | if parameters: | ||
322 | config_id = parameters[0] | ||
323 | if config_id not in configs_dict: | ||
324 | raise Exception("Bitbake configuration {} not found; replace with one of {}".format(config_id, configs_dict)) | ||
325 | return configs_dict[config_id] | ||
326 | |||
327 | enumerated_configs = list(enumerate(flattened_configs)) | ||
328 | if len(enumerated_configs) == 1: | ||
329 | only_config = flattened_configs[0] | ||
330 | print("\nSelecting the only available bitbake configuration {}".format(only_config["name"])) | ||
331 | return only_config | ||
332 | |||
333 | if non_interactive: | ||
334 | raise Exception("Unable to choose from bitbake configurations in non-interactive mode: {}".format(configs_dict)) | ||
335 | |||
336 | print("\nAvailable bitbake configurations:") | ||
337 | for n, config_data in enumerated_configs: | ||
338 | print("{}. {}\t{}".format(n, config_data["name"], config_data["description"])) | ||
339 | print("\nPlease select one of the above bitbake configurations by its number:") | ||
340 | config_n = int_input([i[0] for i in enumerated_configs]) | ||
341 | return flattened_configs[config_n] | ||
342 | |||
343 | def choose_config(configs, non_interactive): | ||
344 | not_expired_configs = [k for k in configs.keys() if not has_expired(configs[k].get("expires", None))] | ||
345 | config_list = list(enumerate(not_expired_configs)) | ||
346 | if len(config_list) == 1: | ||
347 | only_config = config_list[0][1] | ||
348 | print("\nSelecting the only available configuration {}\n".format(only_config)) | ||
349 | return only_config | ||
350 | |||
351 | if non_interactive: | ||
352 | raise Exception("Unable to choose from configurations in non-interactive mode: {}".format(not_expired_configs)) | ||
353 | |||
354 | print("\nAvailable configurations:") | ||
355 | for n, config_name in config_list: | ||
356 | config_data = configs[config_name] | ||
357 | expiry_date = config_data.get("expires", None) | ||
358 | config_desc = config_data["description"] | ||
359 | if expiry_date: | ||
360 | print("{}. {}\t{} (supported until {})".format(n, config_name, config_desc, expiry_date)) | ||
361 | else: | ||
362 | print("{}. {}\t{}".format(n, config_name, config_desc)) | ||
363 | print("\nPlease select one of the above configurations by its number:") | ||
364 | config_n = int_input([i[0] for i in config_list]) | ||
365 | return config_list[config_n][1] | ||
366 | |||
367 | def choose_fragments(possibilities, parameters, non_interactive, skip_selection): | ||
368 | choices = {} | ||
369 | for k,v in possibilities.items(): | ||
370 | if skip_selection and k in skip_selection: | ||
371 | print("Skipping a selection of {}, as requested on command line. The resulting bitbake configuration may require further manual adjustments.".format(k)) | ||
372 | continue | ||
373 | choice = [o for o in v["options"] if o in parameters] | ||
374 | if len(choice) > 1: | ||
375 | raise Exception("Options specified on command line do not allow a single selection from possibilities {}, please remove one or more from {}".format(v["options"], parameters)) | ||
376 | if len(choice) == 1: | ||
377 | choices[k] = choice[0] | ||
378 | continue | ||
379 | |||
380 | if non_interactive: | ||
381 | raise Exception("Unable to choose from options in non-interactive mode: {}".format(v["options"])) | ||
382 | |||
383 | print("\n" + v["description"] + ":") | ||
384 | options_enumerated = list(enumerate(v["options"])) | ||
385 | for n,o in options_enumerated: | ||
386 | print("{}. {}".format(n, o)) | ||
387 | print("\nPlease select one of the above options by its number:") | ||
388 | option_n = int_input([i[0] for i in options_enumerated]) | ||
389 | choices[k] = options_enumerated[option_n][1] | ||
390 | return choices | ||
391 | |||
392 | def obtain_config(top_dir, settings, args, source_overrides, d): | ||
393 | if args.config: | ||
394 | config_id = args.config[0] | ||
395 | config_parameters = args.config[1:] | ||
396 | if os.path.exists(config_id): | ||
397 | print("Reading configuration from local file\n {}".format(config_id)) | ||
398 | upstream_config = {'type':'local', | ||
399 | 'path':os.path.abspath(config_id), | ||
400 | 'name':get_config_name(config_id), | ||
401 | 'data':json.load(open(config_id)) | ||
402 | } | ||
403 | elif config_id.startswith("http://") or config_id.startswith("https://"): | ||
404 | print("Reading configuration from network URI\n {}".format(config_id)) | ||
405 | import urllib.request | ||
406 | with urllib.request.urlopen(config_id) as f: | ||
407 | upstream_config = {'type':'network','uri':config_id,'name':get_config_name(config_id),'data':json.load(f)} | ||
408 | else: | ||
409 | print("Looking up config {} in configuration registry".format(config_id)) | ||
410 | registry_path = update_registry(settings["default"]["registry"], cache_dir(top_dir), d) | ||
411 | registry_configs = list_registry(registry_path, with_expired=True) | ||
412 | if config_id not in registry_configs: | ||
413 | raise Exception("Config {} not found in configuration registry, re-run 'init' without parameters to choose from available configurations.".format(config_id)) | ||
414 | upstream_config = {'type':'registry','registry':settings["default"]["registry"],'name':config_id,'data':json.load(open(get_registry_config(registry_path,config_id)))} | ||
415 | expiry_date = upstream_config['data'].get("expires", None) | ||
416 | if has_expired(expiry_date): | ||
417 | print("This configuration is no longer supported after {}. Please consider changing to a supported configuration.".format(expiry_date)) | ||
418 | else: | ||
419 | registry_path = update_registry(settings["default"]["registry"], cache_dir(top_dir), d) | ||
420 | registry_configs = list_registry(registry_path, with_expired=True) | ||
421 | config_id = choose_config(registry_configs, args.non_interactive) | ||
422 | config_parameters = [] | ||
423 | upstream_config = {'type':'registry','registry':settings["default"]["registry"],'name':config_id,'data':json.load(open(get_registry_config(registry_path,config_id)))} | ||
424 | |||
425 | upstream_config['bitbake-config'] = choose_bitbake_config(upstream_config['data']['bitbake-setup']['configurations'], config_parameters, args.non_interactive) | ||
426 | upstream_config['bitbake-config']['oe-fragment-choices'] = choose_fragments(upstream_config['bitbake-config'].get('oe-fragments-one-of',{}), config_parameters[1:], args.non_interactive, args.skip_selection) | ||
427 | upstream_config['non-interactive-cmdline-options'] = [config_id, upstream_config['bitbake-config']['name']] + sorted(upstream_config['bitbake-config']['oe-fragment-choices'].values()) | ||
428 | upstream_config['source-overrides'] = source_overrides | ||
429 | upstream_config['skip-selection'] = args.skip_selection | ||
430 | return upstream_config | ||
431 | |||
432 | def init_config(top_dir, settings, args, d): | ||
433 | stdout = sys.stdout | ||
434 | def handle_task_progress(event, d): | ||
435 | rate = event.rate if event.rate else '' | ||
436 | progress = event.progress if event.progress > 0 else 0 | ||
437 | print("{}% {} ".format(progress, rate), file=stdout, end='\r') | ||
438 | |||
439 | create_siteconf(top_dir, args.non_interactive) | ||
440 | source_overrides = json.load(open(args.source_overrides)) if args.source_overrides else {'sources':{}} | ||
441 | upstream_config = obtain_config(top_dir, settings, args, source_overrides, d) | ||
442 | print("\nRun 'bitbake-setup init --non-interactive {}' to select this configuration non-interactively.\n".format(" ".join(upstream_config['non-interactive-cmdline-options']))) | ||
443 | |||
444 | builddir = os.path.join(os.path.abspath(top_dir), args.build_dir_name or "{}-{}".format(upstream_config['name']," ".join(upstream_config['non-interactive-cmdline-options'][1:]).replace(" ","-").replace("/","_"))) | ||
445 | if os.path.exists(os.path.join(builddir, "layers")): | ||
446 | print(f"Build already initialized in:\n {builddir}\nUse 'bitbake-setup status' to check if it needs to be updated, or 'bitbake-setup update' to perform the update.\nIf you would like to start over and re-initialize a build in this directory, remove it, and run 'bitbake-setup init' again.") | ||
447 | return | ||
448 | |||
449 | print("Initializing a build in\n {}".format(builddir)) | ||
450 | if not args.non_interactive: | ||
451 | y_or_n = input('Continue? (y/N): ') | ||
452 | if y_or_n != 'y': | ||
453 | exit() | ||
454 | print() | ||
455 | |||
456 | os.makedirs(builddir, exist_ok=True) | ||
457 | |||
458 | confdir = os.path.join(builddir, "config") | ||
459 | layerdir = os.path.join(builddir, "layers") | ||
460 | |||
461 | os.makedirs(confdir) | ||
462 | os.makedirs(layerdir) | ||
463 | |||
464 | bb.process.run("git -C {} init -b main".format(confdir)) | ||
465 | # Make sure commiting doesn't fail if no default git user is configured on the machine | ||
466 | bb.process.run("git -C {} config user.name bitbake-setup".format(confdir)) | ||
467 | bb.process.run("git -C {} config user.email bitbake-setup@not.set".format(confdir)) | ||
468 | bb.process.run("git -C {} commit --no-verify --allow-empty -m 'Initial commit'".format(confdir)) | ||
469 | |||
470 | bb.event.register("bb.build.TaskProgress", handle_task_progress, data=d) | ||
471 | |||
472 | write_config(upstream_config, confdir) | ||
473 | commit_config(confdir) | ||
474 | update_build(upstream_config, confdir, builddir, layerdir, d) | ||
475 | |||
476 | bb.event.remove("bb.build.TaskProgress", None) | ||
477 | |||
478 | def get_diff(file1, file2): | ||
479 | try: | ||
480 | bb.process.run('diff -uNr {} {}'.format(file1, file2)) | ||
481 | except bb.process.ExecutionError as e: | ||
482 | if e.exitcode == 1: | ||
483 | return e.stdout | ||
484 | else: | ||
485 | raise e | ||
486 | return None | ||
487 | |||
488 | def are_layers_changed(layers, layerdir, d): | ||
489 | changed = False | ||
490 | for r_name in layers: | ||
491 | r_data = layers[r_name] | ||
492 | repodir = r_data["path"] | ||
493 | |||
494 | r_remote = r_data['git-remote'] | ||
495 | rev = r_remote['rev'] | ||
496 | branch = r_remote.get('branch', None) | ||
497 | remotes = r_remote['remotes'] | ||
498 | |||
499 | for remote in remotes: | ||
500 | type,host,path,user,pswd,params = bb.fetch.decodeurl(remotes[remote]["uri"]) | ||
501 | fetchuri = bb.fetch.encodeurl(('git',host,path,user,pswd,params)) | ||
502 | if branch: | ||
503 | fetcher = bb.fetch.FetchData("{};protocol={};rev={};branch={};destsuffix={}".format(fetchuri,type,rev,branch,repodir), d) | ||
504 | else: | ||
505 | fetcher = bb.fetch.FetchData("{};protocol={};rev={};nobranch=1;destsuffix={}".format(fetchuri,type,rev,repodir), d) | ||
506 | upstream_revision = fetcher.method.latest_revision(fetcher, d, 'default') | ||
507 | rev_parse_result = bb.process.run('git -C {} rev-parse HEAD'.format(os.path.join(layerdir, repodir))) | ||
508 | local_revision = rev_parse_result[0].strip() | ||
509 | if upstream_revision != local_revision: | ||
510 | changed = True | ||
511 | print('Layer repository {} checked out into {} updated revision {} from {} to {}'.format(remotes[remote]["uri"], os.path.join(layerdir, repodir), rev, local_revision, upstream_revision)) | ||
512 | |||
513 | return changed | ||
514 | |||
515 | def build_status(top_dir, settings, args, d, update=False): | ||
516 | builddir = args.build_dir | ||
517 | |||
518 | confdir = os.path.join(builddir, "config") | ||
519 | layerdir = os.path.join(builddir, "layers") | ||
520 | |||
521 | current_upstream_config = json.load(open(os.path.join(confdir, "config-upstream.json"))) | ||
522 | |||
523 | args.config = current_upstream_config['non-interactive-cmdline-options'] | ||
524 | args.non_interactive = True | ||
525 | args.skip_selection = current_upstream_config['skip-selection'] | ||
526 | source_overrides = current_upstream_config["source-overrides"] | ||
527 | new_upstream_config = obtain_config(top_dir, settings, args, source_overrides, d) | ||
528 | |||
529 | write_config(new_upstream_config, confdir) | ||
530 | config_diff = bb.process.run('git -C {} diff'.format(confdir))[0] | ||
531 | |||
532 | if config_diff: | ||
533 | print('\nConfiguration in {} has changed:\n{}'.format(builddir, config_diff)) | ||
534 | if update: | ||
535 | commit_config(confdir) | ||
536 | update_build(new_upstream_config, confdir, builddir, layerdir, d) | ||
537 | else: | ||
538 | bb.process.run('git -C {} restore config-upstream.json'.format(confdir)) | ||
539 | return | ||
540 | |||
541 | if are_layers_changed(current_upstream_config["data"]["sources"], layerdir, d): | ||
542 | if update: | ||
543 | update_build(current_upstream_config, confdir, builddir, layerdir, d) | ||
544 | return | ||
545 | |||
546 | print("\nConfiguration in {} has not changed.".format(builddir)) | ||
547 | |||
548 | def build_update(top_dir, settings, args, d): | ||
549 | build_status(top_dir, settings, args, d, update=True) | ||
550 | |||
551 | def do_fetch(fetcher, dir): | ||
552 | # git fetcher simply dumps git output to stdout; in bitbake context that is redirected to temp/log.do_fetch | ||
553 | # and we need to set up smth similar here | ||
554 | fetchlogdir = os.path.join(dir, 'logs') | ||
555 | os.makedirs(fetchlogdir, exist_ok=True) | ||
556 | fetchlog = os.path.join(fetchlogdir, 'fetch_log.{}'.format(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))) | ||
557 | with open(fetchlog, 'a') as f: | ||
558 | oldstdout = sys.stdout | ||
559 | sys.stdout = f | ||
560 | fetcher.download() | ||
561 | fetcher.unpack(dir) | ||
562 | sys.stdout = oldstdout | ||
563 | |||
564 | def update_registry(registry, cachedir, d): | ||
565 | registrydir = 'configurations' | ||
566 | if registry.startswith("."): | ||
567 | full_registrydir = os.path.join(os.getcwd(), registry, registrydir) | ||
568 | elif registry.startswith("/"): | ||
569 | full_registrydir = os.path.join(registry, registrydir) | ||
570 | else: | ||
571 | full_registrydir = os.path.join(cachedir, registrydir) | ||
572 | print("Fetching configuration registry\n {}\ninto\n {}".format(registry, full_registrydir)) | ||
573 | fetcher = bb.fetch.Fetch(["{};destsuffix={}".format(registry, registrydir)], d) | ||
574 | do_fetch(fetcher, cachedir) | ||
575 | return full_registrydir | ||
576 | |||
577 | def has_expired(expiry_date): | ||
578 | if expiry_date: | ||
579 | return datetime.datetime.now() > datetime.datetime.fromisoformat(expiry_date) | ||
580 | return False | ||
581 | |||
582 | def list_registry(registry_path, with_expired): | ||
583 | json_data = {} | ||
584 | |||
585 | for root, dirs, files in os.walk(registry_path): | ||
586 | for f in files: | ||
587 | if f.endswith('.conf.json'): | ||
588 | config_name = get_config_name(f) | ||
589 | config_data = json.load(open(os.path.join(root, f))) | ||
590 | config_desc = config_data["description"] | ||
591 | expiry_date = config_data.get("expires", None) | ||
592 | if expiry_date: | ||
593 | if with_expired or not has_expired(expiry_date): | ||
594 | json_data[config_name] = {"description": config_desc, "expires": expiry_date} | ||
595 | else: | ||
596 | json_data[config_name] = {"description": config_desc} | ||
597 | return json_data | ||
598 | |||
599 | def list_configs(top_dir, settings, args, d): | ||
600 | registry_path = update_registry(settings["default"]["registry"], cache_dir(top_dir), d) | ||
601 | json_data = list_registry(registry_path, args.with_expired) | ||
602 | print("\nAvailable configurations:") | ||
603 | for config_name, config_data in json_data.items(): | ||
604 | expiry_date = config_data.get("expires", None) | ||
605 | config_desc = config_data["description"] | ||
606 | if expiry_date: | ||
607 | if args.with_expired or not has_expired(expiry_date): | ||
608 | print("{}\t{} (supported until {})".format(config_name, config_desc, expiry_date)) | ||
609 | else: | ||
610 | print("{}\t{}".format(config_name, config_desc)) | ||
611 | print("\nRun 'init' with one of the above configuration identifiers to set up a build.") | ||
612 | |||
613 | if args.write_json: | ||
614 | with open(args.write_json, 'w') as f: | ||
615 | json.dump(json_data, f, sort_keys=True, indent=4) | ||
616 | print("Available configurations written into {}".format(args.write_json)) | ||
617 | |||
618 | def install_buildtools(top_dir, settings, args, d): | ||
619 | buildtools_install_dir = os.path.join(args.build_dir, 'buildtools') | ||
620 | if os.path.exists(buildtools_install_dir): | ||
621 | if not args.force: | ||
622 | print("Buildtools are already installed in {}.".format(buildtools_install_dir)) | ||
623 | env_scripts = glob.glob(os.path.join(buildtools_install_dir, 'environment-setup-*')) | ||
624 | if env_scripts: | ||
625 | print("If you wish to use them, you need to source the environment setup script e.g.") | ||
626 | for s in env_scripts: | ||
627 | print("$ . {}".format(s)) | ||
628 | print("You can also re-run bitbake-setup install-buildtools with --force option to force a reinstallation.") | ||
629 | return | ||
630 | shutil.rmtree(buildtools_install_dir) | ||
631 | |||
632 | install_buildtools = os.path.join(args.build_dir, 'layers/oe-scripts/install-buildtools') | ||
633 | buildtools_download_dir = os.path.join(args.build_dir, 'buildtools-downloads/{}'.format(time.strftime("%Y%m%d%H%M%S"))) | ||
634 | print("Buildtools archive is downloaded into {} and its content installed into {}".format(buildtools_download_dir, buildtools_install_dir)) | ||
635 | subprocess.check_call("{} -d {} --downloads-directory {}".format(install_buildtools, buildtools_install_dir, buildtools_download_dir), shell=True) | ||
636 | |||
637 | def default_settings_path(top_dir): | ||
638 | return os.path.join(top_dir, 'settings.conf') | ||
639 | |||
640 | def create_siteconf(top_dir, non_interactive=True): | ||
641 | siteconfpath = os.path.join(top_dir, 'site.conf') | ||
642 | print('A common site.conf file will be created, please edit or replace before running builds\n {}\n'.format(siteconfpath)) | ||
643 | if not non_interactive: | ||
644 | y_or_n = input('Proceed? (y/N): ') | ||
645 | if y_or_n != 'y': | ||
646 | exit() | ||
647 | |||
648 | os.makedirs(os.path.dirname(top_dir), exist_ok=True) | ||
649 | if os.path.exists(siteconfpath): | ||
650 | backup_siteconf = siteconfpath + "-backup.{}".format(time.strftime("%Y%m%d%H%M%S")) | ||
651 | os.rename(siteconfpath, backup_siteconf) | ||
652 | print("Previous settings are in {}".format(backup_siteconf)) | ||
653 | with open(siteconfpath, 'w') as siteconffile: | ||
654 | siteconffile.write('# This file is intended for build host-specific bitbake settings\n') | ||
655 | |||
656 | def global_settings_path(args): | ||
657 | return os.path.abspath(args.global_settings) if args.global_settings else os.path.join(os.path.expanduser('~'), '.config', 'bitbake-setup', 'settings.conf') | ||
658 | |||
659 | def load_settings(settings_path): | ||
660 | settings = configparser.ConfigParser() | ||
661 | if os.path.exists(settings_path): | ||
662 | print('Loading settings from\n {}\n'.format(settings_path)) | ||
663 | settings.read_file(open(settings_path)) | ||
664 | return settings | ||
665 | |||
666 | def change_setting(top_dir, args): | ||
667 | if vars(args)['global']: | ||
668 | settings_path = global_settings_path(args) | ||
669 | else: | ||
670 | settings_path = default_settings_path(top_dir) | ||
671 | settings = load_settings(settings_path) | ||
672 | |||
673 | if args.subcommand == 'set': | ||
674 | if args.section not in settings.keys(): | ||
675 | settings[args.section] = {} | ||
676 | settings[args.section][args.setting] = args.value | ||
677 | print(f"From section '{args.section}' the setting '{args.setting}' was changed to '{args.value}'") | ||
678 | if args.subcommand == 'unset': | ||
679 | if args.section in settings.keys() and args.setting in settings[args.section].keys(): | ||
680 | del settings[args.section][args.setting] | ||
681 | print(f"From section '{args.section}' the setting '{args.setting}' has been removed") | ||
682 | |||
683 | os.makedirs(os.path.dirname(settings_path), exist_ok=True) | ||
684 | with open(settings_path, 'w') as settingsfile: | ||
685 | settings.write(settingsfile) | ||
686 | print(f"Settings written to {settings_path}") | ||
687 | |||
688 | def list_settings(all_settings): | ||
689 | for section, section_settings in all_settings.items(): | ||
690 | for key, value in section_settings.items(): | ||
691 | print("{} {} {}".format(section, key, value)) | ||
692 | |||
693 | def settings_func(top_dir, all_settings, args): | ||
694 | if args.subcommand == 'list': | ||
695 | list_settings(all_settings) | ||
696 | elif args.subcommand == 'set' or args.subcommand == 'unset': | ||
697 | change_setting(top_dir, args) | ||
698 | |||
699 | def get_build_dir_via_bbpath(): | ||
700 | bbpath = os.environ.get('BBPATH') | ||
701 | if bbpath: | ||
702 | bitbake_dir = os.path.normpath(bbpath.split(':')[0]) | ||
703 | if os.path.exists(os.path.join(bitbake_dir,'init-build-env')): | ||
704 | build_dir = os.path.dirname(bitbake_dir) | ||
705 | return build_dir | ||
706 | return None | ||
707 | |||
708 | def get_top_dir(args, settings): | ||
709 | build_dir_via_bbpath = get_build_dir_via_bbpath() | ||
710 | if build_dir_via_bbpath: | ||
711 | top_dir = os.path.dirname(build_dir_via_bbpath) | ||
712 | if os.path.exists(default_settings_path(top_dir)): | ||
713 | return top_dir | ||
714 | |||
715 | if hasattr(args, 'build_dir'): | ||
716 | top_dir = os.path.dirname(os.path.normpath(args.build_dir)) | ||
717 | return top_dir | ||
718 | |||
719 | top_dir_prefix = settings['default']['top-dir-prefix'] | ||
720 | top_dir_name = settings['default']['top-dir-name'] | ||
721 | return os.path.join(top_dir_prefix, top_dir_name) | ||
722 | |||
723 | def merge_settings(builtin_settings, global_settings, local_settings, cmdline_settings): | ||
724 | all_settings = builtin_settings | ||
725 | |||
726 | for s in (global_settings, local_settings): | ||
727 | for section, section_settings in s.items(): | ||
728 | for setting, value in section_settings.items(): | ||
729 | all_settings[section][setting] = value | ||
730 | |||
731 | for (section, setting, value) in cmdline_settings: | ||
732 | all_settings[section][setting] = value | ||
733 | |||
734 | return all_settings | ||
735 | |||
736 | def main(): | ||
737 | def add_build_dir_arg(parser): | ||
738 | build_dir = get_build_dir_via_bbpath() | ||
739 | if build_dir: | ||
740 | parser.add_argument('--build-dir', default=build_dir, help="Path to the build, default is %(default)s via BBPATH") | ||
741 | else: | ||
742 | parser.add_argument('--build-dir', required=True, help="Path to the build") | ||
743 | |||
744 | parser = argparse.ArgumentParser( | ||
745 | description="BitBake setup utility. Run with 'init' argument to get started.", | ||
746 | epilog="Use %(prog)s <subcommand> --help to get help on a specific command" | ||
747 | ) | ||
748 | parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true') | ||
749 | parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true') | ||
750 | parser.add_argument('--color', choices=['auto', 'always', 'never'], default='auto', help='Colorize output (where %(metavar)s is %(choices)s)', metavar='COLOR') | ||
751 | parser.add_argument('--no-network', action='store_true', help='Do not check whether configuration repositories and layer repositories have been updated; use only the local cache.') | ||
752 | parser.add_argument('--global-settings', action='store', metavar='PATH', help='Path to the global settings file.') | ||
753 | parser.add_argument('--setting', default=[], action='append', dest='cmdline_settings', | ||
754 | nargs=3, metavar=('SECTION', 'SETTING', 'VALUE'), | ||
755 | help='Modify a setting (for this bitbake-setup invocation only), for example "--setting default top-dir-prefix /path/to/top/dir".') | ||
756 | |||
757 | subparsers = parser.add_subparsers() | ||
758 | |||
759 | parser_list = subparsers.add_parser('list', help='List available configurations') | ||
760 | parser_list.add_argument('--with-expired', action='store_true', help='List also configurations that are no longer supported due to reaching their end-of-life dates.') | ||
761 | parser_list.add_argument('--write-json', action='store', help='Write available configurations into a json file so they can be programmatically processed.') | ||
762 | parser_list.set_defaults(func=list_configs) | ||
763 | |||
764 | parser_init = subparsers.add_parser('init', help='Select a configuration and initialize a build from it') | ||
765 | parser_init.add_argument('config', nargs='*', help="path/URL/id to a configuration file (use 'list' command to get available ids), followed by configuration options. Bitbake-setup will ask to choose from available choices if command line doesn't completely specify them.") | ||
766 | parser_init.add_argument('--non-interactive', action='store_true', help='Do not ask to interactively choose from available options; if bitbake-setup cannot make a decision it will stop with a failure.') | ||
767 | parser_init.add_argument('--source-overrides', action='store', help='Override sources information (repositories/revisions) with values from a local json file.') | ||
768 | parser_init.add_argument('--build-dir-name', action='store', help='A custom build directory name under the top directory.') | ||
769 | parser_init.add_argument('--skip-selection', action='append', help='Do not select and set an option/fragment from available choices; the resulting bitbake configuration may be incomplete.') | ||
770 | parser_init.set_defaults(func=init_config) | ||
771 | |||
772 | parser_status = subparsers.add_parser('status', help='Check if the build needs to be synchronized with configuration') | ||
773 | add_build_dir_arg(parser_status) | ||
774 | parser_status.set_defaults(func=build_status) | ||
775 | |||
776 | parser_update = subparsers.add_parser('update', help='Update a build to be in sync with configuration') | ||
777 | add_build_dir_arg(parser_update) | ||
778 | parser_update.set_defaults(func=build_update) | ||
779 | |||
780 | parser_install_buildtools = subparsers.add_parser('install-buildtools', help='Install buildtools which can help fulfil missing or incorrect dependencies on the host machine') | ||
781 | add_build_dir_arg(parser_install_buildtools) | ||
782 | parser_install_buildtools.add_argument('--force', action='store_true', help='Force a reinstall of buildtools over the previous installation.') | ||
783 | parser_install_buildtools.set_defaults(func=install_buildtools) | ||
784 | |||
785 | parser_settings_arg_global = argparse.ArgumentParser(add_help=False) | ||
786 | parser_settings_arg_global.add_argument('--global', action='store_true', help="Modify the setting in a global settings file, rather than one specific to a top directory") | ||
787 | |||
788 | parser_settings = subparsers.add_parser('settings', parents=[parser_settings_arg_global], | ||
789 | help='List current settings, or set or unset a setting in a settings file (e.g. the default prefix and name of the top directory, the location of build configuration registry, downloads directory and other settings specific to a top directory)') | ||
790 | parser_settings.set_defaults(func=settings_func) | ||
791 | |||
792 | subparser_settings = parser_settings.add_subparsers(dest="subcommand", required=True, help="The action to perform on the settings file") | ||
793 | |||
794 | parser_settings_list = subparser_settings.add_parser('list', | ||
795 | help="List all settings with their values") | ||
796 | |||
797 | parser_settings_set = subparser_settings.add_parser('set', parents=[parser_settings_arg_global], | ||
798 | help="In a Section, set a setting to a certain value") | ||
799 | parser_settings_set.add_argument("section", metavar="<section>", help="Section in a settings file, typically 'default'") | ||
800 | parser_settings_set.add_argument("setting", metavar="<setting>", help="Name of a setting") | ||
801 | parser_settings_set.add_argument("value", metavar="<value>", help="The setting value") | ||
802 | |||
803 | parser_settings_unset = subparser_settings.add_parser('unset', parents=[parser_settings_arg_global], | ||
804 | help="Unset a setting, e.g. 'bitbake-setup settings unset default registry' would revert to the registry setting in a global settings file") | ||
805 | parser_settings_unset.add_argument("section", metavar="<section>", help="Section in a settings file, typically 'default'") | ||
806 | parser_settings_unset.add_argument("setting", metavar="<setting>", help="The setting to remove") | ||
807 | |||
808 | args = parser.parse_args() | ||
809 | |||
810 | logging.basicConfig(stream=sys.stdout) | ||
811 | if args.debug: | ||
812 | logger.setLevel(logging.DEBUG) | ||
813 | elif args.quiet: | ||
814 | logger.setLevel(logging.ERROR) | ||
815 | |||
816 | # Need to re-run logger_create with color argument | ||
817 | # (will be the same logger since it has the same name) | ||
818 | bb.msg.logger_create('bitbake-setup', output=sys.stdout, | ||
819 | color=args.color, | ||
820 | level=logger.getEffectiveLevel()) | ||
821 | |||
822 | if 'func' in args: | ||
823 | if hasattr(args, 'build_dir'): | ||
824 | if not os.path.exists(os.path.join(args.build_dir,'build', 'init-build-env')): | ||
825 | print("Not a valid build directory: build/init-build-env does not exist in {}".format(args.build_dir)) | ||
826 | return | ||
827 | |||
828 | if not hasattr(args, 'non_interactive'): | ||
829 | args.non_interactive = True | ||
830 | |||
831 | builtin_settings = {} | ||
832 | builtin_settings['default'] = { | ||
833 | 'top-dir-prefix':os.path.expanduser('~'), | ||
834 | 'top-dir-name':'bitbake-builds', | ||
835 | 'registry':default_registry, | ||
836 | } | ||
837 | |||
838 | global_settings = load_settings(global_settings_path(args)) | ||
839 | top_dir = get_top_dir(args, merge_settings(builtin_settings, global_settings, {}, args.cmdline_settings)) | ||
840 | |||
841 | # This cannot be set with the rest of the builtin settings as top_dir needs to be determined first | ||
842 | builtin_settings['default']['dl-dir'] = os.path.join(top_dir, '.bitbake-setup-downloads') | ||
843 | |||
844 | topdir_settings = load_settings(default_settings_path(top_dir)) | ||
845 | all_settings = merge_settings(builtin_settings, global_settings, topdir_settings, args.cmdline_settings) | ||
846 | |||
847 | if args.func == settings_func: | ||
848 | settings_func(top_dir, all_settings, args) | ||
849 | return | ||
850 | |||
851 | print('Bitbake-setup is using {} as top directory ("bitbake-setup settings --help" shows how to change it).\n'.format(top_dir, global_settings_path(args))) | ||
852 | |||
853 | d = init_bb_cache(top_dir, all_settings, args) | ||
854 | args.func(top_dir, all_settings, args, d) | ||
855 | save_bb_cache() | ||
856 | else: | ||
857 | from argparse import Namespace | ||
858 | parser.print_help() | ||
859 | |||
860 | main() | ||