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.

179 lines
5.1 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)))
  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. )
  85. def main(args):
  86. if os.path.exists(args.custom_h):
  87. raise SystemExit(
  88. clr(
  89. Color.YELLOW,
  90. "{} already exists, please run this script in a git-worktree(1) or a separate directory".format(
  91. args.custom_h
  92. ),
  93. )
  94. )
  95. configurations = []
  96. if not args.no_default:
  97. configurations = list(glob.glob(args.default_configurations))
  98. configurations.extend(x for x in (args.add or []))
  99. if not configurations:
  100. raise SystemExit(clr(Color.YELLOW, "No configurations selected"))
  101. if len(configurations) > 1:
  102. atexit.register(print_total_time)
  103. print(clr(Color.BOLD, "> Selected configurations:"))
  104. for cfg in configurations:
  105. print(cfg)
  106. if args.list:
  107. return
  108. if not args.environment:
  109. raise SystemExit(clr(Color.YELLOW, "No environment selected"))
  110. print(clr(Color.BOLD, "> Selected environment: {}".format(args.environment)))
  111. atexit.register(try_remove, args.custom_h)
  112. run_configurations(args, configurations)
  113. if __name__ == "__main__":
  114. parser = argparse.ArgumentParser()
  115. parser.add_argument(
  116. "-l", "--list", action="store_true", help="List selected configurations"
  117. )
  118. parser.add_argument(
  119. "--custom-h",
  120. default="espurna/config/custom.h",
  121. help="Header that will be included in by the config/all.h",
  122. )
  123. parser.add_argument(
  124. "-n",
  125. "--no-default",
  126. action="store_true",
  127. help="Do not use default configurations (--default-configurations=...)",
  128. )
  129. parser.add_argument(
  130. "-a",
  131. "--add",
  132. action="append",
  133. help="Add path to selected configurations (can specify multiple times)",
  134. )
  135. parser.add_argument("-e", "--environment", help="PIO environment")
  136. parser.add_argument(
  137. "--default-configurations",
  138. default="test/build/*.h",
  139. help="(glob) default configuration headers",
  140. )
  141. parser.add_argument(
  142. "--no-silent", action="store_true", help="Do not silence pio-run"
  143. )
  144. parser.add_argument(
  145. "--no-single-source", action="store_true", help="Disable 'unity' build"
  146. )
  147. main(parser.parse_args())