Upgrade to a new Bazel version

This one has better performance and various new features. It also
prevents us from falling too far behind upstream.

Change-Id: I3b8e1c5bda886ea2e990523af9e6de2434c784be
diff --git a/aos/build/queues.bzl b/aos/build/queues.bzl
index 87d5b54..91b4978 100644
--- a/aos/build/queues.bzl
+++ b/aos/build/queues.bzl
@@ -18,10 +18,10 @@
     progress_message = 'Generating C++ code for %s' % ctx.file.src.short_path,
   )
 
-def _single_queue_file_outputs(attrs):
+def _single_queue_file_outputs(src):
   return {
-    'header': attrs.src.name + '.h',
-    'cc': attrs.src.name + '.cc',
+    'header': src.name + '.h',
+    'cc': src.name + '.cc',
   }
 
 _single_queue_file = rule(
@@ -30,7 +30,7 @@
     'src': attr.label(
       mandatory = True,
       single_file = True,
-      allow_files = FileType(['.q']),
+      allow_files = ['.q'],
     ),
     'q_deps': attr.label(
       providers = ['transitive_q_files'],
@@ -42,6 +42,7 @@
     '_queue_compiler': attr.label(
       executable = True,
       default = Label('//aos/build/queues:compiler'),
+      cfg = 'host',
     ),
   },
   outputs = _single_queue_file_outputs,
@@ -60,7 +61,7 @@
     'srcs': attr.label_list(
       mandatory = True,
       non_empty = True,
-      allow_files = FileType(['.q']),
+      allow_files = ['.q'],
     ),
     'deps': attr.label_list(
       mandatory = True,
diff --git a/aos/common/controls/BUILD b/aos/common/controls/BUILD
index 44e6db4..de100bb 100644
--- a/aos/common/controls/BUILD
+++ b/aos/common/controls/BUILD
@@ -31,6 +31,7 @@
     '//aos/testing:googletest',
     '//aos/testing:test_shm',
   ],
+  testonly = True,
 )
 
 cc_library(
diff --git a/aos/downloader/downloader.bzl b/aos/downloader/downloader.bzl
index e5179c9..0219cf7 100644
--- a/aos/downloader/downloader.bzl
+++ b/aos/downloader/downloader.bzl
@@ -61,7 +61,7 @@
   attrs = {
     '_downloader': attr.label(
       executable = True,
-      cfg = HOST_CFG,
+      cfg = 'host',
       default = Label('//aos/downloader'),
     ),
     'start_srcs': attr.label_list(
diff --git a/aos/externals/seasocks/gen_embedded.bzl b/aos/externals/seasocks/gen_embedded.bzl
index 4bba776..07a3a33 100644
--- a/aos/externals/seasocks/gen_embedded.bzl
+++ b/aos/externals/seasocks/gen_embedded.bzl
@@ -23,6 +23,7 @@
     '_gen_embedded': attr.label(
       executable = True,
       default = Label('//aos/externals/seasocks:gen_embedded'),
+      cfg = 'host',
     ),
   },
   outputs = {
diff --git a/compilers/linaro_linux_gcc_4.9.BUILD b/compilers/linaro_linux_gcc_4.9.BUILD
index c1d40b5..52748d5 100644
--- a/compilers/linaro_linux_gcc_4.9.BUILD
+++ b/compilers/linaro_linux_gcc_4.9.BUILD
@@ -91,7 +91,8 @@
     'libexec/**',
     'lib/gcc/arm-linux-gnueabihf/**',
     'include/**',
-  ], [
+  ],
+  exclude=[
     # Exclude empty files so Bazel's caching works.
     # TODO(Brian): remove this once the Bazel bug is fixed.
     '**/.install',
diff --git a/doc/make_bazel_package.sh b/doc/make_bazel_package.sh
index a42acfa..ff79ce1 100755
--- a/doc/make_bazel_package.sh
+++ b/doc/make_bazel_package.sh
@@ -15,7 +15,7 @@
 "${BAZEL_SOURCE}/compile.sh" compile
 (
 cd "${BAZEL_SOURCE}"
-./output/bazel build -c opt //scripts/packages:bazel-debian --embed_label="${VERSION}"
+./output/bazel build -c opt //scripts/packages:bazel-debian --embed_label="${VERSION}" --stamp=yes
 )
 
 cp "${BAZEL_SOURCE}/bazel-bin/scripts/packages/bazel-debian.deb" "${DEB}"
diff --git a/frc971/control_loops/BUILD b/frc971/control_loops/BUILD
index 9dee424..b5f4060 100644
--- a/frc971/control_loops/BUILD
+++ b/frc971/control_loops/BUILD
@@ -14,6 +14,7 @@
     '//aos/common/network:team_number',
     '//aos/testing:googletest',
   ],
+  testonly = True,
 )
 
 cc_test(
diff --git a/third_party/allwpilib_2016/BUILD b/third_party/allwpilib_2016/BUILD
index edc42d5..2d789d7 100644
--- a/third_party/allwpilib_2016/BUILD
+++ b/third_party/allwpilib_2016/BUILD
@@ -50,8 +50,8 @@
 ] + ['wpilibc/shared/include/%s/**/*' % d
      for d in _excluded_shared_directories] +
  ['wpilibc/*/include/%s.h' % d for d in _excluded_devices])
