1@echo off
2if defined MSYSTEM (
3    echo This .bat file is for Windows CMD.EXE shell only. When using MSYS, run:
4    echo   . ./export.sh.
5    goto :eof
6)
7
8:: Missing requirements check
9set MISSING_REQUIREMENTS=
10python.exe --version >NUL 2>NUL
11if %errorlevel% neq 0 set "MISSING_REQUIREMENTS=  python &echo\"
12git.exe --version >NUL 2>NUL
13if %errorlevel% neq 0 set "MISSING_REQUIREMENTS=%MISSING_REQUIREMENTS%  git"
14
15if not "%MISSING_REQUIREMENTS%" == "" goto :__error_missing_requirements
16
17:: Infer IDF_PATH from script location
18set IDF_PATH=%~dp0
19set IDF_PATH=%IDF_PATH:~0,-1%
20
21set "IDF_TOOLS_PY_PATH=%IDF_PATH%\tools\idf_tools.py"
22set "IDF_TOOLS_JSON_PATH=%IDF_PATH%\tools\tools.json"
23set "IDF_TOOLS_EXPORT_CMD=%IDF_PATH%\export.bat"
24set "IDF_TOOLS_INSTALL_CMD=%IDF_PATH%\install.bat"
25echo Setting IDF_PATH: %IDF_PATH%
26echo.
27
28set "OLD_PATH=%PATH%"
29echo Adding ESP-IDF tools to PATH...
30:: Export tool paths and environment variables.
31:: It is possible to do this without a temporary file (running idf_tools.py from for /r command),
32:: but that way it is impossible to get the exit code of idf_tools.py.
33set "IDF_TOOLS_EXPORTS_FILE=%TEMP%\idf_export_vars.tmp"
34python.exe "%IDF_PATH%\tools\idf_tools.py" export --format key-value >"%IDF_TOOLS_EXPORTS_FILE%"
35if %errorlevel% neq 0 goto :__end
36
37for /f "usebackq tokens=1,2 eol=# delims==" %%a in ("%IDF_TOOLS_EXPORTS_FILE%") do (
38      call set "%%a=%%b"
39    )
40
41:: This removes OLD_PATH substring from PATH, leaving only the paths which have been added,
42:: and prints semicolon-delimited components of the path on separate lines
43call set PATH_ADDITIONS=%%PATH:%OLD_PATH%=%%
44if "%PATH_ADDITIONS%"=="" call :__print_nothing_added
45if not "%PATH_ADDITIONS%"=="" echo     %PATH_ADDITIONS:;=&echo.    %
46
47DOSKEY idf.py=python.exe "%IDF_PATH%\tools\idf.py" $*
48DOSKEY esptool.py=python.exe "%IDF_PATH%\components\esptool_py\esptool\esptool.py" $*
49DOSKEY espefuse.py=python.exe "%IDF_PATH%\components\esptool_py\esptool\espefuse.py" $*
50DOSKEY otatool.py=python.exe "%IDF_PATH%\components\app_update\otatool.py" $*
51DOSKEY parttool.py=python.exe "%IDF_PATH%\components\partition_table\parttool.py" $*
52
53echo Checking if Python packages are up to date...
54python.exe "%IDF_PATH%\tools\check_python_dependencies.py"
55if %errorlevel% neq 0 goto :__end
56
57echo.
58echo Done! You can now compile ESP-IDF projects.
59echo Go to the project directory and run:
60echo.
61echo   idf.py build
62echo.
63
64goto :__end
65
66:__print_nothing_added
67    echo No directories added to PATH:
68    echo.
69    echo %PATH%
70    echo.
71    goto :eof
72
73:__error_missing_requirements
74    echo.
75    echo Error^: The following tools are not installed in your environment.
76    echo.
77    echo %MISSING_REQUIREMENTS%
78    echo.
79    echo Please use the Windows Tool installer for setting up your environment.
80    echo Download link: https://dl.espressif.com/dl/esp-idf/
81    echo For more details please visit our website: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/windows-setup.html
82    goto :__end
83
84:__end
85:: Clean up
86if not "%IDF_TOOLS_EXPORTS_FILE%"=="" (
87    del "%IDF_TOOLS_EXPORTS_FILE%" 1>nul 2>nul
88)
89set IDF_TOOLS_EXPORTS_FILE=
90set IDF_TOOLS_EXPORT_CMD=
91set IDF_TOOLS_INSTALL_CMD=
92set IDF_TOOLS_PY_PATH=
93set IDF_TOOLS_JSON_PATH=
94set OLD_PATH=
95set PATH_ADDITIONS=
96set MISSING_REQUIREMENTS=
97