1 /* 2 * Copyright (C) 2021 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 class CMakeBuilder(val toolchain: Toolchain, val sysroot: File) : 24 RunBuilder() 25 26 abstract class CMakePortTask : PortTask() { 27 @get:Input 28 abstract val cmake: Property<CMakeBuilder.() -> Unit> 29 cmakenull30 fun cmake(block: CMakeBuilder.() -> Unit) = cmake.set(block) 31 32 override fun buildForAbi( 33 toolchain: Toolchain, 34 workingDirectory: File, 35 buildDirectory: File, 36 installDirectory: File 37 ) { 38 configure(toolchain, buildDirectory, installDirectory) 39 build(buildDirectory) 40 install(buildDirectory) 41 } 42 configurenull43 private fun configure( 44 toolchain: Toolchain, buildDirectory: File, installDirectory: File 45 ) { 46 val cmakeBlock = cmake.get() 47 val builder = CMakeBuilder( 48 toolchain, 49 prefabGenerated.get().asFile.resolve(toolchain.abi.triple) 50 ) 51 builder.cmakeBlock() 52 53 val toolchainFile = 54 toolchain.ndk.path.resolve("build/cmake/android.toolchain.cmake") 55 56 buildDirectory.mkdirs() 57 executeSubprocess( 58 listOf( 59 "cmake", 60 "-DCMAKE_TOOLCHAIN_FILE=${toolchainFile.absolutePath}", 61 "-DCMAKE_BUILD_TYPE=RelWithDebInfo", 62 "-DCMAKE_INSTALL_PREFIX=${installDirectory.absolutePath}", 63 "-DANDROID_ABI=${toolchain.abi.abiName}", 64 "-DANDROID_API_LEVEL=${toolchain.api}", 65 "-GNinja", 66 sourceDirectory.get().asFile.absolutePath, 67 ) + builder.cmd, buildDirectory, builder.env 68 ) 69 } 70 buildnull71 private fun build(buildDirectory: File) = 72 executeSubprocess(listOf("ninja", "-v"), buildDirectory) 73 74 private fun install(buildDirectory: File) = 75 executeSubprocess(listOf("ninja", "-v", "install"), buildDirectory) 76 }