Skip to content

Commit

Permalink
Cache result of nativeLink
Browse files Browse the repository at this point in the history
So that it won't be run again if no input file has changed and if none
of the relevant scala-native options has changed.
  • Loading branch information
Duhemm authored and densh committed Nov 22, 2016
1 parent e5811ba commit d7078df
Showing 1 changed file with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,44 @@ object ScalaNativePluginInternal {

checkThatClangIsRecentEnough(clang)

IO.createDirectory(target)
val unpackSuccess = unpackRtlib(clang, clangpp, classpath)

if (unpackSuccess) {
val links = compileNir(opts).map(_.name)
compileLl(clangpp, target, appll, binary, links, linkage, clangOpts)

binary
} else {
throw new MessageOnlyException("Couldn't unpack nativelib.")
}
val nirFiles = (Keys.target.value ** "*.nir").get.toSet
val configFile = (streams.value.cacheDirectory / "native-config")
val inputFiles = nirFiles + configFile

writeConfigHash(configFile,
opts,
clang,
clangpp,
classpath,
target,
appll,
binary,
linkage,
clangOpts)

val compileIfChanged =
FileFunction.cached(streams.value.cacheDirectory / "native-cache",
FilesInfo.hash) {
_ =>
IO.createDirectory(target)
val unpackSuccess = unpackRtlib(clang, clangpp, classpath)
if (unpackSuccess) {
val links = compileNir(opts).map(_.name)
compileLl(clangpp,
target,
appll,
binary,
links,
linkage,
clangOpts)
Set(binary)
} else {
throw new MessageOnlyException("Couldn't unpack nativelib.")
}
}

val _ = compileIfChanged(inputFiles)
binary
},
run := {
val log = streams.value.log
Expand All @@ -198,6 +225,11 @@ object ScalaNativePluginInternal {
}
)

private def writeConfigHash(file: File, config: Any*): Unit = {
val _ = config.## // Force evaluation of lazy structures
IO.write(file, Hash(config.toString))
}

private def maybeInjectShared(lib: Boolean): Seq[String] =
if (lib) Seq("-shared") else Seq.empty

Expand Down

0 comments on commit d7078df

Please sign in to comment.