xref: /aosp_15_r20/external/tink/kokoro/testutils/create_main_build_file.py (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1# Copyright 2022 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14r"""Creates the "BUILD.bazel" file on Kororo.
15
16Usage: python3 create_main_build_file.py \
17  java_dependencies_file.txt  \
18  android_dependencies_file.txt \
19  aws_kms_dependencies_file.txt \
20  gcp_kms_dependencies_file.txt
21where the files contain a list of dependencies to be used in the created file.
22"""
23
24import string
25import sys
26
27TEMPLATE = string.Template("""\
28## This file is created using "create_main_build_file.py".
29
30load("//tools:gen_maven_jar_rules.bzl", "gen_maven_jar_rules")
31
32package(default_visibility = ["//visibility:public"])
33
34licenses(["notice"])
35
36exports_files(["BUILD"])
37
38# Maven jars.
39# DO NOT USE FOR ANY OTHER PURPOSES.
40
41gen_maven_jar_rules(
42    name = "tink",
43    doctitle = "Tink Cryptography API",
44    manifest_lines = [
45        "Automatic-Module-Name: com.google.crypto.tink",
46    ],
47    root_packages = [
48        "com.google.crypto.tink",
49    ],
50    deps = [
51$java_deps_formatted
52    ],
53)
54
55gen_maven_jar_rules(
56    name = "tink-android",
57    doctitle = "Tink Cryptography API for Android",
58    resources = glob([
59        "src/main/resources/**",
60    ]),
61    root_packages = [
62        "com.google.crypto.tink",
63    ],
64    shaded_packages = [
65        # The following package(s) will be shaded, according to the rules
66        # specified in shading_rules.
67        "com.google.protobuf",
68    ],
69    shading_rules = "jar_jar_rules.txt",
70    deps = [
71$andr_deps_formatted
72    ],
73)
74
75gen_maven_jar_rules(
76    name = "tink-awskms",
77    doctitle = "Tink Cryptography API with AWS KMS",
78    manifest_lines = [
79        "Automatic-Module-Name: com.google.crypto.tink.integration.awskms",
80    ],
81    root_packages = [
82        "com.google.crypto.tink.integration.awskms",
83    ],
84    deps = [
85$awsk_deps_formatted
86    ],
87)
88
89gen_maven_jar_rules(
90    name = "tink-gcpkms",
91    doctitle = "Tink Cryptography API with Google Cloud KMS",
92    manifest_lines = [
93        "Automatic-Module-Name: com.google.crypto.tink.integration.gcpkms",
94    ],
95    root_packages = [
96        "com.google.crypto.tink.integration.gcpkms",
97    ],
98    deps = [
99$gcpk_deps_formatted
100    ],
101)""")
102
103
104def _format_deps(deps_list):
105  """Maps a list of dependencies into a single string."""
106
107  stripped_quoted_deps = ['"' + l.strip() + '",' for l in deps_list]
108  return '\n'.join(stripped_quoted_deps)
109
110
111def main():
112  if len(sys.argv) != 5:
113    sys.exit('4 Arguments Required')
114
115  with open(sys.argv[1], 'r') as f:
116    java_deps_file_content = f.readlines()
117  with open(sys.argv[2], 'r') as f:
118    android_deps_file_content = f.readlines()
119  with open(sys.argv[3], 'r') as f:
120    aws_kms_deps_file_content = f.readlines()
121  with open(sys.argv[4], 'r') as f:
122    gcp_kms_deps_file_content = f.readlines()
123
124  print(
125      TEMPLATE.substitute(
126          java_deps_formatted=_format_deps(java_deps_file_content),
127          andr_deps_formatted=_format_deps(android_deps_file_content),
128          awsk_deps_formatted=_format_deps(aws_kms_deps_file_content),
129          gcpk_deps_formatted=_format_deps(gcp_kms_deps_file_content)))
130
131if __name__ == '__main__':
132  main()
133