blob: 87a5e5a55bc3583543e5c66432871757d8161767 [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):
23 notes_fixme = """FIXME: Please place here the description of this build configuration.
24It will be shown to the users when they set up their builds via TEMPLATECONF.
25"""
26
27 def _save_conf(self, templatename, templatepath, oecorepath, relpaths_to_oecore):
28 confdir = os.path.join(os.environ["BBPATH"], "conf")
29 destdir = os.path.join(templatepath, "conf", "templates", templatename)
30 os.makedirs(destdir, exist_ok=True)
31
32 with open(os.path.join(confdir, "local.conf")) as src:
33 with open(os.path.join(destdir, "local.conf.sample"), 'w') as dest:
34 dest.write(src.read())
35
36 with open(os.path.join(confdir, "bblayers.conf")) as src:
37 with open(os.path.join(destdir, "bblayers.conf.sample"), 'w') as dest:
38 bblayers_data = src.read()
39
40 for (abspath, relpath) in relpaths_to_oecore:
41 bblayers_data = bblayers_data.replace(abspath, "##OEROOT##/" + relpath)
42 dest.write(bblayers_data)
43
44 with open(os.path.join(destdir, "conf-notes.txt"), 'w') as dest:
45 dest.write(self.notes_fixme)
46
47 logger.info("""Configuration template placed into {}
48Please review the files in there, and particularly provide a configuration description in {}
49You can try out the configuration with
50TEMPLATECONF={} . {}/oe-init-build-env build-try-{}"""
51.format(destdir, os.path.join(destdir, "conf-notes.txt"), destdir, oecorepath, templatename))
52
53 def do_save_build_conf(self, args):
54 """ 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 -050055 layers = oe.buildcfg.get_layer_revisions(self.tinfoil.config_data)
56 targetlayer = None
57 oecore = None
58
59 for l in layers:
Patrick Williams2390b1b2022-11-03 13:47:49 -050060 if os.path.abspath(l[0]) == os.path.abspath(args.layerpath):
Patrick Williams92b42cb2022-09-03 06:53:57 -050061 targetlayer = l[0]
62 if l[1] == 'meta':
63 oecore = os.path.dirname(l[0])
64
65 if not targetlayer:
66 logger.error("Layer {} not in one of the currently enabled layers:\n{}".format(args.layerpath, "\n".join([l[0] for l in layers])))
67 elif not oecore:
68 logger.error("Openembedded-core not in one of the currently enabled layers:\n{}".format("\n".join([l[0] for l in layers])))
69 else:
70 relpaths_to_oecore = [(l[0], os.path.relpath(l[0], start=oecore)) for l in layers]
71 self._save_conf(args.templatename, targetlayer, oecore, relpaths_to_oecore)
72
73 def register_commands(self, sp):
74 parser_build_conf = self.add_command(sp, 'save-build-conf', self.do_save_build_conf, parserecipes=False)
75 parser_build_conf.add_argument('layerpath',
76 help='The path to the layer where the configuration template should be saved.')
77 parser_build_conf.add_argument('templatename',
78 help='The name of the configuration template.')