-
Notifications
You must be signed in to change notification settings - Fork 127
Change how libraries are specified to the linker when using searched libs #890
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -1289,41 +1289,15 @@ public final class LdLinkerSpec : GenericLinkerSpec, SpecIdentifierType, @unchec | |
| private static func computeLibraryArgs(_ libraries: [LibrarySpecifier], scope: MacroEvaluationScope) -> (args: [String], inputs: [Path]) { | ||
| // Construct the library arguments. | ||
| return libraries.compactMap { specifier -> (args: [String], inputs: [Path]) in | ||
| let basename = specifier.path.basename | ||
|
|
||
| // FIXME: This isn't a good system, we need to redesign how we talk to the linker w.r.t. search paths and our notion of paths. | ||
| switch specifier.kind { | ||
| case .static: | ||
| if specifier.useSearchPaths, basename.hasPrefix("lib"), basename.hasSuffix(".a") { | ||
| return (specifier.searchPathFlagsForLd(basename.withoutPrefix("lib").withoutSuffix(".a")), []) | ||
| } | ||
| return (specifier.absolutePathFlagsForLd(), [specifier.path]) | ||
| case .dynamic: | ||
| let suffix = ".\(scope.evaluate(BuiltinMacros.DYNAMIC_LIBRARY_EXTENSION))" | ||
| if specifier.useSearchPaths, basename.hasPrefix("lib"), basename.hasSuffix(suffix) { | ||
| return (specifier.searchPathFlagsForLd(basename.withoutPrefix("lib").withoutSuffix(suffix)), []) | ||
| } | ||
| return (specifier.absolutePathFlagsForLd(), [specifier.path]) | ||
| case .textBased: | ||
| if specifier.useSearchPaths, basename.hasPrefix("lib"), basename.hasSuffix(".tbd") { | ||
| // .merge and .reexport are not supported for text-based libraries. | ||
| return (specifier.searchPathFlagsForLd(basename.withoutPrefix("lib").withoutSuffix(".tbd")), []) | ||
| } | ||
| return (specifier.absolutePathFlagsForLd(), [specifier.path]) | ||
| case .framework: | ||
| let frameworkName = Path(basename).withoutSuffix | ||
| case .static, .dynamic, .textBased, .framework: | ||
| if specifier.useSearchPaths { | ||
| return (specifier.searchPathFlagsForLd(frameworkName), []) | ||
| } | ||
| let absPathArgs = specifier.absolutePathFlagsForLd() | ||
| let returnPath: Path | ||
| if let pathArg = absPathArgs.last, Path(pathArg).basename == frameworkName { | ||
| returnPath = Path(pathArg) | ||
| } | ||
| else { | ||
| returnPath = specifier.path | ||
| let args = specifier.searchPathFlagsForLd() | ||
| if !args.isEmpty { // no search args, fallback to absolute path to library | ||
| return (args, []) | ||
| } | ||
| } | ||
| return (absPathArgs, [returnPath]) | ||
| return (specifier.absolutePathFlagsForLd(), [specifier.path]) | ||
| case .object: | ||
| // Object files are added to linker inputs in the sources task producer. | ||
| return ([], []) | ||
|
|
@@ -1559,35 +1533,44 @@ public final class LdLinkerSpec : GenericLinkerSpec, SpecIdentifierType, @unchec | |
|
|
||
| /// Extensions to `LinkerSpec.LibrarySpecifier` specific to the dynamic linker. | ||
| fileprivate extension LinkerSpec.LibrarySpecifier { | ||
| func searchPathFlagsForLd(_ name: String) -> [String] { | ||
| func searchPathFlagsForLd() -> [String] { | ||
| let strippedName: String | ||
|
Collaborator
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. Same question here, should we warn if we get a filename format we don't recognize here / don't think will be found?
Contributor
Author
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. Hmm, rethinking this.... I think we need to fallback to using absolute path linking when that happens, this would be the same as how it was before....
Contributor
Author
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. There are also tests that would have this warning (and fail due to it), but since we will fallback to an absolute path, not sure the warning it necessary now.
Collaborator
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. falling back w/ no warning makes sense to me |
||
| if let prefix = libPrefix, path.basename.hasPrefix(prefix) { | ||
| strippedName = Path(path.basename).withoutSuffix.withoutPrefix(prefix) | ||
| } else { | ||
| if libPrefix != nil { // we need a prefix for linking with search paths | ||
| return [] // this will fallback to using absolute paths | ||
| } | ||
| strippedName = Path(path.basename).withoutSuffix | ||
| } | ||
| switch (kind, mode) { | ||
| case (.dynamic, .normal): | ||
| return ["-l" + name] | ||
| return ["-l" + strippedName] | ||
| case (.dynamic, .reexport): | ||
| return ["-Xlinker", "-reexport-l" + name] | ||
| return ["-Xlinker", "-reexport-l" + strippedName] | ||
| case (.dynamic, .merge): | ||
| return ["-Xlinker", "-merge-l" + name] | ||
| return ["-Xlinker", "-merge-l" + strippedName] | ||
| case (.dynamic, .reexport_merge): | ||
| return ["-Xlinker", "-no_merge-l" + name] | ||
| return ["-Xlinker", "-no_merge-l" + strippedName] | ||
| case (.dynamic, .weak): | ||
| return ["-weak-l" + name] | ||
| return ["-weak-l" + strippedName] | ||
| case (.static, .weak), | ||
| (.textBased, .weak): | ||
| return ["-weak-l" + name] | ||
| return ["-weak-l" + strippedName] | ||
| case (.static, _), | ||
| (.textBased, _): | ||
| // Other modes are not supported for these kinds. | ||
| return ["-l" + name] | ||
| return ["-l" + strippedName] | ||
| case (.framework, .normal): | ||
| return ["-framework", name] | ||
| return ["-framework", strippedName] | ||
| case (.framework, .reexport): | ||
| return ["-Xlinker", "-reexport_framework", "-Xlinker", name] | ||
| return ["-Xlinker", "-reexport_framework", "-Xlinker", strippedName] | ||
| case (.framework, .merge): | ||
| return ["-Xlinker", "-merge_framework", "-Xlinker", name] | ||
| return ["-Xlinker", "-merge_framework", "-Xlinker", strippedName] | ||
| case (.framework, .reexport_merge): | ||
| return ["-Xlinker", "-no_merge_framework", "-Xlinker", name] | ||
| return ["-Xlinker", "-no_merge_framework", "-Xlinker", strippedName] | ||
| case (.framework, .weak): | ||
| return ["-weak_framework", name] | ||
| return ["-weak_framework", strippedName] | ||
| case (.object, _): | ||
| // Object files are added to linker inputs in the sources task producer. | ||
| return [] | ||
|
|
@@ -1724,15 +1707,17 @@ public final class LibtoolLinkerSpec : GenericLinkerSpec, SpecIdentifierType, @u | |
| delegate.warning("Product \(cbc.output.basename) cannot weak-link \(specifier.kind) \(basename)") | ||
| } | ||
|
|
||
| if specifier.useSearchPaths, basename.hasPrefix("lib"), basename.hasSuffix(".a") { | ||
| if specifier.useSearchPaths { | ||
|
Collaborator
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. Should we emit some kind of warning here if the library name doesn't have one of the prefixes we think will allow it to be found via search paths? |
||
| // Locate using search paths: Add a -l option and *don't* add the path to the library as an input to the task. | ||
| return ["-l" + basename.withoutPrefix("lib").withoutSuffix(".a")] | ||
| } | ||
| else { | ||
| // Locate using an absolute path: Add the path as an option and as an input to the task. | ||
| inputPaths.append(specifier.path) | ||
| return [specifier.path.str] | ||
| let basename = specifier.path.basename | ||
| let expectedPrefix = specifier.libPrefix ?? "lib" | ||
| if basename.hasPrefix(expectedPrefix) { | ||
| return ["-l" + Path(basename).withoutSuffix.withoutPrefix(expectedPrefix)] | ||
| } | ||
| } | ||
| // Locate using an absolute path: Add the path as an option and as an input to the task. | ||
| inputPaths.append(specifier.path) | ||
| return [specifier.path.str] | ||
|
|
||
| case .object: | ||
| // Object files are added to linker inputs in the sources task producer and so end up in the link-file-list. | ||
|
|
||
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.
Why do we only consider the first element here?
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.
Ya, I was thinking about that and I just couldn't think of a case where there would ever be more that one so I forego the extract logic of handling multiple and just went with the lazy handle one method. Do you feel its worth handling multiple?
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.
Only handling one sounds ok to me, but I think we should then either only allow one in the spec or error out if we parse more than one