1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58

build / config / siso / ar.star [blame]

# -*- bazel-starlark -*-
# Copyright 2024 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Siso configuration for ar."""

load("@builtin//path.star", "path")
load("@builtin//struct.star", "module")

# https://en.wikipedia.org/wiki/Ar_(Unix)

def __file_header(fname, size):
    fid = fname + " " * 16
    header = fid[:16]
    if fname == "//":
        header += " " * 12  # file modification timestamp
        header += " " * 6  # owner id
        header += " " * 6  # group id
        header += " " * 8  # file mode
    else:
        header += "0" + " " * 11  # file modification timestamp
        header += "0" + " " * 5  # owner id
        header += "0" + " " * 5  # group id
        header += "644" + " " * 5  # file mode
    s = ("%d" % size) + " " * 10
    header += s[:10]  # file size
    header += "\x60\n"  # header trailer string
    return header

def __ref_fname(offset, fname):
    i = offset[fname]
    return "/%d" % i

def __padding(data):
    if len(data) % 2 == 0:
        return data
    return data + "\n"

def __ar_create(ctx, wd, ins):
    data = "!<thin>\n"
    offset = {}
    content = ""
    for fname in ins:
        offset[fname] = len(content)
        content += path.rel(wd, fname) + "/\n"
    content = __padding(content)
    data += __file_header("//", len(content))
    data += content
    for fname in ins:
        size = ctx.fs.size(fname)
        if size:
            data += __file_header(__ref_fname(offset, fname), size)
    return bytes(data)

ar = module(
    "ar",
    create = __ar_create,
)