Upgrade to a new, fixed version of Bazel

Change-Id: I90f63a9cb40bcfde7552a79355dfbf9123bcd798
diff --git a/WORKSPACE b/WORKSPACE
index 6d6f7e1..0d4eb2f 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -1,3 +1,5 @@
+workspace(name = 'org_frc971')
+
 new_local_repository(
   name = 'usr_repo',
   path = '/usr',
diff --git a/aos/common/logging/BUILD b/aos/common/logging/BUILD
index d4d3ca5..8b28482 100644
--- a/aos/common/logging/BUILD
+++ b/aos/common/logging/BUILD
@@ -176,6 +176,9 @@
   hdrs = [
     'implementations.h',
   ],
+  linkopts = [
+    '-pthread',
+  ],
   deps = [
     '//aos/common:die',
     '//aos/common:time',
diff --git a/aos/downloader/downloader.bzl b/aos/downloader/downloader.bzl
index 0085804..e5179c9 100644
--- a/aos/downloader/downloader.bzl
+++ b/aos/downloader/downloader.bzl
@@ -6,7 +6,7 @@
     content = '\n'.join([
       '#!/bin/bash',
       'set -e',
-      'cd "${BASH_SOURCE[@]}.runfiles"',
+      'cd "${BASH_SOURCE[@]}.runfiles/%s"' % ctx.workspace_name,
     ] + ['%s %s --dirs %s -- %s "$@"' % (
        ctx.executable._downloader.short_path,
        ' '.join([src.short_path for src in d.downloader_srcs]),
diff --git a/aos/linux_code/ipc_lib/BUILD b/aos/linux_code/ipc_lib/BUILD
index 98aca55..95b30ce 100644
--- a/aos/linux_code/ipc_lib/BUILD
+++ b/aos/linux_code/ipc_lib/BUILD
@@ -8,6 +8,9 @@
   hdrs = [
     'aos_sync.h',
   ],
+  linkopts = [
+    '-pthread',
+  ],
   deps = [
     '//aos/common/logging',
     '//aos/common:once',
diff --git a/debian/slycot.BUILD b/debian/slycot.BUILD
index 64eb358..55274b1 100644
--- a/debian/slycot.BUILD
+++ b/debian/slycot.BUILD
@@ -66,15 +66,15 @@
     ':slycot_c',
   ],
   linkopts = ['-shared', '-lblas', '-llapack'],
-  linkstatic=0,
+  linkstatic = False,
 )
 
 # Generate the _wrapper file which loads _fortranwrapper and pretends.
 genrule(
   name = '_wrapper',
   outs = ['slycot/_wrapper.py'],
-  cmd = 'echo "from _fortranwrapper import *" > $(OUTS)',
-  output_to_bindir=1,
+  cmd = 'echo "from external.slycot_repo._fortranwrapper import *" > $(OUTS)',
+  output_to_bindir = True,
 )
 
 # Now present a python library for slycot
diff --git a/doc/make_bazel_package.sh b/doc/make_bazel_package.sh
index 1c10df0..a42acfa 100755
--- a/doc/make_bazel_package.sh
+++ b/doc/make_bazel_package.sh
@@ -10,12 +10,12 @@
 BAZEL_SOURCE="$1"
 
 VERSION="$(date +%Y%m%d%H%M)+$(GIT_DIR="${BAZEL_SOURCE}/.git" git rev-parse --short HEAD)"
-DEB="bazel_${VERSION}.deb"
+DEB="bazel_${VERSION}_amd64.deb"
 
 "${BAZEL_SOURCE}/compile.sh" compile
 (
 cd "${BAZEL_SOURCE}"
-./output/bazel build //scripts/packages:bazel-debian --embed_label="${VERSION}"
+./output/bazel build -c opt //scripts/packages:bazel-debian --embed_label="${VERSION}"
 )
 
 cp "${BAZEL_SOURCE}/bazel-bin/scripts/packages/bazel-debian.deb" "${DEB}"
diff --git a/third_party/gperftools/BUILD b/third_party/gperftools/BUILD
index ada8712..4fa332f 100644
--- a/third_party/gperftools/BUILD
+++ b/third_party/gperftools/BUILD
@@ -137,6 +137,9 @@
     '//third_party/empty_config_h',
   ],
   copts = common_copts,
+  linkopts = [
+    '-pthread',
+  ],
   alwayslink = True,
   nocopts = '-std=gnu\+\+1y',
 )
diff --git a/tools/bazel b/tools/bazel
new file mode 100755
index 0000000..39951f3
--- /dev/null
+++ b/tools/bazel
@@ -0,0 +1,105 @@
+#!/bin/bash
+
+# The bazel script calls this instead of the bazel-real binary which is
+# installed next to it. This script downloads a specific version of Bazel and
+# then calls that.
+
+# Alternatively, if the environment variable BAZEL_OVERRIDE is set, that will be
+# run directly (after printing a message). That is intended for testing only.
+
+# This script operates based on the assumption that any directory of the correct
+# name is a fully extracted, valid Bazel installation. It is careful to avoid
+# putting an invalid directory at that name at any point.
+
+set -e
+set -u
+set -o pipefail
+
+if [[ -n "${BAZEL_OVERRIDE+x}" ]]; then
+  tput setaf 1 >&2
+  echo -n "Actually calling " >&2
+  tput setaf 3 >&2
+  echo "${BAZEL_OVERRIDE}" >&2
+  tput sgr0 >&2
+  exec "${BAZEL_OVERRIDE}" "$@"
+fi
+
+readonly VERSION="201607070016+7a0d360"
+
+readonly DOWNLOAD_DIR="$(dirname "${BASH_SOURCE[0]}")/../bazel-downloads"
+# Directory to unpack bazel into.  This must change whenever bazel changes.
+readonly VERSION_DIR="${DOWNLOAD_DIR}/${VERSION}-v1"
+readonly VERSION_BAZEL="${VERSION_DIR}/usr/bin/bazel"
+
+# Creating might fail if another invocation is racing us.
+if [[ ! -d "${DOWNLOAD_DIR}" ]]; then
+  mkdir "${DOWNLOAD_DIR}" || true
+fi
+if [[ ! -d "${DOWNLOAD_DIR}" ]]; then
+  echo "Failed to create ${DOWNLOAD_DIR}" >&2
+  exit 1
+fi
+
+readonly INSTALLER_NAME="bazel_${VERSION}_amd64.deb"
+readonly DOWNLOAD_URL="http://frc971.org/Build-Dependencies/${INSTALLER_NAME}"
+
+if [[ ! -d "${VERSION_DIR}" ]]; then
+  echo "Downloading Bazel version ${VERSION} from ${DOWNLOAD_URL}..." >&2
+
+  # A temporary directory which is definitely on the same filesystem as our final
+  # destination, which is important so we can atomically move it.
+  # If this move is non-atomic, then a concurrent Bazel command (like the verifier
+  # uses several of) could use a half-copied Bazel installation.
+  TEMP_DIR="$(mktemp --directory --tmpdir="${DOWNLOAD_DIR}")"
+  readonly TEMP_DIR
+
+  ( cd "${TEMP_DIR}"
+    wget "${DOWNLOAD_URL}" -O "${INSTALLER_NAME}" --no-verbose --show-progress
+    echo "Unpacking Bazel version ${VERSION}..." >&2
+    dpkg-deb -x "${INSTALLER_NAME}" extracted
+  )
+
+  touch "${TEMP_DIR}/extracted/usr/bin/bazel.bazelrc"
+
+  # Careful: somebody else might have already done it. If they manage to make
+  # the move between our check and our move, then we'll end up with a random
+  # extracted directory which won't do anybody any harm. If somebody else does
+  # that first, then our move will fail.
+  if [[ ! -d "${VERSION_DIR}" ]]; then
+    mv "${TEMP_DIR}/extracted" "${VERSION_DIR}" || true
+  fi
+  if [[ ! -d "${VERSION_DIR}" ]]; then
+    echo "Failed to create ${VERSION_DIR}" >&2
+    exit 1
+  fi
+  rm -rf "${TEMP_DIR}"
+  echo "Done downloading Bazel version ${VERSION}"
+fi
+
+if [[ -x "${VERSION_BAZEL}-real" ]]; then
+  exec -a "${VERSION_BAZEL}" env -i \
+      HOSTNAME="${HOSTNAME}" \
+      SHELL="${SHELL}" \
+      USER="${USER}" \
+      PATH="${PATH}" \
+      LANG="${LANG}" \
+      HOME="${HOME}" \
+      LOGNAME="${LOGNAME}" \
+      TERM="${TERM}" \
+      "${VERSION_BAZEL}-real" "$@"
+fi
+if [[ -x "${VERSION_BAZEL}" ]]; then
+  exec env -i \
+      HOSTNAME="${HOSTNAME}" \
+      SHELL="${SHELL}" \
+      USER="${USER}" \
+      PATH="${PATH}" \
+      LANG="${LANG}" \
+      HOME="${HOME}" \
+      LOGNAME="${LOGNAME}" \
+      TERM="${TERM}" \
+      "${VERSION_BAZEL}" "$@"
+fi
+
+echo "Can't find the real bazel!" >&2
+exit 1