Modify build scripts to use repo

Test: Compiles on GCE with lots of patches
Change-Id: I10b8c46479cbe06b811fe35020a8a28acf80268d
diff --git a/rebuild.sh b/rebuild.sh
index 8363290..1f717ff 100755
--- a/rebuild.sh
+++ b/rebuild.sh
@@ -15,6 +15,7 @@
   "" "IP address or DNS name of an ARM system to do the secondary build"
 DEFINE_string arm_user \
   "vsoc-01" "User to invoke on the ARM system"
+DEFINE_string custom_manifest "" "Custom manifest to use for the build"
 DEFINE_string project "$(gcloud config get-value project)" "Project to use" "p"
 DEFINE_string source_image_family debian-9 "Image familty to use as the base" \
   "s"
@@ -41,6 +42,15 @@
   set -x
   fail=0
   source_files=("${DIR}"/rebuild_gce.sh)
+  gce_flags=()
+  if [[ -n "${FLAGS_custom_manifest}" ]]; then
+    if [[ ! -f "${FLAGS_custom_manifest}" ]]; then
+      echo custom manifest not found: ${FLAGS_custom_manifest} 1>&1
+      exit 2
+    fi
+    source_files+=("${FLAGS_custom_manifest}")
+    gce_flags+=("CUSTOM_MANIFEST=$(basename "${FLAGS_custom_manifest}")")
+  fi
   if [[ -z "${FLAGS_project}" ]]; then
     echo Must specify project 1>&2
     fail=1
@@ -75,15 +85,11 @@
     gcloud compute ssh "${SSH_FLAGS[@]}" \
       "${project_zone_flags[@]}" \
       "${FLAGS_x86_user}@${FLAGS_x86_instance}" -- \
-        ./rebuild_gce.sh prepare_source x86_64_build
+        ./rebuild_gce.sh "${gce_flags[@]}" prepare_source x86_64_build
     gcloud beta compute scp --recurse "${SSH_FLAGS[@]}" \
       "${project_zone_flags[@]}" \
       "${FLAGS_x86_user}@${FLAGS_x86_instance}":x86_64-linux-gnu \
       "${ANDROID_BUILD_TOP}/device/google/cuttlefish_vmm"
-    gcloud beta compute scp "${SSH_FLAGS[@]}" \
-      "${project_zone_flags[@]}" \
-      "${FLAGS_x86_user}@${FLAGS_x86_instance}":clean-source.tgz \
-      "${ANDROID_BUILD_TOP}/device/google/cuttlefish_vmm"
     gcloud compute disks describe \
       "${project_zone_flags[@]}" "${FLAGS_x86_instance}" | \
         grep ^sourceImage: > "${DIR}"/x86_64-linux-gnu/builder_image.txt
@@ -91,11 +97,10 @@
   if [[ -n "${FLAGS_arm_system}" ]]; then
     scp \
       "${source_files[@]}" \
-      "${ANDROID_BUILD_TOP}/device/google/cuttlefish_vmm/clean-source.tgz" \
       "${FLAGS_arm_user}@${FLAGS_arm_system}:"
     # We're using the source tarball from the x86_64 build
     ssh -t "${FLAGS_arm_user}@${FLAGS_arm_system}" -- \
-        ./rebuild_gce.sh restore_source arm64_build
+        ./rebuild_gce.sh "${gce_flags[@]}" prepare_source arm64_build
     scp -r "${FLAGS_arm_user}@${FLAGS_arm_system}":aarch64-linux-gnu \
       "${ANDROID_BUILD_TOP}/device/google/cuttlefish_vmm"
   fi
diff --git a/rebuild_gce.sh b/rebuild_gce.sh
index 7a4f193..37bf788 100755
--- a/rebuild_gce.sh
+++ b/rebuild_gce.sh
@@ -5,12 +5,14 @@
 OUT_DIR="$(pwd)/${ARCH}-linux-gnu"
 popd > /dev/null 2>&1
 LIB_PATH="${OUT_DIR}/lib"
+REPO_DIR=${HOME}/repo
 BUILD_DIR=${HOME}/build
 export THIRD_PARTY_ROOT="${BUILD_DIR}/third_party"
