Copy a directory to a new directory in Bazel

2.8k views Asked by At

essentially all I want is cp -r src/ dist/, but for some reason I simply cannot get this to work.

Currently I am trying:

filegroup(
    name = "src_files",
    srcs = glob([
        "src/**",
    ]),
)
filegroup(
    name = "dist_files",
    srcs = glob([
        "dist/**"
    ]),
)
genrule(
    name = "copy",
    srcs = ["//packages/variables:src_files"],
    outs = ["//packages/variables:dist_files"],
    cmd = "cp -R $(locations //packages/variables:src_files) $(locations //packages/variables:dist_files)"
)

I've gone through at least 4 pages of google and the docs, but it seems unless I create a genrule and manually specify all 100 files in the rule it won't work?

1

There are 1 answers

0
Jon L On

@JamesSharpe had what I was missing, updated the BUILD file to:

filegroup(
    name = "src_files",
    srcs = glob([
        "src/**",
    ]),
)
pkg_tar(
    name = "pack_srcs",
    extension = "tar.gz",
    srcs = [":src_files"],
)
genrule(
    name = "unpack_to_dist",
    srcs = [":pack_srcs"],
    outs = ["dist"],
    cmd = "mkdir $(RULEDIR)/dist && tar -C $(RULEDIR)/dist -zxvf $(SRCS)"
)

And was able to successfully pass this on to the downstream rule.