idf_ext.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import copy
  2. import glob
  3. import os
  4. import os.path
  5. import re
  6. import shutil
  7. import tempfile
  8. def action_extensions(base_actions, project_path=os.getcwd()):
  9. """ Describes extensions for unit tests. This function expects that actions "all" and "reconfigure" """
  10. PROJECT_NAME = "unit-test-app"
  11. # List of unit-test-app configurations.
  12. # Each file in configs/ directory defines a configuration. The format is the
  13. # same as sdkconfig file. Configuration is applied on top of sdkconfig.defaults
  14. # file from the project directory
  15. CONFIG_NAMES = os.listdir(os.path.join(project_path, "configs"))
  16. # Build (intermediate) and output (artifact) directories
  17. BUILDS_DIR = os.path.join(project_path, "builds")
  18. BINARIES_DIR = os.path.join(project_path, "output")
  19. def parse_file_to_dict(path, regex):
  20. """
  21. Parse the config file at 'path'
  22. Returns a dict of name:value.
  23. """
  24. compiled_regex = re.compile(regex)
  25. result = {}
  26. with open(path) as f:
  27. for line in f:
  28. m = compiled_regex.match(line)
  29. if m:
  30. result[m.group(1)] = m.group(2)
  31. return result
  32. def parse_config(path):
  33. """
  34. Expected format with default regex is "key=value"
  35. """
  36. return parse_file_to_dict(path, r"^([^=]+)=(.+)$")
  37. def ut_apply_config(ut_apply_config_name, ctx, args):
  38. config_name = re.match(r"ut-apply-config-(.*)", ut_apply_config_name).group(1)
  39. # Make sure that define_cache_entry is list
  40. args.define_cache_entry = list(args.define_cache_entry)
  41. new_cache_values = {}
  42. sdkconfig_set = list(filter(lambda s: "SDKCONFIG=" in s, args.define_cache_entry))
  43. sdkconfig_path = os.path.join(args.project_dir, "sdkconfig")
  44. if sdkconfig_set:
  45. sdkconfig_path = sdkconfig_set[-1].split("=")[1]
  46. sdkconfig_path = os.path.abspath(sdkconfig_path)
  47. try:
  48. os.remove(sdkconfig_path)
  49. except OSError:
  50. pass
  51. if config_name in CONFIG_NAMES:
  52. # Parse the sdkconfig for components to be included/excluded and tests to be run
  53. config_path = os.path.join(project_path, "configs", config_name)
  54. config = parse_config(config_path)
  55. new_cache_values["EXCLUDE_COMPONENTS"] = config.get("EXCLUDE_COMPONENTS", "''")
  56. new_cache_values["TEST_EXCLUDE_COMPONENTS"] = config.get("TEST_EXCLUDE_COMPONENTS", "''")
  57. new_cache_values["TEST_COMPONENTS"] = config.get("TEST_COMPONENTS", "''")
  58. new_cache_values["TESTS_ALL"] = int(new_cache_values["TEST_COMPONENTS"] == "''")
  59. with tempfile.NamedTemporaryFile() as sdkconfig_temp:
  60. # Use values from the combined defaults and the values from
  61. # config folder to build config
  62. sdkconfig_default = os.path.join(project_path, "sdkconfig.defaults")
  63. with open(sdkconfig_default, "rb") as sdkconfig_default_file:
  64. sdkconfig_temp.write(sdkconfig_default_file.read())
  65. sdkconfig_config = os.path.join(project_path, "configs", config_name)
  66. with open(sdkconfig_config, "rb") as sdkconfig_config_file:
  67. sdkconfig_temp.write(b"\n")
  68. sdkconfig_temp.write(sdkconfig_config_file.read())
  69. sdkconfig_temp.flush()
  70. new_cache_values["SDKCONFIG_DEFAULTS"] = sdkconfig_temp.name
  71. args.define_cache_entry.extend(["%s=%s" % (k, v) for k, v in new_cache_values.items()])
  72. reconfigure = base_actions["actions"]["reconfigure"]["callback"]
  73. reconfigure(None, ctx, args)
  74. # This target builds the configuration. It does not currently track dependencies,
  75. # but is good enough for CI builds if used together with clean-all-configs.
  76. # For local builds, use 'apply-config-NAME' target and then use normal 'all'
  77. # and 'flash' targets.
  78. def ut_build(ut_build_name, ctx, args):
  79. # Create a copy of the passed arguments to prevent arg modifications to accrue if
  80. # all configs are being built
  81. build_args = copy.copy(args)
  82. config_name = re.match(r"ut-build-(.*)", ut_build_name).group(1)
  83. if config_name in CONFIG_NAMES:
  84. build_args.build_dir = os.path.join(BUILDS_DIR, config_name)
  85. src = os.path.join(BUILDS_DIR, config_name)
  86. dest = os.path.join(BINARIES_DIR, config_name)
  87. try:
  88. os.makedirs(dest)
  89. except OSError:
  90. pass
  91. # Build, tweaking paths to sdkconfig and sdkconfig.defaults
  92. ut_apply_config("ut-apply-config-" + config_name, ctx, build_args)
  93. build_target = base_actions["actions"]["all"]["callback"]
  94. build_target("all", ctx, build_args)
  95. # Copy artifacts to the output directory
  96. shutil.copyfile(
  97. os.path.join(build_args.project_dir, "sdkconfig"),
  98. os.path.join(dest, "sdkconfig"),
  99. )
  100. binaries = [PROJECT_NAME + x for x in [".elf", ".bin", ".map"]]
  101. for binary in binaries:
  102. shutil.copyfile(os.path.join(src, binary), os.path.join(dest, binary))
  103. try:
  104. os.mkdir(os.path.join(dest, "bootloader"))
  105. except OSError:
  106. pass
  107. shutil.copyfile(
  108. os.path.join(src, "bootloader", "bootloader.bin"),
  109. os.path.join(dest, "bootloader", "bootloader.bin"),
  110. )
  111. for partition_table in glob.glob(os.path.join(src, "partition_table", "partition-table*.bin")):
  112. try:
  113. os.mkdir(os.path.join(dest, "partition_table"))
  114. except OSError:
  115. pass
  116. shutil.copyfile(
  117. partition_table,
  118. os.path.join(dest, "partition_table", os.path.basename(partition_table)),
  119. )
  120. shutil.copyfile(
  121. os.path.join(src, "flasher_args.json"),
  122. os.path.join(dest, "flasher_args.json"),
  123. )
  124. binaries = glob.glob(os.path.join(src, "*.bin"))
  125. binaries = [os.path.basename(s) for s in binaries]
  126. for binary in binaries:
  127. shutil.copyfile(os.path.join(src, binary), os.path.join(dest, binary))
  128. def ut_clean(ut_clean_name, ctx, args):
  129. config_name = re.match(r"ut-clean-(.*)", ut_clean_name).group(1)
  130. if config_name in CONFIG_NAMES:
  131. shutil.rmtree(os.path.join(BUILDS_DIR, config_name), ignore_errors=True)
  132. shutil.rmtree(os.path.join(BINARIES_DIR, config_name), ignore_errors=True)
  133. def test_component_callback(ctx, global_args, tasks):
  134. """ Convert the values passed to the -T and -E parameter to corresponding cache entry definitions TESTS_ALL and TEST_COMPONENTS """
  135. test_components = global_args.test_components
  136. test_exclude_components = global_args.test_exclude_components
  137. cache_entries = {}
  138. if test_components:
  139. if "all" in test_components:
  140. cache_entries["TESTS_ALL"] = 1
  141. cache_entries["TEST_COMPONENTS"] = "''"
  142. else:
  143. cache_entries["TESTS_ALL"] = 0
  144. cache_entries["TEST_COMPONENTS"] = " ".join(test_components)
  145. if test_exclude_components:
  146. cache_entries["TEST_EXCLUDE_COMPONENTS"] = " ".join(test_exclude_components)
  147. if cache_entries:
  148. global_args.define_cache_entry = list(global_args.define_cache_entry)
  149. global_args.define_cache_entry.extend(["%s=%s" % (k, v) for k, v in cache_entries.items()])
  150. # Add global options
  151. extensions = {
  152. "global_options": [{
  153. "names": ["-T", "--test-components"],
  154. "help": "Specify the components to test.",
  155. "scope": "shared",
  156. "multiple": True,
  157. }, {
  158. "names": ["-E", "--test-exclude-components"],
  159. "help": "Specify the components to exclude from testing.",
  160. "scope": "shared",
  161. "multiple": True,
  162. }],
  163. "global_action_callbacks": [test_component_callback],
  164. "actions": {},
  165. }
  166. # This generates per-config targets (clean, build, apply-config).
  167. build_all_config_deps = []
  168. clean_all_config_deps = []
  169. for config in CONFIG_NAMES:
  170. config_build_action_name = "ut-build-" + config
  171. config_clean_action_name = "ut-clean-" + config
  172. config_apply_config_action_name = "ut-apply-config-" + config
  173. extensions["actions"][config_build_action_name] = {
  174. "callback":
  175. ut_build,
  176. "help":
  177. "Build unit-test-app with configuration provided in configs/NAME. " +
  178. "Build directory will be builds/%s/, " % config_build_action_name +
  179. "output binaries will be under output/%s/" % config_build_action_name,
  180. }
  181. extensions["actions"][config_clean_action_name] = {
  182. "callback": ut_clean,
  183. "help": "Remove build and output directories for configuration %s." % config_clean_action_name,
  184. }
  185. extensions["actions"][config_apply_config_action_name] = {
  186. "callback":
  187. ut_apply_config,
  188. "help":
  189. "Generates configuration based on configs/%s in sdkconfig file." % config_apply_config_action_name +
  190. "After this, normal all/flash targets can be used. Useful for development/debugging.",
  191. }
  192. build_all_config_deps.append(config_build_action_name)
  193. clean_all_config_deps.append(config_clean_action_name)
  194. extensions["actions"]["ut-build-all-configs"] = {
  195. "callback": ut_build,
  196. "help": "Build all configurations defined in configs/ directory.",
  197. "dependencies": build_all_config_deps,
  198. }
  199. extensions["actions"]["ut-clean-all-configs"] = {
  200. "callback": ut_clean,
  201. "help": "Remove build and output directories for all configurations defined in configs/ directory.",
  202. "dependencies": clean_all_config_deps,
  203. }
  204. return extensions