2016-07-13 62 views
0

我想使用go lint工具爲bazel生成BUILD文件。我可以在Bazel中生成BUILD文件嗎?

我想一展身手的可執行文件,將這樣的bash腳本執行的東西:

#!/bin/bash 

cat <<EOF > BUILD 
# THIS FILE IS AUTOGENERATED 

package(default_visibility = ["//visibility:public"]) 

load("//go:def.bzl", "go_prefix", "go_library", "go_binary", "go_test") 
EOF 

for pkg in `go list -f {{.ImportPath}} ./...`; do 
    go list -f "`cat test.in`" $pkg >> "BUILD"; 
done 

buildifier -mode fix BUILD 

的test.in文件包含:

{{ if eq .Name "main" }} 
go_binary 
{{else}} 
go_library 
{{end}} 
("{{.Name}}", 
    srcs=[ 
     {{range .GoFiles}} 
      "{{.}}", 
     {{end}} 
    ], 
    deps=[ 
     {{range .Imports}} 
      "{{.}}", 
     {{end}} 
    ], 
    csrcs=[ 
     {{range .CgoFiles}} 
      "{{.}}", 
     {{end}} 
    ], 
    swig_srcs=[ 
     {{range .SwigFiles}} 
      "{{.}}", 
     {{end}} 
    ], 
    cxxswig=[ 
     {{range .SwigCXXFiles}} 
      "{{.}}", 
     {{end}} 
    ], 
    cflags=[ 
     {{range .CgoCFLAGS}} 
      "{{.}}", 
     {{end}} 
    ], 
    cxxflags=[ 
     {{range .CgoCXXFLAGS}} 
      "{{.}}", 
     {{end}} 
    ], 
    cppflags=[ 
     {{range .CgoCPPFLAGS}} 
      "{{.}}", 
     {{end}} 
    ],   
    {{ with $ctx := context}} 
     {{$ctx.InstallSuffix}} 
    {{end}}  
) 

這產生:

# THIS FILE IS AUTOGENERATED 

package(default_visibility = ["//visibility:public"]) 

load("//go:def.bzl", "go_prefix", "go_library", "go_binary", "go_test") 

go_library 

(
    "tensorflow", 
    srcs = [ 
     "doc.go", 
     "gen.go", 
     "graph.go", 
     "session.go", 
    ], 
    deps = [ 
     "C", 
     "encoding/binary", 
     "fmt", 
     "github.com/golang/protobuf/proto", 
     "github.com/tensorflow/tensorflow/tensorflow/contrib/go/proto", 
     "math", 
     "reflect", 
     "runtime", 
     "strings", 
     "unsafe", 
    ], 
    csrcs = [ 
     "lib.go", 
     "tensor.go", 
    ], 
    swig_srcs = [ 
    ], 
    cxxswig = [ 
     "tensorflow.swigcxx", 
    ], 
    cflags = [ 
     "-I/Users/fmilo/workspace/gopath/src/github.com/tensorflow/tensorflow/tensorflow/contrib/go/../../../", 
    ], 
    cxxflags = [ 
     "-I/Users/fmilo/workspace/gopath/src/github.com/tensorflow/tensorflow/tensorflow/contrib/go/../../../", 
     "-std=c++11", 
    ], 
    cppflags = [ 
    ], 
) 

go_library 

