From d525231b69df3f77a32495ee4a620fed58f752bc Mon Sep 17 00:00:00 2001 From: Matthew de Detrich Date: Sat, 30 Dec 2023 15:56:42 +1100 Subject: [PATCH 1/2] Add PekkoInlinePlugin --- .../pekkobuild/PekkoInlinePlugin.scala | 68 +++++++++++++++++++ .../pekkobuild/PekkoInlineSettings.scala | 10 +++ 2 files changed, 78 insertions(+) create mode 100644 src/main/scala/com/github/pjfanning/pekkobuild/PekkoInlinePlugin.scala create mode 100644 src/main/scala/com/github/pjfanning/pekkobuild/PekkoInlineSettings.scala diff --git a/src/main/scala/com/github/pjfanning/pekkobuild/PekkoInlinePlugin.scala b/src/main/scala/com/github/pjfanning/pekkobuild/PekkoInlinePlugin.scala new file mode 100644 index 0000000..2abdfc0 --- /dev/null +++ b/src/main/scala/com/github/pjfanning/pekkobuild/PekkoInlinePlugin.scala @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * license agreements; and to You under the Apache License, version 2.0: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * This file is part of the Apache Pekko project, derived from Akka. + */ + +package com.github.pjfanning.pekkobuild + +import sbt.plugins.JvmPlugin +import sbt._ +import sbt.Keys._ + +object PekkoInlinePlugin extends AutoPlugin { + override def trigger: PluginTrigger = allRequirements + + override def requires: Plugins = JvmPlugin + + object autoImport extends PekkoInlineSettings + + import autoImport._ + + private def flagsForScala2(coreProject: Boolean) = { + val baseInlineFlags = Seq( + "-opt-inline-from:", + "-opt:l:inline" + ) + + if (coreProject) + baseInlineFlags ++ Seq( + // Since the Pekko core project doesn't allow for mixing of different versions, + // i.e. you cannot mix pekko-actor 1.0.0 with pekko-streams 1.0.1 at runtime + // its safe to do inter sbt project inlining. + "-opt-inline-from:org.apache.pekko.**" + ) + else baseInlineFlags ++ Seq( + // These are safe to inline even across modules since they are + // wrappers for cross compilation that is stable within Pekko core. + "-opt-inline-from:org.apache.pekko.dispatch.internal.SameThreadExecutionContext**", + "-opt-inline-from:org.apache.pekko.util.OptionConverters**", + "-opt-inline-from:org.apache.pekko.util.FutureConverters**", + "-opt-inline-from:org.apache.pekko.util.FunctionConverters**", + "-opt-inline-from:org.apache.pekko.util.PartialFunction**", + "-opt-inline-from:org.apache.pekko.util.JavaDurationConverters**" + ) + } + + // Optimizer not yet available for Scala3, see https://docs.scala-lang.org/overviews/compiler-options/optimizer.html + private val flagsForScala3 = Seq() + + override lazy val globalSettings = Seq( + pekkoInlineEnabled := !sys.props.contains("pekko.no.inline"), + pekkoInlineCoreProject := false + ) + + override lazy val projectSettings = Seq(Compile / scalacOptions ++= { + if (pekkoInlineEnabled.value) { + CrossVersion.partialVersion(scalaVersion.value) match { + case Some((2, n)) if n == 12 | n == 13 => + flagsForScala2(pekkoInlineCoreProject.value) + case Some((3, _)) => + flagsForScala3 + } + } else Seq.empty + }) +} diff --git a/src/main/scala/com/github/pjfanning/pekkobuild/PekkoInlineSettings.scala b/src/main/scala/com/github/pjfanning/pekkobuild/PekkoInlineSettings.scala new file mode 100644 index 0000000..fb74fc4 --- /dev/null +++ b/src/main/scala/com/github/pjfanning/pekkobuild/PekkoInlineSettings.scala @@ -0,0 +1,10 @@ +package com.github.pjfanning.pekkobuild + +import sbt.{SettingKey, settingKey} + +trait PekkoInlineSettings { + lazy val pekkoInlineEnabled: SettingKey[Boolean] = settingKey("Whether to enable the Scala 2 inliner for Pekko modules." + + "Defaults to pekko.no.inline property") + lazy val pekkoInlineCoreProject: SettingKey[Boolean] = settingKey("Whether this is the core Pekko project or a Pekko module " + + "since the core Pekko project has different inline settings. Defaults to false") +} From 71840f9ab3d340dd3ff99a06fabb4a3603df6afc Mon Sep 17 00:00:00 2001 From: Matthew de Detrich Date: Wed, 10 Jan 2024 17:40:26 +1100 Subject: [PATCH 2/2] Make pekkoCoreProject its own general setting --- .../pjfanning/pekkobuild/PekkoCorePlugin.scala | 17 +++++++++++++++++ .../pekkobuild/PekkoCoreSettings.scala | 8 ++++++++ .../pekkobuild/PekkoInlinePlugin.scala | 8 ++++---- .../pekkobuild/PekkoInlineSettings.scala | 2 -- 4 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 src/main/scala/com/github/pjfanning/pekkobuild/PekkoCorePlugin.scala create mode 100644 src/main/scala/com/github/pjfanning/pekkobuild/PekkoCoreSettings.scala diff --git a/src/main/scala/com/github/pjfanning/pekkobuild/PekkoCorePlugin.scala b/src/main/scala/com/github/pjfanning/pekkobuild/PekkoCorePlugin.scala new file mode 100644 index 0000000..ed48ed1 --- /dev/null +++ b/src/main/scala/com/github/pjfanning/pekkobuild/PekkoCorePlugin.scala @@ -0,0 +1,17 @@ +package com.github.pjfanning.pekkobuild + +import sbt._ + +object PekkoCorePlugin extends AutoPlugin { + + override def trigger: PluginTrigger = allRequirements + + object autoImport extends PekkoCoreSettings + + import autoImport._ + + override lazy val globalSettings = Seq( + pekkoCoreProject := false + ) + +} diff --git a/src/main/scala/com/github/pjfanning/pekkobuild/PekkoCoreSettings.scala b/src/main/scala/com/github/pjfanning/pekkobuild/PekkoCoreSettings.scala new file mode 100644 index 0000000..a322d19 --- /dev/null +++ b/src/main/scala/com/github/pjfanning/pekkobuild/PekkoCoreSettings.scala @@ -0,0 +1,8 @@ +package com.github.pjfanning.pekkobuild + +import sbt._ + +trait PekkoCoreSettings { + lazy val pekkoCoreProject: SettingKey[Boolean] = settingKey("Whether this is the core Pekko project or a Pekko" + + " module. Defaults to false") +} diff --git a/src/main/scala/com/github/pjfanning/pekkobuild/PekkoInlinePlugin.scala b/src/main/scala/com/github/pjfanning/pekkobuild/PekkoInlinePlugin.scala index 2abdfc0..f4ccc2d 100644 --- a/src/main/scala/com/github/pjfanning/pekkobuild/PekkoInlinePlugin.scala +++ b/src/main/scala/com/github/pjfanning/pekkobuild/PekkoInlinePlugin.scala @@ -9,6 +9,7 @@ package com.github.pjfanning.pekkobuild +import com.github.pjfanning.pekkobuild.PekkoCorePlugin.autoImport.pekkoCoreProject import sbt.plugins.JvmPlugin import sbt._ import sbt.Keys._ @@ -16,7 +17,7 @@ import sbt.Keys._ object PekkoInlinePlugin extends AutoPlugin { override def trigger: PluginTrigger = allRequirements - override def requires: Plugins = JvmPlugin + override def requires: Plugins = JvmPlugin && PekkoCorePlugin object autoImport extends PekkoInlineSettings @@ -51,15 +52,14 @@ object PekkoInlinePlugin extends AutoPlugin { private val flagsForScala3 = Seq() override lazy val globalSettings = Seq( - pekkoInlineEnabled := !sys.props.contains("pekko.no.inline"), - pekkoInlineCoreProject := false + pekkoInlineEnabled := !sys.props.contains("pekko.no.inline") ) override lazy val projectSettings = Seq(Compile / scalacOptions ++= { if (pekkoInlineEnabled.value) { CrossVersion.partialVersion(scalaVersion.value) match { case Some((2, n)) if n == 12 | n == 13 => - flagsForScala2(pekkoInlineCoreProject.value) + flagsForScala2(pekkoCoreProject.value) case Some((3, _)) => flagsForScala3 } diff --git a/src/main/scala/com/github/pjfanning/pekkobuild/PekkoInlineSettings.scala b/src/main/scala/com/github/pjfanning/pekkobuild/PekkoInlineSettings.scala index fb74fc4..675f19a 100644 --- a/src/main/scala/com/github/pjfanning/pekkobuild/PekkoInlineSettings.scala +++ b/src/main/scala/com/github/pjfanning/pekkobuild/PekkoInlineSettings.scala @@ -5,6 +5,4 @@ import sbt.{SettingKey, settingKey} trait PekkoInlineSettings { lazy val pekkoInlineEnabled: SettingKey[Boolean] = settingKey("Whether to enable the Scala 2 inliner for Pekko modules." + "Defaults to pekko.no.inline property") - lazy val pekkoInlineCoreProject: SettingKey[Boolean] = settingKey("Whether this is the core Pekko project or a Pekko module " + - "since the core Pekko project has different inline settings. Defaults to false") }