export.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "Adding ESP-IDF tools to PATH..."
  73. # Call idf_tools.py to export tool paths
  74. export IDF_TOOLS_EXPORT_CMD=${IDF_PATH}/export.sh
  75. export IDF_TOOLS_INSTALL_CMD=${IDF_PATH}/install.sh
  76. idf_exports=$("${IDF_PATH}/tools/idf_tools.py" export) || return 1
  77. eval "${idf_exports}"
  78. echo "Checking if Python packages are up to date..."
  79. python "${IDF_PATH}/tools/check_python_dependencies.py" || return 1
  80. # Allow calling some IDF python tools without specifying the full path
  81. # ${IDF_PATH}/tools is already added by 'idf_tools.py export'
  82. IDF_ADD_PATHS_EXTRAS="${IDF_PATH}/components/esptool_py/esptool"
  83. IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/espcoredump"
  84. IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/partition_table/"
  85. export PATH="${IDF_ADD_PATHS_EXTRAS}:${PATH}"
  86. if [ -n "$BASH" ]
  87. then
  88. path_prefix=${PATH%%${old_path}}
  89. # shellcheck disable=SC2169,SC2039 # unreachable with 'dash'
  90. paths="${path_prefix//:/ }"
  91. if [ -n "${paths}" ]; then
  92. echo "Added the following directories to PATH:"
  93. else
  94. echo "All paths are already set."
  95. fi
  96. for path_entry in ${paths}
  97. do
  98. echo " ${path_entry}"
  99. done
  100. else
  101. echo "Updated PATH variable:"
  102. echo " ${PATH}"
  103. fi
  104. # Clean up
  105. unset old_path
  106. unset paths
  107. unset path_prefix
  108. unset path_entry
  109. unset IDF_ADD_PATHS_EXTRAS
  110. unset idf_exports
  111. # Not unsetting IDF_PYTHON_ENV_PATH, it can be used by IDF build system
  112. # to check whether we are using a private Python environment
  113. echo "Done! You can now compile ESP-IDF projects."
  114. echo "Go to the project directory and run:"
  115. echo ""
  116. echo " idf.py build"
  117. echo ""
  118. }
  119. idf_export_main
  120. unset realpath_int
  121. unset idf_export_main