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.

165 lines
5.1 KiB

  1. # coding=utf-8
  2. # pylint: dummy-variables-rgx='(_+[a-zA-Z0-9]*?$)|dummy|env'
  3. #
  4. # Original extra_scripts.py
  5. # Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  6. #
  7. # ldscripts, lwip patching, updated postmortem flags and git support
  8. # Copyright (C) 2019-2020 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
  9. # Run this script every time building an env BEFORE platform-specific code is loaded
  10. from __future__ import print_function
  11. Import("env")
  12. import os
  13. import sys
  14. from SCons.Script import ARGUMENTS
  15. from espurna_utils.release import merge_cpp
  16. CI = any([os.environ.get("TRAVIS"), os.environ.get("CI")])
  17. PIO_PLATFORM = env.PioPlatform()
  18. CONFIG = env.GetProjectConfig()
  19. VERBOSE = "1" == ARGUMENTS.get("PIOVERBOSE", "0")
  20. class ExtraScriptError(Exception):
  21. pass
  22. def log(message, verbose=False, file=sys.stderr):
  23. if verbose or VERBOSE:
  24. print(message, file=file)
  25. # Most portable way, without depending on platformio internals
  26. def subprocess_libdeps(lib_deps, storage=None, silent=True):
  27. import subprocess
  28. args = [env.subst("$PYTHONEXE"), "-mplatformio", "lib"]
  29. if not storage:
  30. args.append("-g")
  31. else:
  32. args.extend(["-d", storage])
  33. args.append("install")
  34. if silent:
  35. args.append("-s")
  36. args.extend(lib_deps)
  37. subprocess.check_call(args)
  38. # Avoid spawning pio lib every time, hook into the LibraryManager API (sort-of internal)
  39. def library_manager_libdeps(lib_deps, storage=None):
  40. from platformio.managers.lib import LibraryManager
  41. from platformio.project.helpers import get_project_global_lib_dir
  42. if not storage:
  43. manager = LibraryManager(get_project_global_lib_dir())
  44. else:
  45. manager = LibraryManager(storage)
  46. for lib in lib_deps:
  47. if manager.get_package_dir(*manager.parse_pkg_uri(lib)):
  48. continue
  49. log("installing: {}".format(lib))
  50. manager.install(lib)
  51. def get_shared_libdeps_dir(section, name):
  52. if not CONFIG.has_option(section, name):
  53. raise ExtraScriptError("{}.{} is required to be set".format(section, name))
  54. opt = CONFIG.get(section, name)
  55. if not opt in env.GetProjectOption("lib_extra_dirs"):
  56. raise ExtraScriptError(
  57. "lib_extra_dirs must contain {}.{}".format(section, name)
  58. )
  59. return os.path.join(env["PROJECT_DIR"], opt)
  60. def ensure_platform_updated():
  61. try:
  62. if PIO_PLATFORM.are_outdated_packages():
  63. log("updating platform packages")
  64. PIO_PLATFORM.update_packages()
  65. except Exception:
  66. log("Warning: no connection, cannot check for outdated packages", verbose=True)
  67. # handle build flags through os environment.
  68. # using env instead of ini to avoid platformio ini changing hash on every change
  69. env.Append(
  70. ESPURNA_BOARD=os.environ.get("ESPURNA_BOARD", ""),
  71. ESPURNA_AUTH=os.environ.get("ESPURNA_AUTH", ""),
  72. ESPURNA_FLAGS=os.environ.get("ESPURNA_FLAGS", ""),
  73. )
  74. ESPURNA_OTA_PORT = os.environ.get("ESPURNA_IP")
  75. if ESPURNA_OTA_PORT:
  76. env.Replace(UPLOAD_PROTOCOL="espota")
  77. env.Replace(UPLOAD_PORT=ESPURNA_OTA_PORT)
  78. env.Replace(UPLOAD_FLAGS="--auth=$ESPURNA_AUTH")
  79. else:
  80. env.Replace(UPLOAD_PROTOCOL="esptool")
  81. # handle `-t release` parameters
  82. if CI:
  83. env.Append(
  84. ESPURNA_RELEASE_NAME=os.environ.get("ESPURNA_RELEASE_NAME", ""),
  85. ESPURNA_RELEASE_VERSION=os.environ.get("ESPURNA_RELEASE_VERSION", ""),
  86. ESPURNA_RELEASE_DESTINATION=os.environ.get("ESPURNA_RELEASE_DESTINATION", ""),
  87. )
  88. # updates arduino core git to the latest master commit
  89. if CI:
  90. package_overrides = env.GetProjectOption("platform_packages")
  91. for package in package_overrides:
  92. if "https://github.com/esp8266/Arduino.git" in package:
  93. ensure_platform_updated()
  94. break
  95. # to speed-up build process, install libraries in either global or local shared storage
  96. if os.environ.get("ESPURNA_PIO_SHARED_LIBRARIES"):
  97. if CI:
  98. storage = None
  99. log("using global library storage")
  100. else:
  101. storage = get_shared_libdeps_dir("common", "shared_libdeps_dir")
  102. log("using shared library storage: {}".format(storage))
  103. subprocess_libdeps(env.GetProjectOption("lib_deps"), storage)
  104. # tweak build system to ignore espurna.ino, but include user code
  105. # ref: platformio-core/platformio/tools/piomisc.py::ConvertInoToCpp()
  106. def ConvertInoToCpp(env):
  107. pass
  108. ino = env.Glob("$PROJECT_DIR/espurna/*.ino") + env.Glob("$PROJECT_DIR/espurna/*.pde")
  109. if len(ino) == 1 and ino[0].name == "espurna.ino":
  110. env.AddMethod(ConvertInoToCpp)
  111. # merge every .cpp into a single file and **only** build that single file
  112. if "1" == os.environ.get("ESPURNA_BUILD_SINGLE_SOURCE", "0"):
  113. cpp_files = []
  114. for root, dirs, filenames in os.walk("espurna"):
  115. for name in filenames:
  116. if not name.endswith(".cpp"):
  117. continue
  118. abspath = os.path.join(os.path.abspath(root), name)
  119. env.AddBuildMiddleware(lambda node: None, abspath)
  120. relpath = os.path.relpath(abspath, "espurna")
  121. cpp_files.append(relpath)
  122. merge_cpp(cpp_files, "espurna/espurna_single_source.cpp")