Skip to content

esp32/Makefile: Add support for EIM environments. - #19657

Open
agatti wants to merge 2 commits into
micropython:masterfrom
agatti:esp32-eim
Open

agatti wants to merge 2 commits into
micropython:masterfrom
agatti:esp32-eim

Conversation

@agatti

@agatti agatti commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR lets the ESP32 port makefile work in environments set up via EIM (Espressif's SDK/toolchain manager) instead of the regular SDK export script.

EIM-activated environment do not place idf.py into the system's PATH, so the Makefile needs to know where to look for it if it cannot be found right away. However, EIM does set up an environment variable that points to the environment root, and therefore picking up idf.py from there is trivial. As a bonus, if the makefile cannot find idf.py, it will report a more sensible error to the user rather than a generic "file not found" message.

The documentation was also updated to mention EIM and the current state of things when using such a tool to manage your ESP-IDF SDKs.

This should close #19390.

Testing

An ESP32 build was initiated successfully with the environment set up by export.fish from a previously set up SDK using the regular git checkout + install.sh method, then from an environment activated via EIM, and finally with no environment set up to see the new error message.

Trade-offs and Alternatives

Currently there is a known issue with EIM preventing building mpy-cross from the same environment it sets up for cross-compilation builds (see espressif/idf-im-ui#1067). However, this PR contains a workaround for this situation.

Generative AI

I did not use generative AI tools when creating this PR.

@github-actions

github-actions Bot commented Aug 27, 2026

Copy link
Copy Markdown

Code size report:

Reference:  rp2: Keep machine.RTC ticking while in lightsleep(). [cc12057]
Comparison: mpy-cross/Makefile: Let mpy-cross build in EIM environments. [merge of b1f9119]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
      esp32:    +0 +0.000% ESP32_GENERIC
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

@agatti

agatti commented Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

The issue with locally-built tools under EIM may have been found, see espressif/idf-im-ui#1067 (comment)

It'd be great if somebody could try this out on macOS after editing the PATH variable to remove the directories mentioned in the comment linked above, as EIM also installs its own version of clang. So I can at least update the documentation mentioning whether EIM can be used to build MicroPython on macOS :)

@agatti
agatti force-pushed the esp32-eim branch 2 times, most recently from 9c3bf6d to d34951a Compare August 31, 2026 11:07
@agatti

agatti commented Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

I've been told EIM does take over a default LLVM installation to use its own, but that's a Xtensa + RV32 cross compiler only.

So, unless Espressif updates its clang version to also target x86_64 and AArch64, this method is not usable on macOS or on systems where there's no GCC installed (*BSDs, maybe?).

@agatti

agatti commented Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

Thinking a bit more about this, there may be a "cleaner" solution if Espressif can't (or won't) fix things on their end.

If $IDF_TOOLS_PATH is not set, congratulations, things will work as before. Otherwise:

  • Make a copy of $PATH
  • Strip away all entries that start with the value of $IDF_TOOLS_PATH
  • Export the stripped version of $PATH
  • Force a build of mpy-tool
  • Export the original $PATH
  • Build the firmware

then, in theory this should work for Linux, WSL, and macOS.

@agatti

agatti commented Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

This should make it work under Linux, both using GCC and LLVM to build mpy-cross from an EIM environment. I assume this should also work on WSL, but it is not tested on macOS.

No idea about building this from Windows outside WSL, but hey, maybe they broke that too...

@codecov

codecov Bot commented Aug 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.55%. Comparing base (cc12057) to head (b1f9119).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #19657   +/-   ##
=======================================
  Coverage   98.55%   98.55%           
=======================================
  Files         182      182           
  Lines       23335    23335           
  Branches        5        5           
=======================================
  Hits        22998    22998           
  Misses        336      336           
  Partials        1        1           
Flag Coverage Δ
unix-coverage-32bit 98.55% <ø> (ø)
unix-coverage-64bit 98.52% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@octoprobe-bot

octoprobe-bot commented Sep 8, 2026

Copy link
Copy Markdown

Octoprobe PR report

Test Tests
passed
Tests
skipped
Tests
xfailed
Tests
failed
format flash 4
run-tests.py 3853 351
run-tests.py --via-mpy --emit native 3812 392
run-tests.py --via-mpy 3855 349
run-perfbench.py 96
run-natmodtests.py 142 22
run-mpremote-tests.sh 36
run-tests.py --test-dirs=extmod_hardware 54 63 15
run-tests.py --test-dirs=extmod_hardware --emit-native 54 63 15
Total 11902 1244 30
Failures

@projectgus projectgus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @agatti, sorry it took me a while to review. This looks good! I have a bunch of suggestions but they're all based around comments and naming - mostly spelling out what EIM is the first time it's mentioned each time - I'd not heard of it until recently, and I assume most people MicroPython won't yet be familiar with it either.

Thanks for working through this support!

Also TIL about posix shell command -v. Had somehow got this far without ever noticing it. Very handy!

Comment thread mpy-cross/Makefile Outdated
Comment on lines +53 to +59
# Workaround for building with EIM. EIM takes over an existing LLVM
# installation (so macOS won't ever be able to compile anything) and adds
# directories to the PATH variable whose files collide with an existing GCC
# installation as they recursively add any directory named `bin` under the tools
# root of the currently chosen ESP-IDF environment. Of course nobody would ever
# build local binaries to do any pre or post build actions or anything else for
# an ESP32 project, right?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest giving a little more context and being really specific about what EIM behaviour we're working around here, because this Makefile isn't part of esp32 port so the reader may not have any idea what they're looking at.

Something like the following (but feel free to change):

Suggested change
# Workaround for building with EIM. EIM takes over an existing LLVM
# installation (so macOS won't ever be able to compile anything) and adds
# directories to the PATH variable whose files collide with an existing GCC
# installation as they recursively add any directory named `bin` under the tools
# root of the currently chosen ESP-IDF environment. Of course nobody would ever
# build local binaries to do any pre or post build actions or anything else for
# an ESP32 project, right?
# Workaround for building native mpy-cross when invoked inside
# Espressif Installation Manager (EIM) environment.
#
# EIM prepends an LLVM installation in front of the system PATH (so macOS can't
# compile native binaries) and adds directories to the PATH variable whose files
# collide with an existing GCC installation as they recursively add any directory
# named `bin` under the tools root of the currently chosen ESP-IDF environment.
# Of course nobody would ever build native binaries to do any pre or post build
# actions or anything else for an ESP32 project, right?

(Left your snarky rhetorical question in as I appreciated it! 😁 )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update!

Regarding the rhetorical question, I'm still wondering why they're doing this. It seems that at least one of the developers working on EIM is based in central Europe, if they show up at Espressif's devcon in Milan this November I'll try to ask them about this.

Comment thread mpy-cross/Makefile Outdated
ifneq ($(IDF_TOOLS_PATH),)
SPACE := $(subst ,, )
PATH_COPY := $(PATH)
PATH = $(subst $(SPACE),:,$(filter-out $(IDF_TOOLS_PATH)%,$(subst :,$(SPACE),$(PATH_COPY))))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
PATH = $(subst $(SPACE),:,$(filter-out $(IDF_TOOLS_PATH)%,$(subst :,$(SPACE),$(PATH_COPY))))
# Filter out any IDF_TOOLS_PATH directories from the PATH
PATH = $(subst $(SPACE),:,$(filter-out $(IDF_TOOLS_PATH)%,$(subst :,$(SPACE),$(PATH_COPY))))

(That's right, isn't it?)

Comment thread ports/esp32/Makefile Outdated

HELP_BUILD_ERROR ?= "See \033[1;31mhttps://github.com/micropython/micropython/wiki/Build-Troubleshooting\033[0m"

# Check if it's exported from EIM first

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Check if it's exported from EIM first
# Check if it's exported from Espressif Installation Manager (EIM) first

Comment thread ports/esp32/Makefile Outdated
Comment thread ports/esp32/README.md Outdated

To obtain the toolchain and set up your environment, the Espressif instructions
will guide you to either use the `install.sh` (or `install.bat` if you're on
Windows) script, or to use their toolchain and environment manager, EIM.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Windows) script, or to use their toolchain and environment manager, EIM.
Windows) script, or to use their Espressif Installation Manager (EIM).

