android.py 14.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#!/usr/bin/env python

############################################################################
# prepare.py
# Copyright (C) 2016  Belledonne Communications, Grenoble France
#
############################################################################
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
############################################################################

import fnmatch
import os
import re
import sys
from distutils.spawn import find_executable
from logging import error, warning, info
from subprocess import Popen
sys.dont_write_bytecode = True
sys.path.insert(0, 'submodules/cmake-builder')
try:
    import prepare
except Exception as e:
    error(
        "Could not find prepare module: {}, probably missing submodules/cmake-builder? Try running:\n"
        "git submodule sync && git submodule update --init --recursive".format(e))
    exit(1)



class AndroidTarget(prepare.Target):

    def __init__(self, arch):
47
        prepare.Target.__init__(self, 'android-' + arch, 'android')
48 49 50
        current_path = os.path.dirname(os.path.realpath(__file__))
        self.config_file = 'configs/config-android.cmake'
        self.toolchain_file = 'toolchains/toolchain-android-' + arch + '.cmake'
51
        self.output = 'android/liblinphone-sdk/android-' + arch
52
        self.external_source_path = os.path.join(current_path, 'submodules')
Sylvain Berfini's avatar
Sylvain Berfini committed
53 54 55 56
        external_builders_path = os.path.join(current_path, 'cmake_builder')
        self.additional_args = [
            "-DLINPHONE_BUILDER_EXTERNAL_BUILDERS_PATH=" + external_builders_path
        ]
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97


class AndroidArmTarget(AndroidTarget):

    def __init__(self):
        AndroidTarget.__init__(self, 'arm')
        self.additional_args += ['-DENABLE_VIDEO=NO']


class AndroidArmv7Target(AndroidTarget):

    def __init__(self):
        AndroidTarget.__init__(self, 'armv7')


class AndroidArm64Target(AndroidTarget):

    def __init__(self):
        AndroidTarget.__init__(self, 'arm64')


class AndroidX86Target(AndroidTarget):

    def __init__(self):
        AndroidTarget.__init__(self, 'x86')


class AndroidX86_64Target(AndroidTarget):

    def __init__(self):
        AndroidTarget.__init__(self,'x86_64')


android_targets = {
    'arm': AndroidArmTarget(),
    'armv7': AndroidArmv7Target(),
    'arm64': AndroidArm64Target(),
    'x86': AndroidX86Target(),
    'x86_64': AndroidX86_64Target()
}

Sylvain Berfini's avatar
Sylvain Berfini committed
98
android_virtual_targets = {
99
    'all': ['armv7', 'arm64', 'x86', 'x86_64']
Sylvain Berfini's avatar
Sylvain Berfini committed
100 101
}

102 103 104
class AndroidPreparator(prepare.Preparator):

    def __init__(self, targets=android_targets):
Sylvain Berfini's avatar
Sylvain Berfini committed
105 106 107
        prepare.Preparator.__init__(self, targets, default_targets=['armv7', 'arm64'], virtual_targets=android_virtual_targets)
        self.min_supported_ndk = 16
        self.max_supported_ndk = 17
108
        self.unsupported_ndk_version = None
Sylvain Berfini's avatar
Sylvain Berfini committed
109
        self.min_cmake_version = "3.10"
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        self.release_with_debug_info = True
        self.veryclean = True
        self.show_gpl_disclaimer = True
        self.argparser.add_argument('-ac', '--all-codecs', help="Enable all codecs, including the non-free ones", action='store_true')

    def parse_args(self):
        prepare.Preparator.parse_args(self)

        if self.args.all_codecs:
            self.additional_args += ["-DENABLE_GPL_THIRD_PARTIES=YES"]
            self.additional_args += ["-DENABLE_NON_FREE_CODECS=YES"]
            self.additional_args += ["-DENABLE_AMRNB=YES"]
            self.additional_args += ["-DENABLE_AMRWB=YES"]
            self.additional_args += ["-DENABLE_BV16=YES"]
            self.additional_args += ["-DENABLE_CODEC2=YES"]
            self.additional_args += ["-DENABLE_G729=YES"]
            self.additional_args += ["-DENABLE_GSM=YES"]
            self.additional_args += ["-DENABLE_ILBC=YES"]
            self.additional_args += ["-DENABLE_ISAC=YES"]
            self.additional_args += ["-DENABLE_OPUS=YES"]
            self.additional_args += ["-DENABLE_SILK=YES"]
            self.additional_args += ["-DENABLE_SPEEX=YES"]
