From 6a8b0ea975e32a6d719d44739e64a72ce710bf08 Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Tue, 11 Nov 2025 11:56:36 +0100 Subject: [PATCH] Do not pass index store options if indexing is disabled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We were passing `-index-store-compress` to the compiler even though we don’t pass `-index-store-path` if `INDEX_STORE_ENABLE = YES` but `COMPILER_INDEX_STORE_ENABLE = NO`. This is because `CLANG_INDEX_STORE_ENABLE` still evaluated to `YES` and thus the `Condition` of `CLANG_INDEX_STORE_COMPRESS` also evaluated to `YES` but we didn’t pass `-index-store-path` because the condition of `CLANG_INDEX_STORE_ENABLE` evaluated to `NO`. This was a misconception on my side when I added these build settings because I assumed that the `Condition` would influence the value of the build setting instead of just influencing `ComandLineArgs`. I don’t like how we need to repeat the same condition in multiple build settings now but haven’t found a better solution. rdar://164248532 --- .../Specs/MetalCompiler.xcspec | 2 +- .../SWBUniversalPlatform/Specs/Clang.xcspec | 6 +-- .../SWBUniversalPlatform/Specs/Swift.xcspec | 4 +- .../SWBTaskConstructionTests/ClangTests.swift | 48 +++++++++++++++++++ .../SWBTaskConstructionTests/MetalTests.swift | 46 ++++++++++++++++++ .../SwiftTaskConstructionTests.swift | 47 ++++++++++++++++++ 6 files changed, 147 insertions(+), 6 deletions(-) diff --git a/Sources/SWBApplePlatform/Specs/MetalCompiler.xcspec b/Sources/SWBApplePlatform/Specs/MetalCompiler.xcspec index 97b73fa9..6ef362eb 100644 --- a/Sources/SWBApplePlatform/Specs/MetalCompiler.xcspec +++ b/Sources/SWBApplePlatform/Specs/MetalCompiler.xcspec @@ -239,7 +239,7 @@ Name = "METAL_INDEX_STORE_ONLY_PROJECT_FILES"; Type = Boolean; DefaultValue = "$(INDEX_STORE_ONLY_PROJECT_FILES)"; - Condition = "$(MTL_ENABLE_INDEX_STORE)"; + Condition = "$(MTL_ENABLE_INDEX_STORE) && ( $(COMPILER_INDEX_STORE_ENABLE) == YES || ( $(COMPILER_INDEX_STORE_ENABLE) == Default && $(MTL_ENABLE_DEBUG_INFO) != NO ) )"; CommandLineArgs = { YES = ( // See corresponding definition in Clang.xcspec diff --git a/Sources/SWBUniversalPlatform/Specs/Clang.xcspec b/Sources/SWBUniversalPlatform/Specs/Clang.xcspec index 36ec0772..4827ca74 100644 --- a/Sources/SWBUniversalPlatform/Specs/Clang.xcspec +++ b/Sources/SWBUniversalPlatform/Specs/Clang.xcspec @@ -3189,7 +3189,7 @@ Name = "CLANG_INDEX_STORE_ONLY_PROJECT_FILES"; Type = Boolean; DefaultValue = "$(INDEX_STORE_ONLY_PROJECT_FILES)"; - Condition = "$(CLANG_INDEX_STORE_ENABLE)"; + Condition = "$(CLANG_INDEX_STORE_ENABLE) && ( $(COMPILER_INDEX_STORE_ENABLE) || ( $(COMPILER_INDEX_STORE_ENABLE) == Default && $(GCC_OPTIMIZATION_LEVEL) == 0 ) )"; CommandLineArgs = { YES = ( "-index-ignore-system-symbols", @@ -3203,7 +3203,7 @@ Name = "CLANG_INDEX_STORE_COMPRESS"; Type = Boolean; DefaultValue = "$(INDEX_STORE_COMPRESS)"; - Condition = "$(CLANG_INDEX_STORE_ENABLE)"; + Condition = "$(CLANG_INDEX_STORE_ENABLE) && ( $(COMPILER_INDEX_STORE_ENABLE) || ( $(COMPILER_INDEX_STORE_ENABLE) == Default && $(GCC_OPTIMIZATION_LEVEL) == 0 ) )"; CommandLineArgs = { YES = ( "-index-store-compress", @@ -3215,7 +3215,7 @@ Name = "CLANG_INDEX_STORE_IGNORE_MACROS"; Type = Boolean; DefaultValue = NO; - Condition = "$(CLANG_INDEX_STORE_ENABLE)"; + Condition = "$(CLANG_INDEX_STORE_ENABLE) && ( $(COMPILER_INDEX_STORE_ENABLE) || ( $(COMPILER_INDEX_STORE_ENABLE) == Default && $(GCC_OPTIMIZATION_LEVEL) == 0 ) )"; DisplayName = "Do not index C macros"; Description = "Do not emit entries for C macros into the Index Store."; CommandLineArgs = { diff --git a/Sources/SWBUniversalPlatform/Specs/Swift.xcspec b/Sources/SWBUniversalPlatform/Specs/Swift.xcspec index d5f32b7d..76b7cb1f 100644 --- a/Sources/SWBUniversalPlatform/Specs/Swift.xcspec +++ b/Sources/SWBUniversalPlatform/Specs/Swift.xcspec @@ -1343,7 +1343,7 @@ Name = "SWIFT_INDEX_STORE_ONLY_PROJECT_FILES"; Type = Boolean; DefaultValue = "$(INDEX_STORE_ONLY_PROJECT_FILES)"; - Condition = "$(SWIFT_INDEX_STORE_ENABLE)"; + Condition = "$(SWIFT_INDEX_STORE_ENABLE) && ( $(COMPILER_INDEX_STORE_ENABLE) || ( $(COMPILER_INDEX_STORE_ENABLE) == Default && $(SWIFT_OPTIMIZATION_LEVEL) == '-Onone' ) )"; CommandLineArgs = { YES = ( // Assume that clang modules are getting indexed by a clang file within them. While this is technically not correct, since you could have a clang module that only consists of header files and is only included from Swift, such scenarios are rare. @@ -1357,7 +1357,7 @@ Name = "SWIFT_INDEX_STORE_COMPRESS"; Type = Boolean; DefaultValue = "$(INDEX_STORE_COMPRESS)"; - Condition = "$(SWIFT_INDEX_STORE_ENABLE)"; + Condition = "$(SWIFT_INDEX_STORE_ENABLE) && ( $(COMPILER_INDEX_STORE_ENABLE) || ( $(COMPILER_INDEX_STORE_ENABLE) == Default && $(SWIFT_OPTIMIZATION_LEVEL) == '-Onone' ) )"; CommandLineArgs = { YES = ( "-Xfrontend", diff --git a/Tests/SWBTaskConstructionTests/ClangTests.swift b/Tests/SWBTaskConstructionTests/ClangTests.swift index 0d2c0bc0..1cc88430 100644 --- a/Tests/SWBTaskConstructionTests/ClangTests.swift +++ b/Tests/SWBTaskConstructionTests/ClangTests.swift @@ -388,4 +388,52 @@ fileprivate struct ClangTests: CoreBasedTests { } } } + + @Test(.requireSDKs(.host)) + func indexOptionsNotAddedIfIndexingIsDisabled() async throws { + try await withTemporaryDirectory { tmpDir in + let testProject = TestProject( + "ProjectName", + sourceRoot: tmpDir, + groupTree: TestGroup( + "SomeFiles", + children: [ + TestFile("File1.c") + ]), + targets: [ + TestStandardTarget( + "Test", + type: .dynamicLibrary, + buildConfigurations: [ + TestBuildConfiguration( + "Debug", + buildSettings: [ + "COMPILER_INDEX_STORE_ENABLE": "NO", + "INDEX_DATA_STORE_DIR": tmpDir.join("index").str, + "INDEX_STORE_COMPRESS": "YES", + "INDEX_STORE_ONLY_PROJECT_FILES": "YES", + "CLANG_INDEX_STORE_IGNORE_MACROS": "YES", + "OTHER_CFLAGS": "-DCLANG_INDEX_STORE_ENABLE=$(CLANG_INDEX_STORE_ENABLE) -DCOMPILER_INDEX_STORE_ENABLE=$(COMPILER_INDEX_STORE_ENABLE)" + ] + ), + ], + buildPhases: [ + TestSourcesBuildPhase(["File1.c"]), + ] + ) + ]) + + let core = try await getCore() + let tester = try TaskConstructionTester(core, testProject) + await tester.checkBuild(BuildParameters(configuration: "Debug", commandLineOverrides: ["INDEX_ENABLE_DATA_STORE": "YES"]), runDestination: .host) { results in + results.checkTask(.matchRuleType("CompileC")) { compileTask in + compileTask.checkCommandLineDoesNotContain("-index-store-path") + compileTask.checkCommandLineDoesNotContain("-index-store-compress") + compileTask.checkCommandLineDoesNotContain("-index-ignore-system-symbols") + compileTask.checkCommandLineDoesNotContain("-index-ignore-pcms") + compileTask.checkCommandLineDoesNotContain("-index-ignore-macros") + } + } + } + } } diff --git a/Tests/SWBTaskConstructionTests/MetalTests.swift b/Tests/SWBTaskConstructionTests/MetalTests.swift index 5d22e07b..7e502a5e 100644 --- a/Tests/SWBTaskConstructionTests/MetalTests.swift +++ b/Tests/SWBTaskConstructionTests/MetalTests.swift @@ -72,4 +72,50 @@ fileprivate struct MetalTests: CoreBasedTests { } } } + + @Test(.requireSDKs(.macOS), .skipInGitHubActions("Metal toolchain is not installed on GitHub runners")) + func indexOptionsNotAddedIfIndexingIsDisabled() async throws { + try await withTemporaryDirectory { tmpDir in + let testProject = TestProject( + "ProjectName", + sourceRoot: tmpDir, + groupTree: TestGroup( + "SomeFiles", + children: [ + TestFile("File1.metal") + ]), + targets: [ + TestStandardTarget( + "Test", + type: .staticLibrary, + buildConfigurations: [ + TestBuildConfiguration( + "Debug", + buildSettings: [ + "COMPILER_INDEX_STORE_ENABLE": "NO", + "INDEX_DATA_STORE_DIR": tmpDir.join("index").str, + "INDEX_STORE_COMPRESS": "YES", + "INDEX_STORE_ONLY_PROJECT_FILES": "YES", + "CLANG_INDEX_STORE_IGNORE_MACROS": "YES", + ] + ), + ], + buildPhases: [ + TestSourcesBuildPhase(["File1.metal"]), + ] + ) + ]) + + let core = try await getCore() + let tester = try TaskConstructionTester(core, testProject) + await tester.checkBuild(BuildParameters(configuration: "Debug", commandLineOverrides: ["INDEX_ENABLE_DATA_STORE": "YES"]), runDestination: .macOS) { results in + results.checkTask(.matchRuleType("CompileMetalFile")) { compileTask in + compileTask.checkCommandLineDoesNotContain("-index-store-path") + compileTask.checkCommandLineDoesNotContain("-index-store-compress") + compileTask.checkCommandLineDoesNotContain("-index-ignore-system-symbols") + compileTask.checkCommandLineDoesNotContain("-index-ignore-pcms") + } + } + } + } } diff --git a/Tests/SWBTaskConstructionTests/SwiftTaskConstructionTests.swift b/Tests/SWBTaskConstructionTests/SwiftTaskConstructionTests.swift index 5ed36621..2db8fe46 100644 --- a/Tests/SWBTaskConstructionTests/SwiftTaskConstructionTests.swift +++ b/Tests/SWBTaskConstructionTests/SwiftTaskConstructionTests.swift @@ -4572,6 +4572,53 @@ fileprivate struct SwiftTaskConstructionTests: CoreBasedTests { } } } + + @Test(.requireSDKs(.host)) + func indexOptionsNotAddedIfIndexingIsDisabled() async throws { + try await withTemporaryDirectory { tmpDir in + let testProject = try await TestProject( + "ProjectName", + sourceRoot: tmpDir, + groupTree: TestGroup( + "SomeFiles", + children: [ + TestFile("File1.swift") + ]), + targets: [ + TestStandardTarget( + "Test", + type: .dynamicLibrary, + buildConfigurations: [ + TestBuildConfiguration( + "Debug", + buildSettings: [ + "SWIFT_EXEC": swiftCompilerPath.str, + "SWIFT_VERSION": swiftVersion, + "COMPILER_INDEX_STORE_ENABLE": "NO", + "INDEX_DATA_STORE_DIR": tmpDir.join("index").str, + "INDEX_STORE_COMPRESS": "YES", + "INDEX_STORE_ONLY_PROJECT_FILES": "YES" + ] + ), + ], + buildPhases: [ + TestSourcesBuildPhase(["File1.swift"]), + ] + ) + ]) + + let core = try await getCore() + let tester = try TaskConstructionTester(core, testProject) + await tester.checkBuild(BuildParameters(configuration: "Debug", commandLineOverrides: ["INDEX_ENABLE_DATA_STORE": "YES"]), runDestination: .host) { results in + results.checkTask(.matchRuleType("SwiftDriver Compilation")) { compileTask in + compileTask.checkCommandLineDoesNotContain("-index-store-path") + compileTask.checkCommandLineDoesNotContain("-index-store-compress") + compileTask.checkCommandLineDoesNotContain("-index-ignore-clang-modules") + compileTask.checkCommandLineDoesNotContain("-index-ignore-system-modules") + } + } + } + } } private func XCTAssertEqual(_ lhs: EnvironmentBindings, _ rhs: [String: String], file: StaticString = #filePath, line: UInt = #line) {