export.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_deactivate=$("$ESP_PYTHON" "${IDF_PATH}/tools/idf_tools.py" export --deactivate) || return 1
  104. eval "${idf_deactivate}"
  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/espcoredump"
  112. IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/partition_table"
  113. IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/app_update"
  114. idf_exports=$("$ESP_PYTHON" "${IDF_PATH}/tools/idf_tools.py" export "--add_paths_extras=${IDF_ADD_PATHS_EXTRAS}") || return 1
  115. eval "${idf_exports}"
  116. export PATH="${IDF_ADD_PATHS_EXTRAS}:${PATH}"
  117. __verbose "Checking if Python packages are up to date..."
  118. "$ESP_PYTHON" "${IDF_PATH}/tools/idf_tools.py" check-python-dependencies || return 1
  119. if [ -n "$BASH" ]
  120. then
  121. path_prefix=${PATH%%${old_path}}
  122. # shellcheck disable=SC2169,SC2039 # unreachable with 'dash'
  123. if [ -n "${path_prefix}" ]; then
  124. __verbose "Added the following directories to PATH:"
  125. else
  126. __verbose "All paths are already set."
  127. fi
  128. old_ifs="$IFS"
  129. IFS=":"
  130. for path_entry in ${path_prefix}
  131. do
  132. __verbose " ${path_entry}"
  133. done
  134. IFS="$old_ifs"
  135. unset old_ifs
  136. else
  137. __verbose "Updated PATH variable:"
  138. __verbose " ${PATH}"
  139. fi
  140. uninstall=$("$ESP_PYTHON" "${IDF_PATH}/tools/idf_tools.py" uninstall --dry-run) || return 1
  141. if [ -n "$uninstall" ]
  142. then
  143. __verbose ""
  144. __verbose "Detected installed tools that are not currently used by active ESP-IDF version."
  145. __verbose "${uninstall}"
  146. __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'."
  147. __verbose ""
  148. fi
  149. __verbose "Done! You can now compile ESP-IDF projects."
  150. __verbose "Go to the project directory and run:"
  151. __verbose ""
  152. __verbose " idf.py build"
  153. __verbose ""
  154. }
  155. __cleanup() {
  156. unset old_path
  157. unset paths
  158. unset path_prefix
  159. unset path_entry
  160. unset IDF_ADD_PATHS_EXTRAS
  161. unset idf_exports
  162. unset idf_deactivate
  163. unset ESP_PYTHON
  164. unset SOURCE_ZSH
  165. unset SOURCE_BASH
  166. unset WARNING_MSG
  167. unset uninstall
  168. unset is_idf_path_esp_idf
  169. unset is_script_dir_esp_idf
  170. unset __realpath
  171. unset __main
  172. unset __verbose
  173. unset __enable_autocomplete
  174. unset __cleanup
  175. unset __is_dir_esp_idf
  176. # Not unsetting IDF_PYTHON_ENV_PATH, it can be used by IDF build system
  177. # to check whether we are using a private Python environment
  178. return $1
  179. }
  180. __enable_autocomplete() {
  181. click_version="$(python -c 'import click; print(click.__version__.split(".")[0])')"
  182. if [ "${click_version}" -lt 8 ]
  183. then
  184. SOURCE_ZSH=source_zsh
  185. SOURCE_BASH=source_bash
  186. else
  187. SOURCE_ZSH=zsh_source
  188. SOURCE_BASH=bash_source
  189. fi
  190. if [ -n "${ZSH_VERSION-}" ]
  191. then
  192. autoload -Uz compinit && compinit -u
  193. eval "$(env _IDF.PY_COMPLETE=$SOURCE_ZSH idf.py)" || echo "WARNING: Failed to load shell autocompletion for zsh version: $ZSH_VERSION!"
  194. elif [ -n "${BASH_SOURCE-}" ]
  195. then
  196. WARNING_MSG="WARNING: Failed to load shell autocompletion for bash version: $BASH_VERSION!"
  197. # shellcheck disable=SC3028,SC3054,SC2086 # code block for 'bash' only
  198. [ ${BASH_VERSINFO[0]} -lt 4 ] && { echo "$WARNING_MSG"; return; }
  199. eval "$(env LANG=en _IDF.PY_COMPLETE=$SOURCE_BASH idf.py)" || echo "$WARNING_MSG"
  200. fi
  201. }
  202. __main && __enable_autocomplete
  203. __cleanup $?