当前位置: 首页 > 工具软件 > Scala Native > 使用案例 >

scala入门_Scala-Native入门

蔚弘量
2023-12-01

scala入门

这只是我为使用scala-native使自己的helloworld运行而采取的步骤的快速回顾。 我还没有真正开始详细研究scala-native,但是其背后的想法真的很棒。 有关scala-native的一些常规输入可以在这里找到:

由于scala-native依赖于LLVM和Clang ++的可用性,因此此设置在您的环境中可能与在我的环境中有所不同。 我仍在使用OS-X Yosemite:

$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.10.3
BuildVersion:	14D136

但是对于Mac用户,步骤将几乎相同。 现在让我们开始设置环境,以便您可以运行scala-native提供的示例项目,并且我们还将创建一个基本的helloworld项目。 这些步骤再次适用于我的环境,您的环境可能有所不同。 您可以遵循几个github问题,或者查看有关如何设置环境的更多信息。

对我有用的步骤如下:

签出代码

我们当然首先需要获取代码,从命令行执行以下操作:

$ git clone --recursive https://github.com/scala-native/scala-native
Cloning into 'scala-native'...
remote: Counting objects: 8295, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 8295 (delta 3), reused 0 (delta 0), pack-reused 8278
Receiving objects: 100% (8295/8295), 1.31 MiB | 94.00 KiB/s, done.
Resolving deltas: 100% (2955/2955), done.

从理论上讲,这应该为您提供所有必需的资源。 但是,我遇到了第一个问题,即子模型scala没有正确拉出。 git想要以某种方式使用公钥身份验证,而不是使用https。 快速解决此问题的最简单方法是:

$ cd scala-native/submodules
$ git clone https://github.com/scala/scala
Cloning into 'scala'...
remote: Counting objects: 333614, done.
remote: Total 333614 (delta 0), reused 0 (delta 0), pack-reused 333613
Receiving objects: 100% (333614/333614), 83.31 MiB | 5.63 MiB/s, done.
Resolving deltas: 100% (226320/226320), done.
Checking connectivity... done.
Checking out files: 100% (10492/10492), done.
 
$ cd scala
$ git checkout 2.11.x

在本地发布库

此时,您将拥有所有必需的资源,但是当您尝试构建它时,可能会遇到以下问题:

# from the scala-native directory
$ sbt
> publish-local
... lots of output
[trace] Stack trace suppressed: run last scalalib/compile:compileIncremental for the full output.
[trace] Stack trace suppressed: run last javalib/compile:doc for the full output.
[error] (scalalib/compile:compileIncremental) java.lang.NoClassDefFoundError: scala/scalanative/nscplugin/NirGlobalAddons$nirPrimitives$
[error] (javalib/compile:doc) java.nio.charset.MalformedInputException: Input length = 1

某些scaladoc东西似乎不正确,这会导致我们的项目失败。 打开build.sbt文件并更改javalib项目。 为了解决这个问题,我们可以通过添加以下两行来禁用scaladoc内容:

sources in (Compile,doc) := Seq.empty,
publishArtifact in packageDoc := false

到这里:

83 lazy val javalib =
84   project.in(file("javalib")).
85     settings(libSettings,
86       sources in (Compile,doc) := Seq.empty,
87       publishArtifact in packageDoc := false
88     ).
89     dependsOn(nativelib)

现在再次进行本地发布,至少应该成功:

> reload
> publish-local
... again, lots of output
[info] 	published ivy to /Users/jos/.ivy2/local/org.scala-native/scalalib_2.11/0.1-SNAPSHOT/ivys/ivy.xml
[success] Total time: 17 s, completed May 14, 2016 4:06:31 PM

在这一点上,我们可以尝试运行演示应用程序,但是您可能会遇到以下两个错误:(至少我这样做了)

