rules_scala icon indicating copy to clipboard operation
rules_scala copied to clipboard

Assembly and generate script

Open paulocoutinhox opened this issue 3 years ago • 10 comments

Hi,

How can i assemble and generate script, like this: https://github.com/sbt/sbt-assembly#prepending-a-launch-script

With pure scala/sbt we use this commands: https://github.com/cross-language-cpp/djinni-generator/blob/1fae66379760dd0ed3dc246265bf93a61d6222a5/.github/workflows/release.yaml#L22-L39

Im converting from sbt to use bazel. We already build with bazel with this:

load("@io_bazel_rules_scala//scala:scala.bzl", "scala_binary")

scala_binary(
    name = "djinni",
    srcs = glob(["source/**/*.scala"]),
    main_class = "djinni.Main",
    deps = [
        "@maven_djinni//:com_github_scopt_scopt_2_11",
        "@maven_djinni//:org_scala_lang_modules_scala_parser_combinators_2_11",
        "@maven_djinni//:org_yaml_snakeyaml",
    ],
    visibility = ["//visibility:public"],
)

Thanks for any help.

paulocoutinhox avatar Dec 17 '21 04:12 paulocoutinhox

@paulo-coutinho I would try looking at gen_rule, which would take djinni_deploy.jar and the script as inputs to produce a prependend jar. Let us know how it goes!

liucijus avatar Dec 17 '21 06:12 liucijus

Hi @liucijus,

Im trying, but blocked with somethings.

  1. I have created a new package with a BUILD file and with a stub.sh that will prepend the JAR file: image Ref: https://github.com/sbt/sbt-assembly/blob/9ff6a954d8d1638573fc7b0cc0041087f17ebacb/README.md#prepending-a-launch-script
  2. In generator/BUILD i put the genrule (the structure is correct?):
genrule(
    name = "generator",
    srcs = ["stub.sh", "djinni.jar"],
    outs = ["djinni"],
    cmd = "cat stub.sh djinni.jar > hello.run && chmod +x hello.run ",
    tools = [],
)
  1. When i execute bazel for the new package i get error (i know that stub.sh and djinni.jar files can be in wrong path):
