Skip to content

Commit 5d58ab6

Browse files
committed
Move additional swift compiler flags from build-script to Package.swift
This allows for a more consistent build, independent of whether build-script was used or not. This will allow us to use a unified build directory for all packages depending on Swift later on.
1 parent 86fe917 commit 5d58ab6

File tree

2 files changed

+57
-35
lines changed

2 files changed

+57
-35
lines changed

Package.swift

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,60 @@
1-
// swift-tools-version:4.2
1+
// swift-tools-version:5.1
22

33
import PackageDescription
4+
import Foundation
5+
6+
#if os(Linux)
7+
import Glibc
8+
#else
9+
import Darwin.C
10+
#endif
411

512
let package = Package(
613
name: "SwiftSyntax",
714
targets: [
815
.target(name: "_CSwiftSyntax"),
9-
.target(name: "SwiftSyntax", dependencies: ["_CSwiftSyntax"]),
1016
.testTarget(name: "SwiftSyntaxTest", dependencies: ["SwiftSyntax"], exclude: ["Inputs"]),
1117
.target(name: "SwiftSyntaxBuilder", dependencies: ["SwiftSyntax"]),
1218
.testTarget(name: "SwiftSyntaxBuilderTest", dependencies: ["SwiftSyntaxBuilder"]),
1319
.target(name: "lit-test-helper", dependencies: ["SwiftSyntax"])
20+
// Also see targets added below
1421
]
1522
)
1623

17-
#if os(Linux)
18-
import Glibc
19-
#else
20-
import Darwin.C
21-
#endif
24+
let swiftSyntaxTarget: PackageDescription.Target
25+
26+
/// If we are in a controlled CI environment, we can use internal compiler flags
27+
/// to speed up the build or improve it.
28+
if getenv("SWIFT_SYNTAX_CI_ENVIRONMENT") != nil {
29+
let groupFile = URL(fileURLWithPath: #file)
30+
.deletingLastPathComponent()
31+
.appendingPathComponent("utils")
32+
.appendingPathComponent("group.json")
33+
34+
var swiftSyntaxUnsafeFlags = ["-Xfrontend", "-group-info-path",
35+
"-Xfrontend", groupFile.path]
36+
// Enforcing exclusivity increases compile time of release builds by 2 minutes.
37+
// Disable it when we're in a controlled CI environment.
38+
swiftSyntaxUnsafeFlags += ["-enforce-exclusivity=unchecked"]
2239

23-
if getenv("SWIFT_SYNTAX_BUILD_SCRIPT") == nil {
24-
package.products.append(.library(name: "SwiftSyntax", targets: ["SwiftSyntax"]))
25-
package.products.append(.library(name: "SwiftSyntaxBuilder", targets: ["SwiftSyntaxBuilder"]))
40+
swiftSyntaxTarget = .target(name: "SwiftSyntax", dependencies: ["_CSwiftSyntax"],
41+
swiftSettings: [.unsafeFlags(swiftSyntaxUnsafeFlags)]
42+
)
2643
} else {
27-
package.products.append(.library(name: "SwiftSyntax", type: .dynamic, targets: ["SwiftSyntax"]))
28-
package.products.append(.library(name: "SwiftSyntaxBuilder", type: .dynamic, targets: ["SwiftSyntaxBuilder"]))
44+
swiftSyntaxTarget = .target(name: "SwiftSyntax", dependencies: ["_CSwiftSyntax"])
2945
}
46+
47+
package.targets.append(swiftSyntaxTarget)
48+
49+
let libraryType: Product.Library.LibraryType
50+
51+
/// When we're in a CI environment, we want to build a dylib instead of a static
52+
/// library since we install the dylib into the toolchain.
53+
if getenv("SWIFT_SYNTAX_CI_ENVIRONMENT") != nil {
54+
libraryType = .dynamic
55+
} else {
56+
libraryType = .static
57+
}
58+
59+
package.products.append(.library(name: "SwiftSyntax", type: libraryType, targets: ["SwiftSyntax"]))
60+
package.products.append(.library(name: "SwiftSyntaxBuilder", type: libraryType, targets: ["SwiftSyntaxBuilder"]))

build-script.py

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def check_gyb_exec():
8787

8888

8989
def check_rsync():
90-
with open(os.devnull, 'w') as DEVNULL:
90+
with open(os.devnull, 'w') as DEVNULL:
9191
if call(['rsync', '--version'], stdout=DEVNULL) != 0:
9292
fatal_error('Error: Could not find rsync.')
9393

@@ -155,11 +155,8 @@ def generate_gyb_files(verbose, add_source_locations, destination=None):
155155

156156
## Building swiftSyntax
157157

158-
def get_installed_name():
159-
return 'SwiftSyntax'
160-
161158
def get_installed_dylib_name():
162-
return 'lib' + get_installed_name() + '.dylib'
159+
return 'libSwiftSyntax.dylib'
163160

164161
def get_swiftpm_invocation(toolchain, action, build_dir, release):
165162
swift_exec = os.path.join(toolchain, 'usr', 'bin', 'swift')
@@ -171,11 +168,6 @@ def get_swiftpm_invocation(toolchain, action, build_dir, release):
171168
if build_dir:
172169
swiftpm_call.extend(['--build-path', build_dir])
173170

174-
# Swift compiler needs to know the module link name.
175-
swiftpm_call.extend(['-Xswiftc', '-module-link-name', '-Xswiftc', get_installed_name()])
176-
177-
# To speed up compilation.
178-
swiftpm_call.extend(['-Xswiftc', '-enforce-exclusivity=unchecked'])
179171
return swiftpm_call
180172

181173
class Builder(object):
@@ -190,20 +182,15 @@ def __init__(self, toolchain, build_dir, release, verbose,
190182
if verbose:
191183
self.swiftpm_call.extend(['--verbose'])
192184
self.verbose = verbose
193-
self._environ = dict(os.environ)
194-
self._environ['SWIFT_SYNTAX_BUILD_SCRIPT'] = ''
195185

196186
def build(self, product_name, module_group_path=''):
197187
print('** Building ' + product_name + ' **')
198188
command = list(self.swiftpm_call)
199189
command.extend(['--product', product_name])
200190

201-
# To build the group information into the module documentation file
202-
if module_group_path:
203-
command.extend(['-Xswiftc', '-Xfrontend', '-Xswiftc', '-group-info-path'])
204-
command.extend(['-Xswiftc', '-Xfrontend', '-Xswiftc', module_group_path])
205-
206-
check_call(command, env=self._environ, verbose=self.verbose)
191+
env = dict(os.environ)
192+
env['SWIFT_SYNTAX_CI_ENVIRONMENT'] = '1'
193+
check_call(command, env=env, verbose=self.verbose)
207194

208195

209196
## Testing
@@ -313,9 +300,15 @@ def run_lit_tests(toolchain, build_dir, release, filecheck_exec, verbose):
313300

314301
def run_xctests(toolchain, build_dir, release, verbose):
315302
print('** Running XCTests **')
303+
304+
# Testing builds with testability enabled. Don't overwrite the
305+
# build that we have just created and which shall be reused by
306+
# dependent projects. Instead, build in a new directory.
307+
test_build_dir = os.path.join(realpath(build_dir), 'test-build')
308+
316309
swiftpm_call = get_swiftpm_invocation(toolchain=toolchain,
317310
action='test',
318-
build_dir=build_dir,
311+
build_dir=test_build_dir,
319312
release=release)
320313

321314
if verbose:
@@ -390,7 +383,7 @@ def main():
390383

391384
basic_group = parser.add_argument_group('Basic')
392385

393-
basic_group.add_argument('--build-dir', default=None, help='''
386+
basic_group.add_argument('--build-dir', default='.build', help='''
394387
The directory in which build products shall be put. If omitted a
395388
directory named '.build' will be put in the swift-syntax directory.
396389
''')
@@ -508,8 +501,6 @@ def main():
508501
release=args.release,
509502
verbose=args.verbose,
510503
disable_sandbox=args.disable_sandbox)
511-
# TODO: Building with group info does not allow us to reuse the build
512-
# for running the tests.
513504
builder.build('SwiftSyntax', module_group_path=GROUP_INFO_PATH)
514505

515506
# Only build lit-test-helper if we are planning to run tests
@@ -524,7 +515,7 @@ def main():
524515
if args.test:
525516
try:
526517
success = run_tests(toolchain=args.toolchain,
527-
build_dir=realpath(args.build_dir),
518+
build_dir=args.build_dir,
528519
release=args.release,
529520
filecheck_exec=realpath(args.filecheck_exec),
530521
verbose=args.verbose)

0 commit comments

Comments
 (0)