1#!/bin/bash
2#
3# Copyright (c) 2008 - 2011, Apple Inc. All rights reserved.<BR>
4# Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
5#
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
15set -e
16shopt -s nocasematch
17
18
19#
20# Setup workspace if it is not set
21#
22if [ -z "$WORKSPACE" ]
23then
24  echo Initializing workspace
25  if [ ! -e `pwd`/edksetup.sh ]
26  then
27    cd ..
28  fi
29# This version is for the tools in the BaseTools project.
30# this assumes svn pulls have the same root dir
31#  export EDK_TOOLS_PATH=`pwd`/../BaseTools
32# This version is for the tools source in edk2
33  export EDK_TOOLS_PATH=`pwd`/BaseTools
34  echo $EDK_TOOLS_PATH
35  source edksetup.sh BaseTools
36else
37  echo Building from: $WORKSPACE
38fi
39
40#
41# Configure defaults for various options
42#
43
44PROCESSOR=
45BUILDTARGET=DEBUG
46BUILD_OPTIONS=
47PLATFORMFILE=
48LAST_ARG=
49RUN_EMULATOR=no
50CLEAN_TYPE=none
51TARGET_TOOLS=GCC44
52NETWORK_SUPPORT=
53BUILD_NEW_SHELL=
54BUILD_FAT=
55HOST_PROCESSOR=X64
56
57case `uname` in
58  CYGWIN*) echo Cygwin not fully supported yet. ;;
59  Darwin*)
60      Major=$(uname -r | cut -f 1 -d '.')
61      if [[ $Major == 9 ]]
62      then
63        echo UnixPkg requires Snow Leopard or later OS
64        exit 1
65      else
66        CLANG_VER=$(clang -ccc-host-triple x86_64-pc-win32-macho 2>&1 >/dev/null) || true
67        if [[ "$CLANG_VER" == *-ccc-host-triple* ]]
68        then
69        # only older versions of Xcode support -ccc-host-tripe, for newer versions
70        # it is -target
71          HOST_TOOLS=XCODE32
72          TARGET_TOOLS=XCODE5
73        else
74          HOST_TOOLS=XCODE32
75          TARGET_TOOLS=XCLANG
76        fi
77      fi
78      BUILD_NEW_SHELL="-D BUILD_NEW_SHELL"
79      BUILD_FAT="-D BUILD_FAT"
80      ;;
81  Linux*)
82    case `uname -m` in
83      i386)
84        HOST_PROCESSOR=IA32
85        ;;
86      i686)
87        HOST_PROCESSOR=IA32
88        ;;
89      x86_64)
90        HOST_PROCESSOR=X64
91        ;;
92    esac
93
94    gcc_version=$(gcc -v 2>&1 | tail -1 | awk '{print $3}')
95    case $gcc_version in
96      4.5.*)
97        TARGET_TOOLS=GCC45
98        ;;
99      4.6.*)
100        TARGET_TOOLS=GCC46
101        ;;
102      4.7.*)
103        TARGET_TOOLS=GCC47
104        ;;
105      4.8.*)
106        TARGET_TOOLS=GCC48
107        ;;
108      4.9.*|4.1[0-9].*|5.*.*)
109        TARGET_TOOLS=GCC49
110        ;;
111      *)
112        TARGET_TOOLS=GCC44
113        ;;
114    esac
115    ;;
116esac
117
118#
119# Scan command line to override defaults
120#
121
122for arg in "$@"
123do
124  if [ -z "$LAST_ARG" ]; then
125    case $arg in
126      -a|-b|-t|-p)
127        LAST_ARG=$arg
128        ;;
129      run)
130        RUN_EMULATOR=yes
131        shift
132        break
133        ;;
134      clean|cleanall)
135        CLEAN_TYPE=$arg
136        shift
137        break
138        ;;
139      *)
140        BUILD_OPTIONS="$BUILD_OPTIONS $arg"
141        ;;
142    esac
143  else
144    case $LAST_ARG in
145      -a)
146        PROCESSOR=$arg
147        ;;
148      -b)
149        BUILDTARGET=$arg
150        ;;
151      -p)
152        PLATFORMFILE=$arg
153        ;;
154      -t)
155        HOST_TOOLS=$arg
156        ;;
157      *)
158        BUILD_OPTIONS="$BUILD_OPTIONS $arg"
159        ;;
160    esac
161    LAST_ARG=
162  fi
163  shift
164done
165if [ -z "$HOST_TOOLS" ]
166then
167  HOST_TOOLS=$TARGET_TOOLS
168fi
169
170if [ -z "$PROCESSOR" ]
171then
172  PROCESSOR=$HOST_PROCESSOR
173fi
174
175case $PROCESSOR in
176  IA32)
177    ARCH_SIZE=32
178    BUILD_OUTPUT_DIR=$WORKSPACE/Build/Emulator32
179    LIB_NAMES="ld-linux.so.2 libdl.so.2 crt1.o crti.o crtn.o"
180    LIB_SEARCH_PATHS="/usr/lib/i386-linux-gnu /usr/lib32 /lib32 /usr/lib /lib"
181    ;;
182  X64)
183    ARCH_SIZE=64
184    BUILD_OUTPUT_DIR=$WORKSPACE/Build/Emulator
185    LIB_NAMES="ld-linux-x86-64.so.2 libdl.so.2 crt1.o crti.o crtn.o"
186    LIB_SEARCH_PATHS="/usr/lib/x86_64-linux-gnu /usr/lib64 /lib64 /usr/lib /lib"
187    ;;
188esac
189
190for libname in $LIB_NAMES
191do
192  for dirname in $LIB_SEARCH_PATHS
193  do
194    if [ -e $dirname/$libname ]; then
195      export HOST_DLINK_PATHS="$HOST_DLINK_PATHS $dirname/$libname"
196      break
197    fi
198  done
199done
200
201PLATFORMFILE=$WORKSPACE/EmulatorPkg/EmulatorPkg.dsc
202BUILD_DIR=$BUILD_OUTPUT_DIR/DEBUG_"$TARGET_TOOLS"
203BUILD_ROOT_ARCH=$BUILD_DIR/$PROCESSOR
204
205if  [[ ! -f `which build` || ! -f `which GenFv` ]];
206then
207  # build the tools if they don't yet exist. Bin scheme
208  echo Building tools as they are not in the path
209  make -C $WORKSPACE/BaseTools
210elif [[ ( -f `which build` ||  -f `which GenFv` )  && ! -d  $EDK_TOOLS_PATH/Source/C/bin ]];
211then
212  # build the tools if they don't yet exist. BinWrapper scheme
213  echo Building tools no $EDK_TOOLS_PATH/Source/C/bin directory
214  make -C $WORKSPACE/BaseTools
215else
216  echo using prebuilt tools
217fi
218
219
220if [[ "$RUN_EMULATOR" == "yes" ]]; then
221  case `uname` in
222    Darwin*)
223      #
224      # On Darwin we can't use dlopen, so we have to load the real PE/COFF images.
225      # This .gdbinit script sets a breakpoint that loads symbols for the PE/COFFEE
226      # images that get loaded in Host
227      #
228      if [[ "$CLANG_VER" == *-ccc-host-triple* ]]
229      then
230      # only older versions of Xcode support -ccc-host-tripe, for newer versions
231      # it is -target
232        cp $WORKSPACE/EmulatorPkg/Unix/lldbefi.py $BUILD_OUTPUT_DIR/DEBUG_"$TARGET_TOOLS"/$PROCESSOR
233        cd $BUILD_ROOT_ARCH; /usr/bin/lldb --source $WORKSPACE/EmulatorPkg/Unix/lldbinit Host
234        exit $?
235      else
236        cp $WORKSPACE/EmulatorPkg/Unix/.gdbinit $BUILD_OUTPUT_DIR/DEBUG_"$TARGET_TOOLS"/$PROCESSOR
237      fi
238      ;;
239  esac
240
241  /usr/bin/gdb $BUILD_ROOT_ARCH/Host -q -cd=$BUILD_ROOT_ARCH -x $WORKSPACE/EmulatorPkg/Unix/GdbRun
242  exit
243fi
244
245case $CLEAN_TYPE in
246  clean)
247    build -p $WORKSPACE/EmulatorPkg/EmulatorPkg.dsc -a $PROCESSOR -b $BUILDTARGET -t $HOST_TOOLS -D UNIX_SEC_BUILD -n 3 clean
248    build -p $WORKSPACE/EmulatorPkg/EmulatorPkg.dsc -a $PROCESSOR -b $BUILDTARGET -t $TARGET_TOOLS -n 3 clean
249    exit $?
250    ;;
251  cleanall)
252    make -C $WORKSPACE/BaseTools clean
253    build -p $WORKSPACE/EmulatorPkg/EmulatorPkg.dsc -a $PROCESSOR -b $BUILDTARGET -t $HOST_TOOLS -D UNIX_SEC_BUILD -n 3 clean
254    build -p $WORKSPACE/EmulatorPkg/EmulatorPkg.dsc -a $PROCESSOR -b $BUILDTARGET -t $TARGET_TOOLS -n 3 clean
255    build -p $WORKSPACE/ShellPkg/ShellPkg.dsc -a IA32 -b $BUILDTARGET -t $TARGET_TOOLS -n 3 clean
256    exit $?
257    ;;
258esac
259
260
261#
262# Build the edk2 EmulatorPkg
263#
264if [[ $HOST_TOOLS == $TARGET_TOOLS ]]; then
265  build -p $WORKSPACE/EmulatorPkg/EmulatorPkg.dsc $BUILD_OPTIONS -a $PROCESSOR -b $BUILDTARGET -t $TARGET_TOOLS -D BUILD_$ARCH_SIZE -D UNIX_SEC_BUILD $NETWORK_SUPPORT $BUILD_NEW_SHELL $BUILD_FAT -n 3
266else
267  build -p $WORKSPACE/EmulatorPkg/EmulatorPkg.dsc $BUILD_OPTIONS -a $PROCESSOR -b $BUILDTARGET -t $HOST_TOOLS  -D BUILD_$ARCH_SIZE -D UNIX_SEC_BUILD -D SKIP_MAIN_BUILD -n 3 modules
268  build -p $WORKSPACE/EmulatorPkg/EmulatorPkg.dsc $BUILD_OPTIONS -a $PROCESSOR -b $BUILDTARGET -t $TARGET_TOOLS -D BUILD_$ARCH_SIZE $NETWORK_SUPPORT $BUILD_NEW_SHELL $BUILD_FAT -n 3
269  cp $BUILD_OUTPUT_DIR/DEBUG_"$HOST_TOOLS"/$PROCESSOR/Host $BUILD_ROOT_ARCH
270fi
271exit $?
272
273