From b7a43db8c1838e2402431f2d5b1d2396c08aed45 Mon Sep 17 00:00:00 2001 From: Gautam Korlam Date: Fri, 26 May 2017 11:26:54 -0700 Subject: [PATCH] Migrate to kotlin in memory compiler support in buck --- .../okbuck/config/DotBuckConfigLocalFile.java | 14 ++-- .../DotBuckConfigLocalGenerator.groovy | 6 +- .../com/uber/okbuck/core/task/OkBuckTask.java | 12 ++-- .../com/uber/okbuck/core/util/KotlinUtil.java | 31 ++------- .../uber/okbuck/core/util/kotlin/bin/kotlinc | 68 ------------------- 5 files changed, 18 insertions(+), 113 deletions(-) delete mode 100755 buildSrc/src/main/resources/com/uber/okbuck/core/util/kotlin/bin/kotlinc diff --git a/buildSrc/src/main/groovy/com/uber/okbuck/config/DotBuckConfigLocalFile.java b/buildSrc/src/main/groovy/com/uber/okbuck/config/DotBuckConfigLocalFile.java index b3cc0e59b..cabb97a50 100644 --- a/buildSrc/src/main/groovy/com/uber/okbuck/config/DotBuckConfigLocalFile.java +++ b/buildSrc/src/main/groovy/com/uber/okbuck/config/DotBuckConfigLocalFile.java @@ -14,8 +14,7 @@ public final class DotBuckConfigLocalFile extends BuckConfigFile { private final String target; private final List ignore; private final String groovyHome; - private final String kotlinCompiler; - private final String kotlinRuntime; + private final String kotlinHome; private final String proguardJar; private final Set defs; @@ -25,8 +24,7 @@ public DotBuckConfigLocalFile( String target, List ignore, String groovyHome, - String kotlinCompiler, - String kotlinRuntime, + String kotlinHome, String proguardJar, Set defs) { this.aliases = aliases; @@ -34,8 +32,7 @@ public DotBuckConfigLocalFile( this.target = target; this.ignore = ignore; this.groovyHome = groovyHome; - this.kotlinCompiler = kotlinCompiler; - this.kotlinRuntime = kotlinRuntime; + this.kotlinHome = kotlinHome; this.proguardJar = proguardJar; this.defs = defs; } @@ -67,10 +64,9 @@ public final void print(PrintStream printer) { printer.println(); } - if (!StringUtils.isEmpty(kotlinCompiler) && !StringUtils.isEmpty(kotlinRuntime)) { + if (!StringUtils.isEmpty(kotlinHome)) { printer.println("[kotlin]"); - printer.println("\tcompiler = " + kotlinCompiler); - printer.println("\truntime_jar = " + kotlinRuntime); + printer.println("\tkotlin_home = " + kotlinHome); printer.println(); } diff --git a/buildSrc/src/main/groovy/com/uber/okbuck/generator/DotBuckConfigLocalGenerator.groovy b/buildSrc/src/main/groovy/com/uber/okbuck/generator/DotBuckConfigLocalGenerator.groovy index 76840bca2..2eeae7c4f 100644 --- a/buildSrc/src/main/groovy/com/uber/okbuck/generator/DotBuckConfigLocalGenerator.groovy +++ b/buildSrc/src/main/groovy/com/uber/okbuck/generator/DotBuckConfigLocalGenerator.groovy @@ -18,8 +18,7 @@ final class DotBuckConfigLocalGenerator { */ static DotBuckConfigLocalFile generate(OkBuckExtension okbuck, String groovyHome, - String kotlinCompiler, - String kotlinRuntime, + String kotlinHome, String proguardJar, Set defs) { Map aliases = [:] @@ -37,8 +36,7 @@ final class DotBuckConfigLocalGenerator { okbuck.target, [".git", "**/.svn"], groovyHome, - kotlinCompiler, - kotlinRuntime, + kotlinHome, proguardJar, defs) } diff --git a/buildSrc/src/main/java/com/uber/okbuck/core/task/OkBuckTask.java b/buildSrc/src/main/java/com/uber/okbuck/core/task/OkBuckTask.java index 957fb06d2..ee889446e 100644 --- a/buildSrc/src/main/java/com/uber/okbuck/core/task/OkBuckTask.java +++ b/buildSrc/src/main/java/com/uber/okbuck/core/task/OkBuckTask.java @@ -44,15 +44,13 @@ void okbuck() { // Fetch Kotlin support deps if needed boolean hasKotlinLib = okBuckExtension.buckProjects.parallelStream().anyMatch(project -> ProjectUtil.getType(project) == ProjectType.KOTLIN_LIB); - Pair kotlinDeps = null; if (hasKotlinLib) { - kotlinDeps = KotlinUtil.setupKotlinHome(getProject()); + KotlinUtil.setupKotlinHome(getProject()); } generate(okBuckExtension, hasGroovyLib ? GroovyUtil.GROOVY_HOME_LOCATION : null, - kotlinDeps != null ? kotlinDeps.getLeft(): null, - kotlinDeps != null ? kotlinDeps.getRight(): null); + hasKotlinLib ? KotlinUtil.KOTLIN_HOME_LOCATION : null); } @Override public String getGroup() { @@ -78,8 +76,7 @@ public File dotBuckConfigLocal() { return getProject().file(".buckconfig.local"); } - private void generate(OkBuckExtension okbuckExt, String groovyHome, - String kotlinCompiler, String KotlinRuntime) { + private void generate(OkBuckExtension okbuckExt, String groovyHome, String kotlinHome) { // generate empty .buckconfig if it does not exist if (!dotBuckConfig().exists()) { try { @@ -101,8 +98,7 @@ private void generate(OkBuckExtension okbuckExt, String groovyHome, PrintStream configPrinter = new PrintStream(dotBuckConfigLocal())) { DotBuckConfigLocalGenerator.generate(okbuckExt, groovyHome, - kotlinCompiler, - KotlinRuntime, + kotlinHome, ProguardUtil.getProguardJarPath(getProject()), defs).print(configPrinter); } catch (IOException e) { diff --git a/buildSrc/src/main/java/com/uber/okbuck/core/util/KotlinUtil.java b/buildSrc/src/main/java/com/uber/okbuck/core/util/KotlinUtil.java index cfeb3abbe..961f28d6d 100644 --- a/buildSrc/src/main/java/com/uber/okbuck/core/util/KotlinUtil.java +++ b/buildSrc/src/main/java/com/uber/okbuck/core/util/KotlinUtil.java @@ -1,32 +1,28 @@ package com.uber.okbuck.core.util; -import com.google.common.collect.ImmutableMap; import com.uber.okbuck.OkBuckGradlePlugin; import com.uber.okbuck.core.dependency.DependencyCache; -import org.apache.commons.lang3.tuple.Pair; import org.gradle.api.Project; import org.gradle.api.artifacts.Configuration; import org.gradle.api.artifacts.ModuleVersionIdentifier; -import org.gradle.api.artifacts.ResolvedArtifact; import org.gradle.api.artifacts.dsl.DependencyHandler; import org.jetbrains.annotations.Nullable; -import java.io.File; import java.util.Collections; public final class KotlinUtil { private static final String KOTLIN_DEPS_CONFIG = "okbuck_kotlin_deps"; private static final String KOTLIN_GROUP = "org.jetbrains.kotlin"; - private static final String KOTLIN_COMPILER_MODULE = "kotlin-compiler-embeddable"; - private static final String KOTLIN_RUNTIME_MODULE = "kotlin-runtime"; - private static final String KOTLIN_HOME_LOCATION = OkBuckGradlePlugin.DEFAULT_CACHE_PATH + "/kotlin_installation"; + private static final String KOTLIN_COMPILER_MODULE = "kotlin-compiler"; + private static final String KOTLIN_STDLIB_MODULE = "kotlin-stdlib"; + public static final String KOTLIN_HOME_LOCATION = OkBuckGradlePlugin.DEFAULT_CACHE_PATH + "/kotlin_home"; private KotlinUtil() {} @Nullable - public static String getKotlinVersion(Project project) { + static String getKotlinVersion(Project project) { return project.getBuildscript() .getConfigurations() .getByName("classpath") @@ -44,31 +40,18 @@ public static String getKotlinVersion(Project project) { } @SuppressWarnings("ResultOfMethodCallIgnored") - public static Pair setupKotlinHome(Project rootProject) { + public static void setupKotlinHome(Project rootProject) { String kotlinVersion = getKotlinVersion(rootProject); Configuration kotlinConfig = rootProject.getConfigurations().maybeCreate(KOTLIN_DEPS_CONFIG); DependencyHandler handler = rootProject.getDependencies(); handler.add(KOTLIN_DEPS_CONFIG, String.format("%s:%s:%s", KOTLIN_GROUP, KOTLIN_COMPILER_MODULE, kotlinVersion)); - handler.add(KOTLIN_DEPS_CONFIG, String.format("%s:%s:%s", KOTLIN_GROUP, KOTLIN_RUNTIME_MODULE, kotlinVersion)); + handler.add(KOTLIN_DEPS_CONFIG, String.format("%s:%s:%s", KOTLIN_GROUP, KOTLIN_STDLIB_MODULE, kotlinVersion)); new DependencyCache("kotlin", rootProject, - KOTLIN_HOME_LOCATION + "/lib", + KOTLIN_HOME_LOCATION, Collections.singleton(kotlinConfig), null); - - File kotlinHome = new File(KOTLIN_HOME_LOCATION); - - File kotlinc = new File(kotlinHome, "bin/kotlinc"); - FileUtil.copyResourceToProject("kotlin/bin/kotlinc", - new File(kotlinHome, "bin/kotlinc"), - ImmutableMap.of("template-kotlin-version", kotlinVersion)); - kotlinc.setExecutable(true); - - String kotlinCompiler = rootProject.relativePath(kotlinc); - String kotlinRuntime = String.format("%s/lib/%s-%s.jar", KOTLIN_HOME_LOCATION, KOTLIN_RUNTIME_MODULE, - kotlinVersion); - return Pair.of(kotlinCompiler, kotlinRuntime); } } diff --git a/buildSrc/src/main/resources/com/uber/okbuck/core/util/kotlin/bin/kotlinc b/buildSrc/src/main/resources/com/uber/okbuck/core/util/kotlin/bin/kotlinc deleted file mode 100755 index b327cb16f..000000000 --- a/buildSrc/src/main/resources/com/uber/okbuck/core/util/kotlin/bin/kotlinc +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/env bash -# -# Modified for okbuck -############################################################################## -# Copyright 2002-2011, LAMP/EPFL -# Copyright 2011-2015, JetBrains -# -# This is free software; see the distribution for copying conditions. -# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. -############################################################################## - -cygwin=false; -case "`uname`" in - CYGWIN*) cygwin=true ;; -esac - -# Based on findScalaHome() from scalac script -findKotlinHome() { - local source="${BASH_SOURCE[0]}" - while [ -h "$source" ] ; do - local linked="$(readlink "$source")" - local dir="$(cd -P $(dirname "$source") && cd -P $(dirname "$linked") && pwd)" - source="$dir/$(basename "$linked")" - done - (cd -P "$(dirname "$source")/.." && pwd) -} - -KOTLIN_HOME="$(findKotlinHome)" - -if $cygwin; then - # Remove spaces from KOTLIN_HOME on windows - KOTLIN_HOME=`cygpath --windows --short-name "$KOTLIN_HOME"` -fi - -[ -n "$JAVA_OPTS" ] || JAVA_OPTS="-Xmx256M -Xms32M" - -declare -a java_args -declare -a kotlin_args - -while [ $# -gt 0 ]; do - case "$1" in - -D*) - java_args=("${java_args[@]}" "$1") - shift - ;; - -J*) - java_args=("${java_args[@]}" "${1:2}") - shift - ;; - *) - kotlin_args=("${kotlin_args[@]}" "$1") - shift - ;; - esac -done - -if [ -z "$JAVACMD" -a -n "$JAVA_HOME" -a -x "$JAVA_HOME/bin/java" ]; then - JAVACMD="$JAVA_HOME/bin/java" -fi - -declare -a kotlin_app - -KOTLIN_COMPILER=org.jetbrains.kotlin.cli.jvm.K2JVMCompiler -java_args=("${java_args[@]}" "-noverify") -kotlin_app=("${KOTLIN_HOME}/lib/kotlin-compiler-embeddable-${template-kotlin-version}.jar" $KOTLIN_COMPILER) - -"${JAVACMD:=java}" $JAVA_OPTS "${java_args[@]}" -cp "${kotlin_app[@]}" "${kotlin_args[@]}"