oe-setup-build 5.8 KB

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