• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# -*- bazel-starlark -*-
2# Copyright 2024 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Siso configuration for ar."""
6
7load("@builtin//path.star", "path")
8load("@builtin//struct.star", "module")
9
10# https://en.wikipedia.org/wiki/Ar_(Unix)
11
12def __file_header(fname, size):
13    fid = fname + " " * 16
14    header = fid[:16]
15    if fname == "//":
16        header += " " * 12  # file modification timestamp
17        header += " " * 6  # owner id
18        header += " " * 6  # group id
19        header += " " * 8  # file mode
20    else:
21        header += "0" + " " * 11  # file modification timestamp
22        header += "0" + " " * 5  # owner id
23        header += "0" + " " * 5  # group id
24        header += "644" + " " * 5  # file mode
25    s = ("%d" % size) + " " * 10
26    header += s[:10]  # file size
27    header += "\x60\n"  # header trailer string
28    return header
29
30def __ref_fname(offset, fname):
31    i = offset[fname]
32    return "/%d" % i
33
34def __padding(data):
35    if len(data) % 2 == 0:
36        return data
37    return data + "\n"
38
39def __ar_create(ctx, wd, ins):
40    data = "!<thin>\n"
41    offset = {}
42    content = ""
43    for fname in ins:
44        offset[fname] = len(content)
45        content += path.rel(wd, fname) + "/\n"
46    content = __padding(content)
47    data += __file_header("//", len(content))
48    data += content
49    for fname in ins:
50        size = ctx.fs.size(fname)
51        if size:
52            data += __file_header(__ref_fname(offset, fname), size)
53    return bytes(data)
54
55ar = module(
56    "ar",
57    create = __ar_create,
58)
59