From 842894d1c680ab7cf6e03eb31150f26740ae2d02 Mon Sep 17 00:00:00 2001 From: Anthony Cui Date: Wed, 24 Jul 2024 13:32:41 -0700 Subject: [PATCH] Generate necessary config files for the Clang tool on the Go side. PiperOrigin-RevId: 655682520 --- tools/nvidia_driver_differ/parser/BUILD | 5 +- .../parser/clang_config.go | 58 +++++++++++++++++++ .../parser/json_definitions.go | 2 +- 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 tools/nvidia_driver_differ/parser/clang_config.go diff --git a/tools/nvidia_driver_differ/parser/BUILD b/tools/nvidia_driver_differ/parser/BUILD index 2c09c1195..04c6c9b8a 100644 --- a/tools/nvidia_driver_differ/parser/BUILD +++ b/tools/nvidia_driver_differ/parser/BUILD @@ -7,7 +7,10 @@ package( go_library( name = "parser", - srcs = ["json_definitions.go"], + srcs = [ + "clang_config.go", + "json_definitions.go", + ], visibility = ["//tools/nvidia_driver_differ:__subpackages__"], deps = ["@com_github_google_go_cmp//cmp:go_default_library"], ) diff --git a/tools/nvidia_driver_differ/parser/clang_config.go b/tools/nvidia_driver_differ/parser/clang_config.go new file mode 100644 index 000000000..675d90342 --- /dev/null +++ b/tools/nvidia_driver_differ/parser/clang_config.go @@ -0,0 +1,58 @@ +// Copyright 2024 The gVisor Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package parser + +import ( + "encoding/json" + "fmt" + "os" +) + +// ClangASTConfig is the format for compilation_commands.json. +type ClangASTConfig struct { + Directory string `json:"directory"` + Arguments []string `json:"arguments"` + Filename string `json:"file"` +} + +// NewParserConfig creates a ClangASTConfig for the given file using the list of includes. +func NewParserConfig(directory, filename string, includes []string) ClangASTConfig { + args := []string{"clang"} + for _, include := range includes { + args = append(args, "-I", include) + } + args = append(args, filename) + + return ClangASTConfig{ + Directory: directory, + Arguments: args, + Filename: filename, + } +} + +// CreateCompileCommandsFile writes the given config to file. +func CreateCompileCommandsFile(config []ClangASTConfig, path string) error { + f, err := os.Create(path) + if err != nil { + return fmt.Errorf("failed to create %s: %w", path, err) + } + defer f.Close() + + if err := json.NewEncoder(f).Encode(config); err != nil { + return fmt.Errorf("failed to write config to file: %w", err) + } + + return nil +} diff --git a/tools/nvidia_driver_differ/parser/json_definitions.go b/tools/nvidia_driver_differ/parser/json_definitions.go index 1d12e6eb2..e7e1d92a0 100644 --- a/tools/nvidia_driver_differ/parser/json_definitions.go +++ b/tools/nvidia_driver_differ/parser/json_definitions.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package parser contains functions for parsing the output of driver_ast_parser. +// Package parser contains functions for interfacing with driver_ast_parser. package parser import (