Rename packages in kotlin-reflect.jar so that they do not duplicate compiler classes

This commit is contained in:
Andrey Breslav
2015-05-14 13:01:37 +03:00
parent 100c1b2787
commit a2b1a86ef5
3 changed files with 138 additions and 11 deletions
+43 -9
View File
@@ -711,14 +711,15 @@
</target>
<macrodef name="pack-runtime-jar">
<attribute name="jar-dir" default="${kotlin-home}/lib"/>
<attribute name="jar-name"/>
<attribute name="implementation-title"/>
<element name="jar-content"/>
<sequential>
<delete file="${kotlin-home}/lib/@{jar-name}" failonerror="false"/>
<delete file="@{jar-dir}/@{jar-name}" failonerror="false"/>
<jar destfile="${kotlin-home}/lib/@{jar-name}" duplicate="fail">
<jar destfile="@{jar-dir}/@{jar-name}" duplicate="fail">
<jar-content/>
<zipfileset file="${kotlin-home}/build.txt" prefix="META-INF"/>
@@ -745,7 +746,7 @@
</jar-content>
</pack-runtime-jar>
<pack-runtime-jar jar-name="kotlin-reflect.jar" implementation-title="${manifest.impl.title.kotlin.jvm.reflect}">
<pack-runtime-jar jar-dir="${output}" jar-name="kotlin-reflect-before-jarjar.jar" implementation-title="${manifest.impl.title.kotlin.jvm.reflect}">
<jar-content>
<fileset dir="${output}/classes/reflection"/>
<fileset dir="${output}/classes/core"/>
@@ -754,26 +755,59 @@
</jar-content>
</pack-runtime-jar>
<jar destfile="${kotlin-home}/lib/kotlin-reflect.jar" update="true">
<jar destfile="${output}/kotlin-reflect-before-jarjar.jar" update="true">
<manifest>
<attribute name="Class-Path" value="kotlin-runtime.jar"/>
</manifest>
</jar>
<delete file="${output}/kotlin-reflect-jarjar.jar" failonerror="false"/>
<taskdef name="jarjar" classname="com.tonicsystems.jarjar.JarJarTask" classpath="dependencies/jarjar.jar"/>
<jarjar jarfile="${output}/kotlin-reflect-jarjar.jar" filesonly="true" filesetmanifest="merge">
<zipfileset src="${output}/kotlin-reflect-before-jarjar.jar"/>
<rule pattern="org.jetbrains.kotlin.**" result="kotlin.reflect.jvm.internal.impl.@1"/>
<rule pattern="com.google.protobuf.**" result="kotlin.reflect.jvm.internal.impl.com.google.protobuf.@1"/>
<rule pattern="javax.inject.**" result="kotlin.reflect.jvm.internal.impl.javax.inject.@1"/>
</jarjar>
<kotlinc src="${basedir}/generators/infrastructure/strip-kotlin-annotations.kts" output="">
<compilerarg value="-script"/>
<compilerarg value="kotlin/jvm/internal/.*"/> <!-- Annotations to strip -->
<compilerarg value="kotlin/reflect/jvm/internal/impl/.*"/> <!-- Classes to strip from -->
<compilerarg value="${output}/kotlin-reflect-jarjar.jar"/>
<compilerarg value="${kotlin-home}/lib/kotlin-reflect.jar"/>
<classpath>
<pathelement location="${idea.sdk}/lib/asm-all.jar"/>
</classpath>
</kotlinc>
</target>
<target name="pack-runtime-sources">
<!-- Rename packages in the sources of reflection impl (core) -->
<delete dir="${output}/core.src" failonerror="false"/>
<copy todir="${output}/core.src/kotlin/reflect/jvm/internal/impl">
<fileset dir="core">
<include name="descriptor.loader.java/src/**"/>
<include name="descriptors/src/**"/>
<include name="descriptors.runtime/src/**"/>
<include name="deserialization/src/**"/>
<include name="util.runtime/src/**"/>
</fileset>
<cutdirsmapper dirs="5"/> <!-- module/src/org/jetbrains/kotlin -->
</copy>
<replaceregexp match="org\.jetbrains\.kotlin" replace="kotlin.reflect.jvm.internal.impl" flags="g">
<fileset dir="${output}/core.src"/>
</replaceregexp>
<pack-runtime-jar jar-name="kotlin-runtime-sources.jar" implementation-title="${manifest.impl.title.kotlin.jvm.runtime.sources}">
<jar-content>
<fileset dir="${basedir}/core/builtins/native" includes="**/*"/>
<fileset dir="${basedir}/core/builtins/src" includes="**/*"/>
<fileset dir="${basedir}/core/descriptor.loader.java/src" includes="**/*"/>
<fileset dir="${basedir}/core/descriptors/src" includes="**/*"/>
<fileset dir="${basedir}/core/descriptors.runtime/src" includes="**/*"/>
<fileset dir="${basedir}/core/deserialization/src" includes="**/*"/>
<fileset dir="${basedir}/core/reflection.jvm/src" includes="**/*"/>
<fileset dir="${basedir}/core/runtime.jvm/src" includes="**/*"/>
<fileset dir="${basedir}/core/util.runtime/src" includes="**/*"/>
<fileset dir="${basedir}/libraries/stdlib/src" includes="**/*"/>
<fileset dir="${output}/core.src" includes="**/*"/>
</jar-content>
</pack-runtime-jar>
</target>
@@ -0,0 +1,93 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.jetbrains.org.objectweb.asm.*
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
import java.util.jar.JarFile
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
/**
* Removes kotlin.jvm.internal.* annotations from Kotlin classes
*/
fun main(args: Array<String>) {
if (args.size() != 4) {
error("Usage: kotlinc -script strip-kotlin-annotations.kts <annotation-internal-name-regex> <class-internal-name-regex> <path-to-in-jar> <path-to-out-jar>")
}
val annotationRegex = args[0].toRegex()
val classRegex = args[1].toRegex()
val inFile = File(args[2])
val outFile = File(args[3])
assert(inFile.exists()) { "Input file not found at $inFile" }
println("Stripping annotations from all classes in $inFile")
println("Input file size: ${inFile.length()} bytes")
fun transform(entryName: String, bytes: ByteArray): ByteArray {
if (!entryName.endsWith(".class")) return bytes
if (!classRegex.matches(entryName.removeSuffix(".class"))) return bytes
var changed = false
val classWriter = ClassWriter(0)
val classVisitor = object : ClassVisitor(Opcodes.ASM5, classWriter) {
override fun visitAnnotation(desc: String, visible: Boolean): AnnotationVisitor? {
if (annotationRegex.matches(Type.getType(desc).getInternalName())) {
changed = true
return null
}
return super.visitAnnotation(desc, visible)
}
}
ClassReader(bytes).accept(classVisitor, 0)
if (!changed) return bytes
return classWriter.toByteArray()
}
ZipOutputStream(BufferedOutputStream(FileOutputStream(outFile))).use {
outJar ->
val inJar = JarFile(inFile)
try {
for (entry in inJar.entries()) {
val inBytes = inJar.getInputStream(entry).readBytes()
val outBytes = transform(entry.getName(), inBytes)
if (inBytes.size() < outBytes.size()) {
error("Size increased for ${entry.getName()}: was ${inBytes.size()} bytes, became ${outBytes.size()} bytes")
}
entry.setCompressedSize(-1L)
outJar.putNextEntry(entry)
outJar.write(outBytes)
outJar.closeEntry()
}
}
finally {
// Yes, JarFile does not extend Closeable on JDK 6 so we can't use "use" here
inJar.close()
}
}
println("Output written to $outFile")
println("Output file size: ${outFile.length()} bytes")
}
main(args)
+2 -2
View File
@@ -139,8 +139,8 @@
</unzip>
<!-- JarJar -->
<get src="http://jarjar.googlecode.com/files/jarjar-1.2.jar" dest="dependencies/download/jarjar-1.2.jar" usetimestamp="true"/>
<copy file="dependencies/download/jarjar-1.2.jar" tofile="dependencies/jarjar.jar" overwrite="true"/>
<get src="http://jarjar.googlecode.com/files/jarjar-1.4.jar" dest="dependencies/download/jarjar-1.4.jar" usetimestamp="true"/>
<copy file="dependencies/download/jarjar-1.4.jar" tofile="dependencies/jarjar.jar" overwrite="true"/>
<!-- ant 1.7.0, 1.8.0 -->
<get-ant-library version="1.7.0" folderName="ant-1.7"/>