Skip to content

Commit ad9f12d

Browse files
committed
[utils] Introduce swift_build_support.diagnostics.
- This uses more Clang/Swift-style diagnostics, and adds a convenient wrapper `fatal()` which happens to match what almost all clients of the previous `print_with_argv0` wanted to do (i.e., fail with a fatal error). - As part of this, I also tried to make the diagnostics more consistent and use "diagnostic style" casing/punctuation. - Part of SR-237.
1 parent 95e3513 commit ad9f12d

File tree

4 files changed

+68
-62
lines changed

4 files changed

+68
-62
lines changed

Diff for: utils/SwiftBuildSupport.py

+9-24
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
sys.path.append(os.path.join(os.path.dirname(__file__), 'swift_build_support'))
2626

2727
# E402 means module level import not at top of file
28+
from swift_build_support import diagnostics # noqa (E402)
2829
from swift_build_support import shell # noqa (E402)
2930

3031

@@ -69,11 +70,6 @@ def _get_default_source_root():
6970
"SWIFT_BUILD_ROOT", os.path.join(SWIFT_SOURCE_ROOT, "build"))
7071

7172

72-
def print_with_argv0(message):
73-
print(sys.argv[0] + ": " + message)
74-
sys.stdout.flush()
75-
76-
7773
def check_call(args, print_command=False, verbose=False, disable_sleep=False):
7874
if disable_sleep:
7975
if platform.system() == 'Darwin':
@@ -87,17 +83,13 @@ def check_call(args, print_command=False, verbose=False, disable_sleep=False):
8783
try:
8884
return subprocess.check_call(args)
8985
except subprocess.CalledProcessError as e:
90-
print_with_argv0(
86+
diagnostics.fatal(
9187
"command terminated with a non-zero exit status " +
9288
str(e.returncode) + ", aborting")
93-
sys.stdout.flush()
94-
sys.exit(1)
9589
except OSError as e:
96-
print_with_argv0(
90+
diagnostics.fatal(
9791
"could not execute '" + shell.quote_command(args) +
9892
"': " + e.strerror)
99-
sys.stdout.flush()
100-
sys.exit(1)
10193

10294

10395
def check_output(args, print_command=False, verbose=False):
@@ -107,25 +99,20 @@ def check_output(args, print_command=False, verbose=False):
10799
try:
108100
return subprocess.check_output(args)
109101
except subprocess.CalledProcessError as e:
110-
print_with_argv0(
102+
diagnostics.fatal(
111103
"command terminated with a non-zero exit status " +
112104
str(e.returncode) + ", aborting")
113-
sys.stdout.flush()
114-
sys.exit(1)
115105
except OSError as e:
116-
print_with_argv0(
106+
diagnostics.fatal(
117107
"could not execute '" + shell.quote_command(args) +
118108
"': " + e.strerror)
119-
sys.stdout.flush()
120-
sys.exit(1)
121109

122110

123111
def _load_preset_files_impl(preset_file_names, substitutions={}):
124112
config = ConfigParser.SafeConfigParser(substitutions, allow_no_value=True)
125113
if config.read(preset_file_names) == []:
126-
print_with_argv0(
114+
diagnostics.fatal(
127115
"preset file not found (tried " + str(preset_file_names) + ")")
128-
sys.exit(1)
129116
return config
130117

131118

@@ -190,12 +177,10 @@ def get_preset_options(substitutions, preset_file_names, preset_name):
190177
(build_script_opts, build_script_impl_opts, missing_opts) = \
191178
_get_preset_options_impl(config, substitutions, preset_name)
192179
if not build_script_opts and not build_script_impl_opts:
193-
print_with_argv0("preset '" + preset_name + "' not found")
194-
sys.exit(1)
180+
diagnostics.fatal("preset '" + preset_name + "' not found")
195181
if missing_opts:
196-
print_with_argv0("missing option(s) for preset '" + preset_name +
197-
"': " + ", ".join(missing_opts))
198-
sys.exit(1)
182+
diagnostics.fatal("missing option(s) for preset '" + preset_name +
183+
"': " + ", ".join(missing_opts))
199184

200185
# Migrate 'swift-sdks' parameter to 'stdlib-deployment-targets'
201186
for opt in build_script_impl_opts:

Diff for: utils/build-script

+26-37
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ from SwiftBuildSupport import (
3131
check_call,
3232
get_all_preset_names,
3333
get_preset_options,
34-
print_with_argv0,
3534
) # noqa (E402 module level import not at top of file)
3635

3736
sys.path.append(os.path.join(os.path.dirname(__file__), 'swift_build_support'))
3837

3938
# E402 means module level import not at top of file
4039
from swift_build_support import arguments # noqa (E402)
40+
from swift_build_support import diagnostics # noqa (E402)
4141
from swift_build_support.toolchain import host_toolchain # noqa (E402)
4242
import swift_build_support.debug # noqa (E402)
4343
from swift_build_support import migration # noqa (E402)
@@ -102,8 +102,7 @@ def main_preset():
102102
return 0
103103

104104
if not args.preset:
105-
print_with_argv0("Missing --preset option")
106-
return 1
105+
diagnostics.fatal("missing --preset option")
107106

108107
args.preset_substitutions = {}
109108

@@ -121,7 +120,7 @@ def main_preset():
121120
if args.distcc:
122121
build_script_args += ["--distcc"]
123122

124-
print_with_argv0(
123+
diagnostics.note(
125124
"using preset '" + args.preset + "', which expands to \n\n" +
126125
shell.quote_command(build_script_args) + "\n")
127126

@@ -995,27 +994,22 @@ details of the setups of other systems or automated environments.""")
995994
toolchain.cmake = args.cmake
996995

997996
if toolchain.cc is None or toolchain.cxx is None:
998-
print_with_argv0(
999-
"Can't find clang. Please install clang-3.5 or a later version.")
1000-
return 1
997+
diagnostics.fatal(
998+
"can't find clang (please install clang-3.5 or a later version)")
1001999

10021000
if toolchain.cmake is None:
1003-
print_with_argv0("Can't find CMake. Please install CMake.")
1004-
return 1
1001+
diagnostics.fatal("can't find CMake (please install CMake)")
10051002

10061003
if args.distcc:
10071004
if toolchain.distcc is None:
1008-
print_with_argv0(
1009-
"Can't find distcc. Please install distcc")
1010-
return 1
1005+
diagnostics.fatal(
1006+
"can't find distcc (please install distcc)")
10111007
if toolchain.distcc_pump is None:
1012-
print_with_argv0(
1013-
"Can't find distcc-pump. Please install distcc-pump")
1014-
return 1
1008+
diagnostics.fatal(
1009+
"can't find distcc-pump (please install distcc-pump)")
10151010

10161011
if args.host_target is None or args.stdlib_deployment_targets is None:
1017-
print_with_argv0("Unknown operating system.")
1018-
return 1
1012+
diagnostics.fatal("unknown operating system")
10191013

10201014
if args.symbols_package:
10211015
if not os.path.isabs(args.symbols_package):
@@ -1024,10 +1018,9 @@ details of the setups of other systems or automated environments.""")
10241018
'(was \'{}\')'.format(args.symbols_package))
10251019
return 1
10261020
if not args.install_symroot:
1027-
print_with_argv0(
1021+
diagnostics.fatal(
10281022
"--install-symroot is required when specifying "
10291023
"--symbols-package.")
1030-
return 1
10311024

10321025
if args.android:
10331026
if args.android_ndk is None or \
@@ -1036,12 +1029,11 @@ details of the setups of other systems or automated environments.""")
10361029
args.android_icu_uc_include is None or \
10371030
args.android_icu_i18n is None or \
10381031
args.android_icu_i18n_include is None:
1039-
print_with_argv0("When building for Android, --android-ndk, "
1040-
"--android-ndk-version, --android-icu-uc, "
1041-
"--android-icu-uc-include, --android-icu-i18n, "
1042-
"and --android-icu-i18n-include must be "
1043-
"specified.")
1044-
return 1
1032+
diagnostics.fatal("when building for Android, --android-ndk, "
1033+
"--android-ndk-version, --android-icu-uc, "
1034+
"--android-icu-uc-include, --android-icu-i18n, "
1035+
"and --android-icu-i18n-include must be "
1036+
"specified")
10451037

10461038
# Build cmark if any cmark-related options were specified.
10471039
if (args.cmark_build_variant is not None):
@@ -1213,9 +1205,8 @@ details of the setups of other systems or automated environments.""")
12131205

12141206
if args.build_ninja:
12151207
if not os.path.exists(workspace.source_dir("ninja")):
1216-
print_with_argv0("Can't find source directory for ninja "
1217-
"(tried %s)" % (workspace.source_dir("ninja")))
1218-
return 1
1208+
diagnostics.fatal("can't find source directory for ninja "
1209+
"(tried %s)" % (workspace.source_dir("ninja")))
12191210

12201211
os.umask(0o022)
12211212

@@ -1464,17 +1455,15 @@ details of the setups of other systems or automated environments.""")
14641455

14651456
def main():
14661457
if not SWIFT_SOURCE_ROOT:
1467-
print_with_argv0(
1468-
"Could not infer source root directory. " +
1469-
"Forgot to set $SWIFT_SOURCE_ROOT environment variable?")
1470-
return 1
1458+
diagnostics.fatal(
1459+
"could not infer source root directory " +
1460+
"(forgot to set $SWIFT_SOURCE_ROOT environment variable?)")
14711461

14721462
if not os.path.isdir(SWIFT_SOURCE_ROOT):
1473-
print_with_argv0(
1474-
"Source root directory \'" + SWIFT_SOURCE_ROOT +
1475-
"\' does not exist. " +
1476-
"Forgot to set $SWIFT_SOURCE_ROOT environment variable?")
1477-
return 1
1463+
diagnostics.fatal(
1464+
"source root directory \'" + SWIFT_SOURCE_ROOT +
1465+
"\' does not exist " +
1466+
"(forgot to set $SWIFT_SOURCE_ROOT environment variable?)")
14781467

14791468
# Determine if we are invoked in the preset mode and dispatch accordingly.
14801469
if any([(opt.startswith("--preset") or opt == "--show-presets")

Diff for: utils/swift_build_support/swift_build_support/arguments.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# ----------------------------------------------------------------------------
1212
"""
13-
argparse suppliments
13+
argparse supplements
1414
"""
1515
# ----------------------------------------------------------------------------
1616

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# swift_build_support/diagnostics.py - Diagnostic Utilities -*- python -*-
2+
#
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2016 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See http://swift.org/LICENSE.txt for license information
9+
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
#
11+
# ----------------------------------------------------------------------------
12+
13+
import sys
14+
15+
16+
def note(message):
17+
"""
18+
note(message)
19+
20+
Print a diagnostic notification to the standard output.
21+
"""
22+
print(sys.argv[0] + ": note: " + message)
23+
sys.stdout.flush()
24+
25+
26+
def fatal(message):
27+
"""
28+
fatal(message)
29+
30+
Raise a fatal error.
31+
"""
32+
raise SystemExit(sys.argv[0] + ": fatal error: " + message)

0 commit comments

Comments
 (0)