Skip to content

Commit

Permalink
Merge pull request #358 from scala-native/topic/fix-321
Browse files Browse the repository at this point in the history
Fix #321
  • Loading branch information
densh committed Oct 26, 2016
2 parents 40ce593 + 5d4e5d0 commit fba450e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 35 deletions.
4 changes: 4 additions & 0 deletions nativelib/src/main/resources/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// At the moment we rely on the conservative
// mode of Boehm GC as our garbage collector.

void scalanative_init() {
GC_init();
}

void* scalanative_alloc(void* info, size_t size) {
void** alloc = (void**) GC_malloc(size);
*alloc = info;
Expand Down
2 changes: 0 additions & 2 deletions nativelib/src/main/scala/scala/scalanative/runtime/GC.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,4 @@ object GC {
def malloc(size: CSize): Ptr[_] = extern
@name("GC_malloc_atomic")
def malloc_atomic(size: CSize): Ptr[_] = extern
@name("GC_init")
def init(): Unit = extern
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ package object runtime {
* rest as Java-style array.
*/
def init(argc: Int, argv: Ptr[Ptr[Byte]]): ObjectArray = {
GC.init()

val args = new scala.Array[String](argc - 1)

// skip the executable name in argv(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ object ScalaNativePluginInternal {
private lazy val nativelib: File =
Path.userHome / ".scalanative" / ("nativelib-" + nir.Versions.current)

private lazy val includes = {
val includedir =
Try(Process("llvm-config --includedir").lines_!.toSeq)
.getOrElse(Seq.empty)
("/usr/local/include" +: includedir).map(s => s"-I$s")
}

private lazy val libs = {
val libdir =
Try(Process("llvm-config --libdir").lines_!.toSeq).getOrElse(Seq.empty)
("/usr/local/lib" +: libdir).map(s => s"-L$s")
}

private def abs(file: File): String =
file.getAbsolutePath

Expand Down Expand Up @@ -60,8 +73,8 @@ object ScalaNativePluginInternal {

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

Process(compilec, nativelib).!
Process(compilecpp, nativelib).!
Expand Down Expand Up @@ -102,24 +115,21 @@ object ScalaNativePluginInternal {
"org.scala-native" % "nscplugin" % nativeVersion cross CrossVersion.full),
resolvers += Resolver.sonatypeRepo("snapshots"),
nativeVerbose := false,
nativeClang := discover("clang", Seq(("3", "8"), ("3", "7"))),
nativeClangPP := discover("clang++", Seq(("3", "8"), ("3", "7"))),
nativeClangOptions := {
val includes = ("/usr/local/include" #:: Try(
Process("llvm-config --includedir").lines_!).getOrElse(Stream.empty))
.map(s => s"-I$s")
val libs =
("/usr/local/lib" #:: Try(Process("llvm-config --libdir").lines_!)
.getOrElse(Stream.empty)).map(s => s"-L$s")

includes #::: libs ++ maybeInjectShared(nativeSharedLibrary.value)
},
nativeEmitDependencyGraphPath := None,
nativeLibraryLinkage := Map(),
nativeSharedLibrary := false,
nativeClang := {
discover("clang", Seq(("3", "8"), ("3", "7")))
},
nativeClangPP := {
discover("clang++", Seq(("3", "8"), ("3", "7")))
},
nativeClangOptions := {
includes ++ libs ++ maybeInjectShared(nativeSharedLibrary.value)
},
artifactPath in nativeLink := {
(crossTarget in Compile).value / (moduleName.value + "-out")
},
nativeSharedLibrary := false,
nativeLink := {
val mainClass = (selectMainClass in Compile).value.getOrElse(
throw new MessageOnlyException("No main class detected.")
Expand Down
1 change: 0 additions & 1 deletion scalafmt.tar.gz

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ class MainInjection(entry: Global)(implicit fresh: Fresh) extends Pass {

defns :+ Defn.Define(
Attrs.None,
mainName,
mainSig,
Seq(Inst.Label(fresh(), Seq(argc, argv)),
Inst.Let(rt.name, Op.Module(Rt.name)),
Inst.Let(arr.name, Op.Call(initSig, init, Seq(rt, argc, argv))),
Inst.Let(module.name, Op.Module(entry.top)),
Inst.Let(Op.Call(mainTy, main, Seq(module, arr))),
Inst.Ret(Val.I32(0))))
MainName,
MainSig,
Seq(
Inst.Label(fresh(), Seq(argc, argv)),
Inst.Let(Op.Call(InitSig, Init, Seq())),
Inst.Let(rt.name, Op.Module(Rt.name)),
Inst.Let(arr.name, Op.Call(RtInitSig, RtInit, Seq(rt, argc, argv))),
Inst.Let(module.name, Op.Module(entry.top)),
Inst.Let(Op.Call(mainTy, main, Seq(module, arr))),
Inst.Ret(Val.I32(0))))
}
}

Expand All @@ -43,14 +45,20 @@ object MainInjection extends PassCompanion {
val ObjectArray =
Type.Class(Global.Top("scala.scalanative.runtime.ObjectArray"))

val Rt = Type.Module(Global.Top("scala.scalanative.runtime.package$"))
val initName = Rt.name member "init_i32_ptr_class.ssnr.ObjectArray"
val initSig =
val Rt =
Type.Module(Global.Top("scala.scalanative.runtime.package$"))
val RtInitSig =
Type.Function(Seq(Arg(Rt), Arg(Type.I32), Arg(Type.Ptr)), ObjectArray)
val init = Val.Global(initName, initSig)
val RtInit =
Val.Global(Rt.name member "init_i32_ptr_class.ssnr.ObjectArray", Type.Ptr)

val mainName = Global.Top("main")
val mainSig = Type.Function(Seq(Arg(Type.I32), Arg(Type.Ptr)), Type.I32)
val MainName = Global.Top("main")
val MainSig = Type.Function(Seq(Arg(Type.I32), Arg(Type.Ptr)), Type.I32)

override val depends = Seq(ObjectArray.name, Rt.name, init.name)
val InitSig = Type.Function(Seq(), Type.Unit)
val Init = Val.Global(Global.Top("scalanative_init"), Type.Ptr)
val InitDecl = Defn.Declare(Attrs.None, Init.name, InitSig)

override val depends = Seq(ObjectArray.name, Rt.name, RtInit.name)
override val injects = Seq(InitDecl)
}

0 comments on commit fba450e

Please sign in to comment.