bitbake-setup 41 KB

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