Home | History | Annotate | Download | only in BaseTools
      1 ## @file
      2 # Setup the environment for unix-like systems running a bash-like shell.
      3 # This file must be "sourced" not merely executed. For example: ". edksetup.sh"
      4 #
      5 # Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
      6 # Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR>
      7 # This program and the accompanying materials
      8 # are licensed and made available under the terms and conditions of the BSD License
      9 # which accompanies this distribution.  The full text of the license may be found at
     10 # http://opensource.org/licenses/bsd-license.php
     11 # 
     12 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     14 #
     15 
     16 SetWorkspace() {
     17 
     18   #
     19   # If WORKSPACE is already set, then we can return right now
     20   #
     21   if [ -n "$WORKSPACE" ]
     22   then
     23     return 0
     24   fi
     25 
     26   #
     27   # Set $WORKSPACE
     28   #
     29   export WORKSPACE=`pwd`
     30 
     31   return 0
     32 
     33 }
     34 
     35 RestorePreviousConfiguration() {
     36   #
     37   # Restore previous configuration
     38   #
     39   if [ -z "$CONF_PATH" ]
     40   then
     41     export CONF_PATH=$WORKSPACE/Conf
     42     if [ ! -d $WORKSPACE/Conf ] && [ -n "$PACKAGES_PATH" ]
     43     then
     44       PATH_LIST=${PACKAGES_PATH//:/ }
     45       for DIR in $PATH_LIST
     46       do
     47         if [ -d $DIR/Conf ]
     48         then
     49           export CONF_PATH=$DIR/Conf
     50           break
     51         fi
     52       done
     53     fi
     54   fi
     55   
     56   PREVIOUS_CONF_FILE=$CONF_PATH/BuildEnv.sh
     57   if [ -e $PREVIOUS_CONF_FILE ]
     58   then
     59     echo Loading previous configuration from $PREVIOUS_CONF_FILE
     60     . $PREVIOUS_CONF_FILE
     61   fi
     62 }
     63 
     64 GenerateShellCodeToSetVariable() {
     65   VARIABLE=$1
     66   OUTPUT_FILE=$2
     67   VAR_VALUE="echo \${${VARIABLE}}"
     68   VAR_VALUE=`eval $VAR_VALUE`
     69   echo "if [ -z \"\$${VARIABLE}\" ]"             >> $OUTPUT_FILE
     70   echo "then"                                    >> $OUTPUT_FILE
     71   echo "  export ${VARIABLE}=${VAR_VALUE}"       >> $OUTPUT_FILE
     72   echo "fi"                                      >> $OUTPUT_FILE
     73 }
     74 
     75 GenerateShellCodeToUpdatePath() {
     76   OUTPUT_FILE=$1
     77   echo "if [ -e $EDK_TOOLS_PATH_BIN ]"                        >> $OUTPUT_FILE
     78   echo "then"                                                 >> $OUTPUT_FILE
     79   echo "  if [ "\${PATH/$EDK_TOOLS_PATH_BIN/}" == "\$PATH" ]" >> $OUTPUT_FILE
     80   echo "  then"                                               >> $OUTPUT_FILE
     81   echo "    export PATH=$EDK_TOOLS_PATH_BIN:\$PATH"           >> $OUTPUT_FILE
     82   echo "  fi"                                                 >> $OUTPUT_FILE
     83   echo "fi"                                                   >> $OUTPUT_FILE
     84 }
     85 
     86 StoreCurrentConfiguration() {
     87   #
     88   # Write configuration to a shell script to allow for configuration to be
     89   # easily reloaded.
     90   #
     91   OUTPUT_FILE=$CONF_PATH/BuildEnv.sh
     92   #echo Storing current configuration into $OUTPUT_FILE
     93   echo "# Auto-generated by ${BASH_SOURCE[0]}" > $OUTPUT_FILE
     94   GenerateShellCodeToSetVariable WORKSPACE $OUTPUT_FILE
     95   GenerateShellCodeToSetVariable EDK_TOOLS_PATH $OUTPUT_FILE
     96   GenerateShellCodeToUpdatePath $OUTPUT_FILE
     97 }
     98 
     99 SetEdkToolsPath() {
    100 
    101   #
    102   # If EDK_TOOLS_PATH is already set, then we can return right now
    103   #
    104   if [ -n "$EDK_TOOLS_PATH" ]
    105   then
    106     return 0
    107   fi
    108 
    109   #
    110   # Try $CONF_PATH/EdkTools
    111   #
    112   if [ -e $CONF_PATH/EdkTools ]
    113   then
    114     export EDK_TOOLS_PATH=$CONF_PATH/EdkTools
    115     return 0
    116   fi
    117 
    118   #
    119   # Try $CONF_PATH/BaseToolsSource
    120   #
    121   if [ -e $CONF_PATH/BaseToolsSource ]
    122   then
    123     export EDK_TOOLS_PATH=$CONF_PATH/BaseToolsSource
    124     return 0
    125   fi
    126 
    127   #
    128   # Try $WORKSPACE/BaseTools
    129   #
    130   if [ -e $WORKSPACE/BaseTools ]
    131   then
    132     export EDK_TOOLS_PATH=$WORKSPACE/BaseTools
    133     return 0
    134   fi
    135 
    136   #
    137   # Try $PACKAGES_PATH
    138   #
    139   if [ -n "$PACKAGES_PATH"]
    140   then
    141     PATH_LIST=${PACKAGES_PATH//:/ }
    142     for DIR in $PATH_LIST
    143     do
    144       if [ -d $DIR/BaseTools ]
    145       then
    146         export EDK_TOOLS_PATH=$DIR/BaseTools
    147         return 0
    148       fi
    149     done
    150   fi
    151 
    152   echo "Unable to determine EDK_TOOLS_PATH"
    153   echo
    154   echo "You may need to download the 'BaseTools' from buildtools.tianocore.org."
    155   echo "After downloading, either create a symbolic link to the source at"
    156   echo "\$WORKSPACE/Conf/BaseToolsSource, or set the EDK_TOOLS_PATH environment"
    157   echo "variable."
    158 
    159 }
    160 
    161 GetBaseToolsBinSubDir() {
    162   #
    163   # Figure out a uniq directory name from the uname command
    164   #
    165   UNAME_DIRNAME=`uname -sm`
    166   UNAME_DIRNAME=${UNAME_DIRNAME// /-}
    167   UNAME_DIRNAME=${UNAME_DIRNAME//\//-}
    168   echo $UNAME_DIRNAME
    169 }
    170 
    171 GetEdkToolsPathBinDirectory() {
    172   #
    173   # Figure out a uniq directory name from the uname command
    174   #
    175   BIN_SUB_DIR=`GetBaseToolsBinSubDir`
    176 
    177   if [ -e $EDK_TOOLS_PATH/BinWrappers/$BIN_SUB_DIR ]
    178   then
    179     EDK_TOOLS_PATH_BIN=$EDK_TOOLS_PATH/BinWrappers/$BIN_SUB_DIR
    180   else
    181     EDK_TOOLS_PATH_BIN=$EDK_TOOLS_PATH/Bin/$BIN_SUB_DIR
    182   fi
    183 
    184   echo $EDK_TOOLS_PATH_BIN
    185 }
    186 
    187 AddDirToStartOfPath() {
    188   DIRNAME=$1
    189   PATH=$DIRNAME:$DIRNAME:$DIRNAME:$PATH
    190   PATH=${PATH//$DIRNAME:/}
    191   PATH=$DIRNAME:$PATH
    192   export PATH
    193 }
    194 
    195 AddEdkToolsToPath() {
    196 
    197   #
    198   # If EDK_TOOLS_PATH is not set, then we cannot update PATH
    199   #
    200   if [ -z "$EDK_TOOLS_PATH" ]
    201   then
    202     return 1
    203   fi
    204 
    205   EDK_TOOLS_PATH_BIN=`GetEdkToolsPathBinDirectory`
    206 
    207   AddDirToStartOfPath $EDK_TOOLS_PATH/BinWrappers/PosixLike
    208   AddDirToStartOfPath $EDK_TOOLS_PATH_BIN
    209 
    210 }
    211 
    212 CopySingleTemplateFile() {
    213 
    214   SRC_FILENAME=Conf/$1.template
    215   DST_FILENAME=$CONF_PATH/$1.txt
    216 
    217   if [ -e $DST_FILENAME ]
    218   then
    219     [ $RECONFIG != TRUE ] && return
    220   fi
    221 
    222   echo "Copying \$EDK_TOOLS_PATH/$SRC_FILENAME"
    223   echo "     to $DST_FILENAME"
    224   SRC_FILENAME=$EDK_TOOLS_PATH/$SRC_FILENAME
    225   cp $SRC_FILENAME $DST_FILENAME
    226 
    227 }
    228 
    229 CopyTemplateFiles() {
    230 
    231   CopySingleTemplateFile build_rule
    232   CopySingleTemplateFile tools_def
    233   CopySingleTemplateFile target
    234 
    235 }
    236 
    237 ScriptMain() {
    238 
    239   SetWorkspace
    240   if [ -z $WORKSPACE ]
    241   then
    242     echo "Failure setting WORKSPACE"
    243     return 1
    244   fi
    245 
    246   RestorePreviousConfiguration
    247 
    248   SetEdkToolsPath
    249   if [ -z $EDK_TOOLS_PATH ]
    250   then
    251     return 1
    252   fi
    253 
    254   AddEdkToolsToPath
    255   if [ $? -ne 0 ]
    256   then
    257     echo "Failure adding EDK Tools into PATH!"
    258     return 1
    259   fi
    260 
    261   StoreCurrentConfiguration
    262 
    263   echo WORKSPACE: $WORKSPACE
    264   echo EDK_TOOLS_PATH: $EDK_TOOLS_PATH
    265   echo CONF_PATH: $CONF_PATH
    266 
    267   CopyTemplateFiles
    268 
    269 }
    270 
    271 #
    272 # Run the main function
    273 #
    274 ScriptMain
    275 
    276