Skip to content

Commit

Permalink
Bug 1946226 - Don't parse TOML multiple times. r=janerik,tthibaud
Browse files Browse the repository at this point in the history
We currently parse `Cargo.lock` for every single Gradle project. This is unnecessary: once is enough. Moreover, this can be a parallelizable and build-cache friendly task. This patch places the task in the :geckoview project, since that's where the version of native Glean is most relevant

Differential Revision: https://phabricator.services.mozilla.com/D234384
  • Loading branch information
AadityaMozilla committed Feb 10, 2025
1 parent 333bd9c commit 44f3f62
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
54 changes: 37 additions & 17 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,55 @@ def tryInt = { string ->
return string
}

// Parses the Cargo.lock and returns the version for the given package name.
def getRustVersionFor(packageName) {
String version = null;
TomlParseResult result = Toml.parse(file("Cargo.lock").getText());
for (object in result.getArray("package").toList()) {
def table = (TomlTable) object
if (table.getString("name") == packageName) {
if (version != null) {
throw new StopExecutionException("Multiple versions for '${packageName}' found." +
" Ensure '${packageName}' is only included once.")
abstract class VerifyGleanVersionTask extends DefaultTask {
@InputFile
final RegularFileProperty source = project.objects.fileProperty().convention(project.layout.projectDirectory.file("Cargo.lock"))

@Input
String expectedVersion = Versions.mozilla_glean

@OutputFiles
FileCollection outputFiles = project.objects.fileCollection()

@TaskAction
void verifyGleanVersion() {
def foundVersion = getRustVersionFor(source.get().asFile, "glean")
if (expectedVersion != foundVersion) {
throw new GradleException("Mismatched Glean version, expected: '${expectedVersion}'," +
" found '${foundVersion}'")
} else {
logger.lifecycle("verifyGleanVersion> expected version matches found version '${foundVersion}'")
}
}

// Parses the Cargo.lock and returns the version for the given package name.
static String getRustVersionFor(file, packageName) {
String version = null;
TomlParseResult result = Toml.parse(file.getText());
for (object in result.getArray("package").toList()) {
def table = (TomlTable) object
if (table.getString("name") == packageName) {
if (version != null) {
throw new GradleException("Multiple versions for '${packageName}' found." +
" Ensure '${packageName}' is only included once.")
}
version = table.getString("version")
}
version = table.getString("version")
}
return version
}
return version
}

tasks.register("verifyGleanVersion", VerifyGleanVersionTask)

allprojects {
// Expose the per-object-directory configuration to all projects.
ext {
mozconfig = gradle.mozconfig
topsrcdir = gradle.mozconfig.topsrcdir
topobjdir = gradle.mozconfig.topobjdir

gleanVersion = Versions.mozilla_glean
if (gleanVersion != getRustVersionFor("glean")) {
throw new StopExecutionException("Mismatched Glean version, expected: ${gleanVersion}," +
" found ${getRustVersionFor("glean")}")
}
gleanVersion = Versions.mozilla_glean // Verification done in verifyGleanVersion task

artifactSuffix = getArtifactSuffix()
versionName = getVersionName()
Expand Down
2 changes: 2 additions & 0 deletions mobile/android/geckoview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,8 @@ task("generateSDKBindings", type: JavaExec) {
dependsOn project(':annotations').jar
}

preBuild.dependsOn(":verifyGleanVersion")

apply plugin: 'org.mozilla.apilint'

apiLint {
Expand Down

0 comments on commit 44f3f62

Please sign in to comment.