blob: 5364f2b48129fe1b874b30ac2b669af103d8f26d [file] [log] [blame]
Patrick Williams73bd93f2024-02-20 08:07:48 -06001#!/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 print("List of layers {} does not exist; were the layers set up using the setup-layers script?".format(layers_file))
22 return None
23
24 templates = []
25 layers_list = json.load(open(layers_file))["layers"]
26 for layer in layers_list:
27 template_dir = os.path.join(os.path.dirname(layers_file), layer, 'conf','templates')
28 if os.path.exists(template_dir):
29 for d in sorted(os.listdir(template_dir)):
30 templatepath = os.path.join(template_dir,d)
31 if not os.path.isfile(os.path.join(templatepath,'local.conf.sample')):
32 continue
33 layer_base = os.path.basename(layer)
34 templatename = "{}-{}".format(layer_base[5:] if layer_base.startswith("meta-") else layer_base, d)
35 buildpath = makebuildpath(os.getcwd(), templatename)
36 notespath = os.path.join(template_dir, d, 'conf-notes.txt')
37 try: notes = open(notespath).read()
38 except: notes = None
39 try: summary = open(os.path.join(template_dir, d, 'conf-summary.txt')).read()
40 except: summary = None
41 templates.append({"templatename":templatename,"templatepath":templatepath,"buildpath":buildpath,"notespath":notespath,"notes":notes,"summary":summary})
42
43 return templates
44
45def print_templates(templates, verbose):
46 print("Available build configurations:\n")
47
48 for i in range(len(templates)):
49 t = templates[i]
50 print("{}. {}".format(i+1, t["templatename"]))
51 print("{}".format(t["summary"].strip() if t["summary"] else "This configuration does not have a summary."))
52 if verbose:
53 print("Configuration template path:", t["templatepath"])
54 print("Build path:", t["buildpath"])
55 print("Usage notes:", t["notespath"] if t["notes"] else "This configuration does not have usage notes.")
56 print("")
57 if not verbose:
58 print("Re-run with 'list -v' to see additional information.")
59
60def list_templates(args):
61 templates = discover_templates(args.layerlist)
62 if not templates:
63 return
64
65 verbose = args.v
66 print_templates(templates, verbose)
67
68def find_template(template_name, templates):
69 print_templates(templates, False)
70 if not template_name:
71 n_s = input("Please choose a configuration by its number: ")
72 try: return templates[int(n_s) - 1]
73 except:
74 print("Invalid selection, please try again.")
75 return None
76 else:
77 for t in templates:
78 if t["templatename"] == template_name:
79 return t
80 print("Configuration {} is not one of {}, please try again.".format(tempalte_name, [t["templatename"] for t in templates]))
81 return None
82
83def setup_build_env(args):
84 templates = discover_templates(args.layerlist)
85 if not templates:
86 return
87
88 template = find_template(args.c, templates)
89 if not template:
90 return
91 builddir = args.b if args.b else template["buildpath"]
92 no_shell = args.no_shell
93 coredir = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))
Patrick Williams2f814a62024-04-16 16:28:03 -050094 cmd = "TEMPLATECONF={} . {} {}".format(template["templatepath"], os.path.join(coredir, 'oe-init-build-env'), builddir)
Patrick Williams73bd93f2024-02-20 08:07:48 -060095 if not no_shell:
96 cmd = cmd + " && {}".format(os.environ['SHELL'])
97 print("Running:", cmd)
98 subprocess.run(cmd, shell=True, executable=os.environ['SHELL'])
99
100parser = 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.")
101parser.add_argument("--layerlist", default=defaultlayers(), help='Where to look for available layers (as written out by setup-layers script) (default is {}).'.format(defaultlayers()))
102
103subparsers = parser.add_subparsers()
104parser_list_templates = subparsers.add_parser('list', help='List available configurations')
105parser_list_templates.add_argument('-v', action='store_true',
106 help='Print detailed information and usage notes for each available build configuration.')
107parser_list_templates.set_defaults(func=list_templates)
108
109parser_setup_env = subparsers.add_parser('setup', help='Set up a build environment and open a shell session with it, ready to run builds.')
110parser_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)")
111parser_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)")
112parser_setup_env.add_argument('--no-shell', action='store_true',
113 help='Create a build directory but do not start a shell session with the build environment from it.')
114parser_setup_env.set_defaults(func=setup_build_env)
115
116args = parser.parse_args()
117
118if 'func' in args:
119 args.func(args)
120else:
121 from argparse import Namespace
122 setup_build_env(Namespace(layerlist=args.layerlist, c=None, b=None, no_shell=False))