idf_cmd_init.ps1 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # This script is called from a Windows shortcut, with
  2. # the working directory set to an ESP-IDF directory.
  3. # Its purpose is to support using the "IDF Tools Directory" method of
  4. # installation for ESP-IDF versions older than IDF v4.1.
  5. # It does the same thing as "export.ps1" in IDF v4.1.
  6. Param
  7. (
  8. [String]$IdfGitDir,
  9. [String]$IdfPythonDir
  10. )
  11. $IDF_PATH = "."
  12. $isEspIdfRoot = (Test-Path "$IDF_PATH/tools/idf.py")
  13. if (-not $isEspIdfRoot) {
  14. Write-Output "Usage: idf_cmd_init.ps1 ^<Python directory^> ^<Git directory^>"
  15. Write-Output "This script must be invoked from ESP-IDF directory."
  16. }
  17. # Clear PYTHONPATH as it may contain libraries of other Python versions
  18. if ($null -ne $env:PYTHONPATH) {
  19. "Clearing PYTHONPATH, was set to $env:PYTHONPATH"
  20. $env:PYTHONPATH=$null
  21. }
  22. # Clear PYTHONHOME as it may contain path to other Python versions which can cause crash of Python using virtualenv
  23. if ($null -ne $env:PYTHONHOME) {
  24. "Clearing PYTHONHOME, was set to $env:PYTHONHOME"
  25. $env:PYTHONHOME=$null
  26. }
  27. # Set PYTHONNOUSERSITE to avoid loading of Python packages from AppData\Roaming profile
  28. if ($null -eq $env:PYTHONNOUSERSITE) {
  29. "Setting PYTHONNOUSERSITE, was not set"
  30. $env:PYTHONNOUSERSITE="True"
  31. }
  32. # Strip quotes
  33. $IdfGitDir = $IdfGitDir.Trim("`"")
  34. $IdfPythonDir = $IdfPythonDir.Trim("`"")
  35. # Add Python and Git paths to PATH
  36. $env:PATH = "$IdfGitDir;$IdfPythonDir;$env:PATH"
  37. Write-Output "Using Python in $IdfPythonDir"
  38. python.exe --version
  39. Write-Output "Using Git in $IdfGitDir"
  40. git.exe --version
  41. # Check if this is a recent enough copy of ESP-IDF.
  42. # If so, use export.ps1 provided there.
  43. $isExport = (Test-Path "$IDF_PATH/export.ps1")
  44. if ($isExport){
  45. . $IDF_PATH/export.ps1
  46. }
  47. else {
  48. Write-Output "IDF version does not include export.ps1. Using the fallback version."
  49. if ((Test-Path "$IDF_PATH/tools/tools.json")){
  50. $IDF_TOOLS_JSON_PATH = "$IDF_PATH/tools/tools.json"
  51. }
  52. else{
  53. Write-Output "IDF version does not include tools/tools.json. Using the fallback version."
  54. $IDF_TOOLS_JSON_PATH = "$PSScriptRoot/tools_fallback.json"
  55. }
  56. if ((Test-Path "$IDF_PATH/tools/idf_tools.py")){
  57. $IDF_TOOLS_PY_PATH = "$IDF_PATH/tools/idf_tools.py"
  58. }
  59. else{
  60. Write-Output "IDF version does not include tools/idf_tools.py. Using the fallback version."
  61. $IDF_TOOLS_PY_PATH = "$PSScriptRoot/idf_tools_fallback.py"
  62. }
  63. Write-Output "Setting IDF_PATH: $IDF_PATH"
  64. $env:IDF_PATH = $IDF_PATH
  65. Write-Output "Adding ESP-IDF tools to PATH..."
  66. $OLD_PATH = $env:Path.split(";") | Select-Object -Unique # array without duplicates
  67. # using idf_tools.py to get $envars_array to set
  68. $envars_raw = (python.exe "$IDF_TOOLS_PY_PATH" --tools-json "$IDF_TOOLS_JSON_PATH" export --format key-value)
  69. if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } # if error
  70. $envars_array # will be filled like:
  71. # [
  72. # [vname1, vval1], [vname2, vval2], ...
  73. # ]
  74. foreach ($line in $envars_raw) {
  75. $pair = $line.split("=") # split in name, val
  76. $var_name = $pair[0].Trim() # trim spaces on the ends of the name
  77. $var_val = $pair[1].Trim() # trim spaces on the ends of the val
  78. $var_val = $var_val -replace "%(.+)%", "`$env:`$1" # convert var syntax to PS using RegEx
  79. $var_val = $ExecutionContext.InvokeCommand.ExpandString($var_val) # expand variables to values
  80. $envars_array += (, ($var_name, $var_val))
  81. }
  82. foreach ($pair in $envars_array) {
  83. # setting the values
  84. $var_name = $pair[0].Trim() # trim spaces on the ends of the name
  85. $var_val = $pair[1].Trim() # trim spaces on the ends of the val
  86. Set-Item -Path "Env:$var_name" -Value "$var_val"
  87. }
  88. #Compare Path's OLD vs. NEW
  89. $NEW_PATH = $env:Path.split(";") | Select-Object -Unique # array without duplicates
  90. $dif_Path = Compare-Object -ReferenceObject $OLD_PATH -DifferenceObject $NEW_PATH -PassThru
  91. if ($dif_Path -ne $null) {
  92. Write-Output $dif_Path
  93. }
  94. else {
  95. Write-Output "No directories added to PATH:"
  96. Write-Output $OLD_PATH
  97. }
  98. Write-Output "Checking if Python packages are up to date..."
  99. Start-Process -Wait -NoNewWindow -FilePath "python" -Args "`"$IDF_PATH/tools/check_python_dependencies.py`""
  100. if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } # if error
  101. Write-Output "
  102. Done! You can now compile ESP-IDF projects.
  103. Go to the project directory and run:
  104. idf.py build
  105. "
  106. }