Skip to content

Commit ba065b4

Browse files
committed
feat: add option for libs to include codegen
1 parent a74387b commit ba065b4

4 files changed

Lines changed: 52 additions & 27 deletions

File tree

packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/GenerateAutolinkingNewArchitecturesFileTask.kt

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ abstract class GenerateAutolinkingNewArchitecturesFileTask : DefaultTask() {
3232
fun taskAction() {
3333
val model = JsonUtils.fromAutolinkingConfigJson(autolinkInputFile.get().asFile)
3434

35-
val packages = filterAndroidPackages(model)
36-
val cmakeFileContent = generateCmakeFileContent(packages)
37-
val cppFileContent = generateCppFileContent(packages)
35+
val dependenciesWithNames = filterAndroidPackagesWithNames(model)
36+
val cmakeFileContent = generateCmakeFileContent(dependenciesWithNames)
37+
val cppFileContent = generateCppFileContent(dependenciesWithNames.map { it.second })
3838

3939
val outputDir = generatedOutputDirectory.get().asFile
4040
outputDir.mkdirs()
@@ -43,49 +43,69 @@ abstract class GenerateAutolinkingNewArchitecturesFileTask : DefaultTask() {
4343
File(outputDir, H_FILENAME).apply { writeText(hTemplate) }
4444
}
4545

46-
internal fun filterAndroidPackages(
46+
internal fun filterAndroidPackagesWithNames(
4747
model: ModelAutolinkingConfigJson?
48-
): List<ModelAutolinkingDependenciesPlatformAndroidJson> =
49-
model?.dependencies?.values?.mapNotNull { it.platforms?.android } ?: emptyList()
48+
): List<Pair<String, ModelAutolinkingDependenciesPlatformAndroidJson>> {
49+
return model?.dependencies?.entries
50+
?.mapNotNull { (name, depValue) ->
51+
depValue.platforms?.android?.let { android -> name to android }
52+
} ?: emptyList()
53+
}
5054

5155
internal fun generateCmakeFileContent(
52-
packages: List<ModelAutolinkingDependenciesPlatformAndroidJson>
56+
dependenciesWithNames: List<Pair<String, ModelAutolinkingDependenciesPlatformAndroidJson>>
5357
): String {
5458
val libraryIncludes =
55-
packages.joinToString("\n") { dep ->
59+
dependenciesWithNames.joinToString("\n") { (depName, dep) ->
5660
var addDirectoryString = ""
5761
val libraryName = dep.libraryName
5862
val cmakeListsPath = dep.cmakeListsPath
5963
val cxxModuleCMakeListsPath = dep.cxxModuleCMakeListsPath
60-
if (libraryName != null && cmakeListsPath != null) {
61-
// If user provided a custom cmakeListsPath, let's honor it.
62-
val nativeFolderPath = sanitizeCmakeListsPath(cmakeListsPath)
63-
addDirectoryString +=
64-
"add_subdirectory(\"$nativeFolderPath\" ${libraryName}_autolinked_build)"
65-
}
66-
if (cxxModuleCMakeListsPath != null) {
67-
// If user provided a custom cxxModuleCMakeListsPath, let's honor it.
68-
val nativeFolderPath = sanitizeCmakeListsPath(cxxModuleCMakeListsPath)
69-
addDirectoryString +=
70-
"\nadd_subdirectory(\"$nativeFolderPath\" ${libraryName}_cxxmodule_autolinked_build)"
64+
65+
if (dep.hasCodegenPrefab) {
66+
addDirectoryString += "find_package($depName CONFIG REQUIRED)"
67+
} else {
68+
if (libraryName != null && cmakeListsPath != null) {
69+
val nativeFolderPath = sanitizeCmakeListsPath(cmakeListsPath)
70+
addDirectoryString +=
71+
"add_subdirectory(\"$nativeFolderPath\" ${libraryName}_autolinked_build)"
72+
}
73+
74+
if (cxxModuleCMakeListsPath != null) {
75+
val nativeFolderPath = sanitizeCmakeListsPath(cxxModuleCMakeListsPath)
76+
addDirectoryString +=
77+
"\nadd_subdirectory(\"$nativeFolderPath\" ${libraryName}_cxxmodule_autolinked_build)"
78+
}
7179
}
7280
addDirectoryString
7381
}
7482

7583
val libraryModules =
76-
packages.joinToString("\n ") { dep ->
84+
dependenciesWithNames.joinToString("\n ") { (depName, dep) ->
7785
var autolinkedLibraries = ""
78-
if (dep.libraryName != null) {
79-
autolinkedLibraries += "$CODEGEN_LIB_PREFIX${dep.libraryName}"
80-
}
81-
if (dep.cxxModuleCMakeListsModuleName != null) {
82-
autolinkedLibraries += "\n${dep.cxxModuleCMakeListsModuleName}"
86+
if (!dep.hasCodegenPrefab) {
87+
if (dep.libraryName != null) {
88+
autolinkedLibraries += "$CODEGEN_LIB_PREFIX${dep.libraryName}"
89+
}
90+
if (dep.cxxModuleCMakeListsModuleName != null) {
91+
autolinkedLibraries += "\n${dep.cxxModuleCMakeListsModuleName}"
92+
}
8393
}
8494
autolinkedLibraries
8595
}
8696

97+
val prefabModules =
98+
dependenciesWithNames.joinToString("\n ") { (depName, dep) ->
99+
var prefabLibraries = ""
100+
if (dep.hasCodegenPrefab) {
101+
prefabLibraries += "$depName::$CODEGEN_LIB_PREFIX${dep.libraryName}"
102+
}
103+
prefabLibraries
104+
}
105+
87106
return CMAKE_TEMPLATE.replace("{{ libraryIncludes }}", libraryIncludes)
88107
.replace("{{ libraryModules }}", libraryModules)
108+
.replace("{{ prefabModules }}", prefabModules)
89109
}
90110

91111
internal fun generateCppFileContent(
@@ -178,6 +198,10 @@ abstract class GenerateAutolinkingNewArchitecturesFileTask : DefaultTask() {
178198
set(AUTOLINKED_LIBRARIES
179199
{{ libraryModules }}
180200
)
201+
202+
set(PREFAB_LIBRARIES
203+
{{ prefabModules }}
204+
)
181205
"""
182206
.trimIndent()
183207

packages/gradle-plugin/shared/src/main/kotlin/com/facebook/react/model/ModelAutolinkingDependenciesPlatformAndroidJson.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ data class ModelAutolinkingDependenciesPlatformAndroidJson(
2020
val cxxModuleHeaderName: String? = null,
2121
val dependencyConfiguration: String? = null,
2222
val isPureCxxDependency: Boolean? = null,
23+
val hasCodegenPrefab: Boolean = false,
2324
)

packages/react-native-codegen/src/generators/modules/GenerateModuleJniH.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ file(GLOB react_codegen_SRCS CONFIGURE_DEPENDS *.cpp react/renderer/components/$
7777
7878
add_library(
7979
react_codegen_${targetName}
80-
OBJECT
80+
SHARED
8181
\${react_codegen_SRCS}
8282
)
8383

packages/react-native/ReactAndroid/cmake-utils/ReactNative-application.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ target_compile_options(common_flags INTERFACE ${folly_FLAGS})
9292
# If project is on RN CLI v9, then we can use the following lines to link against the autolinked 3rd party libraries.
9393
if(EXISTS ${PROJECT_BUILD_DIR}/generated/autolinking/src/main/jni/Android-autolinking.cmake)
9494
include(${PROJECT_BUILD_DIR}/generated/autolinking/src/main/jni/Android-autolinking.cmake)
95-
target_link_libraries(${CMAKE_PROJECT_NAME} ${AUTOLINKED_LIBRARIES})
95+
target_link_libraries(${CMAKE_PROJECT_NAME} ${AUTOLINKED_LIBRARIES} ${PREBUILT_LIBRARIES})
9696
foreach(autolinked_library ${AUTOLINKED_LIBRARIES})
9797
target_link_libraries(${autolinked_library} common_flags)
9898
endforeach()

0 commit comments

Comments
 (0)