-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run native linker only if project was changed
- Loading branch information
Showing
7 changed files
with
401 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
modules/cli/src/main/scala/scala/cli/internal/NativeBuilderHelper.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package scala.cli.internal | ||
|
||
import java.math.BigInteger | ||
import java.security.MessageDigest | ||
|
||
import scala.build.Build | ||
import scala.scalanative.{build => sn} | ||
|
||
case object NativeBuilderHelper { | ||
|
||
private def resolveProjectShaPath(nativeWorkDir: os.Path) = nativeWorkDir / ".project_sha" | ||
private def resolveOutputShaPath(nativeWorkDir: os.Path) = nativeWorkDir / ".output_sha" | ||
|
||
private def fileSha(filePath: os.Path): String = { | ||
val md = MessageDigest.getInstance("SHA-1") | ||
md.update(os.read.bytes(filePath)) | ||
|
||
val digest = md.digest() | ||
val calculatedSum = new BigInteger(1, digest) | ||
String.format(s"%040x", calculatedSum) | ||
} | ||
|
||
private def projectSha(build: Build.Successful, nativeConfig: sn.NativeConfig) = { | ||
val md = MessageDigest.getInstance("SHA-1") | ||
md.update(build.inputs.sourceHash().getBytes) | ||
md.update(nativeConfig.toString.getBytes) | ||
md.update(build.options.hash.getOrElse("").getBytes) | ||
|
||
val digest = md.digest() | ||
val calculatedSum = new BigInteger(1, digest) | ||
String.format(s"%040x", calculatedSum) | ||
} | ||
|
||
def updateOutputSha(dest: os.Path, nativeWorkDir: os.Path) = { | ||
val outputShaPath = resolveOutputShaPath(nativeWorkDir) | ||
val sha = fileSha(dest) | ||
os.write.over(outputShaPath, sha) | ||
} | ||
|
||
def buildIfChanged( | ||
build: Build.Successful, | ||
nativeConfig: sn.NativeConfig, | ||
dest: os.Path, | ||
nativeWorkDir: os.Path | ||
): Boolean = { | ||
val projectShaPath = resolveProjectShaPath(nativeWorkDir) | ||
val outputShaPath = resolveOutputShaPath(nativeWorkDir) | ||
|
||
val currentProjectSha = projectSha(build, nativeConfig) | ||
val currentOutputSha = if (os.exists(dest)) Some(fileSha(dest)) else None | ||
|
||
val previousProjectSha = if (os.exists(projectShaPath)) Some(os.read(projectShaPath)) else None | ||
val previousOutputSha = if (os.exists(outputShaPath)) Some(os.read(outputShaPath)) else None | ||
|
||
val changed = | ||
!previousProjectSha.contains(currentProjectSha) || | ||
previousOutputSha != currentOutputSha || | ||
!os.exists(dest) | ||
|
||
// update sha in .projectShaPath | ||
if (changed) os.write.over(projectShaPath, currentProjectSha, createFolders = true) | ||
|
||
changed | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
modules/cli/src/test/scala/cli/tests/internal/BuildTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package cli.tests.internal | ||
|
||
import dependency.{ScalaParameters, ScalaVersion} | ||
|
||
import java.io.IOException | ||
import java.nio.charset.StandardCharsets | ||
import java.security.SecureRandom | ||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
import scala.build.internal.Constants | ||
import scala.build.options.{BuildOptions, Scope} | ||
import scala.build.{Artifacts, Build, Inputs, Project, ScalaCompiler, Sources} | ||
import scala.util.control.NonFatal | ||
|
||
final case class BuildTests( | ||
files: Seq[(os.RelPath, String)], | ||
options: BuildOptions = BuildOptions() | ||
) { | ||
|
||
def withClangPath(clangPath: os.Path) = copy( | ||
options = options.copy( | ||
scalaNativeOptions = options.scalaNativeOptions.copy( | ||
clang = Some(clangPath.toString()) | ||
) | ||
) | ||
) | ||
|
||
def withClangppPath(clangppPath: os.Path) = copy( | ||
options = options.copy( | ||
scalaNativeOptions = options.scalaNativeOptions.copy( | ||
clangpp = Some(clangppPath.toString()) | ||
) | ||
) | ||
) | ||
|
||
private def writeIn(dir: os.Path): Unit = | ||
for ((relPath, content) <- files) { | ||
val path = dir / relPath | ||
os.write(path, content.getBytes(StandardCharsets.UTF_8), createFolders = true) | ||
} | ||
def root(): os.Path = { | ||
val tmpDir = BuildTests.tmpDir | ||
writeIn(tmpDir) | ||
tmpDir | ||
} | ||
def fromRoot[T](f: (os.Path, Build.Successful) => T): T = | ||
BuildTests.withTmpDir { tmpDir => | ||
writeIn(tmpDir) | ||
f(tmpDir, generateBuilds(tmpDir)) | ||
} | ||
|
||
private def inputs(workDir: os.Path): Inputs = Inputs.empty(workDir).copy(elements = files.map { | ||
case (relPath, _) => Inputs.ScalaFile(workDir, relPath.asSubPath) | ||
}) | ||
|
||
private val emptyArtifacts: Artifacts = Artifacts( | ||
compilerDependencies = Seq.empty, | ||
compilerArtifacts = Seq.empty, | ||
compilerPlugins = Seq.empty, | ||
dependencies = Seq.empty, | ||
detailedArtifacts = Seq.empty, | ||
extraClassPath = Seq.empty, | ||
extraCompileOnlyJars = Seq.empty, | ||
extraSourceJars = Seq.empty, | ||
params = ScalaParameters(BuildTests.defaultScalaVersion) | ||
) | ||
|
||
private def emptyProject(workDir: os.Path): Project = Project( | ||
workspace = workDir, | ||
classesDir = workDir, | ||
scalaCompiler = ScalaCompiler( | ||
BuildTests.defaultScalaVersion, | ||
ScalaVersion.binary(BuildTests.defaultScalaVersion), | ||
Seq.empty, | ||
Seq.empty | ||
), | ||
scalaJsOptions = None, | ||
scalaNativeOptions = None, | ||
projectName = "build-test", | ||
classPath = Seq.empty, | ||
sources = Seq.empty, | ||
resolution = None, | ||
resourceDirs = Seq.empty, | ||
javaHomeOpt = None, | ||
scope = Scope.Main, | ||
javacOptions = Nil | ||
) | ||
|
||
private def generateBuilds(workDir: os.Path): Build.Successful = Build.Successful( | ||
inputs = inputs(workDir), | ||
options = options, | ||
scalaParams = ScalaParameters(BuildTests.defaultScalaVersion), | ||
scope = Scope.Main, | ||
sources = Sources.empty, | ||
artifacts = emptyArtifacts, | ||
project = emptyProject(workDir), | ||
output = workDir, | ||
diagnostics = None | ||
) | ||
|
||
} | ||
|
||
object BuildTests { | ||
|
||
lazy val defaultScalaVersion = Constants.defaultScalaVersion | ||
|
||
private lazy val baseTmpDir = { | ||
val base = os.temp.dir() | ||
val rng = new SecureRandom | ||
val d = base / s"run-${math.abs(rng.nextInt().toLong)}" | ||
os.makeDir.all(d) | ||
Runtime.getRuntime.addShutdownHook( | ||
new Thread("scala-cli-its-clean-up-tmp-dir") { | ||
setDaemon(true) | ||
override def run(): Unit = | ||
try os.remove.all(d) | ||
catch { | ||
case NonFatal(_) => | ||
System.err.println(s"Could not remove $d, ignoring it.") | ||
} | ||
} | ||
) | ||
d | ||
} | ||
|
||
private val tmpCount = new AtomicInteger | ||
|
||
private def withTmpDir[T](f: os.Path => T): T = { | ||
val tmpDir = baseTmpDir / s"test-${tmpCount.incrementAndGet()}" | ||
os.makeDir.all(tmpDir) | ||
val tmpDir0 = os.Path(tmpDir.toIO.getCanonicalFile) | ||
def removeAll(): Unit = | ||
try os.remove.all(tmpDir0) | ||
catch { | ||
case ex: IOException => | ||
System.err.println(s"Ignoring $ex while removing $tmpDir0") | ||
} | ||
try f(tmpDir0) | ||
finally removeAll() | ||
} | ||
|
||
private def tmpDir: os.Path = { | ||
val tmpDir = baseTmpDir / s"test-${tmpCount.incrementAndGet()}" | ||
os.makeDir.all(tmpDir) | ||
os.Path(tmpDir.toIO.getCanonicalFile) | ||
} | ||
} |
Oops, something went wrong.