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