Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[android] Add support for autolinking via CMake #1630

Merged
merged 2 commits into from
Jun 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions packages/platform-android/native_modules.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ import-codegen-modules := \\
{{ libraryModules }}
"""

def cmakeTemplate = """# This code was generated by [React Native CLI](https://www.npmjs.com/package/@react-native-community/cli)

cmake_minimum_required(VERSION 3.13)
set(CMAKE_VERBOSE_MAKEFILE on)

{{ libraryIncludes }}

set(AUTOLINKED_LIBRARIES
{{ libraryModules }}
)
"""

def rncliCppTemplate = """/**
* This code was generated by [React Native CLI](https://www.npmjs.com/package/@react-native-community/cli).
*
Expand Down Expand Up @@ -275,6 +287,39 @@ class ReactNativeModules {
}
}

void generateCmakeFile(File outputDir, String generatedFileName, String generatedFileContentsTemplate) {
ArrayList<HashMap<String, String>> packages = this.reactNativeModules
String packageName = this.packageName
String codegenLibPrefix = "react_codegen_"
String libraryIncludes = ""
String libraryModules = ""

if (packages.size() > 0) {
libraryIncludes = packages.collect {
if (it.libraryName != null && it.androidMkPath != null) {
// The CMakeLists.txt file is in the same folder as the Android.mk
String nativeFolderPath = it.androidMkPath.replace("Android.mk", "")
"add_subdirectory($nativeFolderPath ${it.libraryName}_autolinked_build)"
} else {
null
}
}.minus(null).join('\n')
libraryModules = packages.collect {
it.libraryName ? "${codegenLibPrefix}${it.libraryName}" : null
}.minus(null).join(" \\\n ")
}

String generatedFileContents = generatedFileContentsTemplate
.replace("{{ libraryIncludes }}", libraryIncludes)
.replace("{{ libraryModules }}", libraryModules)

outputDir.mkdirs()
final FileTreeBuilder treeBuilder = new FileTreeBuilder(outputDir)
treeBuilder.file(generatedFileName).newWriter().withWriter { w ->
w << generatedFileContents
}
}

void generateRncliCpp(File outputDir, String generatedFileName, String generatedFileContentsTemplate) {
ArrayList<HashMap<String, String>> packages = this.reactNativeModules
String rncliCppIncludes = ""
Expand Down Expand Up @@ -487,6 +532,7 @@ ext.applyNativeModulesAppBuildGradle = { Project project, String root = null ->
task generateNewArchitectureFiles {
doLast {
autoModules.generateAndroidMkFile(generatedJniDir, "Android-rncli.mk", androidMkTemplate)
autoModules.generateCmakeFile(generatedJniDir, "Android-rncli.cmake", cmakeTemplate)
autoModules.generateRncliCpp(generatedJniDir, "rncli.cpp", rncliCppTemplate)
autoModules.generateRncliH(generatedJniDir, "rncli.h", rncliHTemplate)
}
Expand All @@ -508,5 +554,3 @@ ext.applyNativeModulesAppBuildGradle = { Project project, String root = null ->
}
}
}