gtest/gmock: build checked out copy

Now that gtest/gmock are part of the manifest, build them directly
and store the results in out/.

BUG=b/27707648
TEST+`make` still works, as does the unittests

Change-Id: I8a8235b4bc0100731168a44c6553859a5d85bbe1
Reviewed-on: https://weave-review.googlesource.com/2952
Reviewed-by: Alex Vakulenko <avakulenko@google.com>
diff --git a/Makefile b/Makefile
index 70f7ee7..00f237c 100644
--- a/Makefile
+++ b/Makefile
@@ -16,6 +16,8 @@
 	-Iinclude \
 	-Ithird_party/chromium \
 	-Ithird_party/include \
+	-Ithird_party/googletest/googletest/include \
+	-Ithird_party/googletest/googlemock/include \
 	-Ithird_party/libuweave \
 	-Ithird_party/modp_b64/modp_b64
 
@@ -90,7 +92,10 @@
 	rm -f $@
 	$(AR) crsT $@ $^
 
-all : out/$(BUILD_MODE)/libweave.so all-examples out/$(BUILD_MODE)/libweave_exports_testrunner out/$(BUILD_MODE)/libweave_testrunner
+all-libs : out/$(BUILD_MODE)/libweave.so
+all-tests : out/$(BUILD_MODE)/libweave_exports_testrunner out/$(BUILD_MODE)/libweave_testrunner
+
+all : all-libs all-examples all-tests
 
 clean :
 	rm -rf out
diff --git a/README.md b/README.md
index fb8d092..0d691f3 100644
--- a/README.md
+++ b/README.md
@@ -78,8 +78,8 @@
 ### For tests
 
   - cmake
-  - gtest (included; see third_party/get_gtest.sh)
-  - gmock (included; see third_party/get_gtest.sh)
+  - gtest (included; see third_party/googletest/googletest/)
+  - gmock (included; see third_party/googletest/googlemock/)
 
 ### For examples
 
diff --git a/tests.mk b/tests.mk
index 4e11f7e..d0042db 100644
--- a/tests.mk
+++ b/tests.mk
@@ -15,7 +15,7 @@
 
 weave_test_obj_files := $(WEAVE_TEST_SRC_FILES:%.cc=out/$(BUILD_MODE)/%.o)
 
-$(weave_test_obj_files) : out/$(BUILD_MODE)/%.o : %.cc third_party/include/gtest/gtest.h
+$(weave_test_obj_files) : out/$(BUILD_MODE)/%.o : %.cc
 	mkdir -p $(dir $@)
 	$(CXX) $(DEFS_$(BUILD_MODE)) $(INCLUDES) $(CFLAGS) $(CFLAGS_$(BUILD_MODE)) $(CFLAGS_CC) -c -o $@ $<
 
@@ -24,7 +24,7 @@
 
 weave_unittest_obj_files := $(WEAVE_UNITTEST_SRC_FILES:%.cc=out/$(BUILD_MODE)/%.o)
 
-$(weave_unittest_obj_files) : out/$(BUILD_MODE)/%.o : %.cc third_party/include/gtest/gtest.h
+$(weave_unittest_obj_files) : out/$(BUILD_MODE)/%.o : %.cc
 	mkdir -p $(dir $@)
 	$(CXX) $(DEFS_TEST) $(INCLUDES) $(CFLAGS) $(CFLAGS_$(BUILD_MODE)) $(CFLAGS_CC) -c -o $@ $<
 
@@ -34,8 +34,8 @@
 	$(third_party_chromium_base_unittest_obj_files) \
 	out/$(BUILD_MODE)/libweave_common.a \
 	out/$(BUILD_MODE)/libweave-test.a \
-	third_party/lib/gmock.a \
-	third_party/lib/gtest.a
+	$(third_party_gtest_lib) \
+	$(third_party_gmock_lib)
 	$(CXX) -o $@ $^ $(CFLAGS) -lcrypto -lexpat -lpthread -lrt -Lthird_party/lib
 
 test : out/$(BUILD_MODE)/libweave_testrunner
@@ -46,7 +46,7 @@
 
 weave_exports_unittest_obj_files := $(WEAVE_EXPORTS_UNITTEST_SRC_FILES:%.cc=out/$(BUILD_MODE)/%.o)
 
-$(weave_exports_unittest_obj_files) : out/$(BUILD_MODE)/%.o : %.cc third_party/include/gtest/gtest.h
+$(weave_exports_unittest_obj_files) : out/$(BUILD_MODE)/%.o : %.cc
 	mkdir -p $(dir $@)
 	$(CXX) $(DEFS_TEST) $(INCLUDES) $(CFLAGS) $(CFLAGS_$(BUILD_MODE)) $(CFLAGS_CC) -c -o $@ $<
 
@@ -55,8 +55,8 @@
 	out/$(BUILD_MODE)/libweave.so \
 	out/$(BUILD_MODE)/libweave-test.a \
 	out/$(BUILD_MODE)/src/test/weave_testrunner.o \
-	third_party/lib/gmock.a \
-	third_party/lib/gtest.a
+	$(third_party_gtest_lib) \
+	$(third_party_gmock_lib)
 	$(CXX) -o $@ $^ $(CFLAGS) -lcrypto -lexpat -lpthread -lrt -Lthird_party/lib -Wl,-rpath=out/$(BUILD_MODE)/
 
 export-test : out/$(BUILD_MODE)/libweave_exports_testrunner
