Revert "Revert "Split out libhidl-gen and libhidl-gen-utils.""

Shared code between hidl-gen, c2hal, and VTS.

Also build for device, as we might use libhidl-gen reflectively in
future (for profiling and instrumentation).

Change-Id: Ia56901f4afbc12b7c2abee693e0ebea97ba76a8b
diff --git a/AST.cpp b/AST.cpp
index d27a72d..b514fe7 100644
--- a/AST.cpp
+++ b/AST.cpp
@@ -17,7 +17,6 @@
 #include "AST.h"
 
 #include "Coordinator.h"
-#include "Formatter.h"
 #include "FQName.h"
 #include "HandleType.h"
 #include "Interface.h"
@@ -25,6 +24,7 @@
 #include "Scope.h"
 #include "TypeDef.h"
 
+#include <hidl-util/Formatter.h>
 #include <android-base/logging.h>
 #include <iostream>
 #include <stdlib.h>
diff --git a/Android.mk b/Android.mk
index bd7a578..4596522 100644
--- a/Android.mk
+++ b/Android.mk
@@ -1,4 +1,61 @@
 LOCAL_PATH := $(call my-dir)
+
+common_libhidl-gen_src_files := \
+    Annotation.cpp              \
+    ArrayType.cpp               \
+    CompoundType.cpp            \
+    ConstantExpression.cpp      \
+    EnumType.cpp                \
+    FQName.cpp                  \
+    GenericBinder.cpp           \
+    HandleType.cpp              \
+    Interface.cpp               \
+    Method.cpp                  \
+    NamedType.cpp               \
+    PredefinedType.cpp          \
+    ScalarType.cpp              \
+    Scope.cpp                   \
+    StringType.cpp              \
+    Type.cpp                    \
+    TypeDef.cpp                 \
+    VectorType.cpp              \
+
+#
+# libhidl-gen for device
+#
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libhidl-gen
+LOCAL_CFLAGS := -O0 -g -Wno-deprecated-register
+LOCAL_SRC_FILES := $(common_libhidl-gen_src_files)
+LOCAL_SHARED_LIBRARIES :=       \
+    libbase                     \
+    liblog                      \
+    libhidl-gen-utils           \
+
+LOCAL_STATIC_LIBRARIES :=       \
+    libutils                    \
+
+include $(BUILD_SHARED_LIBRARY)
+
+#
+# libhidl-gen for host
+#
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libhidl-gen
+LOCAL_CFLAGS := -O0 -g -Wno-deprecated-register
+LOCAL_SRC_FILES := $(common_libhidl-gen_src_files)
+LOCAL_SHARED_LIBRARIES :=       \
+    libbase                     \
+    liblog                      \
+    libhidl-gen-utils           \
+
+LOCAL_STATIC_LIBRARIES :=       \
+    libutils                    \
+
+include $(BUILD_HOST_SHARED_LIBRARY)
+
 include $(CLEAR_VARS)
 
 LOCAL_MODULE := hidl-gen
@@ -6,42 +63,21 @@
 LOCAL_IS_HOST_MODULE := true
 
 LOCAL_SRC_FILES :=              \
-    Annotation.cpp              \
-    ArrayType.cpp               \
-    CompoundType.cpp            \
-    ConstantExpression.cpp      \
     Coordinator.cpp             \
-    EnumType.cpp                \
-    Formatter.cpp               \
-    FQName.cpp                  \
     generateCpp.cpp             \
     generateCppImpl.cpp         \
     generateJava.cpp            \
     generateVts.cpp             \
-    GenericBinder.cpp           \
-    HandleType.cpp              \
     hidl-gen_y.yy               \
     hidl-gen_l.ll               \
-    Interface.cpp               \
-    Method.cpp                  \
-    NamedType.cpp               \
-    PredefinedType.cpp          \
-    ScalarType.cpp              \
-    Scope.cpp                   \
-    StringHelper.cpp            \
-    StringType.cpp              \
-    Type.cpp                    \
-    TypeDef.cpp                 \
-    VectorType.cpp              \
     AST.cpp                     \
     main.cpp                    \
 
 LOCAL_SHARED_LIBRARIES :=       \
     libbase                     \
     liblog                      \
-
-LOCAL_STATIC_LIBRARIES :=       \
-    libutils                    \
+    libhidl-gen                 \
+    libhidl-gen-utils           \
 
 LOCAL_CFLAGS := -O0 -g -Wno-deprecated-register
 
diff --git a/Annotation.cpp b/Annotation.cpp
index b7f71bb..b1d496f 100644
--- a/Annotation.cpp
+++ b/Annotation.cpp
@@ -16,8 +16,7 @@
 
 #include "Annotation.h"
 
