Skip to content

Commit

Permalink
[core] Build libc++ ourselves (LSPosed#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
yujincheng08 authored Mar 26, 2021
1 parent 40c43fe commit e5d61af
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 22 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
[submodule "core/src/main/cpp/external/DexBuilder"]
path = core/src/main/cpp/external/DexBuilder
url = https://github.com/LSPosed/DexBuilder.git
[submodule "core/src/main/cpp/external/libcxx"]
path = core/src/main/cpp/external/libcxx
url = https://github.com/topjohnwu/libcxx.git
[submodule "core/src/main/cpp/external/Dobby"]
path = core/src/main/cpp/external/Dobby
url = https://github.com/jmpews/Dobby.git
100 changes: 80 additions & 20 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.tools.ant.filters.FixCrLfFilter
import org.gradle.internal.os.OperatingSystem
import org.jetbrains.kotlin.daemon.common.toHexString
import java.nio.file.Paths

import java.security.MessageDigest

Expand Down Expand Up @@ -64,7 +65,7 @@ val verName: String by rootProject.extra

dependencies {
implementation("dev.rikka.ndk:riru:${moduleMinRiruVersionName}")
implementation(files("libs/dobby_prefab.aar"))
// implementation(files("libs/dobby_prefab.aar"))
implementation("com.android.tools.build:apksig:4.1.3")
compileOnly(project(":hiddenapi-stubs"))
compileOnly("androidx.annotation:annotation:1.2.0")
Expand Down Expand Up @@ -93,10 +94,13 @@ android {
abiFilters("arm64-v8a", "armeabi-v7a", "x86", "x86_64")
cppFlags("-std=c++20 -ffixed-x18 -Qunused-arguments -fno-rtti -fno-exceptions -fomit-frame-pointer -fpie -fPIC")
cFlags("-std=c11 -ffixed-x18 -Qunused-arguments -fno-rtti -fno-exceptions -fomit-frame-pointer -fpie -fPIC")
arguments("-DRIRU_MODULE_API_VERSION=$moduleMaxRiruApiVersion",
"-DRIRU_MODULE_VERSION=$verCode",
"-DRIRU_MODULE_VERSION_NAME:STRING=\"$verName\"",
"-DMODULE_NAME:STRING=riru_$riruModuleId")
arguments(
"-DRIRU_MODULE_API_VERSION=$moduleMaxRiruApiVersion",
"-DRIRU_MODULE_VERSION=$verCode",
"-DRIRU_MODULE_VERSION_NAME:STRING=\"$verName\"",
"-DMODULE_NAME:STRING=riru_$riruModuleId",
"-DANDROID_STL=none"
)
targets("lspd")
}
}
Expand Down Expand Up @@ -145,6 +149,41 @@ android {
}
}

fun findInPath(executable: String): String? {
val pathEnv = System.getenv("PATH")
return pathEnv.split(File.pathSeparator).map { folder ->
Paths.get("${folder}${File.separator}${executable}").toFile()
}.firstOrNull { path ->
path.exists()
}?.absolutePath
}

task("buildLibcxx", Exec::class) {
val ndkDir = android.ndkDirectory
executable = "$ndkDir/${if (isWindows) "ndk-build.cmd" else "ndk-build"}"
workingDir = projectDir
findInPath("ccache")?.let {
println("using ccache $it")
environment("NDK_CCACHE", it)
environment("USE_CCACHE", "1")
environment("CCACHE_COMPILERCHECK", "content")
} ?: run {
println("not using ccache")
}

setArgs(
arrayListOf(
"NDK_PROJECT_PATH=build/intermediates/ndk",
"APP_BUILD_SCRIPT=$projectDir/src/main/cpp/external/libcxx/Android.mk",
"APP_CPPFLAGS=-std=c++20",
"APP_STL=none",
"-j${Runtime.getRuntime().availableProcessors()}"
)
)
}

tasks.getByName("preBuild").dependsOn("buildLibcxx")

