2019-04-28 22:37:02 -04:00
|
|
|
# This script should be sourced, not executed.
|
|
|
|
|
2020-08-31 19:00:57 -04:00
|
|
|
__realpath() {
|
2019-07-16 09:38:40 -04:00
|
|
|
wdir="$PWD"; [ "$PWD" = "/" ] && wdir=""
|
2019-07-28 04:04:36 -04:00
|
|
|
arg=$1
|
|
|
|
case "$arg" in
|
|
|
|
/*) scriptdir="${arg}";;
|
|
|
|
*) scriptdir="$wdir/${arg#./}";;
|
2019-07-16 09:38:40 -04:00
|
|
|
esac
|
|
|
|
scriptdir="${scriptdir%/*}"
|
|
|
|
echo "$scriptdir"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-31 19:00:57 -04:00
|
|
|
__verbose() {
|
2021-10-06 07:48:11 -04:00
|
|
|
[ -n "${IDF_EXPORT_QUIET}" ] && return
|
2020-08-31 19:00:57 -04:00
|
|
|
echo "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
__main() {
|
2019-04-28 22:37:02 -04:00
|
|
|
# The file doesn't have executable permissions, so this shouldn't really happen.
|
|
|
|
# Doing this in case someone tries to chmod +x it and execute...
|
2020-06-08 13:14:48 -04:00
|
|
|
|
|
|
|
# shellcheck disable=SC2128,SC2169,SC2039 # ignore array expansion warning
|
2021-02-28 17:26:06 -05:00
|
|
|
if [ -n "${BASH_SOURCE-}" ] && [ "${BASH_SOURCE[0]}" = "${0}" ]
|
2020-06-08 13:14:48 -04:00
|
|
|
then
|
2019-04-28 22:37:02 -04:00
|
|
|
echo "This script should be sourced, not executed:"
|
2020-06-08 13:14:48 -04:00
|
|
|
# shellcheck disable=SC2039 # reachable only with bash
|
2019-04-28 22:37:02 -04:00
|
|
|
echo ". ${BASH_SOURCE[0]}"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2020-06-08 13:14:48 -04:00
|
|
|
if [ -z "${IDF_PATH}" ]
|
2019-04-28 22:37:02 -04:00
|
|
|
then
|
2020-04-02 13:05:58 -04:00
|
|
|
# IDF_PATH not set in the environment.
|
|
|
|
# If using bash or zsh, try to guess IDF_PATH from script location.
|
|
|
|
self_path=""
|
2020-06-08 13:14:48 -04:00
|
|
|
|
|
|
|
# shellcheck disable=SC2128 # ignore array expansion warning
|
2021-02-28 17:26:06 -05:00
|
|
|
if [ -n "${BASH_SOURCE-}" ]
|
2019-04-28 22:37:02 -04:00
|
|
|
then
|
2020-04-02 13:05:58 -04:00
|
|
|
self_path="${BASH_SOURCE}"
|
2021-02-28 17:26:06 -05:00
|
|
|
elif [ -n "${ZSH_VERSION-}" ]
|
2020-04-02 13:05:58 -04:00
|
|
|
then
|
|
|
|
self_path="${(%):-%x}"
|
2019-04-28 22:37:02 -04:00
|
|
|
else
|
2020-04-02 13:05:58 -04:00
|
|
|
echo "Could not detect IDF_PATH. Please set it before sourcing this script:"
|
|
|
|
echo " export IDF_PATH=(add path here)"
|
2019-04-28 22:37:02 -04:00
|
|
|
return 1
|
|
|
|
fi
|
2020-04-02 13:05:58 -04:00
|
|
|
|
2020-06-08 13:14:48 -04:00
|
|
|
# shellcheck disable=SC2169,SC2169,SC2039 # unreachable with 'dash'
|
2020-04-02 13:05:58 -04:00
|
|
|
if [[ "$OSTYPE" == "darwin"* ]]; then
|
|
|
|
# convert possibly relative path to absolute
|
2021-10-14 03:44:16 -04:00
|
|
|
script_dir="$(__realpath "${self_path}")"
|
2020-04-02 13:05:58 -04:00
|
|
|
# resolve any ../ references to make the path shorter
|
2020-06-08 13:14:48 -04:00
|
|
|
script_dir="$(cd "${script_dir}" || exit 1; pwd)"
|
2020-04-02 13:05:58 -04:00
|
|
|
else
|
|
|
|
# convert to full path and get the directory name of that
|
2020-06-08 13:14:48 -04:00
|
|
|
script_name="$(readlink -f "${self_path}")"
|
|
|
|
script_dir="$(dirname "${script_name}")"
|
2020-04-02 13:05:58 -04:00
|
|
|
fi
|
|
|
|
export IDF_PATH="${script_dir}"
|
|
|
|
echo "Setting IDF_PATH to '${IDF_PATH}'"
|
|
|
|
else
|
|
|
|
# IDF_PATH came from the environment, check if the path is valid
|
2020-06-08 13:14:48 -04:00
|
|
|
if [ ! -d "${IDF_PATH}" ]
|
2020-04-02 13:05:58 -04:00
|
|
|
then
|
|
|
|
echo "IDF_PATH is set to '${IDF_PATH}', but it is not a valid directory."
|
|
|
|
echo "If you have set IDF_PATH manually, check if the path is correct."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
# Check if this path looks like an IDF directory
|
2020-06-08 13:14:48 -04:00
|
|
|
if [ ! -f "${IDF_PATH}/tools/idf.py" ] || [ ! -f "${IDF_PATH}/tools/idf_tools.py" ]
|
2020-04-02 13:05:58 -04:00
|
|
|
then
|
|
|
|
echo "IDF_PATH is set to '${IDF_PATH}', but it doesn't look like an ESP-IDF directory."
|
|
|
|
echo "If you have set IDF_PATH manually, check if the path is correct."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# The varible might have been set (rather than exported), re-export it to be sure
|
|
|
|
export IDF_PATH="${IDF_PATH}"
|
2019-04-28 22:37:02 -04:00
|
|
|
fi
|
|
|
|
|
2020-04-02 13:05:58 -04:00
|
|
|
old_path="$PATH"
|
2019-04-28 22:37:02 -04:00
|
|
|
|
2021-02-02 07:20:09 -05:00
|
|
|
echo "Detecting the Python interpreter"
|
|
|
|
. "${IDF_PATH}/tools/detect_python.sh"
|
|
|
|
|
2020-08-31 19:00:57 -04:00
|
|
|
__verbose "Adding ESP-IDF tools to PATH..."
|
2019-04-28 22:37:02 -04:00
|
|
|
# Call idf_tools.py to export tool paths
|
|
|
|
export IDF_TOOLS_EXPORT_CMD=${IDF_PATH}/export.sh
|
|
|
|
export IDF_TOOLS_INSTALL_CMD=${IDF_PATH}/install.sh
|
2021-02-02 07:20:09 -05:00
|
|
|
idf_exports=$("$ESP_PYTHON" "${IDF_PATH}/tools/idf_tools.py" export) || return 1
|
2019-04-28 22:37:02 -04:00
|
|
|
eval "${idf_exports}"
|
|
|
|
|
2020-08-31 19:00:57 -04:00
|
|
|
__verbose "Using Python interpreter in $(which python)"
|
|
|
|
__verbose "Checking if Python packages are up to date..."
|
2021-09-16 10:48:03 -04:00
|
|
|
python "${IDF_PATH}/tools/idf_tools.py" check-python-dependencies || return 1
|
2019-04-28 22:37:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Allow calling some IDF python tools without specifying the full path
|
|
|
|
# ${IDF_PATH}/tools is already added by 'idf_tools.py export'
|
|
|
|
IDF_ADD_PATHS_EXTRAS="${IDF_PATH}/components/esptool_py/esptool"
|
|
|
|
IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/espcoredump"
|
2020-04-02 13:06:48 -04:00
|
|
|
IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/partition_table"
|
|
|
|
IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/app_update"
|
2019-04-28 22:37:02 -04:00
|
|
|
export PATH="${IDF_ADD_PATHS_EXTRAS}:${PATH}"
|
|
|
|
|
2020-06-08 13:14:48 -04:00
|
|
|
if [ -n "$BASH" ]
|
2019-04-28 22:37:02 -04:00
|
|
|
then
|
|
|
|
path_prefix=${PATH%%${old_path}}
|
2020-06-08 13:14:48 -04:00
|
|
|
# shellcheck disable=SC2169,SC2039 # unreachable with 'dash'
|
2020-10-09 11:40:57 -04:00
|
|
|
if [ -n "${path_prefix}" ]; then
|
2020-08-31 19:00:57 -04:00
|
|
|
__verbose "Added the following directories to PATH:"
|
2019-04-28 22:37:02 -04:00
|
|
|
else
|
2020-08-31 19:00:57 -04:00
|
|
|
__verbose "All paths are already set."
|
2019-04-28 22:37:02 -04:00
|
|
|
fi
|
2020-10-09 11:40:57 -04:00
|
|
|
old_ifs="$IFS"
|
|
|
|
IFS=":"
|
|
|
|
for path_entry in ${path_prefix}
|
2019-04-28 22:37:02 -04:00
|
|
|
do
|
2020-08-31 19:00:57 -04:00
|
|
|
__verbose " ${path_entry}"
|
2019-04-28 22:37:02 -04:00
|
|
|
done
|
2020-10-09 11:40:57 -04:00
|
|
|
IFS="$old_ifs"
|
|
|
|
unset old_ifs
|
2019-04-28 22:37:02 -04:00
|
|
|
else
|
2020-08-31 19:00:57 -04:00
|
|
|
__verbose "Updated PATH variable:"
|
|
|
|
__verbose " ${PATH}"
|
2019-04-28 22:37:02 -04:00
|
|
|
fi
|
|
|
|
|
2020-08-17 15:08:32 -04:00
|
|
|
|
|
|
|
__verbose "Done! You can now compile ESP-IDF projects."
|
|
|
|
__verbose "Go to the project directory and run:"
|
|
|
|
__verbose ""
|
|
|
|
__verbose " idf.py build"
|
|
|
|
__verbose ""
|
|
|
|
}
|
|
|
|
|
|
|
|
__cleanup() {
|
2019-04-28 22:37:02 -04:00
|
|
|
unset old_path
|
|
|
|
unset paths
|
|
|
|
unset path_prefix
|
|
|
|
unset path_entry
|
|
|
|
unset IDF_ADD_PATHS_EXTRAS
|
|
|
|
unset idf_exports
|
2021-02-02 07:20:09 -05:00
|
|
|
unset ESP_PYTHON
|
2021-10-11 08:11:09 -04:00
|
|
|
unset SOURCE_ZSH
|
|
|
|
unset SOURCE_BASH
|
2021-11-26 09:16:47 -05:00
|
|
|
unset WARNING_MSG
|
2019-07-16 09:38:40 -04:00
|
|
|
|
2020-08-17 15:08:32 -04:00
|
|
|
unset __realpath
|
|
|
|
unset __main
|
|
|
|
unset __verbose
|
|
|
|
unset __enable_autocomplete
|
|
|
|
unset __cleanup
|
|
|
|
|
2019-04-28 22:37:02 -04:00
|
|
|
# Not unsetting IDF_PYTHON_ENV_PATH, it can be used by IDF build system
|
|
|
|
# to check whether we are using a private Python environment
|
|
|
|
|
2020-08-17 15:08:32 -04:00
|
|
|
return $1
|
2019-04-28 22:37:02 -04:00
|
|
|
}
|
|
|
|
|
2020-08-17 15:08:32 -04:00
|
|
|
|
2021-10-06 07:48:11 -04:00
|
|
|
__enable_autocomplete() {
|
2021-05-24 08:11:56 -04:00
|
|
|
click_version="$(python -c 'import click; print(click.__version__.split(".")[0])')"
|
|
|
|
if [[ click_version -lt 8 ]]
|
|
|
|
then
|
|
|
|
SOURCE_ZSH=source_zsh
|
|
|
|
SOURCE_BASH=source_bash
|
|
|
|
else
|
|
|
|
SOURCE_ZSH=zsh_source
|
|
|
|
SOURCE_BASH=bash_source
|
|
|
|
fi
|
2021-02-21 00:09:30 -05:00
|
|
|
if [ -n "${ZSH_VERSION-}" ]
|
2020-11-02 08:11:14 -05:00
|
|
|
then
|
|
|
|
autoload -Uz compinit && compinit -u
|
2021-06-10 03:44:38 -04:00
|
|
|
eval "$(env _IDF.PY_COMPLETE=$SOURCE_ZSH idf.py)" || echo "WARNING: Failed to load shell autocompletion for zsh version: $ZSH_VERSION!"
|
2021-02-28 17:26:06 -05:00
|
|
|
elif [ -n "${BASH_SOURCE-}" ]
|
|
|
|
then
|
2021-11-26 09:16:47 -05:00
|
|
|
WARNING_MSG="WARNING: Failed to load shell autocompletion for bash version: $BASH_VERSION!"
|
|
|
|
[[ ${BASH_VERSINFO[0]} -lt 4 ]] && { echo "$WARNING_MSG"; return; }
|
|
|
|
eval "$(env LANG=en _IDF.PY_COMPLETE=$SOURCE_BASH idf.py)" || echo "$WARNING_MSG"
|
2020-11-02 08:11:14 -05:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-10-11 08:11:09 -04:00
|
|
|
__main && __enable_autocomplete
|
2020-08-17 15:08:32 -04:00
|
|
|
__cleanup $?
|