Skip to content

Move additional swift compiler flags from build-script to Package.swift #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,29 +1,60 @@
// swift-tools-version:4.2
// swift-tools-version:5.1

import PackageDescription
import Foundation

#if os(Linux)
import Glibc
#else
import Darwin.C
#endif

let package = Package(
name: "SwiftSyntax",
targets: [
.target(name: "_CSwiftSyntax"),
.target(name: "SwiftSyntax", dependencies: ["_CSwiftSyntax"]),
.testTarget(name: "SwiftSyntaxTest", dependencies: ["SwiftSyntax"], exclude: ["Inputs"]),
.target(name: "SwiftSyntaxBuilder", dependencies: ["SwiftSyntax"]),
.testTarget(name: "SwiftSyntaxBuilderTest", dependencies: ["SwiftSyntaxBuilder"]),
.target(name: "lit-test-helper", dependencies: ["SwiftSyntax"])
// Also see targets added below
]
)

#if os(Linux)
import Glibc
#else
import Darwin.C
#endif
let swiftSyntaxTarget: PackageDescription.Target

/// If we are in a controlled CI environment, we can use internal compiler flags
/// to speed up the build or improve it.
if getenv("SWIFT_SYNTAX_CI_ENVIRONMENT") != nil {
let groupFile = URL(fileURLWithPath: #file)
.deletingLastPathComponent()
.appendingPathComponent("utils")
.appendingPathComponent("group.json")

var swiftSyntaxUnsafeFlags = ["-Xfrontend", "-group-info-path",
"-Xfrontend", groupFile.path]
// Enforcing exclusivity increases compile time of release builds by 2 minutes.
// Disable it when we're in a controlled CI environment.
swiftSyntaxUnsafeFlags += ["-enforce-exclusivity=unchecked"]

if getenv("SWIFT_SYNTAX_BUILD_SCRIPT") == nil {
package.products.append(.library(name: "SwiftSyntax", targets: ["SwiftSyntax"]))
package.products.append(.library(name: "SwiftSyntaxBuilder", targets: ["SwiftSyntaxBuilder"]))
swiftSyntaxTarget = .target(name: "SwiftSyntax", dependencies: ["_CSwiftSyntax"],
swiftSettings: [.unsafeFlags(swiftSyntaxUnsafeFlags)]
)
} else {
package.products.append(.library(name: "SwiftSyntax", type: .dynamic, targets: ["SwiftSyntax"]))
package.products.append(.library(name: "SwiftSyntaxBuilder", type: .dynamic, targets: ["SwiftSyntaxBuilder"]))
swiftSyntaxTarget = .target(name: "SwiftSyntax", dependencies: ["_CSwiftSyntax"])
}

package.targets.append(swiftSyntaxTarget)

let libraryType: Product.Library.LibraryType

/// When we're in a CI environment, we want to build a dylib instead of a static
/// library since we install the dylib into the toolchain.
if getenv("SWIFT_SYNTAX_CI_ENVIRONMENT") != nil {
libraryType = .dynamic
} else {
libraryType = .static
}

package.products.append(.library(name: "SwiftSyntax", type: libraryType, targets: ["SwiftSyntax"]))
package.products.append(.library(name: "SwiftSyntaxBuilder", type: libraryType, targets: ["SwiftSyntaxBuilder"]))
29 changes: 8 additions & 21 deletions build-script.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def check_gyb_exec():


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

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

## Building swiftSyntax

def get_installed_name():
return 'SwiftSyntax'

def get_installed_dylib_name():
return 'lib' + get_installed_name() + '.dylib'
return 'libSwiftSyntax.dylib'

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

# Swift compiler needs to know the module link name.
swiftpm_call.extend(['-Xswiftc', '-module-link-name', '-Xswiftc', get_installed_name()])

# To speed up compilation.
swiftpm_call.extend(['-Xswiftc', '-enforce-exclusivity=unchecked'])
return swiftpm_call

class Builder(object):
Expand All @@ -190,20 +182,15 @@ def __init__(self, toolchain, build_dir, release, verbose,
if verbose:
self.swiftpm_call.extend(['--verbose'])
self.verbose = verbose
self._environ = dict(os.environ)
self._environ['SWIFT_SYNTAX_BUILD_SCRIPT'] = ''

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

# To build the group information into the module documentation file
if module_group_path:
command.extend(['-Xswiftc', '-Xfrontend', '-Xswiftc', '-group-info-path'])
command.extend(['-Xswiftc', '-Xfrontend', '-Xswiftc', module_group_path])

check_call(command, env=self._environ, verbose=self.verbose)
env = dict(os.environ)
env['SWIFT_SYNTAX_CI_ENVIRONMENT'] = '1'
check_call(command, env=env, verbose=self.verbose)


## Testing
Expand Down Expand Up @@ -321,7 +308,9 @@ def run_xctests(toolchain, build_dir, release, verbose):
if verbose:
swiftpm_call.extend(['--verbose'])

return call(swiftpm_call, verbose=verbose) == 0
env = dict(os.environ)
env['SWIFT_SYNTAX_CI_ENVIRONMENT'] = '1'
return call(swiftpm_call, env=env, verbose=verbose) == 0

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

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