idf_cmd_init.bat 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. @echo off
  2. :: This script is called from a shortcut (cmd.exe /k export_fallback.bat), with
  3. :: the working directory set to an ESP-IDF directory.
  4. :: Its purpose is to support using the "IDF Tools Directory" method of
  5. :: installation for ESP-IDF versions older than IDF v4.0.
  6. :: It does the same thing as "export.bat" in IDF v4.0.
  7. set IDF_PATH=%CD%
  8. if not exist "%IDF_PATH%\tools\idf.py" (
  9. echo This script must be invoked from ESP-IDF directory.
  10. goto :end
  11. )
  12. if "%~2"=="" (
  13. echo Usage: idf_cmd_init.bat ^<Python directory^> ^<Git directory^>
  14. echo This script must be invoked from ESP-IDF directory.
  15. goto :end
  16. )
  17. set "IDF_PYTHON_DIR=%1"
  18. set "IDF_GIT_DIR=%2"
  19. :: Strip quoutes
  20. set "IDF_PYTHON_DIR=%IDF_PYTHON_DIR:"=%"
  21. set "IDF_GIT_DIR=%IDF_GIT_DIR:"=%"
  22. :: Clear PYTHONPATH as it may contain libraries of other Python versions
  23. if not "%PYTHONPATH%"=="" (
  24. echo Clearing PYTHONPATH, was set to %PYTHONPATH%
  25. set PYTHONPATH=
  26. )
  27. :: Add Python and Git paths to PATH
  28. set "PATH=%IDF_PYTHON_DIR%;%IDF_GIT_DIR%;%PATH%"
  29. echo Using Python in %IDF_PYTHON_DIR%
  30. python.exe --version
  31. echo Using Git in %IDF_GIT_DIR%
  32. git.exe --version
  33. :: Check if this is a recent enough copy of ESP-IDF.
  34. :: If so, use export.bat provided there.
  35. :: Note: no "call", will not return into this batch file.
  36. if exist "%IDF_PATH%\export.bat" %IDF_PATH%\export.bat
  37. echo IDF version does not include export.bat. Using the fallback version.
  38. if exist "%IDF_PATH%\tools\tools.json" (
  39. set "IDF_TOOLS_JSON_PATH=%IDF_PATH%\tools\tools.json"
  40. ) else (
  41. echo IDF version does not include tools\tools.json. Using the fallback version.
  42. set "IDF_TOOLS_JSON_PATH=%~dp0%tools_fallback.json"
  43. )
  44. if exist "%IDF_PATH%\tools\idf_tools.py" (
  45. set "IDF_TOOLS_PY_PATH=%IDF_PATH%\tools\idf_tools.py"
  46. ) else (
  47. echo IDF version does not include tools\idf_tools.py. Using the fallback version.
  48. set "IDF_TOOLS_PY_PATH=%~dp0%idf_tools_fallback.py"
  49. )
  50. echo.
  51. echo Setting IDF_PATH: %IDF_PATH%
  52. echo.
  53. set "OLD_PATH=%PATH%"
  54. echo Adding ESP-IDF tools to PATH...
  55. :: Export tool paths and environment variables.
  56. :: It is possible to do this without a temporary file (running idf_tools.py from for /r command),
  57. :: but that way it is impossible to get the exit code of idf_tools.py.
  58. set "IDF_TOOLS_EXPORTS_FILE=%TEMP%\idf_export_vars.tmp"
  59. python.exe "%IDF_TOOLS_PY_PATH%" --tools-json "%IDF_TOOLS_JSON_PATH%" export --format key-value >"%IDF_TOOLS_EXPORTS_FILE%"
  60. if %errorlevel% neq 0 goto :end
  61. for /f "usebackq tokens=1,2 eol=# delims==" %%a in ("%IDF_TOOLS_EXPORTS_FILE%") do (
  62. call set "%%a=%%b"
  63. )
  64. :: This removes OLD_PATH substring from PATH, leaving only the paths which have been added,
  65. :: and prints semicolon-delimited components of the path on separate lines
  66. call set PATH_ADDITIONS=%%PATH:%OLD_PATH%=%%
  67. if "%PATH_ADDITIONS%"=="" call :print_nothing_added
  68. if not "%PATH_ADDITIONS%"=="" echo %PATH_ADDITIONS:;=&echo. %
  69. echo Checking if Python packages are up to date...
  70. python.exe %IDF_PATH%\tools\check_python_dependencies.py
  71. if %errorlevel% neq 0 goto :end
  72. echo.
  73. echo Done! You can now compile ESP-IDF projects.
  74. echo Go to the project directory and run:
  75. echo.
  76. echo idf.py build
  77. echo.
  78. goto :end
  79. :print_nothing_added
  80. echo No directories added to PATH:
  81. echo.
  82. echo %PATH%
  83. echo.
  84. goto :eof
  85. :end
  86. :: Clean up
  87. if not "%IDF_TOOLS_EXPORTS_FILE%"=="" (
  88. del "%IDF_TOOLS_EXPORTS_FILE%" 1>nul 2>nul
  89. )
  90. set IDF_TOOLS_EXPORTS_FILE=
  91. set IDF_PYTHON_DIR=
  92. set IDF_GIT_DIR=
  93. set IDF_TOOLS_PY_PATH=
  94. set IDF_TOOLS_JSON_PATH=
  95. set OLD_PATH=
  96. set PATH_ADDITIONS=