Skip to content

Commit 41adc85

Browse files
authored
Merge pull request swiftlang#29756 from Rostepher/build-swift-constants
[Build System: build-script] build_swift constants module.
2 parents 6640316 + e793fcf commit 41adc85

File tree

18 files changed

+457
-162
lines changed

18 files changed

+457
-162
lines changed

utils/build-parser-lib

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@ import os
3030
import platform
3131
import sys
3232

33-
from build_swift.build_swift import argparse, defaults
33+
from build_swift.build_swift import argparse
34+
from build_swift.build_swift import defaults
35+
from build_swift.build_swift.constants import SWIFT_BUILD_ROOT
36+
from build_swift.build_swift.constants import SWIFT_SOURCE_ROOT
3437
from build_swift.build_swift.wrappers import xcrun
3538

3639
from swift_build_support.swift_build_support import shell
37-
from swift_build_support.swift_build_support.SwiftBuildSupport import (
38-
SWIFT_BUILD_ROOT,
39-
SWIFT_SOURCE_ROOT,
40-
)
4140
from swift_build_support.swift_build_support.toolchain import host_toolchain
4241

4342
isDarwin = platform.system() == "Darwin"

utils/build-script

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,29 @@ from build_swift.build_swift import defaults
2828
from build_swift.build_swift import driver_arguments
2929
from build_swift.build_swift import migration
3030
from build_swift.build_swift import presets
31+
from build_swift.build_swift.constants import BUILD_SCRIPT_IMPL_PATH
32+
from build_swift.build_swift.constants import SWIFT_BUILD_ROOT
33+
from build_swift.build_swift.constants import SWIFT_REPO_NAME
34+
from build_swift.build_swift.constants import SWIFT_SOURCE_ROOT
3135

3236
import six
3337

3438
from swift_build_support.swift_build_support import products
3539
from swift_build_support.swift_build_support import shell
3640
from swift_build_support.swift_build_support import targets
3741
from swift_build_support.swift_build_support import workspace
38-
from swift_build_support.swift_build_support.SwiftBuildSupport import (
39-
HOME,
40-
SWIFT_BUILD_ROOT,
41-
SWIFT_REPO_NAME,
42-
SWIFT_SOURCE_ROOT,
43-
)
4442
from swift_build_support.swift_build_support.cmake import CMake
4543
from swift_build_support.swift_build_support.host_specific_configuration \
4644
import HostSpecificConfiguration
47-
from swift_build_support.swift_build_support.targets import \
48-
StdlibDeploymentTarget
45+
from swift_build_support.swift_build_support.targets import StdlibDeploymentTarget
4946
from swift_build_support.swift_build_support.toolchain import host_toolchain
5047

5148

5249
# -----------------------------------------------------------------------------
5350
# Constants
5451

55-
BUILD_SCRIPT_IMPL = os.path.join(
56-
SWIFT_SOURCE_ROOT, SWIFT_REPO_NAME, "utils", "build-script-impl")
52+
# TODO: Remove this constant, it's really not helpful.
53+
HOME = os.environ.get("HOME", "/")
5754

5855

