xref: /openbmc/openbmc/poky/scripts/oe-setup-build (revision 96e4b4e121e0e2da1535d7d537d6a982a6ff5bc0)
1#!/usr/bin/env python3
2#
3# Copyright OpenEmbedded Contributors
4#
5# SPDX-License-Identifier: MIT
6#
7
8import argparse
9import json
10import os
11import subprocess
12
13def defaultlayers():
14    return os.path.abspath(os.path.join(os.path.dirname(__file__), '.oe-layers.json'))
15
16def makebuildpath(topdir, template):
17    return os.path.join(topdir, "build-{}".format(template))
18
19def discover_templates(layers_file):
20    if not os.path.exists(layers_file):
21        raise Exception("List of layers {} does not exist; were the layers set up using the setup-layers script or bitbake-setup tool?".format(layers_file))
22
23    templates = []
24    layers_list = json.load(open(layers_file))["layers"]
25    for layer in layers_list:
26        template_dir = os.path.join(os.path.dirname(layers_file), layer, 'conf','templates')
27        if os.path.exists(template_dir):
28            for d in sorted(os.listdir(template_dir)):
29                templatepath = os.path.join(template_dir,d)
30                if not os.path.isfile(os.path.join(templatepath,'local.conf.sample')):
31                    continue
32                layer_base = os.path.basename(layer)
33                templatename = "{}-{}".format(layer_base[5:] if layer_base.startswith("meta-") else layer_base, d)
34                buildpath = makebuildpath(os.getcwd(), templatename)
35                notespath = os.path.join(template_dir, d, 'conf-notes.txt')
36                try: notes = open(notespath).read()
37                except: notes = None
38                try: summary = open(os.path.join(template_dir, d, 'conf-summary.txt')).read()
39                except: summary = None
40                templates.append({"templatename":templatename,"templatepath":templatepath,"buildpath":buildpath,"notespath":notespath,"notes":notes,"summary":summary})
41
42    return templates
43
44def print_templates(templates, verbose):
45    print("Available build configurations:\n")
46
47    for i in range(len(templates)):
48        t = templates[i]
49        print("{}. {}".format(i+1, t["templatename"]))
50        print("{}".format(t["summary"].strip() if t["summary"] else "This configuration does not have a summary."))
51        if verbose:
52            print("Configuration template path:", t["templatepath"])
53            print("Build path:", t["buildpath"])
54            print("Usage notes:", t["notespath"] if t["notes"] else "This configuration does not have usage notes.")
55        print("")
56    if not verbose:
57        print("Re-run with 'list -v' to see additional information.")
58
59def list_templates(args):
60    templates = discover_templates(args.layerlist)
61    if not templates:
62        return
63
64    verbose = args.v
65    print_templates(templates, verbose)
66
67def find_template(template_name, templates):
68    print_templates(templates, False)
69    if not template_name:
70        n_s = input("Please choose a configuration by its number: ")
71        try: return templates[int(n_s) - 1]
72        except:
73            print("Invalid selection, please try again.")
74            return None
75    else:
76        for t in templates:
77            if t["templatename"] == template_name:
78                return t
79        raise Exception("Configuration {} is not one of {}, please try again.".format(template_name, [t["templatename"] for t in templates]))
80
81def setup_build_env(args):
82    templates = discover_templates(args.layerlist)
83    if not templates:
84        return
85
86    template = find_template(args.c, templates)
87    if not template:
88        return
89    builddir = args.b if args.b else template["buildpath"]
90    no_shell = args.no_shell
91    coredir = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))
92    cmd_base = ". {} {}".format(os.path.join(coredir, 'oe-init-build-env'), os.path.abspath(builddir))
93
94    initbuild = os.path.join(builddir, 'init-build-env')
95    if not os.path.exists(initbuild):
96        os.makedirs(builddir, exist_ok=True)
97        with open(initbuild, 'w') as f:
98            f.write(cmd_base)
99    print("\nRun '. {}' to initialize the build in a current shell session.\n".format(initbuild))
100
101    cmd = "TEMPLATECONF={} {}".format(template["templatepath"], cmd_base)
102    if not no_shell:
103        cmd = cmd + " && {}".format(os.environ.get('SHELL','bash'))
104    print("Running:", cmd)
105    subprocess.run(cmd, shell=True, executable=os.environ.get('SHELL','bash'))
106
107parser = argparse.ArgumentParser(description="A script that discovers available build configurations and sets up a build environment based on one of them. Run without arguments to choose one interactively.")
108parser.add_argument("--layerlist", default=defaultlayers(), help='Where to look for available layers (as written out by setup-layers script) (default is {}).'.format(defaultlayers()))
109
110subparsers = parser.add_subparsers()
111parser_list_templates = subparsers.add_parser('list', help='List available configurations')
112parser_list_templates.add_argument('-v', action='store_true',
113        help='Print detailed information and usage notes for each available build configuration.')
114parser_list_templates.set_defaults(func=list_templates)
115
116parser_setup_env = subparsers.add_parser('setup', help='Set up a build environment and open a shell session with it, ready to run builds.')
117parser_setup_env.add_argument('-c', metavar='configuration_name', help="Use a build configuration configuration_name to set up a build environment (run this script with 'list' to see what is available)")
118parser_setup_env.add_argument('-b', metavar='build_path', help="Set up a build directory in build_path (run this script with 'list -v' to see where it would be by default)")
119parser_setup_env.add_argument('--no-shell', action='store_true',
120        help='Create a build directory but do not start a shell session with the build environment from it.')
121parser_setup_env.set_defaults(func=setup_build_env)
122
123args = parser.parse_args()
124
125if 'func' in args:
126    args.func(args)
127else:
128    from argparse import Namespace
129    setup_build_env(Namespace(layerlist=args.layerlist, c=None, b=None, no_shell=False))
130