Sylvain Berfini's avatar
Sylvain Berfini committed
132 133 134 135
            self.additional_args += ["-DENABLE_FFMPEG=YES"]
            self.additional_args += ["-DENABLE_H263=YES"]
            self.additional_args += ["-DENABLE_H263P=YES"]
            self.additional_args += ["-DENABLE_MPEG4=YES"]
136 137
            self.additional_args += ["-DENABLE_OPENH264=YES"]
            self.additional_args += ["-DENABLE_VPX=YES"]
Sylvain Berfini's avatar
Sylvain Berfini committed
138
            # self.additional_args += ["-DENABLE_X264=YES"] # Do not activate x264 because it has text relocation issues
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

    def list_feature_target(self):
        return android_targets['armv7']

    def check_ndk_version(self):
        retval = True
        ndk_build = find_executable('ndk-build')
        ndk_path = os.path.dirname(ndk_build)
        # NDK prior to r11 had a RELEASE.TXT file holding the version number
        release_file = os.path.join(ndk_path, 'RELEASE.TXT')
        if os.path.isfile(release_file):
            version = open(release_file).read().strip()
            res = re.match('^r(\d+)(.*)$', version)
            version = int(res.group(1))
            retval = False
        else:
            # Hack to find the NDK version since the RELEASE.TXT file is no longer there
            python_config_files = []
            for root, dirnames, filenames in os.walk(ndk_path):
                for filename in fnmatch.filter(filenames, 'python-config'):
                    python_config_files.append(os.path.join(root, filename))
            if len(python_config_files) > 0:
                version = open(python_config_files[0]).readlines()[0]
                res = re.match('^.*/(aosp-)?ndk-r(\d+).*$', version)
Sylvain Berfini's avatar
Sylvain Berfini committed
163 164 165 166 167 168 169 170 171 172 173 174 175
                if res is not None: # Will be if NDK < 16
                    version = int(res.group(2))
                    retval = False
                else:
                    release_file = os.path.join(ndk_path, 'source.properties') # Since NDK 16
                    if os.path.isfile(release_file):
                        version = open(release_file).read().strip()
                        res = re.findall(r'(?:(\d+))', version)
                        version = int(res[0])
                        retval = False
                    else:
                        error("Could not get Android NDK version!")
                        sys.exit(-1)
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
            else:
                error("Could not get Android NDK version!")
                sys.exit(-1)
        if retval == False and (version < self.min_supported_ndk or version > self.max_supported_ndk):
            self.unsupported_ndk_version = version
            retval = True
        return retval

    def check_environment(self):
        ret = 0
        ret_ndk = not self.check_is_installed('ndk-build', 'Android NDK r{}'.format(self.max_supported_ndk))
        if not ret_ndk:
            ret_ndk = self.check_ndk_version()
        ret |= ret_ndk
        ret |= prepare.Preparator.check_environment(self)
        return ret

    def show_environment_errors(self):
        if self.unsupported_ndk_version is not None:
            error("Unsupported Android NDK r{}. Please install version r{}.".format(self.unsupported_ndk_version, self.max_supported_ndk))
        else:
            prepare.Preparator.show_environment_errors(self)

    def clean(self):
        prepare.Preparator.clean(self)
201 202
        if os.path.isfile('Makefile.android'):
            os.remove('Makefile.android')
203 204
        if os.path.isdir('android') and not os.listdir('android'):
            os.rmdir('android')
205 206 207 208
        if os.path.isdir('liblinphone-sdk') and not os.listdir('liblinphone-sdk'):
            os.rmdir('liblinphone-sdk')

    def prepare(self):
209
        self.download_gradle()
210 211
        prepare.Preparator.prepare(self)

212 213 214
    def download_gradle(self):
        os.system('./gradlew')

