kythe.io@v0.0.68-0.20240422202219-7225dbc01741/tools/build_rules/docker.bzl (about)

     1  def docker_build(
     2          name,
     3          image_name,
     4          src = "Dockerfile",
     5          deps = [],
     6          data = [],
     7          stage_only = False,
     8          tags = None,
     9          use_cache = False):
    10      done_marker = name + ".done"
    11  
    12      data_locs = []
    13      for d in data:
    14          data_locs.append("$(locations " + d + ")")
    15  
    16      args = []
    17      if not use_cache:
    18          args += ["--force-rm", "--no-cache"]
    19  
    20      # Use cp -p instead of cp --preserve=all because --preserve is a non-standard
    21      # flag.
    22      build_step = []
    23      if not stage_only:
    24          build_step = ["docker build -t %s %s ." % (image_name, " ".join(args))]
    25      cmd = [
    26          "set +u",
    27          "CTX=$@.ctx",
    28          'rm -rf "$$CTX"',
    29          'mkdir "$$CTX"',
    30          "srcs=(%s)" % (" ".join(data_locs)),
    31          "for src in $${srcs[@]}; do",
    32          "  dir=$$(dirname $$src)",
    33          "  if [[ $$dir == bazel-out/* ]]; then",
    34          "    dir=$${dir#*/*/*/}",
    35          "  fi",
    36          '  mkdir -p "$$CTX/$$dir"',
    37          '  cp -Lp $$src "$$CTX/$$dir"',
    38          "done",
    39          "cp $(location %s) \"$$CTX\"" % (src),
    40          'cd "$$CTX"',
    41      ] + build_step + [
    42          "cd - >/dev/null",
    43          "touch $@",
    44      ]
    45  
    46      native.genrule(
    47          name = name,
    48          cmd = "\n".join(cmd),
    49          srcs = [src] + data + deps,
    50          outs = [done_marker],
    51          tags = tags + ["docker"],
    52          local = 1,
    53      )