Skip to content

Remove environment name from build_cache_dir cache keys #4574

Open

Description

@LordMike

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

  1. Create two environments of identical board, platform, dependencies, code
  2. Set build_cache_dir to some directory
  3. Build the two environments
  4. 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 ============

Additional info

Activity

  1. ivankravets commented on Mar 17, 2023

    @ivankravets
    Member

    Are you sure that build environments (flags, etc) are the same for both projects?

    Please try to run both projects with pio run -v and compare VERBOSE output. The flags MUST be the same.

  2. LordMike commented on Mar 18, 2023

    @LordMike
    Author

    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.

  3. LordMike commented on Mar 18, 2023

    @LordMike
    Author

    I've not been able to figure out how to run pio directly, or how to run esphome myself. 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 trigger esphome through their dashboard. I did alter esphome though 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:

    ## light-41-1
    xtensa-lx106-elf-g++ -o /data/light-41-1/.pioenvs/light-41-1/src/main.cpp.o -c -fno-rtti -std=gnu++17 -fno-exceptions -Wno-nonnull-compare -Wno-sign-compare -Wno-unused-but-set-variable -Wno-unused-variable -fno-exceptions -Os -mlongcalls -mtext-section-literals -falign-functions=4 -U__STRICT_ANSI__ -D_GNU_SOURCE -ffunction-sections -fdata-sections -Wall -Werror=return-type -free -fipa-pta -DPLATFORMIO=60106 -DESP8266 -DARDUINO_ARCH_ESP8266 -DARDUINO_ESP8266_ESP01 -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 -DF_CPU=80000000L -D__ets__ -DICACHE_FLASH -DARDUINO=10805 -DARDUINO_BOARD=\"PLATFORMIO_ESP8285\" -DFLASHMODE_DOUT -DLWIP_OPEN_SRC -DNONOSDK22x_190703=1 -DTCP_MSS=1460 -DLWIP_FEATURES=0 -DLWIP_IPV6=0 -DVTABLES_IN_FLASH -DMMU_IRAM_SIZE=0x8000 -DMMU_ICACHE_SIZE=0x8000 -Isrc -I/data/light-41-1/.piolibdeps/light-41-1/ArduinoJson/src -I/data/light-41-1/.piolibdeps/light-41-1/noise-c/include -I/data/light-41-1/.piolibdeps/light-41-1/noise-c/src -I/data/light-41-1/.piolibdeps/light-41-1/libsodium/libsodium/src/libsodium/include -I/data/light-41-1/.piolibdeps/light-41-1/libsodium/libsodium/src/libsodium -I/data/light-41-1/.piolibdeps/light-41-1/libsodium/libsodium/src/libsodium/include/sodium -I/data/light-41-1/.piolibdeps/light-41-1/libsodium/port_include -I/data/cache/platformio/packages/framework-arduinoespressif8266/libraries/ESP8266mDNS/src -I/data/cache/platformio/packages/framework-arduinoespressif8266/libraries/DNSServer/src -I/data/light-41-1/.piolibdeps/light-41-1/ESPAsyncWebServer-esphome/src -I/data/cache/platformio/packages/framework-arduinoespressif8266/libraries/ESP8266WiFi/src -I/data/cache/platformio/packages/framework-arduinoespressif8266/libraries/Hash/src -I/data/light-41-1/.piolibdeps/light-41-1/ESPAsyncTCP-esphome/src -I/data/cache/platformio/packages/framework-arduinoespressif8266/tools/sdk/include -I/data/cache/platformio/packages/framework-arduinoespressif8266/cores/esp8266 -I/data/cache/platformio/packages/toolchain-xtensa/include -I/data/cache/platformio/packages/framework-arduinoespressif8266/tools/sdk/lwip2/include -I/data/cache/platformio/packages/framework-arduinoespressif8266/variants/generic src/main.cpp
    
    ## light-41-2
    xtensa-lx106-elf-g++ -o /data/light-41-2/.pioenvs/light-41-2/src/main.cpp.o -c -fno-rtti -std=gnu++17 -fno-exceptions -Wno-nonnull-compare -Wno-sign-compare -Wno-unused-but-set-variable -Wno-unused-variable -fno-exceptions -Os -mlongcalls -mtext-section-literals -falign-functions=4 -U__STRICT_ANSI__ -D_GNU_SOURCE -ffunction-sections -fdata-sections -Wall -Werror=return-type -free -fipa-pta -DPLATFORMIO=60106 -DESP8266 -DARDUINO_ARCH_ESP8266 -DARDUINO_ESP8266_ESP01 -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 -DF_CPU=80000000L -D__ets__ -DICACHE_FLASH -DARDUINO=10805 -DARDUINO_BOARD=\"PLATFORMIO_ESP8285\" -DFLASHMODE_DOUT -DLWIP_OPEN_SRC -DNONOSDK22x_190703=1 -DTCP_MSS=1460 -DLWIP_FEATURES=0 -DLWIP_IPV6=0 -DVTABLES_IN_FLASH -DMMU_IRAM_SIZE=0x8000 -DMMU_ICACHE_SIZE=0x8000 -Isrc -I/data/light-41-2/.piolibdeps/light-41-2/ArduinoJson/src -I/data/light-41-2/.piolibdeps/light-41-2/noise-c/include -I/data/light-41-2/.piolibdeps/light-41-2/noise-c/src -I/data/light-41-2/.piolibdeps/light-41-2/libsodium/libsodium/src/libsodium/include -I/data/light-41-2/.piolibdeps/light-41-2/libsodium/libsodium/src/libsodium -I/data/light-41-2/.piolibdeps/light-41-2/libsodium/libsodium/src/libsodium/include/sodium -I/data/light-41-2/.piolibdeps/light-41-2/libsodium/port_include -I/data/cache/platformio/packages/framework-arduinoespressif8266/libraries/ESP8266mDNS/src -I/data/cache/platformio/packages/framework-arduinoespressif8266/libraries/DNSServer/src -I/data/light-41-2/.piolibdeps/light-41-2/ESPAsyncWebServer-esphome/src -I/data/cache/platformio/packages/framework-arduinoespressif8266/libraries/ESP8266WiFi/src -I/data/cache/platformio/packages/framework-arduinoespressif8266/libraries/Hash/src -I/data/light-41-2/.piolibdeps/light-41-2/ESPAsyncTCP-esphome/src -I/data/cache/platformio/packages/framework-arduinoespressif8266/tools/sdk/include -I/data/cache/platformio/packages/framework-arduinoespressif8266/cores/esp8266 -I/data/cache/platformio/packages/toolchain-xtensa/include -I/data/cache/platformio/packages/framework-arduinoespressif8266/tools/sdk/lwip2/include -I/data/cache/platformio/packages/framework-arduinoespressif8266/variants/generic src/main.cpp
    

    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:

    ## light-41-1
    xtensa-lx106-elf-g++
      -c
      -fno-rtti
      -std=gnu++17
      -fno-exceptions
      -Wno-nonnull-compare
      -Wno-sign-compare
      -Wno-unused-but-set-variable
      -Wno-unused-variable
      -fno-exceptions
      -Os
      -mlongcalls
      -mtext-section-literals
      -falign-functions=4
      -U__STRICT_ANSI__
      -D_GNU_SOURCE
      -ffunction-sections
      -fdata-sections
      -Wall
      -Werror=return-type
      -free
      -fipa-pta
      -DPLATFORMIO=60106
      -DESP8266
      -DARDUINO_ARCH_ESP8266
      -DARDUINO_ESP8266_ESP01
      -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
      -DF_CPU=80000000L
      -D__ets__
      -DICACHE_FLASH
      -DARDUINO=10805
      -DARDUINO_BOARD=\"PLATFORMIO_ESP8285\"
      -DFLASHMODE_DOUT
      -DLWIP_OPEN_SRC
      -DNONOSDK22x_190703=1
      -DTCP_MSS=1460
      -DLWIP_FEATURES=0
      -DLWIP_IPV6=0
      -DVTABLES_IN_FLASH
      -DMMU_IRAM_SIZE=0x8000
      -DMMU_ICACHE_SIZE=0x8000
    
    ## light-41-2
    xtensa-lx106-elf-g++
      -c
      -fno-rtti
      -std=gnu++17
      -fno-exceptions
      -Wno-nonnull-compare
      -Wno-sign-compare
      -Wno-unused-but-set-variable
      -Wno-unused-variable
      -fno-exceptions
      -Os
      -mlongcalls
      -mtext-section-literals
      -falign-functions=4
      -U__STRICT_ANSI__
      -D_GNU_SOURCE
      -ffunction-sections
      -fdata-sections
      -Wall
      -Werror=return-type
      -free
      -fipa-pta
      -DPLATFORMIO=60106
      -DESP8266
      -DARDUINO_ARCH_ESP8266
      -DARDUINO_ESP8266_ESP01
      -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
      -DF_CPU=80000000L
      -D__ets__
      -DICACHE_FLASH
      -DARDUINO=10805
      -DARDUINO_BOARD=\"PLATFORMIO_ESP8285\"
      -DFLASHMODE_DOUT
      -DLWIP_OPEN_SRC
      -DNONOSDK22x_190703=1
      -DTCP_MSS=1460
      -DLWIP_FEATURES=0
      -DLWIP_IPV6=0
      -DVTABLES_IN_FLASH
      -DMMU_IRAM_SIZE=0x8000
      -DMMU_ICACHE_SIZE=0x8000
    

    Other files are compiled with similar (long) commandlines. Example for src/esphome/components/api/api_pb2_service.cpp:

    xtensa-lx106-elf-g++ -o /data/light-41-1/.pioenvs/light-41-1/src/esphome/components/api/api_pb2_service.cpp.o -c -fno-rtti -std=gnu++17 -fno-exceptions -Wno-nonnull-compare -Wno-sign-compare -Wno-unused-but-set-variable -Wno-unused-variable -fno-exceptions -Os -mlongcalls -mtext-section-literals -falign-functions=4 -U__STRICT_ANSI__ -D_GNU_SOURCE -ffunction-sections -fdata-sections -Wall -Werror=return-type -free -fipa-pta -DPLATFORMIO=60106 -DESP8266 -DARDUINO_ARCH_ESP8266 -DARDUINO_ESP8266_ESP01 -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 -DF_CPU=80000000L -D__ets__ -DICACHE_FLASH -DARDUINO=10805 -DARDUINO_BOARD=\"PLATFORMIO_ESP8285\" -DFLASHMODE_DOUT -DLWIP_OPEN_SRC -DNONOSDK22x_190703=1 -DTCP_MSS=1460 -DLWIP_FEATURES=0 -DLWIP_IPV6=0 -DVTABLES_IN_FLASH -DMMU_IRAM_SIZE=0x8000 -DMMU_ICACHE_SIZE=0x8000 -Isrc -I/data/light-41-1/.piolibdeps/light-41-1/ArduinoJson/src -I/data/light-41-1/.piolibdeps/light-41-1/noise-c/include -I/data/light-41-1/.piolibdeps/light-41-1/noise-c/src -I/data/light-41-1/.piolibdeps/light-41-1/libsodium/libsodium/src/libsodium/include -I/data/light-41-1/.piolibdeps/light-41-1/libsodium/libsodium/src/libsodium -I/data/light-41-1/.piolibdeps/light-41-1/libsodium/libsodium/src/libsodium/include/sodium -I/data/light-41-1/.piolibdeps/light-41-1/libsodium/port_include -I/data/cache/platformio/packages/framework-arduinoespressif8266/libraries/ESP8266mDNS/src -I/data/cache/platformio/packages/framework-arduinoespressif8266/libraries/DNSServer/src -I/data/light-41-1/.piolibdeps/light-41-1/ESPAsyncWebServer-esphome/src -I/data/cache/platformio/packages/framework-arduinoespressif8266/libraries/ESP8266WiFi/src -I/data/cache/platformio/packages/framework-arduinoespressif8266/libraries/Hash/src -I/data/light-41-1/.piolibdeps/light-41-1/ESPAsyncTCP-esphome/src -I/data/cache/platformio/packages/framework-arduinoespressif8266/tools/sdk/include -I/data/cache/platformio/packages/framework-arduinoespressif8266/cores/esp8266 -I/data/cache/platformio/packages/toolchain-xtensa/include -I/data/cache/platformio/packages/framework-arduinoespressif8266/tools/sdk/lwip2/include -I/data/cache/platformio/packages/framework-arduinoespressif8266/variants/generic src/esphome/components/api/api_pb2_service.cpp
    
  4. LordMike commented on Mar 18, 2023

    @LordMike
    Author

    I've tried modifying what I think is the code that does the hashing, to see what the contents of the config are.

    def compute_project_checksum(config):
    # rebuild when PIO Core version changes
    checksum = sha1(hashlib_encode_data(__version__))
    # configuration file state
    checksum.update(hashlib_encode_data(config.to_json()))
    # project file structure
    check_suffixes = (".c", ".cc", ".cpp", ".h", ".hpp", ".s", ".S")

    I've put them below. I see multiple path-specific items, not just the env:light-41-1. Apparently the build_dir and libdeps_dir are 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 esphome has 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 in esphome .. 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"
            ]
          ]
        ]
      ]
    ]
  5. LordMike commented on Mar 18, 2023

    @LordMike
    Author

    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.

    Retrieved `/data/light-41-1/.pioenvs/light-41-1/src/esphome/components/api/api_connection.cpp.o' from cache
    Retrieved `/data/light-41-1/.pioenvs/light-41-1/src/esphome/components/api/api_frame_helper.cpp.o' from cache
    Retrieved `/data/light-41-1/.pioenvs/light-41-1/src/esphome/components/api/api_pb2.cpp.o' from cache
    Retrieved `/data/light-41-1/.pioenvs/light-41-1/src/esphome/components/api/api_pb2_service.cpp.o' from cache
    ...
    

    I followed the log message above to SCons:

    And 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. ... :'(

  6. ivankravets commented on Mar 18, 2023

    @ivankravets
    Member

    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.

  7. mcspr commented on Mar 20, 2023

    @mcspr
    Contributor

    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.

  8. ivankravets commented on Mar 20, 2023

    @ivankravets
    Member

    @mcspr , could you reproduce this issue with the bare PlatformIO projects?

  9. mcspr commented on Mar 20, 2023

    @mcspr
    Contributor

    @ivankravets https://github.com/mcspr/pio4574

    So, there are two identical envs - 'a' and 'b'. Doing some clean-up and building 'a' first

    > rm -rf .build_cache/
    > pio run -s -t clean
    > pio run -e a | grep 'from cache' | grep libFrameworkArduino.a
    > pio run -s -t clean
    > pio run -e a | grep 'from cache' | grep libFrameworkArduino.a
    Retrieved `.pio/build/a/libFrameworkArduino.a' from cache

    Cache picked up our framework file, everything good. Building 'b' next

    > pio run -e b | grep 'from cache' | grep libFrameworkArduino.a

    Can'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

    > pio run -s -e b -t clean
    > pio run -e b | grep 'from cache' | grep libFrameworkArduino.a
    Retrieved `.pio/build/b/libFrameworkArduino.a' from cache
  10. 25 remaining items

  11. ivankravets commented on Mar 28, 2023

    @ivankravets
    Member

    As I understand it you'd like for two identical build commands excluding different target locations to be able to use the same cached built file. Is that correct?

    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 VariantDir but COMMON CacheDir.

    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 on VariantDir which should have LESS priority.

  12. ivankravets commented on Mar 28, 2023

    @ivankravets
    Member

    Please file an enhancement request in SCons?

    @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.

  13. mwichmann commented on Mar 28, 2023

    @mwichmann

    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.

  14. bdbaddog commented on Mar 28, 2023

    @bdbaddog

    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..

  15. ivankravets commented on Mar 28, 2023

    @ivankravets
    Member

    @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 + CacheDir together. Now, it is not clear to me what CacheDir contains. 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?

  16. mwichmann commented on Mar 28, 2023

    @mwichmann

    the actual objects are copied to the cache as they're built, and copied back when needed.

  17. bdbaddog commented on Mar 30, 2023

    @bdbaddog

    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 $TARGET was 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?

  18. mcspr commented on Mar 31, 2023

    @mcspr
    Contributor

    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)

  19. bdbaddog commented on Mar 31, 2023

    @bdbaddog

    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.

  20. mcspr commented on Mar 31, 2023

    @mcspr
    Contributor

    So likely this should be a per builder (and/or per target node) option, defaulting to including the target path.

    If I understood the idea correctly...
    SCons/scons@master...mcspr:scons:pio4574v2

    def factory func or scanner somewhere on PIO side?(node, *arg, **kwarg):
        x = env.File(node)
        x.set_cachedir_bsig_path('')
        return x

    Please file an enhancement request issue in SCons' github repo.

    Will do

  21. expaso commented on Apr 16, 2023

    @expaso

    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?

  22. LordMike commented on Apr 18, 2023

    @LordMike
    Author

    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.

  23. mwichmann commented on Apr 18, 2023

    @mwichmann

    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.

  24. expaso commented on Apr 22, 2023

    @expaso

    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.

  25. added a commit that references this issue on May 28, 2024
    4b446b0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions