Skip to content

Commit bf4d43b

Browse files
committed
[Commands] Factor out common helper for version information.
- This also normalizes exactly what version information can be overridden by the bootstrap script.
1 parent fde884e commit bf4d43b

File tree

5 files changed

+98
-44
lines changed

5 files changed

+98
-44
lines changed

Sources/Commands/SwiftBuildTool.swift

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ import PackageLoading
1515
import PackageModel
1616
import Utility
1717

18-
#if HasCustomVersionString
19-
import VersionInfo
20-
#endif
21-
2218
import enum Build.Configuration
2319
import enum Utility.ColorWrap
2420
import protocol Build.Toolchain
@@ -135,11 +131,7 @@ public struct SwiftBuildTool: SwiftTool {
135131
usage()
136132

137133
case .version:
138-
#if HasCustomVersionString
139-
print(String(cString: VersionInfo.DisplayString()))
140-
#else
141-
print("Swift Package Manager – Swift 3.0")
142-
#endif
134+
print(Versioning.currentVersion.completeDisplayString)
143135

144136
case .build(let conf, let toolchain):
145137
let graph = try loadPackage(at: opts.path.root, ignoreDependencies: opts.ignoreDependencies)

Sources/Commands/SwiftPackageTool.swift

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ import PackageModel
1616
import Utility
1717
import Xcodeproj
1818

19-
#if HasCustomVersionString
20-
import VersionInfo
21-
#endif
22-
2319
import enum Build.Configuration
2420
import enum Utility.ColorWrap
2521
import protocol Build.Toolchain
@@ -168,11 +164,7 @@ public struct SwiftPackageTool: SwiftTool {
168164
usage()
169165

170166
case .version:
171-
#if HasCustomVersionString
172-
print(String(cString: VersionInfo.DisplayString()))
173-
#else
174-
print("Swift Package Manager – Swift 3.0")
175-
#endif
167+
print(Versioning.currentVersion.completeDisplayString)
176168

177169
case .initPackage:
178170
let initPackage = try InitPackage(mode: opts.initMode)

Sources/Commands/SwiftTestTool.swift

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ import Basic
1414
import Build
1515
import Utility
1616

17-
#if HasCustomVersionString
18-
import VersionInfo
19-
#endif
20-
2117
import func POSIX.chdir
2218
import func POSIX.exit
2319

@@ -158,11 +154,7 @@ public struct SwiftTestTool: SwiftTool {
158154
usage()
159155

160156
case .version:
161-
#if HasCustomVersionString
162-
print(String(cString: VersionInfo.DisplayString()))
163-
#else
164-
print("Swift Package Manager – Swift 3.0")
165-
#endif
157+
print(Versioning.currentVersion.completeDisplayString)
166158

167159
case .listTests:
168160
let testPath = try buildTestsIfNeeded(opts)

Sources/Utility/Versioning.swift

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright 2016 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
// Import the custom version string (generated via the bootstrap script), if
12+
// available.
13+
#if HasCustomVersionString
14+
import VersionInfo
15+
#endif
16+
17+
/// A Swift version number.
18+
///
19+
/// Note that these are *NOT* semantically versioned numbers.
20+
public struct SwiftVersion {
21+
/// The version number.
22+
public var version: (major: Int, minor: Int, patch: Int)
23+
24+
/// Whether or not this is a development version.
25+
public var isDevelopment: Bool
26+
27+
/// Build information, as an unstructured string.
28+
public var buildIdentifier: String?
29+
30+
/// The version as a readable string.
31+
public var displayString: String {
32+
var result = "\(version.major).\(version.minor).\(version.patch)"
33+
if isDevelopment {
34+
result += "-dev"
35+
}
36+
if let buildIdentifier = self.buildIdentifier {
37+
result += " (" + buildIdentifier + ")"
38+
}
39+
return result
40+
}
41+
42+
/// The complete product version display string (including the name).
43+
public var completeDisplayString: String {
44+
var vendorPrefix = ""
45+
#if HasCustomVersionString
46+
vendorPrefix += String(cString: VersionInfo.VendorNameString()) + " "
47+
#endif
48+
return vendorPrefix + "Swift Package Manager - Swift " + displayString
49+
}
50+
}
51+
52+
private func getBuildIdentifier() -> String? {
53+
#if HasCustomVersionString
54+
return String(cString: VersionInfo.BuildIdentifierString())
55+
#else
56+
return nil
57+
#endif
58+
}
59+
60+
/// Version support for the package manager.
61+
public struct Versioning {
62+
/// The current version of the package manager.
63+
public static let currentVersion = SwiftVersion(
64+
version: (3, 0, 0),
65+
isDevelopment: true,
66+
buildIdentifier: getBuildIdentifier())
67+
}

Utilities/bootstrap

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -354,22 +354,28 @@ def make_xml_string_literal(str):
354354

355355

356356
# Write out a header containing version information, and a module map for it.
357-
def write_bootstrap_version_info(include_path, version_str):
357+
def write_bootstrap_version_info(include_path, vendor_name, build_identifier):
358358
# Construct the header and write it out if it's changed.
359359
output = StringIO()
360-
print("static inline const char * DisplayString() {", file=output)
361-
print(" return " + make_c_string_literal(version_str) + ";", file=output)
362-
print("}", file=output)
363-
write_file_if_changed(os.path.join(include_path, "VersionInfo.h"),
364-
output.getvalue())
360+
vendor_name_literal = make_c_string_literal(vendor_name)
361+
build_identifier_literal = make_c_string_literal(build_identifier)
362+
contents = """
363+
static inline const char* VendorNameString() {
364+
return %(vendor_name_literal)s;
365+
}
366+
static inline const char* BuildIdentifierString() {
367+
return %(build_identifier_literal)s;
368+
}
369+
""" % locals()
370+
write_file_if_changed(os.path.join(include_path, "VersionInfo.h"), contents)
365371

366372
# Also construct the module map and write it out if it's changed.
367-
output = StringIO()
368-
print("module VersionInfo [extern_c] {", file=output)
369-
print(" header \"VersionInfo.h\"", file=output)
370-
print("}", file=output)
371-
write_file_if_changed(os.path.join(include_path, "module.map"),
372-
output.getvalue())
373+
contents = """\
374+
module VersionInfo [extern_c] {
375+
header \"VersionInfo.h\"
376+
}
377+
"""
378+
write_file_if_changed(os.path.join(include_path, "module.map"), contents)
373379

374380

375381
def create_bootstrap_files(sandbox_path, args):
@@ -521,8 +527,11 @@ def create_bootstrap_files(sandbox_path, args):
521527
output.getvalue())
522528

523529
# Write out the version info header and module map file, if appropriate.
524-
if args.version_str:
525-
write_bootstrap_version_info(inc_dir, args.version_str)
530+
if args.vendor_name or args.build_identifier:
531+
if not args.vendor_name or not args.build_identifier:
532+
error("--build-identifier is required with --vendor-name")
533+
write_bootstrap_version_info(inc_dir, args.vendor_name,
534+
args.build_identifier)
526535

527536

528537
def process_runtime_libraries(build_path, args, bootstrap=False):
@@ -690,8 +699,10 @@ def main():
690699
help="Build stage 2 for release")
691700
parser.add_argument("--generate-xcodeproj", action="store_true",
692701
help="Also generate an Xcode project for SwiftPM")
693-
parser.add_argument("--version-string", dest="version_str",
694-
help="version string to display for --version")
702+
parser.add_argument("--vendor-name", dest="vendor_name",
703+
help="vendor name for use in --version")
704+
parser.add_argument("--build-identifier", dest="build_identifier",
705+
help="build identifier for use in --version")
695706
args = parser.parse_args()
696707

697708
if not args.swiftc_path:
@@ -793,7 +804,7 @@ def main():
793804
build_flags.append("-v")
794805

795806
# If appropriate, add flags for a custom version string:
796-
if args.version_str:
807+
if args.vendor_name or args.build_identifier:
797808
incdir = os.path.join(sandbox_path, "inc")
798809
build_flags.extend(["-Xswiftc", "-I{}".format(incdir)])
799810
build_flags.extend(["-Xswiftc", "-DHasCustomVersionString"])

0 commit comments

Comments
 (0)