reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    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
   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
  121
  122
  123
  124
  125
  126
  127
  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
  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
  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
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
#!/usr/bin/python

from __future__ import print_function

import argparse
import getpass
import os
import os.path
import re
import select
import sys
import subprocess

_COMMON_SYNC_OPTS = "-avzh --delete"
_COMMON_EXCLUDE_OPTS = "--exclude=DerivedData --exclude=.svn --exclude=.git --exclude=llvm-build/Release+Asserts"


def normalize_configuration(config_text):
    if not config_text:
        return "debug"

    config_lower = config_text.lower()
    if config_lower in ["debug", "release"]:
        return config_lower
    else:
        raise Exception("unknown configuration specified: %s" % config_text)


def parse_args():
    DEFAULT_REMOTE_ROOT_DIR = "/mnt/ssd/work/macosx.sync"
    DEFAULT_REMOTE_HOSTNAME = "tfiala2.mtv.corp.google.com"
    OPTIONS_FILENAME = ".remote-build.conf"
    DEFAULT_SSH_PORT = "22"

    parser = argparse.ArgumentParser(fromfile_prefix_chars='@')

    parser.add_argument(
        "--configuration",
        "-c",
        help="specify configuration (Debug, Release)",
        default=normalize_configuration(
            os.environ.get(
                'CONFIGURATION',
                'Debug')))
    parser.add_argument(
        "--debug", "-d",
        action="store_true",
        help="help debug the remote-build script by adding extra logging")
    parser.add_argument(
        "--local-lldb-dir", "-l", metavar="DIR",
        help="specify local lldb directory (Xcode layout assumed for llvm/clang)",
        default=os.getcwd())
    parser.add_argument(
        "--port", "-p",
        help="specify the port ssh should use to connect to the remote side",
        default=DEFAULT_SSH_PORT)
    parser.add_argument(
        "--remote-address", "-r", metavar="REMOTE-ADDR",
        help="specify the dns name or ip address of the remote linux system",
        default=DEFAULT_REMOTE_HOSTNAME)
    parser.add_argument(
        "--remote-dir", metavar="DIR",
        help="specify the root of the linux source/build dir",
        default=DEFAULT_REMOTE_ROOT_DIR)
    parser.add_argument(
        "--user", "-u", help="specify the user name for the remote system",
        default=getpass.getuser())
    parser.add_argument(
        "--xcode-action",
        "-x",
        help="$(ACTION) from Xcode",
        nargs='?',
        default=None)

    command_line_args = sys.argv[1:]
    if os.path.exists(OPTIONS_FILENAME):
        # Prepend the file so that command line args override the file
        # contents.
        command_line_args.insert(0, "@%s" % OPTIONS_FILENAME)

    return parser.parse_args(command_line_args)


def maybe_create_remote_root_dir(args):
    commandline = [
        "ssh",
        "-p", args.port,
        "%s@%s" % (args.user, args.remote_address),
        "mkdir",
        "-p",
        args.remote_dir]
    print("create remote root dir command:\n{}".format(commandline))
    return subprocess.call(commandline)


def init_with_args(args):
    # Expand any user directory specs in local-side source dir (on MacOSX).
    args.local_lldb_dir = os.path.expanduser(args.local_lldb_dir)

    # Append the configuration type to the remote build dir.
    args.configuration = normalize_configuration(args.configuration)
    args.remote_build_dir = os.path.join(
        args.remote_dir,
        "build-%s" % args.configuration)

    # We assume the local lldb directory is really named 'lldb'.
    # This is because on the remote end, the local lldb root dir
    # is copied over underneath llvm/tools and will be named there
    # whatever it is named locally.  The remote build will assume
    # is is called lldb.
    if os.path.basename(args.local_lldb_dir) != 'lldb':
        raise Exception(
            "local lldb root needs to be called 'lldb' but was {} instead"
            .format(os.path.basename(args.local_lldb_dir)))

    args.lldb_dir_relative_regex = re.compile(
        "%s/llvm/tools/lldb/" % args.remote_dir)
    args.llvm_dir_relative_regex = re.compile("%s/" % args.remote_dir)

    print("Xcode action:", args.xcode_action)

    # Ensure the remote directory exists.
    result = maybe_create_remote_root_dir(args)
    if result == 0:
        print("using remote root dir: %s" % args.remote_dir)
    else:
        print("remote root dir doesn't exist and could not be created, "
              + "error code:", result)
        return False

    return True


def sync_llvm(args):
    commandline = ["rsync"]
    commandline.extend(_COMMON_SYNC_OPTS.split())
    commandline.extend(_COMMON_EXCLUDE_OPTS.split())
    commandline.append("--exclude=/llvm/tools/lldb")
    commandline.extend(["-e", "ssh -p {}".format(args.port)])
    commandline.extend([
        "%s/llvm" % args.local_lldb_dir,
        "%s@%s:%s" % (args.user, args.remote_address, args.remote_dir)])
    if args.debug:
        print("going to execute llvm sync: {}".format(commandline))
    return subprocess.call(commandline)


def sync_lldb(args):
    commandline = ["rsync"]
    commandline.extend(_COMMON_SYNC_OPTS.split())
    commandline.extend(_COMMON_EXCLUDE_OPTS.split())
    commandline.append("--exclude=/lldb/llvm")
    commandline.extend(["-e", "ssh -p {}".format(args.port)])
    commandline.extend([args.local_lldb_dir, "%s@%s:%s/llvm/tools" %
                        (args.user, args.remote_address, args.remote_dir)])
    if args.debug:
        print("going to execute lldb sync: {}".format(commandline))
    return subprocess.call(commandline)


def build_cmake_command(args):
    # args.remote_build_dir
    # args.configuration in ('release', 'debug')

    if args.configuration == 'debug-optimized':
        build_type_name = "RelWithDebInfo"
    elif args.configuration == 'release':
        build_type_name = "Release"
    else:
        build_type_name = "Debug"

    ld_flags = "\"-lstdc++ -lm\""

    install_dir = os.path.join(
        args.remote_build_dir, "..", "install-{}".format(args.configuration))

    command_line = [
        "cmake",
        "-GNinja",
        "-DCMAKE_CXX_COMPILER=clang",
        "-DCMAKE_C_COMPILER=clang",
        # "-DCMAKE_CXX_FLAGS=%s" % cxx_flags,
        "-DCMAKE_SHARED_LINKER_FLAGS=%s" % ld_flags,
        "-DCMAKE_EXE_LINKER_FLAGS=%s" % ld_flags,
        "-DCMAKE_INSTALL_PREFIX:PATH=%s" % install_dir,
        "-DCMAKE_BUILD_TYPE=%s" % build_type_name,
        "-Wno-dev",
        os.path.join("..", "llvm")
    ]

    return command_line


def maybe_configure(args):
    commandline = [
        "ssh",
        "-p", args.port,
        "%s@%s" % (args.user, args.remote_address),
        "cd", args.remote_dir, "&&",
        "mkdir", "-p", args.remote_build_dir, "&&",
        "cd", args.remote_build_dir, "&&"
    ]
    commandline.extend(build_cmake_command(args))

    if args.debug:
        print("configure command: {}".format(commandline))

    return subprocess.call(commandline)


def filter_build_line(args, line):
    lldb_relative_line = args.lldb_dir_relative_regex.sub('', line)
    if len(lldb_relative_line) != len(line):
        # We substituted - return the modified line
        return lldb_relative_line

    # No match on lldb path (longer on linux than llvm path).  Try
    # the llvm path match.
    return args.llvm_dir_relative_regex.sub('', line)


def run_remote_build_command(args, build_command_list):
    commandline = [
        "ssh",
        "-p", args.port,
        "%s@%s" % (args.user, args.remote_address),
        "cd", args.remote_build_dir, "&&"]
    commandline.extend(build_command_list)

    if args.debug:
        print("running remote build command: {}".format(commandline))

    proc = subprocess.Popen(
        commandline,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)

    # Filter stdout/stderr output for file path mapping.
    # We do this to enable Xcode to see filenames relative to the
    # MacOSX-side directory structure.
    while True:
        reads = [proc.stdout.fileno(), proc.stderr.fileno()]
        select_result = select.select(reads, [], [])

        for fd in select_result[0]:
            if fd == proc.stdout.fileno():
                line = proc.stdout.readline()
                display_line = filter_build_line(args, line.rstrip())
                if display_line and len(display_line) > 0:
                    print(display_line)
            elif fd == proc.stderr.fileno():
                line = proc.stderr.readline()
                display_line = filter_build_line(args, line.rstrip())
                if display_line and len(display_line) > 0:
                    print(display_line, file=sys.stderr)

        proc_retval = proc.poll()
        if proc_retval is not None:
            # Process stopped.  Drain output before finishing up.

            # Drain stdout.
            while True:
                line = proc.stdout.readline()
                if line:
                    display_line = filter_build_line(args, line.rstrip())
                    if display_line and len(display_line) > 0:
                        print(display_line)
                else:
                    break

            # Drain stderr.
            while True:
                line = proc.stderr.readline()
                if line:
                    display_line = filter_build_line(args, line.rstrip())
                    if display_line and len(display_line) > 0:
                        print(display_line, file=sys.stderr)
                else:
                    break

            return proc_retval


def build(args):
    return run_remote_build_command(args, ["time", "ninja"])


def clean(args):
    return run_remote_build_command(args, ["ninja", "clean"])


if __name__ == "__main__":
    # Handle arg parsing.
    args = parse_args()

    # Initialize the system.
    if not init_with_args(args):
        exit(1)

    # Sync over llvm and clang source.
    sync_llvm(args)

    # Sync over lldb source.
    sync_lldb(args)

    # Configure the remote build if it's not already.
    maybe_configure(args)

    if args.xcode_action == 'clean':
        exit(clean(args))
    else:
        exit(build(args))