diff --git a/README.md b/README.md index cf13edc..7ce7b28 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,17 @@ It also wraps calls to the linker in order to: Finally, the tool requires the use of the `-trimpath` build flag, to ensure the binary doesn't include paths from the current filesystem. +### Exclude +Identifiers can be excluded from garble by adding a comment in the form of a single line +// Identifier garble:exclude + +or multiline + +// Identifier some documentation +// garble:exclude + +comment before it. + ### Options By default, the tool garbles the packages under the current module. If not diff --git a/go.mod b/go.mod index 5d2498c..e4d9df0 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module mvdan.cc/garble go 1.14 require ( - github.com/rogpeppe/go-internal v1.5.2 - golang.org/x/tools v0.0.0-20200330040139-fa3cc9eebcfe + github.com/rogpeppe/go-internal v1.6.0 + golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375 ) diff --git a/go.sum b/go.sum index fb11bd0..c2d9daf 100644 --- a/go.sum +++ b/go.sum @@ -3,9 +3,9 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/rogpeppe/go-internal v1.5.2 h1:qLvObTrvO/XRCqmkKxUlOBc48bI3efyDuAZe25QiF0w= -github.com/rogpeppe/go-internal v1.5.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= -github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/rogpeppe/go-internal v1.6.0 h1:IZRgg4sfrDH7nsAD1Y/Nwj+GzIfEwpJSLjCaNC3SbsI= +github.com/rogpeppe/go-internal v1.6.0/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= @@ -18,8 +18,8 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20200330040139-fa3cc9eebcfe h1:sOd+hT8wBUrIFR5Q6uQb/rg50z8NjHk96kC4adwvxjw= -golang.org/x/tools v0.0.0-20200330040139-fa3cc9eebcfe/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8= +golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375 h1:SjQ2+AKWgZLc1xej6WSzL+Dfs5Uyd5xcZH1mGC411IA= +golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/main.go b/main.go index 53ae290..c717488 100644 --- a/main.go +++ b/main.go @@ -286,13 +286,10 @@ func transformCompile(args []string) ([]string, error) { return nil, err } // log.Printf("%#v", ids) - var files []*ast.File - for _, path := range paths { - file, err := parser.ParseFile(fset, path, nil, parser.ParseComments) - if err != nil { - return nil, err - } - files = append(files, file) + + files, err := parseFilesFromPaths(paths) + if err != nil { + return nil, err } info := &types.Info{ @@ -304,6 +301,7 @@ func transformCompile(args []string) ([]string, error) { } tempDir, err := ioutil.TempDir("", "garble-build") + log.Println(tempDir) if err != nil { return nil, err } @@ -473,6 +471,7 @@ func transformGo(file *ast.File, info *types.Info) *ast.File { } return true } + pkg := obj.Pkg() if pkg == nil { return true // universe scope @@ -484,12 +483,14 @@ func transformGo(file *ast.File, info *types.Info) *ast.File { pkg = obj.Pkg() } - if pkg.Name() == "main" && obj.Exported() && obj.Parent() == pkg.Scope() { + if pkg.Name() == "main" && obj.Exported() && obj.Parent() == pkg.Scope() || excludeObj(obj, origComments) { // TODO: only do this when -buildmode is plugin? what // about other -buildmode options? + + log.Println("here", obj.Name()) return true // could be a Go plugin API } - // log.Printf("%#v %T", node, obj) + //log.Printf("%#v %T", node, obj) switch x := obj.(type) { case *types.Var: if x.IsField() && x.Exported() { @@ -539,6 +540,7 @@ func transformGo(file *ast.File, info *types.Info) *ast.File { // log.Printf("%q hashed with %q to %q", orig, buildID, node.Name) return true } + return astutil.Apply(file, pre, nil).(*ast.File) } @@ -640,3 +642,45 @@ func flagSetValue(flags []string, name, value string) []string { } return append(flags, name+"="+value) } + +func parseFilesFromPaths(paths []string) (files []*ast.File, err error) { + for _, path := range paths { + file, err := parser.ParseFile(fset, path, nil, parser.ParseComments) + if err != nil { + return nil, err + } + files = append(files, file) + } + + return +} + +func excludeObj(obj types.Object, comments []*ast.CommentGroup) bool { + if obj.Exported() { + for _, commentGroup := range comments { + if commentGroup.Pos() > obj.Pos() { + continue + } + + var groupHasObjName bool + var groupHasExclude bool + for _, comment := range commentGroup.List { + if strings.Contains(comment.Text, obj.Name()) { + groupHasObjName = true + } + + if strings.Contains(comment.Text, "garble:exclude") { + groupHasExclude = true + } + } + + if groupHasExclude && groupHasObjName { + + log.Println("skipped", obj.Name()) + return true + } + } + } + + return false +} diff --git a/testdata/scripts/imports.txt b/testdata/scripts/imports.txt index 04b82d4..2795e99 100644 --- a/testdata/scripts/imports.txt +++ b/testdata/scripts/imports.txt @@ -3,6 +3,7 @@ exec ./main cmp stdout main.stdout ! binsubstr main$exe 'ImportedVar' 'ImportedConst' 'ImportedFunc' 'ImportedType' 'main.go' 'imported.go' +binsubstr main$exe 'ImportedAPI' [short] stop # checking that the build is reproducible is slow @@ -24,6 +25,7 @@ package main import ( "fmt" _ "unsafe" + "reflect" "test/main/imported" @@ -38,6 +40,9 @@ func main() { fmt.Println(imported.ImportedConst) imported.ImportedFunc('x') fmt.Println(imported.ImportedType(3)) + + api := new(imported.ImportedAPI) + fmt.Println(reflect.TypeOf(api)) linkedPrintln(nil) fmt.Println(quote.Go()) } @@ -45,6 +50,7 @@ func main() { imported var value imported const value 3 +*imported.ImportedAPI Don't communicate by sharing memory, share memory by communicating. -- imported/imported.go -- @@ -59,3 +65,6 @@ func ImportedFunc(param rune) string { } type ImportedType int + +// ImportedAPI garble:exclude +type ImportedAPI int \ No newline at end of file