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