|
14 | 14 |
|
15 | 15 | import java.util.* |
16 | 16 | import java.io.* |
| 17 | +import kotlin.system.exitProcess |
| 18 | +import kotlinx.serialization.json.* |
17 | 19 |
|
18 | 20 | plugins { |
19 | 21 | java |
@@ -44,41 +46,60 @@ tasks.withType(JavaCompile::class).forEach { |
44 | 46 | } |
45 | 47 |
|
46 | 48 |
|
47 | | -// FIXME: cannot share definition with 'buildSrc' so we duplicated the impl here |
48 | | -fun javaLibraryPaths(dir: File): List<String> { |
49 | | - val osName = System.getProperty("os.name") |
| 49 | +fun getSwiftRuntimeLibraryPaths(): List<String> { |
| 50 | + val process = ProcessBuilder("swiftc", "-print-target-info") |
| 51 | + .redirectError(ProcessBuilder.Redirect.INHERIT) |
| 52 | + .start() |
| 53 | + |
| 54 | + val output = process.inputStream.bufferedReader().use { it.readText() } |
| 55 | + val exitCode = process.waitFor() |
| 56 | + if (exitCode != 0) { |
| 57 | + System.err.println("Error executing swiftc -print-target-info") |
| 58 | + exitProcess(exitCode) |
| 59 | + } |
| 60 | + |
| 61 | + val json = Json.parseToJsonElement(output) |
| 62 | + val runtimeLibraryPaths = json.jsonObject["paths"]?.jsonObject?.get("runtimeLibraryPaths")?.jsonArray |
| 63 | + return runtimeLibraryPaths?.map { it.jsonPrimitive.content } ?: emptyList() |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Find library paths for 'java.library.path' when running or testing projects inside this build. |
| 68 | + */ |
| 69 | +// TODO: can't figure out how to share this code between BuildLogic/ and buildSrc/ |
| 70 | +fun javaLibraryPaths(rootDir: File): List<String> { |
| 71 | + val osName = System.getProperty("os.name").lowercase(Locale.getDefault()) |
50 | 72 | val osArch = System.getProperty("os.arch") |
51 | | - val isLinux = osName.lowercase(Locale.getDefault()).contains("linux") |
| 73 | + val isLinux = osName.contains("linux") |
| 74 | + val base = rootDir.path.let { "$it/" } |
52 | 75 |
|
53 | | - return listOf( |
54 | | - if (isLinux) { |
55 | | - if (osArch.equals("x86_64") || osArch.equals("amd64")) { |
56 | | - "$dir/.build/x86_64-unknown-linux-gnu/debug/" |
57 | | - } else { |
58 | | - "$dir/.build/$osArch-unknown-linux-gnu/debug/" |
59 | | - } |
60 | | - } else { |
61 | | - if (osArch.equals("aarch64")) { |
62 | | - "$dir/.build/arm64-apple-macosx/debug/" |
63 | | - } else { |
64 | | - "$dir/.build/$osArch-apple-macosx/debug/" |
65 | | - } |
66 | | - }, |
| 76 | + val projectBuildOutputPath = |
67 | 77 | if (isLinux) { |
68 | | - "/usr/lib/swift/linux" |
| 78 | + if (osArch == "amd64" || osArch == "x86_64") |
| 79 | + "$base.build/x86_64-unknown-linux-gnu" |
| 80 | + else |
| 81 | + "$base.build/${osArch}-unknown-linux-gnu" |
69 | 82 | } else { |
70 | | - // assume macOS |
71 | | - "/usr/lib/swift/" |
72 | | - }, |
73 | | - if (isLinux) { |
74 | | - System.getProperty("user.home") + "/.local/share/swiftly/toolchains/6.0.2/usr/lib/swift/linux" |
75 | | - } else { |
76 | | - // assume macOS |
77 | | - "/usr/lib/swift/" |
| 83 | + if (osArch == "aarch64") |
| 84 | + "$base.build/arm64-apple-macosx" |
| 85 | + else |
| 86 | + "$base.build/${osArch}-apple-macosx" |
78 | 87 | } |
| 88 | + val parentParentBuildOutputPath = |
| 89 | + "../../$projectBuildOutputPath" |
| 90 | + |
| 91 | + |
| 92 | + val swiftBuildOutputPaths = listOf( |
| 93 | + projectBuildOutputPath, |
| 94 | + parentParentBuildOutputPath |
79 | 95 | ) |
80 | | -} |
81 | 96 |
|
| 97 | + val debugBuildOutputPaths = swiftBuildOutputPaths.map { "$it/debug" } |
| 98 | + val releaseBuildOutputPaths = swiftBuildOutputPaths.map { "$it/release" } |
| 99 | + val swiftRuntimePaths = getSwiftRuntimeLibraryPaths() |
| 100 | + |
| 101 | + return debugBuildOutputPaths + releaseBuildOutputPaths + swiftRuntimePaths |
| 102 | +} |
82 | 103 |
|
83 | 104 | // Configure paths for native (Swift) libraries |
84 | 105 | tasks.test { |
|
0 commit comments