Commit 44345aa9 authored by rbpotter's avatar rbpotter Committed by Commit Bot

Print Preview: Autogenerate grd for non-optimized builds

Bug: 1132403
Change-Id: Ib476e0a84de6e441f82c35029b2952d45a093993
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2427309
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Reviewed-by: default avatardpapad <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#810905}
parent 22e4c40d
......@@ -574,15 +574,20 @@ if (enable_print_preview) {
"root_gen_dir=" + rebase_path(root_gen_dir, root_build_dir),
]
defines = chrome_grit_defines
if (optimize_webui) {
source = "print_preview/print_preview_resources_vulcanized.grd"
deps = [ "//chrome/browser/resources/print_preview:build" ]
} else {
source = "print_preview/print_preview_resources.grd"
deps = [ "//chrome/browser/resources/print_preview/ui:web_components" ]
# These arguments are needed since the grd is generated at build time.
enable_input_discovery_for_gn_analyze = false
defines += [ "SHARED_INTERMEDIATE_DIR=" +
rebase_path(root_gen_dir, root_build_dir) ]
print_gen_dir = "$root_gen_dir/chrome/browser/resources/print_preview"
source = "$print_gen_dir/print_preview_resources.grd"
deps = [ "//chrome/browser/resources/print_preview:build_grd" ]
}
defines = chrome_grit_defines
outputs = [
"grit/print_preview_resources.h",
"grit/print_preview_resources_map.cc",
......
......@@ -5,11 +5,14 @@
import("//chrome/common/features.gni")
import("//third_party/closure_compiler/compile_js.gni")
import("//tools/grit/preprocess_grit.gni")
import("//ui/webui/resources/tools/generate_grd.gni")
import("../optimize_webui.gni")
if (optimize_webui) {
preprocess_folder = "preprocessed"
preprocess_folder = "preprocessed"
preprocess_manifest = "preprocessed_manifest.json"
preprocess_gen_manifest = "preprocessed_gen_manifest.json"
if (optimize_webui) {
optimize_webui("build") {
host = "print"
input = rebase_path("$target_gen_dir/$preprocess_folder", root_build_dir)
......@@ -26,104 +29,120 @@ if (optimize_webui) {
"pdf/pdf_scripting_api.js",
]
}
preprocess_grit("preprocess") {
in_folder = "./"
out_folder = "$target_gen_dir/$preprocess_folder"
in_files = [
"cloud_print_interface.js",
"cloud_print_interface_impl.js",
"dark_mode_behavior.js",
"metrics.js",
"native_layer.js",
"print_preview.js",
"print_preview_utils.js",
"data/cloud_parsers.js",
"data/coordinate2d.js",
"data/destination.js",
"data/destination_match.js",
"data/destination_policies.js",
"data/destination_store.js",
"data/document_info.js",
"data/invitation.js",
"data/invitation_store.js",
"data/local_parsers.js",
"data/margins.js",
"data/measurement_system.js",
"data/model.js",
"data/printable_area.js",
"data/scaling.js",
"data/size.js",
"data/state.js",
"data/user_manager.js",
"ui/highlight_utils.js",
"ui/input_behavior.js",
"ui/plugin_proxy.js",
"ui/select_behavior.js",
"ui/settings_behavior.js",
} else {
generate_grd("build_grd") {
deps = [
":preprocess",
":preprocess_generated",
]
manifest_files = [
"$target_gen_dir/$preprocess_manifest",
"$target_gen_dir/$preprocess_gen_manifest",
]
grd_prefix = "print_preview"
out_grd = "$target_gen_dir/print_preview_resources.grd"
}
}
preprocess_grit("preprocess") {
in_folder = "./"
out_folder = "$target_gen_dir/$preprocess_folder"
out_manifest = "$target_gen_dir/$preprocess_manifest"
in_files = [
"print_preview.html",
"cloud_print_interface.js",
"cloud_print_interface_impl.js",
"dark_mode_behavior.js",
"metrics.js",
"native_layer.js",
"print_preview.js",
"print_preview_utils.js",
"data/cloud_parsers.js",
"data/coordinate2d.js",
"data/destination.js",
"data/destination_match.js",
"data/destination_policies.js",
"data/destination_store.js",
"data/document_info.js",
"data/invitation.js",
"data/invitation_store.js",
"data/local_parsers.js",
"data/margins.js",
"data/measurement_system.js",
"data/model.js",
"data/printable_area.js",
"data/scaling.js",
"data/size.js",
"data/state.js",
"data/user_manager.js",
"ui/highlight_utils.js",
"ui/input_behavior.js",
"ui/plugin_proxy.js",
"ui/select_behavior.js",
"ui/settings_behavior.js",
]
if (is_chromeos) {
in_files += [ "data/printer_status_cros.js" ]
}
if (is_chromeos) {
in_files += [ "data/printer_status_cros.js" ]
}
}
preprocess_grit("preprocess_generated") {
deps = [ "ui:web_components" ]
in_folder = target_gen_dir
out_folder = "$target_gen_dir/$preprocess_folder"
in_files = [
"ui/advanced_options_settings.js",
"ui/advanced_settings_dialog.js",
"ui/advanced_settings_item.js",
"ui/app.js",
"ui/button_strip.js",
"ui/color_settings.js",
"ui/copies_settings.js",
"ui/destination_dialog.js",
"ui/destination_list.js",
"ui/destination_list_item.js",
"ui/destination_select_css.js",
"ui/destination_settings.js",
"ui/dpi_settings.js",
"ui/duplex_settings.js",
"ui/header.js",
"ui/icons.js",
"ui/layout_settings.js",
"ui/margin_control.js",
"ui/margin_control_container.js",
"ui/margins_settings.js",
"ui/media_size_settings.js",
"ui/more_settings.js",
"ui/number_settings_section.js",
"ui/other_options_settings.js",
"ui/pages_per_sheet_settings.js",
"ui/pages_settings.js",
"ui/preview_area.js",
"ui/print_preview_search_box.js",
"ui/print_preview_shared_css.js",
"ui/print_preview_vars_css.js",
"ui/provisional_destination_resolver.js",
"ui/scaling_settings.js",
"ui/settings_section.js",
"ui/settings_select.js",
"ui/sidebar.js",
"ui/throbber_css.js",
]
preprocess_grit("preprocess_generated") {
deps = [ "ui:web_components" ]
in_folder = target_gen_dir
out_folder = "$target_gen_dir/$preprocess_folder"
out_manifest = "$target_gen_dir/$preprocess_gen_manifest"
in_files = [
"ui/advanced_options_settings.js",
"ui/advanced_settings_dialog.js",
"ui/advanced_settings_item.js",
"ui/app.js",
"ui/button_strip.js",
"ui/color_settings.js",
"ui/copies_settings.js",
"ui/destination_dialog.js",
"ui/destination_list.js",
"ui/destination_list_item.js",
"ui/destination_select_css.js",
"ui/destination_settings.js",
"ui/dpi_settings.js",
"ui/duplex_settings.js",
"ui/header.js",
"ui/icons.js",
"ui/layout_settings.js",
"ui/margin_control.js",
"ui/margin_control_container.js",
"ui/margins_settings.js",
"ui/media_size_settings.js",
"ui/more_settings.js",
"ui/number_settings_section.js",
"ui/other_options_settings.js",
"ui/pages_per_sheet_settings.js",
"ui/pages_settings.js",
"ui/preview_area.js",
"ui/print_preview_search_box.js",
"ui/print_preview_shared_css.js",
"ui/print_preview_vars_css.js",
"ui/provisional_destination_resolver.js",
"ui/scaling_settings.js",
"ui/settings_section.js",
"ui/settings_select.js",
"ui/sidebar.js",
"ui/throbber_css.js",
]
if (is_chromeos) {
in_files += [
"ui/destination_dropdown_cros.js",
"ui/destination_select_cros.js",
"ui/pin_settings.js",
"ui/printer_status_icon_cros.js",
]
} else {
in_files += [
"ui/destination_select.js",
"ui/link_container.js",
]
}
if (is_chromeos) {
in_files += [
"ui/destination_dropdown_cros.js",
"ui/destination_select_cros.js",
"ui/pin_settings.js",
"ui/printer_status_icon_cros.js",
]
} else {
in_files += [
"ui/destination_select.js",
"ui/link_container.js",
]
}
}
......
......@@ -90,7 +90,7 @@ const char kBasicPrintShortcut[] = "(Ctrl+Shift+P)";
#if !BUILDFLAG(OPTIMIZE_WEBUI)
constexpr char kGeneratedPath[] =
"@out_folder@/gen/chrome/browser/resources/print_preview/";
"@out_folder@/gen/chrome/browser/resources/print_preview/preprocessed/";
#endif
PrintPreviewUI::TestDelegate* g_test_delegate = nullptr;
......
......@@ -26,5 +26,13 @@ template("preprocess_grit") {
rebase_path(invoker.out_folder, root_build_dir),
"--in-files",
] + invoker.in_files + grit_defines
if (defined(invoker.out_manifest)) {
args += [
"--out-manifest",
rebase_path(invoker.out_manifest, root_build_dir),
]
outputs += [ invoker.out_manifest ]
}
}
}
......@@ -5,6 +5,7 @@
import argparse
import errno
import io
import json
import os
import sys
......@@ -51,6 +52,7 @@ def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument('--in-folder', required=True)
parser.add_argument('--out-folder', required=True)
parser.add_argument('--out-manifest')
parser.add_argument('--in-files', required=True, nargs="*")
parser.add_argument('-D', '--defines', nargs="*", action='append')
parser.add_argument('-E', '--environment')
......@@ -67,6 +69,7 @@ def main(argv):
defines[name] = val
node = PreprocessNode.Construct(defines, args.target)
for input_file in args.in_files:
output = node.ProcessFile(os.path.join(in_folder, input_file))
......@@ -83,6 +86,14 @@ def main(argv):
raise
with io.open(out_path, mode='wb') as f:
f.write(output.encode('utf-8'))
if args.out_manifest:
manifest_data = {}
manifest_data['base_dir'] = '%s' % args.out_folder
manifest_data['files'] = args.in_files
manifest_file = open(
os.path.normpath(os.path.join(_CWD, args.out_manifest)), 'wb')
json.dump(manifest_data, manifest_file)
return
......
......@@ -171,9 +171,9 @@
"chrome/browser/resources/print_preview/print_preview_resources_vulcanized.grd": {
"includes": [1700],
},
"chrome/browser/resources/print_preview/print_preview_resources.grd": {
"<(SHARED_INTERMEDIATE_DIR)/chrome/browser/resources/print_preview/print_preview_resources.grd": {
"META": {"sizes": {"includes": [500],}},
"includes": [1720],
"structures": [1740],
},
"chrome/browser/resources/print_preview/print_preview_pdf_resources.grd": {
"includes": [1750],
......
......@@ -93,12 +93,27 @@ def _CheckJsModulizer(input_api, output_api):
return results
def _CheckGenerateGrd(input_api, output_api):
affected = input_api.AffectedFiles()
affected_files = [input_api.os_path.basename(f.LocalPath()) for f in affected]
results = []
if 'generate_grd.py' in affected_files:
presubmit_path = input_api.PresubmitLocalPath()
sources = [input_api.os_path.join('tools', 'generate_grd_test.py')]
tests = [input_api.os_path.join(presubmit_path, s) for s in sources]
results += input_api.canned_checks.RunUnitTests(
input_api, output_api, tests)
return results
def _CommonChecks(input_api, output_api):
results = []
results += _CheckForTranslations(input_api, output_api)
results += _CheckSvgsOptimized(input_api, output_api)
results += _CheckWebDevStyle(input_api, output_api)
results += _CheckJsModulizer(input_api, output_api)
results += _CheckGenerateGrd(input_api, output_api)
results += input_api.canned_checks.CheckPatchFormatted(input_api, output_api,
check_js=True)
return results
# Copyright 2020 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.
template("generate_grd") {
action(target_name) {
script = "//ui/webui/resources/tools/generate_grd.py"
if (defined(invoker.deps)) {
deps = invoker.deps
}
inputs = invoker.manifest_files
outputs = [ invoker.out_grd ]
args = [
"--out-grd",
rebase_path(invoker.out_grd, root_build_dir),
"--grd-prefix",
invoker.grd_prefix,
"--root-gen-dir",
rebase_path(root_gen_dir, root_build_dir),
"--manifest-files",
] + rebase_path(invoker.manifest_files, root_build_dir)
}
}
# Copyright 2020 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.
# Generates a grit grd file from a list of input manifest files. This is useful
# for preventing the need to list JS files in multiple locations, as files can
# be listed just once in the BUILD.gn file as inputs for a build rule that knows
# how to output such a manifest (e.g. preprocess_grit).
#
# Variables:
# manifest-files:
# List of paths to manifest files. Each must contain a JSON object with
# 2 fields:
# - base_dir, the base directory where the files are located
# - files, a list of file paths from the base directory
#
# out_grd:
# Path where the generated grd file should be written.
#
# grd_prefix:
# Used to generate both grit IDs for included files and names for the
# header/pak/resource map files specified in the <outputs> section of the
# grd file. For example, prefix "foo" will result in a grd file that has
# as output "foo_resources.pak", "grit/foo_resources.h", etc, and has grit
# IDs prefixed by IDS_FOO.
#
# root_gen_dir:
# Path to the root generated directory. Used to compute the relative path
# from the root generated directory for setting file paths, as grd files
# with generated file paths must specify these paths as
# "${root_gen_dir}/<path_to_file>"
import argparse
import json
import os
import sys
_CWD = os.getcwd()
GRD_BEGIN_TEMPLATE = '<?xml version="1.0" encoding="UTF-8"?>\n'\
'<grit latest_public_release="0" current_release="1" '\
'output_all_resource_defines="false">\n'\
' <outputs>\n'\
' <output filename="grit/{prefix}_resources.h" '\
'type="rc_header">\n'\
' <emit emit_type=\'prepend\'></emit>\n'\
' </output>\n'\
' <output filename="grit/{prefix}_resources_map.cc"\n'\
' type="resource_file_map_source" />\n'\
' <output filename="grit/{prefix}_resources_map.h"\n'\
' type="resource_map_header" />\n'\
' <output filename="{prefix}_resources.pak" '\
'type="data_package" />\n'\
' </outputs>\n'\
' <release seq="1">\n'\
' <includes>\n'
GRD_INCLUDE_TEMPLATE = ' <include name="{name}" ' \
'file="${{root_gen_dir}}/{path_from_gen}" ' \
'use_base_dir="false" type="BINDATA" />\n'
GRD_END_TEMPLATE = ' </includes>\n'\
' </release>\n'\
'</grit>\n'
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument('--manifest-files', required=True, nargs="*")
parser.add_argument('--out-grd', required=True)
parser.add_argument('--grd-prefix', required=True)
parser.add_argument('--root-gen-dir', required=True)
args = parser.parse_args(argv)
grd_file = open(os.path.normpath(os.path.join(_CWD, args.out_grd)), 'w')
grd_file.write(GRD_BEGIN_TEMPLATE.format(prefix=args.grd_prefix))
for manifest_file in args.manifest_files:
manifest_path = os.path.normpath(os.path.join(_CWD, manifest_file))
with open(manifest_path, 'r') as f:
data = json.load(f)
base_dir= os.path.normpath(os.path.join(_CWD, data['base_dir']))
for filename in data['files']:
name_suffix = filename.upper().replace('/', '_').replace('.', '_')
name = 'IDR_%s_%s' % (args.grd_prefix.upper(), name_suffix)
filepath = os.path.join(base_dir, filename).replace('\\', '/')
rebased_path = os.path.relpath(filepath, args.root_gen_dir)
grd_file.write(GRD_INCLUDE_TEMPLATE.format(name=name,
path_from_gen=rebased_path))
grd_file.write(GRD_END_TEMPLATE)
return
if __name__ == '__main__':
main(sys.argv[1:])
#!/usr/bin/env python
# Copyright 2019 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 generate_grd
import os
import shutil
import tempfile
import unittest
_CWD = os.getcwd()
_HERE_DIR = os.path.dirname(__file__)
pathToHere = os.path.relpath(_HERE_DIR, _CWD)
class GenerateGrdTest(unittest.TestCase):
def setUp(self):
self._out_folder = None
def tearDown(self):
shutil.rmtree(self._out_folder)
def _read_out_file(self, file_name):
assert self._out_folder
return open(os.path.join(self._out_folder, file_name), 'rb').read()
def _run_test_(self, manifest_files, grd_expected):
assert not self._out_folder
self._out_folder = tempfile.mkdtemp(dir=_HERE_DIR)
args = [
'--out-grd', os.path.join(self._out_folder, 'test_resources.grd'),
'--grd-prefix', 'test',
'--root-gen-dir', os.path.join(_CWD, pathToHere, 'tests'),
'--manifest-files',
] + manifest_files
generate_grd.main(args)
actual_grd = self._read_out_file('test_resources.grd')
expected_grd = open(
os.path.join(_HERE_DIR, 'tests', grd_expected), 'rb').read()
self.assertEquals(expected_grd, actual_grd)
def testSuccess(self):
self._run_test_([
os.path.join(pathToHere, 'tests', 'test_manifest_1.json'),
os.path.join(pathToHere, 'tests', 'test_manifest_2.json'),
], 'expected_grd.grd')
if __name__ == '__main__':
unittest.main()
<?xml version="1.0" encoding="UTF-8"?>
<grit latest_public_release="0" current_release="1" output_all_resource_defines="false">
<outputs>
<output filename="grit/test_resources.h" type="rc_header">
<emit emit_type='prepend'></emit>
</output>
<output filename="grit/test_resources_map.cc"
type="resource_file_map_source" />
<output filename="grit/test_resources_map.h"
type="resource_map_header" />
<output filename="test_resources.pak" type="data_package" />
</outputs>
<release seq="1">
<includes>
<include name="IDR_TEST_TEST_HTML" file="${root_gen_dir}/preprocessed/test.html" use_base_dir="false" type="BINDATA" />
<include name="IDR_TEST_TEST_JS" file="${root_gen_dir}/preprocessed/test.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_TEST_DIR_ELEMENT_IN_DIR_JS" file="${root_gen_dir}/preprocessed/dir/element_in_dir.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_TEST_TEST_UI_JS" file="${root_gen_dir}/preprocessed/test_ui.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_TEST_TEST_PROXY_JS" file="${root_gen_dir}/preprocessed/test_proxy.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_TEST_DIR_ANOTHER_ELEMENT_IN_DIR_JS" file="${root_gen_dir}/preprocessed/dir/another_element_in_dir.js" use_base_dir="false" type="BINDATA" />
</includes>
</release>
</grit>
{"files": ["test.html", "test.js", "dir/element_in_dir.js"], "base_dir": "tools/tests/preprocessed" }
{"files": ["test_ui.js", "test_proxy.js", "dir/another_element_in_dir.js"], "base_dir": "tools/tests/preprocessed" }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment