1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: GPL-2.0-only
5#
6
7import logging
8import os
9import stat
10import sys
11import shutil
12import json
13
14import bb.utils
15import bb.process
16
17from bblayers.common import LayerPlugin
18
19logger = logging.getLogger('bitbake-layers')
20
21sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
22
23import oe.buildcfg
24
25def plugin_init(plugins):
26    return BuildConfPlugin()
27
28class BuildConfPlugin(LayerPlugin):
29    notes_fixme = """FIXME: Please place here the description of this build configuration.
30It 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 {}
54Please review the files in there, and particularly provide a configuration description in {}
55You can try out the configuration with
56TEMPLATECONF={} . {}/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:
67            if l[0] == os.path.abspath(args.layerpath):
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.')
86