Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.2 KiB

  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2019 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. from __future__ import print_function
  18. import time
  19. import glob
  20. import argparse
  21. import atexit
  22. import subprocess
  23. import os
  24. import sys
  25. import datetime
  26. from espurna_utils.display import clr, print_warning, Color
  27. def restore_source_tree(files):
  28. cmd = ["git", "checkout", "-f", "--"]
  29. cmd.extend(files)
  30. subprocess.check_call(cmd)
  31. def try_remove(path):
  32. try:
  33. os.remove(path)
  34. except: # pylint: disable=bare-except
  35. print_warning("Please manually remove the file `{}`".format(path))
  36. total_time = 0
  37. def print_total_time():
  38. print()
  39. print(
  40. clr(
  41. Color.BOLD,
  42. "> Total time: {}".format(datetime.timedelta(seconds=total_time)),
  43. )
  44. )
  45. def run_configurations(args, configurations):
  46. cmd = ["platformio", "run"]
  47. if not args.no_silent:
  48. cmd.extend(["-s"])
  49. cmd.extend(["-e", args.environment])
  50. for cfg in configurations:
  51. print(clr(Color.BOLD, "> Building {}".format(cfg)), flush=True)
  52. with open(args.custom_h, "w") as custom_h:
  53. def write(line):
  54. sys.stdout.write(line)
  55. custom_h.write(line)
  56. name, _ = os.path.splitext(cfg)
  57. name = os.path.basename(name)
  58. write('#define MANUFACTURER "TEST_BUILD"\n')
  59. write('#define DEVICE "{}"\n'.format(name.upper()))
  60. with open(cfg, "r") as cfg_file:
  61. for line in cfg_file:
  62. write(line)
  63. os_env = os.environ.copy()
  64. os_env["PLATFORMIO_SRC_BUILD_FLAGS"] = "-DUSE_CUSTOM_H"
  65. os_env["PLATFORMIO_BUILD_CACHE_DIR"] = "test/pio_cache"
  66. if not args.no_single_source:
  67. os_env["ESPURNA_BUILD_SINGLE_SOURCE"] = "1"
  68. start = time.time()
  69. subprocess.check_call(cmd, env=os_env)
  70. diff = time.time() - start
  71. global total_time
  72. total_time += diff
  73. print(
  74. clr(
  75. Color.BOLD,
  76. "> {}: {} bytes, {}".format(
  77. cfg,
  78. os.stat(
  79. os.path.join(".pio", "build", args.environment, "firmware.bin")
  80. ).st_size,
  81. datetime.timedelta(seconds=diff),
  82. ),
  83. ),
  84. flush=True,
  85. )
  86. def main(args):
  87. if os.path.exists(args.custom_h):
  88. raise SystemExit(
  89. clr(
  90. Color.YELLOW,
  91. "{} already exists, please run this script in a git-worktree(1) or a separate directory".format(
  92. args.custom_h
  93. ),
  94. )
  95. )
  96. configurations = []
  97. if not args.no_default:
  98. configurations = list(glob.glob(args.default_configurations))
  99. configurations.extend(x for x in (args.add or []))
  100. if not configurations:
  101. raise SystemExit(clr(Color.YELLOW, "No configurations selected"))
  102. if len(configurations) > 1:
  103. atexit.register(print_total_time)
  104. print(clr(Color.BOLD, "> Selected configurations:"))
  105. for cfg in configurations:
  106. print(cfg)
  107. if args.list:
  108. return
  109. if not args.environment:
  110. raise SystemExit(clr(Color.YELLOW, "No environment selected"))
  111. print(clr(Color.BOLD, "> Selected environment: {}".format(args.environment)))
  112. atexit.register(try_remove, args.custom_h)
  113. run_configurations(args, configurations)
  114. if __name__ == "__main__":
  115. parser = argparse.ArgumentParser()
  116. parser.add_argument(
  117. "-l", "--list", action="store_true", help="List selected configurations"
  118. )
  119. parser.add_argument(
  120. "--custom-h",
  121. default="espurna/config/custom.h",
  122. help="Header that will be included in by the config/all.h",
  123. )
  124. parser.add_argument(
  125. "-n",
  126. "--no-default",
  127. action="store_true",
  128. help="Do not use default configurations (--default-configurations=...)",
  129. )
  130. parser.add_argument(
  131. "-a",
  132. "--add",
  133. action="append",
  134. help="Add path to selected configurations (can specify multiple times)",
  135. )
  136. parser.add_argument("-e", "--environment", help="PIO environment")
  137. parser.add_argument(
  138. "--default-configurations",
  139. default="test/build/*.h",
  140. help="(glob) default configuration headers",
  141. )
  142. parser.add_argument(
  143. "--no-silent", action="store_true", help="Do not silence pio-run"
  144. )
  145. parser.add_argument(
  146. "--no-single-source", action="store_true", help="Disable 'unity' build"
  147. )
  148. main(parser.parse_args())