Skip to content

Commit 8d48ef2

Browse files
committed
Rewrite Build.scala for readability
1 parent 522c1f2 commit 8d48ef2

File tree

2 files changed

+180
-111
lines changed

2 files changed

+180
-111
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*.log
44
*~
55

6+
*.sublime*
7+
68
# sbt specific
79
dist/*
810
target/

project/Build.scala

Lines changed: 178 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,109 @@ import java.io.{ RandomAccessFile, File }
44
import java.nio.channels.FileLock
55
import scala.reflect.io.Path
66

7-
object DottyBuild extends Build {
7+
trait BuildUtils {
8+
def buildOpts(envVarName: String, opts: List[String]) =
9+
if (sys.props.isDefinedAt(envVarName)) opts
10+
else Nil
11+
}
812

9-
val travisMemLimit = List("-Xmx1g", "-Xss2m")
13+
trait CIBuild extends BuildUtils {
1014

1115
val JENKINS_BUILD = "dotty.jenkins.build"
1216

13-
val agentOptions = List(
14-
// "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"
15-
// "-agentpath:/home/dark/opt/yjp-2013-build-13072/bin/linux-x86-64/libyjpagent.so"
16-
// "-agentpath:/Applications/YourKit_Java_Profiler_2015_build_15052.app/Contents/Resources/bin/mac/libyjpagent.jnilib",
17-
// "-XX:+HeapDumpOnOutOfMemoryError", "-Xmx1g", "-Xss2m"
18-
)
17+
val travisMemLimit = List("-Xmx1g", "-Xss2m")
18+
19+
val travisBuild = // propagate if this is a travis build
20+
buildOpts(JENKINS_BUILD,
21+
List(s"-D$JENKINS_BUILD=${sys.props(JENKINS_BUILD)}") :::
22+
travisMemLimit)
23+
24+
val tuning =
25+
buildOpts("Oshort",
26+
// Optimize for short-running applications,
27+
// see https://github.com/lampepfl/dotty/issues/222
28+
List("-XX:+TieredCompilation", "-XX:TieredStopAtLevel=1"))
29+
30+
}
31+
32+
trait DebugBuild extends BuildUtils {
1933

20-
val defaults = Defaults.coreDefaultSettings ++ Seq(
34+
// YOURKIT_AGENT should point to 'libyjpagent.so' location
35+
val YOURKIT_AGENT = "YOURKIT_AGENT_LIB"
36+
37+
val agentOptions =
38+
buildOpts(YOURKIT_AGENT,
39+
List(
40+
"-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005",
41+
s"-agentpath:${YOURKIT_AGENT}",
42+
"-XX:+HeapDumpOnOutOfMemoryError", "-Xmx1g", "-Xss2m"))
43+
44+
}
45+
46+
object DottyBuild extends Build with CIBuild with DebugBuild {
47+
48+
lazy val dotty = Project(id = "dotty", base = file("."),
49+
settings = defaults)
50+
51+
def scalaCompilerVersion = "2.11.5-20151022-113908-7fb0e653fd"
52+
53+
lazy val defaults = Defaults.coreDefaultSettings ++ Seq(
2154
scalaVersion in Global := "2.11.5",
22-
version in Global := "0.1-SNAPSHOT",
55+
version in Global := "0.1-SNAPSHOT",
2356
organization in Global := "org.scala-lang",
24-
organizationName in Global := "LAMP/EPFL",
57+
organizationName in Global := "LAMP/EPFL",
2558
organizationHomepage in Global := Some(url("http://lamp.epfl.ch")),
26-
homepage in Global := Some(url("https://github.com/lampepfl/dotty")),
59+
homepage in Global := Some(url("https://github.com/lampepfl/dotty")),
60+
61+
// to get Scala 2.11
62+
resolvers += Resolver.sonatypeRepo("releases"),
2763

2864
// set sources to src/, tests to test/ and resources to resources/
2965
scalaSource in Compile := baseDirectory.value / "src",
30-
javaSource in Compile := baseDirectory.value / "src",
31-
scalaSource in Test := baseDirectory.value / "test",
32-
javaSource in Test := baseDirectory.value / "test",
66+
javaSource in Compile := baseDirectory.value / "src",
67+
scalaSource in Test := baseDirectory.value / "test",
68+
javaSource in Test := baseDirectory.value / "test",
3369
resourceDirectory in Compile := baseDirectory.value / "resources",
34-
unmanagedSourceDirectories in Compile := Seq((scalaSource in Compile).value),
35-
unmanagedSourceDirectories in Test := Seq((scalaSource in Test).value),
70+
unmanagedSourceDirectories in Compile :=
71+
Seq((scalaSource in Compile).value),
72+
unmanagedSourceDirectories in Test := Seq((scalaSource in Test).value),
3673

3774
// include sources in eclipse (downloads source code for all dependencies)
38-
//http://stackoverflow.com/questions/10472840/how-to-attach-sources-to-sbt-managed-dependencies-in-scala-ide#answer-11683728
39-
com.typesafe.sbteclipse.plugin.EclipsePlugin.EclipseKeys.withSource := true,
75+
// http://stackoverflow.com/questions/10472840/\
76+
// how-to-attach-sources-to-sbt-managed-dependencies\
77+
// -in-scala-ide#answer-11683728
78+
com.typesafe.sbteclipse.plugin.
79+
EclipsePlugin.EclipseKeys.withSource := true,
4080

41-
// to get Scala 2.11
42-
resolvers += Resolver.sonatypeRepo("releases"),
4381

4482
// get libraries onboard
45-
partestDeps := Seq("me.d-d" % "scala-compiler" % "2.11.5-20151022-113908-7fb0e653fd",
46-
"org.scala-lang" % "scala-reflect" % scalaVersion.value,
47-
"org.scala-lang" % "scala-library" % scalaVersion.value % "test"),
83+
partestDeps :=
84+
Seq("me.d-d" % "scala-compiler" % scalaCompilerVersion,
85+
"org.scala-lang" % "scala-reflect" % scalaVersion.value,
86+
"org.scala-lang" % "scala-library" % scalaVersion.value % "test"),
4887
libraryDependencies ++= partestDeps.value,
49-
libraryDependencies ++= Seq("org.scala-lang.modules" %% "scala-xml" % "1.0.1",
50-
"org.scala-lang.modules" %% "scala-partest" % "1.0.5" % "test",
51-
"com.novocode" % "junit-interface" % "0.11" % "test",
52-
"jline" % "jline" % "2.12"),
88+
libraryDependencies ++=
89+
Seq("org.scala-lang.modules" %% "scala-xml" % "1.0.1",
90+
"org.scala-lang.modules" %% "scala-partest" % "1.0.5" % "test",
91+
"com.novocode" % "junit-interface" % "0.11" % "test",
92+
"jline" % "jline" % "2.12"),
5393

5494
// scalac options
5595
scalacOptions in Global ++= Seq("-feature", "-deprecation", "-language:_"),
56-
57-
javacOptions ++= Seq("-Xlint:unchecked", "-Xlint:deprecation"),
96+
javacOptions ++= Seq("-Xlint:unchecked", "-Xlint:deprecation"),
5897

5998
// enable improved incremental compilation algorithm
6099
incOptions := incOptions.value.withNameHashing(true),
61100

101+
// Adjust classpath for running dotty
102+
mainClass in (Compile, run) := Some("dotty.tools.dotc.Main"),
103+
104+
// --- Various test tasks and options ---
105+
62106
// enable verbose exception messages for JUnit
63-
testOptions in Test += Tests.Argument(TestFrameworks.JUnit, "-a", "-v", "--run-listener=test.ContextEscapeDetector"),
107+
testOptions in Test += Tests.Argument(
108+
TestFrameworks.JUnit, "-a", "-v",
109+
"--run-listener=test.ContextEscapeDetector"),
64110
testOptions in Test += Tests.Cleanup({ () => partestLockFile.delete }),
65111

66112
lockPartestFile := {
@@ -70,58 +116,59 @@ object DottyBuild extends Build {
70116
lockDir.mkdirs
71117
// Cannot have concurrent partests as they write to the same directory.
72118
if (lockDir.list.size > 0)
73-
throw new RuntimeException("ERROR: sbt partest: another partest is already running, pid in lock file: " + lockDir.list.toList.mkString(" "))
119+
throw new RuntimeException(
120+
s"""|ERROR: sbt partest: another partest is already running,
121+
| pid in lock file: ${lockDir.list.mkString(" ")}""")
74122
partestLockFile.createNewFile
75123
partestLockFile.deleteOnExit
76124
},
125+
77126
runPartestRunner <<= Def.inputTaskDyn {
78127
// Magic! This is both an input task and a dynamic task. Apparently
79128
// command line arguments get passed to the last task in an aliased
80129
// sequence (see partest alias below), so this works.
81130
val args = Def.spaceDelimited("<arg>").parsed
82-
val jars = Seq((packageBin in Compile).value.getAbsolutePath) ++
83-
getJarPaths(partestDeps.value, ivyPaths.value.ivyHome)
84-
val dottyJars = "-dottyJars " + (jars.length + 1) + " dotty.jar" + " " + jars.mkString(" ")
131+
val dottyJars =
132+
Seq("dotty.jar", (packageBin in Compile).value.getAbsolutePath) ++
133+
getJarPaths(partestDeps.value, ivyPaths.value.ivyHome)
134+
135+
val dottyJarsConf =
136+
Seq(s"-dottyJars ${dottyJars.length}") ++ dottyJars ++ args
85137
// Provide the jars required on the classpath of run tests
86-
runTask(Test, "dotty.partest.DPConsoleRunner", dottyJars + " " + args.mkString(" "))
138+
runTask(Test, "dotty.partest.DPConsoleRunner",
139+
dottyJarsConf.mkString(" "))
87140
},
88141

89-
// Adjust classpath for running dotty
90-
mainClass in (Compile, run) := Some("dotty.tools.dotc.Main"),
91142
fork in run := true,
92143
fork in Test := true,
93144
parallelExecution in Test := false,
94145

95-
// http://grokbase.com/t/gg/simple-build-tool/135ke5y90p/sbt-setting-jvm-boot-paramaters-for-scala
96-
javaOptions <++= (managedClasspath in Runtime, packageBin in Compile) map { (attList, bin) =>
97-
// put the Scala {library, reflect} in the classpath
98-
val path = for {
99-
file <- attList.map(_.data)
100-
path = file.getAbsolutePath
101-
} yield "-Xbootclasspath/p:" + path
102-
// dotty itself needs to be in the bootclasspath
103-
val fullpath = ("-Xbootclasspath/a:" + bin) :: path.toList
104-
// System.err.println("BOOTPATH: " + fullpath)
105-
106-
val travis_build = // propagate if this is a travis build
107-
if (sys.props.isDefinedAt(JENKINS_BUILD))
108-
List(s"-D$JENKINS_BUILD=${sys.props(JENKINS_BUILD)}") ::: travisMemLimit
109-
else
110-
List()
111-
112-
val tuning =
113-
if (sys.props.isDefinedAt("Oshort"))
114-
// Optimize for short-running applications, see https://github.com/lampepfl/dotty/issues/222
115-
List("-XX:+TieredCompilation", "-XX:TieredStopAtLevel=1")
116-
else
117-
List()
118-
119-
("-DpartestParentID=" + pid) :: tuning ::: agentOptions ::: travis_build ::: fullpath
120-
}
121-
) ++ addCommandAlias("partest", ";test:package;package;test:runMain dotc.build;lockPartestFile;test:test;runPartestRunner") ++
122-
addCommandAlias("partest-only", ";test:package;package;test:runMain dotc.build;lockPartestFile;test:test-only dotc.tests;runPartestRunner")
146+
// http://grokbase.com/t/gg/simple-build-tool/135ke5y90p/
147+
// sbt-setting-jvm-boot-paramaters-for-scala
148+
javaOptions <++=
149+
(managedClasspath in Runtime, packageBin in Compile) map {
150+
(attList, bin) =>
151+
// put the Scala {library, reflect} in the classpath
152+
val path = for {
153+
file <- attList.map(_.data)
154+
path = file.getAbsolutePath
155+
} yield "-Xbootclasspath/p:" + path
156+
// dotty itself needs to be in the bootclasspath
157+
val fullpath = ("-Xbootclasspath/a:" + bin) :: path.toList
158+
// System.err.println("BOOTPATH: " + fullpath)
159+
160+
("-DpartestParentID=" + pid) :: tuning ::: agentOptions :::
161+
travisBuild ::: fullpath
162+
}
163+
) ++ partestAlias ++ partestOnlyAlias
164+
165+
lazy val partestAlias = addCommandAlias("partest",
166+
";test:package;package;test:runMain dotc.build;" +
167+
"lockPartestFile;test:test;runPartestRunner")
168+
lazy val partestOnlyAlias = addCommandAlias("partest-only",
169+
";test:package;package;test:runMain dotc.build;" +
170+
"lockPartestFile;test:test-only dotc.tests;runPartestRunner")
123171

124-
lazy val dotty = Project(id = "dotty", base = file("."), settings = defaults)
125172

126173
lazy val benchmarkSettings = Defaults.coreDefaultSettings ++ Seq(
127174

@@ -131,62 +178,82 @@ object DottyBuild extends Build {
131178
baseDirectory in (Test,run) := (baseDirectory in dotty).value,
132179

133180

134-
libraryDependencies ++= Seq("com.storm-enroute" %% "scalameter" % "0.6" % Test,
181+
libraryDependencies ++= Seq(
182+
"com.storm-enroute" %% "scalameter" % "0.6" % Test,
135183
"com.novocode" % "junit-interface" % "0.11"),
136184
testFrameworks += new TestFramework("org.scalameter.ScalaMeterFramework"),
137185

138-
// scalac options
139186
scalacOptions in Global ++= Seq("-feature", "-deprecation", "-language:_"),
140-
141187
javacOptions ++= Seq("-Xlint:unchecked", "-Xlint:deprecation"),
142188

143189
fork in Test := true,
144190
parallelExecution in Test := false,
145191

146-
// http://grokbase.com/t/gg/simple-build-tool/135ke5y90p/sbt-setting-jvm-boot-paramaters-for-scala
147-
javaOptions <++= (dependencyClasspath in Runtime, packageBin in Compile) map { (attList, bin) =>
148-
// put the Scala {library, reflect, compiler} in the classpath
149-
val path = for {
150-
file <- attList.map(_.data)
151-
path = file.getAbsolutePath
152-
prefix = if (path.endsWith(".jar")) "p" else "a"
153-
} yield "-Xbootclasspath/" + prefix + ":" + path
154-
// dotty itself needs to be in the bootclasspath
155-
val fullpath = ("-Xbootclasspath/a:" + bin) :: path.toList
156-
// System.err.println("BOOTPATH: " + fullpath)
157-
158-
val travis_build = // propagate if this is a travis build
159-
if (sys.props.isDefinedAt(JENKINS_BUILD))
160-
List(s"-D$JENKINS_BUILD=${sys.props(JENKINS_BUILD)}")
161-
else
162-
List()
163-
val res = agentOptions ::: travis_build ::: fullpath
164-
println("Running with javaOptions: " + res)
165-
res
166-
}
192+
// http://grokbase.com/t/gg/simple-build-tool/135ke5y90p/\
193+
// sbt-setting-jvm-boot-paramaters-for-scala
194+
javaOptions <++=
195+
(dependencyClasspath in Runtime, packageBin in Compile) map {
196+
(attList, bin) =>
197+
// put the Scala {library, reflect, compiler} in the classpath
198+
val path = for {
199+
file <- attList.map(_.data)
200+
path = file.getAbsolutePath
201+
prefix = if (path.endsWith(".jar")) "p" else "a"
202+
} yield "-Xbootclasspath/" + prefix + ":" + path
203+
// dotty itself needs to be in the bootclasspath
204+
val fullpath = ("-Xbootclasspath/a:" + bin) :: path.toList
205+
// System.err.println("BOOTPATH: " + fullpath)
206+
207+
val travis_build = // propagate if this is a travis build
208+
if (sys.props.isDefinedAt(JENKINS_BUILD))
209+
List(s"-D$JENKINS_BUILD=${sys.props(JENKINS_BUILD)}")
210+
else
211+
List()
212+
val opts = agentOptions ::: travis_build ::: fullpath
213+
println(s"Running with javaOptions: $opts")
214+
opts
215+
}
167216
)
168217

169218

170-
lazy val benchmarks = Project(id = "dotty-bench", settings = benchmarkSettings,
171-
base = file("bench")) dependsOn(dotty % "compile->test")
219+
lazy val benchmarks =
220+
Project(id = "dotty-bench", settings = benchmarkSettings,
221+
base = file("bench")) dependsOn(dotty % "compile->test")
172222

173223
// Partest tasks
174-
lazy val lockPartestFile = TaskKey[Unit]("lockPartestFile", "Creates the lock file at ./tests/locks/partest-<pid>.lock")
175-
lazy val partestLockFile = new File("." + File.separator + "tests" + File.separator + "locks" + File.separator + s"partest-$pid.lock")
176-
def pid = java.lang.Long.parseLong(java.lang.management.ManagementFactory.getRuntimeMXBean().getName().split("@")(0))
177-
178-
lazy val runPartestRunner = InputKey[Unit]("runPartestRunner", "Runs partest")
179-
180-
lazy val partestDeps = SettingKey[Seq[ModuleID]]("partestDeps", "Finds jars for partest dependencies")
181-
def getJarPaths(modules: Seq[ModuleID], ivyHome: Option[File]): Seq[String] = ivyHome match {
182-
case Some(home) =>
183-
modules.map({ module =>
184-
val file = Path(home) / Path("cache") /
185-
Path(module.organization) / Path(module.name) / Path("jars") /
186-
Path(module.name + "-" + module.revision + ".jar")
187-
if (!file.isFile) throw new RuntimeException("ERROR: sbt getJarPaths: dependency jar not found: " + file)
188-
else file.jfile.getAbsolutePath
189-
})
190-
case None => throw new RuntimeException("ERROR: sbt getJarPaths: ivyHome not defined")
191-
}
224+
lazy val lockPartestFile =
225+
TaskKey[Unit]("lockPartestFile",
226+
"Creates the lock file at ./tests/locks/partest-<pid>.lock")
227+
lazy val partestLockFile =
228+
new File(
229+
List(".", "tests", "locks", s"partest-$pid.lock").
230+
reduce(_ + File.separator + _))
231+
232+
def pid = java.lang.Long.parseLong(
233+
java.lang.management.ManagementFactory.getRuntimeMXBean().
234+
getName().split("@")(0))
235+
236+
lazy val runPartestRunner =
237+
InputKey[Unit]("runPartestRunner", "Runs partest")
238+
239+
lazy val partestDeps =
240+
SettingKey[Seq[ModuleID]]("partestDeps",
241+
"Finds jars for partest dependencies")
242+
243+
def getJarPaths(modules: Seq[ModuleID], ivyHome: Option[File]) =
244+
ivyHome map { home =>
245+
modules map { module =>
246+
val modJar =
247+
Path(home) / Path("cache") / Path(module.organization) /
248+
Path(module.name) / Path("jars") /
249+
Path(module.name + "-" + module.revision + ".jar")
250+
if (!modJar.isFile)
251+
throw new RuntimeException(
252+
s"ERROR: sbt getJarPaths: dependency jar not found: $modJar")
253+
else modJar.jfile.getAbsolutePath
254+
}
255+
} getOrElse {
256+
throw new RuntimeException(
257+
s"ERROR: sbt getJarPaths: ivyHome not defined")
258+
}
192259
}

0 commit comments

Comments
 (0)