afterEvaluate {

android.applicationVariants.forEach { variant ->
Expand All @@ -162,12 +201,17 @@ afterEvaluate {
from("$projectDir/tpl/module.prop.tpl")
into(zipPathMagiskReleasePath)
rename("module.prop.tpl", "module.prop")
expand("moduleId" to moduleId,
"versionName" to verName,
"versionCode" to verCode,
"authorList" to authors,
"minRiruVersionName" to moduleMinRiruVersionName)
filter(mapOf("eol" to FixCrLfFilter.CrLf.newInstance("lf")), FixCrLfFilter::class.java)
expand(
"moduleId" to moduleId,
"versionName" to verName,
"versionCode" to verCode,
"authorList" to authors,
"minRiruVersionName" to moduleMinRiruVersionName
)
filter(
mapOf("eol" to FixCrLfFilter.CrLf.newInstance("lf")),
FixCrLfFilter::class.java
)
}
copy {
from("${rootProject.projectDir}/README.md")
Expand Down Expand Up @@ -196,11 +240,23 @@ afterEvaluate {
include("riru.sh")
filter { line ->
line.replace("%%%RIRU_MODULE_ID%%%", riruModuleId)
.replace("%%%RIRU_MODULE_API_VERSION%%%", moduleMaxRiruApiVersion.toString())
.replace("%%%RIRU_MODULE_MIN_API_VERSION%%%", moduleMinRiruApiVersion.toString())
.replace("%%%RIRU_MODULE_MIN_RIRU_VERSION_NAME%%%", moduleMinRiruVersionName)
.replace(
"%%%RIRU_MODULE_API_VERSION%%%",
moduleMaxRiruApiVersion.toString()
)
.replace(
"%%%RIRU_MODULE_MIN_API_VERSION%%%",
moduleMinRiruApiVersion.toString()
)
.replace(
"%%%RIRU_MODULE_MIN_RIRU_VERSION_NAME%%%",
moduleMinRiruVersionName
)
}
filter(mapOf("eol" to FixCrLfFilter.CrLf.newInstance("lf")), FixCrLfFilter::class.java)
filter(
mapOf("eol" to FixCrLfFilter.CrLf.newInstance("lf")),
FixCrLfFilter::class.java
)
}
copy {
include("lspd")
Expand Down Expand Up @@ -252,9 +308,11 @@ afterEvaluate {
task("push${variantCapped}", Exec::class) {
dependsOn(zipTask)
workingDir("${projectDir}/release")
val commands = arrayOf(android.adbExecutable, "push",
zipFileName,
"/data/local/tmp/")
val commands = arrayOf(
android.adbExecutable, "push",
zipFileName,
"/data/local/tmp/"
)
if (isWindows) {
commandLine("cmd", "/c", commands.joinToString(" "))
} else {
Expand All @@ -264,8 +322,10 @@ afterEvaluate {
task("flash${variantCapped}", Exec::class) {
dependsOn(tasks.getByPath("push${variantCapped}"))
workingDir("${projectDir}/release")
val commands = arrayOf(android.adbExecutable, "shell", "su", "-c",
"magisk --install-module /data/local/tmp/${zipFileName}")
val commands = arrayOf(
android.adbExecutable, "shell", "su", "-c",
"magisk --install-module /data/local/tmp/${zipFileName}"
)
if (isWindows) {
commandLine("cmd", "/c", commands.joinToString(" "))
} else {
Expand Down
Binary file removed core/libs/dobby_prefab.aar
Binary file not shown.
2 changes: 2 additions & 0 deletions core/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ add_definitions(-DRIRU_MODULE_VERSION=${RIRU_MODULE_VERSION})
add_definitions(-DRIRU_MODULE_VERSION_NAME=${RIRU_MODULE_VERSION_NAME})
add_definitions(-DMODULE_NAME=${MODULE_NAME})

include_directories(external/libcxx/include)

add_subdirectory(main)
add_subdirectory(external)
16 changes: 16 additions & 0 deletions core/src/main/cpp/external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,19 @@ add_subdirectory(yahfa)

add_subdirectory(DexBuilder)
target_include_directories(dex_builder PUBLIC DexBuilder)

macro(SET_OPTION option value)
set(${option} ${value} CACHE INTERNAL "" FORCE)
endmacro()

SET_OPTION(DOBBY_GENERATE_SHARED OFF)
SET_OPTION(Plugin.Android.BionicLinkerRestriction ON)
if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
SET_OPTION(DOBBY_DEBUG OFF)
endif (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
add_subdirectory(Dobby)
target_include_directories(dobby PUBLIC Dobby/include)
target_include_directories(dobby PUBLIC Dobby/builtin-plugin/BionicLinkerRestriction)

add_library(libcxx STATIC IMPORTED GLOBAL)
set_property(TARGET libcxx PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../../../../build/intermediates/ndk/obj/local/${CMAKE_ANDROID_ARCH_ABI}/libcxx.a)
1 change: 1 addition & 0 deletions core/src/main/cpp/external/Dobby
Submodule Dobby added at 1e3acd
1 change: 1 addition & 0 deletions core/src/main/cpp/external/libcxx
Submodule libcxx added at cca529
5 changes: 3 additions & 2 deletions core/src/main/cpp/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ aux_source_directory(src/jni SRC_JNI_LIST)
include_directories(include src)
add_executable(lspd ${SRC_LIST} ${SRC_JNI_LIST})

set(ENV{CCACHE_COMPILERCHECK} "content")

SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--dynamic-list=${CMAKE_SOURCE_DIR}/dynamic_list")

find_package(riru REQUIRED CONFIG)
find_package(dobby REQUIRED CONFIG)
find_library(log-lib log)
target_link_libraries(lspd yahfa riru::riru android dobby::dobby dex_builder ${log-lib})
target_link_libraries(lspd yahfa riru::riru android dobby dex_builder libcxx ${log-lib})

0 comments on commit e5d61af

Please sign in to comment.