refactor(buildGo): Minor cleanups of buildGo implementation

This commit is contained in:
Vincent Ambo 2019-11-22 16:14:14 +00:00
parent 78ca29dd23
commit 5a89a0265b

View file

@ -3,6 +3,7 @@
# #
# TODO(tazjin): Go through Bazel rules_go options and implement corresponding flags # TODO(tazjin): Go through Bazel rules_go options and implement corresponding flags
# TODO(tazjin): Refactor to include /golang/protobuf/descriptor in goProto deps # TODO(tazjin): Refactor to include /golang/protobuf/descriptor in goProto deps
# TODO(tazjin): Find a way to expose documentation (esp. for generated stuff)
{ pkgs, ... }@args: { pkgs, ... }@args:
@ -10,16 +11,13 @@ let
inherit (builtins) inherit (builtins)
attrNames attrNames
baseNameOf baseNameOf
currentSystem elemAt
filter filter
isString
head
map map
match match
readDir readDir
toFile replaceStrings
toPath toPath;
toString;
inherit (pkgs) bash lib go runCommand fetchFromGitHub protobuf; inherit (pkgs) bash lib go runCommand fetchFromGitHub protobuf;
@ -66,17 +64,17 @@ let
in (runCommand "golib-${name}" {} '' in (runCommand "golib-${name}" {} ''
mkdir -p $out/${path} mkdir -p $out/${path}
${srcList path (map (s: "${s}") srcs)} ${srcList path (map (s: "${s}") srcs)}
${go}/bin/go tool compile -o $out/${path}.a -trimpath=$PWD -p ${path} ${includeSources uniqueDeps} ${spaceOut srcs} ${go}/bin/go tool compile -o $out/${path}.a -trimpath=$PWD -trimpath=${go} -p ${path} ${includeSources uniqueDeps} ${spaceOut srcs}
'') // { goDeps = uniqueDeps; }; '') // { goDeps = uniqueDeps; };
# Build a Go library out of the specified proto/gRPC file. # Build a Go library out of the specified protobuf definition.
proto = { name, proto, path ? name }: package { proto = { name, proto, path ? name, protocFlags ? "", extraDeps ? [] }: package {
inherit name path; inherit name path;
deps = [ goProto ]; deps = [ goProto ] ++ extraDeps;
srcs = lib.singleton (runCommand "goproto-${name}.pb.go" {} '' srcs = lib.singleton (runCommand "goproto-${name}.pb.go" {} ''
cp ${proto} ${baseNameOf proto} cp ${proto} ${baseNameOf proto}
${protobuf}/bin/protoc --plugin=${protocGo}/bin/protoc-gen-go \ ${protobuf}/bin/protoc --plugin=${protocGo}/bin/protoc-gen-go \
--go_out=import_path=${baseNameOf path}:. ${baseNameOf proto} --go_out=${protocFlags}import_path=${baseNameOf path}:. ${baseNameOf proto}
mv *.pb.go $out mv *.pb.go $out
''); '');
}; };
@ -89,27 +87,25 @@ let
sha256 = "0fynqrim022x9xi2bivkw19npbz4316v4yr7mb677s9s36z4dc4h"; sha256 = "0fynqrim022x9xi2bivkw19npbz4316v4yr7mb677s9s36z4dc4h";
}; };
protoPart = path: deps: package {
inherit deps;
name = replaceStrings ["/"] ["_"] path;
path = "github.com/golang/protobuf/${path}";
srcs = goFilesIn (toPath "${proto-go-src}/${path}");
};
goProto = goProto =
let let
protobuf = package { protobuf = package {
name = "protobuf"; name = "protobuf";
path = "github.com/golang/protobuf/proto"; path = "github.com/golang/protobuf/proto";
# TODO(tazjin): How does this build toggle work?
srcs = filter srcs = filter
(f: (match "(.*)/pointer_reflect.go" f) == null) (f: (match "(.*)/pointer_reflect.go" f) == null)
(goFilesIn (toPath "${proto-go-src}/proto")); (goFilesIn (toPath "${proto-go-src}/proto"));
}; };
type = name: package { type = name: protoPart "ptypes/${name}" [ protobuf ];
name = "ptypes-${name}"; descriptor = protoPart "descriptor" [ protobuf ];
path = "github.com/golang/protobuf/ptypes/${name}";
srcs = goFilesIn (toPath "${proto-go-src}/ptypes/${name}");
deps = [ protobuf ];
};
# descriptor = package {
# name = "descriptor";
# path = "github.com/golang/protobuf/descriptor";
# deps = [ protobuf ];
# srcs = goFilesIn (toPath "${proto-go-src}/descriptor");
# };
ptypes = package { ptypes = package {
name = "ptypes"; name = "ptypes";
path = "github.com/golang/protobuf/ptypes"; path = "github.com/golang/protobuf/ptypes";
@ -125,37 +121,14 @@ let
}; };
in protobuf // { goDeps = allDeps (protobuf.goDeps ++ [ ptypes ]); }; in protobuf // { goDeps = allDeps (protobuf.goDeps ++ [ ptypes ]); };
protocDescriptor = (protoPart "protoc-gen-go/descriptor" [ goProto ]);
protocGo = protocGo =
let let
descriptor = package { generator = protoPart "protoc-gen-go/generator" [
name = "descriptor"; (protoPart "protoc-gen-go/generator/internal/remap" [])
path = "github.com/golang/protobuf/protoc-gen-go/descriptor"; (protoPart "protoc-gen-go/plugin" [ protocDescriptor ])
srcs = goFilesIn (toPath "${proto-go-src}/protoc-gen-go/descriptor"); ];
deps = [ goProto ]; grpc = protoPart "protoc-gen-go/grpc" [ generator ];
};
plugin = package {
name = "plugin";
path = "github.com/golang/protobuf/protoc-gen-go/plugin";
srcs = goFilesIn (toPath "${proto-go-src}/protoc-gen-go/plugin");
deps = [ descriptor ];
};
remap = package {
name = "remap";
path = "github.com/golang/protobuf/protoc-gen-go/generator/internal/remap";
srcs = goFilesIn (toPath "${proto-go-src}/protoc-gen-go/generator/internal/remap");
};
generator = package {
name = "generator";
path = "github.com/golang/protobuf/protoc-gen-go/generator";
srcs = goFilesIn (toPath "${proto-go-src}/protoc-gen-go/generator");
deps = [ descriptor remap plugin ];
};
grpc = package {
name = "grpc";
path = "github.com/golang/protobuf/protoc-gen-go/grpc";
deps = [ generator ];
srcs = goFilesIn (toPath "${proto-go-src}/protoc-gen-go/grpc");
};
in program { in program {
name = "protoc-gen-go"; name = "protoc-gen-go";
deps = [ goProto grpc generator ]; deps = [ goProto grpc generator ];