aboutsummaryrefslogtreecommitdiff
path: root/kicad/lib/tools/make_eboard_lib.sh
blob: 1399513a1f73cc1f729eeaa93b615989b068c24f (plain)
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
59
60
61
62
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-or-later
# Build kicad/lib/eboard.kicad_sym:
#  - symbols generated by kipart from the CSV files in this folder,
#  - plus ready-made symbols from the *.kicad_sym files in this folder
#    (e.g. the HR911130A symbol from a KiCad library merge request).
#
#   kicad/lib/tools/make_eboard_lib.sh
#
# Needs kipart (pipx install kipart).
set -eu
here=$(cd "$(dirname "$0")" && pwd)
command -v kipart >/dev/null 2>&1 || {
    echo "kipart not found; install it with: pipx install kipart" >&2
    exit 1
}
out=$here/../eboard.kicad_sym
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT

kipart -b --bundle-style none -w -o "$tmp/raw.kicad_sym" \
    "$here"/ch347f.csv "$here"/rtl8211f.csv "$here"/sm410561n.csv
python3 -I "$here/kicad_sym_grid.py" "$tmp/raw.kicad_sym" > "$tmp/grid.kicad_sym"

# append the top-level symbols of the extra libraries
python3 -I - "$tmp/grid.kicad_sym" "$here"/*.kicad_sym > "$out" <<'EOF'
import re, sys


def match_paren(t, i):
    depth, j = 0, i
    while True:
        c = t[j]
        if c == '"':
            j += 1
            while t[j] != '"':
                j += 2 if t[j] == "\\" else 1
        elif c == "(":
            depth += 1
        elif c == ")":
            depth -= 1
            if depth == 0:
                return j + 1
        j += 1


base = open(sys.argv[1]).read()
blocks = []
for path in sys.argv[2:]:
    t = open(path).read()
    i = t.index("(", 1)
    while True:
        m = re.compile(r'\(symbol "').search(t, i)
        if not m:
            break
        e = match_paren(t, m.start())
        blocks.append(t[m.start():e])
        i = e
end = base.rstrip().rfind(")")
sys.stdout.write(base[:end] + "".join("  " + b + "\n" for b in blocks) + ")\n")
EOF
echo "wrote $out"