Repository navigation
Description
What kind of issue is this?
- Feature Request.
Start by telling us what problem you’re trying to solve. Often a solution
already exists! Don’t send pull requests to implement new features without first getting our
support. Sometimes we leave features out on purpose to keep the project small.
Configuration
Operating system: docker/linux, host is Debian 10, image is ghcr.io/esphome/esphome-hassio:2023.3.0
PlatformIO Version (platformio --version): PlatformIO Core, version 6.1.6
Description of problem
I'm using esphome to manage tens of mostly identical devices with identical sets of source code sans a few items such as their individual name and some encryption keys. When I use esphome to compile the firmwares for these devices, they create a separate environment in platformio for each device, which is good for isolation. It does take a long time to compile the mostly identical source code though, for all these devices, when updates are available. I'd like to alleviate that.
In another issue, I've been digging in to what can be done, and I've experimented with the build_cache_dir setting. It's not currently used by esphome. This setting works wonders for a single device: if I recompile after cleaning the output, the build is instantaneous (as can be), as it uses the cache.
The cached output from one device is not used in another, and I've found this topic (https://community.platformio.org/t/build-cache-dir-will-not-share-object-files-between-envs/10011/8) on your community forums that provides a crucial hint. It seems the env:name is part of the cache key, and as each device in esphome is a separate environment, I have my issue.
I hope this issue can lead to the env:name being removed from the cache key, to allow the build cache to be reused across environments. It's my impression that the rest of the build commandline includes all the details that are in environments, such as boards, platforms and libraries.
Steps to Reproduce
- Create two environments of identical board, platform, dependencies, code
- Set
build_cache_dirto some directory - Build the two environments
- Observe that the identical code is compiled twice
Actual Results
The cache is not reused between identical builds, if they have two environments.
Expected Results
Environment name should not affect the cache key. It's my impression that the rest of the build commandline (which is part of the key) is enough to distinguish one output from another.
If problems with PlatformIO Build System:
The content of platformio.ini:
This file is modified by me, locally, to include the cache_build_dir. It is identical to other devices I have, except for the environment name.
; Auto generated code by esphome
[common]
lib_deps =
build_flags =
upload_flags =
; ========== AUTO GENERATED CODE BEGIN ===========
[platformio]
description = ESPHome 2023.3.0
build_cache_dir = /esphome_cache/
[env:light-extra01]
board = esp8285
board_build.flash_mode = dout
board_build.ldscript = eagle.flash.1m.ld
build_flags =
-DESPHOME_LOG_LEVEL=ESPHOME_LOG_LEVEL_INFO
-DNEW_OOM_ABORT
-DPIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH_LOW_FLASH
-DUSE_ARDUINO
-DUSE_ESP8266
-DUSE_ESP8266_FRAMEWORK_ARDUINO
-DUSE_STORE_LOG_STR_IN_FLASH
-Wno-nonnull-compare
-Wno-sign-compare
-Wno-unused-but-set-variable
-Wno-unused-variable
-fno-exceptions
extra_scripts =
post:post_build.py
framework = arduino
lib_deps =
ottowinter/ESPAsyncTCP-esphome@1.2.3
esphome/ESPAsyncWebServer-esphome@2.1.0
DNSServer
ESP8266WiFi
ESP8266mDNS
esphome/noise-c@0.1.4
bblanchon/ArduinoJson@6.18.5
${common.lib_deps}
lib_ldf_mode = off
platform = platformio/espressif8266 @ 3.2.0
platform_packages =
platformio/framework-arduinoespressif8266 @ ~3.30002.0
; =========== AUTO GENERATED CODE END ============
Activity
ivankravets commentedon Mar 17, 2023
Are you sure that build environments (flags, etc) are the same for both projects?
Please try to run both projects with
pio run -vand compare VERBOSE output. The flags MUST be the same.LordMike commentedon Mar 18, 2023
I’ll try to see if I can get a run output for you. Hopefully today.
I can confirm though, that if I remove my build output for a specific device, and run the build again in esphome for the same device, most (all iirc) of the output - even the final “firmware.elf” comes from the cache.
So my assumption is that all things are identical - except for the environment name.
LordMike commentedon Mar 18, 2023
I've not been able to figure out how to run
piodirectly, or how to runesphomemyself. I think there may be a working directory or something I'm not aware of, because what I get does not match what I see in the logs when I triggeresphomethrough their dashboard. I did alteresphomethough to enable verbosity, which gave me a lot more log output when building:I've captured what I think is the relevant commandline from when I trigger a rebuild of two of my identical devices, and they're shown here:
If I remove all the inputs (
-I), the output (-o) and the source file itself (src/main.cpp), and split out each arg on a newline, I have the following. Winmerge tells me the two sets are identical:Other files are compiled with similar (long) commandlines. Example for
src/esphome/components/api/api_pb2_service.cpp:LordMike commentedon Mar 18, 2023
I've tried modifying what I think is the code that does the hashing, to see what the contents of the
configare.platformio-core/platformio/project/helpers.py
Lines 106 to 114 in 065607b
I've put them below. I see multiple path-specific items, not just the
env:light-41-1. Apparently thebuild_dirandlibdeps_dirare also relevant. I'm not sure why, as surely the build flags and the contents of the source code (headers and code) are enough to determine if the output will be identical. The actual location of the files must be irrelevant..Something weird is going on though, because
esphomehas chosen to "hack" out this bit of platformio: https://github.com/esphome/esphome/blob/b184b01600c9cb20f8d130dbac270809ac706c24/esphome/platformio_api.py#L18-L41. My code printing this config didn't actually run until I commented out that bit inesphome.. which then means that this isn't actually even running in my situation. It feels like this function is a red herring.[ [ "common", [ [ "lib_deps", "" ], [ "build_flags", "" ], [ "upload_flags", "" ] ] ], [ "platformio", [ [ "description", "ESPHome 2023.3.0" ], [ "globallib_dir", "/piolibs" ], [ "platforms_dir", "/data/cache/platformio/platforms" ], [ "packages_dir", "/data/cache/platformio/packages" ], [ "cache_dir", "/data/cache/platformio/cache" ], [ "build_dir", "/data/light-41-1/.pioenvs" ], [ "libdeps_dir", "/data/light-41-1/.piolibdeps" ] ] ], [ "env:light-41-1", [ [ "board", "esp8285" ], [ "board_build.flash_mode", "dout" ], [ "board_build.ldscript", "eagle.flash.1m.ld" ], [ "build_flags", [ "-DESPHOME_LOG_LEVEL=ESPHOME_LOG_LEVEL_INFO", "-DNEW_OOM_ABORT", "-DPIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH_LOW_FLASH", "-DUSE_ARDUINO", "-DUSE_ESP8266", "-DUSE_ESP8266_FRAMEWORK_ARDUINO", "-DUSE_STORE_LOG_STR_IN_FLASH", "-Wno-nonnull-compare", "-Wno-sign-compare", "-Wno-unused-but-set-variable", "-Wno-unused-variable", "-fno-exceptions" ] ], [ "extra_scripts", [ "post:post_build.py" ] ], [ "framework", [ "arduino" ] ], [ "lib_deps", [ "ottowinter/ESPAsyncTCP-esphome@1.2.3", "esphome/ESPAsyncWebServer-esphome@2.1.0", "DNSServer", "ESP8266WiFi", "ESP8266mDNS", "esphome/noise-c@0.1.4", "bblanchon/ArduinoJson@6.18.5" ] ], [ "lib_ldf_mode", "off" ], [ "platform", "platformio/espressif8266 @ 3.2.0" ], [ "platform_packages", [ "platformio/framework-arduinoespressif8266 @ ~3.30002.0" ] ] ] ] ]LordMike commentedon Mar 18, 2023
So the hashing code above is obviously a red herring, since it doesn't actually run in my case, but if I set
build_cache_dir, and recompile a single device twice (clearing output in between), it uses the cache for the files it already compiled.I followed the log message above to SCons:
cachepath.. https://github.com/SCons/scons/blob/f9b27906ef5a1540774210e6adf7c954cf7d81b6/SCons/CacheDir.py#L268get_cachedir_bsighttps://github.com/SCons/scons/blob/79dcabc57575bc9001df32b31635ca8e4bf0fbf8/SCons/Node/FS.py#L3688-L3717And here I fear that
sigs.append(self.get_internal_path())might actually be the actual file path on disk. This would mean that you could never reuse a build output between two identical source files.. The first point hints that.get_internal_path()is actually the full path, as seen in the log output I have. ... :'(ivankravets commentedon Mar 18, 2023
Sorry, we can't help you so much with the 3rd party software. If you still experience any issues with PlatformIO Core, please create clean, simple, and independent PlatformIO-based project to reproduce this issue.
mcspr commentedon Mar 20, 2023
I believe the gist of the issue is still the same as my previous ones - how do we build projects with multiple environments fast. ESPHome, ESPurna, etc. build process are already as minimal as we can get the example?
Libraries are built separately, framework files are built separately; all while build flags stay the exact same, it becomes not very effective way to spend CPU resources to rebuild everything.
ivankravets commentedon Mar 20, 2023
@mcspr , could you reproduce this issue with the bare PlatformIO projects?
mcspr commentedon Mar 20, 2023
@ivankravets https://github.com/mcspr/pio4574
So, there are two identical envs - 'a' and 'b'. Doing some clean-up and building 'a' first
Cache picked up our framework file, everything good. Building 'b' next
> pio run -e b | grep 'from cache' | grep libFrameworkArduino.aCan't reuse already built file from 'a' per the description above, even when they are identical. 2nd run of 'b' works with the cache related to 'b' only
25 remaining items
ivankravets commentedon Mar 28, 2023
Yes, correct. Let me give you a few real examples. C/C++ developers use 3-rd party libraries/frameworks in the projects. These libraries/frameworks are common per project. Following the example above, each project will have its own
VariantDirbut COMMONCacheDir.Now, when you build a few projects on the same host machine (or CI) developers expect that common libraries/frameworks will not be recompiled for each project. This is actually the context of this issue #4574.
If we use common
CacheDir, we expect that SCons will pull pre-built artifacts from the cache storage instead of relying onVariantDirwhich should have LESS priority.ivankravets commentedon Mar 28, 2023
@mcspr, could I ask you to file a feature request at https://github.com/SCons/scons/issues and point to this issue? You already reviewed the SCons sources and have a better understanding of this problem.
mwichmann commentedon Mar 28, 2023
In this example, you can place the built tree (say of boost - I know that's a common one) in what SCons calls a "repository", then various builds in SCons will populate from that as long as they specify that they use a repository. It's kind of like a cache at the opposite end, if that makes sense.
bdbaddog commentedon Mar 28, 2023
This is unlikely to be a general change, but rather one specified per builder, or enabled per user, because as I've said above some tools embed the target file/location/other related info in the generated file.
But in many cases (including yours) it makes sense to enable at least for some builders..
ivankravets commentedon Mar 28, 2023
@mwichmann, thanks for the hint with
env.Repository(). I've just tried it and it didn't work for us. As I understand, the goal of "Repository" is to keep artifacts/objects directly in the "Repository" folder. Only in this case, the caching might work.The issue with we faced is to use
VariantDir+CacheDirtogether. Now, it is not clear to me whatCacheDircontains. Does it contain only the references to the objects which are stored in the VariantDir or Repostiry, or it contains the complete artifacts/objects of the compiled source files?mwichmann commentedon Mar 28, 2023
the actual objects are copied to the cache as they're built, and copied back when needed.
bdbaddog commentedon Mar 30, 2023
Had a thought. Not sure if it would resolve this, but can one of you give it a try?
So the issue ( I think) is that the hash of the command line is different and that contributes to the build hash used to name the file in the cache.
So if the
-o $TARGETwas changed to$( -o $TARGET $)where$(and$)exclude items on the command line from that action's signature, it's possible many builds from the same source with the same flags but different output file names/locations could map to the same cachedir file.Does that make sense?
mcspr commentedon Mar 31, 2023
Right. But, looking at the code path cachedir sig takes, children and Nodes own contents sig match up until it appends path of the node that differs.
https://github.com/SCons/scons/blob/c80cbb0846c5d9265f356e86c4e79f1d13e3e8a8/SCons/Node/FS.py#L3693-L3697
https://github.com/SCons/scons/blob/c80cbb0846c5d9265f356e86c4e79f1d13e3e8a8/SCons/Node/FS.py#L3713
Only using the Nodes 'children + own' signature instead of 'children + own + path' would definitely fix the issue, 'own' signature is enough to distinguish targets
(where patch above was trying to preserve existing behaviour and optionally normalize node path based on some known variable, pretending that we were Chdir()ed into build directory)
bdbaddog commentedon Mar 31, 2023
You are correct, ignoring the target from the build signature won't resolve it.
The problem is ignoring the target file and it's path is not a good change for all builders.
For your usage (any many) this may be acceptable for compile and link builders.
So likely this should be a per builder (and/or per target node) option, defaulting to including the target path.
Please file an enhancement request issue in SCons' github repo.
mcspr commentedon Mar 31, 2023
If I understood the idea correctly...
SCons/scons@master...mcspr:scons:pio4574v2
Will do
expaso commentedon Apr 16, 2023
I was wondering about this behaviour for a few years, and by coincidence I've decided to digg this out myself today.
And voila! This discussion here couldn't be more actual :)
On topic: ESPHome auto-generates the source directories by itself. What would happen if we would make it generate symlinks for all the library code and the components? Wouldnt these sourcefiles then be seen as identical paths on disk, thus hitting the cache?
LordMike commentedon Apr 18, 2023
Sounds like an approach like https://pnpm.io/ is using. NPM has the same issue, just way worse: If APP dependes on A and B, and A also depends on B, npm creates a folder structure that includes B twice. This does not scale at all. Pnpm fixes this with a hierarchy of symlinks.
To make that work here, the links path needs to be resolved - I didn't spot any "readlink"-like approach when I was looking through code - but I might've missed it.
mwichmann commentedon Apr 18, 2023
It's not an excuse, just an explanation: because of the Windows symlink situation (previously needed elevated privilege, now needing "developer mode" to be set, neither of which SCons itself can control), SCons has stayed away from depending heavily on symlinks. There are cases, like variant dirs, where symlinking is done and there's a policy difference between platforms on whether to attempt, but that's relatively rare.
expaso commentedon Apr 22, 2023
I think it's generally a good idea to stay away from relying on symlinks, thats for sure.
But in this case, if the source-file happens to be a symlink (because that's simply the was the source-folders were organized), maybe SCons could follow the symlink and store the real location of the file as part of the cache-key instead.
What other options do we have left?
2 separate files, on separate physical locations, splitted by 2 environments. This can only be resolved by hashing the file contents and use the hash as a key.
Fixed an issue when "build_cache_dir" was not honored across differen…