Skip to content

Commit f05dee0

Browse files
authored
Begin adding support for using swiftc as a linker driver (#236)
This is controlled via the LINKER_DRIVER setting
1 parent f32a63b commit f05dee0

File tree

10 files changed

+275
-21
lines changed

10 files changed

+275
-21
lines changed

Sources/SWBCore/Settings/BuiltinMacros.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ public final class BuiltinMacros {
822822
public static let LIBTOOL_DEPENDENCY_INFO_FILE = BuiltinMacros.declarePathMacro("LIBTOOL_DEPENDENCY_INFO_FILE")
823823
public static let LIBTOOL_USE_RESPONSE_FILE = BuiltinMacros.declareBooleanMacro("LIBTOOL_USE_RESPONSE_FILE")
824824
public static let LINKER = BuiltinMacros.declareStringMacro("LINKER")
825+
public static let LINKER_DRIVER = BuiltinMacros.declareEnumMacro("LINKER_DRIVER") as EnumMacroDeclaration<LinkerDriverChoice>
825826
public static let ALTERNATE_LINKER = BuiltinMacros.declareStringMacro("ALTERNATE_LINKER")
826827
public static let LINK_OBJC_RUNTIME = BuiltinMacros.declareBooleanMacro("LINK_OBJC_RUNTIME")
827828
public static let LINK_WITH_STANDARD_LIBRARIES = BuiltinMacros.declareBooleanMacro("LINK_WITH_STANDARD_LIBRARIES")
@@ -1371,6 +1372,7 @@ public final class BuiltinMacros {
13711372
ALL_OTHER_LDFLAGS,
13721373
ALL_SETTINGS,
13731374
ALTERNATE_GROUP,
1375+
LINKER_DRIVER,
13741376
ALTERNATE_LINKER,
13751377
ALTERNATE_MODE,
13761378
ALTERNATE_OWNER,
@@ -2662,6 +2664,13 @@ public enum ModuleVerifierKind: String, Equatable, Hashable, EnumerationMacroTyp
26622664
case both
26632665
}
26642666

2667+
public enum LinkerDriverChoice: String, Equatable, Hashable, EnumerationMacroType {
2668+
public static let defaultValue: LinkerDriverChoice = .clang
2669+
2670+
case clang
2671+
case swiftc
2672+
}
2673+
26652674
/// Enumeration macro type for the value of the `INFOPLIST_KEY_LSApplicationCategoryType` build setting.
26662675
public enum ApplicationCategory: String, Equatable, Hashable, EnumerationMacroType {
26672676
public static let defaultValue = ApplicationCategory.none

Sources/SWBCore/SpecImplementations/ProductTypes.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,22 @@ public class ProductTypeSpec : Spec, SpecType, @unchecked Sendable {
270270
if producer.isApplePlatform {
271271
let compatibilityVersion = scope.evaluate(BuiltinMacros.DYLIB_COMPATIBILITY_VERSION)
272272
if !compatibilityVersion.isEmpty {
273-
args += ["-compatibility_version", compatibilityVersion]
273+
switch scope.evaluate(BuiltinMacros.LINKER_DRIVER) {
274+
case .clang:
275+
args += ["-compatibility_version", compatibilityVersion]
276+
case .swiftc:
277+
args += ["-Xlinker", "-compatibility_version", "-Xlinker", compatibilityVersion]
278+
}
274279
}
275280

276281
let currentVersion = scope.evaluate(BuiltinMacros.DYLIB_CURRENT_VERSION)
277282
if !currentVersion.isEmpty {
278-
args += ["-current_version", currentVersion]
283+
switch scope.evaluate(BuiltinMacros.LINKER_DRIVER) {
284+
case .clang:
285+
args += ["-current_version", currentVersion]
286+
case .swiftc:
287+
args += ["-Xlinker", "-current_version", "-Xlinker", currentVersion]
288+
}
279289
}
280290
}
281291

Sources/SWBCore/SpecImplementations/PropertyDomainSpec.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ private final class EnumBuildOptionType : BuildOptionType {
110110
return try namespace.declareEnumMacro(name) as EnumMacroDeclaration<SwiftEnableExplicitModulesSetting>
111111
case "SWIFT_ENABLE_EXPLICIT_MODULES":
112112
return try namespace.declareEnumMacro(name) as EnumMacroDeclaration<SwiftEnableExplicitModulesSetting>
113+
case "LINKER_DRIVER":
114+
return try namespace.declareEnumMacro(name) as EnumMacroDeclaration<LinkerDriverChoice>
113115
default:
114116
return try namespace.declareStringMacro(name)
115117
}

Sources/SWBCore/SpecImplementations/Tools/LinkerTools.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,13 @@ public final class LdLinkerSpec : GenericLinkerSpec, SpecIdentifierType, @unchec
234234
public static let identifier = "com.apple.pbx.linkers.ld"
235235

236236
public override func computeExecutablePath(_ cbc: CommandBuildContext) -> String {
237-
return cbc.producer.hostOperatingSystem.imageFormat.executableName(basename: "clang")
237+
// TODO: We should also provide an "auto" option which chooses based on the source files in the target
238+
switch cbc.scope.evaluate(BuiltinMacros.LINKER_DRIVER) {
239+
case .clang:
240+
return cbc.producer.hostOperatingSystem.imageFormat.executableName(basename: "clang")
241+
case .swiftc:
242+
return cbc.producer.hostOperatingSystem.imageFormat.executableName(basename: "swiftc")
243+
}
238244
}
239245

240246
override public var toolBasenameAliases: [String] {

Sources/SWBGenericUnixPlatform/Specs/UnixLd.xcspec

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
};
2727
Options = (
2828
{
29-
Name = "MACH_O_TYPE";
29+
Name = "CLANG_MACH_O_TYPE";
3030
Type = Enumeration;
3131
Values = (
3232
{
@@ -46,13 +46,26 @@
4646
CommandLineFlag = "-r";
4747
},
4848
);
49+
Condition = "$(LINKER_DRIVER) == clang";
50+
DefaultValue = "$(MACH_O_TYPE)";
4951
},
52+
// We can inherit SWIFTC_MACHO_TYPE from the universal platform Ld.xcspec
5053
{
51-
Name = SDKROOT;
54+
Name = CLANG_SDKROOT_LINKER_INPUT;
5255
Type = Path;
56+
DefaultValue = "$(SDKROOT)";
57+
Condition = "$(LINKER_DRIVER) == clang";
5358
CommandLineFlag = "--sysroot";
5459
IsInputDependency = Yes;
5560
},
61+
{
62+
Name = SWIFTC_SDKROOT_LINKER_INPUT;
63+
Type = Path;
64+
DefaultValue = "$(SDKROOT)";
65+
Condition = "$(LINKER_DRIVER) == swiftc";
66+
CommandLineFlag = "-sysroot";
67+
IsInputDependency = Yes;
68+
},
5669
{
5770
Name = "LD_DYLIB_INSTALL_NAME";
5871
Type = String;
@@ -65,6 +78,17 @@
6578
);
6679
Condition = "$(MACH_O_TYPE) == mh_dylib";
6780
},
81+
// Override the differentiated settings to no-ops, both linker drivers use the same flags.
82+
{
83+
Name = "CLANG_LD_DYLIB_INSTALL_NAME";
84+
Type = String;
85+
Condition = "NO";
86+
},
87+
{
88+
Name = "SWIFTC_LD_DYLIB_INSTALL_NAME";
89+
Type = String;
90+
Condition = "NO";
91+
},
6892
{
6993
Name = GOLD_BUILDID;
7094
Type = Boolean;
@@ -130,6 +154,17 @@
130154
);
131155
IsInputDependency = Yes;
132156
},
157+
// Override the differentiated settings to no-ops, both linker drivers use the same flags.
158+
{
159+
Name = "CLANG__INPUT_FILE_LIST_PATH__";
160+
Type = Path;
161+
Condition = "NO";
162+
},
163+
{
164+
Name = "SWIFTC__INPUT_FILE_LIST_PATH__";
165+
Type = Path;
166+
Condition = "NO";
167+
}
133168
);
134169
},
135170
)

Sources/SWBUniversalPlatform/Specs/Clang LLVM 1.0.xcspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@
320320
// that the ObjC runtime must be linked in (with possible
321321
// backwards compatibility libraries linked in).
322322
AdditionalLinkerArgs = {
323-
YES = ( "-fobjc-link-runtime" );
323+
YES = ( "$(LD_OBJC_RUNTIME_ARGS)" );
324324
NO = ();
325325
};
326326
FileTypes = (

0 commit comments

Comments
 (0)