chromium/ui/qt/BUILD.gn

# Copyright 2022 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/config/cast.gni")
import("//build/config/linux/pkg_config.gni")
import("//build/config/sysroot.gni")
import("//printing/buildflags/buildflags.gni")
import("//ui/qt/qt.gni")

assert(use_qt)
assert(is_linux)
assert(!is_castos)

config("qt_internal_config") {
  if (is_clang) {
    # libstdc++ headers are incompatible with -fcomplete-member-pointers.
    cflags = [ "-fno-complete-member-pointers" ]
  }

  # It's OK to depend on the system libstdc++ since it's a dependency of QT, so
  # it will get loaded into the process anyway.
  libs = [ "stdc++" ]

  configs = [
    "//build/config/linux:runtime_library",
    "//build/config/posix:runtime_library",
  ]
}

source_set("qt_interface") {
  visibility = [ ":*" ]

  configs -= [ "//build/config/compiler:runtime_library" ]
  configs += [ ":qt_internal_config" ]

  # Since `:qt` depends on `qt_shim` via data_deps, gn check would error-out
  # if qt_interface.h was placed in `qt_shim`, so it's placed in a separate
  # target instead.
  public = [ "qt_interface.h" ]
  sources = [ "qt_interface.cc" ]
}

template("qt_shim") {
  if (!use_sysroot) {
    action("generate_moc" + invoker.qt_version) {
      script = "moc_wrapper.py"
      inputs = [ "//ui/qt/qt_shim.h" ]
      outputs = [ "$root_gen_dir/qt" + invoker.qt_version + "/qt_shim_moc.cc" ]
      args = rebase_path(inputs + outputs, root_build_dir)
      if (invoker.moc_qt_path != "") {
        args += [
          "--path",
          invoker.moc_qt_path,
        ]
      }
    }
  }

  pkg_config("qt" + invoker.qt_version + "_config") {
    packages = [
      "Qt" + invoker.qt_version + "Core",
      "Qt" + invoker.qt_version + "Widgets",
    ]
  }

  shared_library(target_name) {
    visibility = [
      ":qt",
      "//chrome/installer/linux:*",
    ]

    # Since qt_shim is a shared library even in non-component builds, it shouldn't
    # depend on any other targets since that would duplicate code between binaries
    # leading to increased size and potential issues from duplicated global state.
    no_default_deps = true
    assert_no_deps = [
      "//base",
      "//buildtools/third_party/libc++",
    ]
    deps = [ ":qt_interface" ]

    configs -= [ "//build/config/compiler:runtime_library" ]
    configs += [
      ":qt_internal_config",
      ":qt" + invoker.qt_version + "_config",
    ]

    public = []
    sources = [
      "qt_shim.cc",
      "qt_shim.h",
    ]
    if (use_sysroot) {
      # This file is generated with gen_qt_shim_moc.sh on an amd64 system to
      # avoid a build-time dependency on `moc` when using the sysroot.
      sources += [ "qt" + invoker.qt_version + "_shim_moc.cc" ]
    } else {
      sources += get_target_outputs(":generate_moc" + invoker.qt_version)
      deps += [ ":generate_moc" + invoker.qt_version ]
    }
  }
}
qt_shim("qt5_shim") {
  qt_version = "5"
  if (!use_sysroot) {
    moc_qt_path = "$moc_qt5_path"
  }
}
if (use_qt6) {
  qt_shim("qt6_shim") {
    qt_version = "6"
    if (!use_sysroot) {
      moc_qt_path = "$moc_qt6_path"
    }
  }
}

component("qt") {
  visibility = [ "//ui/linux:linux_ui_factory" ]

  defines = [ "IS_QT_IMPL" ]

  # qt_shim is in data_deps since we want to load it manually.
  data_deps = [ ":qt5_shim" ]
  if (use_qt6) {
    data_deps += [ ":qt6_shim" ]
  }
  deps = [
    ":qt_interface",
    "//base",
    "//printing/buildflags",
    "//ui/base/ime/linux",
    "//ui/color",
    "//ui/color:mixers",
    "//ui/gfx",
    "//ui/linux:linux_ui",
    "//ui/native_theme",
    "//ui/shell_dialogs",
    "//ui/views",
  ]
  public_deps = [ "//skia" ]
  if (enable_printing) {
    public_deps += [ "//printing" ]
  }

  sources = [
    "qt_ui.cc",
    "qt_ui.h",
  ]
}