From f5423c40f2db70bfe092778b3785448b148f8719 Mon Sep 17 00:00:00 2001
From: Ivan Kravets <me@ikravets.com>
Date: Sat, 21 Apr 2018 00:56:30 +0300
Subject: [PATCH] @PlatformIO: VTables, custom lwIP, sync libs and flags //
 Resolve #4618

---
 tools/platformio-build.py | 83 ++++++++++++++++++++++++++++++++++-----
 1 file changed, 73 insertions(+), 10 deletions(-)

diff --git a/tools/platformio-build.py b/tools/platformio-build.py
index 45a138fd22..d6d935a57e 100644
--- a/tools/platformio-build.py
+++ b/tools/platformio-build.py
@@ -50,41 +50,104 @@ def scons_patched_match_splitext(path, suffixes=None):
 assert isdir(FRAMEWORK_DIR)
 
 
-env.Prepend(
+env.Append(
+    CCFLAGS=[
+        "-Wall"
+    ],
+
     CPPDEFINES=[
         ("ARDUINO", 10805),
+        ("ARDUINO_BOARD", '\\"PLATFORMIO_%s\\"'
+            % env.BoardConfig().id.upper()),
         "LWIP_OPEN_SRC"
     ],
+
     CPPPATH=[
         join(FRAMEWORK_DIR, "tools", "sdk", "include"),
-        join(FRAMEWORK_DIR, "tools", "sdk", "lwip", "include"),
         join(FRAMEWORK_DIR, "tools", "sdk", "libc",
              "xtensa-lx106-elf", "include"),
         join(FRAMEWORK_DIR, "cores", env.BoardConfig().get("build.core"))
     ],
+
     LIBPATH=[
+        join("$BUILD_DIR", "ld"),  # eagle.app.v6.common.ld
         join(FRAMEWORK_DIR, "tools", "sdk", "lib"),
         join(FRAMEWORK_DIR, "tools", "sdk", "ld"),
         join(FRAMEWORK_DIR, "tools", "sdk", "libc", "xtensa-lx106-elf", "lib")
     ],
+
     LIBS=[
-        "wpa2", "smartconfig", "espnow", "pp", "main", "wpa", "lwip_gcc",
-        "net80211", "wps", "crypto", "phy", "hal", "axtls", "gcc",
-        "m", "c", "stdc++"
-    ]
-)
+        "hal", "phy", "pp", "net80211", "wpa", "crypto", "main",
+        "wps", "axtls", "espnow", "smartconfig", "airkiss", "wpa2",
+        "stdc++", "m", "c", "gcc"
+    ],
 
-env.Append(
     LIBSOURCE_DIRS=[
         join(FRAMEWORK_DIR, "libraries")
-    ],
+    ]
+)
+
+env.Replace(
     LINKFLAGS=[
+        "-Os",
+        "-nostdlib",
+        "-Wl,--no-check-sections",
+        "-Wl,-static",
+        "-Wl,--gc-sections",
         "-Wl,-wrap,system_restart_local",
         "-Wl,-wrap,spi_flash_read",
-        "-u,app_entry"
+        "-u", "app_entry",
+        "-u", "_printf_float",
+        "-u", "_scanf_float"
     ]
 )
 
+flatten_cppdefines = env.Flatten(env['CPPDEFINES'])
+
+#
+# lwIP
+#
+if "PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY" in flatten_cppdefines:
+    env.Append(
+        CPPDEFINES=[("TCP_MSS", 536)],
+        CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")],
+        LIBS=["lwip2"]
+    )
+elif "PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH" in flatten_cppdefines:
+    env.Append(
+        CPPDEFINES=[("TCP_MSS", 1460)],
+        CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")],
+        LIBS=["lwip2_1460"]
+    )
+else:
+    env.Append(
+        CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip", "include")],
+        LIBS=["lwip_gcc"]
+    )
+
+#
+# VTables
+#
+
+current_vtables = None
+for d in flatten_cppdefines:
+    if str(d).startswith("VTABLES_IN_"):
+        current_vtables = d
+if not current_vtables:
+    current_vtables = "VTABLES_IN_FLASH"
+    env.Append(CPPDEFINES=[current_vtables])
+assert current_vtables
+
+# Build the eagle.app.v6.common.ld linker file
+app_ld = env.Command(
+    join("$BUILD_DIR", "ld", "eagle.app.v6.common.ld"),
+    join(FRAMEWORK_DIR, "tools", "sdk", "ld", "eagle.app.v6.common.ld.h"),
+    env.VerboseAction(
+        "$CC -CC -E -P -D%s $SOURCE -o $TARGET" % current_vtables,
+        "Generating LD script $TARGET"))
+env.Depends("$BUILD_DIR/$PROGNAME$PROGSUFFIX", app_ld)
+
+
 #
 # Target: Build Core Library
 #