-_h_hdrs = glob([d + '/**/*.h' for d in _header_dirs], _bad_hdrs)
-_hpp_hdrs = glob([d + '/**/*.hpp' for d in _header_dirs], _bad_hdrs)
+_h_hdrs = glob([d + '/**/*.h' for d in _header_dirs], exclude=_bad_hdrs)
+_hpp_hdrs = glob([d + '/**/*.hpp' for d in _header_dirs], exclude=_bad_hdrs)
 
 cc_library(
  name = 'wpilib',
@@ -65,8 +65,8 @@
    'hal/lib/Athena/cpp/*.cpp',
    'hal/lib/Athena/ctre/*.cpp',
    'hal/lib/Shared/*.cpp',
- ], (['wpilibc/shared/src/%s/**/*' % d for d in _excluded_shared_directories] +
-     ['wpilibc/*/src/%s.cpp' % d for d in _excluded_devices])),
+ ], exclude=(['wpilibc/shared/src/%s/**/*' % d for d in _excluded_shared_directories] +
+             ['wpilibc/*/src/%s.cpp' % d for d in _excluded_devices])),
  copts = [
    '-Wno-unused-parameter',
    '-Wno-switch-enum',
diff --git a/third_party/eigen/BUILD b/third_party/eigen/BUILD
index fd6f889..7012271 100644
--- a/third_party/eigen/BUILD
+++ b/third_party/eigen/BUILD
@@ -5,7 +5,7 @@
   visibility = ['//visibility:public'],
   srcs = glob(['Eigen/src/**/*.h']),
   includes = ['.'],
-  hdrs = glob(['Eigen/*'], [
+  hdrs = glob(['Eigen/*'], exclude=[
     # Stuff that we don't have the dependencies for.
     'Eigen/CholmodSupport',
     'Eigen/MetisSupport',
diff --git a/third_party/gflags/BUILD b/third_party/gflags/BUILD
index 0aec151..cffd8fe 100644
--- a/third_party/gflags/BUILD
+++ b/third_party/gflags/BUILD
@@ -31,7 +31,7 @@
   srcs = glob([
     'src/*.cc',
     'src/*.h',
-  ], [
+  ], exclude=[
     'src/windows_*',
   ]) + [
     'include/gflags/gflags_declare.h',
diff --git a/third_party/libjpeg/BUILD b/third_party/libjpeg/BUILD
index a518300..a139773 100644
--- a/third_party/libjpeg/BUILD
+++ b/third_party/libjpeg/BUILD
@@ -53,7 +53,7 @@
   ]),
   hdrs = glob([
     '*.h',
-  ], [
+  ], exclude=[
     'jmemdos.h',
     'jmemmac.h',
   ]),
diff --git a/third_party/ntcore_2016/BUILD b/third_party/ntcore_2016/BUILD
index 7e79831..acddc7f 100644
--- a/third_party/ntcore_2016/BUILD
+++ b/third_party/ntcore_2016/BUILD
@@ -6,7 +6,7 @@
   srcs = glob([
     'src/**/*.cpp',
     'src/**/*.h',
-  ], [
+  ], exclude=[
     'src/networktables/**',
   ]),
   copts = [
diff --git a/third_party/protobuf/protobuf.bzl b/third_party/protobuf/protobuf.bzl
index d91cde4..cfcb69a 100644
--- a/third_party/protobuf/protobuf.bzl
+++ b/third_party/protobuf/protobuf.bzl
@@ -90,7 +90,7 @@
         "deps": attr.label_list(providers = ["proto"]),
         "includes": attr.string_list(),
         "protoc": attr.label(
-            cfg = HOST_CFG,
+            cfg = 'host',
             executable = True,
             single_file = True,
             mandatory = True,
diff --git a/third_party/seasocks/BUILD b/third_party/seasocks/BUILD
index d85e3c3..0daf8ea 100644
--- a/third_party/seasocks/BUILD
+++ b/third_party/seasocks/BUILD
@@ -6,7 +6,7 @@
   srcs = glob(['src/main/c/**/*.cpp']),
   hdrs = glob([
     'src/main/c/**/*.h',
-  ], [
+  ], exclude=[
     'src/main/internal/**/*',
   ]),
   includes = ['src/main/c'],
diff --git a/tools/bazel b/tools/bazel
index 415aeff..f6f4c52 100755
--- a/tools/bazel
+++ b/tools/bazel
@@ -24,7 +24,7 @@
   exec "${BAZEL_OVERRIDE}" "$@"
 fi
 
-readonly VERSION="201607070016+7a0d360"
+readonly VERSION="201701021712+4964f2c"
 
 readonly DOWNLOAD_DIR="$(dirname "${BASH_SOURCE[0]}")/../bazel-downloads"
 # Directory to unpack bazel into.  This must change whenever bazel changes.
diff --git a/tools/bazel.rc b/tools/bazel.rc
index 7df645e..7530102 100644
--- a/tools/bazel.rc
+++ b/tools/bazel.rc
@@ -46,3 +46,5 @@
 build --show_result 5
 # Dump the output of the failing test to stdout.
 test --test_output=errors
+
+build --experimental_sandbox_shm
diff --git a/tools/build_rules/fortran.bzl b/tools/build_rules/fortran.bzl
index f8d0add..2d6e4db 100644
--- a/tools/build_rules/fortran.bzl
+++ b/tools/build_rules/fortran.bzl
@@ -36,11 +36,11 @@
     progress_message = 'Building %s' % ctx.outputs.pic_o.short_path,
   )
 
-def _define_fortran_output(attrs):
-  if not attrs.src.name.endswith('.f'):
+def _define_fortran_output(src):
+  if not src.name.endswith('.f'):
     fail('Fortran files must end in \'.f\'', 'src')
 
-  fortran_file_base = attrs.src.name[:-2]
+  fortran_file_base = src.name[:-2]
   return {
     'pic_o': fortran_file_base + '.pic.o',
   }
diff --git a/tools/build_rules/protobuf.bzl b/tools/build_rules/protobuf.bzl
index fe6af4f..d5f4284 100644
--- a/tools/build_rules/protobuf.bzl
+++ b/tools/build_rules/protobuf.bzl
@@ -19,8 +19,8 @@
     ],
   )
 
-def _do_proto_cc_library_outputs(attr):
-  basename = attr.src.name[:-len('.proto')]
+def _do_proto_cc_library_outputs(src):
+  basename = src.name[:-len('.proto')]
   return {
     'pb_h': '%s.pb.h' % basename,
     'pb_cc': '%s.pb.cc' % basename,
@@ -37,7 +37,7 @@
     '_protoc': attr.label(
       default = Label('//third_party/protobuf:protoc'),
       executable = True,
-      cfg = HOST_CFG,
+      cfg = 'host',
     ),
     '_well_known_protos': attr.label(
       default = Label('//third_party/protobuf:well_known_protos'),
diff --git a/tools/build_rules/ruby.bzl b/tools/build_rules/ruby.bzl
index 7c02745..2c93596 100644
--- a/tools/build_rules/ruby.bzl
+++ b/tools/build_rules/ruby.bzl
@@ -51,7 +51,7 @@
   ),
   'data': attr.label_list(
     allow_files = True,
-    cfg = DATA_CFG,
+    cfg = 'data',
   ),
 }
 
@@ -84,6 +84,7 @@
   attrs = _ruby_attrs + {
     '_ruby_linker': attr.label(
       executable = True,
+      cfg = 'host',
       default = Label('//tools/ruby:standalone_ruby'),
     )
   },
diff --git a/tools/cpp/BUILD b/tools/cpp/BUILD
index 40e674f..053b0e4 100644
--- a/tools/cpp/BUILD
+++ b/tools/cpp/BUILD
@@ -23,27 +23,13 @@
   srcs = [],
 )
 
-# This is the entry point for --crosstool_top.  Toolchains are found
-# by lopping off the name of --crosstool_top and searching for
-# 'cc-compiler-${CPU}' in this BUILD file, where CPU is the target CPU
-# specified in --cpu.
-#
-# This file group should include
-#   * all cc_toolchain targets supported
-#   * all file groups that said cc_toolchain might refer to,
-# including the default_grte_top setting in the CROSSTOOL
-# protobuf.
-filegroup(
+# This is the entry point for --crosstool_top.
+cc_toolchain_suite(
   name = 'toolchain',
-  srcs = [
-    ':cc-compiler-k8',
-    ':cc-compiler-roborio',
-    '@arm_frc_linux_gnueabi_repo//:compiler_components',
-    ':roborio-compiler-files',
-    ':flags_compiler_inputs',
-    '@linaro_linux_gcc_4_9_repo//:compiler_components',
-    ':linaro-gcc-files',
-  ],
+  toolchains = {
+    'k8|clang': ':cc-compiler-k8',
+    'roborio|gcc': ':cc-compiler-roborio',
+  },
 )
 
 # Compiler inputs given by --copt etc in //tools:bazel.rc.
@@ -90,6 +76,7 @@
     '//tools/cpp/arm-frc-linux-gnueabi:ld',
     '//tools/cpp/arm-frc-linux-gnueabi:ar',
     '//tools/cpp/arm-frc-linux-gnueabi:gcc',
+    '//tools/cpp/arm-frc-linux-gnueabi:libs',
     '@arm_frc_linux_gnueabi_repo//:compiler_pieces',
   ],
 )
@@ -98,6 +85,8 @@
   srcs = [
     '//tools/cpp/arm-frc-linux-gnueabi:gcc',
     '//tools/cpp/arm-frc-linux-gnueabi:ld',
+    '@arm_frc_linux_gnueabi_repo//:compiler_components',
+    '@arm_frc_linux_gnueabi_repo//:compiler_pieces',
   ],
 )
 
