export.sh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 [ "$(uname -s)" = "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. # shellcheck disable=SC2296 # ignore parameter starts with '{' because it's zsh
  61. self_path="${(%):-%x}"
  62. fi
  63. script_dir="$(__script_dir)"
  64. # Since sh or dash shells can't detect script_dir correctly, check if script_dir looks like an IDF directory
  65. is_script_dir_esp_idf=$(__is_dir_esp_idf "${script_dir}")
  66. if [ -z "${IDF_PATH-}" ]
  67. then
  68. # IDF_PATH not set in the environment.
  69. if [ -n "${is_script_dir_esp_idf}" ]
  70. then
  71. echo "Could not detect IDF_PATH. Please set it before sourcing this script:"
  72. echo " export IDF_PATH=(add path here)"
  73. return 1
  74. fi
  75. export IDF_PATH="${script_dir}"
  76. echo "Setting IDF_PATH to '${IDF_PATH}'"
  77. else
  78. # IDF_PATH came from the environment, check if the path is valid
  79. # Set IDF_PATH to script_dir, if script_dir looks like an IDF directory
  80. if [ ! "${IDF_PATH}" = "${script_dir}" ] && [ -z "${is_script_dir_esp_idf}" ]
  81. then
  82. # Change IDF_PATH is important when there are 2 ESP-IDF versions in different directories.
  83. # Sourcing this script without change, would cause sourcing wrong export script.
  84. echo "Resetting IDF_PATH from '${IDF_PATH}' to '${script_dir}' "
  85. export IDF_PATH="${script_dir}"
  86. fi
  87. # Check if this path looks like an IDF directory
  88. is_idf_path_esp_idf=$(__is_dir_esp_idf "${IDF_PATH}")
  89. if [ -n "${is_idf_path_esp_idf}" ]
  90. then
  91. echo "IDF_PATH is set to '${IDF_PATH}', but it doesn't look like an ESP-IDF directory."
  92. echo "If you have set IDF_PATH manually, check if the path is correct."
  93. return 1
  94. fi
  95. # The varible might have been set (rather than exported), re-export it to be sure
  96. export IDF_PATH="${IDF_PATH}"
  97. fi
  98. old_path="$PATH"
  99. echo "Detecting the Python interpreter"
  100. . "${IDF_PATH}/tools/detect_python.sh"
  101. echo "Checking Python compatibility"
  102. "$ESP_PYTHON" "${IDF_PATH}/tools/python_version_checker.py"
  103. __verbose "Checking other ESP-IDF version."
  104. idf_deactivate=$("$ESP_PYTHON" "${IDF_PATH}/tools/idf_tools.py" export --deactivate) || return 1
  105. eval "${idf_deactivate}"
  106. __verbose "Adding ESP-IDF tools to PATH..."
  107. # Call idf_tools.py to export tool paths
  108. export IDF_TOOLS_EXPORT_CMD=${IDF_PATH}/export.sh
  109. export IDF_TOOLS_INSTALL_CMD=${IDF_PATH}/install.sh
  110. # Allow calling some IDF python tools without specifying the full path
  111. # ${IDF_PATH}/tools is already added by 'idf_tools.py export'
  112. 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 "Checking if Python packages are up to date..."
  119. "$ESP_PYTHON" "${IDF_PATH}/tools/idf_tools.py" check-python-dependencies || return 1
  120. if [ -n "$BASH" ]
  121. then
  122. path_prefix="${PATH%%"${old_path}"}"
  123. # shellcheck disable=SC2169,SC2039 # unreachable with 'dash'
  124. if [ -n "${path_prefix}" ]; then
  125. __verbose "Added the following directories to PATH:"
  126. else
  127. __verbose "All paths are already set."
  128. fi
  129. old_ifs="$IFS"
  130. IFS=":"
  131. for path_entry in ${path_prefix}
  132. do
  133. __verbose " ${path_entry}"
  134. done
  135. IFS="$old_ifs"
  136. unset old_ifs
  137. else
  138. __verbose "Updated PATH variable:"
  139. __verbose " ${PATH}"
  140. fi
  141. uninstall=$("$ESP_PYTHON" "${IDF_PATH}/tools/idf_tools.py" uninstall --dry-run) || return 1
  142. if [ -n "$uninstall" ]
  143. then
  144. __verbose ""
  145. __verbose "Detected installed tools that are not currently used by active ESP-IDF version."
  146. __verbose "${uninstall}"
  147. __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'."
  148. __verbose ""
  149. fi
  150. __verbose "Done! You can now compile ESP-IDF projects."
  151. __verbose "Go to the project directory and run:"
  152. __verbose ""
  153. __verbose " idf.py build"
  154. __verbose ""
  155. }
  156. __cleanup() {
  157. unset old_path
  158. unset paths
  159. unset path_prefix
  160. unset path_entry
  161. unset IDF_ADD_PATHS_EXTRAS
  162. unset idf_exports
  163. unset idf_deactivate
  164. unset ESP_PYTHON
  165. unset SOURCE_ZSH
  166. unset SOURCE_BASH
  167. unset WARNING_MSG
  168. unset uninstall
  169. unset is_idf_path_esp_idf
  170. unset is_script_dir_esp_idf
  171. unset __realpath
  172. unset __main
  173. unset __verbose
  174. unset __enable_autocomplete
  175. unset __cleanup
  176. unset __is_dir_esp_idf
  177. # Not unsetting IDF_PYTHON_ENV_PATH, it can be used by IDF build system
  178. # to check whether we are using a private Python environment
  179. return "$1"
  180. }
  181. __enable_autocomplete() {
  182. click_version="$(python -c 'import click; print(click.__version__.split(".")[0])')"
  183. if [ "${click_version}" -lt 8 ]
  184. then
  185. SOURCE_ZSH=source_zsh
  186. SOURCE_BASH=source_bash
  187. else
  188. SOURCE_ZSH=zsh_source
  189. SOURCE_BASH=bash_source
  190. fi
  191. if [ -n "${ZSH_VERSION-}" ]
  192. then
  193. autoload -Uz compinit && compinit -u
  194. eval "$(env _IDF.PY_COMPLETE=$SOURCE_ZSH idf.py)" || echo "WARNING: Failed to load shell autocompletion for zsh version: $ZSH_VERSION!"
  195. elif [ -n "${BASH_SOURCE-}" ]
  196. then
  197. WARNING_MSG="WARNING: Failed to load shell autocompletion for bash version: $BASH_VERSION!"
  198. # shellcheck disable=SC3028,SC3054,SC2086,SC2169 # code block for 'bash' only
  199. [ ${BASH_VERSINFO[0]} -lt 4 ] && { echo "$WARNING_MSG"; return; }
  200. eval "$(env LANG=en _IDF.PY_COMPLETE=$SOURCE_BASH idf.py)" || echo "$WARNING_MSG"
  201. fi
  202. }
  203. __main && __enable_autocomplete
  204. __cleanup $?