prepare.py 15 KB
Newer Older
1 2 3
#!/usr/bin/env python

#
DanmeiChen's avatar
DanmeiChen committed
4
# Copyright (c) 2010-2019 Belledonne Communications SARL.
5
#
DanmeiChen's avatar
DanmeiChen committed
6 7 8 9 10 11
# This file is part of linphone-iphone 
#
# 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 3 of the License, or
# (at your option) any later version.
12 13 14 15 16 17 18
#
# 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
DanmeiChen's avatar
DanmeiChen committed
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20 21 22
#

import os
23
import re
24
import shutil
25
import sys
26
from distutils.spawn import find_executable
27
from logging import error, warning, info
28
from subprocess import Popen, PIPE
29
sys.dont_write_bytecode = True
30
sys.path.insert(0, 'submodules/cmake-builder')
31 32
try:
    import prepare
33
except Exception as e:
34
    error(
35 36
        "Could not find prepare module: {}, probably missing submodules/cmake-builder? Try running:\n"
        "git submodule sync && git submodule update --init --recursive".format(e))
37
    exit(1)
38 39


40

41
class IOSTarget(prepare.Target):
42

43 44 45 46 47 48
    def __init__(self, arch):
        prepare.Target.__init__(self, 'ios-' + arch)
        current_path = os.path.dirname(os.path.realpath(__file__))
        self.config_file = 'configs/config-ios-' + arch + '.cmake'
        self.toolchain_file = 'toolchains/toolchain-ios-' + arch + '.cmake'
        self.output = 'liblinphone-sdk/' + arch + '-apple-darwin.ios'
49 50 51 52 53
        self.external_source_path = os.path.join(current_path, 'submodules')
        external_builders_path = os.path.join(current_path, 'cmake_builder')
        self.additional_args = [
            "-DLINPHONE_BUILDER_EXTERNAL_BUILDERS_PATH=" + external_builders_path
        ]
54 55 56


class IOSi386Target(IOSTarget):
57

58 59
    def __init__(self):
        IOSTarget.__init__(self, 'i386')
60

61 62

class IOSx8664Target(IOSTarget):
63

64 65
    def __init__(self):
        IOSTarget.__init__(self, 'x86_64')
66

67 68

class IOSarmv7Target(IOSTarget):
69

70 71
    def __init__(self):
        IOSTarget.__init__(self, 'armv7')
72

73 74

class IOSarm64Target(IOSTarget):
75

76 77
    def __init__(self):
        IOSTarget.__init__(self, 'arm64')
78 79


80 81

ios_targets = {
82 83 84 85 86
    'i386': IOSi386Target(),
    'x86_64': IOSx8664Target(),
    'armv7': IOSarmv7Target(),
    'arm64': IOSarm64Target()
}
87

88 89
ios_virtual_targets = {
    'devices': ['armv7', 'arm64'],
90 91
    'simulators': ['i386', 'x86_64'],
    'all': ['i386', 'x86_64', 'armv7', 'arm64']
92 93
}

94 95
class IOSPreparator(prepare.Preparator):

96
    def __init__(self, targets=ios_targets, virtual_targets=ios_virtual_targets):
97
        prepare.Preparator.__init__(self, targets, default_targets=['armv7', 'arm64', 'x86_64'], virtual_targets=virtual_targets)
98 99 100 101 102 103 104 105 106 107
        self.veryclean = True
        self.show_gpl_disclaimer = True
        self.argparser.add_argument('-ac', '--all-codecs', help="Enable all codecs, including the non-free ones. Final application must comply with their respective license (see README.md).", action='store_true')

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

        self.additional_args += ["-DLINPHONE_IOS_DEPLOYMENT_TARGET=" + self.extract_deployment_target()]
        self.additional_args += ["-DLINPHONE_BUILDER_DUMMY_LIBRARIES=" + ' '.join(self.extract_libs_list())]
        if self.args.all_codecs:
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
            self.additional_args += ["-DENABLE_GPL_THIRD_PARTIES=ON"]
            self.additional_args += ["-DENABLE_NON_FREE_CODECS=ON"]
            self.additional_args += ["-DENABLE_AMRNB=ON"]
            self.additional_args += ["-DENABLE_AMRWB=ON"]
            self.additional_args += ["-DENABLE_BV16=ON"]
            self.additional_args += ["-DENABLE_G729=ON"]
            self.additional_args += ["-DENABLE_GSM=ON"]
            self.additional_args += ["-DENABLE_ILBC=ON"]
            self.additional_args += ["-DENABLE_ISAC=ON"]
            self.additional_args += ["-DENABLE_OPUS=ON"]
            self.additional_args += ["-DENABLE_SILK=ON"]
            self.additional_args += ["-DENABLE_SPEEX=ON"]
            self.additional_args += ["-DENABLE_FFMPEG=ON"]
            self.additional_args += ["-DENABLE_H263=ON"]
            self.additional_args += ["-DENABLE_H263P=ON"]
            self.additional_args += ["-DENABLE_MPEG4=ON"]
            self.additional_args += ["-DENABLE_OPENH264=ON"]
            self.additional_args += ["-DENABLE_VPX=ON"]
            self.additional_args += ["-DENABLE_X264=ON"]
