update_engine: Migrate build system to gyp.

This change migrates the SConstruct file to the platform2 gyp
infrastructure without changing the way UE is compiled. The
code is still compiled as a single static library shared
between delta_generator, update_engine and update_engine_client.

This patch adds two generic .gypi rules to generate bindings for
DBus glib and public keys using openssl.

update_engine_unittests requires to run the openssl version from the
board and not the one on the host. This changes does that by using
SYSROOT when possible.

delta_generator is built without -pie to avoid a bug on how it
is run from au-generator.zip.

BUG=chromium:393293,chromium:394241
TEST=emerge-link update_engine; sudo emerge update_engine
TEST=cros_workon_make --reconf --test update_engine
TEST=cbuildbot link-release amd64-generic-full x86-generic-full
CQ-DEPEND=CL:Id81c7dca798ac8003252259eed03a12a60054437

Change-Id: Icc5e24ac202e552275cd197ee0b9a4a3d9b4a47c
Reviewed-on: https://chromium-review.googlesource.com/208487
Tested-by: Alex Deymo <deymo@chromium.org>
Reviewed-by: Bertrand Simonnet <bsimonnet@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
diff --git a/SConstruct b/SConstruct
deleted file mode 100644
index 9cc76e5..0000000
--- a/SConstruct
+++ /dev/null
@@ -1,371 +0,0 @@
-# Copyright (c) 2012 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.
-
-import os
-
-# Protobuffer compilation
-def ProtocolBufferEmitter(target, source, env):
-  """ Inputs:
-          target: list of targets to compile to
-          source: list of sources to compile
-          env: the scons environment in which we are compiling
-      Outputs:
-          target: the list of targets we'll emit
-          source: the list of sources we'll compile"""
-  output = str(source[0])
-  output = output[0:output.rfind('.proto')]
-  target = [
-    output + '.pb.cc',
-    output + '.pb.h',
-  ]
-  return target, source
-
-def ProtocolBufferGenerator(source, target, env, for_signature):
-  """ Inputs:
-          source: list of sources to process
-          target: list of targets to generate
-          env: scons environment in which we are working
-          for_signature: unused
-      Outputs: a list of commands to execute to generate the targets from
-               the sources."""
-  commands = [
-    '/usr/bin/protoc '
-    ' --proto_path . ${SOURCES} --cpp_out .']
-  return commands
-
-proto_builder = Builder(generator = ProtocolBufferGenerator,
-                        emitter = ProtocolBufferEmitter,
-                        single_source = 1,
-                        suffix = '.pb.cc')
-
-def DbusBindingsEmitter(target, source, env):
-  """ Inputs:
-          target: unused
-          source: list containing the source .xml file
-          env: the scons environment in which we are compiling
-      Outputs:
-          target: the list of targets we'll emit
-          source: the list of sources we'll process"""
-  output = str(source[0])
-  output = output[0:output.rfind('.xml')]
-  target = [
-    output + '.dbusserver.h',
-    output + '.dbusclient.h'
-  ]
-  return target, source
-
-def DbusBindingsGenerator(source, target, env, for_signature):
-  """ Inputs:
-          source: list of sources to process
-          target: list of targets to generate
-          env: scons environment in which we are working
-          for_signature: unused
-      Outputs: a list of commands to execute to generate the targets from
-               the sources."""
-  commands = []
-  for target_file in target:
-    if str(target_file).endswith('.dbusserver.h'):
-      mode_flag = '--mode=glib-server '
-    else:
-      mode_flag = '--mode=glib-client '
-    cmd = '/usr/bin/dbus-binding-tool %s --prefix=update_engine_service ' \
-          '%s > %s' % (mode_flag, str(source[0]), str(target_file))
-    commands.append(cmd)
-  return commands
-
-dbus_bindings_builder = Builder(generator = DbusBindingsGenerator,
-                                emitter = DbusBindingsEmitter,
-                                single_source = 1,
-                                suffix = 'dbusclient.h')
-
-# Public key generation
-def PublicKeyEmitter(target, source, env):
-  """ Inputs:
-          target: list of targets to compile to
-          source: list of sources to compile
-          env: the scons environment in which we are compiling
-      Outputs:
-          target: the list of targets we'll emit
-          source: the list of sources we'll compile"""
-  targets = []
-  for source_file in source:
-    output = str(source_file)
-    output = output[0:output.rfind('.pem')]
-    output += '.pub.pem'
-    targets.append(output)
-  return targets, source
-
-def PublicKeyGenerator(source, target, env, for_signature):
-  """ Inputs:
-          source: list of sources to process
-          target: list of targets to generate
-          env: scons environment in which we are working
-          for_signature: unused
-      Outputs: a list of commands to execute to generate the targets from
-               the sources."""
-  commands = []
-  for source_file in source:
-    output = str(source_file)
-    output = output[0:output.rfind('.pem')]
-    output += '.pub.pem'
-    cmd = '/usr/bin/openssl rsa -in %s -pubout -out %s' % (source_file, output)
-    commands.append(cmd)
-  return commands
-
-public_key_builder = Builder(generator = PublicKeyGenerator,
-                             emitter = PublicKeyEmitter,
-                             suffix = '.pub.pem')
-
-env = Environment()
-for key in Split('CC CXX AR RANLIB LD NM'):
-  value = os.environ.get(key)
-  if value != None:
-    env[key] = value
-for key in Split('CFLAGS CCFLAGS LDFLAGS CPPPATH LIBPATH'):
-  value = os.environ.get(key)
-  if value != None:
-    env[key] = Split(value)
-
-for key in Split('PKG_CONFIG_LIBDIR PKG_CONFIG_PATH SYSROOT'):
-  if os.environ.has_key(key):
-    env['ENV'][key] = os.environ[key]
-
-
-env['LINKFLAGS'] = Split("""
-    -Wl,--gc-sections""")
-env['LINKFLAGS'] += env.get('LDFLAGS', [])
-
-env['CCFLAGS'] = Split("""
-    -g
-    -ffunction-sections
-    -fno-exceptions
-    -fno-strict-aliasing
-    -std=gnu++11
-    -Wall
-    -Wextra
-    -Werror
-    -Wno-unused-parameter
-    -Wno-deprecated-register
-    -D__STDC_FORMAT_MACROS=1
-    -D_FILE_OFFSET_BITS=64
-    -D_POSIX_C_SOURCE=199309L""")
-env['CCFLAGS'] += env['CFLAGS']
-
-BASE_VER = os.environ.get('BASE_VER', '271506')
-env['LIBS'] = Split("""bz2
-                       gflags
-                       policy-%s
-                       rootdev
-                       rt
-                       vboot_host""" % (BASE_VER,))
-env['CPPPATH'] = ['..']
-env['BUILDERS']['ProtocolBuffer'] = proto_builder
-env['BUILDERS']['DbusBindings'] = dbus_bindings_builder
-env['BUILDERS']['PublicKey'] = public_key_builder
-
-# Fix issue with scons not passing pkg-config vars through the environment.
-for key in Split('PKG_CONFIG_LIBDIR PKG_CONFIG_PATH'):
-  if os.environ.has_key(key):
-    env['ENV'][key] = os.environ[key]
-
-pkgconfig = os.environ.get('PKG_CONFIG', 'pkg-config')
-
-env.ParseConfig(pkgconfig + ' --cflags --libs ' + ' '.join((
-                'dbus-1',
-                'dbus-glib-1',
-                'ext2fs',
-                'gio-2.0',
-                'gio-unix-2.0',
-                'glib-2.0',
-                'gthread-2.0',
-                'libchrome-%s' % BASE_VER,
-                'libchromeos-%s' % BASE_VER,
-                'libcrypto',
-                'libcurl',
-                'libmetrics-%s' % BASE_VER,
-                'libssl',
-                'libxml-2.0',
-                'protobuf')))
-env.ProtocolBuffer('update_metadata.pb.cc', 'update_metadata.proto')
-env.PublicKey('unittest_key.pub.pem', 'unittest_key.pem')
-env.PublicKey('unittest_key2.pub.pem', 'unittest_key2.pem')
-
-# Target name is derived from the source .xml filename. The passed name is
-# unused.
-env.DbusBindings(None, 'update_engine.xml')
-
-if ARGUMENTS.get('debug', 0):
-  env['CCFLAGS'] += ['-fprofile-arcs', '-ftest-coverage']
-  env['LIBS'] += ['bz2', 'gcov']
-
-sources = Split("""action_processor.cc
-                   bzip.cc
-                   bzip_extent_writer.cc
-                   certificate_checker.cc
-                   chrome_browser_proxy_resolver.cc
-                   clock.cc
-                   connection_manager.cc
-                   constants.cc
-                   dbus_service.cc
-                   delta_performer.cc
-                   download_action.cc
-                   extent_ranges.cc
-                   extent_writer.cc
-                   file_descriptor.cc
-                   file_writer.cc
-                   filesystem_copier_action.cc
-                   hardware.cc
-                   http_common.cc
-                   http_fetcher.cc
-                   hwid_override.cc
-                   install_plan.cc
-                   libcurl_http_fetcher.cc
-                   metrics.cc
-                   multi_range_http_fetcher.cc
-                   omaha_hash_calculator.cc
-                   omaha_request_action.cc
-                   omaha_request_params.cc
-                   omaha_response_handler_action.cc
-                   p2p_manager.cc
-                   payload_constants.cc
-                   payload_generator/cycle_breaker.cc
-                   payload_generator/delta_diff_generator.cc
-                   payload_generator/extent_mapper.cc
-                   payload_generator/filesystem_iterator.cc
-                   payload_generator/full_update_generator.cc
-                   payload_generator/graph_utils.cc
-                   payload_generator/metadata.cc
-                   payload_generator/tarjan.cc
-                   payload_generator/topological_sort.cc
-                   payload_signer.cc
-                   payload_state.cc
-                   postinstall_runner_action.cc
-                   prefs.cc
-                   proxy_resolver.cc
-                   real_system_state.cc
-                   simple_key_value_store.cc
-                   subprocess.cc
-                   terminator.cc
-                   update_attempter.cc
-                   update_check_scheduler.cc
-                   update_manager/boxed_value.cc
-                   update_manager/chromeos_policy.cc
-                   update_manager/evaluation_context.cc
-                   update_manager/event_loop.cc
-                   update_manager/policy.cc
-                   update_manager/real_config_provider.cc
-                   update_manager/real_device_policy_provider.cc
-                   update_manager/real_random_provider.cc
-                   update_manager/real_shill_provider.cc
-                   update_manager/real_system_provider.cc
-                   update_manager/real_time_provider.cc
-                   update_manager/real_updater_provider.cc
-                   update_manager/state_factory.cc
-                   update_manager/update_manager.cc
-                   update_metadata.pb.cc
-                   utils.cc""")
-main = ['main.cc']
-
-unittest_sources = Split("""action_pipe_unittest.cc
-                            action_processor_unittest.cc
-                            action_unittest.cc
-                            bzip_extent_writer_unittest.cc
-                            certificate_checker_unittest.cc
-                            chrome_browser_proxy_resolver_unittest.cc
-                            connection_manager_unittest.cc
-                            delta_performer_unittest.cc
-                            download_action_unittest.cc
-                            extent_ranges_unittest.cc
-                            extent_writer_unittest.cc
-                            fake_prefs.cc
-                            fake_system_state.cc
-                            file_writer_unittest.cc
-                            filesystem_copier_action_unittest.cc
-                            http_fetcher_unittest.cc
-                            hwid_override_unittest.cc
-                            mock_http_fetcher.cc
-                            omaha_hash_calculator_unittest.cc
-                            omaha_request_action_unittest.cc
-                            omaha_request_params_unittest.cc
-                            omaha_response_handler_action_unittest.cc
-                            p2p_manager_unittest.cc
-                            payload_generator/cycle_breaker_unittest.cc
-                            payload_generator/delta_diff_generator_unittest.cc
-                            payload_generator/extent_mapper_unittest.cc
-                            payload_generator/filesystem_iterator_unittest.cc
-                            payload_generator/full_update_generator_unittest.cc
-                            payload_generator/graph_utils_unittest.cc
-                            payload_generator/metadata_unittest.cc
-                            payload_generator/tarjan_unittest.cc
-                            payload_generator/topological_sort_unittest.cc
-                            payload_signer_unittest.cc
-                            payload_state_unittest.cc
-                            postinstall_runner_action_unittest.cc
-                            prefs_unittest.cc
-                            simple_key_value_store_unittest.cc
-                            subprocess_unittest.cc
-                            terminator_unittest.cc
-                            test_utils.cc
-                            update_attempter_unittest.cc
-                            update_check_scheduler_unittest.cc
-                            update_manager/boxed_value_unittest.cc
-                            update_manager/chromeos_policy_unittest.cc
-                            update_manager/evaluation_context_unittest.cc
-                            update_manager/event_loop_unittest.cc
-                            update_manager/generic_variables_unittest.cc
-                            update_manager/prng_unittest.cc
-                            update_manager/real_config_provider_unittest.cc
-                            update_manager/real_device_policy_provider_unittest.cc
-                            update_manager/real_random_provider_unittest.cc
-                            update_manager/real_shill_provider_unittest.cc
-                            update_manager/real_system_provider_unittest.cc
-                            update_manager/real_time_provider_unittest.cc
-                            update_manager/real_updater_provider_unittest.cc
-                            update_manager/umtest_utils.cc
-                            update_manager/update_manager_unittest.cc
-                            update_manager/variable_unittest.cc
-                            utils_unittest.cc
-                            zip_unittest.cc""")
-unittest_main = ['testrunner.cc']
-
-client_main = ['update_engine_client.cc']
-
-delta_generator_main = ['payload_generator/generate_delta_main.cc']
-
-# Hack to generate header files first. They are generated as a side effect
-# of generating other files (usually their corresponding .c(c) files),
-# so we make all sources depend on those other files.
-all_sources = []
-all_sources.extend(sources)
-all_sources.extend(unittest_sources)
-all_sources.extend(main)
-all_sources.extend(unittest_main)
-all_sources.extend(client_main)
-all_sources.extend(delta_generator_main)
-for source in all_sources:
-  if source.endswith('_unittest.cc'):
-    env.Depends(source, 'unittest_key.pub.pem')
-  if source.endswith('.pb.cc'):
-    continue
-  env.Depends(source, 'update_metadata.pb.cc')
-  env.Depends(source, 'update_engine.dbusclient.h')
-
-update_engine_core = env.Library('update_engine_core', sources)
-env.Prepend(LIBS=[update_engine_core])
-
-env.Program('update_engine', main)
-
-client_cmd = env.Program('update_engine_client', client_main);
-
-delta_generator_cmd = env.Program('delta_generator',
-                                  delta_generator_main)
-
-http_server_cmd = env.Program('test_http_server', 'test_http_server.cc')
-
-unittest_env = env.Clone()
-unittest_env.Append(LIBS=['gmock', 'gtest'])
-unittest_cmd = unittest_env.Program('update_engine_unittests',
-                           unittest_sources + unittest_main)
-Clean(unittest_cmd, Glob('*.gcda') + Glob('*.gcno') + Glob('*.gcov') +
-                    Split('html app.info'))
diff --git a/delta_performer_unittest.cc b/delta_performer_unittest.cc
index 8d4eacd..226427f 100644
--- a/delta_performer_unittest.cc
+++ b/delta_performer_unittest.cc
@@ -196,9 +196,9 @@
   // Generates a new private key that will not match the public key.
   if (signature_test == kSignatureGeneratedShellBadKey) {
     LOG(INFO) << "Generating a mismatched private key.";
-    ASSERT_EQ(0,
-              System(base::StringPrintf("openssl genrsa -out %s 2048",
-                                        private_key_path.c_str())));
+    ASSERT_EQ(0, System(base::StringPrintf(
+        "%s genrsa -out %s 2048",
+        utils::GetPathOnBoard("openssl").c_str(), private_key_path.c_str())));
   }
   int signature_size = GetSignatureSize(private_key_path);
   string hash_file;
