export.sh 4.4 KB

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