make caffe2_gtest also uses globalinit. Not allowing init to run twice.
diff --git a/caffe2/core/BREW b/caffe2/core/BREW
index 2c9647d..b2c9e12 100644
--- a/caffe2/core/BREW
+++ b/caffe2/core/BREW
@@ -76,7 +76,7 @@
   deps = [
       ":core",
       "//gtest:gtest",
-      "//gtest:gtest_main",
+      "//gtest:caffe2_gtest_main",
   ],
 )
 
@@ -88,7 +88,7 @@
   deps = [
       ":core_gpu",
       "//gtest:gtest",
-      "//gtest:gtest_main",
+      "//gtest:caffe2_gtest_main",
   ],
 )
 
@@ -100,7 +100,7 @@
   deps = [
       ":core_gpu",
       "//gtest:gtest",
-      "//gtest:gtest_main",
+      "//gtest:caffe2_gtest_main",
   ],
 )
 
@@ -110,6 +110,17 @@
   deps = [
       ":core",
       "//gtest:gtest",
-      "//gtest:gtest_main",
+      "//gtest:caffe2_gtest_main",
   ],
 )
+
+cc_test(
+  name = "init_test",
+  srcs = ["init_test.cc"],
+  deps = [
+      ":core",
+      "//gtest:gtest",
+      "//gtest:caffe2_gtest_main",
+  ],
+)
+
diff --git a/caffe2/core/init.h b/caffe2/core/init.h
index d49f685..077a1c6 100644
--- a/caffe2/core/init.h
+++ b/caffe2/core/init.h
@@ -56,6 +56,10 @@
 
 // Initialize the global environment of caffe2.
 inline bool GlobalInit(int* pargc, char*** pargv) {
+  static bool global_init_was_already_run = false;
+  if (global_init_was_already_run) {
+    LOG(FATAL) << "GlobalInit has already been called: did you double-call?";
+  }
   // Google flags.
   ::gflags::ParseCommandLineFlags(pargc, pargv, true);
   // Google logging.
@@ -63,8 +67,10 @@
   // Provide a backtrace on segfault.
   ::google::InstallFailureSignalHandler();
   // All other initialization functions.
-  return internal::Caffe2InitializeRegistry::Registry()
+  bool retval = internal::Caffe2InitializeRegistry::Registry()
       ->RunRegisteredInitFunctions();
+  global_init_was_already_run = true;
+  return retval;
 }
 
 
diff --git a/caffe2/core/init_test.cc b/caffe2/core/init_test.cc
new file mode 100644
index 0000000..647f351
--- /dev/null
+++ b/caffe2/core/init_test.cc
@@ -0,0 +1,35 @@
+#include <iostream>
+#include <memory>
+
+#include "caffe2/core/init.h"
+#include "gtest/gtest.h"
+#include "glog/logging.h"
+
+namespace caffe2 {
+namespace {
+bool gTestInitFunctionHasBeenRun = false;
+
+void TestInitFunction() {
+  gTestInitFunctionHasBeenRun = true;
+}
+REGISTER_CAFFE2_INIT_FUNCTION(TestInitFunction,
+                              &TestInitFunction,
+                              "Just a test to see if GlobalInit invokes "
+                              "registered functions correctly.");
+}  // namespace
+
+TEST(InitTest, TestInitFunctionHasRun) {
+  EXPECT_TRUE(gTestInitFunctionHasBeenRun);
+}
+
+TEST(InitDeathTest, CannotRerunGlobalInit) {
+  int dummy_argc = 1;
+  char* dummy_name = "foo";
+  char** dummy_argv = &dummy_name;
+  EXPECT_DEATH(caffe2::GlobalInit(&dummy_argc, &dummy_argv),
+               "blabla");
+}
+
+}  // namespace caffe2
+
+
diff --git a/caffe2/end_to_end_test/BREW b/caffe2/end_to_end_test/BREW
index d9bbbac..903a3f5 100644
--- a/caffe2/end_to_end_test/BREW
+++ b/caffe2/end_to_end_test/BREW
@@ -11,6 +11,6 @@
       "//caffe2/utils:proto_utils",
       "//data/toy:toy_models",
       "//data/mnist:mnist_models",
