kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/util/tools/print_extra_action/print_extra_action.go (about)

     1  /*
     2   * Copyright 2015 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  // Binary print_extra_action takes a single argument that is a path to file with
    18  // an protobuf wire-encoding of a Bazel ExtraActionInfo and prints it as JSON to
    19  // stdout.
    20  package main
    21  
    22  import (
    23  	"context"
    24  	"encoding/json"
    25  	"flag"
    26  	"io/ioutil"
    27  	"os"
    28  
    29  	"kythe.io/kythe/go/platform/vfs"
    30  	"kythe.io/kythe/go/util/log"
    31  
    32  	"github.com/golang/protobuf/proto"
    33  
    34  	xapb "kythe.io/third_party/bazel/extra_actions_base_go_proto"
    35  )
    36  
    37  var knownExtensions = []*proto.ExtensionDesc{
    38  	xapb.E_SpawnInfo_SpawnInfo,
    39  	xapb.E_CppCompileInfo_CppCompileInfo,
    40  	xapb.E_CppLinkInfo_CppLinkInfo,
    41  	xapb.E_JavaCompileInfo_JavaCompileInfo,
    42  	xapb.E_PythonInfo_PythonInfo,
    43  }
    44  
    45  func main() {
    46  	flag.Parse()
    47  
    48  	f, err := vfs.Open(context.Background(), flag.Arg(0))
    49  	if err != nil {
    50  		log.Fatal(err)
    51  	}
    52  
    53  	data, err := ioutil.ReadAll(f)
    54  	f.Close()
    55  	if err != nil {
    56  		log.Fatal(err)
    57  	}
    58  
    59  	var xa xapb.ExtraActionInfo
    60  	if err := proto.Unmarshal(data, &xa); err != nil {
    61  		log.Fatal(err)
    62  	}
    63  
    64  	obj := make(map[string]any)
    65  	obj["extra_action_info"] = &xa
    66  
    67  	xs, err := proto.GetExtensions(&xa, knownExtensions)
    68  	if err != nil {
    69  		log.Fatal(err)
    70  	}
    71  
    72  	var extensions []any
    73  	for _, e := range xs {
    74  		if e != nil {
    75  			extensions = append(extensions, e)
    76  		}
    77  	}
    78  
    79  	if len(extensions) > 0 {
    80  		obj["extensions"] = extensions
    81  	}
    82  
    83  	if err := json.NewEncoder(os.Stdout).Encode(obj); err != nil {
    84  		log.Fatal(err)
    85  	}
    86  
    87  }