libweave: Build libweave as shared library

Added own gyp files of libweave.
Added exports of necessary symbols.
Added own test runner.

CQ-DEPEND=CL:290403
BUG=brillo:1258
TEST='FEATURES=test emerge-gizmo libweave buffet'

Change-Id: I2c6c5f3e345b8baeabd9f56be44e91eaeb28bc8b
Reviewed-on: https://chromium-review.googlesource.com/290405
Reviewed-by: Vitaly Buka <vitalybuka@chromium.org>
Tested-by: Vitaly Buka <vitalybuka@chromium.org>
diff --git a/buffet/buffet.gyp b/buffet/buffet.gyp
index 400bf44..800737a 100644
--- a/buffet/buffet.gyp
+++ b/buffet/buffet.gyp
@@ -2,9 +2,6 @@
   'target_defaults': {
     'variables': {
       'deps': [
-        'dbus-1',
-        'expat',
-        'openssl',
         'libchrome-<(libbase_ver)',
         'libchromeos-<(libbase_ver)',
         'system_api',
@@ -12,8 +9,6 @@
     },
     'include_dirs': [
       '.',
-      # TODO(vitalybuka): Remove both.
-      '../libweave/include',
     ],
   },
   'targets': [
@@ -23,6 +18,18 @@
       'variables': {
         'dbus_adaptors_out_dir': 'include/buffet',
         'dbus_service_config': 'dbus_bindings/dbus-service-config.json',
+        'exported_deps': [
+          'libwebserv-<(libbase_ver)',
+          'libweave-<(libbase_ver)',
+        ],
+        'deps': ['<@(exported_deps)'],
+      },
+      'all_dependent_settings': {
+        'variables': {
+          'deps': [
+            '<@(exported_deps)',
+          ],
+        },
       },
       'sources': [
         'ap_manager_client.cc',
@@ -36,47 +43,6 @@
         'peerd_client.cc',
         'shill_client.cc',
         'webserv_client.cc',
-        '../libweave/src/base_api_handler.cc',
-        '../libweave/src/buffet_config.cc',
-        '../libweave/src/commands/cloud_command_proxy.cc',
-        '../libweave/src/commands/command_definition.cc',
-        '../libweave/src/commands/command_dictionary.cc',
-        '../libweave/src/commands/command_instance.cc',
-        '../libweave/src/commands/command_manager.cc',
-        '../libweave/src/commands/command_queue.cc',
-        '../libweave/src/commands/object_schema.cc',
-        '../libweave/src/commands/prop_constraints.cc',
-        '../libweave/src/commands/prop_types.cc',
-        '../libweave/src/commands/prop_values.cc',
-        '../libweave/src/commands/schema_constants.cc',
-        '../libweave/src/commands/schema_utils.cc',
-        '../libweave/src/commands/user_role.cc',
-        '../libweave/src/device_manager.cc',
-        '../libweave/src/device_registration_info.cc',
-        '../libweave/src/notification/notification_parser.cc',
-        '../libweave/src/notification/pull_channel.cc',
-        '../libweave/src/notification/xml_node.cc',
-        '../libweave/src/notification/xmpp_channel.cc',
-        '../libweave/src/notification/xmpp_iq_stanza_handler.cc',
-        '../libweave/src/notification/xmpp_stream_parser.cc',
-        '../libweave/src/privet/cloud_delegate.cc',
-        '../libweave/src/privet/constants.cc',
-        '../libweave/src/privet/device_delegate.cc',
-        '../libweave/src/privet/openssl_utils.cc',
-        '../libweave/src/privet/privet_handler.cc',
-        '../libweave/src/privet/privet_manager.cc',
-        '../libweave/src/privet/privet_types.cc',
-        '../libweave/src/privet/publisher.cc',
-        '../libweave/src/privet/security_manager.cc',
-        '../libweave/src/privet/wifi_bootstrap_manager.cc',
-        '../libweave/src/privet/wifi_ssid_generator.cc',
-        '../libweave/src/registration_status.cc',
-        '../libweave/src/states/error_codes.cc',
-        '../libweave/src/states/state_change_queue.cc',
-        '../libweave/src/states/state_manager.cc',
-        '../libweave/src/states/state_package.cc',
-        '../libweave/src/storage_impls.cc',
-        '../libweave/src/utils.cc',
       ],
       'includes': ['../common-mk/generate-dbus-adaptors.gypi'],
       'actions': [
@@ -143,12 +109,6 @@
       'dependencies': [
         'buffet_common',
       ],
-      'variables': {
-        'exported_deps': [
-          'libwebserv-<(libbase_ver)',
-        ],
-        'deps': ['<@(exported_deps)'],
-      },
       'sources': [
         'main.cc',
       ],
@@ -181,37 +141,14 @@
             'deps': [
               'libchrome-test-<(libbase_ver)',
               'libchromeos-test-<(libbase_ver)',
+              'libweave-test-<(libbase_ver)',
             ],
           },
           'includes': ['../common-mk/common_test.gypi'],
           'sources': [
+            'buffet_testrunner.cc',
             'dbus_command_proxy_unittest.cc',
             'dbus_conversion_unittest.cc',
-            '../libweave/src/base_api_handler_unittest.cc',
-            '../libweave/src/buffet_config_unittest.cc',
-            '../libweave/src/buffet_testrunner.cc',
-            '../libweave/src/commands/cloud_command_proxy_unittest.cc',
-            '../libweave/src/commands/command_definition_unittest.cc',
-            '../libweave/src/commands/command_dictionary_unittest.cc',
-            '../libweave/src/commands/command_instance_unittest.cc',
-            '../libweave/src/commands/command_manager_unittest.cc',
-            '../libweave/src/commands/command_queue_unittest.cc',
-            '../libweave/src/commands/mock_command.cc',
-            '../libweave/src/commands/object_schema_unittest.cc',
-            '../libweave/src/commands/schema_utils_unittest.cc',
-            '../libweave/src/commands/unittest_utils.cc',
-            '../libweave/src/device_registration_info_unittest.cc',
-            '../libweave/src/notification/notification_parser_unittest.cc',
-            '../libweave/src/notification/xml_node_unittest.cc',
-            '../libweave/src/notification/xmpp_channel_unittest.cc',
-            '../libweave/src/notification/xmpp_iq_stanza_handler_unittest.cc',
-            '../libweave/src/notification/xmpp_stream_parser_unittest.cc',
-            '../libweave/src/privet/privet_handler_unittest.cc',
-            '../libweave/src/privet/security_manager_unittest.cc',
-            '../libweave/src/privet/wifi_ssid_generator_unittest.cc',
-            '../libweave/src/states/state_change_queue_unittest.cc',
-            '../libweave/src/states/state_manager_unittest.cc',
-            '../libweave/src/states/state_package_unittest.cc',
           ],
         },
       ],
diff --git a/libweave/src/buffet_testrunner.cc b/buffet/buffet_testrunner.cc
similarity index 100%
rename from libweave/src/buffet_testrunner.cc
rename to buffet/buffet_testrunner.cc
diff --git a/libweave/include/weave/device.h b/libweave/include/weave/device.h
index 539e429..660fad8 100644
--- a/libweave/include/weave/device.h
+++ b/libweave/include/weave/device.h
@@ -13,6 +13,7 @@
 #include <weave/cloud.h>
 #include <weave/commands.h>
 #include <weave/config.h>
+#include <weave/export.h>
 #include <weave/http_server.h>
 #include <weave/mdns.h>
 #include <weave/network.h>
@@ -48,7 +49,7 @@
   virtual Cloud* GetCloud() = 0;
   virtual Privet* GetPrivet() = 0;
 
-  static std::unique_ptr<Device> Create();
+  LIBWEAVE_EXPORT static std::unique_ptr<Device> Create();
 };
 
 }  // namespace weave
diff --git a/libweave/include/weave/enum_to_string.h b/libweave/include/weave/enum_to_string.h
index 647f990..9d2aa59 100644
--- a/libweave/include/weave/enum_to_string.h
+++ b/libweave/include/weave/enum_to_string.h
@@ -9,6 +9,8 @@
 
 #include <string>
 
+#include <weave/export.h>
+
 namespace weave {
 
 // Helps to map enumeration to stings and back.
@@ -30,7 +32,7 @@
     const char* const name;
   };
 
-  EnumToStringMap();
+  LIBWEAVE_EXPORT EnumToStringMap();
 
   const Map* begin() const { return begin_; }
   const Map* end() const { return end_; }
