-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
Changes from 7 commits
0b7369c
ff3498c
d0eb649
2af0bae
bdcac7f
2421f37
d82e47a
3463e5d
248b0f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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_library : Flag<["-"], "static">, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: the name of the option should match the name of the flag. (It'll get concatenated to |
||
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">; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
|
@@ -1371,10 +1373,15 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args, | |
break; | ||
|
||
case options::OPT_emit_library: | ||
OI.LinkAction = LinkKind::DynamicLibrary; | ||
OI.LinkAction = Args.hasArg(options::OPT_static_library) ? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's probably worth complaining when using |
||
LinkKind::StaticLibrary : | ||
LinkKind::DynamicLibrary; | ||
OI.CompilerOutputType = file_types::TY_Object; | ||
break; | ||
|
||
case options::OPT_static_library: | ||
break; | ||
|
||
case options::OPT_emit_object: | ||
OI.CompilerOutputType = file_types::TY_Object; | ||
break; | ||
|
@@ -1550,7 +1557,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. | ||
|
@@ -1949,8 +1957,14 @@ 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); | ||
jrose-apple marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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 | ||
|
@@ -2177,6 +2191,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; | ||
|
@@ -2192,6 +2219,7 @@ static StringRef baseNameForImage(const JobAction *JA, const OutputInfo &OI, | |
Buffer.append(".dll"); | ||
else | ||
Buffer.append(".so"); | ||
|
||
return Buffer.str(); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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_library); | ||
inputArgs.AddLastArg(arguments, options::OPT_swift_version); | ||
inputArgs.AddLastArg(arguments, options::OPT_enforce_exclusivity_EQ); | ||
inputArgs.AddLastArg(arguments, options::OPT_stats_output_dir); | ||
|
@@ -1078,6 +1079,74 @@ 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."); | ||
assert(job.getKind() == LinkKind::StaticLibrary); | ||
|
||
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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we actually want to prefer @cooperp, what's the current recommendation on this? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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." There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cooperp - thats the trick wrt There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Xcode doesn't ship with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, but it does ship with There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Formatting nitpick: no indentation for switch cases. |
||
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 { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably don't need this. I appreciate it being passed in to the constructor, though.