blob: efb39aae7b0093b924808f8e402d30ecccf6c2d8 [file] [log] [blame]
Sebastien Hertz9006e9c2017-09-11 11:03:26 +02001// 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
5package kotlin;
6
7import java.io.File;
8import java.io.IOException;
9import java.nio.file.Path;
10import java.nio.file.Paths;
11import org.gradle.api.Action;
12import org.gradle.api.DefaultTask;
13import org.gradle.api.UncheckedIOException;
Ian Zerny793d9322018-12-18 13:24:25 +010014import org.gradle.api.file.FileCollection;
Sebastien Hertz9006e9c2017-09-11 11:03:26 +020015import org.gradle.api.file.FileTree;
16import org.gradle.api.tasks.InputFiles;
17import org.gradle.api.tasks.OutputFile;
18import org.gradle.api.tasks.TaskAction;
19import org.gradle.process.ExecSpec;
20import utils.Utils;
21
22/**
Sebastien Hertzfe97a712018-02-13 12:08:59 +010023 * Gradle task to compile Kotlin source files. By default the generated classes target Java 1.6.
Sebastien Hertz9006e9c2017-09-11 11:03:26 +020024 */
25public class Kotlinc extends DefaultTask {
26
Ian Zerny793d9322018-12-18 13:24:25 +010027 private static final String kotlincExecName = Utils.toolsDir().equals("windows")
28 ? "kotlinc.bat"
29 : "kotlinc";
30
Morten Krogh-Jespersenb328dc62020-05-12 09:11:52 +020031 private static final Path kotlincExecPath =
32 Paths.get(
Morten Krogh-Jespersen3e956732020-05-12 10:56:28 +020033 "third_party", "kotlin", "kotlin-compiler-1.3.72", "kotlinc", "bin", kotlincExecName);
Ian Zerny793d9322018-12-18 13:24:25 +010034
Sebastien Hertzfe97a712018-02-13 12:08:59 +010035 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 Hertz9006e9c2017-09-11 11:03:26 +020046 private FileTree source;
47
48 @OutputFile
49 private File destination;
50
Sebastien Hertzfe97a712018-02-13 12:08:59 +010051 private KotlinTargetVersion targetVersion = KotlinTargetVersion.JAVA_6;
52
Ian Zerny793d9322018-12-18 13:24:25 +010053 @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 Hertz9006e9c2017-09-11 11:03:26 +020060 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 Hertzfe97a712018-02-13 12:08:59 +010076 public KotlinTargetVersion getTargetVersion() {
77 return targetVersion;
78 }
79
80 public void setTargetVersion(KotlinTargetVersion targetVersion) {
81 this.targetVersion = targetVersion;
82 }
83
Sebastien Hertz9006e9c2017-09-11 11:03:26 +020084 @TaskAction
85 public void compile() {
86 getProject().exec(new Action<ExecSpec>() {
87 @Override
88 public void execute(ExecSpec execSpec) {
89 try {
Sebastien Hertz9006e9c2017-09-11 11:03:26 +020090 execSpec.setExecutable(kotlincExecPath.toFile());
91 execSpec.args("-include-runtime");
92 execSpec.args("-nowarn");
Sebastien Hertzfe97a712018-02-13 12:08:59 +010093 execSpec.args("-jvm-target", targetVersion.optionName);
Sebastien Hertz9006e9c2017-09-11 11:03:26 +020094 execSpec.args("-d", destination.getCanonicalPath());
95 execSpec.args(source.getFiles());
96 } catch (IOException e) {
97 throw new UncheckedIOException(e);
98 }
99 }
100 });
101 }
102}