Skip to content

Add '-emit-archive' option to mark modules as being part of static libraries #25088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ class alignas(1 << DeclAlignInBits) Decl {
HasAnyUnavailableValues : 1
);

SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1,
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1,
/// If the module was or is being compiled with `-enable-testing`.
TestingEnabled : 1,

Expand All @@ -624,7 +624,10 @@ class alignas(1 << DeclAlignInBits) Decl {
ImplicitDynamicEnabled : 1,

// Whether the module is a system module.
IsSystemModule : 1
IsSystemModule : 1,

// If the module is a static library.
IsStaticLibrary : 1
);

SWIFT_INLINE_BITFIELD(PrecedenceGroupDecl, Decl, 1+2,
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsDriver.def
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ ERROR(error_expected_frontend_command,none,
ERROR(error_cannot_specify__o_for_multiple_outputs,none,
"cannot specify -o when generating multiple output files", ())

ERROR(error_static_emit_executable_disallowed,none,
"-static may not be used with -emit-executable", ())

ERROR(error_unable_to_load_output_file_map, none,
"unable to load output file map '%1': %0", (StringRef, StringRef))

Expand Down
8 changes: 8 additions & 0 deletions include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,14 @@ class ModuleDecl : public DeclContext, public TypeDecl {
return getResilienceStrategy() != ResilienceStrategy::Default;
}

bool isStaticLibrary() const {
return Bits.ModuleDecl.IsStaticLibrary;
}

void setIsStaticLibrary(bool isStaticLibrary = true) {
Bits.ModuleDecl.IsStaticLibrary = isStaticLibrary;
}

/// Look up a (possibly overloaded) value set at top-level scope
/// (but with the specified access path, which may come from an import decl)
/// within the current module.
Expand Down
17 changes: 16 additions & 1 deletion include/swift/Driver/Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Action {
AutolinkExtractJob,
REPLJob,
LinkJob,
ArchiveJob,
GenerateDSYMJob,
VerifyDebugInfoJob,
GeneratePCHJob,
Expand Down Expand Up @@ -313,7 +314,7 @@ class LinkJobAction : public JobAction {
LinkJobAction(ArrayRef<const Action *> Inputs, LinkKind K)
: JobAction(Action::Kind::LinkJob, Inputs, file_types::TY_Image),
Kind(K) {
assert(Kind != LinkKind::None);
assert(Kind != LinkKind::None && Kind != LinkKind::StaticLibrary);
}

LinkKind getKind() const { return Kind; }
Expand All @@ -323,6 +324,20 @@ class LinkJobAction : public JobAction {
}
};

class ArchiveJobAction : public JobAction {
virtual void anchor();

public:
ArchiveJobAction(ArrayRef<const Action *> Inputs, LinkKind K)
: JobAction(Action::Kind::ArchiveJob, Inputs, file_types::TY_Image) {
assert(K == LinkKind::StaticLibrary);
}

static bool classof(const Action *A) {
return A->getKind() == Action::Kind::ArchiveJob;
}
};

} // end namespace driver
} // end namespace swift

Expand Down
3 changes: 3 additions & 0 deletions include/swift/Driver/ToolChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ class ToolChain {
virtual InvocationInfo constructInvocation(const LinkJobAction &job,
const JobContext &context) const;

virtual InvocationInfo constructInvocation(const ArchiveJobAction &job,
const JobContext &context) const;

/// Searches for the given executable in appropriate paths relative to the
/// Swift binary.
///
Expand Down
3 changes: 2 additions & 1 deletion include/swift/Driver/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ namespace driver {
enum class LinkKind {
None,
Executable,
DynamicLibrary
DynamicLibrary,
StaticLibrary
};

/// Used by a Job to request a "filelist": a file containing a list of all
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ class FrontendOptions {
/// the Objective-C half should implicitly be visible to the Swift sources.
bool ImportUnderlyingModule = false;

/// If set, this module should be built and linked as a static library.
bool IsStaticLibrary = false;

/// If set, the header provided in ImplicitObjCHeaderPath will be rewritten
/// by the Clang importer as part of semantic analysis.
bool SerializeBridgingHeader = false;
Expand Down
2 changes: 1 addition & 1 deletion include/swift/IRGen/Linking.h
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ class LinkInfo {

static LinkInfo get(const UniversalLinkageInfo &linkInfo, StringRef name,
SILLinkage linkage, ForDefinition_t isDefinition,
bool isWeakImported);
bool isWeakImported, bool isStaticLibrary);

StringRef getName() const {
return Name.str();
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ def emit_objc_header_path : Separate<["-"], "emit-objc-header-path">,
ArgumentIsPath]>,
MetaVarName<"<path>">, HelpText<"Emit an Objective-C header file to <path>">;

def static : Flag<["-"], "static">,
Flags<[FrontendOption, ModuleInterfaceOption, NoInteractiveOption]>,
HelpText<"Make this module statically linkable and make the output of -emit-library a static library.">;

def import_cf_types : Flag<["-"], "import-cf-types">,
Flags<[FrontendOption, HelpHidden]>,
HelpText<"Recognize and import CF types as class types">;
Expand Down
7 changes: 6 additions & 1 deletion include/swift/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,8 @@ namespace options_block {
IS_SIB,
IS_TESTABLE,
RESILIENCE_STRATEGY,
ARE_PRIVATE_IMPORTS_ENABLED
ARE_PRIVATE_IMPORTS_ENABLED,
IS_STATIC_LIBRARY
};

using SDKPathLayout = BCRecordLayout<
Expand Down Expand Up @@ -630,6 +631,10 @@ namespace options_block {
RESILIENCE_STRATEGY,
BCFixed<2>
>;

using IsStaticLibraryLayout = BCRecordLayout<
IS_STATIC_LIBRARY
>;
}

/// The record types within the input block.
Expand Down
6 changes: 6 additions & 0 deletions include/swift/Serialization/Validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class ExtendedValidationInfo {
unsigned IsSIB : 1;
unsigned IsTestable : 1;
unsigned ResilienceStrategy : 2;
unsigned IsStaticLibrary : 1;
} Bits;
public:
ExtendedValidationInfo() : Bits() {}
Expand Down Expand Up @@ -135,6 +136,11 @@ class ExtendedValidationInfo {
void setResilienceStrategy(ResilienceStrategy resilience) {
Bits.ResilienceStrategy = unsigned(resilience);
}

bool isStaticLibrary() const { return Bits.IsStaticLibrary; }
void setIsStaticLibrary(bool isStaticLibrary) {
Bits.IsStaticLibrary = isStaticLibrary;
}
};

/// Returns info about the serialized AST in the given data.
Expand Down
3 changes: 3 additions & 0 deletions lib/Driver/Action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const char *Action::getClassName(Kind AC) {
case Kind::AutolinkExtractJob: return "swift-autolink-extract";
case Kind::REPLJob: return "repl";
case Kind::LinkJob: return "link";
case Kind::ArchiveJob: return "archive";
case Kind::GenerateDSYMJob: return "generate-dSYM";
case Kind::VerifyDebugInfoJob: return "verify-debug-info";
case Kind::GeneratePCHJob: return "generate-pch";
Expand Down Expand Up @@ -57,6 +58,8 @@ void REPLJobAction::anchor() {}

void LinkJobAction::anchor() {}

void ArchiveJobAction::anchor() {}

void GenerateDSYMJobAction::anchor() {}

void VerifyDebugInfoJobAction::anchor() {}
Expand Down
2 changes: 2 additions & 0 deletions lib/Driver/DarwinToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,
case LinkKind::DynamicLibrary:
Arguments.push_back("-dylib");
break;
case LinkKind::StaticLibrary:
llvm_unreachable("invalid link kind");
}

assert(Triple.isOSDarwin());
Expand Down
41 changes: 37 additions & 4 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,8 @@ static bool maybeBuildingExecutable(const OutputInfo &OI,
return true;
case LinkKind::DynamicLibrary:
return false;
case LinkKind::StaticLibrary:
return false;
case LinkKind::None:
break;
}
Expand Down Expand Up @@ -1366,15 +1368,24 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,

switch (OutputModeArg->getOption().getID()) {
case options::OPT_emit_executable:
if (Args.hasArg(options::OPT_static))
Diags.diagnose(SourceLoc(),
diag::error_static_emit_executable_disallowed);

OI.LinkAction = LinkKind::Executable;
OI.CompilerOutputType = file_types::TY_Object;
break;

case options::OPT_emit_library:
OI.LinkAction = LinkKind::DynamicLibrary;
OI.LinkAction = Args.hasArg(options::OPT_static) ?
LinkKind::StaticLibrary :
LinkKind::DynamicLibrary;
OI.CompilerOutputType = file_types::TY_Object;
break;

case options::OPT_static:
break;

case options::OPT_emit_object:
OI.CompilerOutputType = file_types::TY_Object;
break;
Expand Down Expand Up @@ -1550,7 +1561,8 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
OI.ModuleName = "REPL";
} else if (const Arg *A = Args.getLastArg(options::OPT_o)) {
OI.ModuleName = llvm::sys::path::stem(A->getValue());
if (OI.LinkAction == LinkKind::DynamicLibrary &&
if ((OI.LinkAction == LinkKind::DynamicLibrary ||
OI.LinkAction == LinkKind::StaticLibrary) &&
!llvm::sys::path::extension(A->getValue()).empty() &&
StringRef(OI.ModuleName).startswith("lib")) {
// Chop off a "lib" prefix if we're building a library.
Expand Down Expand Up @@ -1949,8 +1961,15 @@ void Driver::buildActions(SmallVectorImpl<const Action *> &TopLevelActions,
}

if (OI.shouldLink() && !AllLinkerInputs.empty()) {
auto *LinkAction = C.createAction<LinkJobAction>(AllLinkerInputs,
OI.LinkAction);
JobAction *LinkAction = nullptr;

if (OI.LinkAction == LinkKind::StaticLibrary) {
LinkAction = C.createAction<ArchiveJobAction>(AllLinkerInputs,
OI.LinkAction);
} else {
LinkAction = C.createAction<LinkJobAction>(AllLinkerInputs,
OI.LinkAction);
}

// On ELF platforms there's no built in autolinking mechanism, so we
// pull the info we need from the .o files directly and pass them as an
Expand Down Expand Up @@ -2177,6 +2196,19 @@ static StringRef baseNameForImage(const JobAction *JA, const OutputInfo &OI,
StringRef BaseInput, StringRef BaseName) {
if (JA->size() == 1 && OI.ModuleNameIsFallback && BaseInput != "-")
return llvm::sys::path::stem(BaseInput);

if (auto link = dyn_cast<ArchiveJobAction>(JA)) {
Buffer = Triple.isOSWindows() ? "" : "lib";
Buffer.append(BaseName);

if (Triple.isOSWindows())
Buffer.append(".lib");
else
Buffer.append(".a");

return Buffer.str();
}

auto link = dyn_cast<LinkJobAction>(JA);
if (!link)
return BaseName;
Expand All @@ -2192,6 +2224,7 @@ static StringRef baseNameForImage(const JobAction *JA, const OutputInfo &OI,
Buffer.append(".dll");
else
Buffer.append(".so");

return Buffer.str();
}

Expand Down
1 change: 1 addition & 0 deletions lib/Driver/ToolChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ std::unique_ptr<Job> ToolChain::constructJob(
CASE(MergeModuleJob)
CASE(ModuleWrapJob)
CASE(LinkJob)
CASE(ArchiveJob)
CASE(GenerateDSYMJob)
CASE(VerifyDebugInfoJob)
CASE(GeneratePCHJob)
Expand Down
68 changes: 68 additions & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ static void addCommonFrontendArgs(const ToolChain &TC, const OutputInfo &OI,
inputArgs.AddLastArg(arguments, options::OPT_warnings_as_errors);
inputArgs.AddLastArg(arguments, options::OPT_sanitize_EQ);
inputArgs.AddLastArg(arguments, options::OPT_sanitize_coverage_EQ);
inputArgs.AddLastArg(arguments, options::OPT_static);
inputArgs.AddLastArg(arguments, options::OPT_swift_version);
inputArgs.AddLastArg(arguments, options::OPT_enforce_exclusivity_EQ);
inputArgs.AddLastArg(arguments, options::OPT_stats_output_dir);
Expand Down Expand Up @@ -1078,6 +1079,73 @@ ToolChain::constructInvocation(const LinkJobAction &job,
llvm_unreachable("linking not implemented for this toolchain");
}

ToolChain::InvocationInfo
ToolChain::constructInvocation(const ArchiveJobAction &job,
const JobContext &context) const {
assert(context.Output.getPrimaryOutputType() == file_types::TY_Image &&
"Invalid linker output type.");

ArgStringList Arguments;

// Configure the toolchain.
bool isLLVMAR = false;
const char *AR = nullptr;
if (const Arg *A = context.Args.getLastArg(options::OPT_tools_directory)) {
StringRef toolchainPath(A->getValue());

// If there is an llvm-ar in the toolchain folder, use that instead.
if (auto toolchainAR =
llvm::sys::findProgramByName("llvm-ar", {toolchainPath})) {
AR = context.Args.MakeArgString(toolchainAR.get());
isLLVMAR = true;
}

}
if (AR == nullptr) {
if (auto pathAR = llvm::sys::findProgramByName("llvm-ar", None)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we actually want to prefer llvm-ar like this, and I'm not sure whether this belongs in the default ToolChain either (rather than GenericUnix). On Apple platforms, libtool -static is what Xcode uses to create static libraries, not ar.

@cooperp, what's the current recommendation on this? ar or libtool for static libraries on Apple platforms?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's libtool. Otherwise you tend to have to call ranlib after ar, while the man page for libtool says it can handle both for you: "Libtool with -static is intended to replace ar(5) and ranlib."

Copy link
Contributor Author

@troughton troughton May 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@compnerd, is what’s here what we want on both GenericUnix and Windows, or should the behaviour differ? If llvm-ar is unavailable on Windows should we fall back to LIB.exe?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cooperp - thats the trick wrt ar crs - it creates the index as part of the invocation. With llvm-ar that also means that we can invoke it as ar --format bsd crs ... which will create the BSD style archive with the index even on a non-BSD host.

Copy link
Contributor

@jrose-apple jrose-apple Jun 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Xcode doesn't ship with llvm-ar. Please stick to libtool on Darwin. (In general, please stick to "what Xcode does when you make a static library from a template".)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but it does ship with ar from cctools, which also supports the crs mode. The problem is that you lose compatibility with AT&T Unix, which I believe didn't support the s option.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. I'll let @cooperp decide what's right here. We already aren't getting code sharing because the Darwin toolchain deliberately isn't a GenericUnix toolchain, on account of Apple doing weird things having more control over the tools it ships.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cooperp, what do you think is best here?

AR = context.Args.MakeArgString(pathAR.get());
isLLVMAR = true;
}
}
if (AR == nullptr) {
if (auto pathAR = llvm::sys::findProgramByName("ar", None))
AR = context.Args.MakeArgString(pathAR.get());
}

assert(AR &&
"neither ar nor llvm-ar was not found in the toolchain directory or system path.");

if (isLLVMAR) {
switch (getTriple().getOS()) {
case llvm::Triple::Darwin:
Arguments.push_back("--format=darwin");
break;
case llvm::Triple::FreeBSD:
Arguments.push_back("--format=bsd");
break;
case llvm::Triple::Linux:
Arguments.push_back("--format=gnu");
break;
default:
break;
}
}

Arguments.push_back("crs");

Arguments.push_back(
context.Args.MakeArgString(context.Output.getPrimaryOutputFilename()));

addPrimaryInputsOfType(Arguments, context.Inputs, context.Args,
file_types::TY_Object);
addInputsOfType(Arguments, context.InputActions, file_types::TY_Object);

InvocationInfo II{AR, Arguments};
II.allowsResponseFiles = true;

return II;
}

void ToolChain::addPathEnvironmentVariableIfNeeded(
Job::EnvironmentVector &env, const char *name, const char *separator,
options::ID optionID, const ArgList &args, StringRef extraEntry) const {
Expand Down
2 changes: 2 additions & 0 deletions lib/Driver/UnixToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
case LinkKind::DynamicLibrary:
Arguments.push_back("-shared");
break;
case LinkKind::StaticLibrary:
llvm_unreachable("invalid link kind");
}

// Select the linker to use.
Expand Down
2 changes: 2 additions & 0 deletions lib/Driver/WindowsToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ toolchains::Windows::constructInvocation(const LinkJobAction &job,
case LinkKind::DynamicLibrary:
Arguments.push_back("-shared");
break;
case LinkKind::StaticLibrary:
llvm_unreachable("invalid link kind");
}

// Select the linker to use.
Expand Down
Loading