-      "//gtest:gtest_main",
+      "//gtest:caffe2_gtest_main",
   ],
 )
diff --git a/caffe2/operators/BREW b/caffe2/operators/BREW
index af2b82e..d73c44e 100644
--- a/caffe2/operators/BREW
+++ b/caffe2/operators/BREW
@@ -96,6 +96,6 @@
       ":core_ops_gpu",
       ":core_ops_cudnn",
       "//data/mnist:mnist_minidb",
-      "//gtest:gtest_main",
+      "//gtest:caffe2_gtest_main",
   ]
 )
diff --git a/caffe2/utils/BREW b/caffe2/utils/BREW
index b1466be..65d7b97 100644
--- a/caffe2/utils/BREW
+++ b/caffe2/utils/BREW
@@ -46,7 +46,7 @@
   deps = [
       ":math",
       "//caffe2/proto:caffe2_proto",
-      "//gtest:gtest_main",
+      "//gtest:caffe2_gtest_main",
       "//caffe2/core:core",
   ],
 )
@@ -65,7 +65,7 @@
   ],
   deps = [
       ":simple_queue",
-      "//gtest:gtest_main",
+      "//gtest:caffe2_gtest_main",
   ],
 )
 
diff --git a/gtest/BREW b/gtest/BREW
index ce38f5c..0e491fa7 100644
--- a/gtest/BREW
+++ b/gtest/BREW
@@ -6,23 +6,21 @@
 )
 
 cc_library(
-  name = "gtest_main",
-  srcs = ["gtest_main.cc"],
+  name = "caffe2_gtest_main",
+  srcs = ["caffe2_gtest_main.cc"],
   deps = [
       ":gtest",
-      "//third_party/gflags:gflags",
-      "//third_party/glog:glog"
+      "//caffe2/core:core"
   ],
   cflags = ["-DGTEST_USE_OWN_TR1_TUPLE=1"],
 )
 
 cc_test(
-  name = "gtest_main_binary",
-  srcs = ["gtest_main.cc"],
+  name = "caffe2_gtest_main_binary",
+  srcs = ["caffe2_gtest_main.cc"],
   deps = [
       ":gtest",
-      "//third_party/gflags:gflags",
-      "//third_party/glog:glog",
+      "//caffe2/core:core",
   ],
   cflags = ["-DGTEST_USE_OWN_TR1_TUPLE=1"],
 )
\ No newline at end of file
diff --git a/gtest/gtest_main.cc b/gtest/caffe2_gtest_main.cc
similarity index 89%
rename from gtest/gtest_main.cc
rename to gtest/caffe2_gtest_main.cc
index 9db62a1..e4174e1 100644
--- a/gtest/gtest_main.cc
+++ b/gtest/caffe2_gtest_main.cc
@@ -29,20 +29,14 @@
 
 #include <iostream>
 
-#include "glog/logging.h"
-#include "gflags/gflags.h"
 #include "gtest/gtest.h"
+#include "caffe2/core/init.h"
 
 DEFINE_string(caffe_test_root, "gen/", "The root of the caffe test folder.");
 
-#ifndef GFLAGS_GFLAGS_H_
-  namespace gflags = google;
-#endif
-
 GTEST_API_ int main(int argc, char **argv) {
   // std::cout << "Running main() from gtest_main.cc\n";
   testing::InitGoogleTest(&argc, argv);
-  gflags::ParseCommandLineFlags(&argc, &argv, true);
-  google::InitGoogleLogging(argv[0]);
+  caffe2::GlobalInit(&argc, &argv);
   return RUN_ALL_TESTS();
 }