<lambda>null1 package com.android.ndkports
2 
3 import com.google.prefab.api.BuildSystemInterface
4 import com.google.prefab.api.Module
5 import com.google.prefab.api.Package
6 import com.google.prefab.api.PlatformDataInterface
7 import java.io.File
8 
9 class PrefabSysrootPlugin(
10     override val outputDirectory: File, override val packages: List<Package>
11 ) : BuildSystemInterface {
12 
13     override fun generate(requirements: Collection<PlatformDataInterface>) {
14         prepareOutputDirectory(outputDirectory)
15 
16         for (pkg in packages) {
17             for (module in pkg.modules) {
18                 for (requirement in requirements) {
19                     installModule(module, requirement)
20                 }
21             }
22         }
23     }
24 
25     private fun installModule(
26         module: Module, requirement: PlatformDataInterface
27     ) {
28         val installDir = outputDirectory.resolve(requirement.targetTriple)
29         val includeDir = installDir.resolve("include")
30 
31         if (module.isHeaderOnly) {
32             installHeaders(module.includePath.toFile(), includeDir)
33             return
34         }
35 
36         val library = module.getLibraryFor(requirement)
37         installHeaders(module.includePath.toFile(), includeDir)
38         val libDir = installDir.resolve("lib").apply {
39             mkdirs()
40         }
41         library.path.toFile().apply { copyTo(libDir.resolve(name)) }
42     }
43 
44     private fun installHeaders(src: File, dest: File) {
45         src.copyRecursively(dest) { file, exception ->
46             if (exception !is FileAlreadyExistsException) {
47                 throw exception
48             }
49 
50             if (!file.readBytes().contentEquals(exception.file.readBytes())) {
51                 val path = file.relativeTo(dest)
52                 throw RuntimeException(
53                     "Found duplicate headers with non-equal contents: $path"
54                 )
55             }
56 
57             OnErrorAction.SKIP
58         }
59     }
60 }