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