-
Notifications
You must be signed in to change notification settings - Fork 20
Added CreateArchToSharedObjectsMapTask. #2023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3d13bfe
Added CollectSharedObjectFilesTask.
priettt 5c0d273
Add CreateArchToSharedObjectsMapTask and its integration test
priettt 19fd68b
Add CreateArchToSharedObjectsMapTask and an integration test for it
priettt 303a607
Rename class to CreateArchitectureToHashedObjectsMapTask, will change…
priettt ae3966d
Add shared object files compression to task
priettt 97a521a
Add compression integration test
priettt 18abadd
Divide Compress and Hash SharedObjectFiles tasks
priettt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
embrace-gradle-plugin-integration-tests/fixtures/compress-native-libs/build.gradle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import io.embrace.android.gradle.plugin.tasks.ndk.CompressSharedObjectFilesTask | ||
|
|
||
| plugins { | ||
| id("java") | ||
| id("io.embrace.swazzler") | ||
| id("io.embrace.android.testplugin") | ||
| } | ||
|
|
||
| project.tasks.register("compressTask", CompressSharedObjectFilesTask) { task -> | ||
| task.architecturesDirectory.set( | ||
| project.layout.projectDirectory.dir("testArchitecturesDir") | ||
| ) | ||
| task.compressedSharedObjectFilesDirectory.set( | ||
| project.layout.buildDirectory.dir("compressedSharedObjectFiles") | ||
| ) | ||
| integrationTest.configureEmbraceTask(task) | ||
| } |
Binary file added
BIN
+6.07 KB
...ation-tests/fixtures/compress-native-libs/testArchitecturesDir/arm64-v8a/libemb-crisps.so
Binary file not shown.
Binary file added
BIN
+6.07 KB
...ation-tests/fixtures/compress-native-libs/testArchitecturesDir/arm64-v8a/libemb-donuts.so
Binary file not shown.
Binary file added
BIN
+4.48 KB
...ion-tests/fixtures/compress-native-libs/testArchitecturesDir/armeabi-v7a/libemb-crisps.so
Binary file not shown.
Binary file added
BIN
+4.48 KB
...ion-tests/fixtures/compress-native-libs/testArchitecturesDir/armeabi-v7a/libemb-donuts.so
Binary file not shown.
Binary file added
BIN
+4.49 KB
...integration-tests/fixtures/compress-native-libs/testArchitecturesDir/x86/libemb-crisps.so
Binary file not shown.
Binary file added
BIN
+4.49 KB
...integration-tests/fixtures/compress-native-libs/testArchitecturesDir/x86/libemb-donuts.so
Binary file not shown.
Binary file added
BIN
+5.66 KB
...egration-tests/fixtures/compress-native-libs/testArchitecturesDir/x86_64/libemb-crisps.so
Binary file not shown.
Binary file added
BIN
+5.66 KB
...egration-tests/fixtures/compress-native-libs/testArchitecturesDir/x86_64/libemb-donuts.so
Binary file not shown.
18 changes: 18 additions & 0 deletions
18
embrace-gradle-plugin-integration-tests/fixtures/hash-native-libs/build.gradle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import io.embrace.android.gradle.plugin.tasks.ndk.HashSharedObjectFilesTask | ||
|
|
||
| plugins { | ||
| id("java") | ||
| id("io.embrace.swazzler") | ||
| id("io.embrace.android.testplugin") | ||
| } | ||
|
|
||
| // Register the hashing task | ||
| project.tasks.register("hashTask", HashSharedObjectFilesTask) { task -> | ||
| task.compressedSharedObjectFilesDirectory.set( | ||
| project.layout.projectDirectory.dir("compressedSharedObjectFiles") | ||
| ) | ||
| task.architecturesToHashedSharedObjectFilesMap.set( | ||
| project.layout.buildDirectory.file("output.json") | ||
| ) | ||
| integrationTest.configureEmbraceTask(task) | ||
| } |
Binary file added
BIN
+2.06 KB
...on-tests/fixtures/hash-native-libs/compressedSharedObjectFiles/arm64-v8a/libemb-crisps.so
Binary file not shown.
Binary file added
BIN
+2.06 KB
...on-tests/fixtures/hash-native-libs/compressedSharedObjectFiles/arm64-v8a/libemb-donuts.so
Binary file not shown.
Binary file added
BIN
+1.99 KB
...-tests/fixtures/hash-native-libs/compressedSharedObjectFiles/armeabi-v7a/libemb-crisps.so
Binary file not shown.
Binary file added
BIN
+1.99 KB
...-tests/fixtures/hash-native-libs/compressedSharedObjectFiles/armeabi-v7a/libemb-donuts.so
Binary file not shown.
Binary file added
BIN
+2.05 KB
...egration-tests/fixtures/hash-native-libs/compressedSharedObjectFiles/x86/libemb-crisps.so
Binary file not shown.
Binary file added
BIN
+2.06 KB
...egration-tests/fixtures/hash-native-libs/compressedSharedObjectFiles/x86/libemb-donuts.so
Binary file not shown.
Binary file added
BIN
+1.97 KB
...ation-tests/fixtures/hash-native-libs/compressedSharedObjectFiles/x86_64/libemb-crisps.so
Binary file not shown.
Binary file added
BIN
+1.97 KB
...ation-tests/fixtures/hash-native-libs/compressedSharedObjectFiles/x86_64/libemb-donuts.so
Binary file not shown.
77 changes: 77 additions & 0 deletions
77
...java/io/embrace/android/gradle/integration/testcases/CompressSharedObjectFilesTaskTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package io.embrace.android.gradle.integration.testcases | ||
|
|
||
| import io.embrace.android.gradle.integration.framework.PluginIntegrationTestRule | ||
| import io.embrace.android.gradle.integration.framework.file | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
|
|
||
| /** | ||
| * Tests the compression of shared object files. | ||
| * Given an input directory structure: | ||
| * testArchitecturesDir/ | ||
| * ├── arm64-v8a/ | ||
| * │ ├── libexample1.so | ||
| * │ └── libexample2.so | ||
| * └── armeabi-v7a/ | ||
| * ├── libexample1.so | ||
| * └── libexample2.so | ||
| * | ||
| * The task will create compressed files in: | ||
| * build/compressedSharedObjectFiles/ | ||
| * ├── arm64-v8a/ | ||
| * │ ├── libexample1.so (compressed) | ||
| * │ └── libexample2.so (compressed) | ||
| * └── armeabi-v7a/ | ||
| * ├── libexample1.so (compressed) | ||
| * └── libexample2.so (compressed) | ||
| */ | ||
| class CompressSharedObjectFilesTaskTest { | ||
|
|
||
| private val expectedSharedObjectFiles = listOf("libemb-donuts.so", "libemb-crisps.so") | ||
| private val expectedArchitectures = listOf("x86_64", "x86", "armeabi-v7a", "arm64-v8a") | ||
|
|
||
| @Rule | ||
| @JvmField | ||
| val rule: PluginIntegrationTestRule = PluginIntegrationTestRule() | ||
|
|
||
| @Test | ||
| fun `verify compression reduces folder size`() { | ||
| rule.runTest( | ||
| fixture = "compress-native-libs", | ||
| task = "compressTask", | ||
| setup = { projectDir -> | ||
| assertTrue(projectDir.file("testArchitecturesDir").exists()) | ||
| }, | ||
| assertions = { projectDir -> | ||
| val originalSize = projectDir.file("testArchitecturesDir") | ||
| .walk() | ||
| .filter { it.isFile } | ||
| .sumOf { it.length() } // returns size in bytes | ||
|
|
||
| val compressedSize = projectDir.file("build/compressedSharedObjectFiles") | ||
| .walk() | ||
| .filter { it.isFile } | ||
| .sumOf { it.length() } // returns size in bytes | ||
|
|
||
| assertTrue(compressedSize < originalSize) | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `verify compressed files maintain directory structure`() { | ||
| rule.runTest( | ||
| fixture = "compress-native-libs", | ||
| task = "compressTask", | ||
| assertions = { projectDir -> | ||
| expectedArchitectures.forEach { architecture -> | ||
| expectedSharedObjectFiles.forEach { sharedObjectFile -> | ||
| val compressedFile = projectDir.file("build/compressedSharedObjectFiles/$architecture/$sharedObjectFile") | ||
| assertTrue(compressedFile.exists()) | ||
| } | ||
| } | ||
| } | ||
| ) | ||
| } | ||
| } |
72 changes: 72 additions & 0 deletions
72
...est/java/io/embrace/android/gradle/integration/testcases/HashSharedObjectFilesTaskTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package io.embrace.android.gradle.integration.testcases | ||
|
|
||
| import io.embrace.android.gradle.integration.framework.PluginIntegrationTestRule | ||
| import io.embrace.android.gradle.integration.framework.file | ||
| import io.embrace.android.gradle.plugin.hash.calculateSha1ForFile | ||
| import io.embrace.android.gradle.plugin.tasks.ndk.ArchitecturesToHashedSharedObjectFilesMap | ||
| import io.embrace.android.gradle.plugin.util.serialization.MoshiSerializer | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
|
|
||
| /** | ||
| * Tests the hashing of compressed shared object files. | ||
| * Given compressed files in: | ||
| * compressedSharedObjectFiles/ | ||
| * ├── arm64-v8a/ | ||
| * │ ├── libexample1.so (compressed) | ||
| * │ └── libexample2.so (compressed) | ||
| * └── armeabi-v7a/ | ||
| * ├── libexample1.so (compressed) | ||
| * └── libexample2.so (compressed) | ||
| * | ||
| * The task will create a JSON map where: | ||
| * - Keys are architecture names (e.g., "arm64-v8a") | ||
| * - Values are maps where: | ||
| * - Keys are shared object filenames (e.g., "libexample1.so") | ||
| * - Values are SHA1 hashes of the compressed files | ||
| * { | ||
| * "arm64-v8a": { | ||
| * "libexample1.so": "2a21dc0b99017d5db5960b80d94815a0fe0f3fc2", | ||
| * "libexample2.so": "3b32ed1c88128e6ec4b71b93a4926a1bf1f4gd3" | ||
| * }, | ||
| * "armeabi-v7a": { | ||
| * "libexample1.so": "4c43fe2d77239f7fd5a71c91b85926b2g2g5he4", | ||
| * "libexample2.so": "5d54gf3e66340g8ge6b82da2c96a37c3h3h6if5" | ||
| * } | ||
| * } | ||
| */ | ||
| class HashSharedObjectFilesTaskTest { | ||
|
|
||
| private val expectedSharedObjectFiles = listOf("libemb-donuts.so", "libemb-crisps.so") | ||
| private val expectedArchitectures = listOf("x86_64", "x86", "armeabi-v7a", "arm64-v8a") | ||
|
|
||
| @Rule | ||
| @JvmField | ||
| val rule: PluginIntegrationTestRule = PluginIntegrationTestRule() | ||
|
|
||
| @Test | ||
| fun `map output is correct`() { | ||
| rule.runTest( | ||
| fixture = "hash-native-libs", | ||
| task = "hashTask", | ||
| setup = { projectDir -> | ||
| assertTrue(projectDir.file("compressedSharedObjectFiles").exists()) | ||
| }, | ||
| assertions = { projectDir -> | ||
| val deserializedOutputMap = MoshiSerializer().fromJson( | ||
| projectDir.file("build/output.json").readText(), | ||
| ArchitecturesToHashedSharedObjectFilesMap::class.java | ||
| ).architecturesToHashedSharedObjectFiles | ||
|
|
||
| expectedArchitectures.forEach { architecture -> | ||
| expectedSharedObjectFiles.forEach { sharedObjectFile -> | ||
| val sha1Hash = deserializedOutputMap[architecture]?.get(sharedObjectFile) | ||
| val compressedSoFile = projectDir.file("compressedSharedObjectFiles/$architecture/$sharedObjectFile") | ||
| assertTrue(sha1Hash == calculateSha1ForFile(compressedSoFile)) | ||
| } | ||
| } | ||
| } | ||
| ) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...a/io/embrace/android/gradle/plugin/tasks/ndk/ArchitecturesToHashedSharedObjectFilesMap.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package io.embrace.android.gradle.plugin.tasks.ndk | ||
|
|
||
| import com.squareup.moshi.JsonClass | ||
|
|
||
| /** | ||
| * Data class representing a mapping of architectures to their shared object files and corresponding hashes. | ||
| * The structure is: | ||
| * - Keys are architecture names (e.g., "arm64-v8a") | ||
| * - Values are maps where: | ||
| * - Keys are shared object filenames (e.g., "libexample1.so") | ||
| * - Values are SHA1 hashes of the compressed files | ||
| */ | ||
| @JsonClass(generateAdapter = true) | ||
| data class ArchitecturesToHashedSharedObjectFilesMap( | ||
| val architecturesToHashedSharedObjectFiles: Map<String, Map<String, String>> | ||
| ) |
81 changes: 81 additions & 0 deletions
81
...n/java/io/embrace/android/gradle/plugin/tasks/ndk/CompressAndHashSharedObjectFilesTask.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,81 @@ | ||||||
| package io.embrace.android.gradle.plugin.tasks.ndk | ||||||
|
|
||||||
| import io.embrace.android.gradle.plugin.hash.calculateSha1ForFile | ||||||
| import io.embrace.android.gradle.plugin.tasks.EmbraceTaskImpl | ||||||
| import io.embrace.android.gradle.plugin.util.compression.ZstdFileCompressor | ||||||
| import io.embrace.android.gradle.plugin.util.serialization.MoshiSerializer | ||||||
| import org.gradle.api.file.DirectoryProperty | ||||||
| import org.gradle.api.file.RegularFileProperty | ||||||
| import org.gradle.api.model.ObjectFactory | ||||||
| import org.gradle.api.tasks.InputDirectory | ||||||
| import org.gradle.api.tasks.OutputDirectory | ||||||
| import org.gradle.api.tasks.OutputFile | ||||||
| import org.gradle.api.tasks.SkipWhenEmpty | ||||||
| import org.gradle.api.tasks.TaskAction | ||||||
| import java.io.File | ||||||
| import javax.inject.Inject | ||||||
|
|
||||||
| /** | ||||||
| * Given a directory containing architecture directories (arm64-v8a, armeabi-v7a, etc.) with shared object files (.so files), | ||||||
| * this task: | ||||||
| * 1. Compresses each shared object file using Zstd compression | ||||||
| * 2. Stores the compressed files in compressedSharedObjectFilesDirectory, preserving the architecture directory structure | ||||||
| * 3. Calculates SHA1 hashes of the compressed files | ||||||
| * 4. Creates a JSON mapping of architectures to a map of shared object filenames to their hashes | ||||||
| */ | ||||||
| abstract class CompressAndHashSharedObjectFilesTask @Inject constructor( | ||||||
| objectFactory: ObjectFactory, | ||||||
| ) : EmbraceTaskImpl(objectFactory) { | ||||||
|
|
||||||
| private val serializer = MoshiSerializer() | ||||||
| private val compressor = ZstdFileCompressor() | ||||||
|
|
||||||
| @get:InputDirectory | ||||||
| @get:SkipWhenEmpty | ||||||
| val architecturesDirectory: DirectoryProperty = objectFactory.directoryProperty() | ||||||
|
|
||||||
| @get:OutputDirectory | ||||||
| val compressedSharedObjectFilesDirectory: DirectoryProperty = objectFactory.directoryProperty() | ||||||
|
|
||||||
| @get:OutputFile | ||||||
| val architecturesToHashedSharedObjectFilesMap: RegularFileProperty = objectFactory.fileProperty() | ||||||
|
|
||||||
| @TaskAction | ||||||
| fun onRun() { | ||||||
| val outputMap = createOutputMap() | ||||||
|
|
||||||
| // Serialize the map to JSON and write it to the output file | ||||||
| val serializableMap = ArchitecturesToHashedSharedObjectFilesMap(outputMap) | ||||||
| architecturesToHashedSharedObjectFilesMap.get().asFile.outputStream().use { outputStream -> | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
It'd be worth using a buffered stream here to improve perf |
||||||
| serializer.toJson(serializableMap, ArchitecturesToHashedSharedObjectFilesMap::class.java, outputStream) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private fun createOutputMap(): Map<String, Map<String, String>> = | ||||||
| architecturesDirectory.get().asFile | ||||||
| .listFiles() | ||||||
| ?.filter { it.isDirectory && it.listFiles()?.isNotEmpty() == true } | ||||||
| ?.associate { archDir -> | ||||||
| archDir.name to mapSharedObjectsToHashes(archDir) | ||||||
| } ?: error("Compression and hashing of shared object files failed") | ||||||
|
|
||||||
| private fun mapSharedObjectsToHashes(architectureDir: File): Map<String, String> { | ||||||
| val outputDirectory = compressedSharedObjectFilesDirectory.dir(architectureDir.name).get().asFile | ||||||
| val sharedObjectFiles = architectureDir.listFiles { file -> | ||||||
| file.name.endsWith(".so") | ||||||
| } ?: error("Shared object files not found") // Should never happen | ||||||
|
|
||||||
| return sharedObjectFiles.associate { it.name to compressAndHashSharedObjectFile(it, outputDirectory) } | ||||||
| } | ||||||
|
|
||||||
| private fun compressAndHashSharedObjectFile(sharedObjectFile: File, outputDirectory: File): String { | ||||||
| val compressedFile = File(outputDirectory, sharedObjectFile.name) | ||||||
| return compressor.compress(sharedObjectFile, compressedFile)?.let { | ||||||
| calculateSha1ForFile(it) | ||||||
| } ?: error("Compression and hashing of shared object file ${sharedObjectFile.name} failed") | ||||||
| } | ||||||
|
|
||||||
| companion object { | ||||||
| const val NAME: String = "compressAndHashSharedObjectFiles" | ||||||
| } | ||||||
| } | ||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't realise we were doing Zstd compression on SO files. IMO it'd be worth breaking that into a separate task that takes the uncompressed SO files as an input and the compressed files as an output. Sorry for the confusion from our discussion yesterday