1# This script should be sourced, not executed.
2
3realpath_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
15idf_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 "Detecting the Python interpreter"
83    . "${IDF_PATH}/tools/detect_python.sh"
84
85    echo "Adding ESP-IDF tools to PATH..."
86    # Call idf_tools.py to export tool paths
87    export IDF_TOOLS_EXPORT_CMD=${IDF_PATH}/export.sh
88    export IDF_TOOLS_INSTALL_CMD=${IDF_PATH}/install.sh
89    idf_exports=$("$ESP_PYTHON" "${IDF_PATH}/tools/idf_tools.py" export) || return 1
90    eval "${idf_exports}"
91
92    echo "Using Python interpreter in $(which python)"
93    echo "Checking if Python packages are up to date..."
94    python "${IDF_PATH}/tools/check_python_dependencies.py" || return 1
95
96
97    # Allow calling some IDF python tools without specifying the full path
98    # ${IDF_PATH}/tools is already added by 'idf_tools.py export'
99    IDF_ADD_PATHS_EXTRAS="${IDF_PATH}/components/esptool_py/esptool"
100    IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/espcoredump"
101    IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/partition_table"
102    IDF_ADD_PATHS_EXTRAS="${IDF_ADD_PATHS_EXTRAS}:${IDF_PATH}/components/app_update"
103    export PATH="${IDF_ADD_PATHS_EXTRAS}:${PATH}"
104
105    if [ -n "$BASH" ]
106    then
107        path_prefix=${PATH%%${old_path}}
108        # shellcheck disable=SC2169,SC2039  # unreachable with 'dash'
109        paths="${path_prefix//:/ }"
110        if [ -n "${paths}" ]; then
111            echo "Added the following directories to PATH:"
112        else
113            echo "All paths are already set."
114        fi
115        for path_entry in ${paths}
116        do
117            echo "  ${path_entry}"
118        done
119    else
120        echo "Updated PATH variable:"
121        echo "  ${PATH}"
122    fi
123
124    # Clean up
125    unset old_path
126    unset paths
127    unset path_prefix
128    unset path_entry
129    unset IDF_ADD_PATHS_EXTRAS
130    unset idf_exports
131    unset ESP_PYTHON
132
133    # Not unsetting IDF_PYTHON_ENV_PATH, it can be used by IDF build system
134    # to check whether we are using a private Python environment
135
136    echo "Done! You can now compile ESP-IDF projects."
137    echo "Go to the project directory and run:"
138    echo ""
139    echo "  idf.py build"
140    echo ""
141}
142
143enable_autocomplete() {
144    if [ -n "$ZSH_VERSION" ]
145    then
146        autoload -Uz compinit && compinit -u
147        eval "$(env _IDF.PY_COMPLETE=source_zsh idf.py)" || echo "WARNING: Failed to load shell autocompletion!"
148    else
149        eval "$(env _IDF.PY_COMPLETE=source_bash idf.py)"  || echo "WARNING: Failed to load shell autocompletion!"
150    fi
151}
152
153idf_export_main
154enable_autocomplete
155
156unset realpath_int
157unset idf_export_main
158unset enable_autocomplete
159