(
    "tensorflow", 
    srcs = [ 
     "allocation_description.pb.go", 
     "attr_value.pb.go", 
     "config.pb.go", 
     "cost_graph.pb.go", 
     "device_attributes.pb.go", 
     "event.pb.go", 
     "function.pb.go", 
     "graph.pb.go", 
     "kernel_def.pb.go", 
     "log_memory.pb.go", 
     "master.pb.go", 
     "memmapped_file_system.pb.go", 
     "meta_graph.pb.go", 
     "named_tensor.pb.go", 
     "op_def.pb.go", 
     "queue_runner.pb.go", 
     "saved_tensor_slice.pb.go", 
     "saver.pb.go", 
     "step_stats.pb.go", 
     "summary.pb.go", 
     "tensor.pb.go", 
     "tensor_description.pb.go", 
     "tensor_shape.pb.go", 
     "tensor_slice.pb.go", 
     "tensorflow_server.pb.go", 
     "test_log.pb.go", 
     "tf_ops_def.go", 
     "types.pb.go", 
     "variable.pb.go", 
     "versions.pb.go", 
     "worker.pb.go", 
    ], 
    deps = [ 
     "fmt", 
     "github.com/golang/protobuf/proto", 
     "github.com/golang/protobuf/ptypes/any", 
     "math", 
    ], 
    csrcs = [ 
    ], 
    swig_srcs = [ 
    ], 
    cxxswig = [ 
    ], 
    cflags = [ 
    ], 
    cxxflags = [ 
    ], 
    cppflags = [ 
    ], 
) 

go_library 

(
    "tensorflow_error", 
    srcs = [ 
     "error_codes.pb.go", 
    ], 
    deps = [ 
     "fmt", 
     "github.com/golang/protobuf/proto", 
     "math", 
    ], 
    csrcs = [ 
    ], 
    swig_srcs = [ 
    ], 
    cxxswig = [ 
    ], 
    cflags = [ 
    ], 
    cxxflags = [ 
    ], 
    cppflags = [ 
    ], 
) 

go_library 

(
    "tensorflow_grpc", 
    srcs = [ 
     "master_service.pb.go", 
     "worker_service.pb.go", 
    ], 
    deps = [ 
     "fmt", 
     "github.com/golang/protobuf/proto", 
     "math", 
     "tensorflow/core/protobuf", 
    ], 
    csrcs = [ 
    ], 
    swig_srcs = [ 
    ], 
    cxxswig = [ 
    ], 
    cflags = [ 
    ], 
    cxxflags = [ 
    ], 
    cppflags = [ 
    ], 
) 

當然上面的BUILD文件還沒有工作,但我想先確定一下,我的方法m追求是有效的。

  • 如何在bazel中指定pre-BUILD階段?
  • 有可能嗎?
+0

我們一般做在谷歌是手動運行,當你需要它(也可以在IDE自動化)的代碼生成器,並在源代碼控制輸出檢查。 – Laurent

回答

1

通常,您不能生成BUILD文件,它們必須在構建開始時存在。 Bazel的一般構建階段是:

  • 解析您在命令行上指定的目標。
  • 查找構建文件在每個目錄的指定。
  • 解析構建這些目標提(等)的文件。
  • 生成的動作樹來執行(例如,「運行shell腳本」)。
  • 實際執行的操作。

因此,在執行腳本時,讀取BUILD文件的時間已經過去了。

此外,輸出放在自己的目錄(在巴澤爾出),使他們甚至不會出現在你的源代碼樹沒有改寫(munging)。

也就是說......

雲雀遠程倉庫做讓你執行代碼來寫文件,包括生成文件,在其他存儲庫。所以,你可以從理論上說,有這樣的事(未經測試):

def _gen_repo_impl(repo_ctx): 
    # Writes external/reponame/BUILD 
    repo_ctx.execute("/your/bash/script.sh") 
    for path in repo_ctx.attr.paths: 
    repo_ctx.symlink(path, path.basename) 

generated_repo = repository_rule(
    implementation = _gen_repo_impl, 
    attrs = {paths = attr.label_list(allow_files = True)} 
) 

然後在您的工作區文件,這樣做:

load('//path/to/your:genrepo.bzl', 'generated_repo') 
generated_repo(
    name = "pwd", 
    paths = ["src", "test", "third_party"], 
) 

然後,你可以建立/取決於@pwd//...和知識庫規則將生成一個外部回購協議與src,測試,和third_party符號鏈接(理論上...我的代碼可能有一些錯別字)。

+0

謝謝!我想知道這是否是bazel項目的公認行爲?有什麼缺點嗎? – fabrizioM

+0

我的意思是,這是一個有點hacky和脆弱(你正在手動指定可能會改變的目錄)。 – kristina

相關問題