export.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # This script should be sourced, not executed.
  2. realpath_int() {
  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. idf_export_main() {
  13. # The file doesn't have executable permissions, so this shouldn't really happen.
  14. # Doing this in case someone tries to chmod +x it and execute...
  15. # shellcheck disable=SC2128,SC2169,SC2039 # ignore array expansion warning
  16. if [ -n "${BASH_SOURCE}" ] && [ "${BASH_SOURCE[0]}" = "${0}" ]
  17. then
  18. echo "This script should be sourced, not executed:"
  19. # shellcheck disable=SC2039 # reachable only with bash
  20. echo ". ${BASH_SOURCE[0]}"
  21. return 1
  22. fi
  23. if [ -z "${IDF_PATH}" ]
  24. then
  25. # IDF_PATH not set in the environment.
  26. # If using bash or zsh, try to guess IDF_PATH from script location.
  27. self_path=""
  28. # shellcheck disable=SC2128 # ignore array expansion warning
  29. if [ -n "${BASH_SOURCE}" ]
  30. then
  31. self_path="${BASH_SOURCE}"
  32. elif [ -n "${ZSH_VERSION}" ]
  33. then
  34. self_path="${(%):-%x}"
  35. else
  36. echo "Could not detect IDF_PATH. Please set it before sourcing this script:"
  37. echo " export IDF_PATH=(add path here)"
  38. return 1
  39. fi
  40. # shellcheck disable=SC2169,SC2169,SC2039 # unreachable with 'dash'
  41. if [[ "$OSTYPE" == "darwin"* ]]; then
  42. # convert possibly relative path to absolute
  43. script_dir="$(realpath_int "${self_path}")"
  44. # resolve any ../ references to make the path shorter
  45. script_dir="$(cd "${script_dir}" || exit 1; pwd)"
  46. else
  47. # convert to full path and get the directory name of that
  48. script_name="$(readlink -f "${self_path}")"
  49. script_dir="$(dirname "${script_name}")"
  50. fi
  51. export IDF_PATH="${script_dir}"
  52. echo "Setting IDF_PATH to '${IDF_PATH}'"
  53. else
  54. # IDF_PATH came from the environment, check if the path is valid
  55. if [ ! -d "${IDF_PATH}" ]
  56. then
  57. echo "IDF_PATH is set to '${IDF_PATH}', but it is not a valid directory."
  58. echo "If you have set IDF_PATH manually, check if the path is correct."
  59. return 1
  60. fi
  61. # Check if this path looks like an IDF directory
  62. if [ ! -f "${IDF_PATH}/tools/idf.py" ] || [ ! -f "${IDF_PATH}/tools/idf_tools.py" ]
  63. then
  64. echo "IDF_PATH is set to '${IDF_PATH}', but it doesn't look like an ESP-IDF directory."
  65. echo "If you have set IDF_PATH manually, check if the path is correct."
  66. return 1
  67. fi
  68. # The varible might have been set (rather than exported), re-export it to be sure
  69. export IDF_PATH="${IDF_PATH}"
  70. fi
  71. old_path="$PATH"
  72. echo "Detecting the Python interpreter"
  73. . "${IDF_PATH}/tools/detect_python.sh"
  74. echo "Adding ESP-IDF tools to PATH..."
  75. # Call idf_tools.py to export tool paths
  76. export IDF_TOOLS_EXPORT_CMD=${IDF_PATH}/export.sh
  77. export IDF_TOOLS_INSTALL_CMD=${IDF_PATH}/install.sh
  78. idf_exports=$("$ESP_PYTHON" "${IDF_PATH}/tools/idf_tools.py" export) || return 1
  79. eval "${idf_exports}"
  80. echo "Checking if Python packages are up to date..."
  81. python "${IDF_PATH}/tools/check_python_dependencies.py" || return 1
  82. # Allow calling some IDF python tools without specifying the full path
  83. # ${IDF_PATH}/tools is already added by 'idf_tools.py export'
  84. IDF_ADD_PATHS_EXTRAS="${IDF_PATH}/components/esptool_py/esptool"
  85. IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/espcoredump"
  86. IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/partition_table/"
  87. export PATH="${IDF_ADD_PATHS_EXTRAS}:${PATH}"
  88. if [ -n "$BASH" ]
  89. then
  90. path_prefix=${PATH%%${old_path}}
  91. # shellcheck disable=SC2169,SC2039 # unreachable with 'dash'
  92. paths="${path_prefix//:/ }"
  93. if [ -n "${paths}" ]; then
  94. echo "Added the following directories to PATH:"
  95. else
  96. echo "All paths are already set."
  97. fi
  98. for path_entry in ${paths}
  99. do
  100. echo " ${path_entry}"
  101. done
  102. else
  103. echo "Updated PATH variable:"
  104. echo " ${PATH}"
  105. fi
  106. # Clean up
  107. unset old_path
  108. unset paths
  109. unset path_prefix
  110. unset path_entry
  111. unset IDF_ADD_PATHS_EXTRAS
  112. unset idf_exports
  113. unset ESP_PYTHON
  114. # Not unsetting IDF_PYTHON_ENV_PATH, it can be used by IDF build system
  115. # to check whether we are using a private Python environment
  116. echo "Done! You can now compile ESP-IDF projects."
  117. echo "Go to the project directory and run:"
  118. echo ""
  119. echo " idf.py build"
  120. echo ""
  121. }
  122. idf_export_main
  123. unset realpath_int
  124. unset idf_export_main