diff --git a/third_party/get_gtest.sh b/third_party/get_gtest.sh
deleted file mode 100755
index 9b546ab..0000000
--- a/third_party/get_gtest.sh
+++ /dev/null
@@ -1,31 +0,0 @@
-#!/bin/bash
-# Copyright 2015 The Weave Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style license that can be
-# found in the LICENSE file.
-
-# Make gtest and gmock.
-cd $(dirname "$0")
-THIRD_PARTY=$(pwd)
-
-mkdir -p include lib
-
-rm -rf $THIRD_PARTY/googletest
-git clone https://github.com/google/googletest.git || exit 1
-cd googletest
-
-# gtest is in process of changing of dir structure and it has broken build
-# files. So this is temporarily workaround to fix that.
-git reset --hard 82b11b8cfcca464c2ac74b623d04e74452e74f32
-mv googletest googlemock/gtest
-
-cd $THIRD_PARTY/googletest/googlemock/gtest/make || exit 1
-make gtest.a || exit 1
-cp -rf ../include/* $THIRD_PARTY/include/ || exit 1
-cp -rf gtest.a $THIRD_PARTY/lib/ || exit 1
-
-cd $THIRD_PARTY/googletest/googlemock/make || exit 1
-make gmock.a || exit 1
-cp -rf ../include/* $THIRD_PARTY/include/ || exit 1
-cp -rf gmock.a $THIRD_PARTY/lib/ || exit 1
-
-rm -rf $THIRD_PARTY/googletest
diff --git a/third_party/third_party.mk b/third_party/third_party.mk
index 7f651a2..e53529b 100644
--- a/third_party/third_party.mk
+++ b/third_party/third_party.mk
@@ -13,7 +13,7 @@
 
 third_party_chromium_base_unittest_obj_files := $(THIRD_PARTY_CHROMIUM_BASE_UNITTEST_SRC_FILES:%.cc=out/$(BUILD_MODE)/%.o)
 
-$(third_party_chromium_base_unittest_obj_files) : out/$(BUILD_MODE)/%.o : %.cc third_party/include/gtest/gtest.h
+$(third_party_chromium_base_unittest_obj_files) : out/$(BUILD_MODE)/%.o : %.cc
 	mkdir -p $(dir $@)
 	$(CXX) $(DEFS_TEST) $(INCLUDES) $(CFLAGS) $(CFLAGS_$(BUILD_MODE)) $(CFLAGS_CC) -c -o $@ $<
 
@@ -25,7 +25,7 @@
 
 third_party_chromium_crypto_unittest_obj_files := $(THIRD_PARTY_CHROMIUM_CRYPTO_UNITTEST_SRC_FILES:%.cc=out/$(BUILD_MODE)/%.o)
 
-$(third_party_chromium_crypto_unittest_obj_files) : out/$(BUILD_MODE)/%.o : %.cc third_party/include/gtest/gtest.h
+$(third_party_chromium_crypto_unittest_obj_files) : out/$(BUILD_MODE)/%.o : %.cc
 	mkdir -p $(dir $@)
 	$(CXX) $(DEFS_TEST) $(INCLUDES) $(CFLAGS) $(CFLAGS_$(BUILD_MODE)) $(CFLAGS_CC) -c -o $@ $<
 
@@ -48,20 +48,35 @@
 	$(CC) $(DEFS_$(BUILD_MODE)) $(INCLUDES) $(CFLAGS) $(CFLAGS_$(BUILD_MODE)) $(CFLAGS_C) -c -o $@ $<
 
 ###
-# libgtest and libgmock (third_party, downloaded on build)
+# libgtest and libgmock (third_party)
 
-third_party/lib/gtest.a: third_party/include/gtest/gtest.h
-third_party/lib/gmock.a: third_party/include/gtest/gtest.h
+third_party_gtest_lib = out/$(BUILD_MODE)/third_party/googletest/libgtest.a
+third_party_gmock_lib = out/$(BUILD_MODE)/third_party/googletest/libgmock.a
 
-third_party/include/gtest/gtest.h:
-	@echo Downloading and building libgtest and libgmock...
-	third_party/get_gtest.sh
-	@echo Finished downloading and building libgtest and libgmock.
+third_party_gtest_all = out/$(BUILD_MODE)/third_party/googletest/gtest-all.o
+third_party_gmock_all = out/$(BUILD_MODE)/third_party/googletest/gmock-all.o
+
+$(third_party_gtest_all) : third_party/googletest/googletest/src/gtest-all.cc
+	mkdir -p $(dir $@)
+	$(CXX) $(DEFS_$(BUILD_MODE)) $(INCLUDES) $(CFLAGS) $(CFLAGS_$(BUILD_MODE)) $(CFLAGS_CC) -c -o $@ $< \
+		-I third_party/googletest/googletest/
+
+$(third_party_gmock_all) : third_party/googletest/googlemock/src/gmock-all.cc
+	mkdir -p $(dir $@)
+	$(CXX) $(DEFS_$(BUILD_MODE)) $(INCLUDES) $(CFLAGS) $(CFLAGS_$(BUILD_MODE)) $(CFLAGS_CC) -c -o $@ $< \
+		-I third_party/googletest/googlemock/
+
+$(third_party_gtest_lib) : $(third_party_gtest_all)
+	mkdir -p $(dir $@)
+	$(AR) crs $@ $^
+
+$(third_party_gmock_lib) : $(third_party_gmock_all)
+	mkdir -p $(dir $@)
+	$(AR) crs $@ $^
 
 clean-gtest :
-	rm -rf third_party/include/gtest third_party/include/gmock
-	rm -rf third_party/lib/libgmock* third_party/lib/libgtest*
-	rm -rf third_party/googletest
+	rm -rf $(third_party_gtest_lib) $(third_party_gmock_lib) \
+		$(third_party_gtest_all) $(third_party_gmock_all)
 
 ###
 # libevhtp (third_party, downloaded on build)