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

refactor: Restrict access to tools and nir types #3812

Merged
44 changes: 13 additions & 31 deletions docs/user/sbt.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,28 @@ user files path, e.g /home/\<USER\>/sn-projects.


This will:

- start sbt.
- prompt for a project name
- use the [.g8
template](https://github.com/scala-native/scala-native.g8/tree/main/src/main/g8).
to generate a basic project with that name.
- use the [.g8 template](https://github.com/scala-native/scala-native.g8/tree/main/src/main/g8) to generate a basic project with that name.
- create a project sub-directory with the project name.
- copy the contents at these template links to the corresponding
location in this new project sub-directory.
- [project/plugins.sbt](https://github.com/scala-native/scala-native.g8/blob/main/src/main/g8/project/plugins.sbt)
adds the Scala Native plugin dependency and its version.
- [project/build.properties](https://github.com/scala-native/scala-native.g8/blob/main/src/main/g8/project/build.properties)
specifies the sbt version.
- [build.sbt](https://github.com/scala-native/scala-native.g8/blob/main/src/main/g8/build.sbt)
enables the plugin and specifies the Scala version.
- [src/main/scala/Main.scala](https://github.com/scala-native/scala-native.g8/blob/main/src/main/g8/src/main/scala/Main.scala)
is a minimal application. :
- copy the contents at these template links to the corresponding location in this new project sub-directory.
- [project/plugins.sbt](https://github.com/scala-native/scala-native.g8/blob/main/src/main/g8/project/plugins.sbt) adds the Scala Native plugin dependency and its version.
- [project/build.properties](https://github.com/scala-native/scala-native.g8/blob/main/src/main/g8/project/build.properties) specifies the sbt version.
- [build.sbt](https://github.com/scala-native/scala-native.g8/blob/main/src/main/g8/build.sbt) enables the plugin and specifies the Scala version.
- [src/main/scala/Main.scala](https://github.com/scala-native/scala-native.g8/blob/main/src/main/g8/src/main/scala/Main.scala) is a minimal application.
```scala
object Main {
def main(args: Array[String]): Unit =
println("Hello, world!")
}
```

To use the new project:

- Change the current working directory to the new project directory.
> - For example, on linux with a project named
> AnswerToProjectNamePrompt, type
> `cd AnswerToProjectNamePrompt`.

- Type `sbt run`.

This will get everything compiled and should have the expected output!
Expand Down Expand Up @@ -126,25 +119,14 @@ nativeConfig ~= { c =>
| 0.1 | `package` | `File` | Similar to standard package with addition of NIR |
| 0.1 | `publish` | `Unit` | Similar to standard publish with addition of NIR (1) |
| 0.1 | `nativeLink` | `File` | Link NIR and generate native binary |
| 0.1 | `nativeClang` | `File` | Path to `clang` command |
| 0.1 | `nativeClangPP` | `File` | Path to `clang++` command |
| 0.1 | `nativeCompileOptions` | `Seq[String]` | Extra options passed to clang verbatim during compilation |
| 0.1 | `nativeLinkingOptions` | `Seq[String]` | Extra options passed to clang verbatim during linking |
| 0.1 | `nativeMode` | `String` | One of `"debug"`, `"release-fast"`, `"release-size"` or `"release-full"` (2) |
| 0.2 | `nativeGC` | `String` | One of `"none"`, `"boehm"`, `"immix"` or `"commix"` (3) |
| 0.3.3 | `nativeLinkStubs` | `Boolean` | Whether to link `@stub` definitions, or to ignore them |
| 0.4.0 | `nativeConfig` | `NativeConfig` | Configuration of the Scala Native plugin |
| 0.4.0 | `nativeLTO` | `String` | One of `"none"`, `"full"` or `"thin"` (4) |
| 0.4.0 | `targetTriple` | `String` | The platform LLVM target triple |
| 0.4.0 | `nativeCheck` | `Boolean` | Shall the linker check intermediate results for correctness? |
| 0.4.0 | `nativeDump` | `Boolean` | Shall the linker dump intermediate results to disk? |
| 0.5.0 | `nativeLinkReleaseFast` | `File` | Alias for `nativeLink` using fast release build mode (2) |
| 0.5.0 | `nativeLinkReleaseFull` | `File` | Alias for `nativeLink` using full release build mode (2) |

For the details of available `NativeConfig` options see [API](https://javadoc.io/doc/org.scala-native/tools_3/latest/scala/scalanative/build/NativeConfig.html)

1. See [Publishing](#publishing) and [Cross
compilation](#cross-compilation) for details.
1. See [Publishing](#publishing) and [Cross compilation](#cross-compilation) for details.
2. See [Compilation modes](#compilation-modes) for details.
3. See [Garbage collectors](#garbage-collectors) for details.
4. See [Link-Time Optimization (LTO)](#link-time-optimization-lto) for
details.

## Compilation modes

Expand Down
2 changes: 1 addition & 1 deletion nir/src/main/scala/scala/scalanative/nir/ControlFlow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import util.unsupported
*
* * What are the successors of given block?
*/
object ControlFlow {
private[scalanative] object ControlFlow {
final case class Edge(from: Block, to: Block, next: Next)

final case class Block(
Expand Down
9 changes: 0 additions & 9 deletions nir/src/main/scala/scala/scalanative/nir/Dep.scala

This file was deleted.

2 changes: 1 addition & 1 deletion nir/src/main/scala/scala/scalanative/nir/Linktime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object LinktimeCondition {

}

object Linktime {
private[scalanative] object Linktime {

final val Linktime = Global.Top("scala.scalanative.linktime")

Expand Down
2 changes: 1 addition & 1 deletion nir/src/main/scala/scala/scalanative/nir/Rt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package nir

import Type._

object Rt {
private[scalanative] object Rt {
val Object = Ref(Global.Top("java.lang.Object"))
val Class = Ref(Global.Top("java.lang.Class"))
val String = Ref(Global.Top("java.lang.String"))
Expand Down
8 changes: 6 additions & 2 deletions project/Settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import ScriptedPlugin.autoImport._
import com.jsuereth.sbtpgp.PgpKeys

import scala.collection.mutable
import scala.scalanative.build.Platform
import MyScalaNativePlugin.isGeneratingForIDE

import java.io.File
import java.util.Locale

object Settings {
lazy val fetchScalaSource = taskKey[File](
Expand Down Expand Up @@ -182,7 +182,11 @@ object Settings {
apiMappings += file("/modules/java.base") -> url(javaDocBaseURL),
Compile / doc / sources := {
val prev = (Compile / doc / sources).value
if (Platform.isWindows &&
val isWindows = System
.getProperty("os.name", "unknown")
.toLowerCase(Locale.ROOT)
.startsWith("windows")
if (isWindows &&
sys.env.contains("CI") // Always present in GitHub Actions
) Nil
else prev
Expand Down
6 changes: 5 additions & 1 deletion scripted-tests/run/link-order/build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
enablePlugins(ScalaNativePlugin)

import scala.sys.process._
import scala.scalanative.build.Platform.isWindows
import java.util.Locale

scalaVersion := {
val scalaVersion = System.getProperty("scala.version")
Expand Down Expand Up @@ -54,6 +54,10 @@ Compile / compile := {
opath
}

val isWindows = System
.getProperty("os.name", "unknown")
.toLowerCase(Locale.ROOT)
.startsWith("windows")
val libName =
if (isWindows) "link-order-test.lib"
else "liblink-order-test.a"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package scala.scalanative.build
import java.util.Locale

/** Utility methods indicating the platform type */
object Platform {
private[scala] object Platform {
final val isJVM = true

private lazy val osUsed =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package scala.scalanative.build
import scala.scalanative.meta.LinktimeInfo

/** Utility methods indicating the platform type */
object Platform {
private[scala] object Platform {
final val isJVM = false

/** Test for the platform type
Expand Down
2 changes: 1 addition & 1 deletion tools/src/main/scala/scala/scalanative/build/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ object Build {
* @return
* the paths to the compiled objects
*/
def findAndCompileNativeLibraries(
private[scala] def findAndCompileNativeLibraries(
config: Config,
analysis: ReachabilityAnalysis.Result
)(implicit ec: ExecutionContext): Future[Seq[Path]] = {
Expand Down
4 changes: 2 additions & 2 deletions tools/src/main/scala/scala/scalanative/build/Descriptor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import scala.util.Try
import java.io.Reader
import scala.annotation.tailrec

final case class Descriptor(
private[build] final case class Descriptor(
organization: Option[String],
name: Option[String],
gcProject: Boolean,
Expand All @@ -17,7 +17,7 @@ final case class Descriptor(
includes: List[String]
)

object Descriptor {
private[build] object Descriptor {

def load(path: Path): Try[Descriptor] = Try {
var reader: Reader = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ object Discover {
}
}

class ContextBasedCache[Ctx, Key, Value <: AnyRef] {
private class ContextBasedCache[Ctx, Key, Value <: AnyRef] {
private val cachedValues = scala.collection.mutable.Map.empty[Key, Value]
private var lastContext: Ctx = _
def apply[T <: Value: ClassTag](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,9 @@ object NativeConfig {
}
}

def checkLinktimeProperties(properties: LinktimeProperites): Unit = {
private[scalanative] def checkLinktimeProperties(
properties: LinktimeProperites
): Unit = {
def isNumberOrString(value: Any) = {
value match {
case _: Boolean | _: Byte | _: Char | _: Short | _: Int | _: Long |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package scala.scalanative.build
import java.nio.file.Files

/** Used to validate config objects */
object Validator {
private[build] object Validator {

/** Runs all the individual private validators
*
Expand Down
16 changes: 10 additions & 6 deletions tools/src/main/scala/scala/scalanative/checker/Check.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import scalanative.linker._
import scalanative.util.partitionBy
import scala.concurrent._

sealed abstract class NIRCheck(implicit analysis: ReachabilityAnalysis.Result) {
private[scalanative] sealed abstract class NIRCheck(implicit
analysis: ReachabilityAnalysis.Result
) {
val errors = mutable.UnrolledBuffer.empty[Check.Error]
var name: nir.Global = nir.Global.None
var ctx: List[String] = Nil
Expand Down Expand Up @@ -77,8 +79,9 @@ sealed abstract class NIRCheck(implicit analysis: ReachabilityAnalysis.Result) {
}
}

final class Check(implicit analysis: ReachabilityAnalysis.Result)
extends NIRCheck {
private[scalanative] final class Check(implicit
analysis: ReachabilityAnalysis.Result
) extends NIRCheck {
val labels = mutable.Map.empty[nir.Local, Seq[nir.Type]]
val env = mutable.Map.empty[nir.Local, nir.Type]

Expand Down Expand Up @@ -741,8 +744,9 @@ final class Check(implicit analysis: ReachabilityAnalysis.Result)
}
}

final class QuickCheck(implicit analysis: ReachabilityAnalysis.Result)
extends NIRCheck {
private[scalanative] final class QuickCheck(implicit
analysis: ReachabilityAnalysis.Result
) extends NIRCheck {
override def checkMethod(meth: Method): Unit = {
meth.insts.foreach(checkInst)
}
Expand All @@ -765,7 +769,7 @@ final class QuickCheck(implicit analysis: ReachabilityAnalysis.Result)

}

object Check {
private[scalanative] object Check {
final case class Error(name: nir.Global, ctx: List[String], msg: String)

private def run(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package scala.scalanative
package codegen

class CommonMemoryLayouts(implicit meta: Metadata) {
private[codegen] class CommonMemoryLayouts(implicit meta: Metadata) {

sealed abstract class Layout(types: List[nir.Type]) {
def this(types: nir.Type*) = this(types.toList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package codegen

import scalanative.linker.{Class, Method}

object DynamicHashMap {
private[codegen] object DynamicHashMap {
final val ty: nir.Type = nir.Type.Ptr
}

class DynamicHashMap(cls: Class, proxies: Seq[nir.Defn])(implicit
meta: Metadata
private[codegen] class DynamicHashMap(cls: Class, proxies: Seq[nir.Defn])(
implicit meta: Metadata
) {

val methods: Seq[nir.Global.Member] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package codegen

import scalanative.linker.{Class, Field}

class FieldLayout(cls: Class)(implicit meta: Metadata) {
private[codegen] class FieldLayout(cls: Class)(implicit meta: Metadata) {

import meta.layouts.{Object, ObjectHeader, ArrayHeader}
import meta.platform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import scala.scalanative.linker.{
import scala.scalanative.build.Logger

// scalafmt: { maxColumn = 120}
object Generate {
private[codegen] object Generate {
private implicit val pos: nir.SourcePosition = nir.SourcePosition.NoPosition
private implicit val scopeId: nir.ScopeId = nir.ScopeId.TopLevel
import Impl._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import scala.collection.mutable

/** Created by lukaskellenberger on 17.12.16.
*/
object GenerateReflectiveProxies {
private[codegen] object GenerateReflectiveProxies {
implicit val scopeId: nir.ScopeId = nir.ScopeId.TopLevel

private def genReflProxy(defn: nir.Defn.Define): nir.Defn.Define = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package codegen

import scalanative.linker.{Trait, Class}

class HasTraitTables(meta: Metadata) {
private[codegen] class HasTraitTables(meta: Metadata) {

private implicit val pos: nir.SourcePosition = nir.SourcePosition.NoPosition

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import scala.io.Source
import scala.language.implicitConversions
import scala.scalanative.build.Build

class IncrementalCodeGenContext(config: build.Config) {
private[codegen] class IncrementalCodeGenContext(config: build.Config) {
private val package2hash: TrieMap[String, Long] = TrieMap[String, Long]()
private val pack2hashPrev: TrieMap[String, Long] = TrieMap[String, Long]()
private val changed: TrieMap[String, Long] = TrieMap[String, Long]()
Expand Down
2 changes: 1 addition & 1 deletion tools/src/main/scala/scala/scalanative/codegen/Lower.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import scalanative.util.{ScopedVar, unsupported}
import scalanative.linker._
import scalanative.interflow.UseDef.eliminateDeadCode

object Lower {
private[scalanative] object Lower {

def apply(
defns: Seq[nir.Defn]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import scala.scalanative.nir.Attr.Alignment
import scala.scalanative.build.BuildException
import scala.annotation.tailrec

final case class MemoryLayout(
private[codegen] final case class MemoryLayout(
size: Long,
tys: Seq[MemoryLayout.PositionedType]
) {
Expand Down Expand Up @@ -48,7 +48,7 @@ final case class MemoryLayout(
.map(_.offset)
}

object MemoryLayout {
private[scalanative] object MemoryLayout {
final val BITS_IN_BYTE = 8
final val BYTES_IN_LONG = 8

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package codegen
import scala.collection.mutable
import scalanative.linker.{Trait, Class, ReachabilityAnalysis}

class Metadata(
private[scalanative] class Metadata(
val analysis: ReachabilityAnalysis.Result,
val buildConfig: build.Config,
proxies: Seq[nir.Defn]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package codegen
import scala.collection.mutable
import scalanative.linker.Class

class ModuleArray(meta: Metadata) {
private[codegen] class ModuleArray(meta: Metadata) {

val index = mutable.Map.empty[Class, Int]
val modules = mutable.UnrolledBuffer.empty[Class]
Expand Down