xref: /oneTBB/integration/linux/env/vars.sh (revision 5f1ae0dc)
1#!/bin/sh
2# shellcheck shell=sh
3#
4# Copyright (c) 2005-2023 Intel Corporation
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18# The script is setting up environment for oneTBB.
19# Supported arguments:
20#   intel64|ia32 - architecture, intel64 is default.
21
22# Get absolute path to script. Gets a relative path as argument and outputs an absolute path.
23get_script_path() (
24  script_path="$1"
25  while [ -L "$script_path" ] ; do
26    script_dir=$(command dirname -- "$script_path")
27    script_dir=$(cd "$script_dir" && command pwd -P)
28    script_path="$(readlink "$script_path")"
29    case $script_path in
30      (/*) ;;
31       (*) script_path="$script_dir/$script_path" ;;
32    esac
33  done
34  script_dir=$(command dirname -- "$script_path")
35  script_dir=$(cd "$script_dir" && command pwd -P)
36  printf "%s" "$script_dir"
37)
38
39_vars_get_proc_name() {
40  if [ -n "${ZSH_VERSION:-}" ] ; then
41    script="$(ps -p "$$" -o comm=)"
42  else
43    script="$1"
44    while [ -L "$script" ] ; do
45      script="$(readlink "$script")"
46    done
47  fi
48  basename -- "$script"
49}
50
51_vars_this_script_name="vars.sh"
52if [ "$_vars_this_script_name" = "$(_vars_get_proc_name "$0")" ] ; then
53  echo ":: ERROR: Incorrect usage: this script must be sourced."
54  echo "   Usage: . path/to/${_vars_this_script_name}"
55  return 255 2>/dev/null || exit 255
56fi
57
58# Prepend path segment(s) to path-like env vars (PATH, CPATH, etc.).
59
60# prepend_path() avoids dangling ":" that affects some env vars (PATH and CPATH)
61# PATH > https://www.gnu.org/software/libc/manual/html_node/Standard-Environment.html
62
63# Usage:
64#   env_var=$(prepend_path "$prepend_to_var" "$existing_env_var")
65#   export env_var
66#
67# Inputs:
68#   $1 == path segment to be prepended to $2
69#   $2 == value of existing path-like environment variable
70
71prepend_path() (
72  path_to_add="$1"
73  path_is_now="$2"
74
75  if [ "" = "${path_is_now}" ] ; then   # avoid dangling ":"
76    printf "%s" "${path_to_add}"
77  else
78    printf "%s" "${path_to_add}:${path_is_now}"
79  fi
80)
81
82# Extract the name and location of this sourced script.
83
84# Generally, "ps -o comm=" is limited to a 15 character result, but it works
85# fine for this usage, because we are primarily interested in finding the name
86# of the execution shell, not the name of any calling script.
87
88vars_script_name=""
89vars_script_shell="$(ps -p "$$" -o comm=)"
90# ${var:-} needed to pass "set -eu" checks
91if [ -n "${ZSH_VERSION:-}" ] && [ -n "${ZSH_EVAL_CONTEXT:-}" ] ; then     # zsh 5.x and later
92  # shellcheck disable=2249
93  case $ZSH_EVAL_CONTEXT in (*:file*) vars_script_name="${(%):-%x}" ;; esac ;
94elif [ -n "${KSH_VERSION:-}" ] ; then                                     # ksh, mksh or lksh
95  if [ "$(set | grep -Fq "KSH_VERSION=.sh.version" ; echo $?)" -eq 0 ] ; then # ksh
96    vars_script_name="${.sh.file}" ;
97  else # mksh or lksh or [lm]ksh masquerading as ksh or sh
98    # force [lm]ksh to issue error msg; which contains this script's path/filename, e.g.:
99    # mksh: /home/ubuntu/intel/oneapi/vars.sh[137]: ${.sh.file}: bad substitution
100    vars_script_name="$( (echo "${.sh.file}") 2>&1 )" || : ;
101    vars_script_name="$(expr "${vars_script_name:-}" : '^.*sh: \(.*\)\[[0-9]*\]:')" ;
102  fi
103elif [ -n "${BASH_VERSION:-}" ] ; then        # bash
104  # shellcheck disable=2128
105  (return 0 2>/dev/null) && vars_script_name="${BASH_SOURCE}" ;
106elif [ "dash" = "$vars_script_shell" ] ; then # dash
107  # force dash to issue error msg; which contains this script's rel/path/filename, e.g.:
108  # dash: 146: /home/ubuntu/intel/oneapi/vars.sh: Bad substitution
109  vars_script_name="$( (echo "${.sh.file}") 2>&1 )" || : ;
110  vars_script_name="$(expr "${vars_script_name:-}" : '^.*dash: [0-9]*: \(.*\):')" ;
111elif [ "sh" = "$vars_script_shell" ] ; then   # could be dash masquerading as /bin/sh
112  # force a shell error msg; which should contain this script's path/filename
113  # sample error msg shown; assume this file is named "vars.sh"; as required by setvars.sh
114  vars_script_name="$( (echo "${.sh.file}") 2>&1 )" || : ;
115  if [ "$(printf "%s" "$vars_script_name" | grep -Eq "sh: [0-9]+: .*vars\.sh: " ; echo $?)" -eq 0 ] ; then # dash as sh
116    # sh: 155: /home/ubuntu/intel/oneapi/vars.sh: Bad substitution
117    vars_script_name="$(expr "${vars_script_name:-}" : '^.*sh: [0-9]*: \(.*\):')" ;
118  fi
119else  # unrecognized shell or dash being sourced from within a user's script
120  # force a shell error msg; which should contain this script's path/filename
121  # sample error msg shown; assume this file is named "vars.sh"; as required by setvars.sh
122  vars_script_name="$( (echo "${.sh.file}") 2>&1 )" || : ;
123  if [ "$(printf "%s" "$vars_script_name" | grep -Eq "^.+: [0-9]+: .*vars\.sh: " ; echo $?)" -eq 0 ] ; then # dash
124    # .*: 164: intel/oneapi/vars.sh: Bad substitution
125    vars_script_name="$(expr "${vars_script_name:-}" : '^.*: [0-9]*: \(.*\):')" ;
126  else
127    vars_script_name="" ;
128  fi
129fi
130
131if [ "" = "$vars_script_name" ] ; then
132  >&2 echo ":: ERROR: Unable to proceed: possible causes listed below."
133  >&2 echo "   This script must be sourced. Did you execute or source this script?" ;
134  >&2 echo "   Unrecognized/unsupported shell (supported: bash, zsh, ksh, m/lksh, dash)." ;
135  >&2 echo "   Can be caused by sourcing from ZSH version 4.x or older." ;
136  return 255 2>/dev/null || exit 255
137fi
138
139TBBROOT=$(get_script_path "${vars_script_name:-}")/..
140
141TBB_TARGET_ARCH="intel64"
142TBB_ARCH_SUFFIX=""
143
144if [ -n "${SETVARS_ARGS:-}" ]; then
145  tbb_arg_ia32="$(expr "${SETVARS_ARGS:-}" : '^.*\(ia32\)')" || true
146  if [ -n "${tbb_arg_ia32:-}" ]; then
147    TBB_TARGET_ARCH="ia32"
148  fi
149else
150  for arg do
151    case "$arg" in
152    (intel64|ia32)
153      TBB_TARGET_ARCH="${arg}"
154      ;;
155    (*) ;;
156    esac
157  done
158fi
159
160TBB_LIB_NAME="libtbb.so.12"
161
162# Parse layout
163if [ -e "$TBBROOT/lib/$TBB_TARGET_ARCH" ]; then
164  TBB_LIB_DIR="$TBB_TARGET_ARCH/gcc4.8"
165else
166  if [ "$TBB_TARGET_ARCH" = "ia32" ] ; then
167    TBB_ARCH_SUFFIX="32"
168  fi
169  TBB_LIB_DIR=""
170fi
171
172if [ -e "$TBBROOT/lib$TBB_ARCH_SUFFIX/$TBB_LIB_DIR/$TBB_LIB_NAME" ]; then
173  export TBBROOT
174
175  LIBRARY_PATH=$(prepend_path "${TBBROOT}/lib$TBB_ARCH_SUFFIX/$TBB_LIB_DIR" "${LIBRARY_PATH:-}") ; export LIBRARY_PATH
176  LD_LIBRARY_PATH=$(prepend_path "${TBBROOT}/lib$TBB_ARCH_SUFFIX/$TBB_LIB_DIR" "${LD_LIBRARY_PATH:-}") ; export LD_LIBRARY_PATH
177  CPATH=$(prepend_path "${TBBROOT}/include" "${CPATH:-}") ; export CPATH
178  CMAKE_PREFIX_PATH=$(prepend_path "${TBBROOT}" "${CMAKE_PREFIX_PATH:-}") ; export CMAKE_PREFIX_PATH
179  PKG_CONFIG_PATH=$(prepend_path "${TBBROOT}/lib$TBB_ARCH_SUFFIX/pkgconfig" "${PKG_CONFIG_PATH:-}") ; export PKG_CONFIG_PATH
180else
181  >&2 echo "ERROR: $TBB_LIB_NAME library does not exist in $TBBROOT/lib$TBB_ARCH_SUFFIX/$TBB_LIB_DIR."
182  return 255 2>/dev/null || exit 255
183fi
184