Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resources support to Scala Native and fixes to overall resources #812

Merged
merged 3 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 56 additions & 42 deletions modules/build/src/main/scala/scala/build/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import scala.build.Ops.*
import scala.build.compiler.{ScalaCompiler, ScalaCompilerMaker}
import scala.build.errors.*
import scala.build.internal.{Constants, CustomCodeWrapper, MainClass, Util}
import scala.build.internal.resource.ResourceMapper
import scala.build.options.*
import scala.build.options.validation.ValidationException
import scala.build.postprocessing.*
Expand Down Expand Up @@ -392,9 +393,9 @@ object Build {

val builds = value(buildScopes())

copyResourceToClassesDir(builds.main)
ResourceMapper.copyResourceToClassesDir(builds.main)
for (testBuild <- builds.get(Scope.Test))
copyResourceToClassesDir(testBuild)
ResourceMapper.copyResourceToClassesDir(testBuild)

if (actionableDiagnostics.getOrElse(false)) {
val projectOptions = builds.get(Scope.Test).getOrElse(builds.main).options
Expand All @@ -404,26 +405,6 @@ object Build {
builds
}

private def copyResourceToClassesDir(build: Build): Unit = build match {
case b: Build.Successful =>
for {
resourceDirPath <- b.sources.resourceDirs.filter(os.exists(_))
resourceFilePath <- os.walk(resourceDirPath).filter(os.isFile(_))
relativeResourcePath = resourceFilePath.relativeTo(resourceDirPath)
// dismiss files generated by scala-cli
if !relativeResourcePath.startsWith(os.rel / Constants.workspaceDirName)
} {
val destPath = b.output / relativeResourcePath
os.copy(
resourceFilePath,
destPath,
replaceExisting = true,
createFolders = true
)
}
case _ =>
}

private def build(
inputs: Inputs,
sources: Sources,
Expand Down Expand Up @@ -482,36 +463,69 @@ object Build {
def classesDir(root: os.Path, projectName: String, scope: Scope, suffix: String = ""): os.Path =
classesRootDir(root, projectName) / s"${scope.name}$suffix"

def resourcesRegistry(
root: os.Path,
projectName: String,
scope: Scope
): os.Path =
root / Constants.workspaceDirName / projectName / s"resources-${scope.name}"

def scalaNativeSupported(
options: BuildOptions,
inputs: Inputs
inputs: Inputs,
logger: Logger
): Either[BuildException, Option[ScalaNativeCompatibilityError]] =
either {
val scalaParamsOpt = value(options.scalaParams)
scalaParamsOpt.flatMap { scalaParams =>
val scalaVersion = scalaParams.scalaVersion
val nativeVersion = options.scalaNativeOptions.numeralVersion
val isCompatible = nativeVersion match {
case Some(snNumeralVer) =>
if (snNumeralVer < SNNumeralVersion(0, 4, 1) && Properties.isWin)
false
else if (scalaVersion.startsWith("3.0"))
false
else if (scalaVersion.startsWith("3") || scalaVersion.startsWith("2.12"))
snNumeralVer >= SNNumeralVersion(0, 4, 3)
else if (scalaVersion.startsWith("2.13"))
true
else false
case None => false
}
if (isCompatible) None
else
Some(
val scalaVersion = scalaParams.scalaVersion
val nativeVersionMaybe = options.scalaNativeOptions.numeralVersion
def snCompatError =
Left(
new ScalaNativeCompatibilityError(
scalaVersion,
options.scalaNativeOptions.finalVersion
)
)
def warnIncompatibleNativeOptions(numeralVersion: SNNumeralVersion) =
if (
numeralVersion < SNNumeralVersion(0, 4, 4)
&& options.scalaNativeOptions.embedResources.isDefined
)
logger.diagnostic(
"This Scala Version cannot embed resources, regardless of the options used."
)

val numeralOrError: Either[ScalaNativeCompatibilityError, SNNumeralVersion] =
nativeVersionMaybe match {
case Some(snNumeralVer) =>
if (snNumeralVer < SNNumeralVersion(0, 4, 1) && Properties.isWin)
snCompatError
else if (scalaVersion.startsWith("3.0"))
snCompatError
else if (scalaVersion.startsWith("3"))
if (snNumeralVer >= SNNumeralVersion(0, 4, 3)) Right(snNumeralVer)
else snCompatError
else if (scalaVersion.startsWith("2.13"))
Right(snNumeralVer)
else if (scalaVersion.startsWith("2.12"))
if (
inputs.sourceFiles().forall {
case _: Inputs.AnyScript => snNumeralVer >= SNNumeralVersion(0, 4, 3)
case _ => true
}
) Right(snNumeralVer)
else snCompatError
else snCompatError
case None => snCompatError
}

numeralOrError match {
case Left(compatError) => Some(compatError)
case Right(snNumeralVersion) =>
warnIncompatibleNativeOptions(snNumeralVersion)
None
}
}
}

Expand Down Expand Up @@ -993,7 +1007,7 @@ object Build {
): Either[BuildException, Build] = either {

if (options.platform.value == Platform.Native)
value(scalaNativeSupported(options, inputs)) match {
value(scalaNativeSupported(options, inputs, logger)) match {
case None =>
case Some(error) => value(Left(error))
}
Expand Down
13 changes: 8 additions & 5 deletions modules/build/src/main/scala/scala/build/Inputs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,6 @@ final case class Inputs(
case dirInput: Inputs.Directory =>
Seq("dir:") ++ Inputs.singleFilesFromDirectory(dirInput, enableMarkdown)
.map(file => s"${file.path}:" + os.read(file.path))
case resDirInput: Inputs.ResourceDirectory =>
// Resource changes for SN require relinking, so they should also be hashed
Seq("resource-dir:") ++ os.walk(resDirInput.path)
.filter(os.isFile(_))
.map(filePath => s"$filePath:" + os.read(filePath))
case _ => Seq(os.read(elem.path))
}
(Iterator(elem.path.toString) ++ content.iterator ++ Iterator("\n")).map(bytes)
Expand Down Expand Up @@ -206,6 +201,10 @@ object Inputs {
extends OnDisk with SourceFile with Compiled {
lazy val path: os.Path = base / subPath
}
final case class CFile(base: os.Path, subPath: os.SubPath)
extends OnDisk with SourceFile with Compiled {
lazy val path = base / subPath
}
final case class MarkdownFile(base: os.Path, subPath: os.SubPath)
extends OnDisk with SourceFile {
lazy val path: os.Path = base / subPath
Expand Down Expand Up @@ -245,6 +244,8 @@ object Inputs {
Inputs.SourceScalaFile(d.path, p.subRelativeTo(d.path))
case p if p.last.endsWith(".sc") =>
Inputs.Script(d.path, p.subRelativeTo(d.path))
case p if p.last.endsWith(".c") || p.last.endsWith(".h") =>
Gedochao marked this conversation as resolved.
Show resolved Hide resolved
Inputs.CFile(d.path, p.subRelativeTo(d.path))
case p if p.last.endsWith(".md") && enableMarkdown =>
Inputs.MarkdownFile(d.path, p.subRelativeTo(d.path))
}
Expand Down Expand Up @@ -274,6 +275,7 @@ object Inputs {
case _: Inputs.JavaFile => "java:"
case _: Inputs.SettingsScalaFile => "config:"
case _: Inputs.SourceScalaFile => "scala:"
case _: Inputs.CFile => "c:"
case _: Inputs.Script => "sc:"
case _: Inputs.MarkdownFile => "md:"
}
Expand Down Expand Up @@ -460,6 +462,7 @@ object Inputs {
else if (arg.endsWith(".sc")) Right(Seq(Script(dir, subPath)))
else if (arg.endsWith(".scala")) Right(Seq(SourceScalaFile(dir, subPath)))
else if (arg.endsWith(".java")) Right(Seq(JavaFile(dir, subPath)))
else if (arg.endsWith(".c") || arg.endsWith(".h")) Right(Seq(CFile(dir, subPath)))
lwronski marked this conversation as resolved.
Show resolved Hide resolved
else if (arg.endsWith(".md")) Right(Seq(MarkdownFile(dir, subPath)))
else if (os.isDir(path)) Right(Seq(Directory(path)))
else if (acceptFds && arg.startsWith("/dev/fd/")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package scala.build.internal.resource

import scala.build.{Build, Inputs}

object NativeResourceMapper {

private def scalaNativeCFileMapping(build: Build.Successful): Map[os.Path, os.RelPath] =
build
.inputs
.flattened()
.collect {
case cfile: Inputs.CFile =>
val inputPath = cfile.path
val destPath = os.rel / "scala-native" / cfile.subPath
(inputPath, destPath)
}
.toMap

private def resolveProjectCFileRegistryPath(nativeWorkDir: os.Path) =
nativeWorkDir / ".native_registry"

/** Copies and maps c file resources from their original path to the destination path in build
* output, also caching output paths in a file.
*
* Remembering the mapping this way allows for the resource to be removed if the original file is
* renamed/deleted.
*/
def copyCFilesToScalaNativeDir(build: Build.Successful, nativeWorkDir: os.Path): Unit = {
val mappingFilePath = resolveProjectCFileRegistryPath(nativeWorkDir)
ResourceMapper.copyResourcesToDirWithMapping(
build.output,
mappingFilePath,
scalaNativeCFileMapping(build)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package scala.build.internal.resource

import scala.build.Build
import scala.build.internal.Constants

object ResourceMapper {

private def resourceMapping(build: Build.Successful): Map[os.Path, os.RelPath] = {
val seq = for {
resourceDirPath <- build.sources.resourceDirs.filter(os.exists(_))
resourceFilePath <- os.walk(resourceDirPath).filter(os.isFile(_))
relativeResourcePath = resourceFilePath.relativeTo(resourceDirPath)
// dismiss files generated by scala-cli
if !relativeResourcePath.startsWith(os.rel / Constants.workspaceDirName)
} yield (resourceFilePath, relativeResourcePath)

seq.toMap
}

def copyResourcesToDirWithMapping(
output: os.Path,
registryFilePath: os.Path,
newMapping: Map[os.Path, os.RelPath]
): Unit = {

val oldRegistry =
if (os.exists(registryFilePath))
os.read(registryFilePath)
.linesIterator
.filter(_.nonEmpty)
.map(os.RelPath(_))
.toSet
else
Set.empty
val removedFiles = oldRegistry -- newMapping.values

for (f <- removedFiles)
os.remove(output / f)

for ((inputPath, outputPath) <- newMapping)
os.copy(
inputPath,
output / outputPath,
replaceExisting = true,
createFolders = true
)

if (newMapping.isEmpty)
os.remove(registryFilePath)
else
os.write.over(
registryFilePath,
newMapping.map(_._2.toString).toVector.sorted.mkString("\n")
)
}

/** Copies and maps resources from their original path to the destination path in build output,
* also caching output paths in a file.
*
* Remembering the mapping this way allows for the resource to be removed if the original file is
* renamed/deleted.
*/
def copyResourceToClassesDir(build: Build): Unit = build match {
case b: Build.Successful =>
val fullFilePath = Build.resourcesRegistry(b.inputs.workspace, b.inputs.projectName, b.scope)
copyResourcesToDirWithMapping(b.output, fullFilePath, resourceMapping(b))
case _ =>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ final case class ScalaNativeOptions(
@Group("Scala Native")
@Hidden
@HelpMessage("Use default compile options")
nativeCompileDefaults: Option[Boolean] = None //TODO does it even work when we default it to true while handling?
nativeCompileDefaults: Option[Boolean] = None, //TODO does it even work when we default it to true while handling?

@Group("Scala Native")
@HelpMessage("Embed resources into the Scala Native binary (can be read with the Java resources API)")
embedResources: Option[Boolean] = None

)
// format: on

Expand Down
10 changes: 7 additions & 3 deletions modules/cli/src/main/scala/scala/cli/commands/Package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import scala.build.errors.*
import scala.build.interactive.InteractiveFileOps
import scala.build.internal.Util.*
import scala.build.internal.{Runner, ScalaJsLinkerConfig}
import scala.build.internal.resource.NativeResourceMapper
import scala.build.options.{PackageType, Platform}
import scala.cli.CurrentParams
import scala.cli.commands.OptionsHelper.*
Expand Down Expand Up @@ -912,7 +913,8 @@ object Package extends ScalaCommand[PackageOptions] with BuildCommandHelpers {
logger: Logger
): Either[BuildException, Unit] = either {

val cliOptions = build.options.scalaNativeOptions.configCliOptions()
val cliOptions =
build.options.scalaNativeOptions.configCliOptions(!build.sources.resourceDirs.isEmpty)

val setupPython = build.options.notForBloopOptions.doSetupPython.getOrElse(false)
val pythonLdFlags =
Expand Down Expand Up @@ -940,10 +942,11 @@ object Package extends ScalaCommand[PackageOptions] with BuildCommandHelpers {
nativeWorkDir
)

if (cacheData.changed)
if (cacheData.changed) {
NativeResourceMapper.copyCFilesToScalaNativeDir(build, nativeWorkDir)
Library.withLibraryJar(build, dest.last.stripSuffix(".jar")) { mainJar =>

val classpath = build.fullClassPath.map(_.toString) :+ mainJar.toString
val classpath = mainJar.toString +: build.artifacts.classPath.map(_.toString)
val args =
allCliOptions ++
logger.scalaNativeCliInternalLoggerOptions ++
Expand Down Expand Up @@ -976,5 +979,6 @@ object Package extends ScalaCommand[PackageOptions] with BuildCommandHelpers {
else
throw new ScalaNativeBuildError
}
}
}
}
Loading