go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cipkg/base/generators/generator.go (about) 1 // Copyright 2023 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package generators 16 17 import ( 18 "context" 19 20 "go.chromium.org/luci/cipkg/core" 21 "go.chromium.org/luci/common/errors" 22 ) 23 24 // Generator is the interface for generating actions. 25 type Generator interface { 26 Generate(ctx context.Context, plats Platforms) (*core.Action, error) 27 } 28 29 var ( 30 ErrUnknowDependencyType = errors.New("unknown dependency type") 31 ) 32 33 // DependencyType includes the different dependency types are used to calculate 34 // dependency's cross-compile platform from the dependent's. 35 type DependencyType int 36 37 func (t DependencyType) String() string { 38 switch t { 39 case DepsBuildBuild: 40 return "depsBuildBuild" 41 case DepsBuildHost: 42 return "depsBuildHost" 43 case DepsBuildTarget: 44 return "depsBuildTarget" 45 case DepsHostHost: 46 return "depsHostHost" 47 case DepsHostTarget: 48 return "depsHostTarget" 49 case DepsTargetTarget: 50 return "depsTargetTarget" 51 default: 52 return "depsUnknown" 53 } 54 } 55 56 const ( 57 DepsUnknown DependencyType = iota 58 DepsBuildBuild 59 DepsBuildHost 60 DepsBuildTarget 61 DepsHostHost 62 DepsHostTarget 63 DepsTargetTarget 64 DepsMaxNum 65 ) 66 67 // Dependency provide function for generating the corresponding action with 68 // platform from the dependency type for cross-compilation. 69 type Dependency struct { 70 Generator Generator 71 Type DependencyType 72 Runtime bool 73 } 74 75 // Generate the corresponding action with platform from the dependency type for 76 // cross-compilation. 77 func (dep *Dependency) Generate(ctx context.Context, plats Platforms) (*core.Action, error) { 78 var depPlats Platforms 79 switch dep.Type { 80 case DepsBuildBuild: 81 depPlats = Platforms{plats.Build, plats.Build, plats.Build} 82 case DepsBuildHost: 83 depPlats = Platforms{plats.Build, plats.Build, plats.Host} 84 case DepsBuildTarget: 85 depPlats = Platforms{plats.Build, plats.Build, plats.Target} 86 case DepsHostHost: 87 depPlats = Platforms{plats.Build, plats.Host, plats.Host} 88 case DepsHostTarget: 89 depPlats = Platforms{plats.Build, plats.Host, plats.Target} 90 case DepsTargetTarget: 91 depPlats = Platforms{plats.Build, plats.Target, plats.Target} 92 default: 93 return nil, ErrUnknowDependencyType 94 } 95 return dep.Generator.Generate(ctx, depPlats) 96 }