Skip to content

Commit 22912bc

Browse files
committed
Add a -l flag to Swift and use it to provide autolinking information.
The spelling of the flag can certainly be changed; I just wanted to get something up and running. Swift SVN r7582
1 parent 940d53c commit 22912bc

File tree

7 files changed

+38
-3
lines changed

7 files changed

+38
-3
lines changed

include/swift/AST/Module.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,15 @@ class Module : public DeclContext {
279279
/// external references in a translation unit, which is one file.
280280
class TranslationUnit : public Module {
281281
private:
282-
283282
/// This is the list of modules that are imported by this module, with the
284283
/// second element of the pair declaring whether the module is reexported.
285284
///
286285
/// This is filled in by the Name Binding phase.
287286
ArrayRef<std::pair<ImportedModule, bool>> Imports;
288287

288+
/// The list of libraries specified as link-time dependencies at compile time.
289+
ArrayRef<LinkLibrary> LinkLibraries;
290+
289291
public:
290292
/// Kind - This is the sort of file the translation unit was parsed for, which
291293
/// can affect some type checking and other behavior.
@@ -330,6 +332,14 @@ class TranslationUnit : public Module {
330332
Imports = IM;
331333
}
332334

335+
void setLinkLibraries(ArrayRef<LinkLibrary> libs) {
336+
assert(LinkLibraries.empty() && "link libraries already set");
337+
LinkLibraries = libs;
338+
}
339+
ArrayRef<LinkLibrary> getLinkLibraries() const {
340+
return LinkLibraries;
341+
}
342+
333343
void clearLookupCache();
334344

335345
void cacheVisibleDecls(SmallVectorImpl<ValueDecl *> &&globals) const;

include/swift/Frontend/Frontend.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/Basic/LangOptions.h"
2323
#include "swift/Basic/SourceManager.h"
2424
#include "swift/AST/DiagnosticEngine.h"
25+
#include "swift/AST/LinkLibrary.h"
2526
#include "swift/AST/Module.h"
2627
#include "swift/Parse/CodeCompletionCallbacks.h"
2728
#include "swift/Parse/Parser.h"
@@ -44,6 +45,7 @@ class CompilerInvocation {
4445
std::string ClangModuleCachePath;
4546
std::vector<std::string> ImportSearchPaths;
4647
std::vector<std::string> FrameworkSearchPaths;
48+
SmallVector<LinkLibrary, 4> LinkLibraries;
4749
std::string RuntimeIncludePath;
4850
std::string SDKPath;
4951

@@ -107,6 +109,14 @@ class CompilerInvocation {
107109
return FrameworkSearchPaths;
108110
}
109111

112+
void addLinkLibrary(StringRef name, LibraryKind kind) {
113+
LinkLibraries.push_back({name, kind});
114+
}
115+
116+
ArrayRef<LinkLibrary> getLinkLibraries() const {
117+
return LinkLibraries;
118+
}
119+
110120
void setMainExecutablePath(StringRef Path);
111121

112122
void setRuntimeIncludePath(StringRef Path) {

lib/AST/Module.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "swift/AST/Diagnostics.h"
18+
#include "swift/AST/LinkLibrary.h"
1819
#include "swift/AST/Module.h"
1920
#include "swift/AST/ModuleLoader.h"
2021
#include "swift/AST/NameLookup.h"
@@ -535,8 +536,9 @@ void Module::collectLinkLibraries(LinkLibraryCallback callback) {
535536
return true;
536537
}
537538

538-
if (isa<TranslationUnit>(module)) {
539-
// FIXME: Should we include libraries specified by the user here?
539+
if (auto TU = dyn_cast<TranslationUnit>(module)) {
540+
for (auto lib : TU->getLinkLibraries())
541+
callback(lib);
540542
return true;
541543
}
542544

lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ bool CompilerInvocation::parseArgs(ArrayRef<const char *> Args,
123123
case OPT_enable_definite_init:
124124
LangOpts.UseDefiniteInit = true;
125125
break;
126+
127+
case OPT_link_library:
128+
addLinkLibrary(InputArg->getValue(), LibraryKind::Library);
129+
break;
126130
}
127131
}
128132

lib/Frontend/Frontend.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ void swift::CompilerInstance::doIt() {
124124
Context->LoadedModules[ID.str()] = TU;
125125

126126
TU->HasBuiltinModuleAccess = Invocation.getParseStdlib();
127+
TU->setLinkLibraries(Invocation.getLinkLibraries());
127128

128129
// If we're in SIL mode, don't auto import any libraries.
129130
// Also don't perform auto import if we are not going to do semantic

lib/Frontend/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def parse_as_library : Flag<["-"], "parse-as-library">,
2020
def parse_stdlib : Flag<["-"], "parse-stdlib">,
2121
HelpText<"Parse the input as the swift standard library">;
2222

23+
def link_library : Joined<["-"], "l">,
24+
HelpText<"Link the given library into the output product">;
25+
2326
// LangOptions
2427
def debug_constraints : Flag<["-"], "debug-constraints">,
2528
HelpText<"Debug the constraint-based type checker">;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: %swift -emit-llvm -lmagic %s | FileCheck %s
2+
3+
// CHECK: !{{[0-9]+}} = metadata !{i32 6, metadata !"Linker Options", metadata ![[LINK_LIST:[0-9]+]]}
4+
// CHECK: ![[LINK_LIST]] = metadata !{
5+
// CHECK-DAG: !{{[0-9]+}} = metadata !{metadata !"-lmagic"}

0 commit comments

Comments
 (0)