Skip to content

Commit 8c23c59

Browse files
committed
Begin adding support for using swiftc as a linker driver
This is controlled via the LINKER_DRIVER setting
1 parent e0befdc commit 8c23c59

File tree

10 files changed

+281
-16
lines changed

10 files changed

+281
-16
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 = (

Sources/SWBUniversalPlatform/Specs/Ld.xcspec

Lines changed: 148 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@
3838
CommandOutputParser = "XCGccCommandOutputParser";
3939
"SupportsInputFileList" = Yes;
4040
Options = (
41+
{
42+
Name = "LINKER_DRIVER";
43+
Type = Enumeration;
44+
Values = (
45+
clang,
46+
swiftc,
47+
);
48+
DefaultValue = "clang";
49+
},
4150
{ Name = LD_DETERMINISTIC_MODE;
4251
Type = Boolean;
4352
DefaultValue = YES;
@@ -72,6 +81,24 @@
7281
{
7382
Name = "MACH_O_TYPE";
7483
Type = Enumeration;
84+
Values = (
85+
{
86+
Value = "mh_execute";
87+
},
88+
{
89+
Value = "mh_dylib";
90+
},
91+
{
92+
Value = "mh_bundle";
93+
},
94+
{
95+
Value = "mh_object";
96+
},
97+
);
98+
},
99+
{
100+
Name = "CLANG_MACH_O_TYPE";
101+
Type = Enumeration;
75102
Values = (
76103
{
77104
Value = "mh_execute";
@@ -90,24 +117,87 @@
90117
CommandLineFlag = "-r";
91118
},
92119
);
120+
Condition = "$(LINKER_DRIVER) == clang";
121+
DefaultValue = "$(MACH_O_TYPE)";
122+
},
123+
{
124+
Name = "SWIFTC_MACH_O_TYPE";
125+
Type = Enumeration;
126+
Values = (
127+
{
128+
Value = "mh_execute";
129+
CommandLineFlag = "-emit-executable";
130+
},
131+
{
132+
Value = "mh_dylib";
133+
CommandLineFlag = "-emit-library";
134+
},
135+
{
136+
Value = "mh_bundle";
137+
CommandLineArgs = ("-Xlinker", "-bundle");
138+
},
139+
{
140+
Value = "mh_object";
141+
CommandLineArgs = ("-Xlinker", "-r");
142+
},
143+
);
144+
Condition = "$(LINKER_DRIVER) == swiftc";
145+
DefaultValue = "$(MACH_O_TYPE)";
146+
},
147+
{
148+
Name = LD_OBJC_RUNTIME_ARGS;
149+
DefaultValue = "$(LD_OBJC_RUNTIME_ARGS_$(LINKER_DRIVER))";
150+
},
151+
{
152+
Name = LD_OBJC_RUNTIME_ARGS_clang;
153+
DefaultValue = "-fobjc-link-runtime";
154+
},
155+
{
156+
Name = LD_OBJC_RUNTIME_ARGS_swiftc;
157+
DefaultValue = "-link-objc-runtime";
93158
},
94159
{
95-
Name = SDKROOT;
160+
Name = CLANG_SDKROOT_LINKER_INPUT;
96161
Type = Path;
162+
DefaultValue = "$(SDKROOT)";
163+
Condition = "$(LINKER_DRIVER) == clang";
97164
CommandLineFlag = "-isysroot";
98165
IsInputDependency = Yes;
99166
},
100-
{ Name = "LD_OPTIMIZATION_LEVEL";
167+
{
168+
Name = SWIFTC_SDKROOT_LINKER_INPUT;
169+
Type = Path;
170+
DefaultValue = "$(SDKROOT)";
171+
Condition = "$(LINKER_DRIVER) == swiftc";
172+
CommandLineFlag = "-sdk";
173+
IsInputDependency = Yes;
174+
},
175+
{
176+
Name = "LD_OPTIMIZATION_LEVEL";
101177
Type = String;
178+
Condition = "$(LINKER_DRIVER) == clang";
102179
DefaultValue = "$(GCC_OPTIMIZATION_LEVEL)";
103180
"CommandLinePrefixFlag" = "-O";
104181
},
105182
{
106183
Name = "LD_SUPPRESS_WARNINGS";
107184
Type = Boolean;
108185
DefaultValue = "$(SUPPRESS_WARNINGS)";
186+
},
187+
{
188+
Name = "CLANG_LD_SUPPRESS_WARNINGS";
189+
Type = Boolean;
190+
Condition = "$(LINKER_DRIVER) == clang";
191+
DefaultValue = "$(LD_SUPPRESS_WARNINGS)";
109192
CommandLineFlag = "-w";
110193
},
194+
{
195+
Name = "SWIFTC_LD_SUPPRESS_WARNINGS";
196+
Type = Boolean;
197+
Condition = "$(LINKER_DRIVER) == swiftc";
198+
DefaultValue = "$(LD_SUPPRESS_WARNINGS)";
199+
CommandLineFlag = "-suppress-warnings";
200+
},
111201
{
112202
Name = "LD_WARN_UNUSED_DYLIBS";
113203
Type = Boolean;
@@ -147,9 +237,23 @@
147237
{
148238
Name = "SYSTEM_FRAMEWORK_SEARCH_PATHS";
149239
Type = PathList;
240+
},
241+
{
242+
Name = "CLANG_SYSTEM_FRAMEWORK_SEARCH_PATHS";
243+
Type = PathList;
150244
FlattenRecursiveSearchPathsInValue = Yes;
245+
Condition = "$(LINKER_DRIVER) == clang";
246+
DefaultValue = "$(SYSTEM_FRAMEWORK_SEARCH_PATHS)";
151247
CommandLineFlag = "-iframework";
152248
},
249+
{
250+
Name = "SWIFTC_SYSTEM_FRAMEWORK_SEARCH_PATHS";
251+
Type = PathList;
252+
FlattenRecursiveSearchPathsInValue = Yes;
253+
Condition = "$(LINKER_DRIVER) == swiftc";
254+
DefaultValue = "$(SYSTEM_FRAMEWORK_SEARCH_PATHS)";
255+
CommandLineFlag = "-Fsystem";
256+
},
153257
{
154258
Name = "PRODUCT_TYPE_LIBRARY_SEARCH_PATHS";
155259
Type = PathList;
@@ -166,7 +270,24 @@
166270
Name = "__INPUT_FILE_LIST_PATH__";
167271
Type = Path;
168272
DefaultValue = "$(LINK_FILE_LIST_$(variant)_$(arch))";
273+
},
274+
{
275+
Name = "CLANG__INPUT_FILE_LIST_PATH__";
276+
Type = Path;
277+
DefaultValue = "$(__INPUT_FILE_LIST_PATH__)";
169278
CommandLineFlag = "-filelist";
279+
Condition = "$(LINKER_DRIVER) == clang";
280+
IsInputDependency = Yes;
281+
},
282+
{
283+
Name = "SWIFTC__INPUT_FILE_LIST_PATH__";
284+
Type = Path;
285+
DefaultValue = "$(__INPUT_FILE_LIST_PATH__)";
286+
CommandLineArgs = {
287+
"" = ();
288+
"<<otherwise>>" = "@$(value)";
289+
};
290+
Condition = "$(LINKER_DRIVER) == swiftc";
170291
IsInputDependency = Yes;
171292
},
172293
{
@@ -184,6 +305,7 @@
184305
{
185306
Name = "INIT_ROUTINE";
186307
Type = String;
308+
Condition = "$(LINKER_DRIVER) == clang";
187309
CommandLineFlag = "-init";
188310
},
189311
{
@@ -243,7 +365,7 @@
243365
{
244366
Name = "GENERATE_PROFILING_CODE";
245367
Type = Boolean;
246-
Condition = "$(variant) == profile";
368+
Condition = "$(variant) == profile && $(LINKER_DRIVER) == clang";
247369
CommandLineFlag = "-pg";
248370
},
249371
{
@@ -262,9 +384,30 @@
262384
Name = "LD_DYLIB_INSTALL_NAME";
263385
Type = String;
264386
DefaultValue = "";
265-
CommandLineFlag = "-install_name";
266387
Condition = "$(MACH_O_TYPE) == mh_dylib";
267388
},
389+
{
390+
Name = "CLANG_LD_DYLIB_INSTALL_NAME";
391+
Type = String;
392+
DefaultValue = "$(LD_DYLIB_INSTALL_NAME)";
393+
CommandLineFlag = "-install_name";
394+
Condition = "$(MACH_O_TYPE) == mh_dylib && $(LINKER_DRIVER) == clang";
395+
},
396+
{
397+
Name = "SWIFTC_LD_DYLIB_INSTALL_NAME";
398+
Type = String;
399+
DefaultValue = "$(LD_DYLIB_INSTALL_NAME)";
400+
CommandLineArgs = {
401+
"" = ();
402+
"<<otherwise>>" = (
403+
"-Xlinker",
404+
"-dylib_install_name",
405+
"-Xlinker",
406+
"$(value)",
407+
);
408+
};
409+
Condition = "$(MACH_O_TYPE) == mh_dylib && $(LINKER_DRIVER) == swiftc";
410+
},
268411
{
269412
Name = "LD_RUNPATH_SEARCH_PATHS";
270413
//Note: Cannot be of type 'PathList' as value is used with relative '../' paths
@@ -308,6 +451,7 @@
308451
Name = "KEEP_PRIVATE_EXTERNS";
309452
Type = Boolean;
310453
DefaultValue = NO;
454+
Condition = "$(LINKER_DRIVER) == clang";
311455
CommandLineFlag = "-keep_private_externs";
312456
},
313457
{

0 commit comments

Comments
 (0)