From 1c4c584e2845a34eb6d2e52e81fd5d3b9d0a73f3 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Thu, 2 Apr 2020 19:05:58 +0200 Subject: [PATCH] tools: export.sh: better detection and checking of IDF_PATH 1. detect IDF_PATH in zsh, if it is not set 2. if IDF_PATH is set, check that it is valid 3. make sure IDF_PATH is exported, not just set Related to https://github.com/espressif/esp-idf/issues/3793 Closes https://github.com/espressif/esp-idf/pull/4418 Closes https://github.com/espressif/esp-idf/issues/4770 --- export.sh | 51 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/export.sh b/export.sh index 0408a76bb..fda9b7e3a 100644 --- a/export.sh +++ b/export.sh @@ -23,23 +23,54 @@ function idf_export_main() { if [[ -z "${IDF_PATH}" ]] then - # If using bash, try to guess IDF_PATH from script location + # IDF_PATH not set in the environment. + # If using bash or zsh, try to guess IDF_PATH from script location. + self_path="" if [[ -n "${BASH_SOURCE}" ]] then - if [[ "$OSTYPE" == "darwin"* ]]; then - script_dir="$(realpath_int $BASH_SOURCE)" - else - script_name="$(readlink -f $BASH_SOURCE)" - script_dir="$(dirname $script_name)" - fi - export IDF_PATH="${script_dir}" + self_path="${BASH_SOURCE}" + elif [[ -n "${ZSH_VERSION}" ]] + then + self_path="${(%):-%x}" else - echo "IDF_PATH must be set before sourcing this script" + echo "Could not detect IDF_PATH. Please set it before sourcing this script:" + echo " export IDF_PATH=(add path here)" return 1 fi + + if [[ "$OSTYPE" == "darwin"* ]]; then + # convert possibly relative path to absolute + script_dir="$(realpath_int ${self_path})" + # resolve any ../ references to make the path shorter + script_dir="$(cd ${script_dir}; pwd)" + else + # convert to full path and get the directory name of that + script_name="$(readlink -f ${self_path})" + script_dir="$(dirname ${script_name})" + 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 + if [[ ! -d "${IDF_PATH}" ]] + 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 + if [[ ! -f "${IDF_PATH}/tools/idf.py" || ! -f "${IDF_PATH}/tools/idf_tools.py" ]] + 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}" fi - old_path=$PATH + old_path="$PATH" echo "Adding ESP-IDF tools to PATH..." # Call idf_tools.py to export tool paths