215 216 217 218 219
    def generate_makefile(self, generator, project_file=''):
        platforms = self.args.target
        arch_targets = ""
        for arch in platforms:
            arch_targets += """
220
android-{arch}: android-{arch}-build
221

222
android-{arch}-build:
223
\t{generator} android/android-{arch}/cmake
224 225 226
\t@echo "Done"
""".format(arch=arch, generator=generator)
        makefile = """
227
android-archs={archs}
228 229 230 231 232
TOPDIR=$(shell pwd)

.PHONY: all
.NOTPARALLEL: all

233
all: generate-android-sdk
234

235
android-build: $(addprefix android-, $(addsuffix -build, $(android-archs)))
236

Sylvain Berfini's avatar
Sylvain Berfini committed
237 238 239 240 241
clean: java-clean

java-clean:
\t./gradlew clean

242
android-copy-libs:
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
\trm -rf android/liblinphone-sdk/res
\trm -rf android/libs-debug/armeabi
\trm -rf android/libs/armeabi
\tif test -d "android/liblinphone-sdk/android-arm"; then \\
\t\tmkdir -p android/libs-debug/armeabi && \\
\t\tcp -f android/liblinphone-sdk/android-arm/lib/lib*.so android/libs-debug/armeabi && \\
\t\tcp -f android/liblinphone-sdk/android-arm/lib/mediastreamer/plugins/*.so android/libs-debug/armeabi && \\
\t\tmkdir -p android/libs/armeabi && \\
\t\tcp -f android/liblinphone-sdk/android-arm/lib/lib*.so android/libs/armeabi && \\
\t\tcp -f android/liblinphone-sdk/android-arm/lib/mediastreamer/plugins/*.so android/libs/armeabi && \\
\t\tcp android/liblinphone-sdk/android-arm/share/linphonecs/LinphoneWrapper.cs Xamarin/Xamarin/Xamarin/ && \\
\t\tsh android/android-arm/strip.sh android/libs/armeabi/*.so; \\
\tfi
\tif test -f "android/liblinphone-sdk/android-arm/lib/wrap.sh"; then \\
\t\tmkdir -p android/liblinphone-sdk/res/lib/armeabi && \\
\t\tcp -f android/liblinphone-sdk/android-arm/lib/wrap.sh android/liblinphone-sdk/res/lib/armeabi; \\
\tfi
\trm -rf android/libs-debug/armeabi-v7a
\trm -rf android/libs/armeabi-v7a
262
\tif test -d "android/liblinphone-sdk/android-armv7"; then \\
263 264 265 266 267
\t\tmkdir -p android/libs-debug/armeabi-v7a && \\
\t\tcp -f android/liblinphone-sdk/android-armv7/lib/lib*.so android/libs-debug/armeabi-v7a && \\
\t\tcp -f android/liblinphone-sdk/android-armv7/lib/mediastreamer/plugins/*.so android/libs-debug/armeabi-v7a && \\
\t\tmkdir -p android/libs/armeabi-v7a && \\
\t\tcp -f android/liblinphone-sdk/android-armv7/lib/lib*.so android/libs/armeabi-v7a && \\
268
\t\tcp -f android/liblinphone-sdk/android-armv7/lib/mediastreamer/plugins/*.so android/libs/armeabi-v7a && \\
269 270
\t\tcp android/liblinphone-sdk/android-armv7/share/linphonecs/LinphoneWrapper.cs Xamarin/Xamarin/Xamarin/ && \\
\t\tsh android/android-armv7/strip.sh android/libs/armeabi-v7a/*.so; \\
271
\tfi
272 273 274 275 276 277
\tif test -f "android/liblinphone-sdk/android-armv7/lib/wrap.sh"; then \\
\t\tmkdir -p android/liblinphone-sdk/res/lib/armeabi-v7a && \\
\t\tcp -f android/liblinphone-sdk/android-armv7/lib/wrap.sh android/liblinphone-sdk/res/lib/armeabi-v7a; \\
\tfi
\trm -rf android/libs-debug/arm64-v8a
\trm -rf android/libs/arm64-v8a
278
\tif test -d "android/liblinphone-sdk/android-arm64"; then \\
279 280 281 282 283 284 285 286 287 288 289 290
\t\tmkdir -p android/libs-debug/arm64-v8a && \\
\t\tcp -f android/liblinphone-sdk/android-arm64/lib/lib*.so android/libs-debug/arm64-v8a && \\
\t\tcp -f android/liblinphone-sdk/android-arm64/lib/mediastreamer/plugins/*.so android/libs-debug/arm64-v8a && \\
\t\tmkdir -p android/libs/arm64-v8a && \\
\t\tcp -f android/liblinphone-sdk/android-arm64/lib/lib*.so android/libs/arm64-v8a && \\
\t\tcp -f android/liblinphone-sdk/android-arm64/lib/mediastreamer/plugins/*.so android/libs/arm64-v8a && \\
\t\tcp android/liblinphone-sdk/android-arm64/share/linphonecs/LinphoneWrapper.cs Xamarin/Xamarin/Xamarin/ && \\
\t\tsh android/android-arm64/strip.sh android/libs/arm64-v8a/*.so; \\
\tfi
\tif test -f "android/liblinphone-sdk/android-arm64/lib/wrap.sh"; then \\
\t\tmkdir -p android/liblinphone-sdk/res/lib/arm64-v8a && \\
\t\tcp -f android/liblinphone-sdk/android-arm64/lib/wrap.sh android/liblinphone-sdk/res/lib/arm64-v8a; \\
291
\tfi
292 293
\trm -rf android/libs-debug/x86
\trm -rf android/libs/x86
294
\tif test -d "android/liblinphone-sdk/android-x86"; then \\
295 296 297 298 299 300 301 302 303 304 305 306
\t\tmkdir -p android/libs-debug/x86 && \\
\t\tcp -f android/liblinphone-sdk/android-x86/lib/lib*.so android/libs-debug/x86 && \\
\t\tcp -f android/liblinphone-sdk/android-x86/lib/mediastreamer/plugins/*.so android/libs-debug/x86 && \\
\t\tmkdir -p android/libs/x86 && \\
\t\tcp -f android/liblinphone-sdk/android-x86/lib/lib*.so android/libs/x86 && \\
\t\tcp -f android/liblinphone-sdk/android-x86/lib/mediastreamer/plugins/*.so android/libs/x86 && \\
\t\tcp android/liblinphone-sdk/android-x86/share/linphonecs/LinphoneWrapper.cs Xamarin/Xamarin/Xamarin/ && \\
\t\tsh android/android-x86/strip.sh android/libs/x86/*.so; \\
\tfi
\tif test -f "android/liblinphone-sdk/android-x86/lib/wrap.sh"; then \\
\t\tmkdir -p android/liblinphone-sdk/res/lib/x86 && \\
\t\tcp -f android/liblinphone-sdk/android-x86/lib/wrap.sh android/liblinphone-sdk/res/lib/x86; \\
307 308
\tfi

Sylvain Berfini's avatar
Sylvain Berfini committed
309 310
generate-android-sdk: clean android-build android-copy-libs create-jar

311 312
create-jar:
\t./gradlew assembleRelease
313
\tcp ./bin/outputs/aar/liblinphone-sdk-*.aar Xamarin/Xamarin/Liblinphone/liblinphone-sdk.aar
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

{arch_targets}

help-prepare-options:
\t@echo "prepare.py was previously executed with the following options:"
\t@echo "   {options}"

help: help-prepare-options
\t@echo ""
\t@echo "(please read the README.md file first)"
\t@echo ""
\t@echo "Available architectures: {archs}"
\t@echo ""
\t@echo "Available targets:"
\t@echo ""
\t@echo "   * all or generate-apk: builds all architectures and creates the linphone application APK"
\t@echo "   * generate-sdk: builds all architectures and creates the liblinphone SDK"
\t@echo "   * install: install the linphone application APK (run this only after generate-apk)"
\t@echo "   * uninstall: uninstall the linphone application"
\t@echo ""
""".format(archs=' '.join(platforms), arch_opts='|'.join(platforms),
           first_arch=platforms[0], options=' '.join(sys.argv),
           arch_targets=arch_targets, generator=generator)
337
        f = open('Makefile.android', 'w')
338 339 340 341 342 343 344 345 346 347 348 349 350
        f.write(makefile)
        f.close()

def main():
    preparator = AndroidPreparator()
    preparator.parse_args()
    if preparator.check_environment() != 0:
        preparator.show_environment_errors()
        return 1
    return preparator.run()

if __name__ == "__main__":
    sys.exit(main())