Debian Codegraph @

env/sh/vars.sh.in

raw
1 #!/bin/sh
2 # shellcheck shell=sh
3 ###############################################################################
4 # Copyright (C) Intel Corporation
5 #
6 # SPDX-License-Identifier: MIT
7 ###############################################################################
8 # Configure environment variables
9
10 # Get absolute path to this script.
11 # Uses `readlink` to remove links and `pwd -P` to turn into an absolute path.
12
13 # Usage:
14 # script_dir=$(get_script_path "$script_rel_path")
15 #
16 # Inputs:
17 # script/relative/pathname/scriptname
18 #
19 # Outputs:
20 # /script/absolute/pathname
21
22 # executing function in a *subshell* to localize vars and effects on `cd`
23 get_script_path() (
24 script="$1"
25 while [ -L "$script" ] ; do
26 # combining next two lines fails in zsh shell
27 script_dir=$(command dirname -- "$script")
28 script_dir=$(cd "$script_dir" && command pwd -P)
29 script="$(readlink "$script")"
30 case $script in
31 (/*) ;;
32 (*) script="$script_dir/$script" ;;
33 esac
34 done
35 # combining next two lines fails in zsh shell
36 script_dir=$(command dirname -- "$script")
37 script_dir=$(cd "$script_dir" && command pwd -P)
38 printf "%s" "$script_dir"
39 )
40
41
42 # ############################################################################
43
44 # Determine if we are being executed or sourced. Need to detect being sourced
45 # within an executed script, which can happen on a CI system. We also must
46 # detect being sourced at a shell prompt (CLI). The setvars.sh script will
47 # always source this script, but this script can also be called directly.
48
49 # We are assuming we know the name of this script, which is a reasonable
50 # assumption. This script _must_ be named "vars.sh" or it will not work
51 # with the top-level setvars.sh script. Making this assumption simplifies
52 # the process of detecting if the script has been sourced or executed. It
53 # also simplifies the process of detecting the location of this script.
54
55 # Using `readlink` to remove possible symlinks in the name of the script.
56 # Also, "ps -o comm=" is limited to a 15 character result, but it works
57 # fine here, because we are only looking for the name of this script or the
58 # name of the execution shell, both always fit into fifteen characters.
59
60 # TODO: Edge cases exist when executed by way of "/bin/sh setvars.sh"
61 # Most shells detect or fall thru to error message, sometimes ksh does not.
62 # This is an odd and unusual situation; not a high priority issue.
63
64 _vars_get_proc_name() {
65 if [ -n "${ZSH_VERSION:-}" ] ; then
66 script="$(ps -p "$$" -o comm=)"
67 else
68 script="$1"
69 while [ -L "$script" ] ; do
70 script="$(readlink "$script")"
71 done
72 fi
73 basename -- "$script"
74 }
75
76 _vars_this_script_name="vars.sh"
77 if [ "$_vars_this_script_name" = "$(_vars_get_proc_name "$0")" ] ; then
78 echo " ERROR: Incorrect usage: this script must be sourced."
79 echo " Usage: . path/to/${_vars_this_script_name}"
80 return 255 2>/dev/null || exit 255
81 fi
82
83
84 # ############################################################################
85
86 # Prepend path segment(s) to path-like env vars (PATH, CPATH, etc.).
87
88 # prepend_path() avoids dangling ":" that affects some env vars (PATH and CPATH)
89 # prepend_manpath() includes dangling ":" needed by MANPATH.
90 # PATH > https://www.gnu.org/software/libc/manual/html_node/Standard-Environment.html
91 # MANPATH > https://manpages.debian.org/stretch/man-db/manpath.1.en.html
92
93 # Usage:
94 # env_var=$(prepend_path "$prepend_to_var" "$existing_env_var")
95 # export env_var
96 #
97 # env_var=$(prepend_manpath "$prepend_to_var" "$existing_env_var")
98 # export env_var
99 #
100 # Inputs:
101 # $1 == path segment to be prepended to $2
102 # $2 == value of existing path-like environment variable
103
104 prepend_path() (
105 path_to_add="$1"
106 path_is_now="$2"
107
108 if [ "" = "${path_is_now}" ] ; then # avoid dangling ":"
109 printf "%s" "${path_to_add}"
110 else
111 printf "%s" "${path_to_add}:${path_is_now}"
112 fi
113 )
114
115 prepend_manpath() (
116 path_to_add="$1"
117 path_is_now="$2"
118
119 if [ "" = "${path_is_now}" ] ; then # include dangling ":"
120 printf "%s" "${path_to_add}:"
121 else
122 printf "%s" "${path_to_add}:${path_is_now}"
123 fi
124 )
125
126
127 # ############################################################################
128
129 # Extract the name and location of this sourced script.
130
131 # Generally, "ps -o comm=" is limited to a 15 character result, but it works
132 # fine for this usage, because we are primarily interested in finding the name
133 # of the execution shell, not the name of any calling script.
134
135 vars_script_name=""
136 vars_script_shell="$(ps -p "$$" -o comm=)"
137 # ${var:-} needed to pass "set -eu" checks
138 # see https://unix.stackexchange.com/a/381465/103967
139 # see https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02
140 if [ -n "${ZSH_VERSION:-}" ] && [ -n "${ZSH_EVAL_CONTEXT:-}" ] ; then # zsh 5.x and later
141 # shellcheck disable=2249
142 case $ZSH_EVAL_CONTEXT in (*:file*) vars_script_name="${(%):-%x}" ;; esac ;
143 elif [ -n "${KSH_VERSION:-}" ] ; then # ksh, mksh or lksh
144 if [ "$(set | grep -Fq "KSH_VERSION=.sh.version" ; echo $?)" -eq 0 ] ; then # ksh
145 vars_script_name="${.sh.file}" ;
146 else # mksh or lksh or [lm]ksh masquerading as ksh or sh
147 # force [lm]ksh to issue error msg; which contains this script's path/filename, e.g.:
148 # mksh: path/to/vars.sh[137]: ${.sh.file}: bad substitution
149 vars_script_name="$( (echo "${.sh.file}") 2>&1 )" || : ;
150 vars_script_name="$(expr "${vars_script_name:-}" : '^.*sh: \(.*\)\[[0-9]*\]:')" ;
151 fi
152 elif [ -n "${BASH_VERSION:-}" ] ; then # bash
153 # shellcheck disable=2128
154 (return 0 2>/dev/null) && vars_script_name="${BASH_SOURCE}" ;
155 elif [ "dash" = "$vars_script_shell" ] ; then # dash
156 # force dash to issue error msg; which contains this script's rel/path/filename, e.g.:
157 # dash: 146: path/to/vars.sh: Bad substitution
158 vars_script_name="$( (echo "${.sh.file}") 2>&1 )" || : ;
159 vars_script_name="$(expr "${vars_script_name:-}" : '^.*dash: [0-9]*: \(.*\):')" ;
160 elif [ "sh" = "$vars_script_shell" ] ; then # could be dash masquerading as /bin/sh
161 # force a shell error msg; which should contain this script's path/filename
162 # sample error msg shown; assume this file is named "vars.sh"; as required by setvars.sh
163 vars_script_name="$( (echo "${.sh.file}") 2>&1 )" || : ;
164 if [ "$(printf "%s" "$vars_script_name" | grep -Eq "sh: [0-9]+: .*vars\.sh: " ; echo $?)" -eq 0 ] ; then # dash as sh
165 # sh: 155: path/to/vars.sh: Bad substitution
166 vars_script_name="$(expr "${vars_script_name:-}" : '^.*sh: [0-9]*: \(.*\):')" ;
167 fi
168 else # unrecognized shell or dash being sourced from within a user's script
169 # force a shell error msg; which should contain this script's path/filename
170 # sample error msg shown; assume this file is named "vars.sh"; as required by setvars.sh
171 vars_script_name="$( (echo "${.sh.file}") 2>&1 )" || : ;
172 if [ "$(printf "%s" "$vars_script_name" | grep -Eq "^.+: [0-9]+: .*vars\.sh: " ; echo $?)" -eq 0 ] ; then # dash
173 # .*: 164: path/to/vars.sh: Bad substitution
174 vars_script_name="$(expr "${vars_script_name:-}" : '^.*: [0-9]*: \(.*\):')" ;
175 else
176 vars_script_name="" ;
177 fi
178 fi
179
180 if [ "" = "$vars_script_name" ] ; then
181 >&2 echo " ERROR: Unable to proceed: possible causes listed below."
182 >&2 echo " This script must be sourced. Did you execute or source this script?" ;
183 >&2 echo " Unrecognized/unsupported shell (supported: bash, zsh, ksh, m/lksh, dash)." ;
184 >&2 echo " May fail in dash if you rename this script (assumes \"vars.sh\")." ;
185 >&2 echo " Can be caused by sourcing from ZSH version 4.x or older." ;
186 return 255 2>/dev/null || exit 255
187 fi
188
189
190 # ############################################################################
191
192 # Component-specific env var settings.
193
194 script_path=$(get_script_path "${vars_script_name:-}")
195
196 vpl_prefix="$script_path/@env_rel_prefix@"
197 vpl_prefix=$(cd "$vpl_prefix" && pwd)
198 vpl_include="$vpl_prefix/@_install_includedir@"
199 vpl_lib="$vpl_prefix/@_install_libdir@"
200 vpl_bin="$vpl_prefix/@_install_bindir@"
201 vpl_pkgconfig="$vpl_prefix/@_install_pkgconfigdir@"
202
203 CPATH=$(prepend_path "$vpl_include" "${CPATH:-}")
204 export CPATH
205 LIBRARY_PATH=$(prepend_path "$vpl_lib" "${LIBRARY_PATH:-}")
206 export LIBRARY_PATH
207 PATH=$(prepend_path "$vpl_bin" "${PATH:-}")
208 export PATH
209 LD_LIBRARY_PATH=$(prepend_path "$vpl_lib" "${LD_LIBRARY_PATH:-}")
210 export LD_LIBRARY_PATH
211 CMAKE_PREFIX_PATH=$(prepend_path "$vpl_prefix" "${CMAKE_PREFIX_PATH:-}")
212 export CMAKE_PREFIX_PATH
213 PKG_CONFIG_PATH=$(prepend_path "$vpl_pkgconfig" "${PKG_CONFIG_PATH:-}")
214 export PKG_CONFIG_PATH
215 VPL_EXAMPLES_PATH="$vpl_prefix/@_install_exampledir@"
216 export VPL_EXAMPLES_PATH