blob: 722cf0723c1fdb0bc8186f508548e94ff9ce8ced [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: GPL-2.0-only
5#
6
7import logging
8import os
Patrick Williams92b42cb2022-09-03 06:53:57 -05009import sys
Patrick Williams92b42cb2022-09-03 06:53:57 -050010
11from bblayers.common import LayerPlugin
12
13logger = logging.getLogger('bitbake-layers')
14
15sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
16
17import oe.buildcfg
18
19def plugin_init(plugins):
20 return BuildConfPlugin()
21
22class BuildConfPlugin(LayerPlugin):
Patrick Williams73bd93f2024-02-20 08:07:48 -060023 notes_fixme = """FIXME: Please place here the detailed instructions for using this build configuration.
24They 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 Williams92b42cb2022-09-03 06:53:57 -050027It 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 Williams73bd93f2024-02-20 08:07:48 -060047 with open(os.path.join(destdir, "conf-summary.txt"), 'w') as dest:
48 dest.write(self.summary_fixme)
Patrick Williams92b42cb2022-09-03 06:53:57 -050049 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 Williams73bd93f2024-02-20 08:07:48 -060053Please review the files in there, and particularly provide a configuration summary in {}
54and notes in {}
Patrick Williams92b42cb2022-09-03 06:53:57 -050055You can try out the configuration with
56TEMPLATECONF={} . {}/oe-init-build-env build-try-{}"""
Patrick Williams73bd93f2024-02-20 08:07:48 -060057.format(destdir, os.path.join(destdir, "conf-summary.txt"), os.path.join(destdir, "conf-notes.txt"), destdir, oecorepath, templatename))
Patrick Williams92b42cb2022-09-03 06:53:57 -050058
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 Williams92b42cb2022-09-03 06:53:57 -050061 layers = oe.buildcfg.get_layer_revisions(self.tinfoil.config_data)
62 targetlayer = None
63 oecore = None
64
65 for l in layers:
Patrick Williams2390b1b2022-11-03 13:47:49 -050066 if os.path.abspath(l[0]) == os.path.abspath(args.layerpath):
Patrick Williams92b42cb2022-09-03 06:53:57 -050067 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.')