Add src/printing to the GN build.

BUG=
[email protected]

Review URL: https://codereview.chromium.org/311603003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@274865 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/BUILD.gn b/BUILD.gn
index 384834a..29013c3 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -48,6 +48,7 @@
     "//mojo",
     "//net",
     "//ppapi:ppapi_c",
+    "//printing",
     #"//sandbox",
     "//sdch",
     "//skia",
diff --git a/printing/BUILD.gn b/printing/BUILD.gn
new file mode 100644
index 0000000..ac07ec9
--- /dev/null
+++ b/printing/BUILD.gn
@@ -0,0 +1,257 @@
+# Copyright 2014 The Chromium 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("//build/config/ui.gni")
+if (is_mac) {
+  import("//build/config/mac/mac_sdk.gni")
+}
+if (is_android) {
+  import("//build/config/android/rules.gni")
+}
+
+use_cups = ((is_linux && !is_chromeos) || is_mac)
+
+component("printing") {
+  sources = [
+    "backend/print_backend.cc",
+    "backend/print_backend.h",
+    "backend/print_backend_consts.cc",
+    "backend/print_backend_consts.h",
+    "backend/print_backend_dummy.cc",
+    "backend/printing_info_win.cc",
+    "backend/printing_info_win.h",
+    "emf_win.cc",
+    "emf_win.h",
+    "image.cc",
+    "image.h",
+    "image_android.cc",
+    "image_linux.cc",
+    "image_mac.cc",
+    "image_win.cc",
+    "metafile.h",
+    "metafile_impl.h",
+    "metafile_skia_wrapper.cc",
+    "metafile_skia_wrapper.h",
+    "page_number.cc",
+    "page_number.h",
+    "page_range.cc",
+    "page_range.h",
+    "page_setup.cc",
+    "page_setup.h",
+    "page_size_margins.cc",
+    "page_size_margins.h",
+    "pdf_metafile_cg_mac.cc",
+    "pdf_metafile_cg_mac.h",
+    "pdf_metafile_skia.cc",
+    "pdf_metafile_skia.h",
+    "print_destination_interface.h",
+    "print_destination_none.cc",
+    "print_dialog_gtk_interface.h",
+    "print_job_constants.cc",
+    "print_job_constants.h",
+    "print_settings.cc",
+    "print_settings.h",
+    "print_settings_initializer.cc",
+    "print_settings_initializer.h",
+    "print_settings_initializer_mac.cc",
+    "print_settings_initializer_mac.h",
+    "print_settings_initializer_win.cc",
+    "print_settings_initializer_win.h",
+    "printed_document.cc",
+    "printed_document.h",
+    "printed_document_linux.cc",
+    "printed_document_mac.cc",
+    "printed_document_win.cc",
+    "printed_page.cc",
+    "printed_page.h",
+    "printed_pages_source.h",
+    "printing_context.cc",
+    "printing_context.h",
+    "printing_utils.cc",
+    "printing_utils.h",
+    "units.cc",
+    "units.h",
+  ]
+
+  cflags = []
+  defines = [ "PRINTING_IMPLEMENTATION" ]
+
+  deps = [
+    "//base",
+    "//base:i18n",
+    "//base/third_party/dynamic_annotations",
+    "//skia",
+    "//third_party/icu",
+    "//ui/gfx",
+    "//ui/gfx/geometry",
+    "//url",
+  ]
+
+  if (use_aura) {
+    # deps += [ "//ui/aura" ]  TODO(GYP)
+  }
+
+  if (is_mac) {
+    # Mac-Aura does not support printing.
+    if (use_aura) {
+      sources -= [
+        "printed_document_mac.cc",
+      ]
+    } else {
+      sources += [
+        "printing_context_mac.mm",
+        "printing_context_mac.h",
+      ]
+    }
+  }
+
+  if (is_win) {
+    # PRINT_BACKEND_AVAILABLE disables the default dummy implementation of the
+    # print backend and enables a custom implementation instead.
+    defines += [ "PRINT_BACKEND_AVAILABLE" ]
+    sources += [
+      "backend/win_helper.cc",
+      "backend/win_helper.h",
+      "backend/print_backend_win.cc",
+      "printing_context_win.cc",
+      "printing_context_win.h",
+    ]
+  }
+
+  if (is_chromeos) {
+    sources += [
+      "printing_context_no_system_dialog.cc",
+      "printing_context_no_system_dialog.h",
+    ]
+  }
+
+  if (use_cups) {
+    cups_version = exec_script("cups_config_helper.py", [ "--api-version" ],
+                               "trim string")
+
+    configs += [ ":cups" ]
+
+    if (is_linux) {
+      if (cups_version == "1.6" || cups_version == "1.7") {
+        cflags += [
+          # CUPS 1.6 deprecated the PPD APIs, but we will stay with this
+          # API for now as supported Linux and Mac OS'es are still using
+          # older versions of CUPS. More info: crbug.com/226176
+          "-Wno-deprecated-declarations",
+          # CUPS 1.7 deprecates httpConnectEncrypt(), see the mac section
+          # below.
+        ]
+      }
+    }
+
+    if (is_mac && mac_sdk_version == "10.9") {
+      # The 10.9 SDK includes cups 1.7, which deprecates
+      # httpConnectEncrypt() in favor of httpConnect2(). hhttpConnect2()
+      # is new in 1.7, so it doesn't exist on OS X 10.6-10.8 and we
+      # can't use it until 10.9 is our minimum system version.
+      # (cups_version isn't reliable on OS X, so key the check off of
+      # mac_sdk).
+      cflags += [ "-Wno-deprecated-declarations" ]
+    }
+
+    # PRINT_BACKEND_AVAILABLE disables the default dummy implementation
+    # of the print backend and enables a custom implementation instead.
+    defines += [ "PRINT_BACKEND_AVAILABLE" ]
+
+    sources += [
+      "backend/cups_helper.cc",
+      "backend/cups_helper.h",
+      "backend/print_backend_cups.cc",
+    ]
+  }
+
+  if (is_chromeos) {
+    # PRINT_BACKEND_AVAILABLE disables the default dummy implementation
+    # of the print backend and enables a custom implementation instead.
+    defines += [ "PRINT_BACKEND_AVAILABLE" ]
+
+    sources += [
+      "backend/print_backend_chromeos.cc",
+    ]
+  } else if (is_linux) {  # Non-ChromeOS Linux.
+    sources += [
+      "printing_context_linux.cc",
+      "printing_context_linux.h",
+    ]
+  }
+
+  if (is_android) {
+    sources += [
+      "printing_context_android.cc",
+      "printing_context_android.h",
+    ]
+
+    deps += [ ":printing_jni_headers" ]
+  }
+}
+
+test("printing_unittests") {
+  sources = [
+    "emf_win_unittest.cc",
+    "page_number_unittest.cc",
+    "page_range_unittest.cc",
+    "page_setup_unittest.cc",
+    "pdf_metafile_cg_mac_unittest.cc",
+    "printed_page_unittest.cc",
+    "printing_context_win_unittest.cc",
+    "printing_test.h",
+    "printing_utils_unittest.cc",
+    "units_unittest.cc",
+  ]
+
+  if (use_cups) {
+    configs += [ ":cups" ]
+    sources += [ "backend/cups_helper_unittest.cc" ]
+  }
+
+  deps = [
+    ":printing",
+    "//base/allocator",
+    "//base/test:run_all_unittests",
+    "//base/test:test_support",
+    "//testing/gtest",
+    "//ui/base",
+    "//ui/gfx",
+    "//ui/gfx/geometry",
+  ]
+}
+
+if (use_cups) {
+  config("cups") {
+    defines = [ "USE_CUPS" ]
+
+    if (is_mac) {
+      libs = [ "libcups.dylib" ]
+    } else {
+      libs = exec_script("cups_config_helper.py", [ "--libs-for-gn" ], "value")
+    }
+  }
+}
+
+if (is_android) {
+  generate_jni("printing_jni_headers") {
+    sources = [
+      "android/java/src/org/chromium/printing/PrintingContext.java",
+    ]
+    jni_package = "printing"
+  }
+
+  # TODO(GYP)
+  #{
+  #  'target_name': 'printing_java',
+  #  'type': 'none',
+  #  'variables': {
+  #    'java_in_dir': '../printing/android/java',
+  #  },
+  #  'dependencies': [
+  #    '../base/base.gyp:base_java',
+  #  ],
+  #  'includes': [ '../build/java.gypi'  ],
+  #}
+}
diff --git a/printing/cups_config_helper.py b/printing/cups_config_helper.py
index 976054f..f3c22fa 100755
--- a/printing/cups_config_helper.py
+++ b/printing/cups_config_helper.py
@@ -11,7 +11,8 @@
 that use cups directly, while libs are only needed on the final link
 line.
 
