ios.py 12.1 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 47 48 49 50
#!/usr/bin/env python

############################################################################
# prepare.py
# Copyright (C) 2015  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 os
import re
import shutil
import sys
from distutils.spawn import find_executable
from logging import error, warning, info
from subprocess import Popen, PIPE
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 IOSTarget(prepare.Target):

    def __init__(self, arch):
        prepare.Target.__init__(self, 'ios-' + arch, 'iOS')
        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'
51
        self.output = 'iOS/liblinphone-sdk/' + arch + '-apple-darwin.ios'
52
        self.external_source_path = os.path.join(current_path, 'submodules')
53 54 55 56 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122


class IOSi386Target(IOSTarget):

    def __init__(self):
        IOSTarget.__init__(self, 'i386')


class IOSx8664Target(IOSTarget):

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


class IOSarmv7Target(IOSTarget):

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


class IOSarm64Target(IOSTarget):

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



ios_targets = {
    'i386': IOSi386Target(),
    'x86_64': IOSx8664Target(),
    'armv7': IOSarmv7Target(),
    'arm64': IOSarm64Target()
}

ios_virtual_targets = {
    'devices': ['armv7', 'arm64'],
    'simulators': ['i386', 'x86_64'],
    'all': ['i386', 'x86_64', 'armv7', 'arm64']
}

class IOSPreparator(prepare.Preparator):

    def __init__(self, targets=ios_targets, virtual_targets=ios_virtual_targets):
        prepare.Preparator.__init__(self, targets, default_targets=['armv7', 'arm64', 'x86_64'], virtual_targets=virtual_targets)
        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)

        if self.args.all_codecs:
            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_CODEC2=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_OPENH264=ON"]
            self.additional_args += ["-DENABLE_VPX=ON"]

    def clean(self):
        prepare.Preparator.clean(self)
123 124
        if os.path.isfile('Makefile.ios'):
            os.remove('Makefile.ios')
125 126
        if os.path.isdir('iOS') and not os.listdir('iOS'):
            os.rmdir('iOS')
127 128 129 130
        #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)
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 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 201 202 203 204 205 206 207 208


    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"

    def check_environment(self):
        reterr = 0
        reterr |= prepare.Preparator.check_environment(self)
        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:
                error("Invalid version of nasm detected. Please make sure that you are NOT using Apple's binary here")
                self.missing_dependencies["nasm"] = "nasm"
                reterr = 1

        if self.check_is_installed("libtoolize", "libtoolize", warn=False):
            if not self.check_is_installed("glibtoolize", "libtool"):
                reterr = 1
                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")))

        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

        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 generate_makefile(self, generator, project_file=''):
        platforms = self.args.target
        arch_targets = ""
        for arch in platforms:
            arch_targets += """
209
ios-{arch}: ios-{arch}-build
210

211
ios-{arch}-build:
212 213 214 215 216 217 218 219 220 221 222 223 224 225
\t{generator} iOS/ios-{arch}/cmake/{project_file}
\t@echo "Done"
""".format(arch=arch, generator=generator, project_file=project_file)
        multiarch = ""
        for arch in platforms[1:]:
            multiarch += \
                """\tif test -f "$${arch}_path"; then \\
\t\tall_paths=`echo $$all_paths $${arch}_path`; \\
\t\tall_archs="$$all_archs,{arch}" ; \\
\telse \\
\t\techo "WARNING: archive `basename $$archive` exists in {first_arch} tree but does not exists in {arch} tree: $${arch}_path."; \\
\tfi; \\
""".format(first_arch=platforms[0], arch=arch)
        makefile = """
226
ios-archs={archs}
227 228 229
LINPHONE_IPHONE_VERSION=$(shell git describe --always)

.PHONY: all
230
.SILENT: ios-sdk
231
all: generate-ios-sdk
232

233
ios-sdk:
234 235 236
\tarchives=`find iOS/liblinphone-sdk/{first_arch}-apple-darwin.ios -name '*.framework'` && \\
\trm -rf iOS/liblinphone-sdk/apple-darwin && \\
\tmkdir -p iOS/liblinphone-sdk/apple-darwin && \\
Benjamin REIS's avatar
Benjamin REIS committed
237
\tcp iOS/liblinphone-sdk/{first_arch}-apple-darwin.ios/share/linphonecs/LinphoneWrapper.cs Xamarin/Xamarin/Xamarin && \\
238 239 240 241
\tcp -rf iOS/liblinphone-sdk/{first_arch}-apple-darwin.ios/share iOS/liblinphone-sdk/apple-darwin/. && \\
\tcp -rf iOS/liblinphone-sdk/{first_arch}-apple-darwin.ios/lib iOS/liblinphone-sdk/apple-darwin/. && \\
\tcp -rf iOS/liblinphone-sdk/{first_arch}-apple-darwin.ios/include iOS/liblinphone-sdk/apple-darwin/. && \\
\tcp -rf iOS/liblinphone-sdk/{first_arch}-apple-darwin.ios/Frameworks iOS/liblinphone-sdk/apple-darwin/. && \\
242 243 244 245 246 247 248 249
\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}"; \\
250 251
\t\tarchive_name=`basename $$archive`; \\
\t\tframework_name=`echo $$archive_name | cut -d '.' -f 1`; \\
252 253 254
\t\tmkdir -p `dirname $$destpath`; \\
\t\t{multiarch} \\
\t\techo "[{archs}] Mixing `basename $$archive` in $$destpath"; \\
255
\t\tlipo -create -output $$destpath/$$framework_name $$armv7_path/$$framework_name $$arm64_path/$$framework_name $$x86_64_path/$$framework_name; \\
256
\tdone; \\
Benjamin REIS's avatar
Benjamin REIS committed
257
\tcp -rf iOS/liblinphone-sdk/apple-darwin/Frameworks Xamarin/Xamarin/Xamarin.iOS \\
258

259
generate-ios-sdk: $(addprefix ios-, $(addsuffix -build, $(ios-archs)))
Benjamin REIS's avatar
Benjamin REIS committed
260
\t$(MAKE) -f Makefile.ios ios-sdk
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

{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 ""
276
\t@echo "   * all or generate-ios-sdk: builds all architectures and creates the liblinphone SDK"
277
\t@echo "   * ios-sdk: creates the liblinphone SDK. Use this only after a full build"
278 279 280 281 282
\t@echo ""
""".format(archs=' '.join(platforms), arch_opts='|'.join(platforms),
           first_arch=platforms[0], options=' '.join(sys.argv),
           arch_targets=arch_targets,
           multiarch=multiarch, generator=generator)
283
        f = open('Makefile.ios', 'w')
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
        f.write(makefile)
        f.close()



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

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