
Non-android scripts use these helpers, so this moves them outside of the //build/android directory. This also modifies the helpers in the following ways: * Renamed them. * Added more assert checks about valid paths * Fixed zip_directory() adding \s instead of /s on windows * Merged in zipalign logic * Changed compress_fn -> compress= (was unused) * Do not call atomic_output from zip_directory() * Updated all callers to do so explicitly. Bug: 1428082 Change-Id: I9ad5df6cc2491efb974bc914ddbcc6d9de2d4657 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4386354 Reviewed-by: Sam Maier <smaier@chromium.org> Commit-Queue: Andrew Grieve <agrieve@chromium.org> Owners-Override: Andrew Grieve <agrieve@chromium.org> Cr-Commit-Position: refs/heads/main@{#1124879}
113 lines
3.3 KiB
Python
Executable File
113 lines
3.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
#
|
|
# Copyright 2020 The Chromium Authors
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
# found in the LICENSE file.
|
|
|
|
import argparse
|
|
import os
|
|
import re
|
|
import sys
|
|
import zipfile
|
|
|
|
from util import build_utils
|
|
from util import java_cpp_utils
|
|
import action_helpers # build_utils adds //build to sys.path.
|
|
import zip_helpers
|
|
|
|
|
|
class FeatureParserDelegate(java_cpp_utils.CppConstantParser.Delegate):
|
|
# Ex. 'BASE_FEATURE(kConstantName, "StringNameOfTheFeature", ...);'
|
|
# would parse as:
|
|
# ExtractConstantName() -> 'ConstantName'
|
|
# ExtractValue() -> '"StringNameOfTheFeature"'
|
|
FEATURE_RE = re.compile(r'BASE_FEATURE\(k([^,]+),')
|
|
VALUE_RE = re.compile(r'\s*("(?:\"|[^"])*")\s*,')
|
|
|
|
def ExtractConstantName(self, line):
|
|
match = FeatureParserDelegate.FEATURE_RE.match(line)
|
|
return match.group(1) if match else None
|
|
|
|
def ExtractValue(self, line):
|
|
match = FeatureParserDelegate.VALUE_RE.search(line)
|
|
return match.group(1) if match else None
|
|
|
|
def CreateJavaConstant(self, name, value, comments):
|
|
return java_cpp_utils.JavaString(name, value, comments)
|
|
|
|
|
|
def _GenerateOutput(template, source_paths, template_path, features):
|
|
description_template = """
|
|
// This following string constants were inserted by
|
|
// {SCRIPT_NAME}
|
|
// From
|
|
// {SOURCE_PATHS}
|
|
// Into
|
|
// {TEMPLATE_PATH}
|
|
|
|
"""
|
|
values = {
|
|
'SCRIPT_NAME': java_cpp_utils.GetScriptName(),
|
|
'SOURCE_PATHS': ',\n // '.join(source_paths),
|
|
'TEMPLATE_PATH': template_path,
|
|
}
|
|
description = description_template.format(**values)
|
|
native_features = '\n\n'.join(x.Format() for x in features)
|
|
|
|
values = {
|
|
'NATIVE_FEATURES': description + native_features,
|
|
}
|
|
return template.format(**values)
|
|
|
|
|
|
def _ParseFeatureFile(path):
|
|
with open(path) as f:
|
|
feature_file_parser = java_cpp_utils.CppConstantParser(
|
|
FeatureParserDelegate(), f.readlines())
|
|
return feature_file_parser.Parse()
|
|
|
|
|
|
def _Generate(source_paths, template_path):
|
|
with open(template_path) as f:
|
|
lines = f.readlines()
|
|
|
|
template = ''.join(lines)
|
|
package, class_name = java_cpp_utils.ParseTemplateFile(lines)
|
|
output_path = java_cpp_utils.GetJavaFilePath(package, class_name)
|
|
|
|
features = []
|
|
for source_path in source_paths:
|
|
features.extend(_ParseFeatureFile(source_path))
|
|
|
|
output = _GenerateOutput(template, source_paths, template_path, features)
|
|
return output, output_path
|
|
|
|
|
|
def _Main(argv):
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--srcjar',
|
|
required=True,
|
|
help='The path at which to generate the .srcjar file')
|
|
|
|
parser.add_argument('--template',
|
|
required=True,
|
|
help='The template file with which to generate the Java '
|
|
'class. Must have "{NATIVE_FEATURES}" somewhere in '
|
|
'the template.')
|
|
|
|
parser.add_argument('inputs',
|
|
nargs='+',
|
|
help='Input file(s)',
|
|
metavar='INPUTFILE')
|
|
args = parser.parse_args(argv)
|
|
|
|
with action_helpers.atomic_output(args.srcjar) as f:
|
|
with zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) as srcjar:
|
|
data, path = _Generate(args.inputs, args.template)
|
|
zip_helpers.add_to_zip_hermetic(srcjar, path, data=data)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
_Main(sys.argv[1:])
|