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 java.io.File
20 
21 abstract class MesonPort : Port() {
22     enum class DefaultLibraryType(val argument: String) {
23         Both("both"),
24         Shared("shared"),
25         Static("static")
26     }
27 
28     open val defaultLibraryType: DefaultLibraryType = DefaultLibraryType.Shared
29 
configurenull30     override fun configure(
31         toolchain: Toolchain,
32         sourceDirectory: File,
33         buildDirectory: File,
34         installDirectory: File,
35         workingDirectory: File
36     ): Result<Unit, String> {
37         val cpuFamily = when (toolchain.abi) {
38             Abi.Arm -> "arm"
39             Abi.Arm64 -> "aarch64"
40             Abi.X86 -> "x86"
41             Abi.X86_64 -> "x86_64"
42         }
43 
44         val cpu = when (toolchain.abi) {
45             Abi.Arm -> "armv7a"
46             Abi.Arm64 -> "armv8a"
47             Abi.X86 -> "i686"
48             Abi.X86_64 -> "x86_64"
49         }
50 
51         val crossFile = workingDirectory.resolve("cross_file.txt").apply {
52             writeText("""
53             [binaries]
54             ar = '${toolchain.ar}'
55             c = '${toolchain.clang}'
56             cpp = '${toolchain.clangxx}'
57             strip = '${toolchain.strip}'
58 
59             [host_machine]
60             system = 'android'
61             cpu_family = '$cpuFamily'
62             cpu = '$cpu'
63             endian = 'little'
64             """.trimIndent())
65         }
66 
67         return executeProcessStep(
68             listOf(
69                 "meson",
70                 "--cross-file",
71                 crossFile.absolutePath,
72                 "--buildtype",
73                 "release",
74                 "--prefix",
75                 installDirectory.absolutePath,
76                 "--default-library",
77                 defaultLibraryType.argument,
78                 sourceDirectory.absolutePath,
79                 buildDirectory.absolutePath
80             ), workingDirectory
81         )
82     }
83 
buildnull84     override fun build(
85         toolchain: Toolchain,
86         buildDirectory: File
87     ): Result<Unit, String> =
88         executeProcessStep(listOf("ninja", "-v"), buildDirectory)
89 
90     override fun install(
91         toolchain: Toolchain,
92         buildDirectory: File,
93         installDirectory: File
94     ): Result<Unit, String> =
95         executeProcessStep(listOf("ninja", "-v", "install"), buildDirectory)
96 }