export.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. __verbose "Adding ESP-IDF tools to PATH..."
  79. # Call idf_tools.py to export tool paths
  80. export IDF_TOOLS_EXPORT_CMD=${IDF_PATH}/export.sh
  81. export IDF_TOOLS_INSTALL_CMD=${IDF_PATH}/install.sh
  82. idf_exports=$("$ESP_PYTHON" "${IDF_PATH}/tools/idf_tools.py" export) || return 1
  83. eval "${idf_exports}"
  84. __verbose "Using Python interpreter in $(which python)"
  85. __verbose "Checking if Python packages are up to date..."
  86. python "${IDF_PATH}/tools/check_python_dependencies.py" || return 1
  87. # Allow calling some IDF python tools without specifying the full path
  88. # ${IDF_PATH}/tools is already added by 'idf_tools.py export'
  89. IDF_ADD_PATHS_EXTRAS="${IDF_PATH}/components/esptool_py/esptool"
  90. IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/espcoredump"
  91. IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/partition_table"
  92. IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/app_update"
  93. export PATH="${IDF_ADD_PATHS_EXTRAS}:${PATH}"
  94. if [ -n "$BASH" ]
  95. then
  96. path_prefix=${PATH%%${old_path}}
  97. # shellcheck disable=SC2169,SC2039 # unreachable with 'dash'
  98. if [ -n "${path_prefix}" ]; then
  99. __verbose "Added the following directories to PATH:"
  100. else
  101. __verbose "All paths are already set."
  102. fi
  103. old_ifs="$IFS"
  104. IFS=":"
  105. for path_entry in ${path_prefix}
  106. do
  107. __verbose " ${path_entry}"
  108. done
  109. IFS="$old_ifs"
  110. unset old_ifs
  111. else
  112. __verbose "Updated PATH variable:"
  113. __verbose " ${PATH}"
  114. fi
  115. __verbose "Done! You can now compile ESP-IDF projects."
  116. __verbose "Go to the project directory and run:"
  117. __verbose ""
  118. __verbose " idf.py build"
  119. __verbose ""
  120. }
  121. __cleanup() {
  122. unset old_path
  123. unset paths
  124. unset path_prefix
  125. unset path_entry
  126. unset IDF_ADD_PATHS_EXTRAS
  127. unset idf_exports
  128. unset ESP_PYTHON
  129. unset SOURCE_ZSH
  130. unset SOURCE_BASH
  131. unset WARNING_MSG
  132. unset __realpath
  133. unset __main
  134. unset __verbose
  135. unset __enable_autocomplete
  136. unset __cleanup
  137. # Not unsetting IDF_PYTHON_ENV_PATH, it can be used by IDF build system
  138. # to check whether we are using a private Python environment
  139. return $1
  140. }
  141. __enable_autocomplete() {
  142. click_version="$(python -c 'import click; print(click.__version__.split(".")[0])')"
  143. if [[ click_version -lt 8 ]]
  144. then
  145. SOURCE_ZSH=source_zsh
  146. SOURCE_BASH=source_bash
  147. else
  148. SOURCE_ZSH=zsh_source
  149. SOURCE_BASH=bash_source
  150. fi
  151. if [ -n "${ZSH_VERSION-}" ]
  152. then
  153. autoload -Uz compinit && compinit -u
  154. eval "$(env _IDF.PY_COMPLETE=$SOURCE_ZSH idf.py)" || echo "WARNING: Failed to load shell autocompletion for zsh version: $ZSH_VERSION!"
  155. elif [ -n "${BASH_SOURCE-}" ]
  156. then
  157. WARNING_MSG="WARNING: Failed to load shell autocompletion for bash version: $BASH_VERSION!"
  158. [[ ${BASH_VERSINFO[0]} -lt 4 ]] && { echo "$WARNING_MSG"; return; }
  159. eval "$(env LANG=en _IDF.PY_COMPLETE=$SOURCE_BASH idf.py)" || echo "$WARNING_MSG"
  160. fi
  161. }
  162. __main && __enable_autocomplete
  163. __cleanup $?