From b1b3b7592f050a699a9faac74e5eb93090e4dc95 Mon Sep 17 00:00:00 2001 From: Matthew de Detrich Date: Sat, 30 Dec 2023 15:56:42 +1100 Subject: [PATCH] Add PekkoInlinePlugin --- .../pekkobuild/PekkoInlinePlugin.scala | 57 +++++++++++++++++++ .../pekkobuild/PekkoInlineSettings.scala | 8 +++ 2 files changed, 65 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..dd519a3 --- /dev/null +++ b/src/main/scala/com/github/pjfanning/pekkobuild/PekkoInlinePlugin.scala @@ -0,0 +1,57 @@ +/* + * 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 + + private val flagsForScala2 = Seq( + "-opt-inline-from:", + "-opt:l:inline", + // These are safe to inline even across modules since they are + // wrappers for cross compilation that is stable within Pekko + "-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**" + ) + + object autoImport extends PekkoInlineSettings + + import autoImport._ + + // 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") + ) + + override lazy val projectSettings = Seq(Compile / scalacOptions ++= { + if (pekkoInlineEnabled.value) { + CrossVersion.partialVersion(scalaVersion.value) match { + case Some((2, n)) if n == 13 => + flagsForScala2 + case Some((2, n)) if n == 12 => + flagsForScala2 + 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..675f19a --- /dev/null +++ b/src/main/scala/com/github/pjfanning/pekkobuild/PekkoInlineSettings.scala @@ -0,0 +1,8 @@ +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") +}