export.sh 8.0 KB

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