@@ -45,6 +45,8 @@ enum BazelTargetStoreError: Error, LocalizedError {
4545 case unableToMapBazelLabelToParents( String )
4646 case unableToMapBazelLabelToTopLevelRuleType( String )
4747 case unableToMapBazelLabelToTopLevelConfig( String )
48+ case unableToMapBazelLabelToParentConfig( String )
49+ case unableToMapConfigToTopLevelLabels( UInt32 )
4850 case noCachedAquery
4951
5052 var errorDescription : String ? {
@@ -57,6 +59,10 @@ enum BazelTargetStoreError: Error, LocalizedError {
5759 return " Unable to map ' \( label) ' to its top-level rule type "
5860 case . unableToMapBazelLabelToTopLevelConfig( let label) :
5961 return " Unable to map ' \( label) ' to its top-level configuration "
62+ case . unableToMapBazelLabelToParentConfig( let label) :
63+ return " Unable to map ' \( label) ' to its parent configuration "
64+ case . unableToMapConfigToTopLevelLabels( let config) :
65+ return " Unable to map configuration ' \( config) ' to its top-level labels "
6066 case . noCachedAquery:
6167 return " No cached aquery result found in the store. "
6268 }
@@ -126,14 +132,6 @@ final class BazelTargetStoreImpl: BazelTargetStore, @unchecked Sendable {
126132 return bspURIs
127133 }
128134
129- /// Retrieves the list of top-level apps that a given Bazel target label belongs to.
130- func bazelLabelToParents( forBazelLabel label: String ) throws -> [ String ] {
131- guard let parents = cqueryResult? . bazelLabelToParentsMap [ label] else {
132- throw BazelTargetStoreError . unableToMapBazelLabelToParents ( label)
133- }
134- return parents
135- }
136-
137135 /// Retrieves the top-level rule type for a given Bazel **top-level** target label.
138136 func topLevelRuleType( forBazelLabel label: String ) throws -> TopLevelRuleType {
139137 guard let ruleType = cqueryResult? . topLevelLabelToRuleMap [ label] else {
@@ -150,20 +148,33 @@ final class BazelTargetStoreImpl: BazelTargetStore, @unchecked Sendable {
150148 return config
151149 }
152150
151+ /// Retrieves the configuration for a given Bazel target label.
152+ func parentConfig( forBazelLabel label: String ) throws -> UInt32 {
153+ guard let config = cqueryResult? . bazelLabelToParentConfigMap [ label] else {
154+ throw BazelTargetStoreError . unableToMapBazelLabelToParentConfig ( label)
155+ }
156+ return config
157+ }
158+
159+ /// Retrieves the list of top-level labels for a given configuration.
160+ func topLevelLabels( forConfig config: UInt32 ) throws -> [ String ] {
161+ guard let labels = cqueryResult? . configurationToTopLevelLabelsMap [ config] else {
162+ throw BazelTargetStoreError . unableToMapConfigToTopLevelLabels ( config)
163+ }
164+ return labels
165+ }
166+
153167 /// Provides the bazel label containing **platform information** for a given BSP URI.
154168 /// This is used to determine the correct set of compiler flags for the target / platform combo.
155169 func platformBuildLabelInfo( forBSPURI uri: URI ) throws -> BazelTargetPlatformInfo {
156170 let bazelLabel = try bazelTargetLabel ( forBSPURI: uri)
157- let parents = try bazelLabelToParents ( forBazelLabel: bazelLabel)
171+ let configId = try parentConfig ( forBazelLabel: bazelLabel)
172+ let parents = try topLevelLabels ( forConfig: configId)
158173 // FIXME: When a target can compile to multiple platforms, the way Xcode handles it is by selecting
159174 // the one matching your selected simulator in the IDE. We don't have any sort of special IDE integration
160175 // at the moment, so for now we just select the first parent.
176+ // We are also not processing the different variants at all (see FIXME in BazelTargetQuerierParser.swift).
161177 let parentToUse = parents [ 0 ]
162- if parents. count > 1 {
163- logger. warning (
164- " Target \( uri. description, privacy: . public) has multiple top-level parents; will pick the first one: \( parentToUse, privacy: . public) "
165- )
166- }
167178 let rule = try topLevelRuleType ( forBazelLabel: parentToUse)
168179 let config = try topLevelConfigInfo ( forBazelLabel: parentToUse)
169180 return BazelTargetPlatformInfo (
@@ -220,7 +231,7 @@ final class BazelTargetStoreImpl: BazelTargetStore, @unchecked Sendable {
220231
221232 reportQueue. async { [ weak self] in
222233 guard let self = self else { return }
223- let outputPath = self . initializedConfig. outputPath
234+ let outputPath : String = self . initializedConfig. outputPath
224235 let fileName = " sourcekit-bazel-bsp-graph.json "
225236 self . writeReport ( toPath: outputPath + " / " + fileName)
226237 }
@@ -253,29 +264,38 @@ extension BazelTargetStoreImpl {
253264 private func generateGraphReport( ) throws -> BazelTargetGraphReport {
254265 logger. info ( " Generating graph report " )
255266 var reportTopLevel : [ BazelTargetGraphReport . TopLevelTarget ] = [ ]
267+ var reportConfigurations : [ UInt32 : BazelTargetGraphReport . Configuration ] = [ : ]
256268 let topLevelTargets = cqueryResult? . topLevelTargets ?? [ ]
257269 for (label, ruleType) in topLevelTargets {
258270 let topLevelConfig = try topLevelConfigInfo ( forBazelLabel: label)
271+ let configId = try parentConfig ( forBazelLabel: label)
259272 reportTopLevel. append (
260273 . init(
261274 label: label,
262275 ruleType: ruleType. rawValue,
276+ isTest: ruleType. testBundleRule != nil ,
277+ configId: configId
278+ )
279+ )
280+ reportConfigurations [ configId] = . init(
281+ . init(
282+ id: configId,
263283 platform: topLevelConfig. platform,
264284 minimumOsVersion: topLevelConfig. minimumOsVersion,
265- cpuArch: topLevelConfig. cpuArch,
266- isTest: ruleType. testBundleRule != nil
285+ cpuArch: topLevelConfig. cpuArch
267286 )
268287 )
269288 }
270289 var reportDependencies : [ BazelTargetGraphReport . DependencyTarget ] = [ ]
271290 let dependencyTargets = cqueryResult? . buildTargets. compactMap { $0. displayName } ?? [ ]
272291 for label in dependencyTargets {
273- let parents = try bazelLabelToParents ( forBazelLabel: label)
274- reportDependencies. append ( . init( label: label, parents : parents ) )
292+ let configId = try parentConfig ( forBazelLabel: label)
293+ reportDependencies. append ( . init( label: label, configId : configId ) )
275294 }
276295 return BazelTargetGraphReport (
277296 topLevelTargets: reportTopLevel,
278- dependencyTargets: reportDependencies
297+ dependencyTargets: reportDependencies,
298+ configurations: reportConfigurations. values. sorted ( by: { $0. id < $1. id } )
279299 )
280300 }
281301}
0 commit comments