rules_scala icon indicating copy to clipboard operation
rules_scala copied to clipboard

Support for Scala Native

Open meisam opened this issue 2 years ago • 1 comments

It would be nice to support Scala Native in Bazel: https://www.scala-native.org/

The Scala ecosystem lacks build tools suitable for not-trivial native projects

Scala on JVM and Scala JS do not need to worry about the system architecture, or the platform, or the toolchain, because their output (Java byte code and Java Script respactively) is platform-independent. But Scala Native, or any other code compiled to native binaries, needs to have control over multiple parts of the build:

  • The target platform: Linux distros vs Mac vs Windows.
  • The target CPU architecture: x86_64 vs ARM64 vs RISC, etc.
  • The toolchain: clang10 vs clang11 vs clang12 vs GCC10 vs GCC11, etc.
  • The build flavors: Debug vs Release vs ASan (Address Sanitizer) vs UBSan (Undefined Behavior Sanitizer).

For example, it should be possible to cross-compile[^1] a Scala Native program on an x86-64 windows machine to run the resulted binary file on a Raspberry Pi with ARM64 CPU, unless one resorts to hacks and custom scripts.

The alternative is to overhaul Scala's build tools, such as SBT, so they support multiple architectures, platforms, toolchains, or build flavors. This is major task and is unlikely to happen. I'd argue that it shouldn't happen because it will make the current tools complicated for anyone else who does not use Scala Native. Even Scala Native users who do not need to cross-compile their code can use the current tools in the Scala ecosystem and be perfectly happy.

Bazel is uniquely suited to be a build tools for Scala Native

Bazel solves many of the challenges in Scala's most common build tools such as SBT, because Bazel supports platforms, CPU architectures, toolchains, and build flavors. Bazel is also designed with multiple languages in mind, which means adding support for a new language is relatively easy. Mind you that adding support for Scala Native is not even as hard as adding support for a new language, because Bazel already supports Scala.

Issue #90 is similar issue to Support Scala JS in Bazel.

[^1]: The term "cross-compile" is overloaded in the Scala universe. It can mean multiple things: 1) Compile Scala code to JavaScript, 2) Compile to a different version of Scala–e.g. compile to Scala 2.13.0 with Scala3's compiler, or 3) Compile to native code. In this issue, "cross-compile" means generating executable or library files for a platform other than the one on which the compiler is running.

meisam avatar Jul 10 '22 02:07 meisam

Bringing Scala native to Rules Scala is a big effort. We can accept contributions in this area. Ideally from someone who uses Scala Native in production and can take care to maintain it.

liucijus avatar Jul 19 '22 07:07 liucijus