update_engine: Remove dependency on libupdate_engine from the client.

The update_engine_client is a very small DBus client that only
depends on a single utils:: function from the update_engine daemon
codebase. Because of this, it was forced to be linked against many
libraries that it didn't use.

This patch factors out this glib helper function to a new
glib_utils.{cc,h} file and includes only that in the
update_engine_client binary.

BUG=chromium:396440
TEST=FEATURES=test emerge-link update_engine

Change-Id: Icf8d8b3c6ebd22cdb39e6674fb3d9071071ec941
Reviewed-on: https://chromium-review.googlesource.com/209472
Reviewed-by: David Zeuthen <zeuthen@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/chrome_browser_proxy_resolver.cc b/chrome_browser_proxy_resolver.cc
index fbeb71f..6d489d8 100644
--- a/chrome_browser_proxy_resolver.cc
+++ b/chrome_browser_proxy_resolver.cc
@@ -16,6 +16,7 @@
 #include <google/protobuf/stubs/common.h>
 
 #include "update_engine/dbus_constants.h"
+#include "update_engine/glib_utils.h"
 #include "update_engine/utils.h"
 
 namespace chromeos_update_engine {
diff --git a/filesystem_copier_action.cc b/filesystem_copier_action.cc
index 23d3b0e..efd454d 100644
--- a/filesystem_copier_action.cc
+++ b/filesystem_copier_action.cc
@@ -20,6 +20,7 @@
 #include <gio/gunixoutputstream.h>
 #include <glib.h>
 
+#include "update_engine/glib_utils.h"
 #include "update_engine/hardware_interface.h"
 #include "update_engine/subprocess.h"
 #include "update_engine/system_state.h"
diff --git a/glib_utils.cc b/glib_utils.cc
new file mode 100644
index 0000000..523a16f
--- /dev/null
+++ b/glib_utils.cc
@@ -0,0 +1,28 @@
+// Copyright 2014 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 "update_engine/glib_utils.h"
+
+#include <base/strings/stringprintf.h>
+
+using std::string;
+
+namespace chromeos_update_engine {
+namespace utils {
+
+string GetAndFreeGError(GError** error) {
+  if (!*error) {
+    return "Unknown GLib error.";
+  }
+  string message =
+      base::StringPrintf("GError(%d): %s",
+                         (*error)->code,
+                         (*error)->message ? (*error)->message : "(unknown)");
+  g_error_free(*error);
+  *error = NULL;
+  return message;
+}
+
+}  // namespace utils
+}  // namespace chromeos_update_engine
diff --git a/glib_utils.h b/glib_utils.h
new file mode 100644
index 0000000..9fea076
--- /dev/null
+++ b/glib_utils.h
@@ -0,0 +1,22 @@
+// Copyright 2014 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 UPDATE_ENGINE_GLIB_UTILS_H_
+#define UPDATE_ENGINE_GLIB_UTILS_H_
+
+#include <string>
+
+#include <glib.h>
+
+namespace chromeos_update_engine {
+namespace utils {
+
+// Returns the error message, if any, from a GError pointer. Frees the GError
+// object and resets error to NULL.
+std::string GetAndFreeGError(GError** error);
+
+}  // namespace utils
+}  // namespace chromeos_update_engine
+
+#endif  // UPDATE_ENGINE_GLIB_UTILS_H_
diff --git a/main.cc b/main.cc
index c7d724b..c5ea6e5 100644
--- a/main.cc
+++ b/main.cc
@@ -14,13 +14,14 @@
 #include <gflags/gflags.h>
 #include <glib.h>
 #include <metrics/metrics_library.h>
-#include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 
 #include "update_engine/certificate_checker.h"
 #include "update_engine/dbus_constants.h"
 #include "update_engine/dbus_service.h"
 #include "update_engine/dbus_wrapper_interface.h"
+#include "update_engine/glib_utils.h"
 #include "update_engine/real_system_state.h"
 #include "update_engine/subprocess.h"
 #include "update_engine/terminator.h"
diff --git a/p2p_manager.cc b/p2p_manager.cc
index bef87bb..6d1a773 100644
--- a/p2p_manager.cc
+++ b/p2p_manager.cc
@@ -32,6 +32,7 @@
 #include <base/logging.h>
 #include <base/strings/stringprintf.h>
 
+#include "update_engine/glib_utils.h"
 #include "update_engine/utils.h"
 
 using base::FilePath;
diff --git a/subprocess.cc b/subprocess.cc
index f2ac17d..fb34267 100644
--- a/subprocess.cc
+++ b/subprocess.cc
@@ -16,7 +16,7 @@
 #include <base/strings/string_util.h>
 #include <base/strings/stringprintf.h>
 
-#include "update_engine/utils.h"
+#include "update_engine/glib_utils.h"
 
 using std::shared_ptr;
 using std::string;
diff --git a/update_attempter.cc b/update_attempter.cc
index e2e1201..bd086a8 100644
--- a/update_attempter.cc
+++ b/update_attempter.cc
@@ -8,8 +8,8 @@
 #include <memory>
 #include <set>
 #include <string>
-#include <vector>
 #include <utility>
+#include <vector>
 
 #include <base/file_util.h>
 #include <base/logging.h>
@@ -30,6 +30,7 @@
 #include "update_engine/dbus_wrapper_interface.h"
 #include "update_engine/download_action.h"
 #include "update_engine/filesystem_copier_action.h"
+#include "update_engine/glib_utils.h"
 #include "update_engine/hardware_interface.h"
 #include "update_engine/libcurl_http_fetcher.h"
 #include "update_engine/metrics.h"
diff --git a/update_engine.gyp b/update_engine.gyp
index 3750673..e8e3723 100644
--- a/update_engine.gyp
+++ b/update_engine.gyp
@@ -92,8 +92,6 @@
       'type': 'static_library',
       'dependencies': [
         'update_metadata-protos',
-        'update_engine-dbus-client',
-        'update_engine-dbus-server',
       ],
       'variables': {
         'exported_deps': [
@@ -152,6 +150,7 @@
         'file_descriptor.cc',
         'file_writer.cc',
         'filesystem_copier_action.cc',
+        'glib_utils.cc',
         'hardware.cc',
         'http_common.cc',
         'http_fetcher.cc',
@@ -199,7 +198,10 @@
     {
       'target_name': 'update_engine',
       'type': 'executable',
-      'dependencies': ['libupdate_engine'],
+      'dependencies': [
+        'libupdate_engine',
+        'update_engine-dbus-server',
+      ],
       'sources': [
         'main.cc',
       ]
@@ -208,8 +210,32 @@
     {
       'target_name': 'update_engine_client',
       'type': 'executable',
-      'dependencies': ['libupdate_engine'],
+      'dependencies': [
+        'update_engine-dbus-client',
+      ],
+      'variables': {
+        'exported_deps': [
+          'dbus-1',
+          'dbus-glib-1',
+          'glib-2.0',
+          'gthread-2.0',
+          'libchrome-<(libbase_ver)',
+          'libchromeos-<(libbase_ver)',
+        ],
+        'deps': ['<@(exported_deps)'],
+      },
+      'link_settings': {
+        'variables': {
+          'deps': [
+            '<@(exported_deps)',
+          ],
+        },
+        'libraries': [
+          '-lgflags',
+        ],
+      },
       'sources': [
+        'glib_utils.cc',
         'update_engine_client.cc',
       ]
     },
diff --git a/update_engine_client.cc b/update_engine_client.cc
index 516895a..eb092b5 100644
--- a/update_engine_client.cc
+++ b/update_engine_client.cc
@@ -4,24 +4,24 @@
 
 #include <string>
 
+#include <base/logging.h>
 #include <chromeos/dbus/service_constants.h>
 #include <dbus/dbus.h>
 #include <gflags/gflags.h>
 #include <glib.h>
 
 #include "update_engine/dbus_constants.h"
-#include "update_engine/subprocess.h"
-#include "update_engine/utils.h"
+#include "update_engine/glib_utils.h"
 
 extern "C" {
 #include "update_engine/update_engine.dbusclient.h"
 }
 
-using chromeos_update_engine::kUpdateEngineServiceName;
-using chromeos_update_engine::kUpdateEngineServicePath;
-using chromeos_update_engine::kUpdateEngineServiceInterface;
 using chromeos_update_engine::AttemptUpdateFlags;
 using chromeos_update_engine::kAttemptUpdateFlagNonInteractive;
+using chromeos_update_engine::kUpdateEngineServiceInterface;
+using chromeos_update_engine::kUpdateEngineServiceName;
+using chromeos_update_engine::kUpdateEngineServicePath;
 using chromeos_update_engine::utils::GetAndFreeGError;
 using std::string;
 
@@ -535,7 +535,6 @@
   // Boilerplate init commands.
   g_type_init();
   dbus_threads_init_default();
-  chromeos_update_engine::Subprocess::Init();
   google::ParseCommandLineFlags(&argc, &argv, true);
 
   // Update the status if requested.
diff --git a/update_manager/real_shill_provider.cc b/update_manager/real_shill_provider.cc
index b7a36d5..dd8261c 100644
--- a/update_manager/real_shill_provider.cc
+++ b/update_manager/real_shill_provider.cc
@@ -10,7 +10,7 @@
 #include <base/strings/stringprintf.h>
 #include <chromeos/dbus/service_constants.h>
 
-#include "update_engine/utils.h"
+#include "update_engine/glib_utils.h"
 
 using std::string;
 
diff --git a/utils.cc b/utils.cc
index 1235a6b..8f1a122 100644
--- a/utils.cc
+++ b/utils.cc
@@ -773,19 +773,6 @@
   return true;
 }
 
-string GetAndFreeGError(GError** error) {
-  if (!*error) {
-    return "Unknown GLib error.";
-  }
-  string message =
-      base::StringPrintf("GError(%d): %s",
-                         (*error)->code,
-                         (*error)->message ? (*error)->message : "(unknown)");
-  g_error_free(*error);
-  *error = NULL;
-  return message;
-}
-
 namespace {
 // Do the actual trigger. We do it as a main-loop callback to (try to) get a
 // consistent stack trace.
diff --git a/utils.h b/utils.h
index 1b3dca2..8c4850e 100644
--- a/utils.h
+++ b/utils.h
@@ -222,10 +222,6 @@
 // param. Returns true on success.
 bool GetBootloader(BootLoader* out_bootloader);
 
-// Returns the error message, if any, from a GError pointer. Frees the GError
-// object and resets error to NULL.
-std::string GetAndFreeGError(GError** error);
-
 // Schedules a Main Loop callback to trigger the crash reporter to perform an
 // upload as if this process had crashed.
 void ScheduleCrashReporterUpload();