Make protobuf build

This includes the following:
  - Setting up dependencies protobuf needs
  - A few hacks to make protobuf work with our version of Bazel (going
    to upgrade soon...)
  - A few tweaks to make protobuf be happy in third_party
  - Commenting out a few things we don't care about that didn't want to
    work

Tested with -c fastbuild for all 3 toolchains.

Change-Id: I02c50cd7e82e509bd7973f2e7cecf9b7d183783e
diff --git a/WORKSPACE b/WORKSPACE
index 964fce2..d4b58f9 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -71,3 +71,34 @@
   name = 'ni-libraries',
   actual = '@allwpilib_ni_libraries_repo//:ni-libraries',
 )
+
+# Downloaded from:
+# https://pypi.python.org/packages/source/s/six/six-1.10.0.tar.gz
+new_http_archive(
+  name = 'six_repo',
+  build_file = 'debian/six.BUILD',
+  sha256 = '105f8d68616f8248e24bf0e9372ef04d3cc10104f1980f54d57b2ce73a5ad56a',
+  url = 'http://frc971.org/Build-Dependencies/six-1.10.0.tar.gz',
+  strip_prefix = 'six-1.10.0',
+)
+
+# For protobuf. Don't use these.
+bind(
+  name = 'six',
+  actual = '@six_repo//:six',
+)
+bind(
+  name = 'gtest',
+  actual = '//third_party/googletest:googlemock',
+)
+bind(
+  name = 'gtest_main',
+  actual = '//third_party/googletest:googlemock_main',
+)
+
+new_http_archive(
+  name = 'python_import_helpers',
+  build_file = 'third_party/python_import_helpers.BUILD',
+  url = 'http://frc971.org/Build-Dependencies/empty.tar.gz',
+  sha256 = '71939a7d75585a57d2e99a33d39f391764d8f930f9a16acf32e00c5d3f432aa0',
+)
diff --git a/debian/six.BUILD b/debian/six.BUILD
new file mode 100644
index 0000000..87cd318
--- /dev/null
+++ b/debian/six.BUILD
@@ -0,0 +1,7 @@
+py_library(
+  name = 'six',
+  visibility = ['//visibility:public'],
+  srcs = [
+    'six.py',
+  ],
+)
diff --git a/third_party/googletest/BUILD b/third_party/googletest/BUILD
index f342e66..fcec287 100644
--- a/third_party/googletest/BUILD
+++ b/third_party/googletest/BUILD
@@ -95,6 +95,15 @@
 )
 
 cc_library(
+    name = "googlemock_main",
+    visibility = ["//visibility:public"],
+    deps = [
+        ":googlemock",
+        ":googletest_main",
+    ],
+)
+
+cc_library(
 	name = "googletest_sample_libs",
 	hdrs = [
 	    "googletest/samples/prime_tables.h",
diff --git a/third_party/protobuf/BUILD b/third_party/protobuf/BUILD
index 3cac4a8..bb5d889 100644
--- a/third_party/protobuf/BUILD
+++ b/third_party/protobuf/BUILD
@@ -2,18 +2,34 @@
 
 licenses(["notice"])
 
+load("/tools/build_rules/select", "compiler_select")
+
 ################################################################################
 # Protobuf Runtime Library
 ################################################################################
 
 COPTS = [
     "-DHAVE_PTHREAD",
+    "-DGOOGLE_THIRD_PARTY_PROTOBUF",
     "-Wall",
     "-Wwrite-strings",
     "-Woverloaded-virtual",
     "-Wno-sign-compare",
-    "-Wno-error=unused-function",
-]
+    "-Wno-unused-function",
+    "-Wno-unused-parameter",
+    "-Wno-format-nonliteral",
+    "-Wno-switch-enum",
+    "-Wno-missing-field-initializers",
+    "-Wno-ignored-qualifiers",
+] + compiler_select({
+    "gcc": [
+        "-Wno-error=cast-align",
+    ],
+    "clang": [
+        "-Wno-unused-const-variable",
+        "-Wno-unused-private-field",
+    ],
+})
 
 # Bazel should provide portable link_opts for pthread.
 LINK_OPTS = ["-lpthread"]
@@ -127,6 +143,7 @@
     deps = [":protobuf_lite"],
 )
 
+'''
 objc_library(
     name = "protobuf_objc",
     hdrs = ["objectivec/GPBProtocolBuffers.h"],
@@ -134,6 +151,7 @@
     non_arc_srcs = ["objectivec/GPBProtocolBuffers.m"],
     visibility = ["//visibility:public"],
 )
+'''
 
 RELATIVE_WELL_KNOWN_PROTOS = [
     # AUTOGEN(well_known_protos)
@@ -342,6 +360,7 @@
     default_runtime = ":protobuf",
     protoc = ":protoc",
     deps = [":cc_wkt_protos"],
+    copts = COPTS,
 )
 
 COMMON_TEST_SRCS = [
@@ -366,6 +385,7 @@
         ":protoc_lib",
         "//external:gtest",
     ],
+    copts = COPTS,
 )
 
 cc_test(
@@ -465,7 +485,7 @@
         "wellknown.srcjar"
     ],
     cmd = "$(location :protoc) --java_out=$(@D)/wellknown.jar" +
-        " -Isrc $(SRCS) " +
+        " -Ithird_party/protobuf/src $(SRCS) " +
         " && mv $(@D)/wellknown.jar $(@D)/wellknown.srcjar",
     tools = [":protoc"],
 )
