Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
| 4 | # SPDX-License-Identifier: GPL-2.0-only |
| 5 | # |
| 6 | |
| 7 | import logging |
| 8 | import os |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 9 | import sys |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 10 | |
| 11 | from bblayers.common import LayerPlugin |
| 12 | |
| 13 | logger = logging.getLogger('bitbake-layers') |
| 14 | |
| 15 | sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) |
| 16 | |
| 17 | import oe.buildcfg |
| 18 | |
| 19 | def plugin_init(plugins): |
| 20 | return BuildConfPlugin() |
| 21 | |
| 22 | class BuildConfPlugin(LayerPlugin): |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 23 | notes_fixme = """FIXME: Please place here the detailed instructions for using this build configuration. |
| 24 | They will be shown to the users when they set up their builds via TEMPLATECONF. |
| 25 | """ |
| 26 | summary_fixme = """FIXME: Please place here the short summary of what this build configuration is for. |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 27 | It will be shown to the users when they set up their builds via TEMPLATECONF. |
| 28 | """ |
| 29 | |
| 30 | def _save_conf(self, templatename, templatepath, oecorepath, relpaths_to_oecore): |
| 31 | confdir = os.path.join(os.environ["BBPATH"], "conf") |
| 32 | destdir = os.path.join(templatepath, "conf", "templates", templatename) |
| 33 | os.makedirs(destdir, exist_ok=True) |
| 34 | |
| 35 | with open(os.path.join(confdir, "local.conf")) as src: |
| 36 | with open(os.path.join(destdir, "local.conf.sample"), 'w') as dest: |
| 37 | dest.write(src.read()) |
| 38 | |
| 39 | with open(os.path.join(confdir, "bblayers.conf")) as src: |
| 40 | with open(os.path.join(destdir, "bblayers.conf.sample"), 'w') as dest: |
| 41 | bblayers_data = src.read() |
| 42 | |
| 43 | for (abspath, relpath) in relpaths_to_oecore: |
| 44 | bblayers_data = bblayers_data.replace(abspath, "##OEROOT##/" + relpath) |
| 45 | dest.write(bblayers_data) |
| 46 | |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 47 | with open(os.path.join(destdir, "conf-summary.txt"), 'w') as dest: |
| 48 | dest.write(self.summary_fixme) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 49 | with open(os.path.join(destdir, "conf-notes.txt"), 'w') as dest: |
| 50 | dest.write(self.notes_fixme) |
| 51 | |
| 52 | logger.info("""Configuration template placed into {} |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 53 | Please review the files in there, and particularly provide a configuration summary in {} |
| 54 | and notes in {} |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 55 | You can try out the configuration with |
| 56 | TEMPLATECONF={} . {}/oe-init-build-env build-try-{}""" |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 57 | .format(destdir, os.path.join(destdir, "conf-summary.txt"), os.path.join(destdir, "conf-notes.txt"), destdir, oecorepath, templatename)) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 58 | |
| 59 | def do_save_build_conf(self, args): |
| 60 | """ Save the currently active build configuration (conf/local.conf, conf/bblayers.conf) as a template into a layer.\n This template can later be used for setting up builds via TEMPLATECONF. """ |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 61 | layers = oe.buildcfg.get_layer_revisions(self.tinfoil.config_data) |
| 62 | targetlayer = None |
| 63 | oecore = None |
| 64 | |
| 65 | for l in layers: |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 66 | if os.path.abspath(l[0]) == os.path.abspath(args.layerpath): |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 67 | targetlayer = l[0] |
| 68 | if l[1] == 'meta': |
| 69 | oecore = os.path.dirname(l[0]) |
| 70 | |
| 71 | if not targetlayer: |
| 72 | logger.error("Layer {} not in one of the currently enabled layers:\n{}".format(args.layerpath, "\n".join([l[0] for l in layers]))) |
| 73 | elif not oecore: |
| 74 | logger.error("Openembedded-core not in one of the currently enabled layers:\n{}".format("\n".join([l[0] for l in layers]))) |
| 75 | else: |
| 76 | relpaths_to_oecore = [(l[0], os.path.relpath(l[0], start=oecore)) for l in layers] |
| 77 | self._save_conf(args.templatename, targetlayer, oecore, relpaths_to_oecore) |
| 78 | |
| 79 | def register_commands(self, sp): |
| 80 | parser_build_conf = self.add_command(sp, 'save-build-conf', self.do_save_build_conf, parserecipes=False) |
| 81 | parser_build_conf.add_argument('layerpath', |
| 82 | help='The path to the layer where the configuration template should be saved.') |
| 83 | parser_build_conf.add_argument('templatename', |
| 84 | help='The name of the configuration template.') |