-#include "Formatter.h"
-
+#include <hidl-util/Formatter.h>
 #include <vector>
 
 namespace android {
diff --git a/ArrayType.cpp b/ArrayType.cpp
index 1952a73..6862262 100644
--- a/ArrayType.cpp
+++ b/ArrayType.cpp
@@ -16,8 +16,7 @@
 
 #include "ArrayType.h"
 
-#include "Formatter.h"
-
+#include <hidl-util/Formatter.h>
 #include <android-base/logging.h>
 
 namespace android {
diff --git a/CompoundType.cpp b/CompoundType.cpp
index de35137..b3f4269 100644
--- a/CompoundType.cpp
+++ b/CompoundType.cpp
@@ -16,9 +16,8 @@
 
 #include "CompoundType.h"
 
-#include "Formatter.h"
 #include "VectorType.h"
-
+#include <hidl-util/Formatter.h>
 #include <android-base/logging.h>
 
 namespace android {
diff --git a/Coordinator.cpp b/Coordinator.cpp
index 8783c15..6deca29 100644
--- a/Coordinator.cpp
+++ b/Coordinator.cpp
@@ -21,7 +21,7 @@
 #include <algorithm>
 #include <android-base/logging.h>
 #include <iterator>
-#include <sys/dir.h>
+#include <dirent.h>
 #include <sys/stat.h>
 
 extern android::status_t parseFile(android::AST *ast);
diff --git a/EnumType.cpp b/EnumType.cpp
index 9db7cf1..ea0a751 100644
--- a/EnumType.cpp
+++ b/EnumType.cpp
@@ -16,10 +16,10 @@
 
 #include "EnumType.h"
 
-#include "Formatter.h"
-#include <inttypes.h>
 #include "ScalarType.h"
 
+#include <inttypes.h>
+#include <hidl-util/Formatter.h>
 #include <android-base/logging.h>
 
 namespace android {
diff --git a/FQName.cpp b/FQName.cpp
index 08ceccb..a477cb2 100644
--- a/FQName.cpp
+++ b/FQName.cpp
@@ -15,8 +15,8 @@
  */
 
 #include "FQName.h"
-#include "StringHelper.h"
 
+#include <hidl-util/StringHelper.h>
 #include <android-base/logging.h>
 #include <iostream>
 #include <regex>
diff --git a/GenericBinder.cpp b/GenericBinder.cpp
index 698b712..8f85ad7 100644
--- a/GenericBinder.cpp
+++ b/GenericBinder.cpp
@@ -16,7 +16,7 @@
 
 #include "GenericBinder.h"
 
-#include "Formatter.h"
+#include <hidl-util/Formatter.h>
 
 namespace android {
 
diff --git a/HandleType.cpp b/HandleType.cpp
index f1a5572..178a2c7 100644
--- a/HandleType.cpp
+++ b/HandleType.cpp
@@ -16,8 +16,7 @@
 
 #include "HandleType.h"
 
-#include "Formatter.h"
-
+#include <hidl-util/Formatter.h>
 #include <android-base/logging.h>
 
 namespace android {
diff --git a/Interface.cpp b/Interface.cpp
index 7bc32a8..a4059d0 100644
--- a/Interface.cpp
+++ b/Interface.cpp
@@ -17,9 +17,9 @@
 #include "Interface.h"
 
 #include "Annotation.h"
-#include "Formatter.h"
 #include "Method.h"
 
+#include <hidl-util/Formatter.h>
 #include <iostream>
 
 namespace android {
diff --git a/Method.cpp b/Method.cpp
index d78f8b9..d9a6a8c 100644
--- a/Method.cpp
+++ b/Method.cpp
@@ -17,10 +17,11 @@
 #include "Method.h"
 
 #include "Annotation.h"
-#include "Formatter.h"
 #include "ScalarType.h"
 #include "Type.h"
 
+#include <hidl-util/Formatter.h>
+
 namespace android {
 
 Method::Method(const char *name,
diff --git a/PredefinedType.cpp b/PredefinedType.cpp
index d04ba72..c1468ab 100644
--- a/PredefinedType.cpp
+++ b/PredefinedType.cpp
@@ -16,8 +16,7 @@
 
 #include "PredefinedType.h"
 
-#include "Formatter.h"
-
+#include <hidl-util/Formatter.h>
 #include <android-base/logging.h>
 
 namespace android {
diff --git a/ScalarType.cpp b/ScalarType.cpp
index eabe86a..96e27c4 100644
--- a/ScalarType.cpp
+++ b/ScalarType.cpp
@@ -16,7 +16,7 @@
 
 #include "ScalarType.h"
 
-#include "Formatter.h"
+#include <hidl-util/Formatter.h>
 
 namespace android {
 
diff --git a/Scope.cpp b/Scope.cpp
index f52fc65..c1de97d 100644
--- a/Scope.cpp
+++ b/Scope.cpp
@@ -16,9 +16,9 @@
 
 #include "Scope.h"
 
-#include "Formatter.h"
 #include "Interface.h"
 
+#include <hidl-util/Formatter.h>
 #include <android-base/logging.h>
 
 namespace android {
diff --git a/StringType.cpp b/StringType.cpp
index ef51cd2..ea00ddc 100644
--- a/StringType.cpp
+++ b/StringType.cpp
@@ -16,7 +16,7 @@
 
 #include "StringType.h"
 
-#include "Formatter.h"
+#include <hidl-util/Formatter.h>
 
 namespace android {
 
diff --git a/Type.cpp b/Type.cpp
index 40a4413..9fea80f 100644
--- a/Type.cpp
+++ b/Type.cpp
@@ -16,9 +16,9 @@
 
 #include "Type.h"
 
-#include "Formatter.h"
 #include "ScalarType.h"
 
+#include <hidl-util/Formatter.h>
 #include <android-base/logging.h>
 
 namespace android {
diff --git a/TypeDef.cpp b/TypeDef.cpp
index d3c0198..3f5cbf6 100644
--- a/TypeDef.cpp
+++ b/TypeDef.cpp
@@ -16,8 +16,7 @@
 
 #include "TypeDef.h"
 
-#include "Formatter.h"
-
+#include <hidl-util/Formatter.h>
 #include <android-base/logging.h>
 
 namespace android {
diff --git a/VectorType.cpp b/VectorType.cpp
index 05d18ed..9ddb171 100644
--- a/VectorType.cpp
+++ b/VectorType.cpp
@@ -16,8 +16,7 @@
 
 #include "VectorType.h"
 
-#include "Formatter.h"
-
+#include <hidl-util/Formatter.h>
 #include <android-base/logging.h>
 
 namespace android {
diff --git a/c2hal/AST.h b/c2hal/AST.h
index bc0808a..c3d6c5c 100644
--- a/c2hal/AST.h
+++ b/c2hal/AST.h
@@ -17,10 +17,10 @@
 #ifndef AST_H_
 #define AST_H_
 
-#include "../Formatter.h"
 #include "Scope.h"
 #include "Expression.h"
 
+#include <hidl-util/Formatter.h>
 #include <android-base/macros.h>
 #include <android-base/logging.h>
 #include <string>
diff --git a/c2hal/Android.mk b/c2hal/Android.mk
index 828264b..cd21ee3 100644
--- a/c2hal/Android.mk
+++ b/c2hal/Android.mk
@@ -6,7 +6,6 @@
 # LOCAL_IS_HOST_MODULE := true
 
 LOCAL_SRC_FILES :=              \
-    ../Formatter.cpp            \
     AST.cpp                     \
     c2hal_l.ll                  \
     c2hal_y.yy                  \
@@ -26,6 +25,7 @@
 LOCAL_SHARED_LIBRARIES :=       \
     libbase                     \
     liblog                      \
+    libhidl-gen-utils           \
 
 LOCAL_STATIC_LIBRARIES :=       \
     libutils                    \
diff --git a/c2hal/Declaration.h b/c2hal/Declaration.h
index bd7bce0..fff73ea 100644
--- a/c2hal/Declaration.h
+++ b/c2hal/Declaration.h
@@ -21,7 +21,7 @@
 #include <android-base/logging.h>
 #include <string>
 #include <vector>
-#include "../Formatter.h"
+#include <hidl-util/Formatter.h>
 
 namespace android {
 
diff --git a/c2hal/Include.h b/c2hal/Include.h
index 21988ff..befad63 100644
--- a/c2hal/Include.h
+++ b/c2hal/Include.h
@@ -17,11 +17,12 @@
 #ifndef INCLUDE_H_
 #define INCLUDE_H_
 
+#include "Declaration.h"
+
 #include <android-base/macros.h>
 #include <android-base/logging.h>
 #include <string>
-#include "../Formatter.h"
-#include "Declaration.h"
+#include <hidl-util/Formatter.h>
 
 namespace android {
 
diff --git a/generateCpp.cpp b/generateCpp.cpp
index e0f3f41..3543e97 100644
--- a/generateCpp.cpp
+++ b/generateCpp.cpp
@@ -18,14 +18,14 @@
 
 #include "Coordinator.h"
 #include "EnumType.h"
-#include "Formatter.h"
 #include "Interface.h"
 #include "Method.h"
 #include "ScalarType.h"
 #include "Scope.h"
-#include "StringHelper.h"
 
 #include <algorithm>
+#include <hidl-util/StringHelper.h>
+#include <hidl-util/Formatter.h>
 #include <android-base/logging.h>
 #include <string>
 #include <vector>
diff --git a/generateCppImpl.cpp b/generateCppImpl.cpp
index e882fe3..bed1d1d 100644
--- a/generateCppImpl.cpp
+++ b/generateCppImpl.cpp
@@ -18,19 +18,18 @@
 
 #include "Coordinator.h"
 #include "EnumType.h"
-#include "Formatter.h"
 #include "Interface.h"
 #include "Method.h"
 #include "ScalarType.h"
 #include "Scope.h"
 
 #include <algorithm>
+#include <hidl-util/Formatter.h>
 #include <android-base/logging.h>
 #include <string>
 #include <vector>
 #include <set>
 
-
 namespace android {
 
 status_t AST::generateCppImpl(const std::string &outputPath) const {
diff --git a/generateJava.cpp b/generateJava.cpp
index 8dab179..51c66bc 100644
--- a/generateJava.cpp
+++ b/generateJava.cpp
@@ -17,12 +17,12 @@
 #include "AST.h"
 
 #include "Coordinator.h"
-#include "Formatter.h"
 #include "Interface.h"
 #include "Method.h"
 #include "Scope.h"
-#include "StringHelper.h"
 
+#include <hidl-util/StringHelper.h>
+#include <hidl-util/Formatter.h>
 #include <android-base/logging.h>
 
 namespace android {
diff --git a/generateVts.cpp b/generateVts.cpp
index dbca6b2..fe71487 100644
--- a/generateVts.cpp
+++ b/generateVts.cpp
@@ -18,11 +18,11 @@
 
 #include "Annotation.h"
 #include "Coordinator.h"
-#include "Formatter.h"
 #include "Interface.h"
 #include "Method.h"
 #include "Scope.h"
 
+#include <hidl-util/Formatter.h>
 #include <android-base/logging.h>
 #include <string>
 #include <vector>
diff --git a/main.cpp b/main.cpp
index 134cc6b..4e2e2b4 100644
--- a/main.cpp
+++ b/main.cpp
@@ -16,10 +16,10 @@
 
 #include "AST.h"
 #include "Coordinator.h"
-#include "Formatter.h"
 #include "FQName.h"
 #include "Scope.h"
 
+#include <hidl-util/Formatter.h>
 #include <android-base/logging.h>
 #include <set>
 #include <stdio.h>
diff --git a/utils/Android.mk b/utils/Android.mk
new file mode 100644
index 0000000..a1fe8ee
--- /dev/null
+++ b/utils/Android.mk
@@ -0,0 +1,50 @@
+# Copyright (C) 2016 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+LOCAL_PATH:= $(call my-dir)
+
+common_libhidl-gen-utils_src_files :=   \
+    Formatter.cpp                       \
+    StringHelper.cpp                    \
+
+#
+# libhidl-gen-utils for device
+#
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libhidl-gen-utils
+LOCAL_CFLAGS := -O0 -g
+LOCAL_SRC_FILES := $(common_libhidl-gen-utils_src_files)
+LOCAL_SHARED_LIBRARIES :=               \
+    libbase                             \
+    liblog                              \
+
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/include/hidl-util
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+include $(BUILD_SHARED_LIBRARY)
+
+#
+# libhidl-gen-utils for host
+#
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libhidl-gen-utils
+LOCAL_CFLAGS := -O0 -g
+LOCAL_SRC_FILES := $(common_libhidl-gen-utils_src_files)
+LOCAL_SHARED_LIBRARIES :=               \
+    libbase                             \
+    liblog                              \
+
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/include/hidl-util
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+include $(BUILD_HOST_SHARED_LIBRARY)
\ No newline at end of file
diff --git a/Formatter.cpp b/utils/Formatter.cpp
similarity index 100%
rename from Formatter.cpp
rename to utils/Formatter.cpp
diff --git a/StringHelper.cpp b/utils/StringHelper.cpp
similarity index 100%
rename from StringHelper.cpp
rename to utils/StringHelper.cpp
diff --git a/Formatter.h b/utils/include/hidl-util/Formatter.h
similarity index 100%
rename from Formatter.h
rename to utils/include/hidl-util/Formatter.h
diff --git a/StringHelper.h b/utils/include/hidl-util/StringHelper.h
similarity index 100%
rename from StringHelper.h
rename to utils/include/hidl-util/StringHelper.h