diff --git a/tools/cpp/CROSSTOOL b/tools/cpp/CROSSTOOL
index 96fb1b9..5a374af 100644
--- a/tools/cpp/CROSSTOOL
+++ b/tools/cpp/CROSSTOOL
@@ -351,7 +351,7 @@
   cxx_builtin_include_directory: "%package(@arm_frc_linux_gnueabi_repo//usr/arm-frc-linux-gnueabi/usr/include)%"
 
   linker_flag: "-lstdc++"
-  #linker_flag: "-B/usr/bin/"
+  linker_flag: "-Ltools/cpp/arm-frc-linux-gnueabi/libs"
 
   feature {
     name: "opt"
diff --git a/tools/cpp/arm-frc-linux-gnueabi/BUILD b/tools/cpp/arm-frc-linux-gnueabi/BUILD
index 17a10c2..95000c3 100644
--- a/tools/cpp/arm-frc-linux-gnueabi/BUILD
+++ b/tools/cpp/arm-frc-linux-gnueabi/BUILD
@@ -77,3 +77,10 @@
     ':strip',
   ],
 )
+
+filegroup(
+  name = 'libs',
+  srcs = glob([
+    'libs/**',
+  ]),
+)
diff --git a/y2016/dashboard/BUILD b/y2016/dashboard/BUILD
index 65e650c..fd2fdbb 100644
--- a/y2016/dashboard/BUILD
+++ b/y2016/dashboard/BUILD
@@ -3,7 +3,7 @@
 
 gen_embedded(
   name = 'gen_embedded',
-  srcs = glob(['www_defaults/**/*'], ['www/**/*']),
+  srcs = glob(['www_defaults/**/*'], exclude=['www/**/*']),
 )
 
 aos_downloader_dir(