Skip to content

Commit

Permalink
PIO: pass APP_REVISION via extra scripts (#1946)
Browse files Browse the repository at this point in the history
* utils/version: use git description token as version, add memoization

* pio: move extra scripts to a separate directory

* pio: add -DAPP_REVISION=... as local build flag
  • Loading branch information
mcspr authored Oct 16, 2019
1 parent 6cb503b commit c18490c
Show file tree
Hide file tree
Showing 15 changed files with 275 additions and 191 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ custom.h
.env
.DS_Store
.vscode
_pycache_/
*.py[cod]
3 changes: 0 additions & 3 deletions code/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ list_envs() {
travis=$(list_envs | grep travis | sort)
available=$(list_envs | grep -Ev -- '-ota$|-ssl$|^travis' | sort)

# Build tools settings
export PLATFORMIO_BUILD_FLAGS="${PLATFORMIO_BUILD_FLAGS} -DAPP_REVISION='\"$git_revision\"'"

# Functions
print_available() {
echo "--------------------------------------------------------------"
Expand Down
41 changes: 27 additions & 14 deletions code/espurna/utils.ino
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,36 @@ String getAdminPass() {
return getSetting("adminPass", ADMIN_PASS);
}

String getCoreVersion() {
String version = ESP.getCoreVersion();
#ifdef ARDUINO_ESP8266_RELEASE
if (version.equals("00000000")) {
version = String(ARDUINO_ESP8266_RELEASE);
}
#endif
version.replace("_", ".");
const String& getCoreVersion() {
static String version;
if (!version.length()) {
#ifdef ARDUINO_ESP8266_RELEASE
version = ESP.getCoreVersion();
if (version.equals("00000000")) {
version = String(ARDUINO_ESP8266_RELEASE);
}
version.replace("_", ".");
#else
#define _GET_COREVERSION_STR(X) #X
#define GET_COREVERSION_STR(X) _GET_COREVERSION_STR(X)
version = GET_COREVERSION_STR(ARDUINO_ESP8266_GIT_DESC);
#undef _GET_COREVERSION_STR
#undef GET_COREVERSION_STR
#endif
}
return version;
}

String getCoreRevision() {
#ifdef ARDUINO_ESP8266_GIT_VER
return String(ARDUINO_ESP8266_GIT_VER, 16);
#else
return String("");
#endif
const String& getCoreRevision() {
static String revision;
if (!revision.length()) {
#ifdef ARDUINO_ESP8266_GIT_VER
revision = String(ARDUINO_ESP8266_GIT_VER, 16);
#else
revision = "";
#endif
}
return revision;
}

unsigned char getHeartbeatMode() {
Expand Down
171 changes: 0 additions & 171 deletions code/extra_scripts.py

This file was deleted.

2 changes: 1 addition & 1 deletion code/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ framework = arduino
board_build.flash_mode = dout
monitor_speed = 115200
upload_speed = 115200
extra_scripts = pre:extra_script_pre.py, extra_scripts.py
extra_scripts = pre:scripts/pio_pre.py, scripts/pio_main.py
lib_extra_dirs =
${common.shared_libdeps_dir}

Expand Down
6 changes: 6 additions & 0 deletions code/scripts/espurna_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .checks import check_cppcheck, check_printsize
from .float_support import remove_float_support
from .ldscripts import ldscripts_inject_libpath
from .lwip import lwip_inject_patcher
from .postmortem import dummy_ets_printf
from .git import app_inject_revision
29 changes: 29 additions & 0 deletions code/scripts/espurna_utils/checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os

from .display import Color, clr, print_filler, print_warning


def check_printsize(target, source, env):
(binary,) = target
path = binary.get_abspath()
size = os.stat(path).st_size
print(clr(Color.LIGHT_BLUE, "Binary size: {} bytes".format(size)))

# Warn 1MB variants about exceeding OTA size limit
flash_size = int(env.BoardConfig().get("upload.maximum_size", 0))
if (flash_size == 1048576) and (size >= 512000):
print_filler("*", color=Color.LIGHT_YELLOW, err=True)
print_warning(
"File is too large for OTA! Here you can find instructions on how to flash it:"
)
print_warning(
"https://github.com/xoseperez/espurna/wiki/TwoStepUpdates",
color=Color.LIGHT_CYAN,
)
print_filler("*", color=Color.LIGHT_YELLOW, err=True)


def check_cppcheck(target, source, env):
print_warning("Started cppcheck...\n")
call(["cppcheck", os.getcwd() + "/espurna", "--force", "--enable=all"])
print_warning("Finished cppcheck...\n")
40 changes: 40 additions & 0 deletions code/scripts/espurna_utils/display.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from __future__ import print_function

import sys
import click


class Color(object):
BLACK = "\x1b[1;30m"
RED = "\x1b[1;31m"
GREEN = "\x1b[1;32m"
YELLOW = "\x1b[1;33m"
BLUE = "\x1b[1;34m"
MAGENTA = "\x1b[1;35m"
CYAN = "\x1b[1;36m"
WHITE = "\x1b[1;37m"
LIGHT_GREY = "\x1b[0;30m"
LIGHT_RED = "\x1b[0;31m"
LIGHT_GREEN = "\x1b[0;32m"
LIGHT_YELLOW = "\x1b[0;33m"
LIGHT_BLUE = "\x1b[0;34m"
LIGHT_MAGENTA = "\x1b[0;35m"
LIGHT_CYAN = "\x1b[0;36m"
LIGHT_WHITE = "\x1b[0;37m"


def clr(color, text):
return color + str(text) + "\x1b[0m"


def print_warning(message, color=Color.LIGHT_YELLOW):
print(clr(color, message), file=sys.stderr)


def print_filler(fill, color=Color.WHITE, err=False):
width, _ = click.get_terminal_size()
if len(fill) > 1:
fill = fill[0]

out = sys.stderr if err else sys.stdout
print(clr(color, fill * width), file=out)
8 changes: 8 additions & 0 deletions code/scripts/espurna_utils/float_support.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def remove_float_support(env):

flags = " ".join(env["LINKFLAGS"])
flags = flags.replace("-u _printf_float", "")
flags = flags.replace("-u _scanf_float", "")
newflags = flags.split()

env.Replace(LINKFLAGS=newflags)
23 changes: 23 additions & 0 deletions code/scripts/espurna_utils/git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os
import subprocess

def git(*args):
cmd = ["git"]
cmd.extend(args)
proc = subprocess.Popen(
cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True
)
return proc.stdout.readlines()[0].strip()

def app_inject_revision(env):
revision = ""
try:
revision = "\\\"{}\\\"".format(git("rev-parse", "--short=8", "HEAD"))
except: # pylint: disable=broad-except
pass

# Note: code expects this as undefined when empty
if revision:
env.Append(CPPDEFINES=[
("APP_REVISION", revision)
])
24 changes: 24 additions & 0 deletions code/scripts/espurna_utils/ldscripts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os


def ldscripts_inject_libpath(env):

platform = env.PioPlatform()
framework_dir = platform.get_package_dir("framework-arduinoespressif8266")

# [email protected] did not append this directory into the LIBPATH
libpath_sdk = os.path.join(framework_dir, "tools", "sdk", "ld")
env.Append(LIBPATH=[libpath_sdk])

libpath_base = os.path.join("$PROJECT_DIR", "..", "dist", "ld")
env.Append(LIBPATH=[os.path.join(libpath_base, "pre_2.5.0")])

# local.eagle.app.v6.common.ld exists only with Core >2.5.0
def check_local_ld(target, source, env):
local_ld = env.subst(
os.path.join("$BUILD_DIR", "ld", "local.eagle.app.v6.common.ld")
)
if os.path.exists(local_ld):
env.Prepend(LIBPATH=[os.path.join(libpath_base, "latest")])

env.AddPreAction(os.path.join("$BUILD_DIR", "firmware.elf"), check_local_ld)
Loading

0 comments on commit c18490c

Please sign in to comment.