Skip to content

Commit

Permalink
After rebase fixes and scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jchyb committed Jul 18, 2022
1 parent 0b34f9a commit dd0d7f0
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 31 deletions.
2 changes: 1 addition & 1 deletion modules/build/src/main/scala/scala/build/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ object Build {
else if (scalaVersion.startsWith("2.12"))
if (
inputs.sourceFiles().forall {
case _: Inputs.AnyScript => false
case _: Inputs.AnyScript => snNumeralVer >= SNNumeralVersion(0, 4, 3)
case _ => true
}
) Right(snNumeralVer)
Expand Down
2 changes: 1 addition & 1 deletion modules/build/src/main/scala/scala/build/Inputs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ object Inputs {
}
final case class CFile(base: os.Path, subPath: os.SubPath)
extends OnDisk with SourceFile with Compiled {
lazy val path = base / subPath
lazy val path = base / subPath
}
final case class Directory(path: os.Path) extends OnDisk with Compiled
final case class ResourceDirectory(path: os.Path) extends OnDisk
Expand Down
31 changes: 15 additions & 16 deletions modules/build/src/main/scala/scala/build/ResourceMapper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,52 @@ package scala.build
import scala.collection.Map
import scala.jdk.CollectionConverters._


/* Maps resources from their original path, to destination path,
* also caching that mapping.
*
* also caching that mapping.
*
* Remembering the mapping this way allows for the resource to be removed
* if the original file is renamed/deleted.
* if the original file is renamed/deleted.
*/
object ResourceMapper {

private def readMappingIfExists(mappingFile: os.Path): Option[Map[os.Path, os.Path]] = {
private def readMappingIfExists(mappingFile: os.Path): Option[Map[os.Path, os.Path]] =
if (os.exists(mappingFile)) {
val mappingContent = os.read(mappingFile)
Some(mappingContent.split('\n').map { pair =>
val sep = pair.split(',')
(os.Path(sep(0)), os.Path(sep(1)))
}.toMap)
} else None
}
}
else None

private def writeMapping(mappingPath: os.Path, mapping: Map[os.Path, os.Path]) = {
val mappingContent = mapping.map{ case (inputPath, outputPath) =>
val mappingContent = mapping.map { case (inputPath, outputPath) =>
s"$inputPath,$outputPath"
}.mkString("\n")
os.write.over(mappingPath, mappingContent)
}

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

private def resolveProjectCFileMappingPath(nativeWorkDir: os.Path) = nativeWorkDir / ".project_native_mapping"
private def resolveProjectCFileMappingPath(nativeWorkDir: os.Path) =
nativeWorkDir / ".project_native_mapping"

def copyCFilesToScalaNativeDir(build: Build.Successful, nativeWorkDir: os.Path): Unit = {
def isInDirectory(output: os.Path, filePath: os.Path) = {
val outputFullPath = output.toNIO.iterator().asScala.toSeq
val fileFullPath = output.toNIO.iterator().asScala.toSeq
val fileFullPath = output.toNIO.iterator().asScala.toSeq
fileFullPath.startsWith(outputFullPath)
}

val mappingPath = resolveProjectCFileMappingPath(nativeWorkDir)
val oldMapping = readMappingIfExists(mappingPath).getOrElse(Map.empty)
val newMapping = scalaNativeCFileMapping(build)
val oldMapping = readMappingIfExists(mappingPath).getOrElse(Map.empty)
val newMapping = scalaNativeCFileMapping(build)

val removedFiles = oldMapping.values.toSet -- newMapping.values.toSet
removedFiles.foreach { outputPath =>
Expand All @@ -68,4 +67,4 @@ object ResourceMapper {
}
writeMapping(mappingPath, newMapping)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final case class ScalaNativeOptions(
nativeCompileDefaults: Option[Boolean] = None, //TODO does it even work when we default it to true while handling?

@Group("Scala Native")
@HelpMessage("Do not embed resources into the Scala Native binary (resources will not be able to be used)")
@HelpMessage("Do not embed resources into the Scala Native binary (java style Resources will not be able to be used)")
noEmbed: Option[Boolean] = None

)
Expand Down
3 changes: 2 additions & 1 deletion modules/cli/src/main/scala/scala/cli/commands/Package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -924,5 +924,6 @@ object Package extends ScalaCommand[PackageOptions] {
else
throw new ScalaNativeBuildError
}
}}
}
}
}
15 changes: 8 additions & 7 deletions modules/cli/src/test/scala/cli/tests/CachedBinaryTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CachedBinaryTests extends munit.FunSuite {
(_, _, maybeBuild) =>
val build = maybeBuild.successfulOpt.get

val config = build.options.scalaNativeOptions.configCliOptions()
val config = build.options.scalaNativeOptions.configCliOptions(resourcesExist = false)
val nativeWorkDir = build.inputs.nativeWorkDir
val destPath = nativeWorkDir / s"main${if (Properties.isWin) ".exe" else ""}"
// generate dummy output
Expand All @@ -65,7 +65,7 @@ class CachedBinaryTests extends munit.FunSuite {
(_, _, maybeBuild) =>
val build = maybeBuild.successfulOpt.get

val config = build.options.scalaNativeOptions.configCliOptions()
val config = build.options.scalaNativeOptions.configCliOptions(resourcesExist = false)
val nativeWorkDir = build.inputs.nativeWorkDir
val destPath = nativeWorkDir / s"main${if (Properties.isWin) ".exe" else ""}"
// generate dummy output
Expand All @@ -91,7 +91,7 @@ class CachedBinaryTests extends munit.FunSuite {
(_, _, maybeBuild) =>
val build = maybeBuild.successfulOpt.get

val config = build.options.scalaNativeOptions.configCliOptions()
val config = build.options.scalaNativeOptions.configCliOptions(resourcesExist = false)
val nativeWorkDir = build.inputs.nativeWorkDir
val destPath = nativeWorkDir / s"main${if (Properties.isWin) ".exe" else ""}"
// generate dummy output
Expand All @@ -118,7 +118,7 @@ class CachedBinaryTests extends munit.FunSuite {
(_, _, maybeBuild) =>
val build = maybeBuild.successfulOpt.get

val config = build.options.scalaNativeOptions.configCliOptions()
val config = build.options.scalaNativeOptions.configCliOptions(resourcesExist = false)
val nativeWorkDir = build.inputs.nativeWorkDir
val destPath = nativeWorkDir / s"main${if (Properties.isWin) ".exe" else ""}"
// generate dummy output
Expand All @@ -145,7 +145,7 @@ class CachedBinaryTests extends munit.FunSuite {
(root, _, maybeBuild) =>
val build = maybeBuild.successfulOpt.get

val config = build.options.scalaNativeOptions.configCliOptions()
val config = build.options.scalaNativeOptions.configCliOptions(resourcesExist = false)
val nativeWorkDir = build.inputs.nativeWorkDir
val destPath = nativeWorkDir / s"main${if (Properties.isWin) ".exe" else ""}"
os.write(destPath, Random.alphanumeric.take(10).mkString(""), createFolders = true)
Expand All @@ -171,7 +171,7 @@ class CachedBinaryTests extends munit.FunSuite {
(_, _, maybeBuild) =>
val build = maybeBuild.successfulOpt.get

val config = build.options.scalaNativeOptions.configCliOptions()
val config = build.options.scalaNativeOptions.configCliOptions(resourcesExist = false)
val nativeWorkDir = build.inputs.nativeWorkDir
val destPath = nativeWorkDir / s"main${if (Properties.isWin) ".exe" else ""}"
os.write(destPath, Random.alphanumeric.take(10).mkString(""), createFolders = true)
Expand All @@ -192,7 +192,8 @@ class CachedBinaryTests extends munit.FunSuite {
)
)
)
val updatedConfig = updatedBuild.options.scalaNativeOptions.configCliOptions(false)
val updatedConfig =
updatedBuild.options.scalaNativeOptions.configCliOptions(resourcesExist = false)

val cacheAfterConfigUpdate =
CachedBinary.getCacheData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
}

test("Scala Native C Files are correctly handled as a regular Input") {
val projectDir = "native-interop"
val projectDir = "native-interop"
val interopFileName = "bindings.c"
val interopMsg = "Hello C!"
val interopMsg = "Hello C!"
val inputs = TestInputs(
Seq(
os.rel / projectDir / "main.scala" ->
Expand All @@ -296,11 +296,11 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
| }
|}
|""".stripMargin,
os.rel / projectDir / interopFileName ->
os.rel / projectDir / interopFileName ->
s"""|#include <stdio.h>
|
|void scalanative_print() {
| printf("${interopMsg}\\n");
| printf("$interopMsg\\n");
|}
|""".stripMargin
)
Expand Down
4 changes: 4 additions & 0 deletions website/docs/reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,10 @@ List of compile options

Use default compile options

#### `--no-embed`

Do not embed resources into the Scala Native binary (java style Resources will not be able to be used)

## Scalac options

Available in commands:
Expand Down
2 changes: 2 additions & 0 deletions website/docs/reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ Add Scala Native options

`//> using nativeClangPP` _value_

`//> using nativeNoEmbed` _true|false_

#### Examples
`//> using nativeVersion "0.4.0"`

Expand Down

0 comments on commit dd0d7f0

Please sign in to comment.