Skip to content

Commit e771660

Browse files
committed
[build-script] Support building "early" SwiftSyntax via --early-swiftsyntax
Similar to the way the early Swift Driver is built, introduce a build-script option `--early-swiftsyntax` that uses the host Swift and CMake to build the parts of the swift-syntax package that are expected to become be used in the compiler. Note that this does not obviate the need for the `--swiftsyntax` option, because that build product uses the just-built Swift compiler and SwiftPM to build, test, and install everything from the swift-syntax package.
1 parent 7d55dea commit e771660

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

utils/build_swift/build_swift/driver_arguments.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,9 @@ def create_argument_parser():
651651
option(['--swiftsyntax'], toggle_true('build_swiftsyntax'),
652652
help='build swiftSyntax')
653653

654+
option(['--early-swiftsyntax'], toggle_true('build_early_swiftsyntax'),
655+
help='build early SwiftSyntax')
656+
654657
option(['--skstresstester'], toggle_true('build_skstresstester'),
655658
help='build the SourceKit stress tester')
656659

utils/build_swift/tests/expected_options.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
'build_swiftpm': False,
9191
'build_swift_driver': False,
9292
'build_early_swift_driver': True,
93+
'build_early_swiftsyntax': False,
9394
'build_swiftsyntax': False,
9495
'build_libparser_only': False,
9596
'build_skstresstester': False,
@@ -516,6 +517,7 @@ class BuildScriptImplOption(_BaseOption):
516517
SetTrueOption('--skip-build'),
517518
SetTrueOption('--swiftpm', dest='build_swiftpm'),
518519
SetTrueOption('--swift-driver', dest='build_swift_driver'),
520+
SetTrueOption('--early-swiftsyntax', dest='build_early_swiftsyntax'),
519521
SetTrueOption('--swiftsyntax', dest='build_swiftsyntax'),
520522
SetTrueOption('--build-libparser-only', dest='build_libparser_only'),
521523
SetTrueOption('--skstresstester', dest='build_skstresstester'),

utils/swift_build_support/swift_build_support/build_script_invocation.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,10 @@ def compute_product_pipelines(self):
548548
builder = ProductPipelineListBuilder(self.args)
549549

550550
builder.begin_pipeline()
551+
552+
builder.add_product(products.EarlySwiftSyntax,
553+
is_enabled=self.args.build_early_swiftsyntax)
554+
551555
# If --skip-early-swift-driver is passed in, swift will be built
552556
# as usual, but relying on its own C++-based (Legacy) driver.
553557
# Otherwise, we build an "early" swift-driver using the host

utils/swift_build_support/swift_build_support/products/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .benchmarks import Benchmarks
1515
from .cmark import CMark
1616
from .curl import LibCurl
17+
from .earlyswiftsyntax import EarlySwiftSyntax
1718
from .earlyswiftdriver import EarlySwiftDriver
1819
from .foundation import Foundation
1920
from .indexstoredb import IndexStoreDB
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# swift_build_support/products/earlyswiftsyntax.py --------------*- python -*-
2+
#
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See https://swift.org/LICENSE.txt for license information
9+
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
#
11+
# ----------------------------------------------------------------------------
12+
13+
import os
14+
15+
from . import cmake_product
16+
from . import product
17+
from .. import shell
18+
from .. import toolchain
19+
20+
21+
# SwiftSyntax is a Swift module used to parse and manipulate Swift syntax. This
22+
# build product is a "Special" SwiftSyntax that gets built with the host
23+
# toolchain that can be linked into the Swift compiler itself, hence it does not
24+
# depend on any other build product of `build-script`.
25+
class EarlySwiftSyntax(cmake_product.CMakeProduct):
26+
@classmethod
27+
def product_source_name(cls):
28+
return "swift-syntax"
29+
30+
@classmethod
31+
def is_build_script_impl_product(cls):
32+
return False
33+
34+
@classmethod
35+
def is_before_build_script_impl_product(cls):
36+
return True
37+
38+
def should_build(self, host_target):
39+
if self.args.build_early_swiftsyntax:
40+
if toolchain.host_toolchain().find_tool("swift") is None:
41+
warn_msg = 'Host toolchain could not locate a '\
42+
'compiler to build early swift-syntax.'
43+
print('-- Warning: {}', warn_msg)
44+
return False
45+
else:
46+
return True
47+
return False
48+
49+
@classmethod
50+
def get_dependencies(cls):
51+
return []
52+
53+
def build(self, host_target):
54+
self.cmake_options.define('CMAKE_BUILD_TYPE:STRING',
55+
self.args.swift_build_variant)
56+
self.cmake_options.define('BUILD_SHARED_LIBS:STRING', 'NO')
57+
58+
(platform, arch) = host_target.split('-')
59+
60+
common_c_flags = ' '.join(self.common_cross_c_flags(platform, arch))
61+
self.cmake_options.define('CMAKE_C_FLAGS', common_c_flags)
62+
self.cmake_options.define('CMAKE_CXX_FLAGS', common_c_flags)
63+
64+
if host_target.startswith("macosx") or \
65+
host_target.startswith("iphone") or \
66+
host_target.startswith("appletv") or \
67+
host_target.startswith("watch"):
68+
toolchain_file = self.generate_darwin_toolchain_file(platform, arch)
69+
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
70+
elif platform == "linux":
71+
toolchain_file = self.generate_linux_toolchain_file(platform, arch)
72+
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
73+
74+
self.build_with_cmake(["all"], self.args.swift_build_variant, [])
75+
76+
def should_test(self, host_target):
77+
# The normal SwiftSyntax target runs tests through SwiftPM.
78+
return False
79+
80+
def test(self, host_target):
81+
pass
82+
83+
def should_install(self, host_target):
84+
# This product is for the swift-syntax used with the build-directory compiler.
85+
# If a toolchain install is required, please use the SwiftSyntax (no 'Early')
86+
# product with `--swift-syntax --install-swift-syntax`.
87+
return False
88+
89+
@classmethod
90+
def is_ignore_install_all_product(cls):
91+
# Ensures that `install_all` setting triggered by `--infer` does not
92+
# affect products which specify `is_ignore_install_all_product` as
93+
# True. This is useful for products which should not be installed into the
94+
# toolchain (corresponding build products that use the just-built
95+
# toolchain are the products that get installed, e.g. `swiftsyntax` to
96+
# `earlyswiftsyntax`).
97+
return True

0 commit comments

Comments
 (0)