Skip to content

Commit ed70a4a

Browse files
committed
Provide a stub for toolchain(use_target_platform_constraints = True)
We already patch up the Protobuf schema to store this information properly. We still need to fix up ResolvedToolchains to select the right platform, but for that I first need to read up a bit more how all of this is supposed to work.
1 parent 1a43d7b commit ed70a4a

6 files changed

Lines changed: 491 additions & 406 deletions

File tree

pkg/model/analysis/compatible_toolchains_for_type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (c *baseComputer[TReference, TMetadata]) ComputeCompatibleToolchainsForType
6969
var compatibleToolchains []*model_analysis_pb.RegisteredToolchain
7070
FindCompatibleToolchains:
7171
for _, toolchain := range allToolchains {
72-
if !constraintsAreCompatible(constraints, toolchain.TargetCompatibleWith) {
72+
if compatibleWith := toolchain.CompatibleWith; compatibleWith != nil && !constraintsAreCompatible(constraints, compatibleWith.Target) {
7373
// Missing incompatible constraint value.
7474
continue FindCompatibleToolchains
7575
}

pkg/model/analysis/registered_toolchains.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ func (h *registeredToolchainExtractingModuleDotBazelHandler[TReference, TMetadat
127127

128128
var targetSettings []string
129129
var toolchain, toolchainType *string
130+
useTargetPlatformConstraints := false
130131
var errIter error
131132
for key, value := range model_starlark.AllStructFields(
132133
h.context,
@@ -169,6 +170,12 @@ func (h *registeredToolchainExtractingModuleDotBazelHandler[TReference, TMetadat
169170
return fmt.Errorf("toolchain_type field of DeclaredToolchainInfo of toolchain %#v is not a label", toolchainLabelStr)
170171
}
171172
toolchainType = &l.Label
173+
case "use_target_platform_constraints":
174+
b, ok := value.Message.Kind.(*model_starlark_pb.Value_Bool)
175+
if !ok {
176+
return fmt.Errorf("use_target_platform_constraints field of DeclaredToolchainInfo of toolchain %#v is not a bool", toolchainLabelStr)
177+
}
178+
useTargetPlatformConstraints = b.Bool
172179
}
173180
}
174181
if errIter != nil {
@@ -253,15 +260,29 @@ func (h *registeredToolchainExtractingModuleDotBazelHandler[TReference, TMetadat
253260
missingDependencies = true
254261
}
255262

263+
var compatibleWith *model_analysis_pb.RegisteredToolchain_CompatibleWith
264+
if useTargetPlatformConstraints {
265+
if len(execCompatibleWith) > 0 {
266+
return fmt.Errorf("exec_compatible_with of toolchain %#v is non-empty, while use_target_platform_constraints is set to True", toolchainLabelStr)
267+
}
268+
if len(targetCompatibleWith) > 0 {
269+
return fmt.Errorf("target_compatible_with of toolchain %#v is non-empty, while use_target_platform_constraints is set to True", toolchainLabelStr)
270+
}
271+
} else {
272+
compatibleWith = &model_analysis_pb.RegisteredToolchain_CompatibleWith{
273+
Exec: execCompatibleWith,
274+
Target: targetCompatibleWith,
275+
}
276+
}
277+
256278
if !missingDependencies {
257279
slices.Sort(targetSettings)
258280
h.registeredToolchainsByType[*toolchainType] = append(
259281
h.registeredToolchainsByType[*toolchainType],
260282
&model_analysis_pb.RegisteredToolchain{
261-
ExecCompatibleWith: execCompatibleWith,
262-
TargetCompatibleWith: targetCompatibleWith,
263-
TargetSettings: slices.Compact(targetSettings),
264-
Toolchain: *toolchain,
283+
CompatibleWith: compatibleWith,
284+
TargetSettings: slices.Compact(targetSettings),
285+
Toolchain: *toolchain,
265286
},
266287
)
267288
}

pkg/model/analysis/resolved_toolchains.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,12 @@ CheckExecutionPlatform:
110110
CheckToolchainType:
111111
for i, toolchainsForType := range compatibleToolchainsByType {
112112
for _, toolchain := range toolchainsForType {
113-
if constraintsAreCompatible(executionPlatform.Constraints, toolchain.ExecCompatibleWith) {
113+
compatibleWith := toolchain.CompatibleWith
114+
if compatibleWith == nil {
115+
// TODO: Add support for this.
116+
return PatchedResolvedToolchainsValue[TMetadata]{}, fmt.Errorf("toolchain %#v uses use_target_platform_constraints, which is not yet supported", toolchain.Toolchain)
117+
}
118+
if constraintsAreCompatible(executionPlatform.Constraints, compatibleWith.Exec) {
114119
toolchainTypeHasAtLeastOneMatchingExecutionPlatform[i] = true
115120
resolvedToolchains = append(resolvedToolchains, toolchain)
116121
continue CheckToolchainType

0 commit comments

Comments
 (0)