Skip to content

Commit 906afa4

Browse files
authored
Add test fixture for experimental-lto-mode (#6891)
Adds a test covering experimental lto mode and fixes a bug where the lto mode was not passed to the linker. Fixes #6888
1 parent e9fa373 commit 906afa4

File tree

8 files changed

+69
-0
lines changed

8 files changed

+69
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// swift-tools-version: 5.9
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "SwiftAndCTargets",
7+
targets: [
8+
.target(name: "cLib"),
9+
.executableTarget(name: "exe", dependencies: ["cLib", "swiftLib"]),
10+
.target(name: "swiftLib"),
11+
]
12+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <stdio.h>
2+
3+
#include "include/cLib.h"
4+
5+
void cPrint(int value) {
6+
printf("c value: %i\n", value);
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
extern void cPrint(int);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import swiftLib
2+
import cLib
3+
4+
cPrint(1)
5+
swiftPrint(value: 2)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public func swiftPrint(value: Int) {
2+
print("swift value: \(value)")
3+
}

Sources/Build/BuildDescription/ProductBuildDescription.swift

+10
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,16 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
325325
// User arguments (from -Xlinker) should follow generated arguments to allow user overrides
326326
args += self.buildParameters.flags.linkerFlags.asSwiftcLinkerFlags()
327327

328+
// Enable the correct lto mode if requested.
329+
switch self.buildParameters.linkingParameters.linkTimeOptimizationMode {
330+
case nil:
331+
break
332+
case .full:
333+
args += ["-lto=llvm-full"]
334+
case .thin:
335+
args += ["-lto=llvm-thin"]
336+
}
337+
328338
// Pass default library paths from the toolchain.
329339
for librarySearchPath in self.buildParameters.toolchain.librarySearchPaths {
330340
args += ["-L", librarySearchPath.pathString]

Tests/FunctionalTests/MiscellaneousTests.swift

+21
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,27 @@ class MiscellaneousTestCase: XCTestCase {
337337
}
338338
}
339339

340+
func testLTO() throws {
341+
#if os(macOS)
342+
// FIXME: this test requires swift-driver to be installed
343+
// Currently swift-ci does not build/install swift-driver before running
344+
// swift-package-manager tests which results in this test failing.
345+
// See the following additional discussion:
346+
// - https://github.com/apple/swift/pull/69696
347+
// - https://github.com/apple/swift/pull/61766
348+
// - https://github.com/apple/swift-package-manager/pull/5842#issuecomment-1301632685
349+
try fixture(name: "Miscellaneous/LTO/SwiftAndCTargets") { fixturePath in
350+
let output = try executeSwiftBuild(
351+
fixturePath,
352+
extraArgs: ["--experimental-lto-mode=full", "--verbose"])
353+
// FIXME: On macOS dsymutil cannot find temporary .o files? (#6890)
354+
// Ensure warnings like the following are not present in build output
355+
// warning: (arm64) /var/folders/ym/6l_0x8vj0b70sz_4h9d70p440000gn/T/main-e120de.o unable to open object file: No such file or directory
356+
// XCTAssertNoMatch(output.stdout, .contains("unable to open object file"))
357+
}
358+
#endif
359+
}
360+
340361
func testUnicode() throws {
341362
#if !os(Linux) && !os(Android) // TODO: - Linux has trouble with this and needs investigation.
342363
try fixture(name: "Miscellaneous/Unicode") { fixturePath in

0 commit comments

Comments
 (0)