1 // Copyright 2021 The Bazel Authors. All rights reserved. 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 main 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "io" 21 ) 22 23 // From https://pkg.go.dev/golang.org/x/tools/go/packages#LoadMode 24 type LoadMode int 25 26 // Only NeedExportsFile is needed in our case 27 const ( 28 // NeedName adds Name and PkgPath. 29 NeedName LoadMode = 1 << iota 30 31 // NeedFiles adds GoFiles and OtherFiles. 32 NeedFiles 33 34 // NeedCompiledGoFiles adds CompiledGoFiles. 35 NeedCompiledGoFiles 36 37 // NeedImports adds Imports. If NeedDeps is not set, the Imports field will contain 38 // "placeholder" Packages with only the ID set. 39 NeedImports 40 41 // NeedDeps adds the fields requested by the LoadMode in the packages in Imports. 42 NeedDeps 43 44 // NeedExportsFile adds ExportFile. 45 NeedExportFile 46 47 // NeedTypes adds Types, Fset, and IllTyped. 48 NeedTypes 49 50 // NeedSyntax adds Syntax. 51 NeedSyntax 52 53 // NeedTypesInfo adds TypesInfo. 54 NeedTypesInfo 55 56 // NeedTypesSizes adds TypesSizes. 57 NeedTypesSizes 58 59 // typecheckCgo enables full support for type checking cgo. Requires Go 1.15+. 60 // Modifies CompiledGoFiles and Types, and has no effect on its own. 61 typecheckCgo 62 63 // NeedModule adds Module. 64 NeedModule 65 ) 66 67 // Deprecated: NeedExportsFile is a historical misspelling of NeedExportFile. 68 const NeedExportsFile = NeedExportFile 69 70 // From https://github.com/golang/tools/blob/v0.1.0/go/packages/external.go#L32 71 // Most fields are disabled since there is no need for them 72 type DriverRequest struct { 73 Mode LoadMode `json:"mode"` 74 // Env specifies the environment the underlying build system should be run in. 75 // Env []string `json:"env"` 76 // BuildFlags are flags that should be passed to the underlying build system. 77 // BuildFlags []string `json:"build_flags"` 78 // Tests specifies whether the patterns should also return test packages. 79 Tests bool `json:"tests"` 80 // Overlay maps file paths (relative to the driver's working directory) to the byte contents 81 // of overlay files. 82 // Overlay map[string][]byte `json:"overlay"` 83 } 84 85 func ReadDriverRequest(r io.Reader) (*DriverRequest, error) { 86 req := &DriverRequest{} 87 if err := json.NewDecoder(r).Decode(&req); err != nil { 88 return nil, fmt.Errorf("unable to decode driver request: %w", err) 89 } 90 return req, nil 91 } 92