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