Skip to content

Commit

Permalink
Merge pull request #2535 from KristianAN/main
Browse files Browse the repository at this point in the history
Add --link flag to compile to allow linking through BSP
  • Loading branch information
tgodzik authored Dec 31, 2024
2 parents e52ee2e + 8a6228b commit c1ff013
Show file tree
Hide file tree
Showing 20 changed files with 638 additions and 409 deletions.
32 changes: 0 additions & 32 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,38 +45,6 @@ jobs:
run: sbt "sbtBloop/publishLocal; sbtBloop/scripted"
shell: bash

bridges:
name: Test platform bridges
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macOS-13, macOS-14]
steps:
- uses: actions/checkout@v4
with:
submodules: true
- uses: sbt/setup-sbt@v1
- uses: coursier/cache-action@v6
- uses: graalvm/setup-graalvm@v1
with:
version: '22.3.0'
java-version: '17'
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: Tests
run: |
sbt \
"jsBridge06/publishLocal; \
jsBridge1/publishLocal; \
nativeBridge04/publishLocal; \
nativeBridge05/publishLocal"
sbt jsBridge1/test
sbt jsBridge06/test
sbt nativeBridge05/test
sbt nativeBridge04/test
shell: bash

test:
runs-on: ${{ matrix.os }}
strategy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,34 @@ import scala.scalanative.util.Scope

import bloop.config.Config.LinkerMode
import bloop.config.Config.NativeConfig
import bloop.data.Project
import bloop.config.Config.NativeBuildTarget
import scala.scalanative.build.BuildTarget
import bloop.io.Paths
import bloop.io.AbsolutePath
import bloop.logging.DebugFilter
import bloop.logging.Logger

class NativeLinkerException(msg: String) extends RuntimeException(msg)

