idf_cmd_init.bat 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. :: Clear PYTHONHOME as it may contain path to other Python versions which can cause crash of Python using virtualenv
  28. if not "%PYTHONHOME%"=="" (
  29. echo Clearing PYTHONHOME, was set to %PYTHONHOME%
  30. set PYTHONHOME=
  31. )
  32. :: Set PYTHONNOUSERSITE to avoid loading of Python packages from AppData\Roaming profile
  33. if "%PYTHONNOUSERSITE%"=="" (
  34. echo Setting PYTHONNOUSERSITE, was not set
  35. set PYTHONNOUSERSITE=True
  36. )
  37. :: Add Python and Git paths to PATH
  38. set "PATH=%IDF_PYTHON_DIR%;%IDF_GIT_DIR%;%PATH%"
  39. echo Using Python in %IDF_PYTHON_DIR%
  40. python.exe --version
  41. echo Using Git in %IDF_GIT_DIR%
  42. git.exe --version
  43. :: Check if this is a recent enough copy of ESP-IDF.
  44. :: If so, use export.bat provided there.
  45. :: Note: no "call", will not return into this batch file.
  46. if exist "%IDF_PATH%\export.bat" %IDF_PATH%\export.bat
  47. echo IDF version does not include export.bat. Using the fallback version.
  48. if exist "%IDF_PATH%\tools\tools.json" (
  49. set "IDF_TOOLS_JSON_PATH=%IDF_PATH%\tools\tools.json"
  50. ) else (
  51. echo IDF version does not include tools\tools.json. Using the fallback version.
  52. set "IDF_TOOLS_JSON_PATH=%~dp0%tools_fallback.json"
  53. )
  54. if exist "%IDF_PATH%\tools\idf_tools.py" (
  55. set "IDF_TOOLS_PY_PATH=%IDF_PATH%\tools\idf_tools.py"
  56. ) else (
  57. echo IDF version does not include tools\idf_tools.py. Using the fallback version.
  58. set "IDF_TOOLS_PY_PATH=%~dp0%idf_tools_fallback.py"
  59. )
  60. echo.
  61. echo Setting IDF_PATH: %IDF_PATH%
  62. echo.
  63. set "OLD_PATH=%PATH%"
  64. echo Adding ESP-IDF tools to PATH...
  65. :: Export tool paths and environment variables.
  66. :: It is possible to do this without a temporary file (running idf_tools.py from for /r command),
  67. :: but that way it is impossible to get the exit code of idf_tools.py.
  68. set "IDF_TOOLS_EXPORTS_FILE=%TEMP%\idf_export_vars.tmp"
  69. python.exe "%IDF_TOOLS_PY_PATH%" --tools-json "%IDF_TOOLS_JSON_PATH%" export --format key-value >"%IDF_TOOLS_EXPORTS_FILE%"
  70. if %errorlevel% neq 0 goto :end
  71. for /f "usebackq tokens=1,2 eol=# delims==" %%a in ("%IDF_TOOLS_EXPORTS_FILE%") do (
  72. call set "%%a=%%b"
  73. )
  74. :: This removes OLD_PATH substring from PATH, leaving only the paths which have been added,
  75. :: and prints semicolon-delimited components of the path on separate lines
  76. call set PATH_ADDITIONS=%%PATH:%OLD_PATH%=%%
  77. if "%PATH_ADDITIONS%"=="" call :print_nothing_added
  78. if not "%PATH_ADDITIONS%"=="" echo %PATH_ADDITIONS:;=&echo. %
  79. echo Checking if Python packages are up to date...
  80. python.exe %IDF_PATH%\tools\check_python_dependencies.py
  81. if %errorlevel% neq 0 goto :end
  82. echo.
  83. echo Done! You can now compile ESP-IDF projects.
  84. echo Go to the project directory and run:
  85. echo.
  86. echo idf.py build
  87. echo.
  88. goto :end
  89. :print_nothing_added
  90. echo No directories added to PATH:
  91. echo.
  92. echo %PATH%
  93. echo.
  94. goto :eof
  95. :end
  96. :: Clean up
  97. if not "%IDF_TOOLS_EXPORTS_FILE%"=="" (
  98. del "%IDF_TOOLS_EXPORTS_FILE%" 1>nul 2>nul
  99. )
  100. set IDF_TOOLS_EXPORTS_FILE=
  101. set IDF_PYTHON_DIR=
  102. set IDF_GIT_DIR=
  103. set IDF_TOOLS_PY_PATH=
  104. set IDF_TOOLS_JSON_PATH=
  105. set OLD_PATH=
  106. set PATH_ADDITIONS=