|
| 1 | +import groovy.json.JsonSlurper |
| 2 | + |
| 3 | +// Workaround for Android Studio's C++ analyzer surfacing errors during Gradle |
| 4 | +// Sync when CMake's precompiled header binary (`cmake_pch.hxx.pch`, generated |
| 5 | +// from `ReanimatedPCH.h`) hasn't been built yet. The project compiles fine, |
| 6 | +// but on a clean sync the IDE can't index sources without the PCH binary. See |
| 7 | +// the underlying issue: https://issuetracker.google.com/issues/187448826 |
| 8 | + |
| 9 | +// Generates minimal stub `.pch` files from an empty header so the IDE's C++ |
| 10 | +// engine doesn't fail during sync. The actual build regenerates the real PCH |
| 11 | +// files via ninja because we set each stub PCH's mtime older than its source |
| 12 | +// (`cmake_pch.hxx.cxx`). |
| 13 | + |
| 14 | +// Adapted from Expo's PR: https://github.com/expo/expo/pull/45921 |
| 15 | + |
| 16 | +val cxxDir = project.file(".cxx") |
| 17 | + |
| 18 | +val generateStubPCHTask = tasks.register("generateStubPCH") { |
| 19 | + dependsOn("configureCMakeDebug") |
| 20 | + doLast { |
| 21 | + if (!cxxDir.exists()) { |
| 22 | + return@doLast |
| 23 | + } |
| 24 | + cxxDir.walkTopDown().forEach { file -> |
| 25 | + if (file.name != "compile_commands.json") { |
| 26 | + return@forEach |
| 27 | + } |
| 28 | + @Suppress("UNCHECKED_CAST") |
| 29 | + val entries = JsonSlurper().parseText(file.readText()) as List<Map<String, Any>> |
| 30 | + entries.forEach entry@{ entry -> |
| 31 | + val entryFile = entry["file"] as String |
| 32 | + if (!entryFile.endsWith("cmake_pch.hxx.cxx")) { |
| 33 | + return@entry |
| 34 | + } |
| 35 | + val pchFile = File(entryFile.substring(0, entryFile.length - ".cxx".length) + ".pch") |
| 36 | + if (!pchFile.exists() || pchFile.length() == 0L) { |
| 37 | + pchFile.parentFile.mkdirs() |
| 38 | + |
| 39 | + val command = entry["command"] as String |
| 40 | + val compiler = command.split(" ").first() |
| 41 | + val target = Regex("""--target=\S+""").find(command)!!.value |
| 42 | + val sysroot = Regex("""--sysroot=\S+""").find(command)!!.value |
| 43 | + |
| 44 | + val stubHeader = File(pchFile.parentFile, "stub_pch.hxx") |
| 45 | + stubHeader.writeText("") |
| 46 | + |
| 47 | + val process = ProcessBuilder( |
| 48 | + compiler, |
| 49 | + target, |
| 50 | + sysroot, |
| 51 | + "-x", "c++-header", |
| 52 | + "-o", pchFile.absolutePath, |
| 53 | + stubHeader.absolutePath, |
| 54 | + ) |
| 55 | + .directory(File(entry["directory"] as String)) |
| 56 | + .redirectErrorStream(true) |
| 57 | + .start() |
| 58 | + process.outputStream.close() |
| 59 | + if (process.waitFor() != 0) { |
| 60 | + throw GradleException( |
| 61 | + "Stub PCH generation failed: ${process.inputStream.bufferedReader().readText()}", |
| 62 | + ) |
| 63 | + } |
| 64 | + } |
| 65 | + // Ensure PCH is older than source so ninja rebuilds the real one during build |
| 66 | + pchFile.setLastModified(File(entryFile).lastModified() - 1) |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// Register `prepareKotlinBuildScriptModel` if absent (Android Studio sync needs it |
| 73 | +// to exist so the stub PCH generation runs) or configure it if some other plugin |
| 74 | +// already registered it (e.g. on CI, where re-registering throws `Cannot add task |
| 75 | +// 'prepareKotlinBuildScriptModel' as a task with that name already exists.`). |
| 76 | +if (tasks.findByName("prepareKotlinBuildScriptModel") == null) { |
| 77 | + tasks.register("prepareKotlinBuildScriptModel") { |
| 78 | + dependsOn(generateStubPCHTask) |
| 79 | + } |
| 80 | +} else { |
| 81 | + tasks.named("prepareKotlinBuildScriptModel") { |
| 82 | + dependsOn(generateStubPCHTask) |
| 83 | + } |
| 84 | +} |
0 commit comments