export.sh 6.8 KB

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