Skip to content

Commit

Permalink
Export prepareLibevent to an internal task
Browse files Browse the repository at this point in the history
Summary:
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

Differential Revision: D31661988

fbshipit-source-id: 0f3915dd61ddbf9facbc696a7de2243bf7a92c54
  • Loading branch information
cortinico authored and facebook-github-bot committed Oct 18, 2021
1 parent eaf97b5 commit b2503ea
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 19 deletions.
24 changes: 5 additions & 19 deletions ReactAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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())
}
}

0 comments on commit b2503ea

Please sign in to comment.