xref: /aosp_15_r20/tools/ndkports/buildSrc/src/main/kotlin/com/android/ndkports/AdHocPortTask.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.provider.Property
20 import org.gradle.api.tasks.Input
21 import java.io.File
22 
23 open class RunBuilder {
24     val cmd = mutableListOf<String>()
argnull25     fun arg(arg: String) = cmd.add(arg)
26     fun args(vararg args: String) = cmd.addAll(args)
27 
28     val env = mutableMapOf<String, String>()
29     fun env(key: String, value: String) = env.set(key, value)
30 }
31 
32 class AdHocBuilder(
33     val sourceDirectory: File,
34     val buildDirectory: File,
35     val installDirectory: File,
36     val toolchain: Toolchain,
37     val sysroot: File,
38     val ncpus: Int,
39 ) {
40     val runs = mutableListOf<RunBuilder>()
41     fun run(block: RunBuilder.() -> Unit) {
42         runs.add(RunBuilder().apply { block() })
43     }
44 }
45 
46 abstract class AdHocPortTask : PortTask() {
47     @get:Input
48     abstract val builder: Property<AdHocBuilder.() -> Unit>
49 
buildernull50     fun builder(block: AdHocBuilder.() -> Unit) = builder.set(block)
51 
52     override fun buildForAbi(
53         toolchain: Toolchain,
54         workingDirectory: File,
55         buildDirectory: File,
56         installDirectory: File
57     ) {
58         buildDirectory.mkdirs()
59 
60         val builderBlock = builder.get()
61         val builder = AdHocBuilder(
62             sourceDirectory.get().asFile,
63             buildDirectory,
64             installDirectory,
65             toolchain,
66             prefabGenerated.get().asFile,
67             ncpus,
68         )
69         builder.builderBlock()
70 
71         for (run in builder.runs) {
72             executeSubprocess(
73                 run.cmd, buildDirectory, additionalEnvironment = run.env
74             )
75         }
76     }
77 }