Skip to content

Commit 71abb4c

Browse files
authored
Merge pull request #2159 from aciidb0mb3r/clang-toolchain
[Toolchain] Lookup clang in the toolchain
2 parents 756eeaa + caf924e commit 71abb4c

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

Sources/Workspace/UserToolchain.swift

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,30 +151,35 @@ public final class UserToolchain: Toolchain {
151151
return toolPath
152152
}
153153

154-
// Otherwise, lookup the tool on the system.
154+
// Then, check the toolchain.
155+
do {
156+
let toolPath = destination.binDir.appending(component: "clang" + hostExecutableSuffix)
157+
if localFileSystem.exists(toolPath) {
158+
_clangCompiler = toolPath
159+
return toolPath
160+
}
161+
}
162+
163+
// Otherwise, lookup it up on the system.
155164
let arguments = whichArgs + ["clang"]
156165
let foundPath = try Process.checkNonZeroExit(arguments: arguments, environment: processEnvironment).spm_chomp()
157166
guard !foundPath.isEmpty else {
158167
throw InvalidToolchainDiagnostic("could not find clang")
159168
}
160169
let toolPath = try AbsolutePath(validating: foundPath)
161-
162-
// If we found clang using xcrun, assume the vendor is Apple.
163-
// FIXME: This might not be the best way to determine this.
164-
#if os(macOS)
165-
__isClangCompilerVendorApple = true
166-
#endif
167-
168170
_clangCompiler = toolPath
169171
return toolPath
170172
}
171173
private var _clangCompiler: AbsolutePath?
172-
private var __isClangCompilerVendorApple: Bool?
173174

174175
public func _isClangCompilerVendorApple() throws -> Bool? {
175-
// The boolean gets computed as a side-effect of lookup for clang compiler.
176-
_ = try getClangCompiler()
177-
return __isClangCompilerVendorApple
176+
// Assume the vendor is Apple on macOS.
177+
// FIXME: This might not be the best way to determine this.
178+
#if os(macOS)
179+
return true
180+
#else
181+
return false
182+
#endif
178183
}
179184

180185
/// Returns the path to llvm-cov tool.

Utilities/bootstrap

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ class Target(object):
306306

307307
link_input_nodes.append(object_path)
308308

309-
args = ["clang"]
309+
args = [args.clang_path]
310310
args.extend(common_args)
311311
args.extend([deps_path, "-c", source,"-o", object_path])
312312

@@ -733,6 +733,20 @@ def get_llbuild_source_path():
733733
note("clone llbuild next to swiftpm directory; see development docs: https://github.com/apple/swift-package-manager/blob/master/Documentation/Development.md#using-trunk-snapshot")
734734
error("unable to find llbuild source directory at %s" % llbuild_path)
735735

736+
def get_clang_path():
737+
try:
738+
if os.getenv("CC"):
739+
clang_path=os.path.realpath(os.getenv("CC"))
740+
return clang_path
741+
elif platform.system() == 'Darwin':
742+
return subprocess.check_output(["xcrun", "--find", "clang"],
743+
stderr=subprocess.PIPE, universal_newlines=True).strip()
744+
else:
745+
return subprocess.check_output(["which", "clang"],
746+
universal_newlines=True).strip()
747+
except:
748+
error("unable to find 'clang' tool for bootstrap build")
749+
736750
def get_swiftc_path():
737751
try:
738752
if os.getenv("SWIFT_EXEC"):
@@ -978,6 +992,7 @@ def main():
978992

979993
if not args.swiftc_path:
980994
args.swiftc_path = get_swiftc_path()
995+
args.clang_path = get_clang_path()
981996

982997
args.swift_stdlib_path = os.path.normpath(
983998
os.path.join(os.path.dirname(os.path.realpath(args.swiftc_path)), "..",
@@ -1128,6 +1143,7 @@ def main():
11281143
def make_fake_toolchain():
11291144
symlink_force(args.swiftc_path, os.path.join(bindir, "swift"))
11301145
symlink_force(args.swiftc_path, os.path.join(bindir, "swiftc"))
1146+
symlink_force(args.clang_path, os.path.join(bindir, "clang"))
11311147
symlink_force(args.sbt_path, os.path.join(bindir, "swift-build-tool"))
11321148
symlink_force(os.path.join(sandbox_path, "bin", "swift-build"),
11331149
bootstrapped_product)

0 commit comments

Comments
 (0)