-TODO(evan): remove me once
+This can be dramatically simplified or maybe removed (depending on GN
+requirements) when this is fixed:
   https://bugs.launchpad.net/ubuntu/+source/cupsys/+bug/163704
 is fixed.
 """
@@ -20,7 +21,8 @@
 import sys
 
 def usage():
-  print 'usage: %s {--cflags|--ldflags|--libs}' % sys.argv[0]
+  print 'usage: %s {--api-version|--cflags|--ldflags|--libs|--libs-for-gn}' % \
+      sys.argv[0]
 
 
 def run_cups_config(mode):
@@ -60,11 +62,33 @@
     return 1
 
   mode = sys.argv[1]
-  if mode not in ('--cflags', '--libs', '--ldflags'):
+  if mode == '--api-version':
+    subprocess.call(['cups-config', '--api-version'])
+    return 0
+
+  # All other modes get the flags.
+  if mode not in ('--cflags', '--libs', '--libs-for-gn', '--ldflags'):
     usage()
     return 1
+
+  if mode == '--libs-for-gn':
+    gn_libs_output = True
+    mode = '--libs'
+  else:
+    gn_libs_output = False
+
   flags = run_cups_config(mode)
-  print ' '.join(flags)
+
+  if gn_libs_output:
+    # Strip "-l" from beginning of libs, quote, and surround in [ ].
+    print '['
+    for lib in flags:
+      if lib[:2] == "-l":
+        print '"%s", ' % lib[2:]
+    print ']'
+  else:
+    print ' '.join(flags)
+
   return 0