Sebastien Hertz | 9006e9c | 2017-09-11 11:03:26 +0200 | [diff] [blame] | 1 | // Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | package kotlin; |
| 6 | |
| 7 | import java.io.File; |
| 8 | import java.io.IOException; |
| 9 | import java.nio.file.Path; |
| 10 | import java.nio.file.Paths; |
| 11 | import org.gradle.api.Action; |
| 12 | import org.gradle.api.DefaultTask; |
| 13 | import org.gradle.api.UncheckedIOException; |
Ian Zerny | 793d932 | 2018-12-18 13:24:25 +0100 | [diff] [blame] | 14 | import org.gradle.api.file.FileCollection; |
Sebastien Hertz | 9006e9c | 2017-09-11 11:03:26 +0200 | [diff] [blame] | 15 | import org.gradle.api.file.FileTree; |
| 16 | import org.gradle.api.tasks.InputFiles; |
| 17 | import org.gradle.api.tasks.OutputFile; |
| 18 | import org.gradle.api.tasks.TaskAction; |
| 19 | import org.gradle.process.ExecSpec; |
| 20 | import utils.Utils; |
| 21 | |
| 22 | /** |
Sebastien Hertz | fe97a71 | 2018-02-13 12:08:59 +0100 | [diff] [blame] | 23 | * Gradle task to compile Kotlin source files. By default the generated classes target Java 1.6. |
Sebastien Hertz | 9006e9c | 2017-09-11 11:03:26 +0200 | [diff] [blame] | 24 | */ |
| 25 | public class Kotlinc extends DefaultTask { |
| 26 | |
Ian Zerny | 793d932 | 2018-12-18 13:24:25 +0100 | [diff] [blame] | 27 | private static final String kotlincExecName = Utils.toolsDir().equals("windows") |
| 28 | ? "kotlinc.bat" |
| 29 | : "kotlinc"; |
| 30 | |
Morten Krogh-Jespersen | b328dc6 | 2020-05-12 09:11:52 +0200 | [diff] [blame] | 31 | private static final Path kotlincExecPath = |
| 32 | Paths.get( |
Morten Krogh-Jespersen | 3e95673 | 2020-05-12 10:56:28 +0200 | [diff] [blame] | 33 | "third_party", "kotlin", "kotlin-compiler-1.3.72", "kotlinc", "bin", kotlincExecName); |
Ian Zerny | 793d932 | 2018-12-18 13:24:25 +0100 | [diff] [blame] | 34 | |
Sebastien Hertz | fe97a71 | 2018-02-13 12:08:59 +0100 | [diff] [blame] | 35 | enum KotlinTargetVersion { |
| 36 | JAVA_6("1.6"), |
| 37 | JAVA_8("1.8"); |
| 38 | |
| 39 | private final String optionName; |
| 40 | |
| 41 | KotlinTargetVersion(String optionName) { |
| 42 | this.optionName = optionName; |
| 43 | } |
| 44 | } |
| 45 | |
Sebastien Hertz | 9006e9c | 2017-09-11 11:03:26 +0200 | [diff] [blame] | 46 | private FileTree source; |
| 47 | |
| 48 | @OutputFile |
| 49 | private File destination; |
| 50 | |
Sebastien Hertz | fe97a71 | 2018-02-13 12:08:59 +0100 | [diff] [blame] | 51 | private KotlinTargetVersion targetVersion = KotlinTargetVersion.JAVA_6; |
| 52 | |
Ian Zerny | 793d932 | 2018-12-18 13:24:25 +0100 | [diff] [blame] | 53 | @InputFiles |
| 54 | public FileCollection getInputFiles() { |
| 55 | // Note: Using Path object directly causes stack overflow. |
| 56 | // See: https://github.com/gradle/gradle/issues/1973 |
| 57 | return source.plus(getProject().files(kotlincExecPath.toFile())); |
| 58 | } |
| 59 | |
Sebastien Hertz | 9006e9c | 2017-09-11 11:03:26 +0200 | [diff] [blame] | 60 | public FileTree getSource() { |
| 61 | return source; |
| 62 | } |
| 63 | |
| 64 | public void setSource(FileTree source) { |
| 65 | this.source = source; |
| 66 | } |
| 67 | |
| 68 | public File getDestination() { |
| 69 | return destination; |
| 70 | } |
| 71 | |
| 72 | public void setDestination(File destination) { |
| 73 | this.destination = destination; |
| 74 | } |
| 75 | |
Sebastien Hertz | fe97a71 | 2018-02-13 12:08:59 +0100 | [diff] [blame] | 76 | public KotlinTargetVersion getTargetVersion() { |
| 77 | return targetVersion; |
| 78 | } |
| 79 | |
| 80 | public void setTargetVersion(KotlinTargetVersion targetVersion) { |
| 81 | this.targetVersion = targetVersion; |
| 82 | } |
| 83 | |
Sebastien Hertz | 9006e9c | 2017-09-11 11:03:26 +0200 | [diff] [blame] | 84 | @TaskAction |
| 85 | public void compile() { |
| 86 | getProject().exec(new Action<ExecSpec>() { |
| 87 | @Override |
| 88 | public void execute(ExecSpec execSpec) { |
| 89 | try { |
Sebastien Hertz | 9006e9c | 2017-09-11 11:03:26 +0200 | [diff] [blame] | 90 | execSpec.setExecutable(kotlincExecPath.toFile()); |
| 91 | execSpec.args("-include-runtime"); |
| 92 | execSpec.args("-nowarn"); |
Sebastien Hertz | fe97a71 | 2018-02-13 12:08:59 +0100 | [diff] [blame] | 93 | execSpec.args("-jvm-target", targetVersion.optionName); |
Sebastien Hertz | 9006e9c | 2017-09-11 11:03:26 +0200 | [diff] [blame] | 94 | execSpec.args("-d", destination.getCanonicalPath()); |
| 95 | execSpec.args(source.getFiles()); |
| 96 | } catch (IOException e) { |
| 97 | throw new UncheckedIOException(e); |
| 98 | } |
| 99 | } |
| 100 | }); |
| 101 | } |
| 102 | } |