Skip to content

Commit

Permalink
Add variants of NativeConfig methods taking a mapping function instea…
Browse files Browse the repository at this point in the history
…d of a value
  • Loading branch information
WojciechMazur committed Aug 26, 2023
1 parent d71e871 commit 083c24a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/run-tests-linux-multiarch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,8 @@ jobs:
.withCheckFatalWarnings(true)
.withTargetTriple(sys.env.get("CROSS_TRIPLE"))
.withMultithreadingSupport(${{matrix.multithreading}})
.withCompileOptions(
prev.compileOptions ++ sysRoot ++ List("-fuse-ld=lld")
)
.withLinkingOptions(
prev.linkingOptions ++ sysRoot ++ List("-fuse-ld=lld", "-latomic")
)
.withCompileOptions(_ ++ sysRoot ++ List("-fuse-ld=lld"))
.withLinkingOptions(_ ++ sysRoot ++ List("-fuse-ld=lld", "-latomic"))
}
EOM
)
Expand Down
7 changes: 2 additions & 5 deletions .github/workflows/run-tests-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ jobs:
# Linking on MacOS in GithubActions fails when using default linker (ld), use lld instead
run: |
SetConfigTemplate=$(cat << EOM
nativeConfig ~= { prev =>
prev
.withLinkingOptions(prev.linkingOptions ++ Seq("-fuse-ld=lld") )
}
nativeConfig ~= { _.withLinkingOptions(_ :+ "-fuse-ld=lld") }
EOM
)
echo set-native-config=${SetConfigTemplate} >> $GITHUB_ENV
Expand Down Expand Up @@ -191,7 +188,7 @@ jobs:
.withCheck(true)
.withCheckFatalWarnings(true)
.withMultithreadingSupport(true)
.withLinkingOptions(prev.linkingOptions ++ Seq("-fuse-ld=lld") )
.withLinkingOptions(_ :+ "-fuse-ld=lld")
}
EOM
)
Expand Down
2 changes: 1 addition & 1 deletion docs/user/sbt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ To append a value to the right of any previous setting:
// Enable verbose reporting during compilation
nativeConfig ~= { c =>
c.withCompileOptions(c.compileOptions ++ Seq("-v"))
c.withCompileOptions(_ :+ "-v")
}
// Use an alternate linker
Expand Down
46 changes: 35 additions & 11 deletions tools/src/main/scala/scala/scalanative/build/NativeConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import scala.scalanative.nir.Val

/** An object describing how to configure the Scala Native toolchain. */
sealed trait NativeConfig {
import NativeConfig.Mapping

/** The path to the `clang` executable. */
def clang: Path
Expand Down Expand Up @@ -108,10 +109,18 @@ sealed trait NativeConfig {
def withClangPP(value: Path): NativeConfig

/** Create a new config with given linking options. */
def withLinkingOptions(value: Seq[String]): NativeConfig
final def withLinkingOptions(value: Seq[String]): NativeConfig =
withLinkingOptions(_ => value)

/** Create a new config with updated linking options. */
def withLinkingOptions(update: Mapping[Seq[String]]): NativeConfig

/** Create a new config with given compilation options. */
def withCompileOptions(value: Seq[String]): NativeConfig
final def withCompileOptions(value: Seq[String]): NativeConfig =
withCompileOptions(_ => value)

/** Create a new config with updated compilation options. */
def withCompileOptions(update: Mapping[Seq[String]]): NativeConfig

/** Create a new config given a target triple. */
def withTargetTriple(value: Option[String]): NativeConfig
Expand Down Expand Up @@ -163,8 +172,13 @@ sealed trait NativeConfig {
def withMultithreadingSupport(enabled: Boolean): NativeConfig

/** Create a new config with given linktime properites */
def withLinktimeProperties(
final def withLinktimeProperties(
value: NativeConfig.LinktimeProperites
): NativeConfig = withLinktimeProperties(_ => value)

/** Create a new config with updated linktime properites */
def withLinktimeProperties(
update: Mapping[NativeConfig.LinktimeProperites]
): NativeConfig

/** Create a new [[NativeConfig]] enabling embedded resources in the
Expand All @@ -179,7 +193,11 @@ sealed trait NativeConfig {
def withBaseName(value: String): NativeConfig

/** Create a optimization configuration */
def withOptimizerConfig(value: OptimizerConfig): NativeConfig
final def withOptimizerConfig(value: OptimizerConfig): NativeConfig =
withOptimizerConfig(_ => value)

/** Modify a optimization configuration */
def withOptimizerConfig(update: Mapping[OptimizerConfig]): NativeConfig

/** Create a new [[NativeConfig]] with given debugMetadata value
*/
Expand All @@ -188,6 +206,7 @@ sealed trait NativeConfig {
}

object NativeConfig {
type Mapping[T] = T => T
type LinktimeProperites = Map[String, Any]

/** Default empty config object where all of the fields are left blank. */
Expand Down Expand Up @@ -248,11 +267,11 @@ object NativeConfig {
def withClangPP(value: Path): NativeConfig =
copy(clangPP = value)

def withLinkingOptions(value: Seq[String]): NativeConfig =
copy(linkingOptions = value)
def withLinkingOptions(update: Mapping[Seq[String]]): NativeConfig =
copy(linkingOptions = update(linkingOptions))

def withCompileOptions(value: Seq[String]): NativeConfig =
copy(compileOptions = value)
def withCompileOptions(update: Mapping[Seq[String]]): NativeConfig =
copy(compileOptions = update(compileOptions))

def withTargetTriple(value: Option[String]): NativeConfig = {
val propertyName = "target.triple"
Expand Down Expand Up @@ -303,7 +322,10 @@ object NativeConfig {
def withMultithreadingSupport(enabled: Boolean): NativeConfig =
copy(multithreadingSupport = enabled)

def withLinktimeProperties(v: LinktimeProperites): NativeConfig = {
def withLinktimeProperties(
update: Mapping[LinktimeProperites]
): NativeConfig = {
val v = update(linktimeProperties)
checkLinktimeProperties(v)
copy(linktimeProperties = v)
}
Expand All @@ -316,8 +338,10 @@ object NativeConfig {
copy(baseName = value)
}

override def withOptimizerConfig(value: OptimizerConfig): NativeConfig = {
copy(optimizerConfig = value)
override def withOptimizerConfig(
update: Mapping[OptimizerConfig]
): NativeConfig = {
copy(optimizerConfig = update(optimizerConfig))
}

override def withDebugMetadata(value: Boolean): NativeConfig =
Expand Down

0 comments on commit 083c24a

Please sign in to comment.