I had been doing this since IDF 3 with make. I had a custom build script.
When I ported to IDF 4 with CMake, I created a new init scripts to set the environment.
(My CMakeLists.txt doesn't need anything special for this b/c my init script sets IDF_PATH in the environment. It still uses include("$ENV{IDF_PATH}/tools/cmake/project.cmake"))
I made a windows version and a linux of the script. It expects the esp-idf to be a git submodule in the project repo/tools/esp-idf/.
You can just double-click the _START_IDF_ENV.cmd file to start a cmd window.
On linux you have to type: source sourceme_IDF_ENV.sh
@REM Script for launching ESP-IDF environment on Windows
@REM Author: Paul Abbott, 2022
@ECHO OFF
TITLE ESP-IDF
SETLOCAL EnableDelayedExpansion
: %CD% gives current dir
SET THISDIR=%CD%
: %~dp0 gives the directory of this batch file (with a trailing \)
SET SCRIPTDIR=%~dp0
:: if using IDF installed globally ::
: SET "IDF_INIT_SCRIPT=C:\Espressif\idf_cmd_init.bat"
:: See C:\Espressif\esp_idf.json for installed IDF version strings
:: Version 4.4
: SET "IDF_VERS_STRING=esp-idf-8de2bd0d9cffd2eca3d3f8442939a034"
:: Version 4.3
: SET "IDF_VERS_STRING=esp-idf-7a3cc3545977e8fdecfa73521e1f3180"
:: if using IDF inside repo as submodule::
SET "IDF_INIT_SCRIPT=%SCRIPTDIR%..\tools\esp-idf\install.bat"
SET "IDF_INIT_SCRIPT2=%SCRIPTDIR%..\tools\esp-idf\export.bat"
IF NOT EXIST %IDF_INIT_SCRIPT% (
MORE %SCRIPTDIR%readme.md
ECHO Script %IDF_INIT_SCRIPT% does not exist!.
GOTO :DIE
)
CALL %IDF_INIT_SCRIPT% %IDF_VERS_STRING%
IF !ERRORLEVEL! NEQ 0 goto :DIE
IF NOT EXIST %IDF_INIT_SCRIPT% GOTO :DONE
CALL %IDF_INIT_SCRIPT2%
IF !ERRORLEVEL! NEQ 0 goto :DIE
:DONE
GOTO :SHELL
:DIE
: Color the screen red
COLOR 4f
ECHO ERROR!
PAUSE
GOTO :QUIT
:QUIT
: Restore the Color of the screen
COLOR 07
GOTO :eof
:SHELL
: Restore the Color of the screen
COLOR 07
ECHO/
ECHO idf build flash monitor -p COM15
ECHO/
cmd /k
#!/bin/bash
# Script for setting up ESP_IDF environment on Ubuntu
# use it like this: `source sourceme_IDF_ENV.sh`
# Author: Paul Abbott, 2022
SCRIPT_VER="1.0"
SCRIPT_DIR=$(realpath "$(dirname "${BASH_SOURCE[0]}")")
PWD=$(pwd)
# this only works when sourced (not run as a script), because it is adding variables into the local shell
[[ "${BASH_SOURCE[0]}" == "${0}" ]] && echo "Error, must source this script! Try: \"source ${BASH_SOURCE[0]}\"" && exit 1
source $SCRIPT_DIR/../tools/esp-idf/export.sh
alias idf="idf.py"
# These commands will not execute, but will be in the history for convenience.
history -s idf build
history -s idf build flash monitor -p /dev/ttyUSB0