Skip to content

Commit

Permalink
Verify that nativelib compiles before publishing. (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
Duhemm authored and densh committed Nov 16, 2016
1 parent e843653 commit e81bd22
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
21 changes: 20 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,26 @@ lazy val sbtplugin =
.dependsOn(tools)

lazy val nativelib =
project.in(file("nativelib")).settings(libSettings).settings(publishSettings)
project
.in(file("nativelib"))
.settings(libSettings)
.settings(publishSettings)
.settings(compile in Compile := {
val clang = nativeClang.value
val clangpp = nativeClangPP.value
val source = baseDirectory.value
val compileSuccess =
IO.withTemporaryDirectory { tmp =>
IO.copyDirectory(baseDirectory.value, tmp)
scala.scalanative.sbtplugin.ScalaNativePluginInternal
.compileCSources(clang, clangpp, tmp)
}
if (compileSuccess) {
(compile in Compile).value
} else {
error("Compilation failed")
}
})

lazy val javalib =
project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,23 @@ object ScalaNativePluginInternal {
compiler.apply()
}

/** Compiles *.c[pp] in `cwd`. */
def compileCSources(clang: File, clangpp: File, cwd: File): Boolean = {
val cpaths = (cwd ** "*.c").get.map(abs)
val cpppaths = (cwd ** "*.cpp").get.map(abs)
val compilec = abs(clang) +: (includes ++ ("-c" +: cpaths))
val compilecpp = abs(clangpp) +: (includes ++ ("-c" +: cpppaths))

val cExit = Process(compilec, cwd).!
val cppExit = Process(compilecpp, cwd).!

cExit == 0 && cppExit == 0
}

/** Compiles rt to llvm ir using clang. */
private def unpackRtlib(clang: File,
clangpp: File,
classpath: Seq[String]): Unit = {
classpath: Seq[String]): Boolean = {
val nativelibjar = classpath.collectFirst {
case p if p.contains("scala-native") && p.contains("nativelib") =>
file(p)
Expand All @@ -71,13 +84,9 @@ object ScalaNativePluginInternal {
IO.unzip(nativelibjar, nativelib)
IO.write(jarhashfile, Hash(nativelibjar))

val cpaths = (nativelib ** "*.c").get.map(abs)
val cpppaths = (nativelib ** "*.cpp").get.map(abs)
val compilec = abs(clang) +: (includes ++ ("-c" +: cpaths))
val compilecpp = abs(clangpp) +: (includes ++ ("-c" +: cpppaths))

Process(compilec, nativelib).!
Process(compilecpp, nativelib).!
compileCSources(clang, clangpp, nativelib)
} else {
true
}
}

Expand Down Expand Up @@ -162,11 +171,16 @@ object ScalaNativePluginInternal {
checkThatClangIsRecentEnough(clang)

IO.createDirectory(target)
unpackRtlib(clang, clangpp, classpath)
val links = compileNir(opts).map(_.name)
compileLl(clangpp, target, appll, binary, links, linkage, clangOpts)
val unpackSuccess = unpackRtlib(clang, clangpp, classpath)

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

binary
binary
} else {
throw new MessageOnlyException("Couldn't unpack nativelib.")
}
},
run := {
val log = streams.value.log
Expand Down

0 comments on commit e81bd22

Please sign in to comment.