Make build.py generate flags.prop for app projects

Reduce logic duplication in build.py and app/buildSrc. The ground truth
is always build.py, so dump all information into a file so the gradle
projects are always in sync with the rest of the project.
This commit is contained in:
topjohnwu
2026-03-18 01:49:38 -07:00
parent 59eca3fd0a
commit 9035a94804
4 changed files with 48 additions and 36 deletions
-1
View File
@@ -21,5 +21,4 @@ dependencies {
implementation("org.jetbrains.kotlin:compose-compiler-gradle-plugin:${libs.versions.kotlin.get()}")
implementation("org.jetbrains.kotlin:kotlin-serialization:${libs.versions.kotlin.get()}")
implementation(libs.android.gradle.plugin)
implementation(libs.jgit)
}
+11 -17
View File
@@ -1,5 +1,4 @@
import org.eclipse.jgit.internal.storage.file.FileRepository
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.provideDelegate
@@ -13,9 +12,6 @@ val RAND_SEED = if (System.getenv("CI") != null) 42 else 0
lateinit var RANDOM: Random
private val props = Properties()
private var commitHash = ""
private val supportAbis = setOf("armeabi-v7a", "x86", "arm64-v8a", "x86_64", "riscv64")
private val defaultAbis = setOf("armeabi-v7a", "x86", "arm64-v8a", "x86_64")
object Config {
operator fun get(key: String): String? {
@@ -25,13 +21,10 @@ object Config {
fun contains(key: String) = get(key) != null
val version: String get() = get("version") ?: commitHash
val version: String get() = get("version")!!
val versionCode: Int get() = get("magisk.versionCode")!!.toInt()
val stubVersion: String get() = get("magisk.stubVersion")!!
val abiList: Set<String> get() {
val abiList = get("abiList") ?: return defaultAbis
return abiList.split(Regex("\\s*,\\s*")).toSet() intersect supportAbis
}
val abiList: List<String> get() = get("abiList")!!.split(",")
}
fun Project.rootFile(path: String): File {
@@ -57,17 +50,18 @@ class MagiskPlugin : Plugin<Project> {
configFile.inputStream().use {
val config = Properties()
config.load(it)
// Remove properties that should be passed by commandline
config.remove("abiList")
props.putAll(config)
}
}
// Commandline override
findProperty("abiList")?.let { props.put("abiList", it) }
val repo = FileRepository(rootFile(".git"))
val refId = repo.refDatabase.exactRef("HEAD").objectId
commitHash = repo.newObjectReader().abbreviate(refId, 8).name()
// Load flags.prop, generated by build.py
val flagsProp = rootProject.layout.buildDirectory.file("flags.prop").get().asFile
if (flagsProp.exists()) {
flagsProp.inputStream().use {
val flags = Properties()
flags.load(it)
props.putAll(flags)
}
}
}
}
-1
View File
@@ -26,7 +26,6 @@ okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
okhttp-dnsoverhttps = { module = "com.squareup.okhttp3:okhttp-dnsoverhttps", version.ref = "okhttp" }
okhttp-logging = { module = "com.squareup.okhttp3:logging-interceptor", version.ref = "okhttp" }
timber = { module = "com.jakewharton.timber:timber", version = "5.0.1" }
jgit = { module = "org.eclipse.jgit:org.eclipse.jgit", version = "7.1.0.202411261347-r" }
# AndroidX
activity = { module = "androidx.activity:activity", version = "1.12.4" }
+37 -17
View File
@@ -63,10 +63,10 @@ cpu_count = multiprocessing.cpu_count()
# Common constants
support_abis = {
"armeabi-v7a": "thumbv7neon-linux-androideabi",
"x86": "i686-linux-android",
"arm64-v8a": "aarch64-linux-android",
"armeabi-v7a": "thumbv7neon-linux-androideabi",
"x86_64": "x86_64-linux-android",
"x86": "i686-linux-android",
"riscv64": "riscv64-linux-android",
}
abi_alias = {
@@ -311,7 +311,7 @@ def write_if_diff(file_name: Path, text: str):
f.write(text)
def dump_flag_header():
def dump_flags_native():
flag_txt = "#pragma once\n"
flag_txt += f'#define MAGISK_VERSION "{config["version"]}"\n'
flag_txt += f'#define MAGISK_VER_CODE {config["versionCode"]}\n'
@@ -356,7 +356,7 @@ def build_native():
header("* Building: " + " ".join(targets))
dump_flag_header()
dump_flags_native()
build_rust_src(targets)
build_cpp_src(targets)
@@ -398,10 +398,21 @@ def find_jdk():
return env
def dump_flags_app():
flag_txt = f"abiList={','.join(build_abis.keys())}\n"
flag_txt += f"version={config['version']}\n"
flag_txt += f"versionCode={config['versionCode']}\n"
app_build_dir = Path("app", "build")
app_build_dir.mkdir(parents=True, exist_ok=True)
write_if_diff(app_build_dir / "flags.prop", flag_txt)
def build_apk(module: str):
ensure_paths()
dump_flags_app()
env = find_jdk()
props = args.config.resolve()
config_path = args.config.resolve()
os.chdir("app")
build_type = "Release" if args.release else "Debug"
@@ -409,8 +420,7 @@ def build_apk(module: str):
[
gradlew,
f"{module}:assemble{build_type}",
f"-PconfigPath={props}",
f"-PabiList={','.join(build_abis.keys())}",
f"-PconfigPath={config_path}",
],
env=env,
)
@@ -528,14 +538,25 @@ def build_all():
def gen_ide():
ensure_paths()
set_build_abis({args.abi})
# Dump flags for both C++ and Rust code
dump_flag_header()
# Dump flags for both native and app
dump_flags_native()
dump_flags_app()
if not args.abi:
# Find the first 64-bit abi in build_abis
for abi in build_abis.keys():
if "64" in abi:
args.abi = abi
break
# If no 64-bit abi is found, use the first abi
args.abi = next(iter(build_abis.keys()))
set_build_abis({args.abi})
# Run build.rs to generate Rust/C++ FFI bindings
os.chdir(Path("native", "src"))
run_cargo(["check"])
run_cargo(["check", "--target", build_abis[args.abi]])
os.chdir(Path("..", ".."))
# Generate compilation database
@@ -785,15 +806,14 @@ def load_config():
config["versionCode"] = 1000000
config["outdir"] = "out"
# Load prop files
# Load config.prop
if args.config.exists():
config.update(parse_props(args.config))
gradle_props = Path("app", "gradle.properties")
if gradle_props.exists():
for key, value in parse_props(gradle_props).items():
if key.startswith("magisk."):
config[key[7:]] = value
for key, value in parse_props(gradle_props).items():
if key.startswith("magisk."):
config[key[7:]] = value
try:
config["versionCode"] = int(config["versionCode"])
@@ -893,7 +913,7 @@ def parse_args():
)
gen_parser = subparsers.add_parser("gen", help="generate files for IDE")
gen_parser.add_argument("--abi", default="arm64-v8a", help="target ABI to generate")
gen_parser.add_argument("--abi", help="target ABI to generate")
# Set callbacks
all_parser.set_defaults(func=build_all)