prepare.py 9.7 KB
Newer Older
1
#!/usr/bin/env python
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

############################################################################
# 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
############################################################################

import argparse
import os
import platform
import shutil
import stat
import subprocess
import sys

33

34
class Target:
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 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

    def __init__(self, name, work_dir='WORK'):
        self.name = name
        self.output = 'OUTPUT'
        self.generator = None
        self.platform_name = None
        self.config_file = None
        self.toolchain_file = None
        self.required_build_platforms = None
        self.additional_args = []
        self.work_dir = work_dir + '/' + self.name
        self.abs_work_dir = os.getcwd() + '/' + self.work_dir
        self.cmake_dir = self.work_dir + '/cmake'
        self.abs_cmake_dir = os.getcwd() + '/' + self.cmake_dir

    def output_dir(self):
        output_dir = self.output
        if not os.path.isabs(self.output):
            top_dir = os.getcwd()
            output_dir = os.path.join(top_dir, self.output)
        if platform.system() == 'Windows':
            output_dir = output_dir.replace('\\', '/')
        return output_dir

    def cmake_command(self, build_type, latest, list_cmake_variables, additional_args):
        current_path = os.path.dirname(os.path.realpath(__file__))
        cmd = ['cmake', current_path]
        if self.generator is not None:
            cmd += ['-G', self.generator]
        if self.platform_name is not None:
            cmd += ['-A', self.platform_name]
        cmd += ['-DCMAKE_BUILD_TYPE=' + build_type]
        cmd += ['-DCMAKE_PREFIX_PATH=' + self.output_dir(), '-DCMAKE_INSTALL_PREFIX=' + self.output_dir()]
        cmd += ['-DLINPHONE_BUILDER_WORK_DIR=' + self.abs_work_dir]
        if self.toolchain_file is not None:
            cmd += ['-DCMAKE_TOOLCHAIN_FILE=' + self.toolchain_file]
        if self.config_file is not None:
            cmd += ['-DLINPHONE_BUILDER_CONFIG_FILE=' + self.config_file]
        if latest:
            cmd += ['-DLINPHONE_BUILDER_LATEST=YES']
        if list_cmake_variables:
            cmd += ['-L']
        for arg in self.additional_args:
            cmd += [arg]
        for arg in additional_args:
            cmd += [arg]
        cmd_str = ''
        for w in cmd:
            if ' ' in w:
                cmd_str += ' \"' + w + '\"'
            else:
                cmd_str += ' ' + w
        print(cmd_str)
        return cmd

    def clean(self):
        if os.path.isdir(self.abs_work_dir):
            shutil.rmtree(self.abs_work_dir, ignore_errors=False, onerror=self.handle_remove_read_only)

    def veryclean(self):
        self.clean()
        if os.path.isdir(self.output_dir()):
            shutil.rmtree(self.output_dir(), ignore_errors=False, onerror=self.handle_remove_read_only)

    def handle_remove_read_only(self, func, path, exc):
        if not os.access(path, os.W_OK):
            os.chmod(path, stat.S_IWUSR)
            func(path)
        else:
            raise

    def build_instructions(self, debug=False):
        if self.generator is not None and self.generator.startswith('Visual Studio'):
            config = "Release"
            if debug:
                config = "Debug"
            return "Open the \"{cmake_dir}/Project.sln\" Visual Studio solution and build with the \"{config}\" configuration".format(cmake_dir=self.cmake_dir, config=config)
        else:
            if self.generator in [None, "Unix Makefiles"]:
                builder = "make"
            elif self.generator == "Ninja":
                builder = "ninja"
            else:
                return "Unknown generator. Files have been generated in {cmake_dir}".format(cmake_dir=self.cmake_dir)
            return "Run the following command to build:\n\t{builder} -C {cmake_dir}".format(builder=builder, cmake_dir=self.cmake_dir)

121 122

class BB10Target(Target):
123 124 125 126 127 128 129

    def __init__(self, arch):
        Target.__init__(self, 'bb10-' + arch)
        self.config_file = 'configs/config-bb10-' + arch + '.cmake'
        self.toolchain_file = 'toolchains/toolchain-bb10-' + arch + '.cmake'
        self.output = 'OUTPUT/liblinphone-bb10-sdk/' + arch

130 131

class BB10i486Target(BB10Target):
132 133 134 135

    def __init__(self):
        BB10Target.__init__(self, 'i486')

136 137

class BB10armTarget(BB10Target):
138 139 140 141

    def __init__(self):
        BB10Target.__init__(self, 'arm')

142 143

class DesktopTarget(Target):
144 145 146 147 148 149 150

    def __init__(self):
        Target.__init__(self, 'desktop')
        self.config_file = 'configs/config-desktop.cmake'
        if platform.system() == 'Windows':
            self.generator = 'Visual Studio 12 2013'

151

152
class FlexisipTarget(Target):
153 154 155 156 157 158 159

    def __init__(self):
        Target.__init__(self, 'flexisip')
        self.required_build_platforms = ['Linux', 'Darwin']
        self.config_file = 'configs/config-flexisip.cmake'
        self.additional_args = ['-DLINPHONE_BUILDER_TARGET=flexisip']

160 161

class FlexisipRpmTarget(Target):
162 163 164 165 166 167 168

    def __init__(self):
        Target.__init__(self, 'flexisip-rpm')
        self.required_build_platforms = ['Linux', 'Darwin']
        self.config_file = 'configs/config-flexisip-rpm.cmake'
        self.additional_args = ['-DLINPHONE_BUILDER_TARGET=flexisip']

169

170
class PythonTarget(Target):
171 172 173 174 175 176 177

    def __init__(self):
        Target.__init__(self, 'python')
        self.config_file = 'configs/config-python.cmake'
        if platform.system() == 'Windows':
            self.generator = 'Visual Studio 9 2008'

178 179

class PythonRaspberryTarget(Target):
180 181 182 183 184 185

    def __init__(self):
        Target.__init__(self, 'python-raspberry')
        self.required_build_platforms = ['Linux']
        self.config_file = 'configs/config-python-raspberry.cmake'
        self.toolchain_file = 'toolchains/toolchain-raspberry.cmake'
186 187 188 189 190 191


targets = {}
targets['bb10-arm'] = BB10armTarget()
targets['bb10-i486'] = BB10i486Target()
targets['desktop'] = DesktopTarget()
192 193
targets['flexisip'] = FlexisipTarget()
targets['flexisip-rpm'] = FlexisipRpmTarget()
194 195 196 197 198
targets['python'] = PythonTarget()
targets['python-raspberry'] = PythonRaspberryTarget()
target_names = sorted(targets.keys())


199
def run(target, debug, latest, list_cmake_variables, force_build, additional_args):
200 201 202 203 204 205 206 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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    build_type = 'Debug' if debug else 'Release'

    if target.required_build_platforms is not None:
        if not platform.system() in target.required_build_platforms:
            print("Cannot build target '{target}' on '{bad_build_platform}' build platform. Build it on one of {good_build_platforms}.".format(
                target=target.name, bad_build_platform=platform.system(), good_build_platforms=', '.join(target.required_build_platforms)))
            return 52

    if os.path.isdir(target.abs_cmake_dir):
        if force_build is False:
            print("Working directory {} already exists. Please remove it (option -C or -c) before re-executing CMake "
                  "to avoid conflicts between executions.".format(target.cmake_dir))
            return 51
    else:
        os.makedirs(target.abs_cmake_dir)

    proc = subprocess.Popen(target.cmake_command(
        build_type, latest, list_cmake_variables, additional_args), cwd=target.abs_cmake_dir, shell=False)
    proc.communicate()
    return proc.returncode


def main(argv=None):
    if argv is None:
        argv = sys.argv
    argparser = argparse.ArgumentParser(description="Prepare build of Linphone and its dependencies.")
    argparser.add_argument(
        '-c', '--clean', help="Clean a previous build instead of preparing a build.", action='store_true')
    argparser.add_argument(
        '-C', '--veryclean', help="Clean a previous build and its installation directory.", action='store_true')
    argparser.add_argument('-d', '--debug', help="Prepare a debug build.", action='store_true')
    argparser.add_argument(
        '-f', '--force', help="Force preparation, even if working directory already exist.", action='store_true')
    argparser.add_argument('-l', '--latest', help="Build latest versions of all dependencies.", action='store_true')
    argparser.add_argument('-o', '--output', help="Specify output directory.")
    argparser.add_argument('-G', '--generator', metavar='generator', help="CMake generator to use.")
    argparser.add_argument('-L', '--list-cmake-variables',
                           help="List non-advanced CMake cache variables.", action='store_true', dest='list_cmake_variables')
    argparser.add_argument('target', choices=target_names, help="The target to build.")
    args, additional_args = argparser.parse_known_args()

    target = targets[args.target]
    if args.generator:
        target.generator = args.generator
    if args.output:
        target.output = args.output

    retcode = 0
    if args.veryclean:
        target.veryclean()
    elif args.clean:
        target.clean()
    else:
        retcode = run(target, args.debug, args.latest, args.list_cmake_variables, args.force, additional_args)
        if retcode == 0:
            print("\n" + target.build_instructions(args.debug))
    return retcode
257 258

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