OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 import glob |
| 4 import json |
| 5 import os |
| 6 import re |
| 7 import shutil |
| 8 import string |
| 9 import subprocess |
| 10 import sys |
| 11 import tempfile |
| 12 |
| 13 _BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 14 _ENSURE_DEPENDENCIES_PATH = os.path.join(_BASE_DIR, "ensure_dependencies.py") |
| 15 _GENERATED_PATH = os.path.join(_BASE_DIR, "generated") |
| 16 _GENERATED_MOZCONFIG_PATH = os.path.join(_GENERATED_PATH, "mozconfig") |
| 17 _MOZCONFIG_COMMON_PATH = os.path.join(_BASE_DIR, "mozconfig-common") |
| 18 _MOZCONFIG_ARM_PATH = os.path.join(_BASE_DIR, "mozconfig-arm") |
| 19 _MOZCONFIG_X86_PATH = os.path.join(_BASE_DIR, "mozconfig-x86") |
| 20 _MOZCONFIG_STORE_PATH = os.path.join(_BASE_DIR, "mozconfig-store") |
| 21 _MOZCONFIG_RELEASE_PATH = os.path.join(_BASE_DIR, "mozconfig-release") |
| 22 _MOZCONFIG_CUSTOM_PATH = os.path.join(_BASE_DIR, "mozconfig-custom") |
| 23 _MULTI_L10N_PATH = os.path.join(_BASE_DIR, "mozharness", "scripts", |
| 24 "multil10n.py") |
| 25 _ABB_PATH = os.path.normpath(os.path.join(_BASE_DIR, "..")) |
| 26 _CMD_BUILD = "build" |
| 27 _CMD_SIGN = "sign" |
| 28 _CMD_BUILD_SIGN = "build-and-sign" |
| 29 _ARCH_ARM = "arm" |
| 30 _ARCH_X86 = "x86" |
| 31 _ARCH_X86_I386 = "i386" |
| 32 _ARCHS = (_ARCH_ARM, _ARCH_X86) |
| 33 _DIST_STANDALONE = "standalone" |
| 34 _DIST_STORE = "store" |
| 35 _DIST_MODES = (_DIST_STANDALONE, _DIST_STORE) |
| 36 _BUILD_DEVBUILD = "devbuild" |
| 37 _BUILD_RELEASE = "release" |
| 38 _BUILD_MODES = (_BUILD_DEVBUILD, _BUILD_RELEASE) |
| 39 _OBJDIR_ARM = "obj-arm-linux-androideabi" |
| 40 _OBJDIR_X86 = "obj-i386-linux-android" |
| 41 |
| 42 |
| 43 def print_usage(): |
| 44 print >>sys.stderr, string.Template("""\ |
| 45 Usage: $name %s |
| 46 $name %s APK_PATH |
| 47 $name %s\ |
| 48 """ % (_CMD_BUILD, _CMD_SIGN, |
| 49 _CMD_BUILD_SIGN)).substitute({"name": os.path.basename(sys.argv[0])}) |
| 50 |
| 51 |
| 52 def _generate_mozconfig(architecture, distribution_mode, build_mode): |
| 53 if not os.path.exists(_GENERATED_PATH): |
| 54 os.makedirs(_GENERATED_PATH) |
| 55 with open(_GENERATED_MOZCONFIG_PATH, "w+") as mozconfig: |
| 56 mozconfig.write(". \"%s\"\n" % _MOZCONFIG_COMMON_PATH) |
| 57 if architecture == _ARCH_X86: |
| 58 mozconfig.write(". \"%s\"\n" % _MOZCONFIG_X86_PATH) |
| 59 else: |
| 60 mozconfig.write(". \"%s\"\n" % _MOZCONFIG_ARM_PATH) |
| 61 if distribution_mode == _DIST_STORE: |
| 62 mozconfig.write(". \"%s\"\n" % _MOZCONFIG_STORE_PATH) |
| 63 if build_mode == _BUILD_RELEASE: |
| 64 mozconfig.write(". \"%s\"\n" % _MOZCONFIG_RELEASE_PATH) |
| 65 mozconfig.write(". \"%s\"\n" % _MOZCONFIG_CUSTOM_PATH) |
| 66 return _GENERATED_MOZCONFIG_PATH |
| 67 |
| 68 |
| 69 def _build(architecture, distribution_mode, build_mode, sdk_path, ndk_path): |
| 70 build_environment = os.environ.copy() |
| 71 build_environment["ANDROID_SDK_PATH"] = sdk_path |
| 72 build_environment["ANDROID_NDK_PATH"] = ndk_path |
| 73 build_environment["MOZCONFIG"] = _generate_mozconfig( |
| 74 architecture, distribution_mode, build_mode) |
| 75 obj_dir = _OBJDIR_X86 if architecture == _ARCH_X86 else _OBJDIR_ARM |
| 76 build_environment["OBJDIR"] = obj_dir |
| 77 subprocess.check_call([os.path.join(_ABB_PATH, "mach"), "clobber"], |
| 78 env=build_environment) |
| 79 subprocess.check_call([_MULTI_L10N_PATH, "--cfg", "adblockbrowser-cfg.py"], |
| 80 env=build_environment) |
| 81 |
| 82 dist_path = os.path.join(_ABB_PATH, obj_dir, "dist") |
| 83 arch_suffix = _ARCH_X86_I386 if architecture == _ARCH_X86 else _ARCH_ARM |
| 84 [manifest_path] = glob.glob(os.path.join( |
| 85 dist_path, "fennec-*.multi.android-%s.json" % arch_suffix)) |
| 86 with open(manifest_path) as manifest_file: |
| 87 manifest = json.load(manifest_file) |
| 88 apk_name = "fennec-%s.multi.android-%s-unsigned-unaligned.apk" % ( |
| 89 manifest["moz_app_version"], architecture) |
| 90 apk_path = os.path.join(dist_path, apk_name) |
| 91 if build_mode == _BUILD_RELEASE: |
| 92 target_apk_name = "adblockbrowser-%s-%s.apk" % ( |
| 93 manifest["moz_app_version"], architecture) |
| 94 else: |
| 95 target_apk_name = "adblockbrowser-%s.%s-%s.apk" % ( |
| 96 manifest["moz_app_version"], manifest["buildid"], architecture) |
| 97 target_apk_path = os.path.join(dist_path, target_apk_name) |
| 98 shutil.copyfile(apk_path, target_apk_path) |
| 99 |
| 100 target_manifest_path = re.sub(r".apk$", ".json", target_apk_path) |
| 101 shutil.copyfile(manifest_path, target_manifest_path) |
| 102 |
| 103 return target_apk_path |
| 104 |
| 105 |
| 106 def _sign(apk_path, key_store, key_name, sdk_path): |
| 107 sys.path.append(os.path.join(_ABB_PATH, "python", "mozboot", "mozboot")) |
| 108 from android import ANDROID_BUILD_TOOLS_VERSION |
| 109 zipalign_path = os.path.join(sdk_path, "build-tools", |
| 110 ANDROID_BUILD_TOOLS_VERSION, "zipalign") |
| 111 temp_apk_path = tempfile.NamedTemporaryFile().name |
| 112 shutil.copyfile(apk_path, temp_apk_path) |
| 113 try: |
| 114 subprocess.check_call(["jarsigner", "-verbose", |
| 115 "-sigalg", "SHA1withRSA", |
| 116 "-digestalg", "SHA1", |
| 117 "-keystore", key_store, |
| 118 temp_apk_path, key_name]) |
| 119 os.remove(apk_path) |
| 120 subprocess.check_call([zipalign_path, "-v", "4", temp_apk_path, |
| 121 apk_path]) |
| 122 finally: |
| 123 os.remove(temp_apk_path) |
| 124 |
| 125 if __name__ == "__main__": |
| 126 if len(sys.argv) < 2: |
| 127 print_usage() |
| 128 sys.exit(1) |
| 129 |
| 130 mode = sys.argv[1] |
| 131 do_build = mode in (_CMD_BUILD, _CMD_BUILD_SIGN) |
| 132 do_sign = mode in (_CMD_SIGN, _CMD_BUILD_SIGN) |
| 133 if not do_build and not do_sign: |
| 134 print_usage() |
| 135 sys.exit(2) |
| 136 |
| 137 if do_sign: |
| 138 if len(sys.argv) < 3: |
| 139 print_usage() |
| 140 sys.exit(3) |
| 141 apk_path = sys.argv[2] |
| 142 |
| 143 subprocess.check_call([_ENSURE_DEPENDENCIES_PATH]) |
| 144 import config |
| 145 |
| 146 error_msg = "Invalid %s, check config.py. Supported %s: %s" |
| 147 distribution_mode = config.DISTRIBUTION_MODE |
| 148 if distribution_mode not in _DIST_MODES: |
| 149 print >>sys.stderr, error_msg % ( |
| 150 "distribution mode", "distribution modes", _DIST_MODES) |
| 151 sys.exit(4) |
| 152 |
| 153 build_mode = config.BUILD_MODE |
| 154 if build_mode not in _BUILD_MODES: |
| 155 print >>sys.stderr, error_msg % ( |
| 156 "build mode", "build modes", _BUILD_MODES) |
| 157 sys.exit(5) |
| 158 |
| 159 architecture = config.ARCHITECTURE |
| 160 if architecture not in _ARCHS: |
| 161 print >>sys.stderr, error_msg % ( |
| 162 "architecture", "architectures", _ARCHS
) |
| 163 sys.exit(6) |
| 164 |
| 165 if do_build: |
| 166 apk_path = _build(architecture, distribution_mode, build_mode, |
| 167 config.ANDROID_SDK_PATH, config.ANDROID_NDK_PATH) |
| 168 if do_sign: |
| 169 _sign(apk_path, config.ANDROID_KEYSTORE_PATH, config.ANDROID_KEY_NAME, |
| 170 config.ANDROID_SDK_PATH) |
| 171 else: |
| 172 print apk_path |
OLD | NEW |