daml/release/util.bzl
Moritz Kiefer 644e76d047
Build a zip containing all protobuf files (#5412)
* Build a zip containing all protobuf files

This builds a fat zip that contains the DAML-LF protobuf files and
the Ledger API protobuf files for a given release.

I’ll tackle the Azure config for uploading this to github releases afterwards.

To ease reviewing this is how the resulting zip looks like:

```
Archive:  bazel-bin/release/protobufs.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
     3593  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/package_service.proto
     1355  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/ledger_identity_service.proto
     6262  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/event.proto
     2282  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/testing/reset_service.proto
     1908  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/testing/time_service.proto
      886  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/ledger_offset.proto
     1327  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/transaction_filter.proto
     2380  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/command_submission_service.proto
     6208  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/transaction_service.proto
     3800  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/transaction.proto
     1506  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/completion.proto
     1893  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/ledger_configuration_service.proto
     5109  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/admin/party_management_service.proto
     3562  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/admin/package_management_service.proto
     3559  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/admin/config_management_service.proto
     5542  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/commands.proto
     4286  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/command_completion_service.proto
     2989  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/active_contracts_service.proto
     3393  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/command_service.proto
     5868  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/value.proto
     1810  2010-01-01 00:00   protos-0.0.0/com/digitalasset/ledger/api/v1/trace_context.proto
      558  2010-01-01 00:00   protos-0.0.0/com/digitalasset/daml_lf_1_6/daml_lf_0.proto
     1766  2010-01-01 00:00   protos-0.0.0/com/digitalasset/daml_lf_1_6/daml_lf.proto
    27265  2010-01-01 00:00   protos-0.0.0/com/digitalasset/daml_lf_1_6/daml_lf_1.proto
      558  2010-01-01 00:00   protos-0.0.0/com/digitalasset/daml_lf_1_7/daml_lf_0.proto
     1766  2010-01-01 00:00   protos-0.0.0/com/digitalasset/daml_lf_1_7/daml_lf.proto
    35657  2010-01-01 00:00   protos-0.0.0/com/digitalasset/daml_lf_1_7/daml_lf_1.proto
      558  2010-01-01 00:00   protos-0.0.0/com/digitalasset/daml_lf_1_8/daml_lf_0.proto
     1766  2010-01-01 00:00   protos-0.0.0/com/digitalasset/daml_lf_1_8/daml_lf.proto
    37168  2010-01-01 00:00   protos-0.0.0/com/digitalasset/daml_lf_1_8/daml_lf_1.proto
      596  2010-01-01 00:00   protos-0.0.0/com/digitalasset/daml_lf_dev/daml_lf_0.proto
     1804  2010-01-01 00:00   protos-0.0.0/com/digitalasset/daml_lf_dev/daml_lf.proto
    39864  2010-01-01 00:00   protos-0.0.0/com/digitalasset/daml_lf_dev/daml_lf_1.proto
```

changelog_begin
changelog_end

* Try to get tar from PATH

* Address review comments
2020-04-03 14:16:15 +02:00

170 lines
6.8 KiB
Python

# Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
load("//daml-lf/language:daml-lf.bzl", "LF_VERSIONS")
load("@build_environment//:configuration.bzl", "sdk_version")
def sdk_tarball(name, version):
native.genrule(
name = name,
srcs = [
":sdk-config.yaml.tmpl",
":install.sh",
":install.bat",
"//ledger/sandbox:src/main/resources/logback.xml",
"//navigator/backend:src/main/resources/logback.xml",
"//extractor:src/main/resources/logback.xml",
"//ledger-service/http-json:release/json-api-logback.xml",
"//language-support/java/codegen:src/main/resources/logback.xml",
"//triggers/runner:src/main/resources/logback.xml",
"//daml-script/runner:src/main/resources/logback.xml",
"//:NOTICES",
"//daml-assistant:daml-dist",
"//compiler/damlc:damlc-dist",
"//compiler/daml-extension:vsix",
"//daml-assistant/daml-helper:daml-helper-dist",
"//language-support/ts/codegen:daml2js-dist",
"//templates:templates-tarball.tar.gz",
"//triggers/daml:daml-trigger-dars",
"//daml-script/daml:daml-script-dars",
"//daml-assistant/daml-sdk:sdk_deploy.jar",
],
outs = ["{}.tar.gz".format(name)],
cmd = """
# damlc
VERSION={version}
OUT=sdk-$$VERSION
mkdir -p $$OUT
cp $(location //:NOTICES) $$OUT/NOTICES
cp $(location :install.sh) $$OUT/install.sh
cp $(location :install.bat) $$OUT/install.bat
cp $(location :sdk-config.yaml.tmpl) $$OUT/sdk-config.yaml
sed -i "s/__VERSION__/$$VERSION/" $$OUT/sdk-config.yaml
mkdir -p $$OUT/daml
tar xf $(location //daml-assistant:daml-dist) --strip-components=1 -C $$OUT/daml
mkdir -p $$OUT/damlc
tar xf $(location //compiler/damlc:damlc-dist) --strip-components=1 -C $$OUT/damlc
mkdir -p $$OUT/daml-libs
cp -t $$OUT/daml-libs $(locations //triggers/daml:daml-trigger-dars)
cp -t $$OUT/daml-libs $(locations //daml-script/daml:daml-script-dars)
mkdir -p $$OUT/daml-helper
tar xf $(location //daml-assistant/daml-helper:daml-helper-dist) --strip-components=1 -C $$OUT/daml-helper
mkdir -p $$OUT/daml2js
tar xf $(location //language-support/ts/codegen:daml2js-dist) --strip-components=1 -C $$OUT/daml2js
mkdir -p $$OUT/studio
cp $(location //compiler/daml-extension:vsix) $$OUT/studio/daml-bundled.vsix
mkdir -p $$OUT/templates
tar xf $(location //templates:templates-tarball.tar.gz) --strip-components=1 -C $$OUT/templates
mkdir -p $$OUT/daml-sdk
cp $(location //daml-assistant/daml-sdk:sdk_deploy.jar) $$OUT/daml-sdk/daml-sdk.jar
cp -L $(location //ledger-service/http-json:release/json-api-logback.xml) $$OUT/daml-sdk/
cp -L $(location //ledger/sandbox:src/main/resources/logback.xml) $$OUT/daml-sdk/sandbox-logback.xml
cp -L $(location //navigator/backend:src/main/resources/logback.xml) $$OUT/daml-sdk/navigator-logback.xml
cp -L $(location //extractor:src/main/resources/logback.xml) $$OUT/daml-sdk/extractor-logback.xml
cp -L $(location //language-support/java/codegen:src/main/resources/logback.xml) $$OUT/daml-sdk/codegen-logback.xml
cp -L $(location //triggers/runner:src/main/resources/logback.xml) $$OUT/daml-sdk/trigger-logback.xml
cp -L $(location //daml-script/runner:src/main/resources/logback.xml) $$OUT/daml-sdk/script-logback.xml
tar c --format=ustar $$OUT \
--owner=0 --group=0 --numeric-owner --mtime=2000-01-01\ 00:00Z --sort=name \
| gzip -n > $@
""".format(version = version),
visibility = ["//visibility:public"],
)
def _protos_zip_impl(ctx):
posix = ctx.toolchains["@rules_sh//sh/posix:toolchain_type"]
tmp_dir = ctx.actions.declare_directory("tmp_dir")
zipper_args_file = ctx.actions.declare_file(
ctx.label.name + ".zipper_args",
)
tools = [ctx.executable.tar, ctx.executable.gzip]
ctx.actions.run_shell(
inputs = [ctx.file.ledger_api_tarball] + ctx.files.daml_lf_tarballs,
outputs = [tmp_dir],
tools = tools,
command = """
set -eou pipefail
export PATH=$PATH:{path}
tar xf {ledger_api_tarball} -C {tmp_dir}
for file in {lf_tarballs}
do
tar xf $file -C {tmp_dir}
done
""".format(
ledger_api_tarball = ctx.file.ledger_api_tarball.path,
tmp_dir = tmp_dir.path,
lf_tarballs = " ".join([f.path for f in ctx.files.daml_lf_tarballs]),
path = ":".join(["$PWD/`dirname {tool}`".format(tool = tool.path) for tool in tools]),
),
)
# zipper does not have an option to recursively zip files so we
# use find to list the files.
ctx.actions.run_shell(
outputs = [zipper_args_file],
inputs = [tmp_dir],
command = """
{find} -L {tmp_dir} -type f -printf "protos-{version}/%P=%p\n" > {args_file}
""".format(
version = sdk_version,
find = posix.commands["find"],
sed = posix.commands["sed"],
tmp_dir = tmp_dir.path,
args_file = zipper_args_file.path,
),
)
ctx.actions.run(
outputs = [ctx.outputs.out],
inputs = [zipper_args_file, tmp_dir],
executable = ctx.executable.zipper,
arguments = ["cC", ctx.outputs.out.path, "@" + zipper_args_file.path],
)
protos_zip = rule(
implementation = _protos_zip_impl,
attrs = {
"daml_lf_tarballs": attr.label_list(
allow_files = True,
default = [
Label("//daml-lf/archive:daml_lf_{}_archive_proto_tarball.tar.gz".format(version))
for version in LF_VERSIONS
],
),
"ledger_api_tarball": attr.label(allow_single_file = True, default = Label("//ledger-api/grpc-definitions:ledger-api-protos.tar.gz")),
"zipper": attr.label(
default = Label("@bazel_tools//tools/zip:zipper"),
cfg = "host",
executable = True,
allow_files = True,
),
"tar": attr.label(
default = Label("@tar_dev_env//:tar"),
cfg = "host",
executable = True,
allow_files = True,
),
"gzip": attr.label(
default = Label("@gzip_dev_env//:gzip"),
cfg = "host",
executable = True,
allow_files = True,
),
},
outputs = {
"out": "%{name}.zip",
},
toolchains = ["@rules_sh//sh/posix:toolchain_type"],
)