1 /*
<lambda>null2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.build
18 
19 import org.gradle.api.DefaultTask
20 import org.gradle.api.file.ConfigurableFileCollection
21 import org.gradle.api.tasks.CacheableTask
22 import org.gradle.api.tasks.Classpath
23 import org.gradle.api.tasks.InputFiles
24 import org.gradle.api.tasks.TaskAction
25 
26 /**
27  * Task for verifying the ELF regions in all shared libs in androidx are aligned to 16Kb boundary
28  */
29 @CacheableTask
30 abstract class VerifyELFRegionAlignmentTask : DefaultTask() {
31     init {
32         group = "Verification"
33         description = "Task for verifying alignment in shared libs"
34     }
35 
36     @get:[InputFiles Classpath]
37     abstract val files: ConfigurableFileCollection
38 
39     @TaskAction
40     fun verifyELFRegionAlignment() {
41         files.forEach {
42             val alignment = getELFAlignment(it.path)
43             check(alignment == "2**14") {
44                 "Expected ELF alignment of 2**14 for file ${it.name}, got $alignment"
45             }
46         }
47     }
48 }
49 
getELFAlignmentnull50 private fun getELFAlignment(filePath: String): String? {
51     val alignment =
52         ProcessBuilder("objdump", "-p", filePath).start().inputStream.bufferedReader().useLines {
53             lines ->
54             lines.filter { it.contains("LOAD") }.map { it.split(" ").last() }.firstOrNull()
55         }
56     return alignment
57 }
58