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.

178 lines
5.7 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 = "true" == 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, verbose=False):
  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 not verbose:
  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_REVISION=os.environ.get("ESPURNA_RELEASE_REVISION", ""),
  85. ESPURNA_RELEASE_NAME=os.environ.get("ESPURNA_RELEASE_NAME", ""),
  86. ESPURNA_RELEASE_VERSION=os.environ.get("ESPURNA_RELEASE_VERSION", ""),
  87. ESPURNA_RELEASE_DESTINATION=os.environ.get("ESPURNA_RELEASE_DESTINATION", ""),
  88. )
  89. # updates arduino core git to the latest master commit
  90. if CI:
  91. package_overrides = env.GetProjectOption("platform_packages")
  92. for package in package_overrides:
  93. if "https://github.com/esp8266/Arduino.git" in package:
  94. ensure_platform_updated()
  95. break
  96. # to speed-up build process, install libraries in either global or local shared storage
  97. if os.environ.get("ESPURNA_PIO_SHARED_LIBRARIES"):
  98. if CI:
  99. storage = None
  100. log("using global library storage")
  101. else:
  102. storage = get_shared_libdeps_dir("common", "shared_libdeps_dir")
  103. log("using shared library storage: {}".format(storage))
  104. subprocess_libdeps(env.GetProjectOption("lib_deps"), storage, verbose=VERBOSE)
  105. # tweak build system to ignore espurna.ino, but include user code
  106. # ref: platformio-core/platformio/tools/piomisc.py::ConvertInoToCpp()
  107. def ConvertInoToCpp(env):
  108. pass
  109. ino = env.Glob("$PROJECT_DIR/espurna/*.ino") + env.Glob("$PROJECT_DIR/espurna/*.pde")
  110. if len(ino) == 1 and ino[0].name == "espurna.ino":
  111. env.AddMethod(ConvertInoToCpp)
  112. # merge every .cpp into a single file and **only** build that single file
  113. if "1" == os.environ.get("ESPURNA_BUILD_SINGLE_SOURCE", "0"):
  114. cpp_files = []
  115. for root, dirs, filenames in os.walk("espurna"):
  116. for name in filenames:
  117. if not name.endswith(".cpp"):
  118. continue
  119. abspath = os.path.join(os.path.abspath(root), name)
  120. env.AddBuildMiddleware(lambda node: None, abspath)
  121. relpath = os.path.relpath(abspath, "espurna")
  122. cpp_files.append(relpath)
  123. merge_cpp(cpp_files, "espurna/espurna_single_source.cpp")
  124. # make sure to register as a valid command. however, it is always called right here
  125. # (--list-targets is a kind-of inefficient for finding this, though, since it *will* install libs into .pio/ anyways...)
  126. def install_libs_dummy(target, source, env):
  127. pass
  128. env.AddCustomTarget("install-libs", None, install_libs_dummy)
  129. from SCons.Script import COMMAND_LINE_TARGETS
  130. if "install-libs" in COMMAND_LINE_TARGETS:
  131. storage = get_shared_libdeps_dir("common", "shared_libdeps_dir")
  132. subprocess_libdeps(env.GetProjectOption("lib_deps"), storage, verbose=VERBOSE)
  133. sys.exit(0)