chromium/chromecast/base/BUILD.gn

# Copyright 2015 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/util/process_version.gni")
import("//chromecast/chromecast.gni")
import("//testing/test.gni")

if (is_android) {
  import("//build/config/android/rules.gni")
  import("//third_party/jni_zero/jni_zero.gni")
}

declare_args() {
  # Denotes the type of Cast product. This is #defined as CAST_PRODUCT_TYPE in
  # version.h. See //third_party/metrics_proto/cast_logs.proto for valid values.
  if (is_android_arc) {
    cast_product_type = 7  # CAST_PRODUCT_TYPE_CHROME_OS
  } else if (is_android) {
    cast_product_type = 4  # CAST_PRODUCT_TYPE_ANDROID_TV
  } else if (is_fuchsia) {
    cast_product_type = 8  # CAST_PRODUCT_TYPE_FUCHSIA_OS
  } else {
    cast_product_type = 0  # CAST_PRODUCT_TYPE_UNKNOWN
  }
}

if (is_android_arc) {
  assert(cast_product_type == 7)
} else if (is_android) {
  assert(cast_product_type == 4 || cast_product_type == 6)
} else {
  assert(cast_product_type >= 0 && cast_product_type <= 8 &&
         cast_product_type != 4 && cast_product_type != 6 &&
         cast_product_type != 7)
}

cast_source_set("base") {
  sources = [
    "alarm_manager.cc",
    "alarm_manager.h",
    "bind_to_task_runner.h",
    "bitstream_audio_codecs.cc",
    "bitstream_audio_codecs.h",
    "cast_constants.cc",
    "cast_constants.h",
    "cast_features.cc",
    "cast_features.h",
    "cast_paths.cc",
    "cast_paths.h",
    "cast_resource.cc",
    "cast_resource.h",
    "device_capabilities.h",
    "device_capabilities_impl.cc",
    "device_capabilities_impl.h",
    "error_codes.cc",
    "error_codes.h",
    "hash_util.cc",
    "hash_util.h",
    "legacy_app_id_mapper.h",
    "observer.h",
    "path_utils.cc",
    "path_utils.h",
    "pref_names.cc",
    "pref_names.h",
    "process_types.h",
    "process_utils.cc",
    "process_utils.h",
    "statistics/weighted_mean.cc",
    "statistics/weighted_mean.h",
    "statistics/weighted_moving_average.cc",
    "statistics/weighted_moving_average.h",
    "statistics/weighted_moving_linear_regression.cc",
    "statistics/weighted_moving_linear_regression.h",
    "system_time_change_notifier.cc",
    "system_time_change_notifier.h",
    "task_runner_impl.cc",
    "task_runner_impl.h",
  ]

  public_deps = [
    ":chromecast_switches",
    ":init_shlib",
    "//base",
    "//chromecast/base/metrics",
    "//chromecast/public",
  ]

  deps = [
    ":cast_version",
    "//chromecast:chromecast_buildflags",
    "//third_party/widevine/cdm:buildflags",
  ]

  if (chromecast_branding == "public") {
    sources += [
      "cast_features_internal.cc",
      "legacy_app_id_mapper_dummy.cc",
    ]
  } else {
    sources += [ "//chromecast/internal/base/legacy_app_id_mapper_impl.cc" ]
    deps += [ "//chromecast/internal/base:cast_internal_features" ]
  }

  if (is_android) {
    sources += [
      "android/dumpstate_writer.cc",
      "android/dumpstate_writer.h",
      "android/system_time_change_notifier_android.cc",
      "android/system_time_change_notifier_android.h",
    ]
    deps += [ ":jni_headers" ]
  }
}

# chromecast related switch values
# b/30398176 components that depend on switch values should depend on
# this target directly instead of depend on //chromecast/base to avoid
# unnecessary size increase.
cast_source_set("chromecast_switches") {
  sources = [
    "chromecast_switches.cc",
    "chromecast_switches.h",
  ]

  deps = [ "//base" ]
}

# command line initialization code for shared libs
# b/30398176 shared libs that depend on InitCommandLineShlib() only should
# use this target instead of //chromecast/base
cast_source_set("init_shlib") {
  sources = [
    "init_command_line_shlib.cc",
    "init_command_line_shlib.h",
  ]

  deps = [
    "//base",
    "//chromecast:chromecast_buildflags",
  ]
}

