|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright (C) 2017 LiveCode Ltd. |
| 3 | +# |
| 4 | +# This file is part of LiveCode. |
| 5 | +# |
| 6 | +# LiveCode is free software; you can redistribute it and/or modify it under |
| 7 | +# the terms of the GNU General Public License v3 as published by the Free |
| 8 | +# Software Foundation. |
| 9 | +# |
| 10 | +# LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | +# WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | +# for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with LiveCode. If not see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +import sys |
| 19 | +import platform |
| 20 | +import os |
| 21 | +import subprocess |
| 22 | + |
| 23 | +def usage(exit_status): |
| 24 | + print( |
| 25 | +"""Fetch prebuilts needed to build LiveCode. |
| 26 | +
|
| 27 | +Usage: |
| 28 | + fetch.py [--target TARGET] |
| 29 | +
|
| 30 | +Options: |
| 31 | + -p, --target TARGET |
| 32 | + Choose which target triple to build for |
| 33 | + -h, --help Print this message |
| 34 | +""") |
| 35 | + for p in KNOWN_PLATFORMS: |
| 36 | + print(" " + p) |
| 37 | + sys.exit(exit_status) |
| 38 | + |
| 39 | +def error(message): |
| 40 | + print('ERROR: ' + message) |
| 41 | + sys.exit(1) |
| 42 | + |
| 43 | +def guess_target(): |
| 44 | + system = platform.system() |
| 45 | + arch = platform.machine() |
| 46 | + if system == 'Darwin': |
| 47 | + return 'mac' |
| 48 | + if system == 'Linux': |
| 49 | + if re.match('^(x|i.?)86$', arch) is not None: |
| 50 | + return 'linux-x86' |
| 51 | + else: |
| 52 | + return 'linux-' + arch |
| 53 | + if system == 'Windows': |
| 54 | + if arch == 'AMD64': |
| 55 | + return 'win32-x86_64' |
| 56 | + else: |
| 57 | + return 'win32-x86' |
| 58 | + |
| 59 | +################################################################ |
| 60 | +# Parse command-line options |
| 61 | +################################################################ |
| 62 | + |
| 63 | +def process_env_options(opts): |
| 64 | + vars = ('TARGET',) |
| 65 | + for v in vars: |
| 66 | + opts[v] = os.getenv(v) |
| 67 | + |
| 68 | +def process_arg_options(opts, args): |
| 69 | + offset = 0 |
| 70 | + while offset < len(args): |
| 71 | + key = args[offset] |
| 72 | + if offset + 1 < len(args): |
| 73 | + value = args[offset + 1] |
| 74 | + else: |
| 75 | + value = None |
| 76 | + |
| 77 | + if key in ('-h', '--help'): |
| 78 | + usage(0) |
| 79 | + if key in ('-p', '--target'): |
| 80 | + opts['TARGET'] = value |
| 81 | + offset += 2 |
| 82 | + continue |
| 83 | + |
| 84 | + # Unrecognised option |
| 85 | + error("Unrecognised option '{}'".format(key)) |
| 86 | + |
| 87 | +################################################################ |
| 88 | +# Validate |
| 89 | +################################################################ |
| 90 | + |
| 91 | +def validate_target(opts): |
| 92 | + target = opts['TARGET'] |
| 93 | + if target is None: |
| 94 | + target = guess_target() |
| 95 | + if target is None: |
| 96 | + error("Cannot guess target; specify '--target <name>-'") |
| 97 | + |
| 98 | + opts['TARGET'] = target |
| 99 | + |
| 100 | +################################################################ |
| 101 | +# Action |
| 102 | +################################################################ |
| 103 | + |
| 104 | +def exec_fetch_libraries(build_platform): |
| 105 | + if platform.system() == 'Windows': |
| 106 | + args = [".\util\invoke-unix.bat", "prebuilt/fetch-libraries.sh", build_platform] |
| 107 | + else: |
| 108 | + args = ["./prebuilt/fetch-libraries.sh", build_platform] |
| 109 | + print(' '.join(args)) |
| 110 | + status = subprocess.call(args) |
| 111 | + if status != 0: |
| 112 | + sys.exit(status) |
| 113 | + |
| 114 | +def fetch(args): |
| 115 | + opts = {} |
| 116 | + process_env_options(opts) |
| 117 | + process_arg_options(opts, args) |
| 118 | + |
| 119 | + validate_target(opts) |
| 120 | + |
| 121 | + # Get the host platform to pass to fetch-libraries |
| 122 | + host_platform = guess_target().split('-')[0] |
| 123 | + |
| 124 | + # Get the target platform to pass to fetch-libraries |
| 125 | + target_platform = opts['TARGET'].split('-')[0] |
| 126 | + |
| 127 | + print('Fetching host platform prebuilts (' + host_platform + ')') |
| 128 | + exec_fetch_libraries(host_platform) |
| 129 | + |
| 130 | + print('Fetching target platform prebuilts (' + target_platform + ')') |
| 131 | + exec_fetch_libraries(target_platform) |
| 132 | + |
| 133 | +if __name__ == '__main__': |
| 134 | + fetch(sys.argv[1:]) |
0 commit comments