@@ -230,7 +230,8 @@
   ScopedPathUnlinker sig_unlinker(sig_file);
   ASSERT_EQ(0,
             System(base::StringPrintf(
-                "openssl rsautl -raw -sign -inkey %s -in %s -out %s",
+                "%s rsautl -raw -sign -inkey %s -in %s -out %s",
+                utils::GetPathOnBoard("openssl").c_str(),
                 private_key_path.c_str(),
                 hash_file.c_str(),
                 sig_file.c_str())));
@@ -241,7 +242,8 @@
       signature_test == kSignatureGeneratedShellRotateCl2) {
     ASSERT_EQ(0,
               System(base::StringPrintf(
-                  "openssl rsautl -raw -sign -inkey %s -in %s -out %s",
+                  "%s rsautl -raw -sign -inkey %s -in %s -out %s",
+                  utils::GetPathOnBoard("openssl").c_str(),
                   kUnittestPrivateKey2Path,
                   hash_file.c_str(),
                   sig_file2.c_str())));
diff --git a/payload_signer.cc b/payload_signer.cc
index 15c2b32..3d09737 100644
--- a/payload_signer.cc
+++ b/payload_signer.cc
@@ -230,6 +230,10 @@
   cmd[cmd.size() - 3] = hash_path;
   cmd[cmd.size() - 1] = sig_path;
 
+  // When running unittests, we need to use the openssl version from the
+  // SYSROOT instead of the one on the $PATH (host).
+  cmd[0] = utils::GetPathOnBoard("openssl");
+
   int return_code = 0;
   TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code, NULL));
   TEST_AND_RETURN_FALSE(return_code == 0);
