@@ -3984,6 +3984,102 @@ fileprivate struct SwiftTaskConstructionTests: CoreBasedTests {
39843984 }
39853985 }
39863986
3987+ @Test( . requireSDKs( . macOS) )
3988+ func enableHardeningInSwift( ) async throws {
3989+
3990+ func setupHardeningTest( _ tmpDir: Path ,
3991+ hardeningMode: String ) async throws -> TaskConstructionTester {
3992+ let testProject = try await TestProject (
3993+ " TestProject " ,
3994+ sourceRoot: tmpDir,
3995+ groupTree: TestGroup (
3996+ " SomeFiles " ,
3997+ children: [
3998+ TestFile ( " source.swift " ) ,
3999+ TestFile ( " source.cpp " )
4000+ ] ) ,
4001+ targets: [
4002+ TestStandardTarget (
4003+ " testFramework " , type: . framework,
4004+ buildConfigurations: [
4005+ TestBuildConfiguration ( " Debug " , buildSettings: [
4006+ " GENERATE_INFOPLIST_FILE " : " YES " ,
4007+ " PRODUCT_NAME " : " $(TARGET_NAME) " ,
4008+ " SWIFT_EXEC " : swiftCompilerPath. str,
4009+ " SWIFT_VERSION " : swiftVersion,
4010+ " CLANG_CXX_STANDARD_LIBRARY_HARDENING " : hardeningMode
4011+ ] ) ,
4012+ ] ,
4013+ buildPhases: [
4014+ TestSourcesBuildPhase ( [ " source.swift " , " source.cpp " ] )
4015+ ]
4016+ )
4017+ ] )
4018+ let tester = try await TaskConstructionTester ( getCore ( ) , testProject)
4019+ return tester
4020+ }
4021+
4022+ // Verify that we don't enable hardening in Swift compilations when C++
4023+ // hardening is none.
4024+ try await withTemporaryDirectory { tmpDir in
4025+ let tester = try await setupHardeningTest ( tmpDir, hardeningMode: " none " )
4026+ await tester. checkBuild ( runDestination: . macOS) { results in
4027+ results. checkTask ( . matchRuleType( " SwiftDriver Compilation " ) ) { task in
4028+ task. checkCommandLineContainsUninterrupted ( [ " -Xcc " , " -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_NONE " ] )
4029+ }
4030+ }
4031+ }
4032+
4033+ try await withTemporaryDirectory { tmpDir in
4034+ let tester = try await setupHardeningTest ( tmpDir, hardeningMode: " fast " )
4035+ await tester. checkBuild ( runDestination: . macOS) { results in
4036+ results. checkTask ( . matchRuleType( " SwiftDriver Compilation " ) ) { task in
4037+ task. checkCommandLineContainsUninterrupted ( [ " -Xcc " , " -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST " ] )
4038+ }
4039+ }
4040+ }
4041+
4042+ try await withTemporaryDirectory { tmpDir in
4043+ let tester = try await setupHardeningTest ( tmpDir, hardeningMode: " extensive " )
4044+ await tester. checkBuild ( runDestination: . macOS) { results in
4045+ results. checkTask ( . matchRuleType( " SwiftDriver Compilation " ) ) { task in
4046+ task. checkCommandLineContainsUninterrupted ( [ " -Xcc " , " -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE " ] )
4047+ }
4048+ }
4049+ }
4050+
4051+ try await withTemporaryDirectory { tmpDir in
4052+ let tester = try await setupHardeningTest ( tmpDir, hardeningMode: " debug " )
4053+ await tester. checkBuild ( runDestination: . macOS) { results in
4054+ results. checkTask ( . matchRuleType( " SwiftDriver Compilation " ) ) { task in
4055+ task. checkCommandLineContainsUninterrupted ( [ " -Xcc " , " -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG " ] )
4056+ }
4057+ }
4058+ }
4059+
4060+ // Verify that we don't enable hardening in Swift compilations when C++
4061+ // hardening mode is garbage.
4062+ try await withTemporaryDirectory { tmpDir in
4063+ let tester = try await setupHardeningTest ( tmpDir, hardeningMode: " unexpected " )
4064+ await tester. checkBuild ( runDestination: . macOS) { results in
4065+ results. checkTask ( . matchRuleType( " SwiftDriver Compilation " ) ) { task in
4066+ task. checkCommandLineNoMatch ( [ . prefix( " -D_LIBCPP_HARDENING_MODE= " ) ] )
4067+ }
4068+ }
4069+ }
4070+
4071+ // Verify that we don't enable hardening in Swift compilations when C++
4072+ // hardening mode is empty.
4073+ try await withTemporaryDirectory { tmpDir in
4074+ let tester = try await setupHardeningTest ( tmpDir, hardeningMode: " " )
4075+ await tester. checkBuild ( runDestination: . macOS) { results in
4076+ results. checkTask ( . matchRuleType( " SwiftDriver Compilation " ) ) { task in
4077+ task. checkCommandLineNoMatch ( [ . prefix( " -D_LIBCPP_HARDENING_MODE= " ) ] )
4078+ }
4079+ }
4080+ }
4081+ }
4082+
39874083 @Test( . requireSDKs( . macOS) )
39884084 func cxxInteropLinkerArgGeneration( ) async throws {
39894085 // When Swift is generating additional linker args, we should not try to inject the response file when a target is a dependent of a cxx-interop target but has no Swift source of its own.
0 commit comments