Comment thread ports/esp32/README.md Outdated
Comment on lines +119 to +120
Installation via the EIM tool
-----------------------------

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Installation via the EIM tool
-----------------------------
Installation via Espressif Installation Manager (EIM) tool
-------------------------------------------------------------

This commit lets the ESP32 port makefile work in environments set up via
EIM (Espressif's SDK/toolchain manager) instead of the regular SDK
export script.

EIM-activated environment do not place `idf.py` into the system's PATH,
so the Makefile needs to know where to look for it if it cannot be found
right away.  However, EIM does set up an environment variable that
points to the environment root, and therefore picking up idf.py from
there is trivial.  As a bonus, if the makefile cannot find idf.py, it
will report a more sensible error to the user rather than a generic
"file not found" message.

The documentation was also updated to mention EIM and the current state
of things when using such a tool to manage your ESP-IDF SDKs.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
This commit updates the build infrastructure to undo the local compiler
takeover done by Espressif's EIM tool, allowing the ESP32 port to build
correctly from an EIM-activated environment.

EIM would blindly edit the PATH environment variable to replace any
local LLVM installation with its own bare-metal version, and add all
directories named `bin` from their tools directory.  Three of those
directories have files whose name clashes with local GCC installations.

As we may build `mpy-cross` for the host system as part of the build
process of the ESP32 port, that's unfortunately a workaround we have to
add ourselves.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
@agatti

agatti commented Sep 16, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review! I've updated the text where suggested and cleaned things up a bit.

The EIM installation section now also has some more information on what a working setup could look like, with what to select on certain EIM prompts.

ESP-IDF 5.3 and 5.4 set IDF_PATH as well, so the ESP32 Makefile was updated to check whether such a variable exists in the first place, and bail out if not as that indicates an environment that is probably not set up correctly. Following on that, there's also a check on whether IDF_PATH points to a valid path (as in, directory containing tools/idf.py, haven't gone further than that). There's more comments in the Makefile explaining that as well.

For mpy-cross, comments have been cleaned up, including mentioning a potential pitfall occurring if the compiler is located in a directory in the system PATH that has a space in the name. Extremely unlikely, but at least mentioned there for whoever will have to deal with that :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

make error with idf install from eim

3 participants