Skip to content

Commit c324144

Browse files
committed
NI size is checked in sbt during buildEngineDistribution
1 parent 18d496f commit c324144

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

build.sbt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ lazy val Benchmark = config("bench") extend sbt.Test
305305
lazy val rebuildNativeImage = taskKey[Unit]("Force to rebuild native image")
306306
lazy val buildNativeImage =
307307
taskKey[Unit]("Ensure that the Native Image is built.")
308+
lazy val checkNativeImageSize =
309+
taskKey[Unit]("Ensures the generated Native Image has reasonable size")
308310

309311
// ============================================================================
310312
// === Global Project =========================================================
@@ -4064,7 +4066,16 @@ lazy val `engine-runner` = project
40644066
"enso",
40654067
targetDir = engineDistributionRoot.value / "bin"
40664068
)
4067-
}.value
4069+
}.value,
4070+
checkNativeImageSize := Def
4071+
.taskDyn {
4072+
NativeImage.checkNativeImageSize(
4073+
name = "enso",
4074+
targetDir = engineDistributionRoot.value / "bin"
4075+
)
4076+
}
4077+
.dependsOn(buildNativeImage)
4078+
.value
40684079
)
40694080
.dependsOn(`version-output`)
40704081
.dependsOn(pkg)
@@ -5671,6 +5682,7 @@ buildEngineDistributionNoIndex := Def.taskIf {
56715682
createEnginePackageNoIndex.value
56725683
if (shouldBuildNativeImage.value) {
56735684
(`engine-runner` / buildNativeImage).value
5685+
(`engine-runner` / checkNativeImageSize).value
56745686
}
56755687
}.value
56765688

project/NativeImage.scala

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,43 @@ object NativeImage {
320320
}
321321
}
322322

323+
def checkNativeImageSize(
324+
name: String,
325+
targetDir: File
326+
): Def.Initialize[Task[Unit]] = Def.task {
327+
val generatedBin = artifactFile(targetDir, name)
328+
val logger = streams.value.log
329+
if (!generatedBin.exists) {
330+
logger.error(s"Generated binary $generatedBin does not exist.")
331+
logger.error(
332+
"Ensure that the dependency on `buildNativeImage` is properly set."
333+
)
334+
}
335+
val bytes = generatedBin.attributes.size()
336+
val productionNIBounds = (150, 450)
337+
val testNIBounds = (110, 550)
338+
val mb = bytes / (1024 * 1024)
339+
val bounds = if (GraalVM.EnsoLauncher.release) {
340+
productionNIBounds
341+
} else {
342+
testNIBounds
343+
}
344+
val isInBounds =
345+
bounds._1 <= mb && mb <= bounds._2
346+
if (!isInBounds) {
347+
logger.error(
348+
s"Generated binary $generatedBin has unexpected size: $mb MB. " +
349+
s"Expected size is between ${bounds._1} and ${bounds._2} MB."
350+
)
351+
throw new RuntimeException(s"Generated binary $generatedBin is too large")
352+
} else {
353+
logger.info(
354+
s"Generated binary $generatedBin size ($mb MB) " +
355+
s"is within the expected size: [${bounds._1}, ${bounds._2}] MB."
356+
)
357+
}
358+
}
359+
323360
/** [[File]] representing the artifact called `name` built with the Native
324361
* Image.
325362
*/

0 commit comments

Comments
 (0)