From 19132b32b7142b5e5b73da16104527177a39d2ba Mon Sep 17 00:00:00 2001 From: Maximilian Gerhardt Date: Mon, 9 Aug 2021 14:13:01 +0200 Subject: [PATCH 1/3] Respect linking order of libraries Now has the same order as the Arduino IDE does with its platform.txt --- tools/platformio-build.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/tools/platformio-build.py b/tools/platformio-build.py index 8998fcde78..db873c77de 100644 --- a/tools/platformio-build.py +++ b/tools/platformio-build.py @@ -170,6 +170,19 @@ def scons_patched_match_splitext(path, suffixes=None): ) ) +# helper functions to place a library at a specific position in the linking +# order, either referenced by "n positions before the end" or "n positions after the start". +def inject_lib_at_position_from_back(env, lib, position): + old_libs = env["LIBS"] + old_libs.insert(len(old_libs) - position, lib) + env["LIBS"] = old_libs + + +def inject_lib_at_position_from_front(env, lib, position): + old_libs = env["LIBS"] + old_libs.insert(position, lib) + env["LIBS"] = old_libs + # copy CCFLAGS to ASFLAGS (-x assembler-with-cpp mode) env.Append(ASFLAGS=env.get("CCFLAGS", [])[:]) @@ -222,39 +235,41 @@ def scons_patched_match_splitext(path, suffixes=None): env.Append( CPPDEFINES=[("TCP_MSS", 536), ("LWIP_FEATURES", 1), ("LWIP_IPV6", 1)], CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")], - LIBS=["lwip6-536-feat"] ) + inject_lib_at_position_from_front(env, "lwip6-536-feat", 4) elif "PIO_FRAMEWORK_ARDUINO_LWIP2_IPV6_HIGHER_BANDWIDTH" in flatten_cppdefines: env.Append( CPPDEFINES=[("TCP_MSS", 1460), ("LWIP_FEATURES", 1), ("LWIP_IPV6", 1)], CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")], - LIBS=["lwip6-1460-feat"] ) + inject_lib_at_position_from_front(env, "lwip6-1460-feat", 4) elif "PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH" in flatten_cppdefines: env.Append( CPPDEFINES=[("TCP_MSS", 1460), ("LWIP_FEATURES", 1), ("LWIP_IPV6", 0)], CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")], - LIBS=["lwip2-1460-feat"] ) + inject_lib_at_position_from_front(env, "lwip2-1460-feat", 4) elif "PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY_LOW_FLASH" in flatten_cppdefines: env.Append( CPPDEFINES=[("TCP_MSS", 536), ("LWIP_FEATURES", 0), ("LWIP_IPV6", 0)], CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")], LIBS=["lwip2-536"] ) + inject_lib_at_position_from_front(env, "lwip2-536", 4) elif "PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH_LOW_FLASH" in flatten_cppdefines: env.Append( CPPDEFINES=[("TCP_MSS", 1460), ("LWIP_FEATURES", 0), ("LWIP_IPV6", 0)], CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")], LIBS=["lwip2-1460"] ) + inject_lib_at_position_from_front(env, "lwip2-1460", 4) # PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY (default) else: env.Append( CPPDEFINES=[("TCP_MSS", 536), ("LWIP_FEATURES", 1), ("LWIP_IPV6", 0)], CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")], - LIBS=["lwip2-536-feat"] ) + inject_lib_at_position_from_front(env, "lwip2-536-feat", 4) # # Waveform @@ -269,14 +284,16 @@ def scons_patched_match_splitext(path, suffixes=None): if "PIO_FRAMEWORK_ARDUINO_ENABLE_EXCEPTIONS" in flatten_cppdefines: env.Append( CXXFLAGS=["-fexceptions"], - LIBS=["stdc++-exc"] ) + # we need to respect the original linking order of the libraries + # and cannot just append it to the end, but at the specific correct + # position, offset from the back. + inject_lib_at_position_from_back(env, "stdc++-exc", 3) else: env.Append( CXXFLAGS=["-fno-exceptions"], - LIBS=["stdc++"] ) - + inject_lib_at_position_from_back(env, "stdc++", 3) # # VTables # From aa0a7a655d040b1e8ecd92e974785e71f14abb03 Mon Sep 17 00:00:00 2001 From: Maximilian Gerhardt Date: Mon, 9 Aug 2021 14:19:13 +0200 Subject: [PATCH 2/3] Remove double-referenced libs --- tools/platformio-build.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tools/platformio-build.py b/tools/platformio-build.py index db873c77de..7ea774aae1 100644 --- a/tools/platformio-build.py +++ b/tools/platformio-build.py @@ -253,14 +253,12 @@ def inject_lib_at_position_from_front(env, lib, position): env.Append( CPPDEFINES=[("TCP_MSS", 536), ("LWIP_FEATURES", 0), ("LWIP_IPV6", 0)], CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")], - LIBS=["lwip2-536"] ) inject_lib_at_position_from_front(env, "lwip2-536", 4) elif "PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH_LOW_FLASH" in flatten_cppdefines: env.Append( CPPDEFINES=[("TCP_MSS", 1460), ("LWIP_FEATURES", 0), ("LWIP_IPV6", 0)], CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")], - LIBS=["lwip2-1460"] ) inject_lib_at_position_from_front(env, "lwip2-1460", 4) # PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY (default) From 2031243affb52eeab16df3bfb0cc259ef69206c4 Mon Sep 17 00:00:00 2001 From: Maximilian Gerhardt Date: Mon, 9 Aug 2021 14:38:16 +0200 Subject: [PATCH 3/3] Change implementation style Instead of injecting at magic indices, which might break when some other extra-scripts inject other libraries, let's create the LIBS array at the bottom in easy to understand and correct order. --- tools/platformio-build.py | 51 ++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/tools/platformio-build.py b/tools/platformio-build.py index 7ea774aae1..a462ff391a 100644 --- a/tools/platformio-build.py +++ b/tools/platformio-build.py @@ -139,12 +139,8 @@ def scons_patched_match_splitext(path, suffixes=None): join(FRAMEWORK_DIR, "tools", "sdk", "ld") ], - # A list of one or more libraries that will be linked with any executable programs created by this environment - LIBS=[ - "hal", "phy", "pp", "net80211", "wpa", "crypto", "main", - "wps", "bearssl", "espnow", "smartconfig", "airkiss", "wpa2", - "m", "c", "gcc" - ], + # LIBS is set at the bottom of the builder script + # where we know about all system libraries to be included LIBSOURCE_DIRS=[ join(FRAMEWORK_DIR, "libraries") @@ -170,19 +166,6 @@ def scons_patched_match_splitext(path, suffixes=None): ) ) -# helper functions to place a library at a specific position in the linking -# order, either referenced by "n positions before the end" or "n positions after the start". -def inject_lib_at_position_from_back(env, lib, position): - old_libs = env["LIBS"] - old_libs.insert(len(old_libs) - position, lib) - env["LIBS"] = old_libs - - -def inject_lib_at_position_from_front(env, lib, position): - old_libs = env["LIBS"] - old_libs.insert(position, lib) - env["LIBS"] = old_libs - # copy CCFLAGS to ASFLAGS (-x assembler-with-cpp mode) env.Append(ASFLAGS=env.get("CCFLAGS", [])[:]) @@ -231,43 +214,44 @@ def inject_lib_at_position_from_front(env, lib, position): # # lwIP # +lwip_lib = None if "PIO_FRAMEWORK_ARDUINO_LWIP2_IPV6_LOW_MEMORY" in flatten_cppdefines: env.Append( CPPDEFINES=[("TCP_MSS", 536), ("LWIP_FEATURES", 1), ("LWIP_IPV6", 1)], CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")], ) - inject_lib_at_position_from_front(env, "lwip6-536-feat", 4) + lwip_lib = "lwip6-536-feat" elif "PIO_FRAMEWORK_ARDUINO_LWIP2_IPV6_HIGHER_BANDWIDTH" in flatten_cppdefines: env.Append( CPPDEFINES=[("TCP_MSS", 1460), ("LWIP_FEATURES", 1), ("LWIP_IPV6", 1)], CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")], ) - inject_lib_at_position_from_front(env, "lwip6-1460-feat", 4) + lwip_lib = "lwip6-1460-feat" elif "PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH" in flatten_cppdefines: env.Append( CPPDEFINES=[("TCP_MSS", 1460), ("LWIP_FEATURES", 1), ("LWIP_IPV6", 0)], CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")], ) - inject_lib_at_position_from_front(env, "lwip2-1460-feat", 4) + lwip_lib = "lwip2-1460-feat" elif "PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY_LOW_FLASH" in flatten_cppdefines: env.Append( CPPDEFINES=[("TCP_MSS", 536), ("LWIP_FEATURES", 0), ("LWIP_IPV6", 0)], CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")], ) - inject_lib_at_position_from_front(env, "lwip2-536", 4) + lwip_lib = "lwip2-536" elif "PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH_LOW_FLASH" in flatten_cppdefines: env.Append( CPPDEFINES=[("TCP_MSS", 1460), ("LWIP_FEATURES", 0), ("LWIP_IPV6", 0)], CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")], ) - inject_lib_at_position_from_front(env, "lwip2-1460", 4) + lwip_lib = "lwip2-1460" # PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY (default) else: env.Append( CPPDEFINES=[("TCP_MSS", 536), ("LWIP_FEATURES", 1), ("LWIP_IPV6", 0)], CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")], ) - inject_lib_at_position_from_front(env, "lwip2-536-feat", 4) + lwip_lib = "lwip2-536-feat" # # Waveform @@ -279,19 +263,17 @@ def inject_lib_at_position_from_front(env, lib, position): # # Exceptions # +stdcpp_lib = None if "PIO_FRAMEWORK_ARDUINO_ENABLE_EXCEPTIONS" in flatten_cppdefines: env.Append( CXXFLAGS=["-fexceptions"], ) - # we need to respect the original linking order of the libraries - # and cannot just append it to the end, but at the specific correct - # position, offset from the back. - inject_lib_at_position_from_back(env, "stdc++-exc", 3) + stdcpp_lib = "stdc++-exc" else: env.Append( CXXFLAGS=["-fno-exceptions"], ) - inject_lib_at_position_from_back(env, "stdc++", 3) + stdcpp_lib = "stdc++" # # VTables # @@ -370,6 +352,15 @@ def inject_lib_at_position_from_front(env, lib, position): assert mmu_flags env.Append(CPPDEFINES=mmu_flags) +# A list of one or more libraries that will be linked with any executable programs created by this environment +# We do this at this point so that we can put the libraries in their correct order more easily +env.Append( + LIBS=[ + "hal", "phy", "pp", "net80211", lwip_lib, "wpa", "crypto", "main", + "wps", "bearssl", "espnow", "smartconfig", "airkiss", "wpa2", + stdcpp_lib, "m", "c", "gcc" + ] +) # Build the eagle.app.v6.common.ld linker file app_ld = env.Command(