Skip to content

Commit c5be148

Browse files
authored
Merge pull request #162 from ahoppen/swift-args-in-package
Move additional swift compiler flags from build-script to Package.swift
2 parents 3297e1f + c1209f6 commit c5be148

File tree

2 files changed

+51
-33
lines changed

2 files changed

+51
-33
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: 8 additions & 21 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
@@ -321,7 +308,9 @@ def run_xctests(toolchain, build_dir, release, verbose):
321308
if verbose:
322309
swiftpm_call.extend(['--verbose'])
323310

324-
return call(swiftpm_call, verbose=verbose) == 0
311+
env = dict(os.environ)
312+
env['SWIFT_SYNTAX_CI_ENVIRONMENT'] = '1'
313+
return call(swiftpm_call, env=env, verbose=verbose) == 0
325314

326315
def delete_rpath(rpath, binary):
327316
if platform.system() == 'Darwin':
@@ -508,8 +497,6 @@ def main():
508497
release=args.release,
509498
verbose=args.verbose,
510499
disable_sandbox=args.disable_sandbox)
511-
# TODO: Building with group info does not allow us to reuse the build
512-
# for running the tests.
513500
builder.build('SwiftSyntax', module_group_path=GROUP_INFO_PATH)
514501

515502
# Only build lit-test-helper if we are planning to run tests

0 commit comments

Comments
 (0)