1#!/bin/bash 2# Copyright 2023 Google LLC 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15################################################################################ 16 17# Script to generate the BUILD.bazel file to create tink and tink-android Maven 18# artifacts. 19 20set -euo pipefail 21 22BAZEL_CMD="bazel" 23# Prefer using Bazelisk if available. 24if command -v "bazelisk" &> /dev/null; then 25 BAZEL_CMD="bazelisk" 26fi 27readonly BAZEL_CMD 28 29OUT_FILE="BUILD.bazel" 30 31usage() { 32 cat <<EOF 33Usage: $0 [-o <destination file>] 34 -o: Output file (default=BUILD.bazel). 35 -h: Show this help message. 36EOF 37 exit 1 38} 39 40process_params() { 41 while getopts "ho:" opt; do 42 case "${opt}" in 43 o) OUT_FILE="${OPTARG}" ;; 44 *) usage ;; 45 esac 46 done 47 shift $((OPTIND - 1)) 48 readonly OUT_FILE 49} 50 51create_build_file() { 52 local -r tink_deps="$1" 53 local -r tink_android_deps="$2" 54 cat <<EOF > "${OUT_FILE}" 55# Build file for generating tink and tink-android Maven artifacts. 56 57load("//tools:gen_maven_jar_rules.bzl", "gen_maven_jar_rules") 58 59package(default_visibility = ["//visibility:public"]) 60 61licenses(["notice"]) 62 63exports_files(["BUILD"]) 64 65# WARNING: This is autogenerated using tools/create_maven_build_file.sh. 66gen_maven_jar_rules( 67 name = "tink", 68 doctitle = "Tink Cryptography API", 69 manifest_lines = [ 70 "Automatic-Module-Name: com.google.crypto.tink", 71 ], 72 root_packages = [ 73 "com.google.crypto.tink" 74 ], 75 deps = [ 76$(cat "${tink_deps}" | sed 's/^/ "/' | sed 's/$/",/') 77 ], 78) 79 80gen_maven_jar_rules( 81 name = "tink-android", 82 doctitle = "Tink Cryptography API for Android", 83 resources = glob([ 84 "src/main/resources/**", 85 ]), 86 root_packages = [ 87 "com.google.crypto.tink", 88 ], 89 shaded_packages = [ 90 # The following package(s) will be shaded, according to the rules 91 # specified in shading_rules. 92 "com.google.protobuf", 93 ], 94 shading_rules = "jar_jar_rules.txt", 95 deps = [ 96$(cat "${tink_android_deps}" | sed 's/^/ "/' | sed 's/$/",/') 97 ], 98) 99EOF 100 buildifier "${OUT_FILE}" 101} 102 103readonly TINK_JAVA_PREFIX="//src/main/java/com/google/crypto/tink" 104readonly TINK_JAVA_ANDROID_PREFIX="//src_android/main/java/com/google/crypto/tink" 105readonly TINK_JAVA_INTEGRATION_PREFIX="${TINK_JAVA_PREFIX}/integration" 106 107main() { 108 process_params "$@" 109 110 # Targets in TINK_JAVA_PREFIX of type java_library, excluding: 111 # * testonly targets, 112 # * targets in tink_java_integration_prefix. 113 local -r expected_tink_deps="$(mktemp)" 114 "${BAZEL_CMD}" query "kind(java_library,${TINK_JAVA_PREFIX}/...) \ 115except attr(testonly,1,${TINK_JAVA_PREFIX}/...) \ 116except kind(java_library,${TINK_JAVA_INTEGRATION_PREFIX}/...)" \ 117 > "${expected_tink_deps}" 118 119 # Targets in TINK_JAVA_PREFIX and TINK_JAVA_ANDROID_PREFIX of type 120 # android_library, excluding testonly targets. 121 local -r expected_android_deps="$(mktemp)" 122 "${BAZEL_CMD}" query "kind(android_library,${TINK_JAVA_PREFIX}/...) \ 123except attr(testonly,1,${TINK_JAVA_PREFIX}/...)" > "${expected_android_deps}" 124 125 "${BAZEL_CMD}" query "kind(android_library,${TINK_JAVA_ANDROID_PREFIX}/...) \ 126except attr(testonly,1,${TINK_JAVA_ANDROID_PREFIX}/...)" >> "${expected_android_deps}" 127 128 create_build_file "${expected_tink_deps}" "${expected_android_deps}" 129} 130 131main "$@" 132