diff options
Diffstat (limited to 'scripts/oe-setup-build')
-rwxr-xr-x | scripts/oe-setup-build | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/scripts/oe-setup-build b/scripts/oe-setup-build new file mode 100755 index 0000000000..c0476992a2 --- /dev/null +++ b/scripts/oe-setup-build | |||
@@ -0,0 +1,131 @@ | |||
1 | #!/usr/bin/env python3 | ||
2 | # | ||
3 | # Copyright OpenEmbedded Contributors | ||
4 | # | ||
5 | # SPDX-License-Identifier: MIT | ||
6 | # | ||
7 | |||
8 | import argparse | ||
9 | import json | ||
10 | import os | ||
11 | import subprocess | ||
12 | |||
13 | def defaultlayers(): | ||
14 | return os.path.abspath(os.path.join(os.path.dirname(__file__), '.oe-layers.json')) | ||
15 | |||
16 | def makebuildpath(topdir, template): | ||
17 | return os.path.join(topdir, "build-{}".format(template)) | ||
18 | |||
19 | def 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 | |||
45 | def 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 | |||
60 | def 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 | |||
68 | def 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 | |||
83 | def 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__)), '..')) | ||
94 | cmd_base = ". {} {}".format(os.path.join(coredir, 'oe-init-build-env'), os.path.abspath(builddir)) | ||
95 | |||
96 | initbuild = os.path.join(builddir, 'init-build-env') | ||
97 | if not os.path.exists(initbuild): | ||
98 | os.makedirs(builddir, exist_ok=True) | ||
99 | with open(initbuild, 'w') as f: | ||
100 | f.write(cmd_base) | ||
101 | print("\nRun '. {}' to initialize the build in a current shell session.\n".format(initbuild)) | ||
102 | |||
103 | cmd = "TEMPLATECONF={} {}".format(template["templatepath"], cmd_base) | ||
104 | if not no_shell: | ||
105 | cmd = cmd + " && {}".format(os.environ['SHELL']) | ||
106 | print("Running:", cmd) | ||
107 | subprocess.run(cmd, shell=True, executable=os.environ['SHELL']) | ||
108 | |||
109 | parser = 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.") | ||
110 | parser.add_argument("--layerlist", default=defaultlayers(), help='Where to look for available layers (as written out by setup-layers script) (default is {}).'.format(defaultlayers())) | ||
111 | |||
112 | subparsers = parser.add_subparsers() | ||
113 | parser_list_templates = subparsers.add_parser('list', help='List available configurations') | ||
114 | parser_list_templates.add_argument('-v', action='store_true', | ||
115 | help='Print detailed information and usage notes for each available build configuration.') | ||
116 | parser_list_templates.set_defaults(func=list_templates) | ||
117 | |||
118 | parser_setup_env = subparsers.add_parser('setup', help='Set up a build environment and open a shell session with it, ready to run builds.') | ||
119 | parser_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)") | ||
120 | parser_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)") | ||
121 | parser_setup_env.add_argument('--no-shell', action='store_true', | ||
122 | help='Create a build directory but do not start a shell session with the build environment from it.') | ||
123 | parser_setup_env.set_defaults(func=setup_build_env) | ||
124 | |||
125 | args = parser.parse_args() | ||
126 | |||
127 | if 'func' in args: | ||
128 | args.func(args) | ||
129 | else: | ||
130 | from argparse import Namespace | ||
131 | setup_build_env(Namespace(layerlist=args.layerlist, c=None, b=None, no_shell=False)) | ||