@@ -517,6 +537,7 @@
     include = "python",
 )
 
+'''
 cc_binary(
     name = "internal/_api_implementation.so",
     srcs = ["python/google/protobuf/internal/api_implementation.cc"],
@@ -527,7 +548,7 @@
     linkstatic = 1,
     deps = select({
         "//conditions:default": [],
-        ":use_fast_cpp_protos": ["//util/python:python_headers"],
+        ":use_fast_cpp_protos": ["//third_party/protobuf/util/python:python_headers"],
     }),
 )
 
@@ -550,9 +571,10 @@
         ":protobuf",
     ] + select({
         "//conditions:default": [],
-        ":use_fast_cpp_protos": ["//util/python:python_headers"],
+        ":use_fast_cpp_protos": ["//third_party/protobuf/util/python:python_headers"],
     }),
 )
+'''
 
 config_setting(
     name = "use_fast_cpp_protos",
diff --git a/third_party/protobuf/protobuf.bzl b/third_party/protobuf/protobuf.bzl
index f674a6c..0cc195b 100644
--- a/third_party/protobuf/protobuf.bzl
+++ b/third_party/protobuf/protobuf.bzl
@@ -1,14 +1,15 @@
 # -*- mode: python; -*- PYTHON-PREPROCESSING-REQUIRED
 
 def _GetPath(ctx, path):
-  if ctx.label.workspace_root:
-    return ctx.label.workspace_root + '/' + path
-  else:
-    return path
+  if str(ctx.label).startswith('@'):
+    fail('External labels not supported for now')
+  return path
 
 def _GenDir(ctx):
   if not ctx.attr.includes:
-    return ctx.label.workspace_root
+    if str(ctx.label).startswith('@'):
+      fail('External labels not supported for now')
+    return ''
   if not ctx.attr.includes[0]:
     return _GetPath(ctx, ctx.label.package)
   if not ctx.label.package:
@@ -34,13 +35,17 @@
 
   path = path[len(include):]
 
-  if not path.startswith(PACKAGE_NAME):
-    fail("The package %s is not within the path %s" % (PACKAGE_NAME, path))
+  package_name = PACKAGE_NAME
+  if not package_name.startswith('third_party/protobuf'):
+    fail('The package %s is not a protobuf package' % package_name)
+  package_name = package_name[len('third_party/protobuf/'):]
+  if not path.startswith(package_name):
+    fail("The package %s is not within the path %s" % (package_name, path))
 
-  if not PACKAGE_NAME:
+  if not package_name:
     return path
 
-  return path[len(PACKAGE_NAME)+1:]
+  return path[len(package_name)+1:]
 
 def _proto_gen_impl(ctx):
   """General implementation for generating protos"""
@@ -265,6 +270,7 @@
 
   if default_runtime and not default_runtime in py_libs + deps:
     py_libs += [default_runtime]
+  py_libs += ['@python_import_helpers//:google_protobuf_importer']
 
   native.py_library(
       name=name,
diff --git a/third_party/protobuf/python/google/protobuf/internal/test_util.py b/third_party/protobuf/python/google/protobuf/internal/test_util.py
index ac88fa8..091cb61 100755
--- a/third_party/protobuf/python/google/protobuf/internal/test_util.py
+++ b/third_party/protobuf/python/google/protobuf/internal/test_util.py
@@ -596,9 +596,9 @@
   # Search up the directory tree looking for the C++ protobuf source code.
   path = '.'
   while os.path.exists(path):
-    if os.path.exists(os.path.join(path, 'src/google/protobuf')):
+    if os.path.exists(os.path.join(path, 'third_party/protobuf/src/google/protobuf')):
       # Found it.  Load the golden file from the testdata directory.
-      full_path = os.path.join(path, 'src/google/protobuf/testdata', filename)
+      full_path = os.path.join(path, 'third_party/protobuf/src/google/protobuf/testdata', filename)
       return open(full_path, 'rb')
     path = os.path.join(path, '..')
 
diff --git a/third_party/protobuf/util/python/BUILD b/third_party/protobuf/util/python/BUILD
index 358c381..073ea63 100644
--- a/third_party/protobuf/util/python/BUILD
+++ b/third_party/protobuf/util/python/BUILD
@@ -1,3 +1,5 @@
+licenses(["notice"])
+
 # This is a placeholder for python headers. Projects needing to use
 # fast cpp protos in protobuf's python interface should build with
 # --define=use_fast_cpp_protos=true, and in addition, provide
diff --git a/third_party/python_import_helpers.BUILD b/third_party/python_import_helpers.BUILD
new file mode 100644
index 0000000..38a68ec
--- /dev/null
+++ b/third_party/python_import_helpers.BUILD
@@ -0,0 +1,19 @@
+# This repository is used to create helper __init__.py files which import things
+# under third_party at the root of the Python import namespace instead of
+# requiring third_party.xyz. in front of them.
+
+genrule(
+  name = 'google_protobuf_importer_init',
+  outs = ['google/__init__.py'],
+  cmd = 'echo "%s" > $@' % '\n'.join([
+    'import sys',
+    'import third_party.protobuf.google.protobuf',
+    'sys.modules[\'google.protobuf\'] = third_party.protobuf.google.protobuf',
+  ]),
+)
+
+py_library(
+  name = 'google_protobuf_importer',
+  visibility = ['//visibility:public'],
+  srcs = [':google_protobuf_importer_init'],
+)