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.

133 lines
4.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 Color, clr, print_warning
  27. CUSTOM_HEADER = "espurna/config/custom.h"
  28. if os.path.exists(CUSTOM_HEADER):
  29. raise SystemExit(
  30. clr(
  31. Color.YELLOW,
  32. "{} already exists, please run this script in a git-worktree(1) or a separate directory".format(
  33. CUSTOM_HEADER
  34. ),
  35. )
  36. )
  37. def try_remove(path):
  38. try:
  39. os.remove(path)
  40. except: # pylint: disable=bare-except
  41. print_warning("Please manually remove the file `{}`".format(path))
  42. atexit.register(try_remove, CUSTOM_HEADER)
  43. def main(args):
  44. configurations = []
  45. if not args.no_default:
  46. configurations = list(glob.glob(args.default_configurations))
  47. configurations.extend(x for x in (args.add or []))
  48. if not configurations:
  49. raise SystemExit(clr(Color.YELLOW, "No configurations selected"))
  50. print(clr(Color.BOLD, "> Selected configurations:"))
  51. for cfg in configurations:
  52. print(cfg)
  53. if args.list:
  54. return
  55. if not args.environment:
  56. raise SystemExit(clr(Color.YELLOW, "No environment selected"))
  57. print(clr(Color.BOLD, "> Selected environment: {}".format(args.environment)))
  58. for cfg in configurations:
  59. print(clr(Color.BOLD, "> Building {}".format(cfg)))
  60. with open(CUSTOM_HEADER, "w") as custom_h:
  61. def write(line):
  62. sys.stdout.write(line)
  63. custom_h.write(line)
  64. name, _ = os.path.splitext(cfg)
  65. name = os.path.basename(name)
  66. write('#define MANUFACTURER "TEST_BUILD"\n')
  67. write('#define DEVICE "{}"\n'.format(name.upper()))
  68. with open(cfg, "r") as cfg_file:
  69. for line in cfg_file:
  70. write(line)
  71. os_env = os.environ.copy()
  72. os_env["PLATFORMIO_SRC_BUILD_FLAGS"] = "-DUSE_CUSTOM_H"
  73. os_env["PLATFORMIO_BUILD_CACHE_DIR"] = "test/pio_cache"
  74. cmd = ["platformio", "run", "-s", "-e", args.environment]
  75. start = time.time()
  76. subprocess.check_call(cmd, env=os_env)
  77. end = time.time()
  78. print(
  79. clr(
  80. Color.BOLD,
  81. "> {}: {} bytes, {}".format(
  82. cfg,
  83. os.stat(
  84. os.path.join(".pio", "build", args.environment, "firmware.bin")
  85. ).st_size,
  86. datetime.timedelta(seconds=(end - start)),
  87. ),
  88. )
  89. )
  90. if __name__ == "__main__":
  91. parser = argparse.ArgumentParser()
  92. parser.add_argument(
  93. "-l", "--list", action="store_true", help="List selected configurations"
  94. )
  95. parser.add_argument(
  96. "-n",
  97. "--no-default",
  98. action="store_true",
  99. help="Do not use default configurations (--default-configurations=...)",
  100. )
  101. parser.add_argument(
  102. "-a",
  103. "--add",
  104. action="append",
  105. help="Add path to selected configurations (can specify multiple times)",
  106. )
  107. parser.add_argument("-e", "--environment", help="PIO environment")
  108. parser.add_argument(
  109. "--default-configurations",
  110. default="test/build/*.h",
  111. help="(glob) default configuration headers",
  112. )
  113. main(parser.parse_args())