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
This commit is contained in:
Jordan Rose
2013-08-26 18:57:48 +00:00
parent 940d53c864
commit 22912bc3b3
7 changed files with 38 additions and 3 deletions

View File

@@ -279,13 +279,15 @@ public:
/// external references in a translation unit, which is one file.
class TranslationUnit : public Module {
private:
/// This is the list of modules that are imported by this module, with the
/// second element of the pair declaring whether the module is reexported.
///
/// This is filled in by the Name Binding phase.
ArrayRef<std::pair<ImportedModule, bool>> Imports;
/// The list of libraries specified as link-time dependencies at compile time.
ArrayRef<LinkLibrary> LinkLibraries;
public:
/// Kind - This is the sort of file the translation unit was parsed for, which
/// can affect some type checking and other behavior.
@@ -330,6 +332,14 @@ public:
Imports = IM;
}
void setLinkLibraries(ArrayRef<LinkLibrary> libs) {
assert(LinkLibraries.empty() && "link libraries already set");
LinkLibraries = libs;
}
ArrayRef<LinkLibrary> getLinkLibraries() const {
return LinkLibraries;
}
void clearLookupCache();
void cacheVisibleDecls(SmallVectorImpl<ValueDecl *> &&globals) const;

View File

@@ -22,6 +22,7 @@
#include "swift/Basic/LangOptions.h"
#include "swift/Basic/SourceManager.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/LinkLibrary.h"
#include "swift/AST/Module.h"
#include "swift/Parse/CodeCompletionCallbacks.h"
#include "swift/Parse/Parser.h"
@@ -44,6 +45,7 @@ class CompilerInvocation {
std::string ClangModuleCachePath;
std::vector<std::string> ImportSearchPaths;
std::vector<std::string> FrameworkSearchPaths;
SmallVector<LinkLibrary, 4> LinkLibraries;
std::string RuntimeIncludePath;
std::string SDKPath;
@@ -107,6 +109,14 @@ public:
return FrameworkSearchPaths;
}
void addLinkLibrary(StringRef name, LibraryKind kind) {
LinkLibraries.push_back({name, kind});
}
ArrayRef<LinkLibrary> getLinkLibraries() const {
return LinkLibraries;
}
void setMainExecutablePath(StringRef Path);
void setRuntimeIncludePath(StringRef Path) {

View File

@@ -15,6 +15,7 @@
//===----------------------------------------------------------------------===//
#include "swift/AST/Diagnostics.h"
#include "swift/AST/LinkLibrary.h"
#include "swift/AST/Module.h"
#include "swift/AST/ModuleLoader.h"
#include "swift/AST/NameLookup.h"
@@ -535,8 +536,9 @@ void Module::collectLinkLibraries(LinkLibraryCallback callback) {
return true;
}
if (isa<TranslationUnit>(module)) {
// FIXME: Should we include libraries specified by the user here?
if (auto TU = dyn_cast<TranslationUnit>(module)) {
for (auto lib : TU->getLinkLibraries())
callback(lib);
return true;
}

View File

@@ -123,6 +123,10 @@ bool CompilerInvocation::parseArgs(ArrayRef<const char *> Args,
case OPT_enable_definite_init:
LangOpts.UseDefiniteInit = true;
break;
case OPT_link_library:
addLinkLibrary(InputArg->getValue(), LibraryKind::Library);
break;
}
}

View File

@@ -124,6 +124,7 @@ void swift::CompilerInstance::doIt() {
Context->LoadedModules[ID.str()] = TU;
TU->HasBuiltinModuleAccess = Invocation.getParseStdlib();
TU->setLinkLibraries(Invocation.getLinkLibraries());
// If we're in SIL mode, don't auto import any libraries.
// Also don't perform auto import if we are not going to do semantic

View File

@@ -20,6 +20,9 @@ def parse_as_library : Flag<["-"], "parse-as-library">,
def parse_stdlib : Flag<["-"], "parse-stdlib">,
HelpText<"Parse the input as the swift standard library">;
def link_library : Joined<["-"], "l">,
HelpText<"Link the given library into the output product">;
// LangOptions
def debug_constraints : Flag<["-"], "debug-constraints">,
HelpText<"Debug the constraint-based type checker">;

View File

@@ -0,0 +1,5 @@
// RUN: %swift -emit-llvm -lmagic %s | FileCheck %s
// CHECK: !{{[0-9]+}} = metadata !{i32 6, metadata !"Linker Options", metadata ![[LINK_LIST:[0-9]+]]}
// CHECK: ![[LINK_LIST]] = metadata !{
// CHECK-DAG: !{{[0-9]+}} = metadata !{metadata !"-lmagic"}