Commit de2306bd authored by topjohnwu's avatar topjohnwu

Proper incremental builds

Auto generate flag.h for precise rebuilding
parent 714feeb9
...@@ -10,6 +10,7 @@ import shutil ...@@ -10,6 +10,7 @@ import shutil
import stat import stat
import subprocess import subprocess
import sys import sys
import textwrap
import urllib.request import urllib.request
import zipfile import zipfile
from distutils.dir_util import copy_tree from distutils.dir_util import copy_tree
...@@ -67,6 +68,7 @@ ndk_path = op.join(ndk_root, 'magisk') ...@@ -67,6 +68,7 @@ ndk_path = op.join(ndk_root, 'magisk')
ndk_build = op.join(ndk_path, 'ndk-build') ndk_build = op.join(ndk_path, 'ndk-build')
gradlew = op.join('.', 'gradlew' + ('.bat' if is_windows else '')) gradlew = op.join('.', 'gradlew' + ('.bat' if is_windows else ''))
adb_path = op.join(sdk_path, 'platform-tools', 'adb' + ('.exe' if is_windows else '')) adb_path = op.join(sdk_path, 'platform-tools', 'adb' + ('.exe' if is_windows else ''))
native_gen_path = op.join('native', 'out', 'generated')
# Global vars # Global vars
config = {} config = {}
...@@ -244,33 +246,60 @@ def sign_zip(unsigned): ...@@ -244,33 +246,60 @@ def sign_zip(unsigned):
error('Signing failed!') error('Signing failed!')
def binary_dump(src, out, var_name): def binary_dump(src, var_name):
out.write(f'constexpr unsigned char {var_name}[] = {{') out_str = f'constexpr unsigned char {var_name}[] = {{'
for i, c in enumerate(xz(src.read())): for i, c in enumerate(xz(src.read())):
if i % 16 == 0: if i % 16 == 0:
out.write('\n') out_str += '\n'
out.write(f'0x{c:02X},') out_str += f'0x{c:02X},'
out.write('\n};\n') out_str += '\n};\n'
out.flush() return out_str
def run_ndk_build(flags): def run_ndk_build(flags):
os.chdir('native') os.chdir('native')
proc = system(f'{ndk_build} {base_flags} {flags} -j{cpu_count}') proc = system(f'{ndk_build} {flags} -j{cpu_count}')
if proc.returncode != 0: if proc.returncode != 0:
error('Build binary failed!') error('Build binary failed!')
os.chdir('..') os.chdir('..')
collect_binary() collect_binary()
def dump_bin_headers(): def write_if_diff(file_name, text):
do_write = True
if op.exists(file_name):
with open(file_name, 'r') as f:
orig = f.read()
do_write = orig != text
if do_write:
with open(file_name, 'w') as f:
f.write(text)
def dump_bin_header():
stub = op.join(config['outdir'], 'stub-release.apk') stub = op.join(config['outdir'], 'stub-release.apk')
if not op.exists(stub): if not op.exists(stub):
error('Build stub APK before building "magiskinit"') error('Build stub APK before building "magiskinit"')
mkdir_p(op.join('native', 'out')) mkdir_p(native_gen_path)
with open(op.join('native', 'out', 'binaries.h'), 'w') as out:
with open(stub, 'rb') as src: with open(stub, 'rb') as src:
binary_dump(src, out, 'manager_xz') text = binary_dump(src, 'manager_xz')
write_if_diff(op.join(native_gen_path, 'binaries.h'), text)
def dump_flag_header():
flag_txt = textwrap.dedent('''\
#pragma once
#define quote(s) #s
#define str(s) quote(s)
#define MAGISK_FULL_VER MAGISK_VERSION "(" str(MAGISK_VER_CODE) ")"
#define NAME_WITH_VER(name) str(name) " " MAGISK_FULL_VER
''')
flag_txt += f'#define MAGISK_VERSION "{config["version"]}"\n'
flag_txt += f'#define MAGISK_VER_CODE {config["versionCode"]}\n'
flag_txt += f'#define MAGISK_DEBUG {0 if args.release else 1}\n'
mkdir_p(native_gen_path)
write_if_diff(op.join(native_gen_path, 'flags.h'), flag_txt)
def build_binary(args): def build_binary(args):
...@@ -291,25 +320,7 @@ def build_binary(args): ...@@ -291,25 +320,7 @@ def build_binary(args):
header('* Building binaries: ' + ' '.join(args.target)) header('* Building binaries: ' + ' '.join(args.target))
update_flags = False dump_flag_header()
flags = op.join('native', 'jni', 'include', 'flags.hpp')
flags_stat = os.stat(flags)
if op.exists(args.config):
if os.stat(args.config).st_mtime_ns > flags_stat.st_mtime_ns:
update_flags = True
if os.stat('gradle.properties').st_mtime_ns > flags_stat.st_mtime_ns:
update_flags = True
if update_flags:
os.utime(flags)
# Basic flags
global base_flags
base_flags = f'MAGISK_VERSION={config["version"]} MAGISK_VER_CODE={config["versionCode"]}'
if not args.release:
base_flags += ' MAGISK_DEBUG=1'
flag = '' flag = ''
...@@ -320,7 +331,7 @@ def build_binary(args): ...@@ -320,7 +331,7 @@ def build_binary(args):
flag += ' B_TEST=1' flag += ' B_TEST=1'
if 'magiskinit' in args.target: if 'magiskinit' in args.target:
dump_bin_headers() dump_bin_header()
flag += ' B_INIT=1' flag += ' B_INIT=1'
if 'magiskpolicy' in args.target: if 'magiskpolicy' in args.target:
......
...@@ -9,7 +9,6 @@ ifdef B_MAGISK ...@@ -9,7 +9,6 @@ ifdef B_MAGISK
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_MODULE := magisk LOCAL_MODULE := magisk
LOCAL_STATIC_LIBRARIES := libnanopb libsystemproperties libutils libphmap libxhook LOCAL_STATIC_LIBRARIES := libnanopb libsystemproperties libutils libphmap libxhook
LOCAL_C_INCLUDES := jni/include
LOCAL_SRC_FILES := \ LOCAL_SRC_FILES := \
core/applets.cpp \ core/applets.cpp \
...@@ -49,7 +48,6 @@ ifdef B_INIT ...@@ -49,7 +48,6 @@ ifdef B_INIT
LOCAL_MODULE := magiskinit LOCAL_MODULE := magiskinit
LOCAL_STATIC_LIBRARIES := libsepol libxz libutils LOCAL_STATIC_LIBRARIES := libsepol libxz libutils
LOCAL_C_INCLUDES := jni/include out
LOCAL_SRC_FILES := \ LOCAL_SRC_FILES := \
init/init.cpp \ init/init.cpp \
...@@ -76,7 +74,6 @@ ifdef B_BOOT ...@@ -76,7 +74,6 @@ ifdef B_BOOT
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_MODULE := magiskboot LOCAL_MODULE := magiskboot
LOCAL_STATIC_LIBRARIES := libmincrypt liblzma liblz4 libbz2 libfdt libutils libz libzopfli LOCAL_STATIC_LIBRARIES := libmincrypt liblzma liblz4 libbz2 libfdt libutils libz libzopfli
LOCAL_C_INCLUDES := jni/include
LOCAL_SRC_FILES := \ LOCAL_SRC_FILES := \
magiskboot/main.cpp \ magiskboot/main.cpp \
...@@ -99,7 +96,6 @@ ifdef B_POLICY ...@@ -99,7 +96,6 @@ ifdef B_POLICY
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_MODULE := magiskpolicy LOCAL_MODULE := magiskpolicy
LOCAL_STATIC_LIBRARIES := libsepol libutils LOCAL_STATIC_LIBRARIES := libsepol libutils
LOCAL_C_INCLUDES := jni/include
LOCAL_SRC_FILES := \ LOCAL_SRC_FILES := \
core/applet_stub.cpp \ core/applet_stub.cpp \
...@@ -120,7 +116,6 @@ ifdef B_PROP ...@@ -120,7 +116,6 @@ ifdef B_PROP
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_MODULE := resetprop LOCAL_MODULE := resetprop
LOCAL_STATIC_LIBRARIES := libnanopb libsystemproperties libutils LOCAL_STATIC_LIBRARIES := libnanopb libsystemproperties libutils
LOCAL_C_INCLUDES := jni/include
LOCAL_SRC_FILES := \ LOCAL_SRC_FILES := \
core/applet_stub.cpp \ core/applet_stub.cpp \
...@@ -139,7 +134,6 @@ ifneq (,$(wildcard jni/test.cpp)) ...@@ -139,7 +134,6 @@ ifneq (,$(wildcard jni/test.cpp))
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_MODULE := test LOCAL_MODULE := test
LOCAL_STATIC_LIBRARIES := libutils libphmap LOCAL_STATIC_LIBRARIES := libutils libphmap
LOCAL_C_INCLUDES := jni/include
LOCAL_SRC_FILES := test.cpp LOCAL_SRC_FILES := test.cpp
include $(BUILD_EXECUTABLE) include $(BUILD_EXECUTABLE)
......
...@@ -10,10 +10,4 @@ APP_STRIP_MODE := --strip-all ...@@ -10,10 +10,4 @@ APP_STRIP_MODE := --strip-all
# Busybox should use stock libc.a # Busybox should use stock libc.a
ifdef B_BB ifdef B_BB
APP_PLATFORM := android-22 APP_PLATFORM := android-22
else
# Make Busybox cflag stable
APP_CFLAGS += -D__MVSTR=${MAGISK_VERSION} -D__MCODE=${MAGISK_VER_CODE}
ifdef MAGISK_DEBUG
APP_CFLAGS += -D__MDBG
endif
endif endif
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include <selinux.hpp> #include <selinux.hpp>
#include <db.hpp> #include <db.hpp>
#include <resetprop.hpp> #include <resetprop.hpp>
#include <flags.hpp> #include <flags.h>
#include "core.hpp" #include "core.hpp"
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#include <magisk.hpp> #include <magisk.hpp>
#include <daemon.hpp> #include <daemon.hpp>
#include <selinux.hpp> #include <selinux.hpp>
#include <flags.hpp> #include <flags.h>
#include "core.hpp" #include "core.hpp"
......
#pragma once
/* Include this header anywhere accessing MAGISK_DEBUG, MAGISK_VERSION, MAGISK_VER_CODE.
*
* This file is for precise incremental builds. We can make sure code that uses
* external flags are re-compiled by updating the timestamp of this file
* */
#define quote(s) #s
#define str(s) quote(s)
#define MAGISK_VERSION str(__MVSTR)
#define MAGISK_VER_CODE __MCODE
#define MAGISK_FULL_VER MAGISK_VERSION "(" str(MAGISK_VER_CODE) ")"
#define NAME_WITH_VER(name) str(name) " " MAGISK_FULL_VER
#ifdef __MDBG
#define MAGISK_DEBUG
#endif
...@@ -6,10 +6,11 @@ ...@@ -6,10 +6,11 @@
#include <vector> #include <vector>
#include <xz.h> #include <xz.h>
#include <magisk.hpp> #include <magisk.hpp>
#include <utils.hpp> #include <utils.hpp>
#include <binaries.h>
#include "binaries.h"
#include "init.hpp" #include "init.hpp"
using namespace std; using namespace std;
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
#include <magisk.hpp> #include <magisk.hpp>
#include <daemon.hpp> #include <daemon.hpp>
#include <utils.hpp> #include <utils.hpp>
#include <flags.hpp> #include <flags.h>
#include "su.hpp" #include "su.hpp"
#include "pts.hpp" #include "pts.hpp"
......
LOCAL_PATH := $(call my-dir) LOCAL_PATH := $(call my-dir)
# All Magisk common code lives here
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_MODULE:= libutils LOCAL_MODULE:= libutils
LOCAL_C_INCLUDES := jni/include $(LOCAL_PATH)/include LOCAL_C_INCLUDES := jni/include $(LOCAL_PATH)/include out/generated
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
LOCAL_EXPORT_STATIC_LIBRARIES := libcxx LOCAL_EXPORT_STATIC_LIBRARIES := libcxx
LOCAL_STATIC_LIBRARIES := libcxx LOCAL_STATIC_LIBRARIES := libcxx
LOCAL_SRC_FILES := \ LOCAL_SRC_FILES := \
......
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <flags.hpp> #include <flags.h>
#include "logging.hpp" #include "logging.hpp"
...@@ -47,7 +47,7 @@ void cmdline_logging() { ...@@ -47,7 +47,7 @@ void cmdline_logging() {
} }
// LTO will optimize out the NOP function // LTO will optimize out the NOP function
#ifdef MAGISK_DEBUG #if MAGISK_DEBUG
void LOGD(const char *fmt, ...) { LOG_BODY(d) } void LOGD(const char *fmt, ...) { LOG_BODY(d) }
#else #else
void LOGD(const char *fmt, ...) {} void LOGD(const char *fmt, ...) {}
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#include <bitset> #include <bitset>
#include <utils.hpp> #include <utils.hpp>
#include <flags.hpp> #include <flags.h>
#include <daemon.hpp> #include <daemon.hpp>
#include "inject.hpp" #include "inject.hpp"
...@@ -356,7 +356,7 @@ static int hook_register(const char *path, const char *symbol, void *new_func, v ...@@ -356,7 +356,7 @@ static int hook_register(const char *path, const char *symbol, void *new_func, v
#define APP_PROCESS "^/system/bin/app_process.*" #define APP_PROCESS "^/system/bin/app_process.*"
void hook_functions() { void hook_functions() {
#ifdef MAGISK_DEBUG #if MAGISK_DEBUG
xhook_enable_debug(1); xhook_enable_debug(1);
xhook_enable_sigsegv_protection(0); xhook_enable_sigsegv_protection(0);
#endif #endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment