-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Introduce PCH for codegen targets #58317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dbab3c2
Introduce PCH for codegen targets
lukmccall 52fef48
Apply requested changes
lukmccall a4c8128
Use `StubPchBuildService` instead of reallying on debug flavour
lukmccall 5bb084e
Run yarn format-kotlin
lukmccall 272b4d4
Check if target_precompile_headers exists
lukmccall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...t-native-gradle-plugin/src/main/kotlin/com/facebook/react/internal/StubPchBuildService.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| package com.facebook.react.internal | ||
|
|
||
| import com.facebook.react.utils.StubPchUtils | ||
| import org.gradle.api.file.DirectoryProperty | ||
| import org.gradle.api.logging.Logging | ||
| import org.gradle.api.services.BuildService | ||
| import org.gradle.api.services.BuildServiceParameters | ||
|
|
||
| /** | ||
| * Generates stub precompiled headers when the build finishes. See [StubPchUtils]. | ||
| * | ||
| * Android Studio configures the C++ projects of the selected variant while it fetches the Gradle | ||
| * models (during sync), which happens after all tasks have run. A build service is closed after | ||
| * that, so by the time [close] runs the `compile_commands.json` files of exactly the variant and | ||
| * ABI that Studio requested are on disk. | ||
| */ | ||
| abstract class StubPchBuildService : BuildService<StubPchBuildService.Params>, AutoCloseable { | ||
|
|
||
| interface Params : BuildServiceParameters { | ||
| val cxxDirectory: DirectoryProperty | ||
| } | ||
|
|
||
| override fun close() { | ||
| try { | ||
| StubPchUtils.generateStubs(parameters.cxxDirectory.get().asFile) | ||
| } catch (e: Exception) { | ||
| logger.warn("RNGP - Could not generate stub precompiled headers: ${e.message}") | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private val logger = Logging.getLogger(StubPchBuildService::class.java) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
...lugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/StubPchUtils.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| package com.facebook.react.utils | ||
|
|
||
| import com.google.gson.Gson | ||
| import com.google.gson.JsonArray | ||
| import java.io.File | ||
| import org.gradle.api.GradleException | ||
|
|
||
| internal object StubPchUtils { | ||
| private const val COMPILE_COMMANDS_FILENAME = "compile_commands.json" | ||
| private const val PCH_SOURCE_SUFFIX = "cmake_pch.hxx.cxx" | ||
| private const val SOURCE_EXTENSION = ".cxx" | ||
| private const val PCH_EXTENSION = ".pch" | ||
| private const val STUB_HEADER_FILENAME = "stub_pch.hxx" | ||
|
|
||
| /** Generates the missing stub precompiled headers for every configuration under [cxxDir]. */ | ||
| fun generateStubs(cxxDir: File) { | ||
| if (!cxxDir.isDirectory) { | ||
| return | ||
| } | ||
|
|
||
| cxxDir | ||
| .walkTopDown() | ||
| .filter { it.isFile && it.name == COMPILE_COMMANDS_FILENAME } | ||
| .forEach { generateStubsFor(it) } | ||
| } | ||
|
|
||
| internal fun generateStubsFor(compileCommands: File) { | ||
| val entries = | ||
| runCatching { Gson().fromJson(compileCommands.readText(), JsonArray::class.java) } | ||
| .getOrNull() ?: return | ||
|
|
||
| for (element in entries) { | ||
| val entry = element.asJsonObject | ||
| val source = entry.get("file")?.asString ?: continue | ||
| if (!source.endsWith(PCH_SOURCE_SUFFIX)) { | ||
| continue | ||
| } | ||
|
|
||
| val header = source.removeSuffix(SOURCE_EXTENSION) | ||
| val pchFile = File(header + PCH_EXTENSION) | ||
| // Anything already on disk was either built for real or stubbed by an earlier sync. | ||
| if (pchFile.length() > 0L) { | ||
| continue | ||
| } | ||
|
|
||
| val command = entry.get("command")?.asString ?: continue | ||
| val directory = entry.get("directory")?.asString ?: continue | ||
| compileEmptyPch(command, header, File(directory), pchFile) | ||
|
|
||
| // A stub is not a valid input for the real compilation, so keep it older than its source. | ||
| // That way the next build treats it as stale and replaces it before anything consumes it. | ||
| pchFile.setLastModified(File(source).lastModified() - 1) | ||
| } | ||
| } | ||
|
|
||
| private fun compileEmptyPch( | ||
| command: String, | ||
| header: String, | ||
| workingDir: File, | ||
| pchFile: File, | ||
| ) { | ||
| pchFile.parentFile.mkdirs() | ||
| val stubHeader = File(pchFile.parentFile, STUB_HEADER_FILENAME).apply { writeText("") } | ||
|
|
||
| val process = | ||
| ProcessBuilder(stubCompilerArguments(command, header, stubHeader)) | ||
| .directory(workingDir) | ||
| .redirectErrorStream(true) | ||
| .start() | ||
|
|
||
| val output = process.inputStream.bufferedReader().use { it.readText() } | ||
| process.outputStream.close() | ||
|
|
||
| if (process.waitFor() != 0) { | ||
| throw GradleException("RNGP - Stub precompiled header generation failed:\n$output") | ||
| } | ||
| } | ||
|
|
||
| internal fun stubCompilerArguments( | ||
| command: String, | ||
| header: String, | ||
| stubHeader: File, | ||
| windows: Boolean = Os.isWindows(), | ||
| ): List<String> { | ||
| val arguments = splitCompilerCommand(command, windows) | ||
| if (arguments.none { it == header }) { | ||
| throw GradleException("RNGP - Could not find $header in: $command") | ||
| } | ||
|
|
||
| return arguments.map { if (it == header) stubHeader.absolutePath else it } | ||
| } | ||
|
|
||
| private fun splitCompilerCommand(command: String, windows: Boolean): List<String> { | ||
| val arguments = mutableListOf<String>() | ||
| var argument: StringBuilder? = null | ||
| var quote: Char? = null | ||
| var index = 0 | ||
|
|
||
| while (index < command.length) { | ||
| val char = command[index++] | ||
| if (char.isWhitespace() && quote == null) { | ||
| argument?.let { arguments.add(it.toString()) } | ||
| argument = null | ||
| continue | ||
| } | ||
|
|
||
| // POSIX line continuations don't start an argument or contribute any characters. | ||
| if (!windows && char == '\\' && quote != '\'' && command.getOrNull(index) == '\n') { | ||
| index++ | ||
| continue | ||
| } | ||
|
|
||
| val token = argument ?: StringBuilder().also { argument = it } | ||
| when { | ||
| char == '\\' && windows -> { | ||
| val start = index - 1 | ||
| while (command.getOrNull(index) == '\\') { | ||
| index++ | ||
| } | ||
| val count = index - start | ||
| val followedByQuote = command.getOrNull(index) == '"' | ||
| repeat(if (followedByQuote) count / 2 else count) { token.append('\\') } | ||
| if (followedByQuote && count % 2 != 0) { | ||
| token.append(command[index++]) | ||
| } | ||
| } | ||
| char == '\\' && quote != '\'' -> { | ||
| val next = | ||
| command.getOrNull(index) | ||
| ?: throw GradleException("RNGP - Trailing escape in compiler command: $command") | ||
| // Inside POSIX double quotes, backslashes only escape these shell characters. | ||
| val escaped = quote != '"' || next in "\"\\\$`" | ||
| token.append(if (escaped) command[index++] else char) | ||
| } | ||
| quote == char && windows && command.getOrNull(index) == '"' -> | ||
| token.append(command[index++]) | ||
| quote == char -> quote = null | ||
| quote == null && (char == '"' || (!windows && char == '\'')) -> quote = char | ||
| else -> token.append(char) | ||
| } | ||
| } | ||
|
|
||
| if (quote != null) { | ||
| throw GradleException("RNGP - Unclosed quote in compiler command: $command") | ||
| } | ||
|
|
||
| argument?.let { arguments.add(it.toString()) } | ||
|
|
||
| return arguments | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh man 😅