export.sh 7.2 KB

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