diff --git a/update_engine.gyp b/update_engine.gyp
new file mode 100644
index 0000000..24a9a98
--- /dev/null
+++ b/update_engine.gyp
@@ -0,0 +1,333 @@
+{
+  'target_defaults': {
+    'variables': {
+      'deps': [
+        'libchrome-<(libbase_ver)',
+        'libchromeos-<(libbase_ver)',
+      ],
+      # Defaults if the -DUSE_* flags are not passed to gyp is 0. You can set
+      # the default value for the USE flag in the ebuild.
+      'USE_hwid_override%': '0',
+      'USE_power_management%': '0',
+    },
+    'cflags': [
+      '-g',
+      '-ffunction-sections',
+      '-Wall',
+      '-Wextra',
+      '-Werror',
+      '-Wno-unused-parameter',
+      '-Wno-deprecated-register',
+    ],
+    'cflags_cc': [
+      '-fno-strict-aliasing',
+      '-std=gnu++11',
+    ],
+    'ldflags': [
+      '-Wl,--gc-sections',
+    ],
+    'defines': [
+      '__STDC_FORMAT_MACROS=1',
+      '_FILE_OFFSET_BITS=64',
+      '_POSIX_C_SOURCE=199309L',
+      'USE_HWID_OVERRIDE=<(USE_hwid_override)',
+      'USE_POWER_MANAGEMENT=<(USE_power_management)',
+    ],
+  },
+  'targets': [
+    # Protobufs.
+    {
+      'target_name': 'update_metadata-protos',
+      'type': 'static_library',
+      'variables': {
+        'proto_in_dir': '.',
+        'proto_out_dir': 'include/update_engine',
+      },
+      'sources': [
+        'update_metadata.proto'
+      ],
+      'includes': ['../common-mk/protoc.gypi'],
+    },
+    # D-Bus glib bindings.
+    {
+      'target_name': 'update_engine-dbus-client',
+      'type': 'none',
+      'variables': {
+        'dbus_glib_type': 'client',
+        'dbus_glib_out_dir': 'include/update_engine',
+        'dbus_glib_prefix': 'update_engine_service',
+      },
+      'sources': [
+        'update_engine.xml',
+      ],
+      'includes': ['../common-mk/dbus_glib.gypi'],
+    },
+    {
+      'target_name': 'update_engine-dbus-server',
+      'type': 'none',
+      'variables': {
+        'dbus_glib_type': 'server',
+        'dbus_glib_out_dir': 'include/update_engine',
+        'dbus_glib_prefix': 'update_engine_service',
+      },
+      'sources': [
+        'update_engine.xml',
+      ],
+      'includes': ['../common-mk/dbus_glib.gypi'],
+    },
+    # The main static_library with all the code.
+    {
+      'target_name': 'libupdate_engine',
+      'type': 'static_library',
+      'dependencies': [
+        'update_metadata-protos',
+        'update_engine-dbus-client',
+        'update_engine-dbus-server',
+      ],
+      'variables': {
+        'exported_deps': [
+          'dbus-1',
+          'dbus-glib-1',
+          'ext2fs',
+          'gio-2.0',
+          'gio-unix-2.0',
+          'glib-2.0',
+          'gthread-2.0',
+          'libchrome-<(libbase_ver)',
+          'libchromeos-<(libbase_ver)',
+          'libcrypto',
+          'libcurl',
+          'libmetrics-<(libbase_ver)',
+          'libssl',
+          'libxml-2.0',
+          'protobuf',
+        ],
+        'deps': ['<@(exported_deps)'],
+      },
+      'all_dependent_settings': {
+        'variables': {
+          'deps': [
+            '<@(exported_deps)',
+          ],
+        },
+      },
+      'link_settings': {
+        'variables': {
+          'deps': [
+            '<@(exported_deps)',
+          ],
+        },
+        'libraries': [
+          '-lbz2',
+          '-lgflags',
+          '-lpolicy-<(libbase_ver)',
+          '-lrootdev',
+          '-lrt',
+          '-lvboot_host',
+        ],
+      },
+      'sources': [
+        'action_processor.cc',
+        'bzip.cc',
+        'bzip_extent_writer.cc',
+        'certificate_checker.cc',
+        'chrome_browser_proxy_resolver.cc',
+        'clock.cc',
+        'connection_manager.cc',
+        'constants.cc',
+        'dbus_service.cc',
+        'delta_performer.cc',
+        'download_action.cc',
+        'extent_ranges.cc',
+        'extent_writer.cc',
+        'file_descriptor.cc',
+        'file_writer.cc',
+        'filesystem_copier_action.cc',
+        'hardware.cc',
+        'http_common.cc',
+        'http_fetcher.cc',
+        'hwid_override.cc',
+        'install_plan.cc',
+        'libcurl_http_fetcher.cc',
+        'metrics.cc',
+        'multi_range_http_fetcher.cc',
+        'omaha_hash_calculator.cc',
+        'omaha_request_action.cc',
+        'omaha_request_params.cc',
+        'omaha_response_handler_action.cc',
+        'p2p_manager.cc',
+        'payload_constants.cc',
+        'payload_generator/cycle_breaker.cc',
+        'payload_generator/delta_diff_generator.cc',
+        'payload_generator/extent_mapper.cc',
+        'payload_generator/filesystem_iterator.cc',
+        'payload_generator/full_update_generator.cc',
+        'payload_generator/graph_utils.cc',
+        'payload_generator/metadata.cc',
+        'payload_generator/tarjan.cc',
+        'payload_generator/topological_sort.cc',
+        'payload_signer.cc',
+        'payload_state.cc',
+        'postinstall_runner_action.cc',
+        'prefs.cc',
+        'proxy_resolver.cc',
+        'real_system_state.cc',
+        'simple_key_value_store.cc',
+        'subprocess.cc',
+        'terminator.cc',
+        'update_attempter.cc',
+        'update_check_scheduler.cc',
+        'update_manager/boxed_value.cc',
+        'update_manager/chromeos_policy.cc',
+        'update_manager/evaluation_context.cc',
+        'update_manager/event_loop.cc',
+        'update_manager/policy.cc',
+        'update_manager/real_config_provider.cc',
+        'update_manager/real_device_policy_provider.cc',
+        'update_manager/real_random_provider.cc',
+        'update_manager/real_shill_provider.cc',
+        'update_manager/real_system_provider.cc',
+        'update_manager/real_time_provider.cc',
+        'update_manager/real_updater_provider.cc',
+        'update_manager/state_factory.cc',
+        'update_manager/update_manager.cc',
+        'utils.cc',
+      ],
+    },
+    # update_engine daemon.
+    {
+      'target_name': 'update_engine',
+      'type': 'executable',
+      'dependencies': ['libupdate_engine'],
+      'sources': [
+        'main.cc',
+      ]
+    },
+    # update_engine console client.
+    {
+      'target_name': 'update_engine_client',
+      'type': 'executable',
+      'dependencies': ['libupdate_engine'],
+      'sources': [
+        'update_engine_client.cc',
+      ]
+    },
+    # server-side delta generator.
+    {
+      'target_name': 'delta_generator',
+      'type': 'executable',
+      'dependencies': ['libupdate_engine'],
+      'link_settings': {
+        'ldflags!': [
+          '-pie',
+        ],
+      },
+      'sources': [
+        'payload_generator/generate_delta_main.cc',
+      ]
+    },
+  ],
+  'conditions': [
+    ['USE_test == 1', {
+      'targets': [
+        # Public keys used for unit testing.
+        {
+          'target_name': 'update_engine-testkeys',
+          'type': 'none',
+          'variables': {
+            'openssl_pem_in_dir': '.',
+            'openssl_pem_out_dir': 'include/update_engine',
+          },
+          'sources': [
+            'unittest_key.pem',
+            'unittest_key2.pem',
+          ],
+          'includes': ['../common-mk/openssl_pem.gypi'],
+        },
+        # Test HTTP Server.
+        {
+          'target_name': 'test_http_server',
+          'type': 'executable',
+          'dependencies': ['libupdate_engine'],
+          'sources': [
+            'test_http_server.cc',
+          ]
+        },
+        # Main unittest file.
+        {
+          'target_name': 'update_engine_unittests',
+          'type': 'executable',
+          'dependencies': ['libupdate_engine'],
+          'includes': ['../common-mk/common_test.gypi'],
+          'defines': [
+            'SYSROOT="<(sysroot)"',
+          ],
+          'sources': [
+            'action_pipe_unittest.cc',
+            'action_processor_unittest.cc',
+            'action_unittest.cc',
+            'bzip_extent_writer_unittest.cc',
+            'certificate_checker_unittest.cc',
+            'chrome_browser_proxy_resolver_unittest.cc',
+            'connection_manager_unittest.cc',
+            'delta_performer_unittest.cc',
+            'download_action_unittest.cc',
+            'extent_ranges_unittest.cc',
+            'extent_writer_unittest.cc',
+            'fake_prefs.cc',
+            'fake_system_state.cc',
+            'file_writer_unittest.cc',
+            'filesystem_copier_action_unittest.cc',
+            'http_fetcher_unittest.cc',
+            'hwid_override_unittest.cc',
+            'mock_http_fetcher.cc',
+            'omaha_hash_calculator_unittest.cc',
+            'omaha_request_action_unittest.cc',
+            'omaha_request_params_unittest.cc',
+            'omaha_response_handler_action_unittest.cc',
+            'p2p_manager_unittest.cc',
+            'payload_generator/cycle_breaker_unittest.cc',
+            'payload_generator/delta_diff_generator_unittest.cc',
+            'payload_generator/extent_mapper_unittest.cc',
+            'payload_generator/filesystem_iterator_unittest.cc',
+            'payload_generator/full_update_generator_unittest.cc',
+            'payload_generator/graph_utils_unittest.cc',
+            'payload_generator/metadata_unittest.cc',
+            'payload_generator/tarjan_unittest.cc',
+            'payload_generator/topological_sort_unittest.cc',
+            'payload_signer_unittest.cc',
+            'payload_state_unittest.cc',
+            'postinstall_runner_action_unittest.cc',
+            'prefs_unittest.cc',
+            'simple_key_value_store_unittest.cc',
+            'subprocess_unittest.cc',
+            'terminator_unittest.cc',
+            'test_utils.cc',
+            'update_attempter_unittest.cc',
+            'update_check_scheduler_unittest.cc',
+            'update_manager/boxed_value_unittest.cc',
+            'update_manager/chromeos_policy_unittest.cc',
+            'update_manager/evaluation_context_unittest.cc',
+            'update_manager/event_loop_unittest.cc',
+            'update_manager/generic_variables_unittest.cc',
+            'update_manager/prng_unittest.cc',
+            'update_manager/real_config_provider_unittest.cc',
+            'update_manager/real_device_policy_provider_unittest.cc',
+            'update_manager/real_random_provider_unittest.cc',
+            'update_manager/real_shill_provider_unittest.cc',
+            'update_manager/real_system_provider_unittest.cc',
+            'update_manager/real_time_provider_unittest.cc',
+            'update_manager/real_updater_provider_unittest.cc',
+            'update_manager/umtest_utils.cc',
+            'update_manager/update_manager_unittest.cc',
+            'update_manager/variable_unittest.cc',
+            'utils_unittest.cc',
+            'zip_unittest.cc',
+            # Main entry point for runnning tests.
+            'testrunner.cc',
+          ],
+        },
+      ],
+    }],
+  ],
+}
diff --git a/utils.cc b/utils.cc
index f78861a..1235a6b 100644
--- a/utils.cc
+++ b/utils.cc
@@ -669,6 +669,27 @@
   return true;
 }
 
