1# This script should be sourced, not executed. 2 3__realpath() { 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__verbose() { 16 [ -n "${IDF_EXPORT_QUIET}" ] && return 17 echo "$@" 18} 19 20__main() { 21 # The file doesn't have executable permissions, so this shouldn't really happen. 22 # Doing this in case someone tries to chmod +x it and execute... 23 24 # shellcheck disable=SC2128,SC2169,SC2039 # ignore array expansion warning 25 if [ -n "${BASH_SOURCE-}" ] && [ "${BASH_SOURCE[0]}" = "${0}" ] 26 then 27 echo "This script should be sourced, not executed:" 28 # shellcheck disable=SC2039 # reachable only with bash 29 echo ". ${BASH_SOURCE[0]}" 30 return 1 31 fi 32 33 if [ -z "${IDF_PATH}" ] 34 then 35 # IDF_PATH not set in the environment. 36 # If using bash or zsh, try to guess IDF_PATH from script location. 37 self_path="" 38 39 # shellcheck disable=SC2128 # ignore array expansion warning 40 if [ -n "${BASH_SOURCE-}" ] 41 then 42 self_path="${BASH_SOURCE}" 43 elif [ -n "${ZSH_VERSION-}" ] 44 then 45 self_path="${(%):-%x}" 46 else 47 echo "Could not detect IDF_PATH. Please set it before sourcing this script:" 48 echo " export IDF_PATH=(add path here)" 49 return 1 50 fi 51 52 # shellcheck disable=SC2169,SC2169,SC2039 # unreachable with 'dash' 53 if [[ "$OSTYPE" == "darwin"* ]]; then 54 # convert possibly relative path to absolute 55 script_dir="$(__realpath "${self_path}")" 56 # resolve any ../ references to make the path shorter 57 script_dir="$(cd "${script_dir}" || exit 1; pwd)" 58 else 59 # convert to full path and get the directory name of that 60 script_name="$(readlink -f "${self_path}")" 61 script_dir="$(dirname "${script_name}")" 62 fi 63 export IDF_PATH="${script_dir}" 64 echo "Setting IDF_PATH to '${IDF_PATH}'" 65 else 66 # IDF_PATH came from the environment, check if the path is valid 67 if [ ! -d "${IDF_PATH}" ] 68 then 69 echo "IDF_PATH is set to '${IDF_PATH}', but it is not a valid directory." 70 echo "If you have set IDF_PATH manually, check if the path is correct." 71 return 1 72 fi 73 # Check if this path looks like an IDF directory 74 if [ ! -f "${IDF_PATH}/tools/idf.py" ] || [ ! -f "${IDF_PATH}/tools/idf_tools.py" ] 75 then 76 echo "IDF_PATH is set to '${IDF_PATH}', but it doesn't look like an ESP-IDF directory." 77 echo "If you have set IDF_PATH manually, check if the path is correct." 78 return 1 79 fi 80 81 # The varible might have been set (rather than exported), re-export it to be sure 82 export IDF_PATH="${IDF_PATH}" 83 fi 84 85 old_path="$PATH" 86 87 echo "Detecting the Python interpreter" 88 . "${IDF_PATH}/tools/detect_python.sh" 89 90 __verbose "Adding ESP-IDF tools to PATH..." 91 # Call idf_tools.py to export tool paths 92 export IDF_TOOLS_EXPORT_CMD=${IDF_PATH}/export.sh 93 export IDF_TOOLS_INSTALL_CMD=${IDF_PATH}/install.sh 94 idf_exports=$("$ESP_PYTHON" "${IDF_PATH}/tools/idf_tools.py" export) || return 1 95 eval "${idf_exports}" 96 97 __verbose "Using Python interpreter in $(which python)" 98 __verbose "Checking if Python packages are up to date..." 99 python "${IDF_PATH}/tools/check_python_dependencies.py" || return 1 100 101 102 # Allow calling some IDF python tools without specifying the full path 103 # ${IDF_PATH}/tools is already added by 'idf_tools.py export' 104 IDF_ADD_PATHS_EXTRAS="${IDF_PATH}/components/esptool_py/esptool" 105 IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/espcoredump" 106 IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/partition_table" 107 IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/app_update" 108 export PATH="${IDF_ADD_PATHS_EXTRAS}:${PATH}" 109 110 if [ -n "$BASH" ] 111 then 112 path_prefix=${PATH%%${old_path}} 113 # shellcheck disable=SC2169,SC2039 # unreachable with 'dash' 114 if [ -n "${path_prefix}" ]; then 115 __verbose "Added the following directories to PATH:" 116 else 117 __verbose "All paths are already set." 118 fi 119 old_ifs="$IFS" 120 IFS=":" 121 for path_entry in ${path_prefix} 122 do 123 __verbose " ${path_entry}" 124 done 125 IFS="$old_ifs" 126 unset old_ifs 127 else 128 __verbose "Updated PATH variable:" 129 __verbose " ${PATH}" 130 fi 131 132 133 __verbose "Done! You can now compile ESP-IDF projects." 134 __verbose "Go to the project directory and run:" 135 __verbose "" 136 __verbose " idf.py build" 137 __verbose "" 138} 139 140__cleanup() { 141 unset old_path 142 unset paths 143 unset path_prefix 144 unset path_entry 145 unset IDF_ADD_PATHS_EXTRAS 146 unset idf_exports 147 unset ESP_PYTHON 148 unset SOURCE_ZSH 149 unset SOURCE_BASH 150 unset WARNING_MSG 151 152 unset __realpath 153 unset __main 154 unset __verbose 155 unset __enable_autocomplete 156 unset __cleanup 157 158 # Not unsetting IDF_PYTHON_ENV_PATH, it can be used by IDF build system 159 # to check whether we are using a private Python environment 160 161 return $1 162} 163 164 165__enable_autocomplete() { 166 click_version="$(python -c 'import click; print(click.__version__.split(".")[0])')" 167 if [[ click_version -lt 8 ]] 168 then 169 SOURCE_ZSH=source_zsh 170 SOURCE_BASH=source_bash 171 else 172 SOURCE_ZSH=zsh_source 173 SOURCE_BASH=bash_source 174 fi 175 if [ -n "${ZSH_VERSION-}" ] 176 then 177 autoload -Uz compinit && compinit -u 178 eval "$(env _IDF.PY_COMPLETE=$SOURCE_ZSH idf.py)" || echo "WARNING: Failed to load shell autocompletion for zsh version: $ZSH_VERSION!" 179 elif [ -n "${BASH_SOURCE-}" ] 180 then 181 WARNING_MSG="WARNING: Failed to load shell autocompletion for bash version: $BASH_VERSION!" 182 [[ ${BASH_VERSINFO[0]} -lt 4 ]] && { echo "$WARNING_MSG"; return; } 183 eval "$(env LANG=en _IDF.PY_COMPLETE=$SOURCE_BASH idf.py)" || echo "$WARNING_MSG" 184 fi 185} 186 187__main && __enable_autocomplete 188__cleanup $? 189