Index: abb-build/build.py |
=================================================================== |
new file mode 100755 |
--- /dev/null |
+++ b/abb-build/build.py |
@@ -0,0 +1,172 @@ |
+#!/usr/bin/env python |
+ |
+import glob |
+import json |
+import os |
+import re |
+import shutil |
+import string |
+import subprocess |
+import sys |
+import tempfile |
+ |
+_BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
+_ENSURE_DEPENDENCIES_PATH = os.path.join(_BASE_DIR, "ensure_dependencies.py") |
+_GENERATED_PATH = os.path.join(_BASE_DIR, "generated") |
+_GENERATED_MOZCONFIG_PATH = os.path.join(_GENERATED_PATH, "mozconfig") |
+_MOZCONFIG_COMMON_PATH = os.path.join(_BASE_DIR, "mozconfig-common") |
+_MOZCONFIG_ARM_PATH = os.path.join(_BASE_DIR, "mozconfig-arm") |
+_MOZCONFIG_X86_PATH = os.path.join(_BASE_DIR, "mozconfig-x86") |
+_MOZCONFIG_STORE_PATH = os.path.join(_BASE_DIR, "mozconfig-store") |
+_MOZCONFIG_RELEASE_PATH = os.path.join(_BASE_DIR, "mozconfig-release") |
+_MOZCONFIG_CUSTOM_PATH = os.path.join(_BASE_DIR, "mozconfig-custom") |
+_MULTI_L10N_PATH = os.path.join(_BASE_DIR, "mozharness", "scripts", |
+ "multil10n.py") |
+_ABB_PATH = os.path.normpath(os.path.join(_BASE_DIR, "..")) |
René Jeschke
2017/09/22 11:57:40
This had to be changed.
|
+_CMD_BUILD = "build" |
+_CMD_SIGN = "sign" |
+_CMD_BUILD_SIGN = "build-and-sign" |
+_ARCH_ARM = "arm" |
+_ARCH_X86 = "x86" |
+_ARCH_X86_I386 = "i386" |
+_ARCHS = (_ARCH_ARM, _ARCH_X86) |
+_DIST_STANDALONE = "standalone" |
+_DIST_STORE = "store" |
+_DIST_MODES = (_DIST_STANDALONE, _DIST_STORE) |
+_BUILD_DEVBUILD = "devbuild" |
+_BUILD_RELEASE = "release" |
+_BUILD_MODES = (_BUILD_DEVBUILD, _BUILD_RELEASE) |
+_OBJDIR_ARM = "obj-arm-linux-androideabi" |
+_OBJDIR_X86 = "obj-i386-linux-android" |
+ |
+ |
+def print_usage(): |
+ print >>sys.stderr, string.Template("""\ |
+Usage: $name %s |
+ $name %s APK_PATH |
+ $name %s\ |
+""" % (_CMD_BUILD, _CMD_SIGN, |
+ _CMD_BUILD_SIGN)).substitute({"name": os.path.basename(sys.argv[0])}) |
+ |
+ |
+def _generate_mozconfig(architecture, distribution_mode, build_mode): |
+ if not os.path.exists(_GENERATED_PATH): |
+ os.makedirs(_GENERATED_PATH) |
+ with open(_GENERATED_MOZCONFIG_PATH, "w+") as mozconfig: |
+ mozconfig.write(". \"%s\"\n" % _MOZCONFIG_COMMON_PATH) |
+ if architecture == _ARCH_X86: |
+ mozconfig.write(". \"%s\"\n" % _MOZCONFIG_X86_PATH) |
+ else: |
+ mozconfig.write(". \"%s\"\n" % _MOZCONFIG_ARM_PATH) |
+ if distribution_mode == _DIST_STORE: |
+ mozconfig.write(". \"%s\"\n" % _MOZCONFIG_STORE_PATH) |
+ if build_mode == _BUILD_RELEASE: |
+ mozconfig.write(". \"%s\"\n" % _MOZCONFIG_RELEASE_PATH) |
+ mozconfig.write(". \"%s\"\n" % _MOZCONFIG_CUSTOM_PATH) |
+ return _GENERATED_MOZCONFIG_PATH |
+ |
+ |
+def _build(architecture, distribution_mode, build_mode, sdk_path, ndk_path): |
+ build_environment = os.environ.copy() |
+ build_environment["ANDROID_SDK_PATH"] = sdk_path |
+ build_environment["ANDROID_NDK_PATH"] = ndk_path |
+ build_environment["MOZCONFIG"] = _generate_mozconfig( |
+ architecture, distribution_mode, build_mode) |
+ obj_dir = _OBJDIR_X86 if architecture == _ARCH_X86 else _OBJDIR_ARM |
+ build_environment["OBJDIR"] = obj_dir |
+ subprocess.check_call([os.path.join(_ABB_PATH, "mach"), "clobber"], |
+ env=build_environment) |
+ subprocess.check_call([_MULTI_L10N_PATH, "--cfg", "adblockbrowser-cfg.py"], |
+ env=build_environment) |
+ |
+ dist_path = os.path.join(_ABB_PATH, obj_dir, "dist") |
+ arch_suffix = _ARCH_X86_I386 if architecture == _ARCH_X86 else _ARCH_ARM |
+ [manifest_path] = glob.glob(os.path.join( |
+ dist_path, "fennec-*.multi.android-%s.json" % arch_suffix)) |
+ with open(manifest_path) as manifest_file: |
+ manifest = json.load(manifest_file) |
+ apk_name = "fennec-%s.multi.android-%s-unsigned-unaligned.apk" % ( |
+ manifest["moz_app_version"], architecture) |
+ apk_path = os.path.join(dist_path, apk_name) |
+ if build_mode == _BUILD_RELEASE: |
+ target_apk_name = "adblockbrowser-%s-%s.apk" % ( |
+ manifest["moz_app_version"], architecture) |
+ else: |
+ target_apk_name = "adblockbrowser-%s.%s-%s.apk" % ( |
+ manifest["moz_app_version"], manifest["buildid"], architecture) |
+ target_apk_path = os.path.join(dist_path, target_apk_name) |
+ shutil.copyfile(apk_path, target_apk_path) |
+ |
+ target_manifest_path = re.sub(r".apk$", ".json", target_apk_path) |
+ shutil.copyfile(manifest_path, target_manifest_path) |
+ |
+ return target_apk_path |
+ |
+ |
+def _sign(apk_path, key_store, key_name, sdk_path): |
+ sys.path.append(os.path.join(_ABB_PATH, "python", "mozboot", "mozboot")) |
+ from android import ANDROID_BUILD_TOOLS_VERSION |
+ zipalign_path = os.path.join(sdk_path, "build-tools", |
+ ANDROID_BUILD_TOOLS_VERSION, "zipalign") |
+ temp_apk_path = tempfile.NamedTemporaryFile().name |
+ shutil.copyfile(apk_path, temp_apk_path) |
+ try: |
+ subprocess.check_call(["jarsigner", "-verbose", |
+ "-sigalg", "SHA1withRSA", |
+ "-digestalg", "SHA1", |
+ "-keystore", key_store, |
+ temp_apk_path, key_name]) |
+ os.remove(apk_path) |
+ subprocess.check_call([zipalign_path, "-v", "4", temp_apk_path, |
+ apk_path]) |
+ finally: |
+ os.remove(temp_apk_path) |
+ |
+if __name__ == "__main__": |
+ if len(sys.argv) < 2: |
+ print_usage() |
+ sys.exit(1) |
+ |
+ mode = sys.argv[1] |
+ do_build = mode in (_CMD_BUILD, _CMD_BUILD_SIGN) |
+ do_sign = mode in (_CMD_SIGN, _CMD_BUILD_SIGN) |
+ if not do_build and not do_sign: |
+ print_usage() |
+ sys.exit(2) |
+ |
+ if do_sign: |
+ if len(sys.argv) < 3: |
+ print_usage() |
+ sys.exit(3) |
+ apk_path = sys.argv[2] |
+ |
+ subprocess.check_call([_ENSURE_DEPENDENCIES_PATH]) |
+ import config |
+ |
+ error_msg = "Invalid %s, check config.py. Supported %s: %s" |
+ distribution_mode = config.DISTRIBUTION_MODE |
+ if distribution_mode not in _DIST_MODES: |
+ print >>sys.stderr, error_msg % ( |
+ "distribution mode", "distribution modes", _DIST_MODES) |
+ sys.exit(4) |
+ |
+ build_mode = config.BUILD_MODE |
+ if build_mode not in _BUILD_MODES: |
+ print >>sys.stderr, error_msg % ( |
+ "build mode", "build modes", _BUILD_MODES) |
+ sys.exit(5) |
+ |
+ architecture = config.ARCHITECTURE |
+ if architecture not in _ARCHS: |
+ print >>sys.stderr, error_msg % ( |
+ "architecture", "architectures", _ARCHS) |
+ sys.exit(6) |
+ |
+ if do_build: |
+ apk_path = _build(architecture, distribution_mode, build_mode, |
+ config.ANDROID_SDK_PATH, config.ANDROID_NDK_PATH) |
+ if do_sign: |
+ _sign(apk_path, config.ANDROID_KEYSTORE_PATH, config.ANDROID_KEY_NAME, |
+ config.ANDROID_SDK_PATH) |
+ else: |
+ print apk_path |