cast_source_set("test_support") {
  sources = [
    "scoped_temp_file.cc",
    "scoped_temp_file.h",
  ]

  public_deps = [
    ":base",
    "//base",
  ]
}

test("cast_base_unittests") {
  sources = [
    "alarm_manager_unittest.cc",
    "cast_features_unittest.cc",
    "device_capabilities_impl_unittest.cc",
    "error_codes_unittest.cc",
    "hash_util_test.cc",
    "metrics/cast_metrics_helper_unittest.cc",
    "observer_unittest.cc",
    "path_utils_unittest.cc",
    "process_utils_unittest.cc",
    "statistics/weighted_moving_average_unittest.cc",
    "statistics/weighted_moving_linear_regression_unittest.cc",
    "system_time_change_notifier_unittest.cc",
    "thread_health_checker_unittest.cc",
  ]

  deps = [
    ":test_support",
    ":thread_health_checker",
    "//base/test:run_all_unittests",
    "//base/test:test_support",
    "//testing/gmock",
    "//testing/gtest",
  ]
}

# Declares CreateSysInfo() without defining the implementation (see below).
cast_source_set("cast_sys_info_util") {
  public = [ "cast_sys_info_util.h" ]
}

# Defines a CastSysInfoDummy suitable for use in tests, and public builds.
cast_source_set("dummy_cast_sys_info") {
  sources = [
    "cast_sys_info_dummy.cc",
    "cast_sys_info_dummy.h",
  ]
  deps = [
    "//base",
    "//chromecast/public",
  ]
}

# Links a CreateSysInfo() implementation that returns CastSysInfoDummy.
cast_source_set("dummy_create_sys_info") {
  sources = [ "cast_sys_info_util_simple.cc" ]
  deps = [ ":dummy_cast_sys_info" ]
  public_deps = [ ":cast_sys_info_util" ]
}

# Links the default CreateSysInfo() implementation for Android.
if (is_android) {
  cast_source_set("android_create_sys_info") {
    deps = [
      ":cast_version",
      ":jni_headers",
      "//base",
      "//chromecast/browser/android:jni_headers",
      "//chromecast/public",
    ]
    public_deps = [ ":cast_sys_info_util" ]
    sources = [
      "cast_sys_info_android.cc",
      "cast_sys_info_android.h",
      "cast_sys_info_android_factory.cc",
    ]
  }
} else {  # !is_android
  # Target for OEM partners to override sys_info shared library, i.e.
  # libcast_sys_info_1.0.so.
  cast_shared_library("libcast_sys_info_1.0") {
    sources = [ "cast_sys_info_shlib.cc" ]

    deps = [
      ":cast_sys_info_util",
      ":chromecast_switches",
      ":dummy_cast_sys_info",
      ":init_shlib",
      "//base",
      "//chromecast/public",
    ]
  }

  # Links a CreateSysInfo() that loads libcast_sys_info_1.0.so.
  cast_source_set("shlib_create_sys_info") {
    sources = [ "cast_sys_info_util_shlib.cc" ]

    deps = [
      ":libcast_sys_info_1.0",
      "//base",
      "//chromecast/public",
    ]

    public_deps = [ ":cast_sys_info_util" ]
  }
}  # is_android

# Links the default CreateSysInfo() for this build configuration.
group("default_create_sys_info") {
  if (is_android) {
    public_deps = [ ":android_create_sys_info" ]
  } else if (chromecast_branding == "public") {
    public_deps = [ ":dummy_create_sys_info" ]
  } else {
    public_deps = [ ":shlib_create_sys_info" ]
  }
}

cast_source_set("thread_health_checker") {
  sources = [
    "thread_health_checker.cc",
    "thread_health_checker.h",
  ]
  deps = [
    ":base",
    "//base",
  ]
}

process_version("cast_version") {
  template_file = "version.h.in"
  output = "$target_gen_dir/version.h"

  sources = [ "//chrome/VERSION" ]
  extra_args = [
    "-e",
    "VERSION_FULL=\"%s.%s.%s.%s\"%(MAJOR,MINOR,BUILD,PATCH)",
    "-e",
    "CAST_BUILD_RELEASE=\"$cast_build_release\"",
    "-e",
    "CAST_BUILD_INCREMENTAL=\"$cast_build_incremental\"",
    "-e",
    "CAST_PRODUCT_TYPE=$cast_product_type",
  ]

  if (cast_is_debug) {
    extra_args += [
      "-e",
      "CAST_IS_DEBUG_BUILD=1",
    ]
  } else {
    extra_args += [
      "-e",
      "CAST_IS_DEBUG_BUILD=0",
    ]
  }
}

if (is_android) {
  generate_jni("jni_headers") {
    sources = [
      "java/src/org/chromium/chromecast/base/CastSysInfoAndroid.java",
      "java/src/org/chromium/chromecast/base/DumpstateWriter.java",
      "java/src/org/chromium/chromecast/base/SystemTimeChangeNotifierAndroid.java",
    ]
  }

  android_library("base_java") {
    java_src_dir = "//chromecast/base/java/src"
    srcjar_deps = [ ":jni_headers" ]
    sources = [
      "$java_src_dir/org/chromium/chromecast/base/Both.java",
      "$java_src_dir/org/chromium/chromecast/base/Box.java",
      "$java_src_dir/org/chromium/chromecast/base/Cell.java",
      "$java_src_dir/org/chromium/chromecast/base/Controller.java",
      "$java_src_dir/org/chromium/chromecast/base/Observable.java",
      "$java_src_dir/org/chromium/chromecast/base/Observer.java",
      "$java_src_dir/org/chromium/chromecast/base/OwnedScope.java",
      "$java_src_dir/org/chromium/chromecast/base/Scope.java",
      "$java_src_dir/org/chromium/chromecast/base/Sequencer.java",
      "$java_src_dir/org/chromium/chromecast/base/Unit.java",

      # TODO(sanfin): Move these files to another target.
      "$java_src_dir/org/chromium/chromecast/base/CastSerialGenerator.java",
      "$java_src_dir/org/chromium/chromecast/base/CastSettingsManager.java",
      "$java_src_dir/org/chromium/chromecast/base/CastSwitches.java",
      "$java_src_dir/org/chromium/chromecast/base/CastSysInfoAndroid.java",
      "$java_src_dir/org/chromium/chromecast/base/DumpstateWriter.java",
      "$java_src_dir/org/chromium/chromecast/base/SystemTimeChangeNotifierAndroid.java",
    ]

    deps = [
      "//base:base_java",
      "//build/android:build_java",
      "//third_party/androidx:androidx_annotation_annotation_java",
      "//third_party/androidx:androidx_core_core_java",
      "//third_party/jni_zero:jni_zero_java",
    ]
  }

  android_library("cast_base_test_utils_java") {
    java_test_dir = "//chromecast/base/java/test"
    testonly = true
    sources = [
      "$java_test_dir/org/chromium/chromecast/base/Inheritance.java",
      "$java_test_dir/org/chromium/chromecast/base/ReactiveRecorder.java",
    ]
    deps = [
      ":base_java",
      "//third_party/hamcrest:hamcrest_java",
      "//third_party/junit",
    ]
  }

  robolectric_binary("cast_base_junit_tests") {
    java_test_dir = "//chromecast/base/java/test"
    sources = [
      "$java_test_dir/org/chromium/chromecast/base/BothTest.java",
      "$java_test_dir/org/chromium/chromecast/base/BoxTest.java",
      "$java_test_dir/org/chromium/chromecast/base/CastSettingsManagerTest.java",
      "$java_test_dir/org/chromium/chromecast/base/CellTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ControllerTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ObservableAfterTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ObservableAlarmTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ObservableAndTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ObservableAndThenTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ObservableDebugTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ObservableDelayTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ObservableDistinctUntilChangedTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ObservableFilterTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ObservableFlatMapTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ObservableFoldTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ObservableMapTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ObservableMiscellaneousTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ObservableNotTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ObservableOpaqueTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ObservableOrTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ObserverTest.java",
      "$java_test_dir/org/chromium/chromecast/base/OwnedScopeTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ReactiveRecorderTest.java",
      "$java_test_dir/org/chromium/chromecast/base/ScopeTest.java",
      "$java_test_dir/org/chromium/chromecast/base/SequencerTest.java",
      "$java_test_dir/org/chromium/chromecast/base/UnitTest.java",
    ]
    deps = [
      ":base_java",
      ":cast_base_test_utils_java",
      "//base:base_java",
      "//base:base_java_test_support",
      "//base:base_junit_test_support",
      "//build/android:build_java",
      "//third_party/hamcrest:hamcrest_java",
      "//third_party/jni_zero:jni_zero_java",
      "//third_party/mockito:mockito_java",
    ]
  }
}