From 001426579585f7eca9bc1cac6bd465212908fc60 Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Mon, 17 Feb 2025 03:35:43 -0800 Subject: [PATCH] Gradle Configuration Cache - Round 2 (#49438) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/49438 This is the second part of a series of diff needed to enable G. Configuration Cache: https://docs.gradle.org/current/userguide/configuration_cache.html as it will make our CI faster (and will be the default in the future Gradle version). Here I'm making the exec tasks CC friendly. The problem is that previously we were using explicit streams which are not CC friendly for stderr/stdout. The solution is to create a custom task and handle files as input properties. Changelog: [Internal] [Changed] - Reviewed By: cipolleschi Differential Revision: D69662246 fbshipit-source-id: ad7e82e52b12d508ee15b68408882fdc3516d287 --- .../react/tasks/internal/CustomExecTask.kt | 44 +++++++++++++++++++ .../tasks/internal/CustomExecTaskTest.kt | 33 ++++++++++++++ .../hermes-engine/build.gradle.kts | 12 ++--- 3 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/internal/CustomExecTask.kt create mode 100644 packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/tasks/internal/CustomExecTaskTest.kt diff --git a/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/internal/CustomExecTask.kt b/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/internal/CustomExecTask.kt new file mode 100644 index 00000000000..ae6d5c108a3 --- /dev/null +++ b/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/internal/CustomExecTask.kt @@ -0,0 +1,44 @@ +/* + * 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.tasks.internal + +import java.io.File +import java.io.FileOutputStream +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.Exec +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Optional +import org.gradle.api.tasks.OutputFile + +/** + * A Task that will just expose an Exec-like task and that offers properties to configure the + * standard output and error. + */ +abstract class CustomExecTask : Exec() { + + @get:OutputFile @get:Optional abstract val standardOutputFile: RegularFileProperty + + @get:OutputFile @get:Optional abstract val errorOutputFile: RegularFileProperty + + @get:Input @get:Optional abstract val onlyIfProvidedPathDoesNotExists: Property + + override fun exec() { + if (onlyIfProvidedPathDoesNotExists.isPresent && + File(onlyIfProvidedPathDoesNotExists.get()).exists()) { + return + } + if (standardOutputFile.isPresent) { + standardOutput = FileOutputStream(standardOutputFile.get().asFile) + } + if (errorOutputFile.isPresent) { + errorOutput = FileOutputStream(errorOutputFile.get().asFile) + } + super.exec() + } +} diff --git a/packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/tasks/internal/CustomExecTaskTest.kt b/packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/tasks/internal/CustomExecTaskTest.kt new file mode 100644 index 00000000000..c61d5b307c4 --- /dev/null +++ b/packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/tasks/internal/CustomExecTaskTest.kt @@ -0,0 +1,33 @@ +/* + * 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.tasks.internal + +import com.facebook.react.tests.createTestTask +import org.assertj.core.api.Assertions.assertThat +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder + +class CustomExecTaskTest { + + @get:Rule val tempFolder = TemporaryFolder() + + @Test + fun customExec_inputProperties_areSetCorrectly() { + val outFile = tempFolder.newFile("stdout") + val errFile = tempFolder.newFile("stderr") + val task = + createTestTask { task -> + task.errorOutputFile.set(errFile) + task.standardOutputFile.set(outFile) + } + + assertThat(task.errorOutputFile.get().asFile).isEqualTo(errFile) + assertThat(task.standardOutputFile.get().asFile).isEqualTo(outFile) + } +} diff --git a/packages/react-native/ReactAndroid/hermes-engine/build.gradle.kts b/packages/react-native/ReactAndroid/hermes-engine/build.gradle.kts index bc5a4bdd22d..43b4268edc9 100644 --- a/packages/react-native/ReactAndroid/hermes-engine/build.gradle.kts +++ b/packages/react-native/ReactAndroid/hermes-engine/build.gradle.kts @@ -5,8 +5,8 @@ * LICENSE file in the root directory of this source tree. */ +import com.facebook.react.tasks.internal.* import de.undercouch.gradle.tasks.download.Download -import java.io.FileOutputStream import org.apache.tools.ant.taskdefs.condition.Os plugins { @@ -131,7 +131,7 @@ val installCMake by } val configureBuildForHermes by - tasks.registering(Exec::class) { + tasks.registering(CustomExecTask::class) { dependsOn(installCMake) workingDir(hermesDir) inputs.dir(hermesDir) @@ -149,11 +149,11 @@ val configureBuildForHermes by hermesBuildDir.toString(), "-DJSI_DIR=" + jsiDir.absolutePath, )) - standardOutput = FileOutputStream("$buildDir/configure-hermesc.log") + standardOutputFile.set(project.file("$buildDir/configure-hermesc.log")) } val buildHermesC by - tasks.registering(Exec::class) { + tasks.registering(CustomExecTask::class) { dependsOn(configureBuildForHermes) workingDir(hermesDir) inputs.files(hermesBuildOutputFileTree) @@ -167,8 +167,8 @@ val buildHermesC by "-j", ndkBuildJobs, ) - standardOutput = FileOutputStream("$buildDir/build-hermesc.log") - errorOutput = FileOutputStream("$buildDir/build-hermesc.error.log") + standardOutputFile.set(project.file("$buildDir/build-hermesc.log")) + errorOutputFile.set(project.file("$buildDir/build-hermesc.error.log")) } val prepareHeadersForPrefab by