2019-04-04 11:33:38 +03:00
|
|
|
# Copyright (c) 2019 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
load("@bazel_skylib//lib:paths.bzl", "paths")
|
|
|
|
|
2019-04-12 14:10:16 +03:00
|
|
|
daml_provider = provider(doc = "DAML provider", fields = {
|
|
|
|
"dalf": "The DAML-LF file.",
|
|
|
|
"dar": "The packaged archive.",
|
|
|
|
"srcjar": "The generated Scala sources as srcjar.",
|
2019-04-04 11:33:38 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
def _daml_impl_compile_dalf(ctx):
|
|
|
|
# Call damlc compile
|
|
|
|
compile_args = ctx.actions.args()
|
|
|
|
compile_args.add("compile")
|
|
|
|
compile_args.add(ctx.file.main_src)
|
|
|
|
compile_args.add("--output", ctx.outputs.dalf.path)
|
|
|
|
if ctx.attr.target:
|
|
|
|
compile_args.add("--target", ctx.attr.target)
|
|
|
|
ctx.actions.run(
|
|
|
|
inputs = depset([ctx.file.main_src] + ctx.files.srcs),
|
|
|
|
outputs = [ctx.outputs.dalf],
|
|
|
|
arguments = [compile_args],
|
|
|
|
progress_message = "Compiling DAML into DAML-LF archive %s" % ctx.outputs.dalf.short_path,
|
2019-04-05 19:11:13 +03:00
|
|
|
executable = ctx.executable.damlc,
|
2019-04-04 11:33:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
def _daml_impl_package_dar(ctx):
|
|
|
|
# Call damlc package
|
|
|
|
package_args = ctx.actions.args()
|
|
|
|
package_args.add("package")
|
|
|
|
package_args.add(ctx.file.main_src)
|
|
|
|
package_args.add(ctx.attr.name)
|
|
|
|
if ctx.attr.target:
|
|
|
|
package_args.add("--target", ctx.attr.target)
|
|
|
|
package_args.add("--output")
|
|
|
|
package_args.add(ctx.outputs.dar.path)
|
|
|
|
ctx.actions.run(
|
|
|
|
inputs = [ctx.file.main_src] + ctx.files.srcs,
|
|
|
|
outputs = [ctx.outputs.dar],
|
|
|
|
arguments = [package_args],
|
|
|
|
progress_message = "Creating DAR package %s" % ctx.outputs.dar.basename,
|
2019-04-05 19:11:13 +03:00
|
|
|
executable = ctx.executable.damlc,
|
2019-04-04 11:33:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
def _daml_outputs_impl(name):
|
|
|
|
patterns = {
|
|
|
|
"dalf": "{name}.dalf",
|
|
|
|
"dar": "{name}.dar",
|
|
|
|
"srcjar": "{name}.srcjar",
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
k: v.format(name = name)
|
|
|
|
for (k, v) in patterns.items()
|
|
|
|
}
|
|
|
|
|
|
|
|
def _daml_compile_impl(ctx):
|
|
|
|
_daml_impl_compile_dalf(ctx)
|
|
|
|
_daml_impl_package_dar(ctx)
|
2019-04-12 14:10:16 +03:00
|
|
|
|
2019-04-04 11:33:38 +03:00
|
|
|
# DAML provider
|
|
|
|
daml = daml_provider(
|
|
|
|
dalf = ctx.outputs.dalf,
|
|
|
|
dar = ctx.outputs.dar,
|
|
|
|
)
|
|
|
|
return [daml]
|
|
|
|
|
|
|
|
def _daml_compile_outputs_impl(name):
|
|
|
|
patterns = {
|
|
|
|
"dalf": "{name}.dalf",
|
|
|
|
"dar": "{name}.dar",
|
|
|
|
}
|
|
|
|
return {
|
2019-04-12 14:10:16 +03:00
|
|
|
k: v.format(name = name)
|
2019-04-04 11:33:38 +03:00
|
|
|
for (k, v) in patterns.items()
|
|
|
|
}
|
|
|
|
|
|
|
|
# TODO(JM): The daml_compile() is same as daml(), but without the codegen bits.
|
|
|
|
# All of this needs a cleanup once we understand the needs for daml related rules.
|
|
|
|
daml_compile = rule(
|
|
|
|
implementation = _daml_compile_impl,
|
|
|
|
attrs = {
|
|
|
|
"main_src": attr.label(
|
|
|
|
allow_single_file = [".daml"],
|
|
|
|
mandatory = True,
|
2019-04-12 14:10:16 +03:00
|
|
|
doc = "The main DAML file that will be passed to the compiler.",
|
2019-04-04 11:33:38 +03:00
|
|
|
),
|
|
|
|
"srcs": attr.label_list(
|
|
|
|
allow_files = [".daml"],
|
|
|
|
default = [],
|
2019-04-12 14:10:16 +03:00
|
|
|
doc = "Other DAML files that compilation depends on.",
|
2019-04-04 11:33:38 +03:00
|
|
|
),
|
2019-04-12 14:10:16 +03:00
|
|
|
"target": attr.string(doc = "DAML-LF version to output"),
|
2019-04-05 19:11:13 +03:00
|
|
|
"damlc": attr.label(
|
2019-04-04 11:33:38 +03:00
|
|
|
executable = True,
|
|
|
|
cfg = "host",
|
|
|
|
allow_files = True,
|
|
|
|
default = Label("//daml-foundations/daml-tools/da-hs-damlc-app"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
executable = False,
|
|
|
|
outputs = _daml_compile_outputs_impl,
|
|
|
|
)
|
|
|
|
|
|
|
|
def _daml_test_impl(ctx):
|
|
|
|
script = """
|
2019-05-28 16:36:42 +03:00
|
|
|
{damlc} test --files {files}
|
2019-04-05 19:11:13 +03:00
|
|
|
""".format(damlc = ctx.executable.damlc.short_path, files = " ".join([f.short_path for f in ctx.files.srcs]))
|
2019-04-04 11:33:38 +03:00
|
|
|
|
|
|
|
ctx.actions.write(
|
|
|
|
output = ctx.outputs.executable,
|
|
|
|
content = script,
|
|
|
|
)
|
2019-06-12 18:18:45 +03:00
|
|
|
damlc_runfiles = ctx.attr.damlc[DefaultInfo].data_runfiles
|
|
|
|
runfiles = ctx.runfiles(
|
|
|
|
collect_data = True,
|
|
|
|
files = ctx.files.srcs,
|
|
|
|
).merge(damlc_runfiles)
|
2019-04-04 11:33:38 +03:00
|
|
|
return [DefaultInfo(runfiles = runfiles)]
|
|
|
|
|
|
|
|
daml_test = rule(
|
2019-04-12 14:10:16 +03:00
|
|
|
implementation = _daml_test_impl,
|
|
|
|
attrs = {
|
|
|
|
"srcs": attr.label_list(
|
|
|
|
allow_files = [".daml"],
|
|
|
|
default = [],
|
|
|
|
doc = "DAML source files to test.",
|
|
|
|
),
|
|
|
|
"damlc": attr.label(
|
|
|
|
executable = True,
|
|
|
|
cfg = "host",
|
|
|
|
allow_files = True,
|
|
|
|
default = Label("//daml-foundations/daml-tools/da-hs-damlc-app"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
test = True,
|
2019-04-04 11:33:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
_daml_binary_script_template = """
|
|
|
|
#!/usr/bin/env sh
|
|
|
|
{java} -jar {sandbox} $@ {dar}
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _daml_binary_impl(ctx):
|
|
|
|
script = _daml_binary_script_template.format(
|
|
|
|
java = ctx.executable._java.short_path,
|
|
|
|
sandbox = ctx.file._sandbox.short_path,
|
|
|
|
dar = ctx.file.dar.short_path,
|
|
|
|
)
|
|
|
|
|
|
|
|
ctx.actions.write(
|
|
|
|
output = ctx.outputs.executable,
|
|
|
|
content = script,
|
|
|
|
)
|
|
|
|
|
|
|
|
runfiles = ctx.runfiles(
|
|
|
|
files = [ctx.file.dar, ctx.file._sandbox, ctx.executable._java],
|
|
|
|
)
|
|
|
|
|
|
|
|
return [DefaultInfo(runfiles = runfiles)]
|
|
|
|
|
|
|
|
daml_binary = rule(
|
|
|
|
implementation = _daml_binary_impl,
|
|
|
|
attrs = {
|
|
|
|
"dar": attr.label(
|
|
|
|
allow_single_file = [".dar"],
|
|
|
|
mandatory = True,
|
|
|
|
doc = "The DAR to execute in the sandbox.",
|
|
|
|
),
|
|
|
|
"_sandbox": attr.label(
|
|
|
|
cfg = "target",
|
|
|
|
allow_single_file = [".jar"],
|
|
|
|
default = Label("//ledger/sandbox:sandbox-binary_deploy.jar"),
|
|
|
|
),
|
|
|
|
"_java": attr.label(
|
|
|
|
executable = True,
|
|
|
|
cfg = "target",
|
|
|
|
allow_files = True,
|
|
|
|
default = Label("@bazel_tools//tools/jdk:java"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
executable = True,
|
|
|
|
)
|
|
|
|
"""
|
|
|
|
Executable target that runs the DAML sandbox on the given DAR package.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
```
|
|
|
|
daml_binary(
|
|
|
|
name = "example-exec",
|
|
|
|
dar = ":dar-out/com/digitalasset/sample/example/0.1/example-0.1.dar",
|
|
|
|
)
|
|
|
|
```
|
|
|
|
|
|
|
|
This target can be executed as follows:
|
|
|
|
|
|
|
|
```
|
|
|
|
$ bazel run //:example-exec
|
|
|
|
```
|
|
|
|
|
|
|
|
Command-line arguments can be passed to the sandbox as follows:
|
|
|
|
|
|
|
|
```
|
|
|
|
$ bazel run //:example-exec -- --help
|
|
|
|
```
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _daml_compile_dalf_output_impl(name):
|
|
|
|
return {"dalf": name + ".dalf"}
|
|
|
|
|
|
|
|
dalf_compile = rule(
|
|
|
|
implementation = _daml_impl_compile_dalf,
|
|
|
|
attrs = {
|
|
|
|
"main_src": attr.label(
|
|
|
|
allow_single_file = [".daml"],
|
|
|
|
mandatory = True,
|
|
|
|
doc = "The main DAML file that will be passed to the compiler.",
|
|
|
|
),
|
|
|
|
"srcs": attr.label_list(
|
|
|
|
allow_files = [".daml"],
|
|
|
|
default = [],
|
|
|
|
doc = "Other DAML files that compilation depends on.",
|
|
|
|
),
|
|
|
|
"target": attr.string(doc = "DAML-LF version to output"),
|
2019-04-05 19:11:13 +03:00
|
|
|
"damlc": attr.label(
|
2019-04-04 11:33:38 +03:00
|
|
|
executable = True,
|
|
|
|
cfg = "host",
|
|
|
|
allow_files = True,
|
|
|
|
default = Label("//daml-foundations/daml-tools/da-hs-damlc-app"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
executable = False,
|
|
|
|
outputs = _daml_compile_dalf_output_impl,
|
|
|
|
)
|
|
|
|
"""
|
|
|
|
Stripped down version of daml_compile that does not package DALFs into DARs
|
|
|
|
"""
|
|
|
|
|
|
|
|
daml_sandbox_version = "6.0.0"
|