object NativeBridge {
private implicit val ctx: DebugFilter = DebugFilter.Link
private val sharedScope = Scope.unsafe()

def nativeLink(
config0: NativeConfig,
project: Project,
workdir: Path,
classpath: Array[Path],
entry: String,
entry: Option[String],
target: Path,
logger: Logger
): Path = {
val workdir = project.out.resolve("native")
if (workdir.isDirectory) Paths.delete(workdir)
Files.createDirectories(workdir.underlying)
val absWorkdir = AbsolutePath(workdir)
if (absWorkdir.isDirectory) Paths.delete(absWorkdir)
Files.createDirectories(workdir)

val nativeLogger =
build.Logger(logger.trace _, logger.debug _, logger.info _, logger.warn _, logger.error _)
val config = setUpNativeConfig(project, classpath, config0)
val config = setUpNativeConfig(classpath, config0)
val nativeMode = config.mode match {
case LinkerMode.Debug => build.Mode.debug
case LinkerMode.Release => build.Mode.releaseFast
Expand All @@ -41,11 +45,18 @@ object NativeBridge {
case LinkerMode.Release => build.LTO.thin
}

val buildTarget = config.buildTarget
.map(_ match {
case NativeBuildTarget.Application => BuildTarget.application
case NativeBuildTarget.LibraryDynamic => BuildTarget.libraryDynamic
case NativeBuildTarget.LibraryStatic => BuildTarget.libraryStatic
})
.getOrElse(BuildTarget.application)

val nativeConfig =
build.Config.empty
.withMainClass(entry)
.withClassPath(classpath)
.withWorkdir(workdir.underlying)
.withWorkdir(workdir)
.withLogger(nativeLogger)
.withCompilerConfig(
build.NativeConfig.empty
Expand All @@ -55,18 +66,25 @@ object NativeBridge {
.withLinkingOptions(config.options.linker)
.withGC(build.GC(config.gc))
.withMode(nativeMode)
.withBuildTarget(buildTarget)
.withLTO(nativeLTO)
.withLinkStubs(config.linkStubs)
.withCheck(config.check)
.withDump(config.dump)
.withTargetTriple(config.targetTriple)
)

build.Build.build(nativeConfig, target)(sharedScope)
if (buildTarget == BuildTarget.application) {
entry match {
case None =>
throw new NativeLinkerException("Missing main class when linking native application")
case Some(mainClass) =>
build.Build.build(nativeConfig.withMainClass(mainClass), target)(sharedScope)
}
} else build.Build.build(nativeConfig, target)(sharedScope)
}

private[scalanative] def setUpNativeConfig(
project: Project,
classpath: Array[Path],
config: NativeConfig
): NativeConfig = {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,40 @@ import java.nio.file.Files
import java.nio.file.Path

import scala.scalanative.build
import scala.scalanative.build.BuildTarget
import scala.scalanative.util.Scope
import scala.concurrent.Future
import scala.concurrent.ExecutionContext

import bloop.config.Config.NativeBuildTarget
import bloop.config.Config.LinkerMode
import bloop.config.Config.NativeConfig
import bloop.data.Project
import bloop.io.Paths
import bloop.io.AbsolutePath
import bloop.logging.DebugFilter
import bloop.logging.Logger

class NativeLinkerException(msg: String) extends RuntimeException(msg)

object NativeBridge {
private implicit val ctx: DebugFilter = DebugFilter.Link
private val sharedScope = Scope.unsafe()

def nativeLink(
config0: NativeConfig,
project: Project,
workdir: Path,
classpath: Array[Path],
entry: String,
entry: Option[String],
target: Path,
logger: Logger,
ec: ExecutionContext
): Future[Path] = {
val workdir = project.out.resolve("native")
if (workdir.isDirectory) Paths.delete(workdir)
Files.createDirectories(workdir.underlying)
val absWorkdir = AbsolutePath(workdir)
if (absWorkdir.isDirectory) Paths.delete(absWorkdir)
Files.createDirectories(workdir)

val nativeLogger =
build.Logger(logger.trace _, logger.debug _, logger.info _, logger.warn _, logger.error _)
val config = setUpNativeConfig(project, classpath, config0)
val config = setUpNativeConfig(classpath, config0)
val nativeMode = config.mode match {
case LinkerMode.Debug => build.Mode.debug
case LinkerMode.Release => build.Mode.releaseFast
Expand All @@ -44,9 +47,16 @@ object NativeBridge {
case LinkerMode.Release => build.LTO.thin
}

val buildTarget = config.buildTarget
.map(_ match {
case NativeBuildTarget.Application => BuildTarget.application
case NativeBuildTarget.LibraryDynamic => BuildTarget.libraryDynamic
case NativeBuildTarget.LibraryStatic => BuildTarget.libraryStatic
})
.getOrElse(BuildTarget.application)

val nativeConfig =
build.Config.empty
.withMainClass(Option(entry))
.withClassPath(classpath)
.withBaseDir(target.getParent())
.withLogger(nativeLogger)
Expand All @@ -59,18 +69,28 @@ object NativeBridge {
.withLinkingOptions(config.options.linker)
.withGC(build.GC(config.gc))
.withMode(nativeMode)
.withBuildTarget(buildTarget)
.withLTO(nativeLTO)
.withLinkStubs(config.linkStubs)
.withCheck(config.check)
.withDump(config.dump)
.withTargetTriple(config.targetTriple)
)

build.Build.build(nativeConfig)(sharedScope, ec)
if (buildTarget == BuildTarget.application) {
entry match {
case None =>
Future.failed(
new NativeLinkerException("Missing main class when linking native application")
)
case Some(mainClass) =>
build.Build.build(nativeConfig.withMainClass(Some(mainClass)))(sharedScope, ec)
}
} else build.Build.build(nativeConfig)(sharedScope, ec)

}

private[scalanative] def setUpNativeConfig(
project: Project,
classpath: Array[Path],
config: NativeConfig
): NativeConfig = {
Expand Down
Loading

0 comments on commit c1ff013

Please sign in to comment.