forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwiftBuildSupport.py
140 lines (111 loc) · 4.3 KB
/
SwiftBuildSupport.py
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
#===--- SwiftBuildSupport.py - Utilities for Swift build scripts -----------===#
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See http://swift.org/LICENSE.txt for license information
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
#
#===------------------------------------------------------------------------===#
from __future__ import print_function
import ConfigParser
import os
import pipes
import subprocess
import sys
HOME = os.environ["HOME"]
# Set SWIFT_SOURCE_ROOT in your environment to control where the sources
# are found.
SWIFT_SOURCE_ROOT = os.environ.get(
"SWIFT_SOURCE_ROOT", os.path.join(HOME, "src", "s"))
# Set SWIFT_BUILD_ROOT to a directory that will contain a subdirectory
# for each build configuration
SWIFT_BUILD_ROOT = os.environ.get(
"SWIFT_BUILD_ROOT", os.path.join(HOME, "build", "swift"))
def print_with_argv0(message):
print(sys.argv[0] + ": " + message)
def bad_usage(message):
print_with_argv0(message)
print("Run '" + pipes.quote(sys.argv[0]) + " --help' for more information.")
sys.exit(1)
def quote_shell_command(args):
return " ".join([ pipes.quote(a) for a in args ])
def check_call(args, verbose=False):
try:
return subprocess.check_call(args)
except subprocess.CalledProcessError as e:
if verbose:
print_with_argv0(e.strerror)
else:
print_with_argv0(
"command terminated with a non-zero exit status " +
str(e.returncode) + ", aborting")
sys.stdout.flush()
sys.exit(1)
except OSError as e:
print_with_argv0("could not execute '" + quote_shell_command(args) +
"': " + e.strerror)
sys.stdout.flush()
sys.exit(1)
def check_output(args, verbose=False):
try:
return subprocess.check_output(args)
except subprocess.CalledProcessError as e:
if verbose:
print_with_argv0(e.strerror)
else:
print_with_argv0(
"command terminated with a non-zero exit status " +
str(e.returncode) + ", aborting")
sys.stdout.flush()
sys.exit(1)
except OSError as e:
print_with_argv0("could not execute '" + quote_shell_command(args) +
"': " + e.strerror)
sys.stdout.flush()
sys.exit(1)
def _get_preset_options_impl(config, substitutions, preset_name):
section_name = "preset: " + preset_name
if section_name not in config.sections():
return (None, None)
build_script_opts = []
build_script_impl_opts = []
dash_dash_seen = False
for o, a in config.items(section_name):
if o in substitutions:
continue
opt = None
if o == "mixin-preset":
# Split on newlines and filter out empty lines.
mixins = filter(None, [m.strip() for m in a.splitlines()])
for mixin in mixins:
(base_build_script_opts, base_build_script_impl_opts) = \
_get_preset_options_impl(config, substitutions, mixin)
build_script_opts += base_build_script_opts
build_script_impl_opts += base_build_script_impl_opts
elif o == "dash-dash":
dash_dash_seen = True
elif a == "":
opt = "--" + o
else:
opt = "--" + o + "=" + a
if opt:
if not dash_dash_seen:
build_script_opts.append(opt)
else:
build_script_impl_opts.append(opt)
return (build_script_opts, build_script_impl_opts)
def get_preset_options(substitutions, preset_file_names, preset_name):
config = ConfigParser.SafeConfigParser(substitutions, allow_no_value=True)
if config.read(preset_file_names) == []:
print_with_argv0(
"preset file not found (tried " + str(preset_file_names) + ")")
sys.exit(1)
(build_script_opts, build_script_impl_opts) = \
_get_preset_options_impl(config, substitutions, preset_name)
if build_script_opts is None:
print_with_argv0("preset '" + preset_name + "' not found")
sys.exit(1)
return build_script_opts + [ "--" ] + build_script_impl_opts