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.

145 lines
5.0 KiB

6 years ago
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import os
  4. import sys
  5. from subprocess import call
  6. import click
  7. Import("env", "projenv")
  8. # ------------------------------------------------------------------------------
  9. # Utils
  10. # ------------------------------------------------------------------------------
  11. class Color(object):
  12. BLACK = '\x1b[1;30m'
  13. RED = '\x1b[1;31m'
  14. GREEN = '\x1b[1;32m'
  15. YELLOW = '\x1b[1;33m'
  16. BLUE = '\x1b[1;34m'
  17. MAGENTA = '\x1b[1;35m'
  18. CYAN = '\x1b[1;36m'
  19. WHITE = '\x1b[1;37m'
  20. LIGHT_GREY = '\x1b[0;30m'
  21. LIGHT_RED = '\x1b[0;31m'
  22. LIGHT_GREEN = '\x1b[0;32m'
  23. LIGHT_YELLOW = '\x1b[0;33m'
  24. LIGHT_BLUE = '\x1b[0;34m'
  25. LIGHT_MAGENTA = '\x1b[0;35m'
  26. LIGHT_CYAN = '\x1b[0;36m'
  27. LIGHT_WHITE = '\x1b[0;37m'
  28. def clr(color, text):
  29. return color + str(text) + '\x1b[0m'
  30. def print_warning(message, color=Color.LIGHT_YELLOW):
  31. print(clr(color, message), file=sys.stderr)
  32. def print_filler(fill, color=Color.WHITE, err=False):
  33. width, _ = click.get_terminal_size()
  34. if len(fill) > 1:
  35. fill = fill[0]
  36. out = sys.stderr if err else sys.stdout
  37. print(clr(color, fill * width), file=out)
  38. # ------------------------------------------------------------------------------
  39. # Callbacks
  40. # ------------------------------------------------------------------------------
  41. def remove_float_support():
  42. flags = " ".join(env['LINKFLAGS'])
  43. flags = flags.replace("-u _printf_float", "")
  44. flags = flags.replace("-u _scanf_float", "")
  45. newflags = flags.split()
  46. env.Replace(
  47. LINKFLAGS = newflags
  48. )
  49. def cpp_check(target, source, env):
  50. print("Started cppcheck...\n")
  51. call(["cppcheck", os.getcwd()+"/espurna", "--force", "--enable=all"])
  52. print("Finished cppcheck...\n")
  53. def check_size(target, source, env):
  54. (binary,) = target
  55. path = binary.get_abspath()
  56. size = os.stat(path).st_size
  57. print(clr(Color.LIGHT_BLUE, "Binary size: {} bytes".format(size)))
  58. # Warn 1MB variants about exceeding OTA size limit
  59. flash_size = int(env.BoardConfig().get("upload.maximum_size", 0))
  60. if (flash_size == 1048576) and (size >= 512000):
  61. print_filler("*", color=Color.LIGHT_YELLOW, err=True)
  62. print_warning("File is too large for OTA! Here you can find instructions on how to flash it:")
  63. print_warning("https://github.com/xoseperez/espurna/wiki/TwoStepUpdates", color=Color.LIGHT_CYAN)
  64. print_filler("*", color=Color.LIGHT_YELLOW, err=True)
  65. def dummy_ets_printf(target, source, env):
  66. (postmortem_src_file, ) = source
  67. (postmortem_obj_file, ) = target
  68. cmd = ["xtensa-lx106-elf-objcopy"]
  69. # recent Core switched to cpp+newlib & ets_printf_P
  70. cmd.extend(["--redefine-sym", "ets_printf=dummy_ets_printf"])
  71. cmd.extend(["--redefine-sym", "ets_printf_P=dummy_ets_printf"])
  72. cmd.append(postmortem_obj_file.get_abspath())
  73. env.Execute(env.VerboseAction(" ".join(cmd), "Removing ets_printf / ets_printf_P"))
  74. env.Depends(postmortem_obj_file,"$BUILD_DIR/src/dummy_ets_printf.c.o")
  75. def patch_lwip():
  76. # ignore when building with lwip2
  77. if "lwip_gcc" not in env["LIBS"]:
  78. return
  79. framework_dir = env["FRAMEWORK_ARDUINOESP8266_DIR"]
  80. platform = env.PioPlatform()
  81. toolchain_prefix = os.path.join(platform.get_package_dir("toolchain-xtensa"), "bin", "xtensa-lx106-elf-")
  82. patch_action = env.VerboseAction(" ".join([
  83. "-patch", "-u", "-N", "-d",
  84. os.path.join(framework_dir, "tools", "sdk", "lwip"),
  85. os.path.join("src", "core", "tcp_out.c"),
  86. env.subst(os.path.join("$PROJECT_DIR", "..", "dist", "patches", "lwip_mtu_issue_1610.patch"))
  87. ]), "Patching lwip source")
  88. build_action = env.VerboseAction(" ".join([
  89. "make", "-C", os.path.join(framework_dir, "tools", "sdk", "lwip", "src"),
  90. "install",
  91. "TOOLS_PATH={}".format(toolchain_prefix),
  92. "LWIP_LIB=liblwip_gcc.a"
  93. ]), "Rebuilding lwip")
  94. patcher = env.Alias("patch-lwip", None, patch_action)
  95. builder = env.Alias("build-lwip", patcher, build_action)
  96. if os.environ.get("ESPURNA_PIO_PATCH_ISSUE_1610"):
  97. env.Depends("$BUILD_DIR/${PROGNAME}.elf", builder)
  98. env.AlwaysBuild(patcher)
  99. env.AlwaysBuild(builder)
  100. # ------------------------------------------------------------------------------
  101. # Hooks
  102. # ------------------------------------------------------------------------------
  103. # Always show warnings for project code
  104. projenv.ProcessUnFlags("-w")
  105. # 2.4.0 and up
  106. remove_float_support()
  107. # two-step update hint when using 1MB boards
  108. env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", check_size)
  109. # disable postmortem printing to the uart. another one is in eboot, but this is what causes the most harm
  110. if "DISABLE_POSTMORTEM_STACKDUMP" in env["CPPFLAGS"]:
  111. env.AddPostAction("$BUILD_DIR/FrameworkArduino/core_esp8266_postmortem.c.o", dummy_ets_printf)
  112. env.AddPostAction("$BUILD_DIR/FrameworkArduino/core_esp8266_postmortem.cpp.o", dummy_ets_printf)
  113. # patch lwip1 sources conditionally:
  114. # https://github.com/xoseperez/espurna/issues/1610
  115. patch_lwip()