oe-setup-build 5.8 KB

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