diff --git a/libweave/include/weave/export.h b/libweave/include/weave/export.h
new file mode 100644
index 0000000..72220a7
--- /dev/null
+++ b/libweave/include/weave/export.h
@@ -0,0 +1,11 @@
+// Copyright 2015 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef LIBWEAVE_INCLUDE_WEAVE_EXPORT_H_
+#define LIBWEAVE_INCLUDE_WEAVE_EXPORT_H_
+
+#define LIBWEAVE_EXPORT __attribute__((__visibility__("default")))
+#define LIBWEAVE_PRIVATE __attribute__((__visibility__("hidden")))
+
+#endif  // LIBWEAVE_INCLUDE_WEAVE_EXPORT_H_
diff --git a/libweave/libweave-test.pc.in b/libweave/libweave-test.pc.in
new file mode 100644
index 0000000..9f9cdda
--- /dev/null
+++ b/libweave/libweave-test.pc.in
@@ -0,0 +1,7 @@
+bslot=@BSLOT@
+
+Name: libweave-test
+Description: Weave test library
+Version: ${bslot}
+Requires: @PRIVATE_PC@
+Libs: -lweave-test-${bslot}
diff --git a/libweave/libweave.gyp b/libweave/libweave.gyp
new file mode 100644
index 0000000..cdc7077
--- /dev/null
+++ b/libweave/libweave.gyp
@@ -0,0 +1,133 @@
+{
+  'target_defaults': {
+    'variables': {
+      'deps': [
+        'expat',
+        'libchrome-<(libbase_ver)',
+        'libchromeos-<(libbase_ver)',
+      ],
+    },
+    'include_dirs': [
+      '.',
+      '../libweave/include',
+    ],
+  },
+  'targets': [
+    {
+      'target_name': 'libweave_common',
+      'type': 'static_library',
+      'sources': [
+        'src/base_api_handler.cc',
+        'src/buffet_config.cc',
+        'src/commands/cloud_command_proxy.cc',
+        'src/commands/command_definition.cc',
+        'src/commands/command_dictionary.cc',
+        'src/commands/command_instance.cc',
+        'src/commands/command_manager.cc',
+        'src/commands/command_queue.cc',
+        'src/commands/object_schema.cc',
+        'src/commands/prop_constraints.cc',
+        'src/commands/prop_types.cc',
+        'src/commands/prop_values.cc',
+        'src/commands/schema_constants.cc',
+        'src/commands/schema_utils.cc',
+        'src/commands/user_role.cc',
+        'src/device_manager.cc',
+        'src/device_registration_info.cc',
+        'src/notification/notification_parser.cc',
+        'src/notification/pull_channel.cc',
+        'src/notification/xml_node.cc',
+        'src/notification/xmpp_channel.cc',
+        'src/notification/xmpp_iq_stanza_handler.cc',
+        'src/notification/xmpp_stream_parser.cc',
+        'src/privet/cloud_delegate.cc',
+        'src/privet/constants.cc',
+        'src/privet/device_delegate.cc',
+        'src/privet/openssl_utils.cc',
+        'src/privet/privet_handler.cc',
+        'src/privet/privet_manager.cc',
+        'src/privet/privet_types.cc',
+        'src/privet/publisher.cc',
+        'src/privet/security_manager.cc',
+        'src/privet/wifi_bootstrap_manager.cc',
+        'src/privet/wifi_ssid_generator.cc',
+        'src/registration_status.cc',
+        'src/states/error_codes.cc',
+        'src/states/state_change_queue.cc',
+        'src/states/state_manager.cc',
+        'src/states/state_package.cc',
+        'src/storage_impls.cc',
+        'src/utils.cc',
+      ],
+    },
+    {
+      'target_name': 'libweave-<(libbase_ver)',
+      'type': 'shared_library',
+      'includes': [
+        '../common-mk/deps.gypi',
+      ],
+      'dependencies': [
+        'libweave_common',
+      ],
+      'sources': [
+        'src/empty.cc',
+      ],
+    },
+    {
+      'target_name': 'libweave-test-<(libbase_ver)',
+      'type': 'static_library',
+      'standalone_static_library': 1,
+      'sources': [
+        'src/commands/mock_command.cc',
+        'src/commands/unittest_utils.cc',
+      ],
+      'includes': ['../common-mk/deps.gypi'],
+    },
+  ],
+  'conditions': [
+    ['USE_test == 1', {
+      'targets': [
+        {
+          'target_name': 'libweave_testrunner',
+          'type': 'executable',
+          'variables': {
+            'deps': [
+              'libchrome-test-<(libbase_ver)',
+              'libchromeos-test-<(libbase_ver)',
+            ],
+          },
+          'dependencies': [
+            'libweave_common',
+            'libweave-test-<(libbase_ver)',
+          ],
+          'includes': ['../common-mk/common_test.gypi'],
+          'sources': [
+            'src/base_api_handler_unittest.cc',
+            'src/buffet_config_unittest.cc',
+            'src/commands/cloud_command_proxy_unittest.cc',
+            'src/commands/command_definition_unittest.cc',
+            'src/commands/command_dictionary_unittest.cc',
+            'src/commands/command_instance_unittest.cc',
+            'src/commands/command_manager_unittest.cc',
+            'src/commands/command_queue_unittest.cc',
+            'src/commands/object_schema_unittest.cc',
+            'src/commands/schema_utils_unittest.cc',
+            'src/device_registration_info_unittest.cc',
+            'src/notification/notification_parser_unittest.cc',
+            'src/notification/xml_node_unittest.cc',
+            'src/notification/xmpp_channel_unittest.cc',
+            'src/notification/xmpp_iq_stanza_handler_unittest.cc',
+            'src/notification/xmpp_stream_parser_unittest.cc',
+            'src/privet/privet_handler_unittest.cc',
+            'src/privet/security_manager_unittest.cc',
+            'src/privet/wifi_ssid_generator_unittest.cc',
+            'src/states/state_change_queue_unittest.cc',
+            'src/states/state_manager_unittest.cc',
+            'src/states/state_package_unittest.cc',
+            'src/weave_testrunner.cc',
+          ],
+        },
+      ],
+    }],
+  ],
+}
diff --git a/libweave/libweave.pc.in b/libweave/libweave.pc.in
new file mode 100644
index 0000000..2257380
--- /dev/null
+++ b/libweave/libweave.pc.in
@@ -0,0 +1,7 @@
+bslot=@BSLOT@
+
+Name: libweave
+Description: Weave device library
+Version: ${bslot}
+Requires.private: @PRIVATE_PC@
+Libs: -lweave-${bslot}
diff --git a/libweave/platform2_preinstall.sh b/libweave/platform2_preinstall.sh
new file mode 100755
index 0000000..dfd19c9
--- /dev/null
+++ b/libweave/platform2_preinstall.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+# Copyright 2015 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+set -e
+
+OUT=$1
+v=$2
+
+deps=$(<"${OUT}"/gen/libweave-${v}-deps.txt)
+sed \
+  -e "s/@BSLOT@/${v}/g" \
+  -e "s/@PRIVATE_PC@/${deps}/g" \
+  "libweave.pc.in" > "${OUT}/lib/libweave-${v}.pc"
+
+
+deps_test=$(<"${OUT}"/gen/libweave-test-${v}-deps.txt)
+sed \
+  -e "s/@BSLOT@/${v}/g" \
+  -e "s/@PRIVATE_PC@/${deps_test}/g" \
+  "libweave-test.pc.in" > "${OUT}/lib/libweave-test-${v}.pc"
diff --git a/libweave/src/empty.cc b/libweave/src/empty.cc
new file mode 100644
index 0000000..1ad31f8
--- /dev/null
+++ b/libweave/src/empty.cc
@@ -0,0 +1,7 @@
+// Copyright 2015 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This file is left empty deliberately to work around some dependency
+// generation in GYP. In GYP for custom actions to run properly, the 'sources'
+// section must contain at least one recognizable source file.
diff --git a/libweave/src/weave_testrunner.cc b/libweave/src/weave_testrunner.cc
new file mode 100644
index 0000000..f050805
--- /dev/null
+++ b/libweave/src/weave_testrunner.cc
@@ -0,0 +1,12 @@
+// Copyright 2015 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <base/at_exit.h>
+#include <gtest/gtest.h>
+
+int main(int argc, char** argv) {
+  base::AtExitManager exit_manager;
+  ::testing::InitGoogleTest(&argc, argv);
+  return RUN_ALL_TESTS();
+}