Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AuxiliaryClassFiles in IncOptions #956

Merged
merged 3 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ type IncOptions {
classfileManagerType: xsbti.compile.ClassFileManagerType = raw"xsbti.compile.IncOptions.defaultClassFileManagerType()"
@since("0.1.0")

## Associate each class file with corresponding files (eg. .tasty files) that must be managed by the ClassfileManager.
auxiliaryClassFiles: [xsbti.compile.AuxiliaryClassFiles] = raw"xsbti.compile.IncOptions.defaultAuxiliaryClassFiles()"
@since("1.5.0")

## Option to turn on customized file manager that tracks generated class files for transactional rollbacks.
## Using customized file manager may conflict with some libraries, this option allows user to decide
## whether to use.
Expand Down Expand Up @@ -122,6 +126,9 @@ type IncOptions {
#x public static java.util.Optional<ClassFileManagerType> defaultClassFileManagerType() {
#x return java.util.Optional.empty();
#x }
#x public static xsbti.compile.AuxiliaryClassFiles[] defaultAuxiliaryClassFiles() {
#x return new xsbti.compile.AuxiliaryClassFiles[0];
#x }
#x public static java.util.Optional<Boolean> defaultRecompileOnMacroDef() {
#x return java.util.Optional.empty();
#x }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package xsbti.compile;

import java.nio.file.Path;

/**
* Interface that associates a class file with other corresponding produced
* files, for instance `.tasty` files or `.sjsir` files.
*
* This class is meant to be used inside the [[ClassFileManager]] to manage
* auxiliary files.
*
* ClassFileManagers are forgiving on auxiliary files that do not exist.
*/
public interface AuxiliaryClassFiles {
Path[] associatedFiles(Path classFile);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ package xsbt

import net.openhft.affinity.AffinityLock
import org.openjdk.jmh.annotations._

import java.io.File
import sbt.inc.{ CompilerSetup, ProjectSetup }
import sbt.internal.inc.BridgeProviderSpecification
import sbt.util.Logger

import xsbt.ZincBenchmark.CompilationInfo
import xsbti.compile.IncOptions

@State(Scope.Benchmark)
class BenchmarkBase extends BridgeProviderSpecification {
Expand Down Expand Up @@ -61,8 +62,8 @@ class BenchmarkBase extends BridgeProviderSpecification {
val scalaVersion = ZincBenchmark.scalaVersion
val bridge = getCompilerBridge(_dir.toPath, noLogger, scalaVersion)
val si = scalaInstance(scalaVersion, _dir.toPath, noLogger)
val pipelining = false
_compilerSetup = _setup.createCompiler(scalaVersion, si, bridge, pipelining, log)
val options = IncOptions.of()
_compilerSetup = _setup.createCompiler(scalaVersion, si, bridge, options, log)
printCompilationDetails()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package xsbti.compile;

import java.nio.file.Path;

public class AuxiliaryClassFileExtension implements AuxiliaryClassFiles {
private final String dotExtension;

public AuxiliaryClassFileExtension(String extension) {
this.dotExtension = "." + extension;
}

@Override
public Path[] associatedFiles(Path classFile) {
// not all class files must have a corresponding auxiliary file
// we strongly assume that ClassFileManager is forgiving on files that do not exist.
String fileName = classFile.getFileName().toString();
if (fileName.endsWith(".class")) {
String prefix = fileName.substring(0, fileName.length() - 6);
Path auxiliaryFile = classFile.resolveSibling(prefix + dotExtension);
return new Path[] { auxiliaryFile };
} else {
return new Path[0];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ public final class ClassFileManagerUtil {
* @return A classfile manager implementation.
*/
public static ClassFileManager getDefaultClassFileManager(ClassFileManagerType classFileManagerType) {
return sbt.internal.inc.ClassFileManager.getDefaultClassFileManager(Optional.of(classFileManagerType));
return sbt.internal.inc.ClassFileManager.getDefaultClassFileManager(
Optional.of(classFileManagerType), new AuxiliaryClassFiles[0]);
}

/**
* Get the default classfile manager implementation for a given classfile manager type,
* extracted from an instance of {@link IncOptions}.
*
* @param classFileManagerType The classfile manager type.
* @param incOptions The Incremental compiler options.
* @return A classfile manager implementation.
*/
public static ClassFileManager getDefaultClassFileManager(IncOptions incOptions) {
return sbt.internal.inc.ClassFileManager.getDefaultClassFileManager(incOptions.classfileManagerType());
return sbt.internal.inc.ClassFileManager.getDefaultClassFileManager(
incOptions.classfileManagerType(), incOptions.auxiliaryClassFiles());
}
}
17 changes: 17 additions & 0 deletions internal/zinc-core/src/main/java/xsbti/compile/ScalaJSFiles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package xsbti.compile;

/**
* Can be added to `IncOptions.auxiliaryClassFiles` so that the sjsir files
* produced by the Scala.js compiler are managed by the ClassFileManager.
*/
public class ScalaJSFiles extends AuxiliaryClassFileExtension {
private static final ScalaJSFiles _instance = new ScalaJSFiles();

public static ScalaJSFiles instance() {
return _instance;
}

private ScalaJSFiles() {
super("sjsir");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package xsbti.compile;

/**
* Can be added to `IncOptions.auxiliaryClassFiles` so that the nir files
* produced by the Scala Native compiler are managed by the ClassFileManager.
*/
public class ScalaNativeFiles extends AuxiliaryClassFileExtension {
private static final ScalaNativeFiles _instance = new ScalaNativeFiles();

public static ScalaNativeFiles instance() {
return _instance;
}

private ScalaNativeFiles() {
super("nir");
}
}
17 changes: 17 additions & 0 deletions internal/zinc-core/src/main/java/xsbti/compile/TastyFiles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package xsbti.compile;

/**
* Can be added to `IncOptions.auxiliaryClassFiles` so that the TASTy files are
* managed by the ClassFileManager.
*/
public class TastyFiles extends AuxiliaryClassFileExtension {
private static final TastyFiles _instance = new TastyFiles();

public static TastyFiles instance() {
return _instance;
}

private TastyFiles() {
super("tasty");
}
}
Loading