5956
# -----------------------------------------------------------------------------
@@ -839,7 +836,7 @@ class BuildScriptInvocation(object):
839836
# `build-script-impl`.
840837
if self.args.legacy_impl:
841838
# Execute the underlying build script implementation.
842-
shell.call_without_sleeping([BUILD_SCRIPT_IMPL] + self.impl_args,
839+
shell.call_without_sleeping([BUILD_SCRIPT_IMPL_PATH] + self.impl_args,
843840
env=self.impl_env, echo=True)
844841
return
845842

@@ -961,7 +958,7 @@ class BuildScriptInvocation(object):
961958

962959
def _execute_action(self, action_name):
963960
shell.call_without_sleeping(
964-
[BUILD_SCRIPT_IMPL] + self.impl_args +
961+
[BUILD_SCRIPT_IMPL_PATH] + self.impl_args +
965962
["--only-execute", action_name],
966963
env=self.impl_env, echo=self.args.verbose_build)
967964

@@ -1142,7 +1139,7 @@ def main_normal():
11421139
# If we received any impl args, check if `build-script-impl` would
11431140
# accept them or not before any further processing.
11441141
try:
1145-
migration.check_impl_args(BUILD_SCRIPT_IMPL,
1142+
migration.check_impl_args(BUILD_SCRIPT_IMPL_PATH,
11461143
args.build_script_impl_args)
11471144
except ValueError as e:
11481145
exit_rejecting_arguments(e, parser)
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See https://swift.org/LICENSE.txt for license information
7+
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
9+
10+
from __future__ import absolute_import
11+
from __future__ import print_function
12+
from __future__ import unicode_literals
13+
14+
import os.path
15+
16+
17+
__all__ = [
18+
"BUILD_SCRIPT_IMPL_PATH",
19+
"BUILD_SCRIPT_PATH",
20+
"BUILD_SWIFT_PATH",
21+
"MODULE_PATH",
22+
"MULTIROOT_DATA_FILE_PATH",
23+
"PROJECT_PATH",
24+
"RESOURCES_PATH",
25+
"SWIFT_BUILD_ROOT",
26+
"SWIFT_REPO_NAME",
27+
"SWIFT_SOURCE_ROOT",
28+
"UTILS_PATH",
29+
]
30+
31+
32+
# --------------------------------------------------------------------------------------
33+
# Project Paths
34+
35+
36+
MODULE_PATH = os.path.abspath(os.path.dirname(__file__))
37+
38+
BUILD_SWIFT_PATH = os.path.dirname(MODULE_PATH)
39+
40+
UTILS_PATH = os.path.dirname(BUILD_SWIFT_PATH)
41+
42+
PROJECT_PATH = os.path.dirname(UTILS_PATH)
43+
44+
45+
BUILD_SCRIPT_PATH = os.path.join(UTILS_PATH, "build-script")
46+
47+
BUILD_SCRIPT_IMPL_PATH = os.path.join(UTILS_PATH, "build-script-impl")
48+
49+
50+
# --------------------------------------------------------------------------------------
51+
# Resources
52+
53+
54+
RESOURCES_PATH = os.path.join(BUILD_SWIFT_PATH, "resources")
55+
56+
57+
# The path to the Xcode workspace to use for a unified build of multiple SwiftPM
58+
# projects.
59+
MULTIROOT_DATA_FILE_PATH = os.path.join(
60+
RESOURCES_PATH, "SwiftPM-Unified-Build.xcworkspace"
61+
)
62+
63+
64+
# --------------------------------------------------------------------------------------
65+
# Helpers
66+
67+
68+
def _is_llvm_checkout(llvm_path):
69+
"""Returns true if the given llvm_path is a valid LLVM checkout, false otherwise.
70+
71+
NOTE: This is a very naive validation, checking only for the existence of a few
72+
known files.
73+
"""
74+
75+
if not os.path.exists(os.path.join(llvm_path, "tools")):
76+
return False
77+
78+
if not os.path.exists(os.path.join(llvm_path, "CMakeLists.txt")):
79+
return False
80+
81+
return True
82+
83+
84+
def _is_swift_checkout(swift_path):
85+
"""Returns true if the given swift_path is a valid Swift checkout, false otherwise.
86+
87+
NOTE: This is a very naive validation, checking only for the existence of a few
88+
known files.
89+
"""
90+
91+
if not os.path.exists(os.path.join(swift_path, "utils")):
92+
return False
93+
94+
if not os.path.exists(os.path.join(swift_path, "CMakeLists.txt")):
95+
return False
96+
97+
return True
98+
99+
100+
def _get_swift_source_root(swift_path, env=None):
101+
"""Returns the Swift source root or None if one cannot be determined.
102+
103+
Users are able to manually override the source root by setting the SWIFT_SOURCE_ROOT
104+
environment variable. If that cannot be found then this function will check the
105+
directory structure to infer if we are building as a standalone Swift build or if we
106+
are building in the unified LLVM.
107+
108+
Building standalone means Swift will be checked out as a peer of LLVM and the
109+
enclosing directory is the source root.
110+
111+
source-root/
112+
|- llvm/
113+
|- swift/
114+
| ...
115+
116+
However the unified case means Swift will be checked out in the llvm/tools
117+
directory, which means the directory containing LLVM is the source root.
118+
119+
source-root/
120+
|- llvm/
121+
| |- tools/
122+
| | |- swift/
123+
| | | ...
124+
| | ...
125+
| ...
126+
127+
In the case that this function is called with an invalid Swift checkout it returns
128+
None as well.
129+
130+
FIXME: What about the new llvm-project monorepo?
131+
"""
132+
133+
env = env or {}
134+
135+
# Check the environment first.
136+
if "SWIFT_SOURCE_ROOT" in env:
137+
return env["SWIFT_SOURCE_ROOT"]
138+
139+
# Assert we are in a valid Swift checkout.
140+
if not _is_swift_checkout(swift_path):
141+
return None
142+
143+
source_root = os.path.dirname(swift_path)
144+
145+
# Check if Swift is checked out as part of a unified build.
146+
if os.path.basename(source_root) != "tools":
147+
return source_root
148+
149+
llvm_path = os.path.dirname(source_root)
150+
if not _is_llvm_checkout(llvm_path):
151+
return source_root
152+
153+
# Return the directory containing LLVM.
154+
return os.path.dirname(llvm_path)
155+
156+
157+
def _get_swift_build_root(source_root, env=None):
158+
"""Returns the Swift build root.
159+
160+
Users are able to manually override the build root by setting the SWIFT_BUILD_ROOT
161+
environment variable. If that cannot be found then this function returns the path
162+
to a directory named "build" in the given source root.
163+
"""
164+
165+
env = env or {}
166+
167+
if "SWIFT_BUILD_ROOT" in env:
168+
return env["SWIFT_BUILD_ROOT"]
169+
170+
return os.path.join(source_root, "build")
171+
172+
173+
def _get_swift_repo_name(swift_path, env=None):
174+
"""Returns the Swift repo name or None if it cannot be determined.
175+
176+
Users are able to manually override the repo name by setting the SWIFT_REPO_NAME
177+
environment variable. If that cannot be found then this function returns the name
178+
of the given swift path or None if it is not a valid Swift checkout.
179+
"""
180+
181+
env = env or {}
182+
183+
if "SWIFT_REPO_NAME" in env:
184+
return env["SWIFT_REPO_NAME"]
185+
186+
if not _is_swift_checkout(swift_path):
187+
return None
188+
189+
return os.path.basename(swift_path)
190+
191+
192+
# --------------------------------------------------------------------------------------
193+
# Swift Source and Build Roots
194+
195+
196+
# Set SWIFT_SOURCE_ROOT in your environment to control where the sources are found.
197+
SWIFT_SOURCE_ROOT = _get_swift_source_root(PROJECT_PATH, env=os.environ)
198+
199+
# Set SWIFT_BUILD_ROOT to a directory that will contain a subdirectory for each build
200+
# configuration
201+
SWIFT_BUILD_ROOT = _get_swift_build_root(SWIFT_SOURCE_ROOT, env=os.environ)
202+
203+
# Set SWIFT_REPO_NAME in your environment to control the name of the swift directory
204+
# name that is used.
205+
SWIFT_REPO_NAME = _get_swift_repo_name(PROJECT_PATH, env=os.environ)

utils/build_swift/resources/SwiftPM-Unified-Build.xcworkspace/contents.xcworkspacedata

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)