export.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # This script should be sourced, not executed.
  2. function 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. function 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. if [[ -n "${BASH_SOURCE}" && ( "${BASH_SOURCE[0]}" == "${0}" ) ]]; then
  16. echo "This script should be sourced, not executed:"
  17. echo ". ${BASH_SOURCE[0]}"
  18. return 1
  19. fi
  20. if [[ -z "${IDF_PATH}" ]]
  21. then
  22. # If using bash, try to guess IDF_PATH from script location
  23. if [[ -n "${BASH_SOURCE}" ]]
  24. then
  25. if [[ "$OSTYPE" == "darwin"* ]]; then
  26. script_dir="$(realpath_int $BASH_SOURCE)"
  27. else
  28. script_name="$(readlink -f $BASH_SOURCE)"
  29. script_dir="$(dirname $script_name)"
  30. fi
  31. export IDF_PATH="${script_dir}"
  32. else
  33. echo "IDF_PATH must be set before sourcing this script"
  34. return 1
  35. fi
  36. fi
  37. old_path=$PATH
  38. echo "Adding ESP-IDF tools to PATH..."
  39. # Call idf_tools.py to export tool paths
  40. export IDF_TOOLS_EXPORT_CMD=${IDF_PATH}/export.sh
  41. export IDF_TOOLS_INSTALL_CMD=${IDF_PATH}/install.sh
  42. idf_exports=$(${IDF_PATH}/tools/idf_tools.py export) || return 1
  43. eval "${idf_exports}"
  44. echo "Checking if Python packages are up to date..."
  45. python ${IDF_PATH}/tools/check_python_dependencies.py || return 1
  46. # Allow calling some IDF python tools without specifying the full path
  47. # ${IDF_PATH}/tools is already added by 'idf_tools.py export'
  48. IDF_ADD_PATHS_EXTRAS="${IDF_PATH}/components/esptool_py/esptool"
  49. IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/espcoredump"
  50. IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/partition_table/"
  51. export PATH="${IDF_ADD_PATHS_EXTRAS}:${PATH}"
  52. if [[ -n "$BASH" ]]
  53. then
  54. path_prefix=${PATH%%${old_path}}
  55. paths="${path_prefix//:/ }"
  56. if [ -n "${paths}" ]; then
  57. echo "Added the following directories to PATH:"
  58. else
  59. echo "All paths are already set."
  60. fi
  61. for path_entry in ${paths}
  62. do
  63. echo " ${path_entry}"
  64. done
  65. else
  66. echo "Updated PATH variable:"
  67. echo " ${PATH}"
  68. fi
  69. # Clean up
  70. unset old_path
  71. unset paths
  72. unset path_prefix
  73. unset path_entry
  74. unset IDF_ADD_PATHS_EXTRAS
  75. unset idf_exports
  76. # Not unsetting IDF_PYTHON_ENV_PATH, it can be used by IDF build system
  77. # to check whether we are using a private Python environment
  78. echo "Done! You can now compile ESP-IDF projects."
  79. echo "Go to the project directory and run:"
  80. echo ""
  81. echo " idf.py build"
  82. echo ""
  83. }
  84. idf_export_main
  85. unset realpath_int
  86. unset idf_export_main