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.

138 lines
4.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. CI = any([os.environ.get("TRAVIS"), os.environ.get("CI")])
  16. PIO_PLATFORM = env.PioPlatform()
  17. CONFIG = env.GetProjectConfig()
  18. VERBOSE = "1" == ARGUMENTS.get("PIOVERBOSE", "0")
  19. class ExtraScriptError(Exception):
  20. pass
  21. def log(message, verbose=False, file=sys.stderr):
  22. if verbose or VERBOSE:
  23. print(message, file=file)
  24. # Most portable way, without depending on platformio internals
  25. def subprocess_libdeps(lib_deps, storage=None, silent=True):
  26. import subprocess
  27. args = [env.subst("$PYTHONEXE"), "-mplatformio", "lib"]
  28. if not storage:
  29. args.append("-g")
  30. else:
  31. args.extend(["-d", storage])
  32. args.append("install")
  33. if silent:
  34. args.append("-s")
  35. args.extend(lib_deps)
  36. subprocess.check_call(args)
  37. # Avoid spawning pio lib every time, hook into the LibraryManager API (sort-of internal)
  38. def library_manager_libdeps(lib_deps, storage=None):
  39. from platformio.managers.lib import LibraryManager
  40. from platformio.project.helpers import get_project_global_lib_dir
  41. if not storage:
  42. manager = LibraryManager(get_project_global_lib_dir())
  43. else:
  44. manager = LibraryManager(storage)
  45. for lib in lib_deps:
  46. if manager.get_package_dir(*manager.parse_pkg_uri(lib)):
  47. continue
  48. log("installing: {}".format(lib))
  49. manager.install(lib)
  50. def get_shared_libdeps_dir(section, name):
  51. if not CONFIG.has_option(section, name):
  52. raise ExtraScriptError("{}.{} is required to be set".format(section, name))
  53. opt = CONFIG.get(section, name)
  54. if not opt in env.GetProjectOption("lib_extra_dirs"):
  55. raise ExtraScriptError(
  56. "lib_extra_dirs must contain {}.{}".format(section, name)
  57. )
  58. return os.path.join(env["PROJECT_DIR"], opt)
  59. def ensure_platform_updated():
  60. try:
  61. if PIO_PLATFORM.are_outdated_packages():
  62. log("updating platform packages")
  63. PIO_PLATFORM.update_packages()
  64. except Exception:
  65. log("Warning: no connection, cannot check for outdated packages", verbose=True)
  66. # handle build flags through os environment.
  67. # using env instead of ini to avoid platformio ini changing hash on every change
  68. env.Append(
  69. ESPURNA_BOARD=os.environ.get("ESPURNA_BOARD", ""),
  70. ESPURNA_AUTH=os.environ.get("ESPURNA_AUTH", ""),
  71. ESPURNA_FLAGS=os.environ.get("ESPURNA_FLAGS", "")
  72. )
  73. ESPURNA_OTA_PORT = os.environ.get("ESPURNA_IP")
  74. if ESPURNA_OTA_PORT:
  75. env.Replace(UPLOAD_PROTOCOL="espota")
  76. env.Replace(UPLOAD_PORT=ESPURNA_OTA_PORT)
  77. env.Replace(UPLOAD_FLAGS="--auth=$ESPURNA_AUTH")
  78. else:
  79. env.Replace(UPLOAD_PROTOCOL="esptool")
  80. # handle `-t release` parameters
  81. if CI:
  82. env.Append(
  83. ESPURNA_RELEASE_NAME=os.environ.get("ESPURNA_RELEASE_NAME", ""),
  84. ESPURNA_RELEASE_VERSION=os.environ.get("ESPURNA_RELEASE_VERSION", ""),
  85. ESPURNA_RELEASE_DESTINATION=os.environ.get("ESPURNA_RELEASE_DESTINATION", "")
  86. )
  87. # updates arduino core git to the latest master commit
  88. if CI:
  89. package_overrides = env.GetProjectOption("platform_packages")
  90. for package in package_overrides:
  91. if "https://github.com/esp8266/Arduino.git" in package:
  92. ensure_platform_updated()
  93. break
  94. # to speed-up build process, install libraries in either global or local shared storage
  95. if os.environ.get("ESPURNA_PIO_SHARED_LIBRARIES"):
  96. if CI:
  97. storage = None
  98. log("using global library storage")
  99. else:
  100. storage = get_shared_libdeps_dir("common", "shared_libdeps_dir")
  101. log("using shared library storage: {}".format(storage))
  102. subprocess_libdeps(env.GetProjectOption("lib_deps"), storage)