Skip to content

Commit 39a4362

Browse files
authored
Merge pull request #4543 from karwa/toolchain-3.0
[swift-3.0] Added -tools-directory option. (#2912)
2 parents e6b7c40 + df77f80 commit 39a4362

File tree

5 files changed

+104
-4
lines changed

5 files changed

+104
-4
lines changed

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ def j : JoinedOrSeparate<["-"], "j">, Flags<[DoesNotAffectIncrementalBuild]>,
130130
def sdk : Separate<["-"], "sdk">, Flags<[FrontendOption]>,
131131
HelpText<"Compile against <sdk>">, MetaVarName<"<sdk>">;
132132

133+
def tools_directory : Separate<["-"], "tools-directory">,
134+
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>,
135+
HelpText<"Look for external executables (ld, clang, binutils) in <directory>">, MetaVarName<"<directory>">;
136+
133137
def D : JoinedOrSeparate<["-"], "D">, Flags<[FrontendOption]>,
134138
HelpText<"Marks a conditional compilation flag as true">;
135139

lib/Driver/ToolChains.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,19 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,
951951
const Driver &D = getDriver();
952952
const llvm::Triple &Triple = getTriple();
953953

954-
InvocationInfo II{"ld"};
954+
// Configure the toolchain.
955+
// By default, use the system `ld` to link.
956+
const char *LD = "ld";
957+
if (const Arg *A = context.Args.getLastArg(options::OPT_tools_directory)) {
958+
StringRef toolchainPath(A->getValue());
959+
960+
// If there is a 'ld' in the toolchain folder, use that instead.
961+
if (auto toolchainLD = llvm::sys::findProgramByName("ld", {toolchainPath})) {
962+
LD = context.Args.MakeArgString(toolchainLD.get());
963+
}
964+
}
965+
966+
InvocationInfo II = {LD};
955967
ArgStringList &Arguments = II.Arguments;
956968

957969
if (context.Args.hasArg(options::OPT_driver_use_filelists) ||
@@ -1269,14 +1281,14 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
12691281
case LinkKind::None:
12701282
llvm_unreachable("invalid link kind");
12711283
case LinkKind::Executable:
1272-
// Default case, nothing extra needed
1284+
// Default case, nothing extra needed.
12731285
break;
12741286
case LinkKind::DynamicLibrary:
12751287
Arguments.push_back("-shared");
12761288
break;
12771289
}
12781290

1279-
// Select the linker to use
1291+
// Select the linker to use.
12801292
std::string Linker;
12811293
if (const Arg *A = context.Args.getLastArg(options::OPT_use_ld)) {
12821294
Linker = A->getValue();
@@ -1287,6 +1299,22 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
12871299
Arguments.push_back(context.Args.MakeArgString("-fuse-ld=" + Linker));
12881300
}
12891301

1302+
// Configure the toolchain.
1303+
// By default, use the system clang++ to link.
1304+
const char * Clang = "clang++";
1305+
if (const Arg *A = context.Args.getLastArg(options::OPT_tools_directory)) {
1306+
StringRef toolchainPath(A->getValue());
1307+
1308+
// If there is a clang in the toolchain folder, use that instead.
1309+
if (auto toolchainClang = llvm::sys::findProgramByName("clang++", {toolchainPath})) {
1310+
Clang = context.Args.MakeArgString(toolchainClang.get());
1311+
}
1312+
1313+
// Look for binutils in the toolchain folder.
1314+
Arguments.push_back("-B");
1315+
Arguments.push_back(context.Args.MakeArgString(A->getValue()));
1316+
}
1317+
12901318
std::string Target = getTargetForLinker();
12911319
if (!Target.empty()) {
12921320
Arguments.push_back("-target");
@@ -1393,7 +1421,7 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
13931421
Arguments.push_back("-o");
13941422
Arguments.push_back(context.Output.getPrimaryOutputFilename().c_str());
13951423

1396-
return {"clang++", Arguments};
1424+
return {Clang, Arguments};
13971425
}
13981426

13991427
std::string
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python
2+
# clang++ - Fake Clang to test finding clang++ in the toolchain path
3+
#
4+
# This source file is part of the Swift.org open source project
5+
#
6+
# Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
7+
# Licensed under Apache License v2.0 with Runtime Library Exception
8+
#
9+
# See http://swift.org/LICENSE.txt for license information
10+
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
11+
#
12+
# ----------------------------------------------------------------------------
13+
14+
from __future__ import print_function
15+
16+
print("Sorry, I'm lazy-clang")

test/Driver/Inputs/fake-toolchain/ld

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python
2+
# ld - Fake ld to test finding the Darwin linker in the toolchain path
3+
#
4+
# This source file is part of the Swift.org open source project
5+
#
6+
# Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
7+
# Licensed under Apache License v2.0 with Runtime Library Exception
8+
#
9+
# See http://swift.org/LICENSE.txt for license information
10+
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
11+
#
12+
# ----------------------------------------------------------------------------
13+
14+
from __future__ import print_function
15+
16+
print("Sorry, wrong number!")

test/Driver/tools_directory.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//=================================================
2+
// ** GENERIC UNIX TARGETS - linking via clang++ **
3+
//=================================================
4+
5+
// RUN: %swiftc_driver -### -target x86_64-linux-unknown -tools-directory %S/Inputs/fake-toolchain %s 2>&1 | %FileCheck -check-prefix CLANGSUB %s
6+
// RUN: %swiftc_driver -### -target x86_64-linux-unknown -tools-directory /Something/obviously/fake %s 2>&1 | %FileCheck -check-prefix BINUTILS %s
7+
8+
// CLANGSUB: swift
9+
// CLANGSUB-SAME: -o [[OBJECTFILE:.*]]
10+
// CLANGSUB: swift-autolink-extract [[OBJECTFILE]]
11+
// CLANGSUB-SAME: -o [[AUTOLINKFILE:.*]]
12+
// CLANGSUB: {{[^ ]+}}/Inputs/fake-toolchain/clang++
13+
// CLANGSUB-DAG: [[OBJECTFILE]]
14+
// CLANGSUB-DAG: @[[AUTOLINKFILE]]
15+
// CLANGSUB: -o tools_directory
16+
17+
// BINUTILS: swift
18+
// BINUTILS-SAME: -o [[OBJECTFILE:.*]]
19+
// BINUTILS: swift-autolink-extract [[OBJECTFILE]]
20+
// BINUTILS-SAME: -o [[AUTOLINKFILE:.*]]
21+
// BINUTILS: clang++
22+
// BINUTILS-DAG: [[OBJECTFILE]]
23+
// BINUTILS-DAG: @[[AUTOLINKFILE]]
24+
// BINUTILS-DAG: -B /Something/obviously/fake
25+
// BINUTILS: -o tools_directory
26+
27+
//======================================
28+
// ** DARWIN TARGETS - linking via ld **
29+
//======================================
30+
31+
// RUN: %swiftc_driver -### -target x86_64-apple-macosx10.9 -tools-directory %S/Inputs/fake-toolchain %s 2>&1 | %FileCheck -check-prefix LDSUB %s
32+
33+
// LDSUB: swift
34+
// LDSUB-SAME: -o [[OBJECTFILE:.*]]
35+
// LDSUB: {{[^ ]+}}/Inputs/fake-toolchain/ld [[OBJECTFILE]]
36+
// LDSUB: -o tools_directory

0 commit comments

Comments
 (0)