+export PLATFORM_ROOT="${BUILD_DIR}/platform"
 export PATH="${PATH}:${HOME}/bin"
-export RUST_VERSION=1.35.0 RUSTFLAGS='--cfg hermetic'
 SOURCE_DIRS=(build)
 BUILD_OUTPUTS=(usr lib)
+CUSTOM_MANIFEST=""
 
 set -o errexit
 set -x
@@ -45,6 +47,9 @@
       protobuf-compiler \
       python3 \
       xutils-dev # Needed to pacify autogen.sh for libepoxy
+  mkdir -p "${HOME}/bin"
+  curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
+  chmod a+x ~/bin/repo
 }
 
 retry() {
@@ -64,7 +69,7 @@
     "https://static.rust-lang.org/rustup/archive/1.14.0/$(uname -m)-unknown-linux-gnu/rustup-init"
   # echo "0077ff9c19f722e2be202698c037413099e1188c0c233c12a2297bf18e9ff6e7 *rustup-init" | sha256sum -c -
   chmod +x rustup-init
-  ./rustup-init -y --no-modify-path --default-toolchain $RUST_VERSION
+  ./rustup-init -y --no-modify-path
   source $HOME/.cargo/env
   if [[ -n "$1" ]]; then
     rustup target add "$1"
@@ -80,13 +85,6 @@
   fi
 }
 