127
            self.additional_args += ["-DENABLE_CODEC2=ON"]
128

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    def clean(self):
        prepare.Preparator.clean(self)
        if os.path.isfile('Makefile'):
            os.remove('Makefile')
        if os.path.isdir('WORK') and not os.listdir('WORK'):
            os.rmdir('WORK')
        if os.path.isdir('liblinphone-sdk'):
            l = os.listdir('liblinphone-sdk')
            if len(l) == 1 and l[0] == 'apple-darwin':
                shutil.rmtree('liblinphone-sdk', ignore_errors=False)

    def extract_from_xcode_project_with_regex(self, regex):
        l = []
        f = open('linphone.xcodeproj/project.pbxproj', 'r')
        lines = f.readlines()
        f.close()
        for line in lines:
            m = regex.search(line)
            if m is not None:
                l += [m.group(1)]
        return list(set(l))

    def extract_deployment_target(self):
        regex = re.compile("IPHONEOS_DEPLOYMENT_TARGET = (.*);")
        return self.extract_from_xcode_project_with_regex(regex)[0]

    def extract_libs_list(self):
156
        regex = re.compile("name = ([A-Za-z0-9\-_]+)\.framework; path = \"liblinphone-sdk/apple-darwin/Frameworks/")
157 158 159 160 161 162 163 164 165 166 167
        return self.extract_from_xcode_project_with_regex(regex)

    def detect_package_manager(self):
        if find_executable("brew"):
            return "brew"
        elif find_executable("port"):
            return "sudo port"
        else:
            error("No package manager found. Please README or install brew using:\n\truby -e \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)\"")
            return "brew"

168
    def check_environment(self):
169
        reterr = 0
170
        reterr |= prepare.Preparator.check_environment(self)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
        package_manager_info = {"brew-pkg-config": "pkg-config",
                                "sudo port-pkg-config": "pkgconfig",
                                "brew-binary-path": "/usr/local/bin/",
                                "sudo port-binary-path": "/opt/local/bin/"
                                }

        for prog in ["autoconf", "automake", "doxygen", "java", "nasm", "cmake", "wget", "yasm", "optipng"]:
            reterr |= not self.check_is_installed(prog, prog)

        reterr |= not self.check_is_installed("pkg-config", package_manager_info[self.detect_package_manager() + "-pkg-config"])
        reterr |= not self.check_is_installed("ginstall", "coreutils")
        reterr |= not self.check_is_installed("intltoolize", "intltool")
        reterr |= not self.check_is_installed("convert", "imagemagick")

        if find_executable("nasm"):
            nasm_output = Popen("nasm -f elf32".split(" "), stderr=PIPE, stdout=PIPE).stderr.read()
            if "fatal: unrecognised output format" in nasm_output:
188
                error("Invalid version of nasm detected. Please make sure that you are NOT using Apple's binary here")
189
                self.missing_dependencies["nasm"] = "nasm"
190
                reterr = 1
191

192
        if self.check_is_installed("libtoolize", "libtoolize", warn=False):
193 194
            if not self.check_is_installed("glibtoolize", "libtool"):
                reterr = 1
195 196 197 198
                glibtoolize_path = find_executable("glibtoolize")
                if glibtoolize_path is not None:
                    msg = "Please do a symbolic link from glibtoolize to libtoolize:\n\tln -s {} ${}"
                    error(msg.format(glibtoolize_path, glibtoolize_path.replace("glibtoolize", "libtoolize")))
199 200 201 202 203 204 205 206

        devnull = open(os.devnull, 'wb')
        # just ensure that JDK is installed - if not, it will automatically display a popup to user
        p = Popen("java -version".split(" "), stderr=devnull, stdout=devnull)
        p.wait()
        if p.returncode != 0:
            error("Please install Java JDK (not just JRE).")
            reterr = 1
207

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
        p = Popen("xcrun --sdk iphoneos --show-sdk-path".split(" "), stdout=devnull, stderr=devnull)
        p.wait()
        if p.returncode != 0:
            error("iOS SDK not found, please install Xcode from AppStore or equivalent.")
            reterr = 1
        else:
            xcode_version = int(
                Popen("xcodebuild -version".split(" "), stdout=PIPE).stdout.read().split("\n")[0].split(" ")[1].split(".")[0])
            if xcode_version < 7:
                if not find_executable("strings"):
                    sdk_strings_path = Popen("xcrun --find strings".split(" "), stdout=PIPE).stdout.read().split("\n")[0]
                    error("strings binary missing, please run:\n\tsudo ln -s {} {}".format(sdk_strings_path, package_manager_info[detect_package_manager() + "-binary-path"]))
                    reterr = 1
        return reterr

    def show_missing_dependencies(self):
        if self.missing_dependencies:
            error("The following binaries are missing: {}. Please install them using:\n\t{} install {}".format(
                " ".join(self.missing_dependencies.keys()),
                self.detect_package_manager(),
                " ".join(self.missing_dependencies.values())))

    def install_git_hook(self):
        git_hook_path = ".git{sep}hooks{sep}pre-commit".format(sep=os.sep)
        if os.path.isdir(".git{sep}hooks".format(sep=os.sep)) and not os.path.isfile(git_hook_path):
            info("Installing Git pre-commit hook")
            shutil.copyfile(".git-pre-commit", git_hook_path)
            os.chmod(git_hook_path, 0755)

237
    def generate_makefile(self, generator, project_file=''):
238 239 240 241
        platforms = self.args.target
        arch_targets = ""
        for arch in platforms:
            arch_targets += """
242
{arch}: {arch}-build
243

244
{arch}-build:
245
\t{generator} WORK/ios-{arch}/cmake/{project_file}
246
\t@echo "Done"
247
""".format(arch=arch, generator=generator, project_file=project_file)
248 249
        multiarch = ""
        for arch in platforms[1:]:
250 251 252 253 254 255
            multiarch += """ \\
\t\t\tif test -f "$${arch}_path/$$framework_name"; then \\
\t\t\t\tall_paths=`echo $$all_paths $${arch}_path/$$framework_name`; \\
\t\t\t\tall_archs="$$all_archs,{arch}" ; \\
\t\t\telse \\
\t\t\t\techo "WARNING: archive `basename $$archive` exists in {first_arch} tree but does not exists in {arch} tree: $${arch}_path."; \\
256
\t\t\tfi; \\
257
""".format(first_arch=platforms[0], arch=arch)
258
        makefile = """
259
archs={archs}
260
LINPHONE_IPHONE_VERSION=$(shell git describe --always)
261 262

.PHONY: all
263
.SILENT: sdk
264
all: build
265

266
sdk:
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
\tarchives=`find liblinphone-sdk/{first_arch}-apple-darwin.ios -name '*.framework'` && \\
\trm -rf liblinphone-sdk/apple-darwin && \\
\tmkdir -p liblinphone-sdk/apple-darwin && \\
\tcp -rf liblinphone-sdk/{first_arch}-apple-darwin.ios/share liblinphone-sdk/apple-darwin/. && \\
\tcp -rf liblinphone-sdk/{first_arch}-apple-darwin.ios/lib liblinphone-sdk/apple-darwin/. && \\
\tcp -rf liblinphone-sdk/{first_arch}-apple-darwin.ios/include liblinphone-sdk/apple-darwin/. && \\
\tcp -rf liblinphone-sdk/{first_arch}-apple-darwin.ios/Frameworks liblinphone-sdk/apple-darwin/. && \\
\tfor archive in $$archives ; do \\
\t\tarmv7_path=`echo $$archive | sed -e "s/{first_arch}/armv7/"`; \\
\t\tarm64_path=`echo $$archive | sed -e "s/{first_arch}/arm64/"`; \\
\t\ti386_path=`echo $$archive | sed -e "s/{first_arch}/i386/"`; \\
\t\tx86_64_path=`echo $$archive | sed -e "s/{first_arch}/x86_64/"`; \\
\t\tdestpath=`echo $$archive | sed -e "s/-debug//" | sed -e "s/{first_arch}-//" | sed -e "s/\.ios//"`; \\
\t\tall_paths=`echo $$archive`; \\
\t\tall_archs="{first_arch}"; \\
\t\tarchive_name=`basename $$archive`; \\
\t\tframework_name=`echo $$archive_name | cut -d '.' -f 1`; \\
\t\tall_paths=`echo $$all_paths/$$framework_name`; \\
\t\tmkdir -p `dirname $$destpath`; \\
286
{multiarch} \\
287 288 289
\t\techo "[{archs}] Mixing `basename $$archive` in $$destpath"; \\
\t\tlipo -create -output $$destpath/$$framework_name $$all_paths;  \\
\tdone; \\
290 291 292
\tif test -s WORK/ios-{first_arch}/Build/dummy_libraries/dummy_libraries.txt; then \\
\t\techo 'NOTE: the following libraries were STUBBED:'; \\
\t\tcat WORK/ios-{first_arch}/Build/dummy_libraries/dummy_libraries.txt; \\
293
\tfi; \\
294

295
build: $(addsuffix -build, $(archs))
296
\t$(MAKE) sdk
297

298
ipa: build
Benjamin REIS's avatar
Benjamin REIS committed
299 300 301 302 303
\txcodebuild -configuration Release && \\
\txcodebuild -sdk iphoneos -project linphone.xcodeproj -scheme linphone -configuration Release build \\
\t-archivePath linphone-iphone-'$(LINPHONE_IPHONE_VERSION)'.xcarchive archive && \\
\txcodebuild -exportArchive -archivePath linphone-iphone-'$(LINPHONE_IPHONE_VERSION)'.xcarchive \\
\t-exportPath linphone-iphone-'$(LINPHONE_IPHONE_VERSION)'.ipa -exportOptionsPlist Tools/exportOptions.plist
304

305
zipsdk: sdk
306 307 308
\trm -rf liblinphone-sdk/apple-darwin/Tools &&\\
\tmkdir -p liblinphone-sdk/apple-darwin/Tools &&\\
\tcp -f Tools/deploy.sh liblinphone-sdk/apple-darwin/Tools/.; \\
309 310 311 312 313 314 315
\techo "Generating SDK zip file for version $(LINPHONE_IPHONE_VERSION)"
\tzip -r liblinphone-iphone-sdk-$(LINPHONE_IPHONE_VERSION).zip \\
\tliblinphone-sdk/apple-darwin \\
\tliblinphone-tutorials \\
\t-x liblinphone-tutorials/hello-world/build\* \\
\t-x liblinphone-tutorials/hello-world/hello-world.xcodeproj/*.pbxuser \\
\t-x liblinphone-tutorials/hello-world/hello-world.xcodeproj/*.mode1v3
316

DanmeiChen's avatar
DanmeiChen committed
317 318 319
podspec: zipsdk
\tsed "s/FRAMEWORK_VERSION/$(LINPHONE_IPHONE_VERSION)/g" Tools/liblinphone.podspec > liblinphone.podspec

320
pull-transifex:
321
\ttx pull -af
322 323

push-transifex:
324
\t./Tools/i18n_generate_strings_files.sh && \\
325
\ttx push -s -f --no-interactive
326 327

zipres:
328
\t@tar -czf ios_assets.tar.gz Resources iTunesArtwork
329

330 331
{arch_targets}

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

help: help-prepare-options
337 338 339 340 341
\t@echo ""
\t@echo "(please read the README.md file first)"
\t@echo ""
\t@echo "Available architectures: {archs}"
\t@echo ""
342
\t@echo "Available targets:"
343
\t@echo ""
344 345
\t@echo "   * all or build: builds all architectures and creates the liblinphone SDK"
\t@echo "   * sdk: creates the liblinphone SDK. Use this only after a full build"
346
\t@echo "   * zipsdk: generates a ZIP archive of liblinphone-sdk/apple-darwin containing the SDK. Use this only after SDK is built."
347
\t@echo "   * zipres: creates a tar.gz file with all the resources (images)"
348 349 350
\t@echo ""
""".format(archs=' '.join(platforms), arch_opts='|'.join(platforms),
           first_arch=platforms[0], options=' '.join(sys.argv),
351
           arch_targets=arch_targets,
352
           multiarch=multiarch, generator=generator)
353 354 355
        f = open('Makefile', 'w')
        f.write(makefile)
        f.close()
356

357

358

359 360 361
def main():
    preparator = IOSPreparator()
    preparator.parse_args()
362 363
    if preparator.check_environment() != 0:
        preparator.show_environment_errors()
364 365
        return 1
    return preparator.run()
366 367

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