From dcad3fa3f6846befc13bec259938303b13e82426 Mon Sep 17 00:00:00 2001 From: Yi Sun Date: Wed, 22 May 2019 17:40:13 -0700 Subject: [PATCH] Update bazel essentials --- build_rules/github.bzl | 171 ++++++++++++++++++++-------------------- build_rules/go.bzl | 39 ++++----- build_rules/init_go.bzl | 20 ++--- build_rules/lls.bzl | 131 ++++++++++++++++-------------- format-build.sh | 2 + 5 files changed, 184 insertions(+), 179 deletions(-) diff --git a/build_rules/github.bzl b/build_rules/github.bzl index 1ae33e0d..caa220a4 100644 --- a/build_rules/github.bzl +++ b/build_rules/github.bzl @@ -5,99 +5,100 @@ github tar.gz easier. """ load("@io_bazel_rules_go//go:def.bzl", "go_repository") +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") def _github_tar_gz_url(package, commit, tag): - if commit: - return "https://github.com/" + package + "/archive/" + commit + ".tar.gz" - else: - return "https://github.com/" + package + "/archive/" + tag + ".tar.gz" + if commit: + return "https://github.com/" + package + "/archive/" + commit + ".tar.gz" + else: + return "https://github.com/" + package + "/archive/" + tag + ".tar.gz" def _tar_gz_prefix(package, commit, tag): - basename = package.split("/")[-1] - if commit: - return basename + "-" + commit - else: - # Github omits the first "v" when produce .tar.gz files if the tags begin - # with "v". E.g., the prefix to strip for package of version v1.0.2 is - # "package-1.0.2", yet for version "release-1.8.0" is - # "package-release-1.8.0". - if tag.startswith("v"): - return basename + "-" + tag[1:] - return basename + "-" + tag + basename = package.split("/")[-1] + if commit: + return basename + "-" + commit + else: + # Github omits the first "v" when produce .tar.gz files if the tags begin + # with "v". E.g., the prefix to strip for package of version v1.0.2 is + # "package-1.0.2", yet for version "release-1.8.0" is + # "package-release-1.8.0". + if tag.startswith("v"): + return basename + "-" + tag[1:] + return basename + "-" + tag -def github_http_archive(name, package, commit=None, tag=None, sha256=""): - """Adding bazel repository from github. +def github_http_archive(name, package, commit = None, tag = None, sha256 = ""): + """Adding bazel repository from github. - Args: - name: (string) Target name, should be com_github_{fullpath}. This should be - case sensitive, e.g., com_github_Shopify_sarama. - package: (string) Package name without "github.com/", E.g., - github.com/Shopify/sarama has package name Shopify/sarama. No trailing - slash is allowed. - commit: (string) Commit number. - tag: (string) Commit tag. Exactly one of commit or tag is required. - sha256: (string) Optional checksum. - """ - return native.http_archive( - name = name, - urls = [_github_tar_gz_url(package, commit, tag)], - strip_prefix = _tar_gz_prefix(package, commit, tag), - sha256 = sha256, - ) + Args: + name: (string) Target name, should be com_github_{fullpath}. This should be + case sensitive, e.g., com_github_Shopify_sarama. + package: (string) Package name without "github.com/", E.g., + github.com/Shopify/sarama has package name Shopify/sarama. No trailing + slash is allowed. + commit: (string) Commit number. + tag: (string) Commit tag. Exactly one of commit or tag is required. + sha256: (string) Optional checksum. + """ + return http_archive( + name = name, + urls = [_github_tar_gz_url(package, commit, tag)], + strip_prefix = _tar_gz_prefix(package, commit, tag), + sha256 = sha256, + ) -def github_new_http_archive(name, package, build_file=None, build_file_content=None, commit=None, tag=None, sha256=""): - """Adding bazel repository from github. +def github_new_http_archive(name, package, build_file = None, build_file_content = None, commit = None, tag = None, sha256 = ""): + """Adding bazel repository from github. - Args: - name: (string) Target name, should be com_github_{fullpath}. This should be - case sensitive, e.g., com_github_Shopify_sarama. - package: (string) Package name without "github.com/", E.g., - github.com/Shopify/sarama has package name Shopify/sarama. No trailing - slash is allowed. - build_file: (string) Build file path. - build_file_content: (string) Build file content. - commit: (string) Commit number. - tag: (string) Commit tag. Exactly one of commit or tag is required. - sha256: (string) Optional checksum. - """ - return native.new_http_archive( - name = name, - urls = [_github_tar_gz_url(package, commit, tag)], - strip_prefix = _tar_gz_prefix(package, commit, tag), - sha256 = sha256, - build_file = build_file, - build_file_content = build_file_content, - ) + Args: + name: (string) Target name, should be com_github_{fullpath}. This should be + case sensitive, e.g., com_github_Shopify_sarama. + package: (string) Package name without "github.com/", E.g., + github.com/Shopify/sarama has package name Shopify/sarama. No trailing + slash is allowed. + build_file: (string) Build file path. + build_file_content: (string) Build file content. + commit: (string) Commit number. + tag: (string) Commit tag. Exactly one of commit or tag is required. + sha256: (string) Optional checksum. + """ + return http_archive( + name = name, + urls = [_github_tar_gz_url(package, commit, tag)], + strip_prefix = _tar_gz_prefix(package, commit, tag), + sha256 = sha256, + build_file = build_file, + build_file_content = build_file_content, + ) -def github_go_repository(name, package, importpath="", commit=None, tag=None, sha256=""): - """Adding go repository from github and convert it to bazel repository. +def github_go_repository(name, package, importpath = "", commit = None, tag = None, sha256 = ""): + """Adding go repository from github and convert it to bazel repository. - Note that for now the dependencies will not be pulled in automatically. You - need to build and run wtools - (https://github.com/bazelbuild/rules_go/tree/master/go/tools/wtool) - to add all the dependencies. + Note that for now the dependencies will not be pulled in automatically. You + need to build and run wtools + (https://github.com/bazelbuild/rules_go/tree/master/go/tools/wtool) + to add all the dependencies. - Args: - name: (string) Target name, should be com_github_{fullpath}. This should be - case sensitive, e.g., com_github_Shopify_sarama. - package: (string) Package name without "github.com/", E.g., - github.com/Shopify/sarama has package name Shopify/sarama. No trailing - slash is allowed. The Go import path is - github.com/Shopify/sarama. - commit: (string) Commit number. - tag: (string) Commit tag. Exactly one of commit or tag is required. - importpath: (string) Optionally override the import path. - sha256: (string) Optional checksum. - """ - remote = "https://github.com/" + package - if importpath == "": - importpath = "github.com/" + package - return go_repository( - name = name, - importpath = importpath, - remote = remote, - vcs = "git", - commit = commit, - tag = tag, - sha256 = sha256, - ) + Args: + name: (string) Target name, should be com_github_{fullpath}. This should be + case sensitive, e.g., com_github_Shopify_sarama. + package: (string) Package name without "github.com/", E.g., + github.com/Shopify/sarama has package name Shopify/sarama. No trailing + slash is allowed. The Go import path is + github.com/Shopify/sarama. + commit: (string) Commit number. + tag: (string) Commit tag. Exactly one of commit or tag is required. + importpath: (string) Optionally override the import path. + sha256: (string) Optional checksum. + """ + remote = "https://github.com/" + package + if importpath == "": + importpath = "github.com/" + package + return go_repository( + name = name, + importpath = importpath, + remote = remote, + vcs = "git", + commit = commit, + tag = tag, + sha256 = sha256, + ) diff --git a/build_rules/go.bzl b/build_rules/go.bzl index 502fc488..ad70387c 100644 --- a/build_rules/go.bzl +++ b/build_rules/go.bzl @@ -1,31 +1,22 @@ """Go related build rules.""" -load("@io_bazel_rules_go//go:def.bzl", "go_binary", rules_go_go_repositories = "go_repositories") +load( + "@io_bazel_rules_go//go:def.bzl", + "go_binary", + rules_go_go_repositories = "go_repositories", +) -def go_repositories(): - native.new_local_repository( - name = "local_go_linux", - path = "/usr/lib/go", # docker go installation path - build_file_content = "", - ) - native.new_local_repository( - name = "local_go_mac", - path = "/usr/local/go", # default os x installation path - build_file_content = "", - ) - rules_go_go_repositories( - go_darwin = "@local_go_mac", - go_linux = "@local_go_linux", - ) +# No op for backward compatibility. +go_repositories = rules_go_go_repositories def static_go_binary(name, **args): - """Produce statically linked go binary which may be put in a minimal docker. + """Produce statically linked go binary which may be put in a minimal docker. - The typical go binary produced is about 12M, which is put into a minimal - docker such as alpine to produce deployable images of size less than 20M. + The typical go binary produced is about 12M, which is put into a minimal + docker such as alpine to produce deployable images of size less than 20M. - WARNING: It is unfortunate that the link flags only work with gcc, not clang, - which in practice means the target will not build on Mac out of box. - """ - print("WARNING: static_go_binary is deprecated, use go_binary instead!") - go_binary(name=name, **args) + WARNING: It is unfortunate that the link flags only work with gcc, not clang, + which in practice means the target will not build on Mac out of box. + """ + print("WARNING: static_go_binary is deprecated, use go_binary instead!") + go_binary(name = name, **args) diff --git a/build_rules/init_go.bzl b/build_rules/init_go.bzl index 9f48363e..b04cf9c5 100644 --- a/build_rules/init_go.bzl +++ b/build_rules/init_go.bzl @@ -1,10 +1,12 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + def init_go(): - # NOTE(yi.sun): This is not the newest version of rules_go. However, we - # have to stuck with this version as later commits do not allow using local - # go installation, which becomes a problem in China as downloading 100M - # go installation packages takes forever. - native.http_archive( - name = "io_bazel_rules_go", - url = "https://github.com/lingochamp/rules_go/archive/d83bf7cf99979003908f58dd6fe2805e22a52d76.tar.gz", - strip_prefix = "rules_go-d83bf7cf99979003908f58dd6fe2805e22a52d76", - ) + # NOTE(yi.sun): This is not the newest version of rules_go. However, we + # have to stuck with this version as later version removes support for + # go_prefix, which requires us to rework ALL the repositories. + http_archive( + name = "io_bazel_rules_go", + url = "https://github.com/lingochamp/rules_go/archive/d88537f26fa530e32095957f374b26bdef9ead96.tar.gz", + sha256 = "7afb888865f383df09c170c65f5ede8a3aff4d463875b1aa92a5b58f304738de", + strip_prefix = "rules_go-d88537f26fa530e32095957f374b26bdef9ead96", + ) diff --git a/build_rules/lls.bzl b/build_rules/lls.bzl index 1e0308a2..9e8cfd05 100644 --- a/build_rules/lls.bzl +++ b/build_rules/lls.bzl @@ -3,34 +3,39 @@ load("@io_bazel_rules_go//go:def.bzl", "go_repository") def _fetch_commit(ctx, remote, ref): - result = ctx.execute(["git", "ls-remote", "--heads", remote]) - if result.return_code: - fail("%s: failed to query repo %s, err: %s" % (ctx.name, remote, result.stderr)) - # Every output line is in the form - # - for ln in result.stdout.splitlines(): - i = ln.rfind(ref) - if i < 0: - continue - commit = ln[:40] # SHA1 is 40 bytes - print("%s: ref %s from repo %s is at %s" % (ctx.name, ref, remote, commit)) - return commit - fail("%s: cannot find ref %s from repo %s" % (ctx.name, ref, remote)) + result = ctx.execute(["git", "ls-remote", "--heads", remote]) + if result.return_code: + fail("%s: failed to query repo %s, err: %s" % (ctx.name, remote, result.stderr)) + + # Every output line is in the form + # + for ln in result.stdout.splitlines(): + i = ln.rfind(ref) + if i < 0: + continue + commit = ln[:40] # SHA1 is 40 bytes + print("%s: ref %s from repo %s is at %s" % (ctx.name, ref, remote, commit)) + return commit + fail("%s: cannot find ref %s from repo %s" % (ctx.name, ref, remote)) def _lls_repository_head_impl(ctx): - result = ctx.execute([ - ctx.path(ctx.attr._fetch_repo), - "--dest", ctx.path(""), - "--remote", ctx.attr.remote, - "--rev", _fetch_commit(ctx, ctx.attr.remote, "refs/heads/master"), - "--vcs", "git", - "--importpath", ctx.attr.importpath, - ]) - if result.return_code: - fail("%s: failed to fetch from repo %s, err: %s" % (ctx.name, ctx.attr.remote, result.stderr)) + result = ctx.execute([ + ctx.path(ctx.attr._fetch_repo), + "--dest", + ctx.path(""), + "--remote", + ctx.attr.remote, + "--rev", + _fetch_commit(ctx, ctx.attr.remote, "refs/heads/master"), + "--vcs", + "git", + "--importpath", + ctx.attr.importpath, + ]) + if result.return_code: + fail("%s: failed to fetch from repo %s, err: %s" % (ctx.name, ctx.attr.remote, result.stderr)) _lls_repository_head = repository_rule( - implementation = _lls_repository_head_impl, attrs = { "importpath": attr.string(mandatory = True), "remote": attr.string(mandatory = True), @@ -44,48 +49,52 @@ _lls_repository_head = repository_rule( }, # Fetch the repository everytime env BAZEL_RUNID changes. environ = ["BAZEL_RUNID"], + implementation = _lls_repository_head_impl, ) -def lls_repository(name, path, commit=None, tag=None, use_local_version=0): - """A liulishuo repository. +def lls_repository(name, path, commit = None, tag = None, use_local_version = 0): + """A liulishuo repository. - If neither commit nor tag is specified, which should be the default case, then - we pull the refs/heads/master from the repository for each build. This ensures - that any internal change propagates to all the other repositories. + If neither commit nor tag is specified, which should be the default case, then + we pull the refs/heads/master from the repository for each build. This ensures + that any internal change propagates to all the other repositories. - name: (string) Name of the repository. - path: (string) Name relative to git.llsapp.com. E.g., - git.llsapp.com/common/protos will have path name common/protos. - commit: (string) Commit. - tag: (string) Tag. - use_local_version: (int) If 1, use the local mirror of the repository. This - assumes that the local folder structure mirrors that one on gitlab. - """ - if use_local_version: - return native.local_repository( - name = name, - path = "../../" + path, - ) - importpath = "git.llsapp.com/" + path - remote = "git@git.llsapp.com:" + path + ".git" - if commit or tag: - # This is a temporary hack as only go_repository supports pulling code using - # git@... protocol. Bazel only supports https, which happens to be not - # supported by our gitlab: (.) - # - # TODO(yi.sun): Figure out a way to always pull the master head. - return go_repository( + name: (string) Name of the repository. + path: (string) Name relative to git.llsapp.com. E.g., + git.llsapp.com/common/protos will have path name common/protos. + commit: (string) Commit. + tag: (string) Tag. + use_local_version: (int) If 1, use the local mirror of the repository. This + assumes that the local folder structure mirrors that one on gitlab. + """ + if use_local_version: + return native.local_repository( + name = name, + path = "../../" + path, + ) + importpath = "git.llsapp.com/" + path + remote = "git@git.llsapp.com:" + path + ".git" + if commit or tag: + # This is a temporary hack as only go_repository supports pulling code using + # git@... protocol. Bazel only supports https, which happens to be not + # supported by our gitlab: (.) + # + # TODO(yi.sun): Figure out a way to always pull the master head. + # + # TODO(yi.sun): Now that bazel has a pretty good support for git + # repository, consider removing this thing. + return go_repository( + name = name, + importpath = importpath, + remote = remote, + vcs = "git", + commit = commit, + tag = tag, + ) + + # No commit or tag specified, just pull from the master head. + return _lls_repository_head( name = name, importpath = importpath, remote = remote, - vcs = "git", - commit = commit, - tag = tag, ) - - # No commit or tag specified, just pull from the master head. - return _lls_repository_head( - name = name, - importpath = importpath, - remote = remote, - ) diff --git a/format-build.sh b/format-build.sh index 4222ce8f..568d4ec2 100644 --- a/format-build.sh +++ b/format-build.sh @@ -1,3 +1,5 @@ buildifier WORKSPACE find . -name BUILD | xargs buildifier + +find . -name *.bzl | xargs buildifier