Export prepareLibevent to an internal task (#32425)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/32425

This diff refactors the `prepareLibevent` task to a separate Gradle task in the `.internal` package.
The reason for this change is that `prepareLibevent` was defining a `doLast` action and would result in being
invalidated whenever the `build.gradle` file would change. This means that the Libevent headers/source files
would have been extracted again, effectively invalidating the timestamps for the native build.

Changelog:
[Internal] [Changed] - Export prepareLibevent to an internal task

Reviewed By: ShikaSD

Differential Revision: D31661988

fbshipit-source-id: e55c2179a187fa156f701c25bae3b48a796e2660
This commit is contained in:
Nicola Corti 2021-10-18 04:32:45 -07:00 коммит произвёл Facebook GitHub Bot
Родитель c3e7ea0b5b
Коммит bb981b2055
3 изменённых файлов: 159 добавлений и 19 удалений

Просмотреть файл

@ -122,26 +122,12 @@ task downloadLibevent(dependsOn: createNativeDepsDirectories, type: Download) {
dest(new File(downloadsDir, "libevent-${LIBEVENT_VERSION}.tar.gz"))
}
task prepareLibevent(dependsOn: dependenciesPath ? [] : [downloadLibevent], type: Copy) {
from(dependenciesPath ?: tarTree(downloadLibevent.dest))
from("src/main/jni/third-party/libevent/Android.mk")
from("src/main/jni/third-party/libevent/event-config.h")
from("src/main/jni/third-party/libevent/evconfig-private.h")
include(
"libevent-${LIBEVENT_VERSION}-stable/*.c",
"libevent-${LIBEVENT_VERSION}-stable/*.h",
"libevent-${LIBEVENT_VERSION}-stable/include/**/*",
"evconfig-private.h",
"event-config.h",
"Android.mk"
)
eachFile { fname -> fname.path = (fname.path - "libevent-${LIBEVENT_VERSION}-stable/") }
includeEmptyDirs = false
into("$thirdPartyNdkDir/libevent")
doLast {
ant.move(file: "$thirdPartyNdkDir/libevent/event-config.h", tofile: "$thirdPartyNdkDir/libevent/include/event2/event-config.h")
}
final def prepareLibevent = tasks.register("prepareLibevent", PrepareLibeventTask) {
it.dependsOn(dependenciesPath ? [] : [downloadLibevent])
it.libeventPath.setFrom(dependenciesPath ?: tarTree(downloadLibevent.dest))
it.libeventVersion.set(LIBEVENT_VERSION)
it.outputDir.set(new File(thirdPartyNdkDir, "libevent"))
}
task prepareHermes(dependsOn: createNativeDepsDirectories, type: Copy) {

Просмотреть файл

@ -0,0 +1,53 @@
/*
* Copyright (c) Facebook, Inc. and its 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 org.gradle.api.DefaultTask
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.*
/**
* A task that takes care of extracting Libevent from a source folder/zip and preparing it to be
* consumed by the NDK.
*/
abstract class PrepareLibeventTask : DefaultTask() {
@get:InputFiles abstract val libeventPath: ConfigurableFileCollection
@get:Input abstract val libeventVersion: Property<String>
@get:OutputDirectory abstract val outputDir: DirectoryProperty
@TaskAction
fun taskAction() {
project.copy { it ->
it.from(libeventPath)
it.from(project.file("src/main/jni/third-party/libevent/Android.mk"))
it.from(project.file("src/main/jni/third-party/libevent/event-config.h"))
it.from(project.file("src/main/jni/third-party/libevent/evconfig-private.h"))
it.include(
"libevent-${libeventVersion.get()}-stable/*.c",
"libevent-${libeventVersion.get()}-stable/*.h",
"libevent-${libeventVersion.get()}-stable/include/**/*",
"evconfig-private.h",
"event-config.h",
"Android.mk")
it.eachFile { it.path = it.path.removePrefix("libevent-${libeventVersion.get()}-stable/") }
it.includeEmptyDirs = false
it.into(outputDir)
}
File(outputDir.asFile.get(), "event-config.h").apply {
val destination =
File(this.parentFile, "include/event2/event-config.h").apply { parentFile.mkdirs() }
renameTo(destination)
}
}
}

Просмотреть файл

@ -0,0 +1,101 @@
/*
* Copyright (c) Facebook, Inc. and its 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.createProject
import com.facebook.react.tests.createTestTask
import java.io.*
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
class PrepareLibeventTaskTest {
@get:Rule val tempFolder = TemporaryFolder()
@Test(expected = IllegalStateException::class)
fun prepareBoostTask_withMissingConfiguration_fails() {
val task = createTestTask<PrepareLibeventTask>()
task.taskAction()
}
@Test
fun prepareBoostTask_copiesMakefile() {
val libeventPath = tempFolder.newFolder("libeventPath")
val output = tempFolder.newFolder("output")
val project = createProject()
val task =
createTestTask<PrepareLibeventTask>(project = project) {
it.libeventPath.setFrom(libeventPath)
it.libeventVersion.set("1.0.0")
it.outputDir.set(output)
}
File(project.projectDir, "src/main/jni/third-party/libevent/Android.mk").apply {
parentFile.mkdirs()
createNewFile()
}
task.taskAction()
assertTrue(File(output, "Android.mk").exists())
}
@Test
fun prepareBoostTask_copiesConfigFiles() {
val libeventPath = tempFolder.newFolder("libeventPath")
val output = tempFolder.newFolder("output")
val project = createProject()
val task =
createTestTask<PrepareLibeventTask>(project = project) {
it.libeventPath.setFrom(libeventPath)
it.libeventVersion.set("1.0.0")
it.outputDir.set(output)
}
File(project.projectDir, "src/main/jni/third-party/libevent/event-config.h").apply {
parentFile.mkdirs()
createNewFile()
}
File(project.projectDir, "src/main/jni/third-party/libevent/evconfig-private.h").createNewFile()
task.taskAction()
assertTrue(File(output, "evconfig-private.h").exists())
assertTrue(File(output, "include/event2/event-config.h").exists())
}
@Test
fun prepareBoostTask_copiesSourceFiles() {
val libeventPath = tempFolder.newFolder("libeventPath")
val output = tempFolder.newFolder("output")
val task =
createTestTask<PrepareLibeventTask> {
it.libeventPath.setFrom(libeventPath)
it.libeventVersion.set("1.0.0")
it.outputDir.set(output)
}
File(libeventPath, "libevent-1.0.0-stable/sample.c").apply {
parentFile.mkdirs()
createNewFile()
}
File(libeventPath, "libevent-1.0.0-stable/sample.h").apply {
parentFile.mkdirs()
createNewFile()
}
File(libeventPath, "libevent-1.0.0-stable/include/sample.h").apply {
parentFile.mkdirs()
createNewFile()
}
task.taskAction()
assertTrue(File(output, "sample.c").exists())
assertTrue(File(output, "sample.h").exists())
assertTrue(File(output, "include/sample.h").exists())
}
}