# This one:
/Users/jos/dev/git/scala-native-clean/scala-native/demo-native/target/scala-2.11/demonative-out.ll:152:40: error: expected '{' in function body
declare double @"llvm.sqrt.f64"(double)
                                       ^
 
# And this one
rt.cpp:3:10: fatal error: 'gc.h' file not found
#include <gc.h>

啊…不好。 还有其他问题。 第一个错误是由于LLVM版本不正确(要求为3.7,我的版本为3.6)引起的,第二个错误是因为Boehm GC库不可用。

正确设置LLVM和Clang ++

我不知道较新的OS-X安装上的版本是什么,但是我的仍然是3.6:

$ clang++ -v
Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.3.0
Thread model: posix

为了解决此问题,而又不弄乱其他依赖于3.6的工具,我使用brew安装了3.7版的LLVM和Clang ++。 如果您找不到llvm37,则可能需要先进行“ brew tap homebrew / versions”。

$ brew install llvm37 --with-clang
==> Installing llvm37 from homebrew/versions
==> Downloading https://homebrew.bintray.com/bottles-versions/llvm37-3.7.1.yosemite.bottle.1.tar.gz
Already downloaded: /Library/Caches/Homebrew/llvm37-3.7.1.yosemite.bottle.1.tar.gz
==> Pouring llvm37-3.7.1.yosemite.bottle.1.tar.gz
==> Caveats
Extra tools are installed in /usr/local/opt/llvm37/share/clang-3.7
 
To link to libc++, something like the following is required:
  CXX="clang++-3.7 -stdlib=libc++"
  CXXFLAGS="$CXXFLAGS -nostdinc++ -I/usr/local/opt/llvm37/lib/llvm-3.7/include/c++/v1"
  LDFLAGS="$LDFLAGS -L/usr/local/opt/llvm37/lib/llvm-3.7/lib"

您可以看到Clang和LLVM的安装目录。 在再次启动SBT之前,我们需要进行符号链接,以便插件可以找到正确的clang可执行文件。

$ cd /usr/local/opt/llvm37/bin
$ ln -s clang++-3.7 clang++

最后,我们只需要在启动SBT之前更新PATH变量以指向安装目录即可:

$ export PATH=/usr/local/opt/llvm37/bin:$PATH
$ clang++ -v
clang version 3.7.1 (tags/RELEASE_371/final)
Target: x86_64-apple-darwin14.3.0
Thread model: posix

在这一点上,我们应该遇到一个问题,所以让我们检查一下如果尝试运行演示会发生什么:

<br/>
# go back to the directory where you cloned scala-native
$ cd ~/dev/git/scala-native-clean/scala-native
$ sbt
> demoNative/run
[info] Updating {file:/Users/jos/dev/git/scala-native-clean/scala-native/}demoNative...
[info] Resolving org.scala-lang#scalap;2.11.8 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/jos/dev/git/scala-native-clean/scala-native/demo-native/target/scala-2.11/classes...
warning: overriding the module target triple with x86_64-apple-macosx10.10.0 [-Woverride-module]
1 warning generated.
/Users/jos/.scalanative/rtlib-0.1-SNAPSHOT/rt.cpp:3:10: fatal error: 'gc.h' file not found
#include <gc.h>
        ^
1 error generated.

如您所见,我们的错误少了一点,但是仍然有错误

安装Boehm GC库

Boehm GC库是缺少的,错误指向的是什么。 我们可以通过两种不同的方式来安装它。 我们可以使用预打包的brew实例,也可以直接下载源代码并以这种方式安装Boehm。 在此示例中,我们将进行后者。 这意味着按照此处的说明进行操作: http : //hboehm.info/gc/

$ mkdir boehm
$ cd boehm
$ git clone https://github.com/ivmai/libatomic_ops.git
$ git clone https://github.com/ivmai/bdwgc.git
$ cd bdgwc
$ ln -s ../libatomic_ops
$ autoreconf -vif
$ automake --add-missing
$ ./configure
$ make
$ sudo make install

或者,您也可以在brew中安装bdw-gc软件包。

$ brew install bdw-gc

然后,将以下build.sbt添加到带有其他clang设置的本地项目中:

$ cat build.sbt
nativeClangOptions := Seq("-I/usr/local/Cellar/bdw-gc/7.4.2/include", "-L/usr/local/Cellar/bdw-gc/7.4.2/lib")

但是,我更喜欢从源安装 *选项。 在这一点上,我们实际上应该能够运行该演示。

> demoNative/run
warning: overriding the module target triple with x86_64-apple-macosx10.10.0 [-Woverride-module]
1 warning generated.
Rendering (8 spp) 100.00%[success] Total time: 13 s, completed May 14, 2016 6:30:42 PM

哇……成功!!! 为了看到我们实际上正在运行代码,我们将对demoNative目录中的源代码进行一些小的更改。 在文件smallpt.scala中,将样本数更改为8,如下所示:

153   final val W = 800
154   final val H = 600
155   final val SAMPLES = 8
156   def main(args: Array[String]): Unit = {

现在再次运行该程序(这需要更多时间):

> demoNative/run
[info] Compiling 1 Scala source to /Users/jos/dev/git/scala-native-clean/scala-native/demo-native/target/scala-2.11/classes...
warning: overriding the module target triple with x86_64-apple-macosx10.10.0 [-Woverride-module]
1 warning generated.
Rendering (32 spp) 100.00%[success] Total time: 52 s, completed May 14, 2016 6:34:11 PM

它似乎正在工作。 顺便说一下,该程序的结果是一个3D渲染场景(示例程序是raytracer)。 如果将样本设置为较高的数量,则实际上可以获得相当不错的渲染:

我们还可以检查输出是否是真正的本机程序:

otool -L demonative-out
demonative-out:
	/usr/local/lib/libgc.1.dylib (compatibility version 2.0.0, current version 2.3.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)

现在让我们创建一个空的helloworld项目,在其中创建我们自己的最小本机标量程序。

Helloworld标量

在scala-native项目中,创建一个名称为helloworld-native的新目录。 在该目录中创建以下文件(** helloworld.scala **):

package helloworld
 
import scalanative.native._, stdlib._
 
object Main {
 
  def main(args: Array[String]): Unit = {
    fprintf(__stdoutp, c"Hello World: Scala Native!!!")
  }
}

在运行它之前,我们还需要更新主build.sbt文件并定义此项目。 为此,请添加以下内容:

lazy val helloworldNative =
  project.in(file("helloworld-native")).
    settings(libSettings).
    settings(
      nativeVerbose := true,
      nativeClangOptions := Seq("-O2")
    ).
    dependsOn(scalalib)

不要忘记Seq(“-I / usr / local / Cellar / bdw-gc / 7.4.2 / include”,“-L / usr / local / Cellar / bdw-gc / 7.4.2 / lib”)如果您不从头开始安装Boehm GC,则您的helloworld本机版本定义。

现在重新加载sbt,您应该可以运行我们自己的scala本地应用程序:

> helloworldNative/run
[info] Compiling 1 Scala source to /Users/jos/dev/git/scala-native-clean/scala-native/helloworld-native/target/scala-2.11/classes...
warning: overriding the module target triple with x86_64-apple-macosx10.10.0 [-Woverride-module]
1 warning generated.
Hello World: Scala Native!!!

或者我们可以直接从命令行运行它:

$ pwd
/Users/jos/dev/git/scala-native-clean/scala-native/helloworld-native/target/scala-2.11
$ ./helloworldnative-out
Hello World: Scala Native!!!

我希望您也可以从这个简短的介绍开始。 如果时间允许,我计划更深入地研究scala-native,并探索其某些功能。

翻译自: https://www.javacodegeeks.com/2016/05/getting-started-scala-native.html

scala入门

 类似资料: