libevhtp: build checked out copy

Now that libevhtp is part of the manifest, build it directly and store
the results in out/.

This also deletes references to third_party/{include,lib} as they're
no longer needed.

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

Change-Id: I6b1dd91cea0f152f8e79527e842add6a9d797a3c
Reviewed-on: https://weave-review.googlesource.com/3001
Reviewed-by: Alex Vakulenko <avakulenko@google.com>
diff --git a/.gitignore b/.gitignore
index c102afc..962e231 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,8 +1,6 @@
 *~
 /out/
 /third_party/cross
-/third_party/include
-/third_party/lib
 /third_party/libevhtp
 /third_party/googletest
 gomacc.lock
diff --git a/Makefile b/Makefile
index 8cbb0e7..e348fd8 100644
--- a/Makefile
+++ b/Makefile
@@ -15,7 +15,6 @@
 	-I. \
 	-Iinclude \
 	-Ithird_party/chromium \
-	-Ithird_party/include \
 	-Ithird_party/googletest/googletest/include \
 	-Ithird_party/googletest/googlemock/include \
 	-Ithird_party/libuweave \
diff --git a/README.md b/README.md
index 26f1cf7..38059db 100644
--- a/README.md
+++ b/README.md
@@ -88,7 +88,7 @@
   - hostapd
   - libavahi-client-dev
   - libcurl4-openssl-dev
-  - libevhtp (included; see third_party/get_libevhtp.sh)
+  - libevhtp (included; see third_party/libevhtp/)
   - libevent-dev
 
 
diff --git a/examples/examples.mk b/examples/examples.mk
index af15d5c..48a1210 100644
--- a/examples/examples.mk
+++ b/examples/examples.mk
@@ -10,9 +10,15 @@
 USE_INTERNAL_LIBEVHTP ?= 1
 
 ifeq (1, $(USE_INTERNAL_LIBEVHTP))
-$(examples_provider_obj_files) : third_party/include/evhtp.h
+LIBEVHTP_INCLUDES = -Ithird_party/libevhtp -I$(dir $(third_party_libevhtp_header))
+LIBEVHTP_HEADERS = $(third_party_libevhtp_header)
+else
+LIBEVHTP_INCLUDES =
+LIBEVHTP_HEADERS =
 endif
 
+$(examples_provider_obj_files) : $(LIBEVHTP_HEADERS)
+$(examples_provider_obj_files) : INCLUDES += $(LIBEVHTP_INCLUDES)
 $(examples_provider_obj_files) : out/$(BUILD_MODE)/%.o : %.cc
 	mkdir -p $(dir $@)
 	$(CXX) $(DEFS_$(BUILD_MODE)) $(INCLUDES) $(CFLAGS) $(CFLAGS_$(BUILD_MODE)) $(CFLAGS_CC) -c -o $@ $<
@@ -31,17 +37,14 @@
 
 examples_daemon_obj_files := $(EXAMPLES_DAEMON_SRC_FILES:%.cc=out/$(BUILD_MODE)/%.o)
 
-ifeq (1, $(USE_INTERNAL_LIBEVHTP))
-$(examples_daemon_obj_files) : third_party/include/evhtp.h
-endif
-
+$(examples_daemon_obj_files) : $(LIBEVHTP_HEADERS)
+$(examples_daemon_obj_files) : INCLUDES += $(LIBEVHTP_INCLUDES)
 $(examples_daemon_obj_files) : out/$(BUILD_MODE)/%.o : %.cc
 	mkdir -p $(dir $@)
 	$(CXX) $(DEFS_$(BUILD_MODE)) $(INCLUDES) $(CFLAGS) $(CFLAGS_$(BUILD_MODE)) $(CFLAGS_CC) -c -o $@ $<
 
 daemon_common_flags := \
 	-Wl,-rpath=out/$(BUILD_MODE)/ \
-	-Lthird_party/lib \
 	-levent \
 	-levent_openssl \
 	-lpthread \
@@ -55,7 +58,7 @@
 daemon_deps := out/$(BUILD_MODE)/examples_provider.a out/$(BUILD_MODE)/libweave.so
 
 ifeq (1, $(USE_INTERNAL_LIBEVHTP))
-daemon_deps += third_party/lib/libevhtp.a
+daemon_deps += $(third_party_libevhtp_lib)
 else
 daemon_common_flags += -levhtp
 endif
