xref: /aosp_15_r20/tools/ndkports/buildSrc/src/main/kotlin/com/android/ndkports/MesonPortTask.kt (revision 2e149f73a8e792fff6dc6aadc18d0d919a071282)
1 /*
2  * Copyright (C) 2019 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 com.android.ndkports
18 
19 import org.gradle.api.model.ObjectFactory
20 import org.gradle.api.provider.Property
21 import org.gradle.api.tasks.Input
22 import java.io.File
23 import javax.inject.Inject
24 
25 @Suppress("UnstableApiUsage")
26 abstract class MesonPortTask @Inject constructor(objects: ObjectFactory) :
27     PortTask() {
28     enum class DefaultLibraryType(val argument: String) {
29         Both("both"), Shared("shared"), Static("static")
30     }
31 
32     @get:Input
33     val defaultLibraryType: Property<DefaultLibraryType> =
34         objects.property(DefaultLibraryType::class.java)
35             .convention(DefaultLibraryType.Shared)
36 
buildForAbinull37     override fun buildForAbi(
38         toolchain: Toolchain,
39         workingDirectory: File,
40         buildDirectory: File,
41         installDirectory: File
42     ) {
43         configure(toolchain, workingDirectory, buildDirectory, installDirectory)
44         build(buildDirectory)
45         install(buildDirectory)
46     }
47 
configurenull48     private fun configure(
49         toolchain: Toolchain,
50         workingDirectory: File,
51         buildDirectory: File,
52         installDirectory: File
53     ) {
54         val cpuFamily = when (toolchain.abi) {
55             Abi.Arm -> "arm"
56             Abi.Arm64 -> "aarch64"
57             Abi.X86 -> "x86"
58             Abi.X86_64 -> "x86_64"
59         }
60 
61         val cpu = when (toolchain.abi) {
62             Abi.Arm -> "armv7a"
63             Abi.Arm64 -> "armv8a"
64             Abi.X86 -> "i686"
65             Abi.X86_64 -> "x86_64"
66         }
67 
68         val crossFile = workingDirectory.resolve("cross_file.txt").apply {
69             writeText(
70                 """
71             [binaries]
72             ar = '${toolchain.ar}'
73             c = '${toolchain.clang}'
74             cpp = '${toolchain.clangxx}'
75             strip = '${toolchain.strip}'
76 
77             [host_machine]
78             system = 'android'
79             cpu_family = '$cpuFamily'
80             cpu = '$cpu'
81             endian = 'little'
82             """.trimIndent()
83             )
84         }
85 
86         executeSubprocess(
87             listOf(
88                 "meson",
89                 "--cross-file",
90                 crossFile.absolutePath,
91                 "--buildtype",
92                 "release",
93                 "--prefix",
94                 installDirectory.absolutePath,
95                 "--default-library",
96                 defaultLibraryType.get().argument,
97                 sourceDirectory.get().asFile.absolutePath,
98                 buildDirectory.absolutePath
99             ), workingDirectory
100         )
101     }
102 
buildnull103     private fun build(buildDirectory: File) =
104         executeSubprocess(listOf("ninja", "-v"), buildDirectory)
105 
106     private fun install(buildDirectory: File) =
107         executeSubprocess(listOf("ninja", "-v", "install"), buildDirectory)
108 }