Skip to content

Commit

Permalink
Build kotlin-reflect.jar in build.xml and for Maven
Browse files Browse the repository at this point in the history
Reflection will be distributed in a separate jar and not in kotlin-runtime.jar
for two primary reasons:
- Reflection implementation at the moment takes almost 2Mb
- Separate libraries for separate features is a technique encouraged by Maven,
  and it's inconvenient to make it different in the compiler distribution
  • Loading branch information
udalov committed Mar 11, 2015
1 parent 2c8754a commit 2b090e0
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 91 deletions.
152 changes: 88 additions & 64 deletions build.xml
Expand Up @@ -15,14 +15,21 @@
<property name="bootstrap.home" value="dependencies/bootstrap-compiler"/>
<property name="bootstrap.compiler.home" value="${bootstrap.home}/Kotlin/kotlinc"/>
<property name="bootstrap.runtime" value="${bootstrap.compiler.home}/lib/kotlin-runtime.jar"/>
<property name="bootstrap.runtime.minimal" value="${bootstrap.compiler.home}/lib/kotlin-runtime-minimal.jar"/>

<!-- TODO: temporary, remove "else" after bootstrap -->
<condition property="bootstrap.reflect"
value="${bootstrap.compiler.home}/lib/kotlin-reflect.jar"
else="${bootstrap.runtime}">
<available file="${bootstrap.compiler.home}/lib/kotlin-reflect.jar"/>
</condition>

<property name="output" value="${basedir}/dist"/>
<property name="kotlin-home" value="${output}/kotlinc"/>
<property name="build.number" value="snapshot"/>
<property name="bootstrap.build.no.tests" value="false"/>
<property name="idea.sdk" value="${basedir}/ideaSDK"/>
<property name="protobuf.jar" value="${idea.sdk}/lib/protobuf-2.5.0.jar"/>
<property name="protobuf-lite.jar" value="${basedir}/dependencies/protobuf-2.5.0-lite.jar"/>
<property name="javax.inject.jar" value="${basedir}/lib/javax.inject.jar"/>

<property name="java.target" value="1.6"/>
Expand Down Expand Up @@ -50,8 +57,8 @@
little to no differences between the new and the newest runtime.
-->
<condition property="compiler.manifest.class.path"
value="kotlin-runtime-internal-bootstrap.jar"
else="kotlin-runtime.jar">
value="kotlin-runtime-internal-bootstrap.jar kotlin-reflect-internal-bootstrap.jar"
else="kotlin-runtime.jar kotlin-reflect.jar">
<istrue value="${bootstrap.or.local.build}"/>
</condition>

Expand Down Expand Up @@ -541,7 +548,7 @@
-libraryjars '${rtjar}'
-libraryjars '${jssejar}'
-libraryjars '${bootstrap.runtime.minimal}'
-libraryjars '${bootstrap.runtime}'
-target 1.6
-dontoptimize
Expand Down Expand Up @@ -625,6 +632,7 @@
<istrue value="${bootstrap.or.local.build}"/>
<then>
<copy file="${bootstrap.runtime}" tofile="${kotlin-home}/lib/kotlin-runtime-internal-bootstrap.jar"/>
<copy file="${bootstrap.reflect}" tofile="${kotlin-home}/lib/kotlin-reflect-internal-bootstrap.jar"/>
</then>
</if>

Expand Down Expand Up @@ -783,6 +791,25 @@
</new-kotlinc>
</target>

<target name="core">
<new-kotlinc output="${output}/classes/core">
<src>
<include name="core/descriptor.loader.java/src"/>
<include name="core/descriptors/src"/>
<include name="core/descriptors.runtime/src"/>
<include name="core/serialization/src"/>
<include name="core/serialization.jvm/src"/>
<include name="core/util.runtime/src"/>
</src>
<class-path>
<pathelement path="${output}/classes/builtins"/>
<pathelement path="${output}/classes/stdlib"/>
<pathelement path="${protobuf-lite.jar}"/>
<pathelement path="${javax.inject.jar}"/>
</class-path>
</new-kotlinc>
</target>

<target name="reflection">
<new-kotlinc output="${output}/classes/reflection">
<src>
Expand All @@ -791,81 +818,78 @@
<class-path>
<pathelement path="${output}/classes/builtins"/>
<pathelement path="${output}/classes/stdlib"/>
<pathelement path="${output}/classes/core"/>
<pathelement path="${protobuf-lite.jar}"/>
</class-path>
</new-kotlinc>
</target>

<target name="pack-runtime">
<macrodef name="do-pack-runtime">
<attribute name="jar-name"/>
<attribute name="implementation-title"/>
<element name="reflection-classes" optional="true"/>

<sequential>
<jar destfile="${kotlin-home}/lib/@{jar-name}" duplicate="fail">
<fileset dir="${output}/classes/builtins"/>
<fileset dir="${output}/classes/stdlib"/>
<reflection-classes/>
<zipfileset file="${kotlin-home}/build.txt" prefix="META-INF"/>
<macrodef name="pack-runtime-jar">
<attribute name="jar-name"/>
<attribute name="implementation-title"/>
<element name="jar-content"/>

<manifest>
<attribute name="Built-By" value="${manifest.impl.vendor}"/>
<attribute name="Implementation-Vendor" value="${manifest.impl.vendor}"/>
<attribute name="Implementation-Title" value="@{implementation-title}"/>
<attribute name="Implementation-Version" value="${build.number}"/>
</manifest>
</jar>
</sequential>
</macrodef>
<sequential>
<jar destfile="${kotlin-home}/lib/@{jar-name}" duplicate="fail">
<jar-content/>
<zipfileset file="${kotlin-home}/build.txt" prefix="META-INF"/>

<do-pack-runtime jar-name="kotlin-runtime.jar" implementation-title="${manifest.impl.title.kotlin.jvm.runtime}">
<reflection-classes>
<manifest>
<attribute name="Built-By" value="${manifest.impl.vendor}"/>
<attribute name="Implementation-Vendor" value="${manifest.impl.vendor}"/>
<attribute name="Implementation-Title" value="@{implementation-title}"/>
<attribute name="Implementation-Version" value="${build.number}"/>
</manifest>
</jar>
</sequential>
</macrodef>

<target name="pack-runtime">
<pack-runtime-jar jar-name="kotlin-runtime.jar" implementation-title="${manifest.impl.title.kotlin.jvm.runtime}">
<jar-content>
<fileset dir="${output}/classes/builtins"/>
<fileset dir="${output}/classes/stdlib"/>
<zipfileset dir="${output}/builtins"/>
</jar-content>
</pack-runtime-jar>

<pack-runtime-jar jar-name="kotlin-reflect.jar" implementation-title="${manifest.impl.title.kotlin.jvm.reflect}">
<jar-content>
<fileset dir="${output}/classes/reflection"/>
</reflection-classes>
</do-pack-runtime>
<fileset dir="${output}/classes/core"/>
<zipfileset src="${protobuf-lite.jar}"/>
<zipfileset src="${javax.inject.jar}"/>
</jar-content>
</pack-runtime-jar>

<do-pack-runtime jar-name="kotlin-runtime-minimal.jar" implementation-title="${manifest.impl.title.kotlin.jvm.runtime.minimal}"/>
<jar destfile="${kotlin-home}/lib/kotlin-reflect.jar" update="true">
<manifest>
<attribute name="Class-Path" value="kotlin-runtime.jar"/>
</manifest>
</jar>
</target>

<target name="pack-runtime-sources">
<macrodef name="do-pack-runtime-sources">
<attribute name="jar-name"/>
<attribute name="implementation-title"/>
<element name="reflection-sources" optional="true"/>

<sequential>
<jar destfile="${kotlin-home}/lib/@{jar-name}" duplicate="fail">
<fileset dir="${basedir}/core/builtins/native" includes="**/*"/>
<fileset dir="${basedir}/core/builtins/src" includes="**/*"/>
<fileset dir="${basedir}/core/runtime.jvm/src" includes="**/*"/>
<fileset dir="${basedir}/core/reflection/src" includes="**/*"/>
<reflection-sources/>
<fileset dir="${basedir}/libraries/stdlib/src" includes="**/*"/>
<zipfileset file="${kotlin-home}/build.txt" prefix="META-INF"/>

<manifest>
<attribute name="Built-By" value="${manifest.impl.vendor}"/>
<attribute name="Implementation-Vendor" value="${manifest.impl.vendor}"/>
<attribute name="Implementation-Title" value="@{implementation-title}"/>
<attribute name="Implementation-Version" value="${build.number}"/>
</manifest>
</jar>
</sequential>
</macrodef>

<do-pack-runtime-sources jar-name="kotlin-runtime-sources.jar"
implementation-title="${manifest.impl.title.kotlin.jvm.runtime.sources}">
<reflection-sources>
<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/reflection/src" includes="**/*"/>
<fileset dir="${basedir}/core/reflection.jvm/src" includes="**/*"/>
</reflection-sources>
</do-pack-runtime-sources>

<do-pack-runtime-sources jar-name="kotlin-runtime-minimal-sources.jar"
implementation-title="${manifest.impl.title.kotlin.jvm.runtime.minimal.sources}"/>
<fileset dir="${basedir}/core/runtime.jvm/src" includes="**/*"/>
<fileset dir="${basedir}/core/serialization/src" includes="**/*"/>
<fileset dir="${basedir}/core/serialization.jvm/src" includes="**/*"/>
<fileset dir="${basedir}/core/util.runtime/src" includes="**/*"/>
<fileset dir="${basedir}/libraries/stdlib/src" includes="**/*"/>
</jar-content>
</pack-runtime-jar>
</target>

<target name="runtime"
depends="builtins,stdlib,reflection,pack-runtime,pack-runtime-sources"/>
depends="builtins,stdlib,core,reflection,pack-runtime,pack-runtime-sources"/>