diff --git a/tests.mk b/tests.mk
index cd711ef..c7db877 100644
--- a/tests.mk
+++ b/tests.mk
@@ -37,7 +37,7 @@
 	out/$(BUILD_MODE)/libweave-test.a \
 	$(third_party_gtest_lib) \
 	$(third_party_gmock_lib)
-	$(CXX) -o $@ $^ $(CFLAGS) -lcrypto -lexpat -lpthread -lrt -Lthird_party/lib
+	$(CXX) -o $@ $^ $(CFLAGS) -lcrypto -lexpat -lpthread -lrt
 
 test : out/$(BUILD_MODE)/libweave_testrunner
 	$(TEST_ENV) $< $(TEST_FLAGS)
@@ -58,7 +58,7 @@
 	out/$(BUILD_MODE)/src/test/weave_testrunner.o \
 	$(third_party_gtest_lib) \
 	$(third_party_gmock_lib)
-	$(CXX) -o $@ $^ $(CFLAGS) -lcrypto -lexpat -lpthread -lrt -Lthird_party/lib -Wl,-rpath=out/$(BUILD_MODE)/
+	$(CXX) -o $@ $^ $(CFLAGS) -lcrypto -lexpat -lpthread -lrt -Wl,-rpath=out/$(BUILD_MODE)/
 
 export-test : out/$(BUILD_MODE)/libweave_exports_testrunner
 	$(TEST_ENV) $< $(TEST_FLAGS)
diff --git a/third_party/get_libevhtp.sh b/third_party/get_libevhtp.sh
deleted file mode 100755
index cfcada9..0000000
--- a/third_party/get_libevhtp.sh
+++ /dev/null
@@ -1,27 +0,0 @@
-#!/bin/bash
-# Copyright 2016 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 libevhtp.
-# Example uses libevhtp to implement HTTPS server. This step could be
-# replaced with apt-get in future (Debian jessie, Ubuntu vivid).
-cd $(dirname "$0")
-THIRD_PARTY=$(pwd)
-
-LIBEVHTP_VERSION=1.2.11n
-
-mkdir -p include lib
-
-rm -rf $THIRD_PARTY/libevhtp
-curl -L https://github.com/ellzey/libevhtp/archive/$LIBEVHTP_VERSION.tar.gz | tar xz || exit 1
-mv libevhtp-$LIBEVHTP_VERSION $THIRD_PARTY/libevhtp || exit 1
-cd $THIRD_PARTY/libevhtp || exit 1
-
-cmake -D EVHTP_DISABLE_REGEX:BOOL=ON . || exit 1
-make evhtp || exit 1
-
-cp -rf *.h $THIRD_PARTY/include/ || exit 1
-cp -f libevhtp.a $THIRD_PARTY/lib/ || exit 1
-
-rm -rf $THIRD_PARTY/libevhtp
diff --git a/third_party/third_party.mk b/third_party/third_party.mk
index e53529b..18f9b98 100644
--- a/third_party/third_party.mk
+++ b/third_party/third_party.mk
@@ -79,15 +79,18 @@
 		$(third_party_gtest_all) $(third_party_gmock_all)
 
 ###
-# libevhtp (third_party, downloaded on build)
+# libevhtp (third_party)
 
-third_party/lib/libevhtp.a : third_party/include/evhtp.h
-third_party/include/evhtp.h :
-	@echo Downloading and building libevhtp...
-	third_party/get_libevhtp.sh
-	@echo Finished downloading and building libevhtp.
+third_party_libevhtp = out/$(BUILD_MODE)/third_party/libevhtp
+third_party_libevhtp_lib = $(third_party_libevhtp)/libevhtp.a
+third_party_libevhtp_header = $(third_party_libevhtp)/evhtp-config.h
+
+$(third_party_libevhtp_header) :
+	mkdir -p $(dir $@)
+	cd $(dir $@) && cmake -D EVHTP_DISABLE_REGEX:BOOL=ON $(PWD)/third_party/libevhtp
+
+$(third_party_libevhtp_lib) : $(third_party_libevhtp_header)
+	$(MAKE) -C $(third_party_libevhtp)
 
 clean-libevhtp :
-	rm -rf third_party/include/evhtp.h third_party/include/evhtp-config.h third_party/include/evthr.h third_party/include/htparse.h
-	rm -rf third_party/lib/libevhtp.a
-	rm -rf third_party/libevhtp
+	rm -rf $(third_party_libevhtp)