Use cc_defaults in inputflinger targets

We add a cc_defaults for all of the targets in inputflinger, and include
those defaults in inputflinger_tests. This makes it so that building
the test compiles all of the sources in the test target and includes
them in the executable.

The primary benefit of this refactor is that running the test on any
device will now test the inputflinger code that the test was compiled
with instead of testing the libraries that were on the device. Also,
the device does not need to be re-flashed or need its libraries to be
updated when running the tests after modifying inputflinger code.

Bug: None
Test: atest inputflinger_tests
Change-Id: Ic0406355e5b485c8bf29a65f7c7c37ea8cb68eab
diff --git a/services/inputflinger/Android.bp b/services/inputflinger/Android.bp
index 5c80d55..439d9bf 100644
--- a/services/inputflinger/Android.bp
+++ b/services/inputflinger/Android.bp
@@ -12,6 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+// Default flags to be used throughout all libraries in inputflinger.
 cc_defaults {
     name: "inputflinger_defaults",
     cflags: [
@@ -23,22 +24,25 @@
     ],
 }
 
-cc_library_shared {
-    name: "libinputflinger",
-    defaults: ["inputflinger_defaults"],
+/////////////////////////////////////////////////
+// libinputflinger
+/////////////////////////////////////////////////
 
+filegroup {
+    name: "libinputflinger_sources",
     srcs: [
         "InputClassifier.cpp",
         "InputClassifierConverter.cpp",
         "InputManager.cpp",
     ],
+}
 
+cc_defaults {
+    name: "libinputflinger_defaults",
+    srcs: [":libinputflinger_sources"],
     shared_libs: [
         "android.hardware.input.classifier@1.0",
         "libbase",
-        "libinputflinger_base",
-        "libinputreporter",
-        "libinputreader",
         "libbinder",
         "libcrypto",
         "libcutils",
@@ -50,60 +54,76 @@
         "libui",
         "server_configurable_flags",
     ],
+}
 
-    static_libs: [
-        "libinputdispatcher",
+cc_library_shared {
+    name: "libinputflinger",
+    defaults: [
+        "inputflinger_defaults",
+        "libinputflinger_defaults",
     ],
-
     cflags: [
         // TODO(b/23084678): Move inputflinger to its own process and mark it hidden
         //-fvisibility=hidden
     ],
-
+    shared_libs: [
+        // This should consist only of dependencies from inputflinger. Other dependencies should be
+        // in cc_defaults so that they are included in the tests.
+        "libinputflinger_base",
+        "libinputreporter",
+        "libinputreader",
+    ],
+    static_libs: [
+        "libinputdispatcher",
+    ],
+    export_static_lib_headers: [
+        "libinputdispatcher",
+    ],
     export_include_dirs: [
         ".",
         "include",
     ],
-
-    export_static_lib_headers: [
-        "libinputdispatcher",
-    ],
 }
 
+/////////////////////////////////////////////////
+// libinputflinger_base
+/////////////////////////////////////////////////
+
 cc_library_headers {
     name: "libinputflinger_headers",
-    header_libs: ["libinputreporter_headers"],
     export_include_dirs: ["include"],
-    export_header_lib_headers: ["libinputreporter_headers"],
 }
 
-cc_library_shared {
-    name: "libinputflinger_base",
-    defaults: ["inputflinger_defaults"],
-
+filegroup {
+    name: "libinputflinger_base_sources",
     srcs: [
         "InputListener.cpp",
         "InputReaderBase.cpp",
         "InputThread.cpp",
     ],
+}
 
+cc_defaults {
+    name: "libinputflinger_base_defaults",
+    srcs: [":libinputflinger_base_sources"],
     shared_libs: [
         "libbase",
         "libinput",
         "liblog",
         "libutils",
     ],
-
     header_libs: [
         "libinputflinger_headers",
     ],
-
-    export_header_lib_headers: [
-        "libinputflinger_headers",
-    ],
 }
 
-subdirs = [
-    "host",
-    "tests",
-]
+cc_library_shared {
+    name: "libinputflinger_base",
+    defaults: [
+        "inputflinger_defaults",
+        "libinputflinger_base_defaults",
+    ],
+    export_header_lib_headers: [
+        "libinputflinger_headers",
+    ],
+}
diff --git a/services/inputflinger/dispatcher/Android.bp b/services/inputflinger/dispatcher/Android.bp
index 3f956a8..a98f4b4 100644
--- a/services/inputflinger/dispatcher/Android.bp
+++ b/services/inputflinger/dispatcher/Android.bp
@@ -12,9 +12,15 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-cc_library_static {
-    name: "libinputdispatcher",
-    defaults: ["inputflinger_defaults"],
+cc_library_headers {
+    name: "libinputdispatcher_headers",
+    export_include_dirs: [
+        "include",
+    ],
+}
+
+filegroup {
+    name: "libinputdispatcher_sources",
     srcs: [
         "Connection.cpp",
         "Entry.cpp",
@@ -24,20 +30,41 @@
         "InputState.cpp",
         "InputTarget.cpp",
         "Monitor.cpp",
-        "TouchState.cpp"
+        "TouchState.cpp",
     ],
+}
+
+cc_defaults {
+    name: "libinputdispatcher_defaults",
+    srcs: [":libinputdispatcher_sources"],
     shared_libs: [
         "libbase",
         "libcrypto",
         "libcutils",
         "libinput",
-        "libinputreporter",
-        "libinputflinger_base",
         "liblog",
         "libstatslog",
         "libui",
         "libutils",
     ],
+    header_libs: [
+        "libinputdispatcher_headers",
+    ],
+}
 
-    export_include_dirs: ["include"],
+cc_library_static {
+    name: "libinputdispatcher",
+    defaults: [
+        "inputflinger_defaults",
+        "libinputdispatcher_defaults",
+    ],
+    shared_libs: [
+        // This should consist only of dependencies from inputflinger. Other dependencies should be
+        // in cc_defaults so that they are included in the tests.
+        "libinputreporter",
+        "libinputflinger_base",
+    ],
+    export_header_lib_headers: [
+        "libinputdispatcher_headers",
+    ],
 }
diff --git a/services/inputflinger/reader/Android.bp b/services/inputflinger/reader/Android.bp
index 23c08b2..83a610f 100644
--- a/services/inputflinger/reader/Android.bp
+++ b/services/inputflinger/reader/Android.bp
@@ -21,10 +21,8 @@
     ],
 }
 
-cc_library_shared {
-    name: "libinputreader",
-    defaults: ["inputflinger_defaults"],
-
+filegroup {
+    name: "libinputreader_sources",
     srcs: [
         "EventHub.cpp",
         "InputDevice.cpp",
@@ -44,14 +42,16 @@
         "mapper/TouchInputMapper.cpp",
         "mapper/VibratorInputMapper.cpp",
         "InputReader.cpp",
-        "InputReaderFactory.cpp",
         "TouchVideoDevice.cpp",
     ],
+}
 
+cc_defaults {
+    name: "libinputreader_defaults",
+    srcs: [":libinputreader_sources"],
     shared_libs: [
         "libbase",
         "libcap",
-        "libinputflinger_base",
         "libcrypto",
         "libcutils",
         "libinput",
@@ -59,13 +59,26 @@
         "libui",
         "libutils",
     ],
-
     header_libs: [
-        "libinputflinger_headers",
         "libinputreader_headers",
     ],
+}
 
+cc_library_shared {
+    name: "libinputreader",
+    defaults: [
+        "inputflinger_defaults",
+        "libinputreader_defaults"
+    ],
+    srcs: [
+        "InputReaderFactory.cpp",
+    ],
+    shared_libs: [
+        // This should consist only of dependencies from inputflinger. Other dependencies should be
+        // in cc_defaults so that they are included in the tests.
+        "libinputflinger_base",
+    ],
     export_header_lib_headers: [
-        "libinputflinger_headers",
+        "libinputreader_headers",
     ],
 }
diff --git a/services/inputflinger/reader/mapper/CursorInputMapper.cpp b/services/inputflinger/reader/mapper/CursorInputMapper.cpp
index 78f3382..520a4a4 100644
--- a/services/inputflinger/reader/mapper/CursorInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/CursorInputMapper.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "Macros.h"
+#include "../Macros.h"
 
 #include "CursorInputMapper.h"
 
diff --git a/services/inputflinger/reader/mapper/ExternalStylusInputMapper.cpp b/services/inputflinger/reader/mapper/ExternalStylusInputMapper.cpp
index 37e4047..37d1d74 100644
--- a/services/inputflinger/reader/mapper/ExternalStylusInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/ExternalStylusInputMapper.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "Macros.h"
+#include "../Macros.h"
 
 #include "ExternalStylusInputMapper.h"
 
diff --git a/services/inputflinger/reader/mapper/InputMapper.cpp b/services/inputflinger/reader/mapper/InputMapper.cpp
index 92af612..c5f99c9 100644
--- a/services/inputflinger/reader/mapper/InputMapper.cpp
+++ b/services/inputflinger/reader/mapper/InputMapper.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "Macros.h"
+#include "../Macros.h"
 
 #include "InputMapper.h"
 
diff --git a/services/inputflinger/reader/mapper/JoystickInputMapper.cpp b/services/inputflinger/reader/mapper/JoystickInputMapper.cpp
index 57c85b6..7c3b1d9 100644
--- a/services/inputflinger/reader/mapper/JoystickInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/JoystickInputMapper.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "Macros.h"
+#include "../Macros.h"
 
 #include "JoystickInputMapper.h"
 
diff --git a/services/inputflinger/reader/mapper/KeyboardInputMapper.cpp b/services/inputflinger/reader/mapper/KeyboardInputMapper.cpp
index 9ab707f..ab354a2 100644
--- a/services/inputflinger/reader/mapper/KeyboardInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/KeyboardInputMapper.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "Macros.h"
+#include "../Macros.h"
 
 #include "KeyboardInputMapper.h"
 
diff --git a/services/inputflinger/reader/mapper/MultiTouchInputMapper.cpp b/services/inputflinger/reader/mapper/MultiTouchInputMapper.cpp
index d195a07..d77c8c8 100644
--- a/services/inputflinger/reader/mapper/MultiTouchInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/MultiTouchInputMapper.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "Macros.h"
+#include "../Macros.h"
 
 #include "MultiTouchInputMapper.h"
 
diff --git a/services/inputflinger/reader/mapper/RotaryEncoderInputMapper.cpp b/services/inputflinger/reader/mapper/RotaryEncoderInputMapper.cpp
index a1cce56..ed93f14 100644
--- a/services/inputflinger/reader/mapper/RotaryEncoderInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/RotaryEncoderInputMapper.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "Macros.h"
+#include "../Macros.h"
 
 #include "RotaryEncoderInputMapper.h"
 
diff --git a/services/inputflinger/reader/mapper/SwitchInputMapper.cpp b/services/inputflinger/reader/mapper/SwitchInputMapper.cpp
index 52b2449..e79aeb2 100644
--- a/services/inputflinger/reader/mapper/SwitchInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/SwitchInputMapper.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "Macros.h"
+#include "../Macros.h"
 
 #include "SwitchInputMapper.h"
 
diff --git a/services/inputflinger/reader/mapper/TouchInputMapper.cpp b/services/inputflinger/reader/mapper/TouchInputMapper.cpp
index e832804..b3a6df8 100644
--- a/services/inputflinger/reader/mapper/TouchInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/TouchInputMapper.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "Macros.h"
+#include "../Macros.h"
 
 #include "TouchInputMapper.h"
 
diff --git a/services/inputflinger/reader/mapper/VibratorInputMapper.cpp b/services/inputflinger/reader/mapper/VibratorInputMapper.cpp
index 1b584ea..7665680 100644
--- a/services/inputflinger/reader/mapper/VibratorInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/VibratorInputMapper.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "Macros.h"
+#include "../Macros.h"
 
 #include "VibratorInputMapper.h"
 
diff --git a/services/inputflinger/reporter/Android.bp b/services/inputflinger/reporter/Android.bp
index 5956fb0..fbc51da 100644
--- a/services/inputflinger/reporter/Android.bp
+++ b/services/inputflinger/reporter/Android.bp
@@ -17,25 +17,33 @@
     export_include_dirs: ["."],
 }
 
-cc_library_shared {
-    name: "libinputreporter",
-    defaults: ["inputflinger_defaults"],
-
+filegroup {
+    name: "libinputreporter_sources",
     srcs: [
-        "InputReporter.cpp",
+            "InputReporter.cpp",
     ],
+}
 
+cc_defaults {
+    name: "libinputreporter_defaults",
+    srcs: [":libinputreporter_sources"],
     shared_libs: [
         "liblog",
         "libutils",
     ],
-
     header_libs: [
-        "libinputflinger_headers",
+        "libinputreporter_headers",
     ],
+}
 
+cc_library_shared {
+    name: "libinputreporter",
+    defaults: [
+        "inputflinger_defaults",
+        "libinputreporter_defaults",
+    ],
     export_header_lib_headers: [
-        "libinputflinger_headers",
+        "libinputreporter_headers",
     ],
 }
 
diff --git a/services/inputflinger/tests/Android.bp b/services/inputflinger/tests/Android.bp
index f913d82..73d2272 100644
--- a/services/inputflinger/tests/Android.bp
+++ b/services/inputflinger/tests/Android.bp
@@ -1,7 +1,31 @@
-// Build the unit tests.
+// Copyright (C) 2020 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.
 
 cc_test {
     name: "inputflinger_tests",
+    defaults: [
+        "inputflinger_defaults",
+        // For all targets inside inputflinger, these tests build all of their sources using their
+        // defaults rather than including them as shared or static libraries. By doing so, the tests
+        // will always run against the compiled version of the inputflinger code rather than the
+        // version on the device.
+        "libinputflinger_base_defaults",
+        "libinputreader_defaults",
+        "libinputreporter_defaults",
+        "libinputdispatcher_defaults",
+        "libinputflinger_defaults",
+    ],
     srcs: [
         "BlockingQueue_test.cpp",
         "EventHub_test.cpp",
@@ -12,31 +36,5 @@
         "InputReader_test.cpp",
         "UinputDevice.cpp",
     ],
-    cflags: [
-        "-Wall",
-        "-Werror",
-        "-Wextra",
-        "-Wno-unused-parameter",
-        "-Wthread-safety",
-    ],
-    shared_libs: [
-        "android.hardware.input.classifier@1.0",
-        "libbase",
-        "libbinder",
-        "libcutils",
-        "liblog",
-        "libutils",
-        "libhardware",
-        "libhardware_legacy",
-        "libui",
-        "libinput",
-        "libinputflinger",
-        "libinputreader",
-        "libinputflinger_base",
-        "libinputservice",
-    ],
-    header_libs: [
-        "libinputreader_headers",
-    ],
     require_root: true,
 }
diff --git a/services/inputflinger/tests/InputReader_test.cpp b/services/inputflinger/tests/InputReader_test.cpp
index f9ebfc0..af11256 100644
--- a/services/inputflinger/tests/InputReader_test.cpp
+++ b/services/inputflinger/tests/InputReader_test.cpp
@@ -1714,7 +1714,7 @@
         mFakePolicy = new FakeInputReaderPolicy();
         mTestListener = new TestInputListener();
 
-        mReader = createInputReader(mFakePolicy, mTestListener);
+        mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
         ASSERT_EQ(mReader->start(), OK);
 
         // Since this test is run on a real device, all the input devices connected