Skip to content

Add basedir config to reuse cache from different directories #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions caches/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func computeDigestForClangTidyBinary(clangTidyPath string) ([]byte, error) {
return computeFileDigest(path)
}

func ComputeFingerPrint(clangTidyPath string, invocation *clang.TidyInvocation, wd string, args []string) ([]byte, error) {
func ComputeFingerPrint(clangTidyPath string, baseDir string, invocation *clang.TidyInvocation,
wd string, args []string) ([]byte, error) {

// extract the compilation target command flags from the database
targetFlags, err := clang.ExtractCompilationTarget(invocation.DatabaseRoot, invocation.TargetPath)
Expand All @@ -68,7 +69,7 @@ func ComputeFingerPrint(clangTidyPath string, invocation *clang.TidyInvocation,
}

// main part of the fingerprint check generate the preprocessed output file and create a SHA256 of it
preProcessedDigest, err := clang.EvaluatePreprocessedFile(targetFlags.Directory, compileCommand)
preProcessedDigest, err := clang.EvaluatePreprocessedFile(targetFlags.Directory, baseDir, compileCommand)
if err != nil {
return nil, err
}
Expand Down
30 changes: 20 additions & 10 deletions clang/clangArgumentParser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package clang

import (
"bytes"
"crypto/sha256"
"errors"
"io"
Expand Down Expand Up @@ -52,7 +53,7 @@ func ParseClangCommandString(commands string) (*CompilerCommand, error) {
return &cmd, nil
}

func EvaluatePreprocessedFile(buildRoot string, command *CompilerCommand) ([]byte, error) {
func EvaluatePreprocessedFile(buildRoot string, baseDir string, command *CompilerCommand) ([]byte, error) {
// make the temporary file
tmpfile, err := ioutil.TempFile("", "ctc-")
if err != nil {
Expand All @@ -71,7 +72,8 @@ func EvaluatePreprocessedFile(buildRoot string, command *CompilerCommand) ([]byt
// build up all of the args
args := make([]string, 0, len(command.Arguments)+10)
args = append(args, command.Arguments...)
args = append(args, "-E", "-o", filename, command.InputPath)
// the -P flag drops linemarkers which contain absolute paths to headers
args = append(args, "-E", "-P", "-o", filename, command.InputPath)

// run the preprocessor
cmd := exec.Command(command.Compiler, args...)
Expand All @@ -82,15 +84,23 @@ func EvaluatePreprocessedFile(buildRoot string, command *CompilerCommand) ([]byt
}

// read the contents of the file am hash it
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()

hasher := sha256.New()
if _, err := io.Copy(hasher, f); err != nil {
return nil, err
if len(baseDir) == 0 {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes a bug that is fixed in #17. The file is not closed before calling os.Remove at line 110, which results in an error.


if _, err := io.Copy(hasher, f); err != nil {
return nil, err
}
} else {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
hasher.Write(bytes.ReplaceAll(data, []byte(baseDir), []byte(".")))
}

// compute the final digest
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import (
"os/exec"
"os/user"
"path"
"path/filepath"
)

const VERSION = "0.3.0"

type Configuration struct {
ClangTidyPath string `json:"clang_tidy_path"`
BaseDir string `json:"base_dir"`
GcsConfig *caches.GcsConfiguration `json:"gcs,omitempty"`
}

Expand Down Expand Up @@ -61,6 +63,9 @@ func readConfigEnv(cfg *Configuration) {
if envPath := os.Getenv("CLANG_TIDY_CACHE_BINARY"); len(envPath) > 0 {
cfg.ClangTidyPath = envPath
}
if envBaseDir := os.Getenv("CLANG_TIDY_CACHE_BASEDIR"); len(envBaseDir) > 0 {
cfg.BaseDir = filepath.Clean(envBaseDir)
}
}

func loadConfiguration() (*Configuration, error) {
Expand Down Expand Up @@ -152,7 +157,7 @@ func evaluateTidyCommand(cfg *Configuration, wd string, args []string, cache cac
invocation = other

// compute the finger print for the file
computedFingerPrint, err := caches.ComputeFingerPrint(cfg.ClangTidyPath, invocation, wd, args)
computedFingerPrint, err := caches.ComputeFingerPrint(cfg.ClangTidyPath, cfg.BaseDir, invocation, wd, args)
if err != nil {
return err
}
Expand Down