diff --git a/.travis.yml b/.travis.yml index 3d3aa6c..f55637a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: go go: - - 1.7.x - - 1.8.x + - 1.10.x + - 1.11.x - master diff --git a/Protobuild.toml b/Protobuild.toml index 466a235..3f1e6cb 100644 --- a/Protobuild.toml +++ b/Protobuild.toml @@ -44,9 +44,9 @@ plugins = ["grpc"] # packages = ["github.com/gogo/protobuf"] # Paths that will be added untouched to the end of the includes. We use - # `/usr/local/include` to pickup the common install location of protobuf. + # a few paths to pickup the common install locations for protobuf. # This is the default. - # after = ["usr/local/include"] + # after = ["usr/local/include", "/usr/include"] # This section let's us map protobuf imports to Go packages. These will become # `-M` directives in the call to the go protobuf generator. diff --git a/config.go b/config.go index 383cc0c..f558f86 100644 --- a/config.go +++ b/config.go @@ -51,7 +51,7 @@ func newDefaultConfig() config { After []string }{ Before: []string{"."}, - After: []string{"/usr/local/include"}, + After: []string{"/usr/local/include", "/usr/include"}, }, } } diff --git a/descriptors.go b/descriptors.go index 788f47b..1c101af 100644 --- a/descriptors.go +++ b/descriptors.go @@ -18,9 +18,10 @@ type descriptorSet struct { merged descriptor.FileDescriptorSet seen map[string]struct{} ignoreFiles map[string]struct{} + descProto string } -func newDescriptorSet(ignoreFiles ...string) *descriptorSet { +func newDescriptorSet(ignoreFiles []string, d string) *descriptorSet { ifm := make(map[string]struct{}, len(ignoreFiles)) for _, ignore := range ignoreFiles { ifm[ignore] = struct{}{} @@ -28,6 +29,7 @@ func newDescriptorSet(ignoreFiles ...string) *descriptorSet { return &descriptorSet{ seen: make(map[string]struct{}), ignoreFiles: ifm, + descProto: d, } } @@ -54,7 +56,7 @@ func (d *descriptorSet) add(descs ...*descriptor.FileDescriptorProto) { // // This is equivalent to the following command: // -// cat merged.pb | protoc --decode google.protobuf.FileDescriptorSet /usr/local/include/google/protobuf/descriptor.proto +// cat merged.pb | protoc --decode google.protobuf.FileDescriptorSet /path/to/google/protobuf/descriptor.proto func (d *descriptorSet) marshalTo(w io.Writer) error { p, err := proto.Marshal(&d.merged) if err != nil { @@ -65,10 +67,7 @@ func (d *descriptorSet) marshalTo(w io.Writer) error { "protoc", "--decode", "google.protobuf.FileDescriptorSet", - - // TODO(stevvooe): Come up with better way to resolve this path. - "-I/usr/local/include", - "/usr/local/include/google/protobuf/descriptor.proto", + d.descProto, } cmd := exec.Command(args[0], args[1:]...) @@ -76,7 +75,9 @@ func (d *descriptorSet) marshalTo(w io.Writer) error { cmd.Stdout = w cmd.Stderr = os.Stderr - fmt.Println(strings.Join(args, " ")) + if !quiet { + fmt.Println(strings.Join(args, " ")) + } return cmd.Run() } diff --git a/main.go b/main.go index ebc9e40..e75875d 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "log" "os" "os/exec" + "path" "path/filepath" "strings" "syscall" @@ -69,10 +70,15 @@ func main() { } } + descProto, err := descriptorProto(c.Includes.After) + if err != nil { + log.Fatalln(err) + } + // Aggregate descriptors for each descriptor prefix. descriptorSets := map[string]*descriptorSet{} for _, stable := range c.Descriptors { - descriptorSets[stable.Prefix] = newDescriptorSet(stable.IgnoreFiles...) + descriptorSets[stable.Prefix] = newDescriptorSet(stable.IgnoreFiles, descProto) } shouldGenerateDescriptors := func(p string) bool { @@ -333,6 +339,22 @@ func gopathJoin(gopath, element string) string { return strings.Join(elements, string(filepath.ListSeparator)) } +// descriptorProto returns the full path to google/protobuf/descriptor.proto +// which might be different depending on whether it was installed. The argument +// is the list of paths to check. +func descriptorProto(paths []string) (string, error) { + const descProto = "google/protobuf/descriptor.proto" + + for _, dir := range paths { + file := path.Join(dir, descProto) + if _, err := os.Stat(file); err == nil { + return file, err + } + } + + return "", fmt.Errorf("File %q not found (looked in: %v)", descProto, paths) +} + var errVendorNotFound = fmt.Errorf("no vendor dir found") // closestVendorDir walks up from dir until it finds the vendor directory.