xref: /oneTBB/integration/mac/env/vars.sh (revision c94904e9)
1#!/bin/sh
2# shellcheck shell=sh
3#
4# Copyright (c) 2005-2021 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# Get absolute path to script. Gets a relative path as argument and outputs an absolute path.
19get_script_path() (
20  script_path="$1"
21  while [ -L "$script_path" ] ; do
22    script_dir=$(command dirname -- "$script_path")
23    script_dir=$(cd "$script_dir" && command pwd -P)
24    script_path="$(readlink "$script_path")"
25    case $script_path in
26      (/*) ;;
27       (*) script_path="$script_dir/$script_path" ;;
28    esac
29  done
30  script_dir=$(command dirname -- "$script_path")
31  script_dir=$(cd "$script_dir" && command pwd -P)
32  printf "%s" "$script_dir"
33)
34
35_vars_get_proc_name() {
36  if [ -n "${ZSH_VERSION:-}" ] ; then
37    script="$(ps -p "$$" -o comm=)"
38  else
39    script="$1"
40    while [ -L "$script" ] ; do
41      script="$(readlink "$script")"
42    done
43  fi
44  basename -- "$script"
45}
46
47_vars_this_script_name="vars.sh"
48if [ "$_vars_this_script_name" = "$(_vars_get_proc_name "$0")" ] ; then
49  echo ":: ERROR: Incorrect usage: this script must be sourced."
50  echo "   Usage: . path/to/${_vars_this_script_name}"
51  return 255 2>/dev/null || exit 255
52fi
53
54# Prepend path segment(s) to path-like env vars (PATH, CPATH, etc.).
55
56# prepend_path() avoids dangling ":" that affects some env vars (PATH and CPATH)
57# PATH > https://www.gnu.org/software/libc/manual/html_node/Standard-Environment.html
58
59# Usage:
60#   env_var=$(prepend_path "$prepend_to_var" "$existing_env_var")
61#   export env_var
62#
63# Inputs:
64#   $1 == path segment to be prepended to $2
65#   $2 == value of existing path-like environment variable
66
67prepend_path() (
68  path_to_add="$1"
69  path_is_now="$2"
70
71  if [ "" = "${path_is_now}" ] ; then   # avoid dangling ":"
72    printf "%s" "${path_to_add}"
73  else
74    printf "%s" "${path_to_add}:${path_is_now}"
75  fi
76)
77
78# Extract the name and location of this sourced script.
79
80# Generally, "ps -o comm=" is limited to a 15 character result, but it works
81# fine for this usage, because we are primarily interested in finding the name
82# of the execution shell, not the name of any calling script.
83
84vars_script_name=""
85vars_script_shell="$(ps -p "$$" -o comm=)"
86# ${var:-} needed to pass "set -eu" checks
87if [ -n "${ZSH_VERSION:-}" ] && [ -n "${ZSH_EVAL_CONTEXT:-}" ] ; then     # zsh 5.x and later
88  # shellcheck disable=2249
89  case $ZSH_EVAL_CONTEXT in (*:file*) vars_script_name="${(%):-%x}" ;; esac ;
90elif [ -n "${KSH_VERSION:-}" ] ; then                                     # ksh, mksh or lksh
91  if [ "$(set | grep -Fq "KSH_VERSION=.sh.version" ; echo $?)" -eq 0 ] ; then # ksh
92    vars_script_name="${.sh.file}" ;
93  else # mksh or lksh or [lm]ksh masquerading as ksh or sh
94    # force [lm]ksh to issue error msg; which contains this script's path/filename, e.g.:
95    # mksh: /home/ubuntu/intel/oneapi/vars.sh[137]: ${.sh.file}: bad substitution
96    vars_script_name="$( (echo "${.sh.file}") 2>&1 )" || : ;
97    vars_script_name="$(expr "${vars_script_name:-}" : '^.*sh: \(.*\)\[[0-9]*\]:')" ;
98  fi
99elif [ -n "${BASH_VERSION:-}" ] ; then        # bash
100  # shellcheck disable=2128
101  (return 0 2>/dev/null) && vars_script_name="${BASH_SOURCE}" ;
102elif [ "dash" = "$vars_script_shell" ] ; then # dash
103  # force dash to issue error msg; which contains this script's rel/path/filename, e.g.:
104  # dash: 146: /home/ubuntu/intel/oneapi/vars.sh: Bad substitution
105  vars_script_name="$( (echo "${.sh.file}") 2>&1 )" || : ;
106  vars_script_name="$(expr "${vars_script_name:-}" : '^.*dash: [0-9]*: \(.*\):')" ;
107elif [ "sh" = "$vars_script_shell" ] ; then   # could be dash masquerading as /bin/sh
108  # force a shell error msg; which should contain this script's path/filename
109  # sample error msg shown; assume this file is named "vars.sh"; as required by setvars.sh
110  vars_script_name="$( (echo "${.sh.file}") 2>&1 )" || : ;
111  if [ "$(printf "%s" "$vars_script_name" | grep -Eq "sh: [0-9]+: .*vars\.sh: " ; echo $?)" -eq 0 ] ; then # dash as sh
112    # sh: 155: /home/ubuntu/intel/oneapi/vars.sh: Bad substitution
113    vars_script_name="$(expr "${vars_script_name:-}" : '^.*sh: [0-9]*: \(.*\):')" ;
114  fi
115else  # unrecognized shell or dash being sourced from within a user's script
116  # force a shell error msg; which should contain this script's path/filename
117  # sample error msg shown; assume this file is named "vars.sh"; as required by setvars.sh
118  vars_script_name="$( (echo "${.sh.file}") 2>&1 )" || : ;
119  if [ "$(printf "%s" "$vars_script_name" | grep -Eq "^.+: [0-9]+: .*vars\.sh: " ; echo $?)" -eq 0 ] ; then # dash
120    # .*: 164: intel/oneapi/vars.sh: Bad substitution
121    vars_script_name="$(expr "${vars_script_name:-}" : '^.*: [0-9]*: \(.*\):')" ;
122  else
123    vars_script_name="" ;
124  fi
125fi
126
127if [ "" = "$vars_script_name" ] ; then
128  >&2 echo ":: ERROR: Unable to proceed: possible causes listed below."
129  >&2 echo "   This script must be sourced. Did you execute or source this script?" ;
130  >&2 echo "   Unrecognized/unsupported shell (supported: bash, zsh, ksh, m/lksh, dash)." ;
131  >&2 echo "   Can be caused by sourcing from ZSH version 4.x or older." ;
132  return 255 2>/dev/null || exit 255
133fi
134
135TBBROOT=$(get_script_path "${vars_script_name:-}")/..
136LIBTBB_NAME="libtbb.dylib"
137
138if [ -e "$TBBROOT/lib/$LIBTBB_NAME" ]; then
139    export TBBROOT
140
141    LIBRARY_PATH=$(prepend_path "${TBBROOT}/lib" "${LIBRARY_PATH:-}") ; export LIBRARY_PATH
142    DYLD_LIBRARY_PATH=$(prepend_path "${TBBROOT}/lib" "${DYLD_LIBRARY_PATH:-}") ; export DYLD_LIBRARY_PATH
143    CPATH=$(prepend_path "${TBBROOT}/include" "${CPATH:-}") ; export CPATH
144    CMAKE_PREFIX_PATH=$(prepend_path "${TBBROOT}" "${CMAKE_PREFIX_PATH:-}") ; export CMAKE_PREFIX_PATH
145    PKG_CONFIG_PATH=$(prepend_path "${TBBROOT}/lib/pkgconfig" "${PKG_CONFIG_PATH:-}") ; export PKG_CONFIG_PATH
146else
147    >&2 echo "ERROR: $LIBTBB_NAME library does not exist in $TBBROOT/lib."
148    return 255 2>/dev/null || exit 255
149fi
150