export.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # This script should be sourced, not executed.
  2. realpath_int() {
  3. wdir="$PWD"; [ "$PWD" = "/" ] && wdir=""
  4. arg=$1
  5. case "$arg" in
  6. /*) scriptdir="${arg}";;
  7. *) scriptdir="$wdir/${arg#./}";;
  8. esac
  9. scriptdir="${scriptdir%/*}"
  10. echo "$scriptdir"
  11. }
  12. idf_export_main() {
  13. # The file doesn't have executable permissions, so this shouldn't really happen.
  14. # Doing this in case someone tries to chmod +x it and execute...
  15. # shellcheck disable=SC2128,SC2169,SC2039 # ignore array expansion warning
  16. if [ -n "${BASH_SOURCE-}" ] && [ "${BASH_SOURCE[0]}" = "${0}" ]
  17. then
  18. echo "This script should be sourced, not executed:"
  19. # shellcheck disable=SC2039 # reachable only with bash
  20. echo ". ${BASH_SOURCE[0]}"
  21. return 1
  22. fi
  23. if [ -z "${IDF_PATH}" ]
  24. then
  25. # IDF_PATH not set in the environment.
  26. # If using bash or zsh, try to guess IDF_PATH from script location.
  27. self_path=""
  28. # shellcheck disable=SC2128 # ignore array expansion warning
  29. if [ -n "${BASH_SOURCE-}" ]
  30. then
  31. self_path="${BASH_SOURCE}"
  32. elif [ -n "${ZSH_VERSION-}" ]
  33. then
  34. self_path="${(%):-%x}"
  35. else
  36. echo "Could not detect IDF_PATH. Please set it before sourcing this script:"
  37. echo " export IDF_PATH=(add path here)"
  38. return 1
  39. fi
  40. # shellcheck disable=SC2169,SC2169,SC2039 # unreachable with 'dash'
  41. if [[ "$OSTYPE" == "darwin"* ]]; then
  42. # convert possibly relative path to absolute
  43. script_dir="$(realpath_int "${self_path}")"
  44. # resolve any ../ references to make the path shorter
  45. script_dir="$(cd "${script_dir}" || exit 1; pwd)"
  46. else
  47. # convert to full path and get the directory name of that
  48. script_name="$(readlink -f "${self_path}")"
  49. script_dir="$(dirname "${script_name}")"
  50. fi
  51. export IDF_PATH="${script_dir}"
  52. echo "Setting IDF_PATH to '${IDF_PATH}'"
  53. else
  54. # IDF_PATH came from the environment, check if the path is valid
  55. if [ ! -d "${IDF_PATH}" ]
  56. then
  57. echo "IDF_PATH is set to '${IDF_PATH}', but it is not a valid directory."
  58. echo "If you have set IDF_PATH manually, check if the path is correct."
  59. return 1
  60. fi
  61. # Check if this path looks like an IDF directory
  62. if [ ! -f "${IDF_PATH}/tools/idf.py" ] || [ ! -f "${IDF_PATH}/tools/idf_tools.py" ]
  63. then
  64. echo "IDF_PATH is set to '${IDF_PATH}', but it doesn't look like an ESP-IDF directory."
  65. echo "If you have set IDF_PATH manually, check if the path is correct."
  66. return 1
  67. fi
  68. # The varible might have been set (rather than exported), re-export it to be sure
  69. export IDF_PATH="${IDF_PATH}"
  70. fi
  71. old_path="$PATH"
  72. echo "Detecting the Python interpreter"
  73. . "${IDF_PATH}/tools/detect_python.sh"
  74. echo "Adding ESP-IDF tools to PATH..."
  75. # Call idf_tools.py to export tool paths
  76. export IDF_TOOLS_EXPORT_CMD=${IDF_PATH}/export.sh
  77. export IDF_TOOLS_INSTALL_CMD=${IDF_PATH}/install.sh
  78. idf_exports=$("$ESP_PYTHON" "${IDF_PATH}/tools/idf_tools.py" export) || return 1
  79. eval "${idf_exports}"
  80. echo "Using Python interpreter in $(which python)"
  81. echo "Checking if Python packages are up to date..."
  82. python "${IDF_PATH}/tools/check_python_dependencies.py" || return 1
  83. # Allow calling some IDF python tools without specifying the full path
  84. # ${IDF_PATH}/tools is already added by 'idf_tools.py export'
  85. IDF_ADD_PATHS_EXTRAS="${IDF_PATH}/components/esptool_py/esptool"
  86. IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/espcoredump"
  87. IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/partition_table"
  88. IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/app_update"
  89. export PATH="${IDF_ADD_PATHS_EXTRAS}:${PATH}"
  90. if [ -n "$BASH" ]
  91. then
  92. path_prefix=${PATH%%${old_path}}
  93. # shellcheck disable=SC2169,SC2039 # unreachable with 'dash'
  94. paths="${path_prefix//:/ }"
  95. if [ -n "${paths}" ]; then
  96. echo "Added the following directories to PATH:"
  97. else
  98. echo "All paths are already set."
  99. fi
  100. for path_entry in ${paths}
  101. do
  102. echo " ${path_entry}"
  103. done
  104. else
  105. echo "Updated PATH variable:"
  106. echo " ${PATH}"
  107. fi
  108. # Clean up
  109. unset old_path
  110. unset paths
  111. unset path_prefix
  112. unset path_entry
  113. unset IDF_ADD_PATHS_EXTRAS
  114. unset idf_exports
  115. unset ESP_PYTHON
  116. # Not unsetting IDF_PYTHON_ENV_PATH, it can be used by IDF build system
  117. # to check whether we are using a private Python environment
  118. echo "Done! You can now compile ESP-IDF projects."
  119. echo "Go to the project directory and run:"
  120. echo ""
  121. echo " idf.py build"
  122. echo ""
  123. }
  124. enable_autocomplete() {
  125. click_version="$(python -c 'import click; print(click.__version__.split(".")[0])')"
  126. if [[ click_version -lt 8 ]]
  127. then
  128. SOURCE_ZSH=source_zsh
  129. SOURCE_BASH=source_bash
  130. else
  131. SOURCE_ZSH=zsh_source
  132. SOURCE_BASH=bash_source
  133. fi
  134. if [ -n "${ZSH_VERSION-}" ]
  135. then
  136. autoload -Uz compinit && compinit -u
  137. eval "$(env _IDF.PY_COMPLETE=$SOURCE_ZSH idf.py)" || echo "WARNING: Failed to load shell autocompletion for zsh version: $ZSH_VERSION!"
  138. elif [ -n "${BASH_SOURCE-}" ]
  139. then
  140. eval "$(env LANG=en _IDF.PY_COMPLETE=$SOURCE_BASH idf.py)" || echo "WARNING: Failed to load shell autocompletion for bash version: $BASH_VERSION!"
  141. fi
  142. unset SOURCE_ZSH
  143. unset SOURCE_BASH
  144. }
  145. idf_export_main
  146. enable_autocomplete
  147. unset realpath_int
  148. unset idf_export_main
  149. unset enable_autocomplete