0

I am writing a genrule as follows :

genrule(
name = "create_run_script",
outs = ["run_script.sh"],
executable = True,
cmd = """
      cat > $@ << EOF
      #!/bin/bash
      cd ../dir/
      exec ./program "$$@"
      EOF
      """,

)

When I execute this I see the output

    # @external_repo//:create_run_script [action 'Executing genrule @ external_repo//:create_run_script']
(cd /home/marc/.cache/bazel/_bazel_marc/0877f3fef7c185b84693d3a53e00a8be/execroot/zoox && \
  exec env - \
    FLAGS_minloglevel=1 \
    LD_LIBRARY_PATH='' \
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
  /bin/bash -c 'source external/bazel_tools/tools/genrule/genrule-setup.sh; 
          cat > bazel-out/k8-fastbuild/bin/external/external_repo/run_script.sh << EOF
          #!/bin/bash
          cd ../external_repo/
          exec ./program "$@"
          EOF
          ')

However, when I open the actual shells script file I see

      #!/bin/bash
      cd ../external_repo/
      exec ./program ""
      EOF
      

The $@ symbol is no longer there ! How do I create a shell script that can take in the inputs from the shell using the Bazel genrule directive ?

marc
  • 797
  • 1
  • 9
  • 26

1 Answers1

4

The shell invocation that creates that script is expanding out $@; the heredoc needs to disable expansion:

genrule(
name = "create_run_script",
outs = ["run_script.sh"],
executable = True,
cmd = """
      cat > $@ << 'EOF'
      #!/bin/bash
      cd ../dir/
      exec ./program "$$@"
EOF
      """,
)

(Notice the quotes around 'EOF'.)

Benjamin Peterson
  • 19,297
  • 6
  • 32
  • 39