Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(build): use kong_template_* rule for luarocks targets #11418

Merged
merged 2 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 148 additions & 12 deletions build/build_system.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,165 @@ kong_rules_group = rule(
},
)

def _kong_template_file_impl(ctx):
_kong_template_attrs = {
"template": attr.label(
mandatory = True,
allow_single_file = True,
),
"output": attr.output(
mandatory = True,
),
"substitutions": attr.string_dict(),
"srcs": attr.label_list(allow_files = True, doc = "List of locations to expand the template, in target configuration"),
"tools": attr.label_list(allow_files = True, cfg = "exec", doc = "List of locations to expand the template, in exec configuration"),
"is_executable": attr.bool(default = False),
# hidden attributes
"_cc_toolchain": attr.label(
default = "@bazel_tools//tools/cpp:current_cc_toolchain",
),
}

def _render_template(ctx, output):
substitutions = dict(ctx.attr.substitutions)
for l in ctx.attr.srcs + ctx.attr.tools:
files = l.files.to_list()
if len(files) == 1:
p = files[0].path
else:
p = "/".join(files[0].path.split("/")[:-1]) # get the directory
substitutions["{{%s}}" % l.label] = p

substitutions["{{CC}}"] = ctx.attr._cc_toolchain[cc_common.CcToolchainInfo].compiler_executable

# yes, not a typo, use gcc for linker
substitutions["{{LD}}"] = substitutions["{{CC}}"]

ctx.actions.expand_template(
template = ctx.file.template,
output = ctx.outputs.output,
substitutions = ctx.attr.substitutions,
output = output,
substitutions = substitutions,
is_executable = ctx.attr.is_executable,
)

def _kong_template_file_impl(ctx):
_render_template(ctx, ctx.outputs.output)

return [
DefaultInfo(files = depset([ctx.outputs.output])),
]

kong_template_file = rule(
implementation = _kong_template_file_impl,
attrs = _kong_template_attrs,
)

def _kong_template_genrule_impl(ctx):
f = ctx.actions.declare_file(ctx.attr.name + ".rendered.sh")
_render_template(ctx, f)

ctx.actions.run_shell(
outputs = [ctx.outputs.output],
inputs = ctx.files.srcs + ctx.files.tools + [f],
command = "{} {}".format(f.path, ctx.outputs.output.path),
progress_message = ctx.attr.progress_message,
)

return [
# don't list f as files/real output
DefaultInfo(files = depset([ctx.outputs.output])),
]

kong_template_genrule = rule(
implementation = _kong_template_genrule_impl,
attrs = _kong_template_attrs | {
"progress_message": attr.string(doc = "Message to display when running the command"),
},
)

def _copyright_header(ctx):
paths = ctx.execute(["find", ctx.path("."), "-type", "f"]).stdout.split("\n")

copyright_content = ctx.read(ctx.path(Label("@kong//:distribution/COPYRIGHT-HEADER"))).replace("--", " ")
copyright_content_js = "/*\n" + copyright_content + "*/\n\n"
copyright_content_html = "<!--\n" + copyright_content + "-->\n\n"
for path in paths:
if path.endswith(".js") or path.endswith(".map") or path.endswith(".css"):
content = ctx.read(path)
if not content.startswith(copyright_content_js):
# the default enabled |legacy_utf8| leads to a double-encoded utf-8
# while writing utf-8 content read by |ctx.read|, let's disable it
ctx.file(path, copyright_content_js + content, legacy_utf8 = False)

elif path.endswith(".html"):
content = ctx.read(path)
if not content.startswith(copyright_content_html):
# the default enabled |legacy_utf8| leads to a double-encoded utf-8
# while writing utf-8 content read by |ctx.read|, let's disable it
ctx.file(path, copyright_content_html + content, legacy_utf8 = False)

def _github_release_impl(ctx):
ctx.file("WORKSPACE", "workspace(name = \"%s\")\n" % ctx.name)

if ctx.attr.build_file:
ctx.file("BUILD.bazel", ctx.read(ctx.attr.build_file))
elif ctx.attr.build_file_content:
ctx.file("BUILD.bazel", ctx.attr.build_file_content)

os_name = ctx.os.name
os_arch = ctx.os.arch

if os_arch == "aarch64":
os_arch = "arm64"
elif os_arch == "x86_64":
os_arch = "amd64"
elif os_arch != "amd64":
fail("Unsupported arch %s" % os_arch)

if os_name == "mac os x":
os_name = "macOS"
elif os_name != "linux":
fail("Unsupported OS %s" % os_name)

gh_bin = "%s" % ctx.path(Label("@gh_%s_%s//:bin/gh" % (os_name, os_arch)))
args = [gh_bin, "release", "download", ctx.attr.tag, "-R", ctx.attr.repo]
downloaded_file = None
if ctx.attr.pattern:
if "/" in ctx.attr.pattern or ".." in ctx.attr.pattern:
fail("/ and .. are not allowed in pattern")
downloaded_file = ctx.attr.pattern.replace("*", "_")
args += ["-p", ctx.attr.pattern]
elif ctx.attr.archive:
args.append("--archive=" + ctx.attr.archive)
downloaded_file = "gh-release." + ctx.attr.archive.split(".")[-1]
else:
fail("at least one of pattern or archive must be set")

args += ["-O", downloaded_file]

ret = ctx.execute(args)

if ret.return_code != 0:
gh_token_set = "GITHUB_TOKEN is set, is it valid?"
if not ctx.os.environ.get("GITHUB_TOKEN", ""):
gh_token_set = "GITHUB_TOKEN is not set, is this a private repo?"
fail("Failed to download release (%s): %s, exit: %d" % (gh_token_set, ret.stderr, ret.return_code))

ctx.extract(downloaded_file, stripPrefix = ctx.attr.strip_prefix)

# only used in EE: always skip here in CE
if not ctx.attr.skip_add_copyright_header and False:
_copyright_header(ctx)

github_release = repository_rule(
implementation = _github_release_impl,
attrs = {
"template": attr.label(
mandatory = True,
allow_single_file = True,
),
"output": attr.output(
mandatory = True,
),
"substitutions": attr.string_dict(),
"is_executable": attr.bool(default = False),
"tag": attr.string(mandatory = True),
"pattern": attr.string(mandatory = False),
"archive": attr.string(mandatory = False, values = ["zip", "tar.gz"]),
"strip_prefix": attr.string(default = "", doc = "Strip prefix from downloaded files"),
"repo": attr.string(mandatory = True),
"build_file": attr.label(allow_single_file = True),
"build_file_content": attr.string(),
"skip_add_copyright_header": attr.bool(default = False, doc = "Whether to inject COPYRIGHT-HEADER into downloaded files, only required for webuis"),
},
)
3 changes: 3 additions & 0 deletions build/luarocks/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ exports_files(
[
"BUILD.luarocks.bazel",
"luarocks_wrap_script.lua",
"templates/luarocks_exec.sh",
"templates/luarocks_make.sh",
"templates/luarocks_target.sh",
],
visibility = ["//visibility:public"],
)
Expand Down
Loading