-save_source() {
-  echo Saving source...
-  cd
-  rm -rf clean-source.tgz
-  tar cfvz clean-source.tgz "${SOURCE_DIRS[@]}"
-}
-
 prepare_source() {
   echo Fetching source...
   # Clean up anything that might be lurking
@@ -94,34 +92,17 @@
   rm -rf "${SOURCE_DIRS[@]}"
   # Needed so we can use git
   install_packages
-  mkdir -p "${THIRD_PARTY_ROOT}"
-  cd "${THIRD_PARTY_ROOT}"
-  # minijail does not exist in upstream linux distros.
-  git clone https://android.googlesource.com/platform/external/minijail
-  git clone https://android.googlesource.com/platform/external/minigbm \
-    -b upstream-master
-  sed 's/-Wall/-Wno-maybe-uninitialized/g' -i minigbm/Makefile
-  # New libepoxy has EGL_KHR_DEBUG entry points needed by crosvm.
-  git clone https://android.googlesource.com/platform/external/libepoxy \
-    -b upstream-master
-  cd libepoxy
-  cd ..
-  git clone https://android.googlesource.com/platform/external/virglrenderer \
-    -b upstream-master
-  git clone https://android.googlesource.com/platform/external/adhd \
-    -b upstream-master
-  mkdir -p "${BUILD_DIR}/platform"
-  cd "${BUILD_DIR}/platform"
-  git clone https://android.googlesource.com/platform/external/crosvm \
-    -b upstream-master
-  save_source
-}
-
-restore_source() {
-  echo Unpacking source...
-  install_packages
-  rm -rf "${SOURCE_DIRS[@]}"
-  tar xfvmz clean-source.tgz
+  mkdir -p "${BUILD_DIR}"
+  cd "${BUILD_DIR}"
+  git config --global user.name "AOSP Crosvm Builder"
+  git config --global user.email "nobody@android.com"
+  git config --global color.ui false
+  repo init -q -b crosvm-master -u https://android.googlesource.com/platform/manifest
+  if [[ -n "${CUSTOM_MANIFEST}" ]]; then
+    cp "${HOME}/${CUSTOM_MANIFEST}" .repo/manifests
+    repo init -m "${CUSTOM_MANIFEST}"
+  fi
+  repo sync
 }
 
 compile() {
@@ -154,51 +135,20 @@
   cp ${HOME}/lib/libgbm.so.1 "${LIB_PATH}/"
 
   cd "${THIRD_PARTY_ROOT}/libepoxy"
-  ./autogen.sh --prefix="${HOME}"
+  if [[ ! -d m4 ]]; then
+    ./autogen.sh --prefix="${HOME}"
+  fi
   make -j install
   cp "${HOME}"/lib/libepoxy.so.0 "${LIB_PATH}"/
 
   # Note: depends on libepoxy
   cd "${THIRD_PARTY_ROOT}/virglrenderer"
-  ./autogen.sh --prefix=${HOME} PKG_CONFIG_PATH=${HOME}/lib/pkgconfig --disable-glx
+  ./autogen.sh --prefix=${HOME} PKG_CONFIG_PATH=${HOME}/lib/pkgconfig
   make -j install
   cp "${HOME}/lib/libvirglrenderer.so.0" "${LIB_PATH}"/
 
-  #cd "${THIRD_PARTY_ROOT}"
-  # Install libtpm2 so that tpm2-sys/build.rs does not try to build it in place in
-  # the read-only source directory.
-  #git clone https://chromium.googlesource.com/chromiumos/third_party/tpm2 \
-  #    && cd tpm2 \
-  #    && git checkout 15260c8cd98eb10b4976d2161cd5cb9bc0c3adac \
-  #    && make -j24
-
-  # Install librendernodehost
-  #RUN git clone https://chromium.googlesource.com/chromiumos/platform2 \
-  #    && cd platform2 \
-  #    && git checkout 226fc35730a430344a68c34d7fe7d613f758f417 \
-  #    && cd rendernodehost \
-  #    && gcc -c src.c -o src.o \
-  #    && ar rcs librendernodehost.a src.o \
-  #    && cp librendernodehost.a /lib
-
-  # Inform pkg-config where libraries we install are placed.
-  #COPY pkgconfig/* /usr/lib/pkgconfig
-
-  # Reduces image size and prevents accidentally using /scratch files
-  #RUN rm -r /scratch /usr/bin/meson
-
-  # The manual installation of shared objects requires an ld.so.cache refresh.
-  #RUN ldconfig
-
-  # Pull down repositories that crosvm depends on to cros checkout-like locations.
-  #ENV CROS_ROOT=/
-  #ENV THIRD_PARTY_ROOT=$CROS_ROOT/third_party
-  #RUN mkdir -p $THIRD_PARTY_ROOT
-  #ENV PLATFORM_ROOT=$CROS_ROOT/platform
-  #RUN mkdir -p $PLATFORM_ROOT
-
   source $HOME/.cargo/env
-  cd "${BUILD_DIR}/platform/crosvm"
+  cd "${PLATFORM_ROOT}/crosvm"
 
   RUSTFLAGS="-C link-arg=-Wl,-rpath,\$ORIGIN -C link-arg=-L${HOME}/lib" \
     cargo build --features gpu
@@ -210,16 +160,7 @@
   cargo --version --verbose > "${OUT_DIR}/cargo_version.txt"
   rustup show > "${OUT_DIR}/rustup_show.txt"
   dpkg-query -W > "${OUT_DIR}/builder-packages.txt"
-
-  cd "${HOME}"
-  for i in $(find build -name .git -type d -print); do
-    dir="$(dirname "$i")"
-    pushd "${dir}" > /dev/null 2>&1
-    echo "${dir}" \
-      "$(git remote get-url "$(git remote show)")" \
-      "$(git rev-parse HEAD)"
-    popd > /dev/null 2>&1
-  done | sort > "${OUT_DIR}/BUILD_INFO"
+  repo manifest -r -o ${OUT_DIR}/manifest.xml
   echo Results in ${OUT_DIR}
 }
 
@@ -246,7 +187,7 @@
   x86_64_retry
 }
 
-if [[ $# -lt 2 ]]; then
+if [[ $# -lt 1 ]]; then
   echo Choosing default config
   set prepare_source x86_64_build
 fi
@@ -256,14 +197,14 @@
 for i in "$@"; do
   echo $i
   case "$i" in
+    CUSTOM_MANIFEST=*) CUSTOM_MANIFEST="${i/CUSTOM_MANIFEST=/}" ;;
     arm64_build) $i ;;
     arm64_retry) $i ;;
     prepare_source) $i ;;
-    restore_source) $i ;;
     x86_64_build) $i ;;
     x86_64_retry) $i ;;
     *) echo $i unknown 1>&2
-      echo usage: $0 'arm64_build|arm64_retry|prepare_source|restore_source|x86_64_build|x86_64_retry ...' 1>&2
+      echo usage: $0 'arm64_build|arm64_retry|prepare_source|x86_64_build|x86_64_retry ...' 1>&2
        exit 2
        ;;
   esac