Using ESP-IFD v4.4/v4.3.2 examples with PlatformIO Core, version 5.2.5?

After a first successful simple blink example using PlatformIO with VSCode and Ubuntu 21.10 on a ESP32 devkit V1, I tried to copy esp-idf/examples/protocols/http_server/restful_server for use within PlatformIO. The example perfectly builds using idf.py build. The first problem is that ESP-IDF expects the sources to be in main while running pio init expects the sources to be in src. Somewhere I read that this is hardcoded, so I moved mv main src. But then idf.py build no longer builds.
So I tried to build by pio setting up platformio.ini with pio project init -b esp32doit-devkit-v1 --ide=vscode -O "framework = espidf" but it is not terminating at all. After terminating by Ctrl-C there was a platformio.ini with:

[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32doit-devkit-v1
framework = espidf

So I tried pio run which failed with Error: Couldn't find the main target of the project!. I found a hint to work around this by using platform = espressif32@1.11.2. So pio run realy starts to compile but now failed with fatal error: esp_vfs_semihost.h: No such file or directory.
Is there a step by step tutorial, how to build the examples from esp-idf/examples using PlatformIO? Or is there an import procedure that I missed? PlatformIO within VSCode only provides import for Arduino projects, not for IDF.
Thanks in advance.

Not valid if that is from the ESP-IDF master branch. It must match the used ESP-IDF version for platform-espressif32, aka 4.3.2 currently. v4.4 support has not been released (or added yet).

That doesn’t look healthy. Please start by copying the platform example https://github.com/platformio/platform-espressif32/tree/develop/examples/espidf-blink, if that does not work there’s no point in trying to go further.

Compiling ESP-IDF provided example (of the correct version) is also covered in ESP32 IDF Protocol Examples not working - #6 by chrisalfred.

Thanks very much @maxgerhardt .

The platform example espidf-blink builds without problems using ‘pio run’. It comes with sources in src and CONFIG_BLINK_GPIO=2 and thus gives a nice blinking LED after pio run --target upload.

So my first fault was to take examples from an independent git clone. Lesson learned is to take examples for use with PlatformIO always from ${HOME}/.platformio/packages/framework-espidf where version.txt tells 4.3.2. But as I found out it doesn’t change a lot.

So I took restful_server from within that copy of IDF and tried to create platformio.ini with pio project init -b esp32doit-devkit-v1 --ide=vscode -O "framework = espidf". Now it didn’t hang up but terminated with Error: Couldn't find the main target of the project!. Adding @1.11.2 (like in my first post) let pio project init terminate with success. The platformio.ini made by pio project init with the version patch now looks like this:

[env:esp32doit-devkit-v1]
platform = espressif32@1.11.2
board = esp32doit-devkit-v1
framework = espidf

Another lesson learned is, using src_dir = main to tell PlatformIO where the sources are, is a bad idea. That was the cause of the fatal error: esp_vfs_semihost.h: No such file or directory. Without it the example compiles even if the sources are in main.

An attempt to build by pio run now fails with a missing linker script:

Linking .pio/build/esp32doit-devkit-v1/firmware.elf
ld: cannot open linker script file esp32.project.ld: No such file or directory

The only *.ld within the project directory that can be found is .pio/build/esp32doit-devkit-v1/esp32_out.ld.

The last hint seems to be not needed. The example already references the protocol_examples_common from within the CMakeLists.txt in the base of the project by:
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
Even with a local copy within the project directory as described in ESP32 IDF Protocol Examples not working - #7 by maxgerhardt the linker script still is missing.

p.s.: I had to remove some links because the platform was complaining.