1# Copyright 2022 Google LLC. All rights reserved. 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. 14 15"""kt_srcjars""" 16 17# go/keep-sorted start 18load("//:visibility.bzl", "RULES_KOTLIN") 19load(":run_deploy_jar.bzl", "kt_run_deploy_jar") 20# go/keep-sorted end 21 22visibility(RULES_KOTLIN) 23 24def _zip( 25 ctx, 26 kt_jvm_toolchain, 27 out_jar, 28 srcs = [], 29 common_srcs = [], 30 ignore_not_allowed_files = False): 31 """Creates a srcjar from a set of Kotlin and Java srcs 32 33 Paths inside the srcjar are derived from the package name in the source file. 34 """ 35 36 args = ctx.actions.args() 37 args.add("zip") 38 args.add(out_jar) 39 args.add_joined("--kotlin_srcs", srcs, join_with = ",") 40 args.add_joined("--common_srcs", common_srcs, join_with = ",") 41 if ignore_not_allowed_files: 42 args.add("-i") 43 44 kt_run_deploy_jar( 45 ctx = ctx, 46 java_runtime = kt_jvm_toolchain.java_runtime, 47 deploy_jar = kt_jvm_toolchain.source_jar_zipper, 48 inputs = srcs + common_srcs, 49 outputs = [out_jar], 50 args = [args], 51 mnemonic = "KtJar", 52 progress_message = "Create Jar (kotlin/common.bzl): %{output}", 53 ) 54 55 return out_jar 56 57def _unzip( 58 ctx, 59 kt_jvm_toolchain, 60 dir, 61 input): 62 args = ctx.actions.args() 63 args.add("unzip", input) 64 args.add(dir.path) 65 66 kt_run_deploy_jar( 67 ctx = ctx, 68 java_runtime = kt_jvm_toolchain.java_runtime, 69 deploy_jar = kt_jvm_toolchain.source_jar_zipper, 70 inputs = [input], 71 outputs = [dir], 72 args = [args], 73 mnemonic = "SrcJarUnzip", 74 ) 75 76 return dir 77 78def _zip_resources(ctx, kt_jvm_toolchain, output_jar, input_dirs): 79 """Packs a sequence of tree artifacts into a single jar. 80 81 Given the following file directory structure, 82 /usr/home/a/x/1.txt 83 /usr/home/b/y/1.txt 84 with an input_dirs as [ 85 "/usr/home/a", 86 "/usr/home/b", 87 ], 88 The tool produces a jar with in-archive structure of, 89 x/1.txt 90 y/1.txt 91 92 The function fails on the duplicate jar entry case. e.g. if we pass an 93 input_dirs as [ 94 "/usr/home/a/x", 95 "/usr/home/b/y", 96 ], 97 then the blaze action would fail with an error message. 98 "java.lang.IllegalStateException: 1.txt has the same path as 1.txt! 99 If it is intended behavior rename one or both of them." 100 101 Args: 102 ctx: The build rule context. 103 kt_jvm_toolchain: Toolchain containing the jar tool. 104 output_jar: The jar to be produced by this action. 105 input_dirs: A sequence of tree artifacts to be zipped. 106 107 Returns: 108 The generated output jar, i.e. output_jar 109 """ 110 111 args = ctx.actions.args() 112 args.add("zip_resources") 113 args.add(output_jar) 114 args.add_joined( 115 "--input_dirs", 116 input_dirs, 117 join_with = ",", 118 omit_if_empty = False, 119 expand_directories = False, 120 ) 121 122 kt_run_deploy_jar( 123 ctx = ctx, 124 java_runtime = kt_jvm_toolchain.java_runtime, 125 deploy_jar = kt_jvm_toolchain.source_jar_zipper, 126 inputs = input_dirs, 127 outputs = [output_jar], 128 args = [args], 129 mnemonic = "KtJarActionFromTreeArtifacts", 130 progress_message = "Create Jar %{output}", 131 ) 132 133 return output_jar 134 135def _DirSrcjarSyncer( 136 ctx, 137 kt_jvm_toolchain, 138 file_factory): 139 """Synchronizes the contents of a set of srcjar files and tree-artifacts""" 140 141 _dirs = [] 142 _srcjars = [] 143 144 def add_dirs(dirs): 145 if not dirs: 146 return 147 148 _dirs.extend(dirs) 149 _srcjars.append( 150 _zip_resources( 151 ctx, 152 kt_jvm_toolchain, 153 file_factory.declare_file("%s-codegen.srcjar" % len(_srcjars)), 154 dirs, 155 ), 156 ) 157 158 def add_srcjars(srcjars): 159 if not srcjars: 160 return 161 162 for srcjar in srcjars: 163 _dirs.append( 164 _unzip( 165 ctx, 166 kt_jvm_toolchain, 167 file_factory.declare_directory("%s.expand" % len(_dirs)), 168 srcjar, 169 ), 170 ) 171 _srcjars.extend(srcjars) 172 173 return struct( 174 add_dirs = add_dirs, 175 add_srcjars = add_srcjars, 176 dirs = _dirs, 177 srcjars = _srcjars, 178 ) 179 180kt_srcjars = struct( 181 zip = _zip, 182 unzip = _unzip, 183 zip_resources = _zip_resources, 184 DirSrcjarSyncer = _DirSrcjarSyncer, 185) 186