bazel build //generator
INFO: Analyzed target //generator:generator (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: /Users/paulo/Developer/workspaces/cpp/djinni/generator/BUILD:1:8: Executing genrule //generator:generator failed: missing input file '//generator:djinni.jar'
Target //generator:generator failed to build
Use --verbose_failures to see the command lines of failed build steps.
ERROR: /Users/paulo/Developer/workspaces/cpp/djinni/generator/BUILD:1:8 Executing genrule //generator:generator failed: 1 input file(s) do not exist
INFO: Elapsed time: 0.112s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
FAILED: Build did NOT complete successfully
  1. The file djinni.jar is not on generator folder, but inside "bazel-bin/src/djinni.jar", because is was generate by an other command in other package (bazel build //src:djinni). How can i reference the required files in my generator/BUILD file? image

  2. The genrule has an outs param that i can't omit, why it is necessary and for what i will need it? It can be a variable for my output filename here cat stub.sh djinni.jar > ${OUTS_VAR} or something like this?

Thanks.

paulocoutinhox avatar Dec 17 '21 18:12 paulocoutinhox

I think you need to use vars to resolve location of the inputs to cat. https://docs.bazel.build/versions/main/be/make-variables.html#predefined_label_variables

liucijus avatar Dec 18 '21 09:12 liucijus

Hi,

I tried this:

genrule(
    name = "generator",
    srcs = ["stub.sh"],
    outs = ["djinni"],
    cmd = "cat stub.sh $(location @src:djinni)/djinni.jar > hello.run && chmod +x hello.run ",
    tools = [],
)

and

genrule(
    name = "generator",
    srcs = ["stub.sh"],
    outs = ["djinni"],
    cmd = "cat stub.sh $(location //src:djinni)/djinni.jar > hello.run && chmod +x hello.run ",
    tools = [],
)

But both tries give me the error:

bazel build //generator:generator
ERROR: /Users/paulo/Developer/workspaces/cpp/djinni/generator/BUILD:10:8: in cmd attribute of genrule rule //generator:generator: invalid label in $(location) expression: invalid repository name '@src:djinni': workspace names may contain only A-Z, a-z, 0-9, '-', '_' and '.'
ERROR: Analysis of target '//generator:generator' failed; build aborted: Analysis of target '//generator:generator' failed
INFO: Elapsed time: 0.122s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (1 packages loaded, 1 target configured)

Thanks.

paulocoutinhox avatar Dec 18 '21 16:12 paulocoutinhox

Your location parameter is incorrect. Example how to pass output jar as a parameter: https://github.com/bazelbuild/rules_scala/blob/b85d1225d0ddc9c376963eb0be86d9d546f25a4a/test_expect_failure/missing_direct_deps/internal_deps/BUILD#L104

All inputs need to be expanded the same way, and output should be referenced with $@

liucijus avatar Dec 20 '21 13:12 liucijus

Hi @liucijus,

Thanks for your time.

It work only if my genrule is in the same package of the package that is the "owner" of file. Example:

scala_binary(
    name = "djinni",
    srcs = glob(["source/**/*.scala"]),
    main_class = "djinni.Main",
    deps = [
        "@maven_djinni//:com_github_scopt_scopt_2_11",
        "@maven_djinni//:org_scala_lang_modules_scala_parser_combinators_2_11",
        "@maven_djinni//:org_yaml_snakeyaml",
    ],
    visibility = ["//visibility:public"],
)

genrule(
    name = "generator",
    srcs = ["stub.sh", "djinni.jar"],
    outs = ["djinni.run"],
    cmd = "cat $(location stub.sh) $(location djinni.jar) > $@ && chmod +x $@ ",
    tools = [],
)

image

But what im trying to do it create a package called "generator" that have it own BUILD file and use djinni.jar from package "src".

image

If i use the same content inside package "generator" i get error, since "djinni.jar" is from package "src". How can i reference "djinni.jar" from package "src" inside package "generator" outside it?

bazel build //generator          
INFO: Analyzed target //generator:generator (1 packages loaded, 3 targets configured).
INFO: Found 1 target...
ERROR: /Users/paulo/Developer/workspaces/cpp/djinni/generator/BUILD:10:8: Executing genrule //generator:generator failed: missing input file '//generator:djinni.jar'
Target //generator:generator failed to build
Use --verbose_failures to see the command lines of failed build steps.
ERROR: /Users/paulo/Developer/workspaces/cpp/djinni/generator/BUILD:10:8 Executing genrule //generator:generator failed: 1 input file(s) do not exist
INFO: Elapsed time: 0.137s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
FAILED: Build did NOT complete successfully

Thanks more one time.

paulocoutinhox avatar Dec 20 '21 19:12 paulocoutinhox

Hi,

I do it and work:

genrule(
    name = "generator",
    srcs = ["stub.sh", "//src:djinni.jar"],
    outs = ["djinni.run"],
    cmd = "cat $(location stub.sh) $(location //src:djinni.jar) > $@ && chmod +x $@ ",
    tools = [],
)

It is correct?

I made based on docs: https://docs.bazel.build/versions/main/build-ref.html

Thanks.

paulocoutinhox avatar Dec 20 '21 21:12 paulocoutinhox

Hi @liucijus,

How i can make it executable and all the params from bazel run be sent to the java executable?

genrule(
    name = "generator",
    srcs = ["stub.sh", "//src:djinni_deploy.jar"],
    outs = ["djinni"],
    cmd = "cat $(location :stub.sh) $(location //src:djinni_deploy.jar) > $@ && chmod +x $@",
    executable = True
)

When i execute bazel run the parameters are not sent to the final binary, but is recognized by bazel as bazel parameter:

bazel run //generator:generator --help
ERROR: --help :: Unrecognized option: --help

But if i call final binary generated direct, it work:

./bazel-bin/generator/djinni --help
Usage: djinni [options]

How i can pass parameters like "--help" to the binary instead of bazel when i execute bazel run?

Thanks.

paulocoutinhox avatar Dec 23 '21 19:12 paulocoutinhox

You need --: https://docs.bazel.build/versions/main/user-manual.html#run

liucijus avatar Dec 24 '21 08:12 liucijus

Hi, thanks, it is correct.

The generated jar/binary always return null on version:

Main.getClass.getPackage.getImplementationVersion

There is any way to add version to the package?

Thanks.

paulocoutinhox avatar Dec 28 '21 04:12 paulocoutinhox