Produce deterministic jar files.

This resets all timestamps present in jars produced by kotlinc.

This is important for build systems like bazel that rely on
deterministic outputs.

Before:

```
$ kotlinc ~/test.kt -d /tmp/a.jar
$ kotlinc ~/test.kt -d /tmp/b.jar
9ab80cd40c9293a7a66adf1154d4e6e9aa92d5b0  /tmp/a.jar
1ba022697317f796bd123fb4dc95418a18bcb51a  /tmp/a.jar
6d2a2683470c24928f3fbd6768a4c57f55b0d196  /tmp/b.jar
$ unzip -l /tmp/a.jar
Archive:  /tmp/a.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
       75  09-25-2020 16:48   META-INF/MANIFEST.MF
      683  09-25-2020 16:48   TestKt.class
       28  09-25-2020 16:48   META-INF/main.kotlin_module
---------                     -------
      786                     3 files
```

After:

```
$ kotlinc ~/test.kt -d /tmp/a.jar
$ kotlinc ~/test.kt -d /tmp/b.jar
$ shasum /tmp/a.jar /tmp/b.jar
9ab80cd40c9293a7a66adf1154d4e6e9aa92d5b0  /tmp/a.jar
9ab80cd40c9293a7a66adf1154d4e6e9aa92d5b0  /tmp/b.jar
$ unzip -l /tmp/a.jar
Archive:  /tmp/a.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
       75  12-31-1969 19:00   META-INF/MANIFEST.MF
      590  12-31-1969 19:00   TestKt.class
       36  12-31-1969 19:00   META-INF/main.kotlin_module
---------                     -------
      701                     3 files
```

See https://github.com/JetBrains/kotlin/pull/3226 for a similar change.
This commit is contained in:
Martin Petrov
2020-09-25 16:45:12 -04:00
committed by Alexander Udalov
parent a3830b2611
commit 68fdeaf865
7 changed files with 130 additions and 10 deletions
@@ -0,0 +1,83 @@
/*
* Copyright 2010-2020 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.
*/
package org.jetbrains.kotlin.cli
import java.io.File
import java.io.FileInputStream
import java.util.jar.JarInputStream
import java.util.zip.ZipEntry
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
import org.jetbrains.kotlin.cli.jvm.compiler.CompileEnvironmentUtil.DOS_EPOCH
import org.jetbrains.kotlin.test.TestCaseWithTmpdir
class DeterministicOutputTest : TestCaseWithTmpdir() {
fun testDeterministicOutput() {
val fooKt = tmpdir.resolve("foo.kt").also {
it.writeText("class Foo")
}
val firstJar = tmpdir.resolve("first.jar")
AbstractCliTest.executeCompilerGrabOutput(
K2JVMCompiler(),
listOf(fooKt.path, "-d", firstJar.path, "-include-runtime"))
val secondJar = tmpdir.resolve("second.jar")
AbstractCliTest.executeCompilerGrabOutput(
K2JVMCompiler(),
listOf(fooKt.path, "-d", secondJar.path, "-include-runtime"))
assertEquals(
firstJar.readBytes().toList(),
secondJar.readBytes().toList(),
"jar contents should be identical if compiler command and inputs are the same")
assertAllTimestampsAreReset(firstJar)
assertAllTimestampsAreReset(secondJar)
}
fun testNoResetJarTimestamps() {
val fooKt = tmpdir.resolve("foo.kt").also {
it.writeText("class Foo")
}
val jar = tmpdir.resolve("jarWithTimestamps.jar")
AbstractCliTest.executeCompilerGrabOutput(
K2JVMCompiler(),
listOf(fooKt.path, "-d", jar.path, "-include-runtime", "-Xno-reset-jar-timestamps"))
assertNoTimestampsAreReset(jar)
}
private fun assertAllTimestampsAreReset(jar: File) {
val zis = JarInputStream(FileInputStream(jar))
var entry: ZipEntry? = zis.nextEntry
while (entry != null) {
assertEquals(entry.time, DOS_EPOCH, "$entry timestamp should be reset")
entry = zis.nextEntry
}
}
private fun assertNoTimestampsAreReset(jar: File) {
val zis = JarInputStream(FileInputStream(jar))
var entry: ZipEntry? = zis.nextEntry
while (entry != null) {
assertNotEquals(entry.time, DOS_EPOCH, "$entry timestamp should not be reset")
entry = zis.nextEntry
}
}
}