1#
2# Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
3# Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR>
4# SPDX-License-Identifier: BSD-2-Clause-Patent
5#
6# In *inux environment, the build tools's source is required and need to be compiled
7# firstly, please reference https://github.com/tianocore/tianocore.github.io/wiki/SourceForge-to-Github-Quick-Start
8# to get how to setup build tool.
9#
10# Setup the environment for unix-like systems running a bash-like shell.
11# This file must be "sourced" not merely executed. For example: ". edksetup.sh"
12#
13# CYGWIN users: Your path and filename related environment variables should be
14# set up in the unix style.  This script will make the necessary conversions to
15# windows style.
16#
17# Please reference edk2 user manual for more detail descriptions at https://github.com/tianocore-docs/Docs/raw/master/User_Docs/EDK_II_UserManual_0_7.pdf
18#
19
20SCRIPTNAME="edksetup.sh"
21RECONFIG=FALSE
22
23function HelpMsg()
24{
25  echo "Usage: $SCRIPTNAME [Options]"
26  echo
27  echo "The system environment variable, WORKSPACE, is always set to the current"
28  echo "working directory."
29  echo
30  echo "Options: "
31  echo "  --help, -h, -?        Print this help screen and exit."
32  echo
33  echo "  --reconfig            Overwrite the WORKSPACE/Conf/*.txt files with the"
34  echo "                        template files from the BaseTools/Conf directory."
35  echo
36  echo Please note: This script must be \'sourced\' so the environment can be changed.
37  echo ". $SCRIPTNAME"
38  echo "source $SCRIPTNAME"
39}
40
41function SetWorkspace()
42{
43  #
44  # If WORKSPACE is already set, then we can return right now
45  #
46  export PYTHONHASHSEED=1
47  if [ -n "$WORKSPACE" ]
48  then
49    return 0
50  fi
51
52  if [ ! ${BASH_SOURCE[0]} -ef ./$SCRIPTNAME ] && [ -z "$PACKAGES_PATH" ]
53  then
54    echo Run this script from the base of your tree.  For example:
55    echo "  cd /Path/To/Edk/Root"
56    echo "  . $SCRIPTNAME"
57    return 1
58  fi
59
60  #
61  # Check for BaseTools/BuildEnv before dirtying the user's environment.
62  #
63  if [ ! -f BaseTools/BuildEnv ] && [ -z "$EDK_TOOLS_PATH" ]
64  then
65    echo BaseTools not found in your tree, and EDK_TOOLS_PATH is not set.
66    echo Please point EDK_TOOLS_PATH at the directory that contains
67    echo the EDK2 BuildEnv script.
68    return 1
69  fi
70
71  #
72  # Set $WORKSPACE
73  #
74  export WORKSPACE=$PWD
75  return 0
76}
77
78function SetupEnv()
79{
80  if [ -n "$EDK_TOOLS_PATH" ]
81  then
82    . $EDK_TOOLS_PATH/BuildEnv
83  elif [ -f "$WORKSPACE/BaseTools/BuildEnv" ]
84  then
85    . $WORKSPACE/BaseTools/BuildEnv
86  elif [ -n "$PACKAGES_PATH" ]
87  then
88    PATH_LIST=$PACKAGES_PATH
89    PATH_LIST=${PATH_LIST//:/ }
90    for DIR in $PATH_LIST
91    do
92      if [ -f "$DIR/BaseTools/BuildEnv" ]
93      then
94        export EDK_TOOLS_PATH=$DIR/BaseTools
95        . $DIR/BaseTools/BuildEnv
96        break
97      fi
98    done
99  else
100    echo BaseTools not found in your tree, and EDK_TOOLS_PATH is not set.
101    echo Please check that WORKSPACE or PACKAGES_PATH is not set incorrectly
102    echo in your shell, or point EDK_TOOLS_PATH at the directory that contains
103    echo the EDK2 BuildEnv script.
104    return 1
105  fi
106}
107
108function SetupPython3()
109{
110  if [ $origin_version ];then
111    origin_version=
112  fi
113  for python in $(whereis python3)
114  do
115    python=$(echo $python | grep "[[:digit:]]$" || true)
116    python_version=${python##*python}
117    if [ -z "${python_version}" ] || (! command -v $python >/dev/null 2>&1);then
118      continue
119    fi
120    if [ -z $origin_version ];then
121      origin_version=$python_version
122      export PYTHON_COMMAND=$python
123      continue
124    fi
125      if [[ "$origin_version" < "$python_version" ]]; then
126      origin_version=$python_version
127      export PYTHON_COMMAND=$python
128    fi
129  done
130  return 0
131}
132
133function SetupPython()
134{
135  if [ $PYTHON_COMMAND ] && [ -z $PYTHON3_ENABLE ];then
136    if ( command -v $PYTHON_COMMAND >/dev/null 2>&1 );then
137      return 0
138    else
139      echo $PYTHON_COMMAND Cannot be used to build or execute the python tools.
140      return 1
141    fi
142  fi
143
144  if [ $PYTHON3_ENABLE ] && [ $PYTHON3_ENABLE == TRUE ]
145  then
146    SetupPython3
147  fi
148
149  if [ $PYTHON3_ENABLE ] && [ $PYTHON3_ENABLE != TRUE ]
150  then
151    if [ $origin_version ];then
152      origin_version=
153    fi
154    for python in $(whereis python2)
155    do
156      python=$(echo $python | grep "[[:digit:]]$" || true)
157      python_version=${python##*python}
158      if [ -z "${python_version}" ] || (! command -v $python >/dev/null 2>&1);then
159        continue
160      fi
161      if [ -z $origin_version ]
162      then
163        origin_version=$python_version
164        export PYTHON_COMMAND=$python
165        continue
166      fi
167      if [[ "$origin_version" < "$python_version" ]]; then
168        origin_version=$python_version
169        export PYTHON_COMMAND=$python
170      fi
171    done
172    return 0
173  fi
174
175  SetupPython3
176}
177
178function SourceEnv()
179{
180  SetWorkspace &&
181  SetupEnv
182  SetupPython
183}
184
185I=$#
186while [ $I -gt 0 ]
187do
188  case "$1" in
189    BaseTools)
190      # Ignore argument for backwards compatibility
191      shift
192    ;;
193    --reconfig)
194      RECONFIG=TRUE
195      shift
196    ;;
197    *)
198      HelpMsg
199      break
200    ;;
201  esac
202  I=$((I - 1))
203done
204
205if [ $I -gt 0 ]
206then
207  return 1
208fi
209
210SourceEnv
211
212unset SCRIPTNAME RECONFIG
213
214return $?
215