|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import com.facebook.react.tasks.internal.* |
| 9 | +import com.facebook.react.tasks.internal.utils.* |
| 10 | + |
| 11 | +plugins { id("com.facebook.react") } |
| 12 | + |
| 13 | +// CMake shipped with the Android SDK (same version the Fantom tester uses). If |
| 14 | +// missing it is downloaded automatically by the Android SDK manager. |
| 15 | +val cmakeVersion = System.getenv("CMAKE_VERSION") ?: "3.30.5" |
| 16 | +val cmakePath = "${getSDKPath()}/cmake/$cmakeVersion" |
| 17 | +val cmakeBinaryPath = "${cmakePath}/bin/cmake" |
| 18 | +val ctestBinaryPath = "${cmakePath}/bin/ctest" |
| 19 | +val buildJobs = Runtime.getRuntime().availableProcessors().toString() |
| 20 | + |
| 21 | +fun getSDKPath(): String { |
| 22 | + val androidSdkRoot = System.getenv("ANDROID_SDK_ROOT") |
| 23 | + val androidHome = System.getenv("ANDROID_HOME") |
| 24 | + return when { |
| 25 | + !androidSdkRoot.isNullOrBlank() -> androidSdkRoot |
| 26 | + !androidHome.isNullOrBlank() -> androidHome |
| 27 | + else -> throw IllegalStateException("Neither ANDROID_SDK_ROOT nor ANDROID_HOME is set.") |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +val buildDir = project.layout.buildDirectory.get().asFile |
| 32 | +val reportsDir = File("$buildDir/reports") |
| 33 | +val reactNativeRootDir = projectDir.parentFile.parentFile |
| 34 | +val reactNativeDir = File("$reactNativeRootDir/packages/react-native") |
| 35 | +val reactAndroidDir = File("$reactNativeDir/ReactAndroid") |
| 36 | +val reactAndroidBuildDir = File("$reactAndroidDir/build") |
| 37 | + |
| 38 | +// The C++ test harness reuses the third-party dependencies staged by the Fantom |
| 39 | +// tester build: folly and gflags land in <fantom>/build/third-party; glog, |
| 40 | +// double-conversion, fast_float, fmt and boost land in |
| 41 | +// ReactAndroid/build/third-party-ndk. Depending on Fantom's |
| 42 | +// prepareNative3pDependencies guarantees all of them are prepared. |
| 43 | +val fantomDir = File("$reactNativeRootDir/private/react-native-fantom") |
| 44 | +val stagedThirdPartyDir = File("$fantomDir/build/third-party") |
| 45 | +val testerThirdPartySrcDir = File("$fantomDir/tester/third-party") |
| 46 | + |
| 47 | +val cxxDir = File("$projectDir/cxx") |
| 48 | +val cxxBuildDir = File("$buildDir/cxx") |
| 49 | +val cxxBuildOutputFileTree = |
| 50 | + fileTree(cxxBuildDir.toString()) |
| 51 | + .include("**/*.cmake", "**/*.marks", "**/compiler_depends.ts", "**/Makefile", "**/link.txt") |
| 52 | + |
| 53 | +val createReportsDir by tasks.registering { reportsDir.mkdirs() } |
| 54 | + |
| 55 | +// Generated codegen sources (FBReactNativeSpec) that Fabric component tests |
| 56 | +// depend on. `generateCodegenArtifactsFromSchema` produces them under |
| 57 | +// ReactAndroid/build/generated; stage them next to codegen/CMakeLists.txt. |
| 58 | +val codegenSrcDir = File("$reactAndroidBuildDir/generated/source/codegen/jni") |
| 59 | +val codegenOutDir = File("$buildDir/codegen") |
| 60 | +val prepareRNCodegen by |
| 61 | + tasks.registering(Copy::class) { |
| 62 | + dependsOn(":packages:react-native:ReactAndroid:generateCodegenArtifactsFromSchema") |
| 63 | + from(codegenSrcDir) |
| 64 | + from("codegen") |
| 65 | + include("react/**/*.h", "react/**/*.cpp", "CMakeLists.txt") |
| 66 | + includeEmptyDirs = false |
| 67 | + duplicatesStrategy = DuplicatesStrategy.INCLUDE |
| 68 | + into(codegenOutDir) |
| 69 | + } |
| 70 | + |
| 71 | +// Hermes VM + prefab headers, for suites whose tests create a JS runtime |
| 72 | +// (hermes::makeHermesRuntime). Mirrors the Fantom tester's Hermes setup. |
| 73 | +val enableHermesBuild by tasks.registering { |
| 74 | + project(":packages:react-native:ReactAndroid:hermes-engine") { |
| 75 | + tasks.configureEach { enabled = true } |
| 76 | + } |
| 77 | +} |
| 78 | +val prepareHermesDependencies by tasks.registering { |
| 79 | + dependsOn( |
| 80 | + enableHermesBuild, |
| 81 | + ":packages:react-native:ReactAndroid:hermes-engine:buildHermesLibWithDebugger", |
| 82 | + ":packages:react-native:ReactAndroid:hermes-engine:prepareHeadersForPrefabWithDebugger", |
| 83 | + ) |
| 84 | +} |
| 85 | + |
| 86 | +val configureCxxTests by |
| 87 | + tasks.registering(CustomExecTask::class) { |
| 88 | + dependsOn( |
| 89 | + createReportsDir, |
| 90 | + prepareRNCodegen, |
| 91 | + prepareHermesDependencies, |
| 92 | + ":private:react-native-fantom:prepareNative3pDependencies", |
| 93 | + ) |
| 94 | + workingDir(cxxDir) |
| 95 | + inputs.dir(cxxDir) |
| 96 | + outputs.files(cxxBuildOutputFileTree) |
| 97 | + commandLine( |
| 98 | + cmakeBinaryPath, |
| 99 | + "--log-level=ERROR", |
| 100 | + "-S", |
| 101 | + ".", |
| 102 | + "-B", |
| 103 | + cxxBuildDir.toString(), |
| 104 | + "-DCMAKE_BUILD_TYPE=Debug", |
| 105 | + "-DREACT_ANDROID_DIR=$reactAndroidDir", |
| 106 | + "-DREACT_COMMON_DIR=$reactNativeDir/ReactCommon", |
| 107 | + "-DREACT_THIRD_PARTY_NDK_DIR=$reactAndroidBuildDir/third-party-ndk", |
| 108 | + "-DRN_STAGED_THIRD_PARTY_DIR=$stagedThirdPartyDir", |
| 109 | + "-DRN_TESTER_THIRD_PARTY_SRC_DIR=$testerThirdPartySrcDir", |
| 110 | + "-DRN_CODEGEN_DIR=$codegenOutDir", |
| 111 | + "-DRN_ENABLE_DEBUG_STRING_CONVERTIBLE=ON", |
| 112 | + ) |
| 113 | + standardOutputFile.set(project.file("$buildDir/reports/configure-cxx-tests.log")) |
| 114 | + errorOutputFile.set(project.file("$buildDir/reports/configure-cxx-tests.error.log")) |
| 115 | + } |
| 116 | + |
| 117 | +val buildCxxTests by |
| 118 | + tasks.registering(CustomExecTask::class) { |
| 119 | + dependsOn(configureCxxTests) |
| 120 | + workingDir(cxxDir) |
| 121 | + inputs.files(cxxBuildOutputFileTree) |
| 122 | + commandLine(cmakeBinaryPath, "--build", cxxBuildDir.toString(), "-j", buildJobs) |
| 123 | + standardOutputFile.set(project.file("$buildDir/reports/build-cxx-tests.log")) |
| 124 | + errorOutputFile.set(project.file("$buildDir/reports/build-cxx-tests.error.log")) |
| 125 | + } |
| 126 | + |
| 127 | +val runCxxTests by |
| 128 | + tasks.registering(CustomExecTask::class) { |
| 129 | + dependsOn(buildCxxTests) |
| 130 | + workingDir(cxxBuildDir) |
| 131 | + commandLine( |
| 132 | + ctestBinaryPath, |
| 133 | + "--output-on-failure", |
| 134 | + "--output-junit", |
| 135 | + "$reportsDir/cxx-tests-results.xml", |
| 136 | + ) |
| 137 | + standardOutputFile.set(project.file("$buildDir/reports/run-cxx-tests.log")) |
| 138 | + errorOutputFile.set(project.file("$buildDir/reports/run-cxx-tests.error.log")) |
| 139 | + } |
0 commit comments