adbd: compile for host.

Preparatory step for testing adb on GCE on non-linux hosts: instead of
pointing them at a device (emulated or otherwise), point them at adbd
running on a linux host instead.

Test: adbd & adb connect localhost:5555; adb -e wait-for-device shell
Change-Id: Ib22d51a4fc9e6e68f71bf1b3b9b2e1b0bd844760
diff --git a/adb/Android.bp b/adb/Android.bp
index 36bfad4..8199fff 100644
--- a/adb/Android.bp
+++ b/adb/Android.bp
@@ -22,35 +22,16 @@
         "-Wexit-time-destructors",
         "-Wno-unused-parameter",
         "-Wno-missing-field-initializers",
+        "-Wthread-safety",
         "-Wvla",
+        "-DADB_HOST=1", // overridden by adbd_defaults
     ],
     cpp_std: "experimental",
 
     use_version_lib: true,
-
     compile_multilib: "first",
-    product_variables: {
-        debuggable: {
-            cflags: [
-                "-DALLOW_ADBD_ROOT",
-                "-DALLOW_ADBD_DISABLE_VERITY",
-                "-DALLOW_ADBD_NO_AUTH",
-            ],
-        },
-    },
 
     target: {
-        android: {
-            cflags: [
-                "-DADB_HOST=0",
-                "-Wthread-safety",
-            ],
-        },
-
-        host: {
-            cflags: ["-DADB_HOST=1"],
-        },
-
         darwin: {
             host_ldlibs: [
                 "-lpthread",
@@ -76,6 +57,9 @@
 
                 // MinGW hides some things behind _POSIX_SOURCE.
                 "-D_POSIX_SOURCE",
+
+                // Not supported yet.
+                "-Wno-thread-safety",
             ],
 
             host_ldlibs: [
@@ -84,15 +68,46 @@
                 "-luserenv",
             ],
         },
+    },
+}
 
-        not_windows: {
+cc_defaults {
+    name: "adbd_defaults",
+    defaults: ["adb_defaults"],
+
+    cflags: ["-UADB_HOST", "-DADB_HOST=0"],
+    product_variables: {
+        debuggable: {
             cflags: [
-                "-Wthread-safety",
+                "-DALLOW_ADBD_ROOT",
+                "-DALLOW_ADBD_DISABLE_VERITY",
+                "-DALLOW_ADBD_NO_AUTH",
             ],
         },
     },
 }
 
+cc_defaults {
+    name: "host_adbd_supported",
+
+    host_supported: true,
+    target: {
+        linux: {
+            enabled: true,
+            host_ldlibs: [
+                "-lresolv", // b64_pton
+                "-lutil", // forkpty
+            ],
+        },
+        darwin: {
+            enabled: false,
+        },
+        windows: {
+            enabled: false,
+        },
+    },
+}
+
 // libadb
 // =========================================================
 // These files are compiled for both the host and the device.
@@ -313,7 +328,7 @@
 // libadbd_core contains the common sources to build libadbd and libadbd_services.
 cc_library_static {
     name: "libadbd_core",
-    defaults: ["adb_defaults"],
+    defaults: ["adbd_defaults", "host_adbd_supported"],
     recovery_available: true,
 
     // libminadbd wants both, as it's used to build native tests.
@@ -322,9 +337,6 @@
     srcs: libadb_srcs + libadb_posix_srcs + [
         "daemon/auth.cpp",
         "daemon/jdwp_service.cpp",
-        "daemon/usb.cpp",
-        "daemon/usb_ffs.cpp",
-        "daemon/usb_legacy.cpp",
     ],
 
     local_include_dirs: [
@@ -335,7 +347,6 @@
 
     static_libs: [
         "libdiagnose_usb",
-        "libqemu_pipe",
     ],
 
     shared_libs: [
@@ -346,22 +357,36 @@
         "libcutils",
         "liblog",
     ],
+
+    target: {
+        android: {
+            whole_static_libs: [
+                "libqemu_pipe",
+            ],
+            srcs: [
+                "daemon/transport_qemu.cpp",
+                "daemon/usb.cpp",
+                "daemon/usb_ffs.cpp",
+                "daemon/usb_legacy.cpp",
+            ]
+        },
+        linux_glibc: {
+            srcs: [
+                "daemon/usb_dummy.cpp",
+            ]
+        }
+    },
 }
 
 cc_library {
     name: "libadbd_services",
-    defaults: ["adb_defaults"],
+    defaults: ["adbd_defaults", "host_adbd_supported"],
     recovery_available: true,
     compile_multilib: "both",
 
     srcs: [
-        "daemon/abb_service.cpp",
         "daemon/file_sync_service.cpp",
-        "daemon/framebuffer_service.cpp",
-        "daemon/mdns.cpp",
-        "daemon/remount_service.cpp",
         "daemon/services.cpp",
-        "daemon/set_verity_enable_state_service.cpp",
         "daemon/shell_service.cpp",
         "shell_service_protocol.cpp",
     ],
@@ -373,27 +398,41 @@
 
     static_libs: [
         "libadbd_core",
-        "libavb_user",
         "libdiagnose_usb",
-        "libqemu_pipe",
     ],
 
     shared_libs: [
         "libasyncio",
         "libbase",
-        "libbootloader_message",
         "libcrypto",
         "libcrypto_utils",
         "libcutils",
-        "libext4_utils",
-        "libfec",
-        "libfs_mgr",
         "liblog",
-        "libmdnssd",
-        "libselinux",
     ],
 
     target: {
+        android: {
+            srcs: [
+                "daemon/abb_service.cpp",
+                "daemon/framebuffer_service.cpp",
+                "daemon/mdns.cpp",
+                "daemon/reboot_service.cpp",
+                "daemon/remount_service.cpp",
+                "daemon/restart_service.cpp",
+                "daemon/set_verity_enable_state_service.cpp",
+            ],
+            static_libs: [
+                "libavb_user",
+            ],
+            shared_libs: [
+                "libbootloader_message",
+                "libmdnssd",
+                "libext4_utils",
+                "libfec",
+                "libfs_mgr",
+                "libselinux",
+            ],
+        },
         recovery: {
             exclude_srcs: [
                 "daemon/abb_service.cpp",
@@ -404,7 +443,7 @@
 
 cc_library {
     name: "libadbd",
-    defaults: ["adb_defaults"],
+    defaults: ["adbd_defaults", "host_adbd_supported"],
     recovery_available: true,
 
     // Avoid getting duplicate symbol of android::build::GetBuildNumber().
@@ -435,7 +474,7 @@
 
 cc_binary {
     name: "adbd",
-    defaults: ["adb_defaults"],
+    defaults: ["adbd_defaults", "host_adbd_supported"],
     recovery_available: true,
 
     srcs: [
@@ -467,7 +506,7 @@
 cc_binary {
     name: "abb",
 
-    defaults: ["adb_defaults"],
+    defaults: ["adbd_defaults"],
     recovery_available: false,
 
     srcs: [
@@ -500,7 +539,7 @@
 
 cc_test {
     name: "adbd_test",
-    defaults: ["adb_defaults"],
+    defaults: ["adbd_defaults"],
     srcs: libadb_test_srcs + [
         "daemon/services.cpp",
         "daemon/shell_service.cpp",