<target name="dist"
depends="clean,init,prepare-dist,preloader,serialize-builtins,compiler,compiler-sources,ant-tools,jdk-annotations,android-sdk-annotations,runtime,kotlin-js-stdlib,android-compiler-plugin"
Expand Down
Expand Up @@ -122,6 +122,7 @@ public static File compileJava(@NotNull String filename, @NotNull String... addi
File javaClassesTempDirectory = JetTestUtils.tmpDir("java-classes");
List<String> classpath = new ArrayList<String>();
classpath.add(ForTestCompileRuntime.runtimeJarForTests().getPath());
classpath.add(ForTestCompileRuntime.reflectJarForTests().getPath());
classpath.add(JetTestUtils.getAnnotationsJar().getPath());
classpath.addAll(Arrays.asList(additionalClasspath));
List<String> options = Arrays.asList(
Expand Down
Expand Up @@ -31,19 +31,31 @@ public class ForTestCompileRuntime {

@NotNull
public static File runtimeJarForTests() {
File runtime = new File("dist/kotlinc/lib/kotlin-runtime.jar");
if (!runtime.exists()) {
throw new IllegalStateException("kotlin-runtime.jar in dist/kotlinc/lib does not exist. Run 'ant dist'");
return assertExists(new File("dist/kotlinc/lib/kotlin-runtime.jar"));
}

@NotNull
public static File reflectJarForTests() {
return assertExists(new File("dist/kotlinc/lib/kotlin-reflect.jar"));
}

@NotNull
private static File assertExists(@NotNull File file) {
if (!file.exists()) {
throw new IllegalStateException(file + " does not exist. Run 'ant dist'");
}
return runtime;
return file;
}

@NotNull
public static synchronized ClassLoader runtimeJarClassLoader() {
ClassLoader loader = runtimeJarClassLoader.get();
if (loader == null) {
try {
loader = new URLClassLoader(new URL[] {runtimeJarForTests().toURI().toURL()}, null);
loader = new URLClassLoader(new URL[] {
runtimeJarForTests().toURI().toURL(),
reflectJarForTests().toURI().toURL()
}, null);
}
catch (MalformedURLException e) {
throw rethrow(e);
Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.jetbrains.kotlin.integration;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime;
import org.jetbrains.kotlin.utils.UtilsPackage;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -40,16 +41,20 @@ protected File getTestDataDir() {
private void doJvmAntTest(String... extraJavaArgs) throws Exception {
doAntTest(SUCCESSFUL, extraJavaArgs);

String jar = getOutputFileByName(JVM_OUT_FILE).getAbsolutePath();
String classpath = UtilsPackage.join(Arrays.asList(
getOutputFileByName(JVM_OUT_FILE).getAbsolutePath(),
ForTestCompileRuntime.runtimeJarForTests().getAbsolutePath(),
ForTestCompileRuntime.reflectJarForTests().getAbsolutePath()
), File.pathSeparator);

runJava("hello.run", "-cp", jar + File.pathSeparator + getKotlinRuntimePath(), "hello.HelloPackage");
runJava("hello.run", "-cp", classpath, "hello.HelloPackage");
}

private static String getClassPathForAnt() {
return UtilsPackage.join(Arrays.asList(
getCompilerLib() + File.separator + "kotlin-ant.jar",
getCompilerLib() + File.separator + "kotlin-compiler.jar",
getKotlinRuntimePath()
ForTestCompileRuntime.runtimeJarForTests().getAbsolutePath()
), File.pathSeparator);
}

Expand Down
Expand Up @@ -18,12 +18,15 @@

import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime;
import org.jetbrains.kotlin.utils.UtilsPackage;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

Expand All @@ -40,13 +43,16 @@ protected File getTestDataDir() {
}

private int runCompiler(String logName, String... arguments) throws Exception {
String classpath = getCompilerLib().getAbsolutePath() + File.separator + "kotlin-compiler.jar" + File.pathSeparator +
getKotlinRuntimePath();

Collection<String> javaArgs = new ArrayList<String>();

javaArgs.add("-cp");
javaArgs.add(classpath);
javaArgs.add(UtilsPackage.join(Arrays.asList(
getCompilerLib().getAbsolutePath() + File.separator + "kotlin-compiler.jar",
ForTestCompileRuntime.runtimeJarForTests().getAbsolutePath(),
ForTestCompileRuntime.reflectJarForTests().getAbsolutePath()
), File.pathSeparator));
javaArgs.add("org.jetbrains.kotlin.cli.jvm.K2JVMCompiler");

Collections.addAll(javaArgs, arguments);

return runJava(logName, ArrayUtil.toStringArray(javaArgs));
Expand Down Expand Up @@ -81,7 +87,8 @@ public void compileAndRunModule() throws Exception {
String jar = tmpdir.getTmpDir().getAbsolutePath() + File.separator + "smoke.jar";

assertEquals("compilation failed", 0, runCompiler("Smoke.compile", "-module", "Smoke.ktm", "-d", jar));
runJava("Smoke.run", "-cp", jar + File.pathSeparator + getKotlinRuntimePath(), "Smoke.SmokePackage", "1", "2", "3");
String classpath = jar + File.pathSeparator + ForTestCompileRuntime.runtimeJarForTests().getAbsolutePath();
runJava("Smoke.run", "-cp", classpath, "Smoke.SmokePackage", "1", "2", "3");
}

@Test
Expand Down
Expand Up @@ -31,7 +31,6 @@
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.cli.common.KotlinVersion;
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime;
import org.jetbrains.kotlin.test.JetTestUtils;
import org.jetbrains.kotlin.test.Tmpdir;
import org.jetbrains.kotlin.utils.PathUtil;
Expand Down Expand Up @@ -166,10 +165,6 @@ protected static File getCompilerLib() {
return file;
}

protected static String getKotlinRuntimePath() {
return ForTestCompileRuntime.runtimeJarForTests().getAbsolutePath();
}

protected static File getKotlinProjectHome() {
return new File(PathManager.getHomePath()).getParentFile();
}
Expand Down

0 comments on commit 2b090e0

Please sign in to comment.