Skip to content

Commit c7863f4

Browse files
Keep SPIFFS/LittleFS code from link when unneeded
Fixes #6691 Idea is to use same principal of weak references to allow the call to SPIFFS.end() only to ever be linked in (along with all of SPIFFS) if there is a call somewhere in the app to SPIFFS.begin(). The biggest use case is httpUpdateServer which includes both LittleFS and SPIFFS code because it has embedded SPIFFS/LittleFS.end() calls. Standard apps need no changes and continue using standard calls. Users of the HttpUpdateServer will automatically save an extra 10-40K of code if they do not use one or both filesystems, or will be unaffected if they use both.
1 parent 14262af commit c7863f4

File tree

9 files changed

+83
-4
lines changed

9 files changed

+83
-4
lines changed

cores/esp8266/FS.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ class FS
245245

246246
} // namespace fs
247247

248+
extern void spiffs_weak_end();
249+
248250
#ifndef FS_NO_GLOBALS
249251
using fs::FS;
250252
using fs::File;

cores/esp8266/spiffs_api.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst) {
3838
}
3939

4040

41-
42-
4341
namespace spiffs_impl {
4442

43+
4544
FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode accessMode)
4645
{
4746
if (!isSpiffsFilenameValid(path)) {

cores/esp8266/spiffs_api.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ extern int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst) __att
5858

5959

6060

61+
extern void enable_real_spiffs_weak_end();
6162

6263
namespace spiffs_impl {
6364

@@ -171,6 +172,8 @@ class SPIFFSImpl : public FSImpl
171172

172173
bool begin() override
173174
{
175+
enable_real_spiffs_weak_end(); // Ensure spiffs_weak_end actually calls SPIFFS.end();
176+
174177
if (SPIFFS_mounted(&_fs) != 0) {
175178
return true;
176179
}

cores/esp8266/spiffs_weak.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Called from SPIFFS.begin() to avoid linking in httpUpdateServer FS when not needed
3+
*/
4+
5+
#include <FS.h>
6+
7+
void enable_real_spiffs_weak_end()
8+
{
9+
/*
10+
* does nothing
11+
* allows overriding below
12+
*/
13+
}
14+
15+
/* the following code is linked only if a call to the above function is made somewhere */
16+
17+
extern void spiffs_weak_end_redefinable()
18+
{
19+
SPIFFS.end();
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
extern void spiffs_weak_end_redefinable() __attribute__((weak));
2+
extern void spiffs_weak_end_redefinable(void)
3+
{
4+
/* noop unless oveerridden by SPIFFS.begin() */
5+
}
6+
7+
static void spiffs_weak_end_custom (void) __attribute__((weakref("spiffs_weak_end_redefinable")));
8+
9+
extern void spiffs_weak_end (void)
10+
{
11+
spiffs_weak_end_custom();
12+
}
13+
14+
15+

libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ void ESP8266HTTPUpdateServerTemplate<ServerType>::setup(ESP8266WebServerTemplate
9494
Serial.printf("Update: %s\n", upload.filename.c_str());
9595
if (upload.name == "filesystem") {
9696
size_t fsSize = ((size_t) &_FS_end - (size_t) &_FS_start);
97-
SPIFFS.end();
98-
LittleFS.end();
97+
spiffs_weak_end();
98+
littlefs_weak_end();
9999
if (!Update.begin(fsSize, U_FS)){//start with max available size
100100
if (_serial_output) Update.printError(Serial);
101101
}

libraries/LittleFS/src/LittleFS.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939

4040
using namespace fs;
4141

42+
extern void enable_real_littlefs_weak_end();
43+
extern void littlefs_weak_end();
44+
4245
namespace littlefs_impl {
4346

4447
class LittleFSFileImpl;
@@ -181,6 +184,8 @@ class LittleFSImpl : public FSImpl
181184
}
182185

183186
bool begin() override {
187+
enable_real_littlefs_weak_end(); // Ensure littlefs_weak_end actually calls LittleFS.end();
188+
184189
if (_size <= 0) {
185190
DEBUGV("LittleFS size is <= zero");
186191
return false;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Called from LittleFS.begin() to avoid linking in httpUpdateServer FS when not needed
3+
*/
4+
5+
#include <LittleFS.h>
6+
7+
void enable_real_littlefs_weak_end()
8+
{
9+
/*
10+
* does nothing
11+
* allows overriding below
12+
*/
13+
}
14+
15+
/* the following code is linked only if a call to the above function is made somewhere */
16+
17+
extern void littlefs_weak_end_redefinable()
18+
{
19+
LittleFS.end();
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
extern void littlefs_weak_end_redefinable() __attribute__((weak));
2+
extern void littlefs_weak_end_redefinable(void)
3+
{
4+
/* noop unless oveerridden by SPIFFS.begin() */
5+
}
6+
7+
static void littlefs_weak_end_custom (void) __attribute__((weakref("littlefs_weak_end_redefinable")));
8+
9+
extern void littlefs_weak_end (void)
10+
{
11+
littlefs_weak_end_custom();
12+
}
13+
14+
15+

0 commit comments

Comments
 (0)