| muslkit: musl static libraries from Alpine, without Alpine 5feb050 nandi 21h ago | 1 | ## The sysroot: a plain directory tree of unpacked Alpine packages, plus a |
| 2 | ## manifest of what was installed so `list` and re-runs are cheap. |
| 3 | |
| 4 | import std/[algorithm, os, sequtils, strutils] |
| 5 | import ./index |
| 6 | |
| 7 | const manifestPath* = ".muslkit/installed.tsv" |
| 8 | |
| 9 | type Installed* = object |
| 10 | name*, version*, repo*: string |
| 11 | |
| 12 | proc manifestFile*(sysroot: string): string = sysroot / manifestPath |
| 13 | |
| 14 | proc readManifest*(sysroot: string): seq[Installed] = |
| 15 | let path = manifestFile(sysroot) |
| 16 | if not fileExists(path): return |
| 17 | for line in readFile(path).splitLines: |
| 18 | if line.len == 0 or line.startsWith("#"): continue |
| 19 | let f = line.split('\t') |
| 20 | if f.len >= 3: |
| 21 | result.add Installed(name: f[0], version: f[1], repo: f[2]) |
| 22 | |
| 23 | proc writeManifest*(sysroot: string, entries: seq[Installed]) = |
| 24 | createDir manifestFile(sysroot).parentDir |
| 25 | var lines = @["# name\tversion\trepo — written by muslkit"] |
| 26 | for e in entries.sortedByIt(it.name): |
| 27 | lines.add e.name & "\t" & e.version & "\t" & e.repo |
| 28 | writeFile(manifestFile(sysroot), lines.join("\n") & "\n") |
| 29 | |
| 30 | proc record*(sysroot: string, pkgs: openArray[Pkg]) = |
| 31 | ## Merge freshly installed packages into the manifest, newest version winning. |
| 32 | var entries = readManifest(sysroot) |
| 33 | for pkg in pkgs: |
| 34 | entries.keepItIf(it.name != pkg.name) |
| 35 | entries.add Installed(name: pkg.name, version: pkg.version, repo: pkg.repo) |
| 36 | writeManifest(sysroot, entries) |
| 37 | |
| 38 | proc installed*(sysroot, name: string): bool = |
| 39 | readManifest(sysroot).anyIt(it.name == name) |
| 40 | |
| 41 | proc staticLibs*(sysroot: string): seq[string] = |
| 42 | ## Every .a in the sysroot, as absolute paths. |
| 43 | for dir in ["usr/lib", "lib", "usr/lib64"]: |
| 44 | let d = sysroot / dir |
| 45 | if dirExists(d): |
| 46 | for path in walkFiles(d / "*.a"): |
| 47 | result.add path |
| 48 | result.sort() |