+string GetPathOnBoard(const string& command) {
+  int return_code = 0;
+  string command_path;
+  // TODO(deymo): prepend SYSROOT to each PATH instead of the result.
+  if (!Subprocess::SynchronousExec(
+      {"which", command}, &return_code, &command_path)) {
+    return command;
+  }
+  if (return_code != 0)
+    return command;
+
+  base::TrimWhitespaceASCII(command_path, base::TRIM_ALL, &command_path);
+  const char* env_sysroot = getenv("SYSROOT");
+  if (env_sysroot) {
+    string sysroot_command_path = env_sysroot + command_path;
+    if (utils::FileExists(sysroot_command_path.c_str()))
+      return sysroot_command_path;
+  }
+  return command_path;
+}
+
 // Tries to parse the header of an ELF file to obtain a human-readable
 // description of it on the |output| string.
 static bool GetFileFormatELF(const char* buffer, size_t size, string* output) {
diff --git a/utils.h b/utils.h
index b99d808..045190d 100644
--- a/utils.h
+++ b/utils.h
@@ -193,6 +193,10 @@
                              int* out_block_count,
                              int* out_block_size);
 
+// Returns the path of the passed |command| on the board. This uses the
+// environment variable SYSROOT to determine the path to the command on the
+// board instead of the path on the running environment.
+std::string GetPathOnBoard(const std::string& command);
 
 // Returns a human-readable string with the file format based on magic constants
 // on the header of the file.