Move strip-kotlin-annotations script to buildSrc

... and invoke it directly in kotlin-reflect's build file, instead of
running another instance of compiler to evaluate a script. Also only
strip kotlin.Metadata, since it's the only annotation with heavy
metadata on Kotlin-generated class files
This commit is contained in:
Alexander Udalov
2017-11-07 17:27:54 +01:00
parent 38bee07fd0
commit 208e68a968
7 changed files with 24 additions and 73 deletions
-2
View File
@@ -64,8 +64,6 @@ allprojects {
extra["kotlin_root"] = rootDir
val bootstrapCompileCfg = configurations.create("bootstrapCompile")
val scriptCompileCfg = configurations.create("scriptCompile").extendsFrom(bootstrapCompileCfg)
val scriptRuntimeCfg = configurations.create("scriptRuntime").extendsFrom(scriptCompileCfg)
repositories {
for (repo in (rootProject.extra["repos"] as List<String>)) {
+1
View File
@@ -45,6 +45,7 @@ dependencies {
// TODO: adding the dep to the plugin breaks the build unexpectedly, resolve and uncomment
// compile("org.jetbrains.kotlin:kotlin-gradle-plugin:${rootProject.extra["bootstrap_kotlin_version"]}")
compile("com.github.jengelman.gradle.plugins:shadow:${property("versions.shadow")}")
compile("org.ow2.asm:asm-all:6.0_BETA")
}
samWithReceiver {
@@ -1,20 +1,7 @@
/*
* 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.
*/
@file:Suppress("unused") // usages in build scripts are not tracked properly
import org.jetbrains.org.objectweb.asm.*
import org.gradle.api.logging.Logger
import org.objectweb.asm.*
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
@@ -22,24 +9,13 @@ import java.util.jar.JarFile
import java.util.zip.ZipOutputStream
/**
* Removes metadata annotations from Kotlin classes
* Removes @kotlin.Metadata annotations from compiled 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])
fun stripMetadata(logger: Logger, classNamePattern: String, inFile: File, outFile: File) {
val classRegex = classNamePattern.toRegex()
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
@@ -48,7 +24,7 @@ fun main(args: Array<String>) {
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())) {
if (Type.getType(desc).internalName == "kotlin/Metadata") {
changed = true
return null
}
@@ -61,8 +37,7 @@ fun main(args: Array<String>) {
return classWriter.toByteArray()
}
ZipOutputStream(BufferedOutputStream(FileOutputStream(outFile))).use {
outJar ->
ZipOutputStream(BufferedOutputStream(FileOutputStream(outFile))).use { outJar ->
JarFile(inFile).use { inJar ->
for (entry in inJar.entries()) {
val inBytes = inJar.getInputStream(entry).readBytes()
@@ -80,8 +55,9 @@ fun main(args: Array<String>) {
}
}
println("Output written to $outFile")
println("Output file size: ${outFile.length()} bytes")
logger.info("Stripping @kotlin.Metadata annotations from all classes in $inFile")
logger.info("Class name pattern: $classNamePattern")
logger.info("Input file size: ${inFile.length()} bytes")
logger.info("Output written to $outFile")
logger.info("Output file size: ${outFile.length()} bytes")
}
main(args)
@@ -28,6 +28,7 @@ class CodeConformanceTest : TestCase() {
private val SOURCES_FILE_PATTERN = Pattern.compile("(.+\\.java|.+\\.kt|.+\\.js)")
private val EXCLUDED_FILES_AND_DIRS = listOf(
"android.tests.dependencies",
"buildSrc",
"core/reflection.jvm/src/kotlin/reflect/jvm/internal/pcollections",
"libraries/kotlin.test/js/it/.gradle",
"libraries/kotlin.test/js/it/node_modules",
+1 -3
View File
@@ -4,9 +4,7 @@
},
"configurations": [
"bootstrapCompile",
"scriptCompile",
"scriptRuntime"
"bootstrapCompile"
],
"extensions": {
"ext": "org.gradle.api.plugins.ExtraPropertiesExtension",
-19
View File
@@ -206,22 +206,3 @@ ext.createPreprocessorTask = { Project project, def name, def sourceDir, def tar
args = [sourceDir, targetDir, profile]
}
}
ext.createScriptTask = { Project project, def name, Closure<JavaExec> configureClosure = null ->
JavaExec task = project.tasks.create(name, JavaExec)
return project.configure(task) {
// dependsOn(rootProject.prepareBootstrap)
classpath = rootProject.configurations.scriptCompile
main = "org.jetbrains.kotlin.cli.jvm.K2JVMCompiler"
args = [
"-script",
"-version",
"-no-stdlib",
"-cp", rootProject.configurations.scriptRuntime.asPath]
if (configureClosure != null) {
configureClosure.delegate = it
configureClosure.call()
}
}
}
+8 -12
View File
@@ -89,11 +89,11 @@ task reflectShadowJar(type: ShadowJar) {
version = null
manifestAttributes(manifest, project, 'Main')
from (sourceSets.main.output)
from ("${core}/descriptors.jvm/src") {
from(sourceSets.main.output)
from(project(":core:descriptors.jvm").sourceSets.main.resources) {
include 'META-INF/services/**'
}
from ("${core}/deserialization/src") {
from(project(":core:deserialization").sourceSets.main.resources) {
include 'META-INF/services/**'
}
@@ -103,19 +103,15 @@ task reflectShadowJar(type: ShadowJar) {
mergeServiceFiles()
}
createScriptTask(project, "stripMetadata") {
task stripMetadata {
dependsOn reflectShadowJar
def inputJar = reflectShadowJar.archivePath
def outputJar = "${libsDir}/kotlin-reflect-stripped.jar"
def outputJar = new File("${libsDir}/kotlin-reflect-stripped.jar")
inputs.file(inputJar)
outputs.file(outputJar)
args += [
"${rootDir}/generators/infrastructure/strip-kotlin-annotations.kts",
"kotlin/Metadata",
"kotlin/reflect/jvm/internal/impl/.*",
inputJar,
outputJar
]
doLast {
StripMetadataKt.stripMetadata(logger, "kotlin/reflect/jvm/internal/impl/.*", inputJar, outputJar)
}
}
def mainArchiveName = "${archivesBaseName}-${project.version}.jar"