Skip to content

Commit

Permalink
Merge pull request #8 in ANDROID/buck from no/kotlin-mixed to master
Browse files Browse the repository at this point in the history
* commit '6ce92651656d13d34fcb7eedaab3fbd8ccf84542':
  Add support for kotlin and java mixed sources.
  • Loading branch information
Christopher Woodward committed May 23, 2017
2 parents c2777b1 + 6ce9265 commit 18b5de7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
Expand Up @@ -41,7 +41,7 @@ public AndroidLibraryCompiler getCompiler(AndroidLibraryDescription.JvmLanguage
case SCALA:
return new ScalaAndroidLibraryCompiler(scalaConfig);
case KOTLIN:
return new KotlinAndroidLibraryCompiler(kotlinBuckConfig);
return new KotlinAndroidLibraryCompiler(kotlinBuckConfig, javaConfig);
}
throw new HumanReadableException("Unsupported `language` parameter value: %s", language);
}
Expand Down
14 changes: 13 additions & 1 deletion src/com/facebook/buck/android/KotlinAndroidLibraryCompiler.java
Expand Up @@ -17,19 +17,25 @@
package com.facebook.buck.android;

import com.facebook.buck.jvm.java.CompileToJarStepFactory;
import com.facebook.buck.jvm.java.JavaBuckConfig;
import com.facebook.buck.jvm.java.Javac;
import com.facebook.buck.jvm.java.JavacFactory;
import com.facebook.buck.jvm.java.JavacOptions;
import com.facebook.buck.jvm.kotlin.KotlinBuckConfig;
import com.facebook.buck.jvm.kotlin.KotlincToJarStepFactory;
import com.facebook.buck.rules.BuildRuleResolver;
import com.facebook.buck.rules.SourcePathRuleFinder;
import com.google.common.collect.ImmutableList;

public class KotlinAndroidLibraryCompiler extends AndroidLibraryCompiler {

private final KotlinBuckConfig kotlinBuckConfig;
private final JavaBuckConfig javaBuckConfig;

public KotlinAndroidLibraryCompiler(KotlinBuckConfig kotlinBuckConfig) {
public KotlinAndroidLibraryCompiler(KotlinBuckConfig kotlinBuckConfig, JavaBuckConfig javaBuckConfig) {
super();
this.kotlinBuckConfig = kotlinBuckConfig;
this.javaBuckConfig = javaBuckConfig;
}

@Override
Expand All @@ -45,6 +51,12 @@ public CompileToJarStepFactory compileToJar(
return new KotlincToJarStepFactory(
kotlinBuckConfig.getKotlinCompiler().get(),
ImmutableList.of(),
getJavac(resolver, args),
javacOptions,
ANDROID_CLASSPATH_FROM_CONTEXT);
}

private Javac getJavac(BuildRuleResolver resolver, AndroidLibraryDescription.CoreArg arg) {
return JavacFactory.create(new SourcePathRuleFinder(resolver), javaBuckConfig, arg);
}
}
Expand Up @@ -59,7 +59,7 @@ protected class BuilderHelper extends DefaultJavaLibraryBuilder.BuilderHelper {
protected CompileToJarStepFactory buildCompileStepFactory() {
return new KotlincToJarStepFactory(
Preconditions.checkNotNull(kotlinBuckConfig).getKotlinCompiler().get(),
extraKotlincArguments);
extraKotlincArguments, getJavac(), javacOptions);
}
}
}
49 changes: 47 additions & 2 deletions src/com/facebook/buck/jvm/kotlin/KotlincToJarStepFactory.java
Expand Up @@ -19,6 +19,10 @@
import com.facebook.buck.io.ProjectFilesystem;
import com.facebook.buck.jvm.java.BaseCompileToJarStepFactory;
import com.facebook.buck.jvm.java.ClassUsageFileWriter;
import com.facebook.buck.jvm.java.ClasspathChecker;
import com.facebook.buck.jvm.java.Javac;
import com.facebook.buck.jvm.java.JavacOptions;
import com.facebook.buck.jvm.java.JavacStep;
import com.facebook.buck.model.BuildTarget;
import com.facebook.buck.rules.BuildContext;
import com.facebook.buck.rules.BuildableContext;
Expand All @@ -30,25 +34,38 @@
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSortedSet;

import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.Optional;
import java.util.stream.Collectors;

public class KotlincToJarStepFactory extends BaseCompileToJarStepFactory {

private static final PathMatcher JAVA_PATH_MATCHER = FileSystems.getDefault()
.getPathMatcher("glob:**.java");

private final Tool kotlinc;
private final ImmutableList<String> extraArguments;
private final JavacOptions javacOptions;
private final Function<BuildContext, Iterable<Path>> extraClassPath;
private final Javac javac;

public KotlincToJarStepFactory(Tool kotlinc, ImmutableList<String> extraArguments) {
this(kotlinc, extraArguments, EMPTY_EXTRA_CLASSPATH);
public KotlincToJarStepFactory(Tool kotlinc, ImmutableList<String> extraArguments, Javac javac, JavacOptions javacOptions) {
this(kotlinc, extraArguments, javac, javacOptions, EMPTY_EXTRA_CLASSPATH);
}

public KotlincToJarStepFactory(
Tool kotlinc,
ImmutableList<String> extraArguments,
Javac javac,
JavacOptions javacOptions,
Function<BuildContext, Iterable<Path>> extraClassPath) {
this.kotlinc = kotlinc;
this.extraArguments = extraArguments;
this.javac = javac;
this.javacOptions = javacOptions;
this.extraClassPath = extraClassPath;
}

Expand Down Expand Up @@ -81,6 +98,34 @@ public void createCompileStep(
.addAll(declaredClasspathEntries)
.build(),
filesystem));

ImmutableSortedSet<Path> javaSourceFiles = ImmutableSortedSet.copyOf(
sourceFilePaths
.stream()
.filter(JAVA_PATH_MATCHER::matches)
.collect(Collectors.toSet()));

// Don't invoke javac if we don't have any java files.
if (!javaSourceFiles.isEmpty()) {
steps.add(new JavacStep(
outputDirectory,
usedClassesFileWriter,
workingDirectory,
javaSourceFiles,
pathToSrcsList,
ImmutableSortedSet.<Path>naturalOrder()
.add(outputDirectory)
.addAll(declaredClasspathEntries)
.build(),
javac,
javacOptions,
invokingRule,
resolver,
filesystem,
new ClasspathChecker(),
Optional.empty()
));
}
}

@Override
Expand Down

0 comments on commit 18b5de7

Please sign in to comment.