Skip to content

Commit

Permalink
[android] Add support for autolinking via CMake (#1630)
Browse files Browse the repository at this point in the history
  • Loading branch information
cortinico authored Jun 28, 2022
1 parent 5002bea commit 86df104
Showing 1 changed file with 46 additions and 2 deletions.
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 ->
}
}
}


0 comments on commit 86df104

Please sign in to comment.