refactor: improve abstractions

This commit is contained in:
Daylin Morgan 2023-09-07 17:55:58 -05:00
parent 2ad23ad783
commit 98a1c74c04
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F
4 changed files with 61 additions and 44 deletions

View file

@ -1,8 +1,10 @@
import std/[os, osproc, strformat, strutils, tables, terminal] import std/[os, osproc, sequtils, strformat, strutils, tables, terminal]
import forge/[config, utils, term] import forge/[config, utils, term, zig]
proc genFlags(target: string, args: seq[string] = @[]): seq[string] =
let triplet = parseTriplet(target)
proc genFlags(triplet: Triplet, args: seq[string] = @[]): seq[string] =
addFlag "cpu" addFlag "cpu"
addFlag "os" addFlag "os"
@ -27,10 +29,11 @@ proc cc(target: string, dryrun: bool = false, nimble: bool = false, args: seq[st
if args.len == 0: if args.len == 0:
termErrQuit "expected additional arguments i.e. -- -d:release src/main.nim" termErrQuit "expected additional arguments i.e. -- -d:release src/main.nim"
checkTargets(@[target])
let let
targets = zigTargets()
ccArgs = genFlags(parseTriplet(target, targets), args)
rest = parseArgs(args) rest = parseArgs(args)
ccArgs = genFlags(target, rest)
baseCmd = if nimble: "nimble" else: "nim" baseCmd = if nimble: "nimble" else: "nim"
cmd = (@[baseCmd] & @["c"] & ccArgs & rest).join(" ") cmd = (@[baseCmd] & @["c"] & ccArgs & rest).join(" ")
@ -79,14 +82,15 @@ proc release(
if cfg.bins.len == 0: if cfg.bins.len == 0:
termErrQuit "expected at least 1 bin" termErrQuit "expected at least 1 bin"
checkTargets(cfg.targets.keys.toSeq())
if verbose: if verbose:
termEcho $cfg termEcho $cfg
if dryrun: if dryrun:
termEcho styleBright, fgBlue, "dry run...see below for commands" termEcho styleBright, fgBlue, "dry run...see below for commands"
let let
targets = zigTargets()
baseCmd = if nimble or cfg.nimble: "nimble" else: "nim" baseCmd = if nimble or cfg.nimble: "nimble" else: "nim"
rest = parseArgs(args) rest = parseArgs(args)
@ -101,7 +105,7 @@ proc release(
) & "'" ) & "'"
cmdParts &= @[baseCmd, "c"] cmdParts &= @[baseCmd, "c"]
cmdParts.add genFlags(parseTriplet(t, targets), rest) cmdParts.add genFlags(t, rest)
cmdParts.add "-d:release" cmdParts.add "-d:release"
cmdParts.add rest cmdParts.add rest
cmdParts.add outFlag cmdParts.add outFlag

View file

@ -1,6 +1,7 @@
import std/[json, macros, math, os, osproc, sequtils, strutils, strformat, terminal] import std/[json, macros, math, os, sequtils, strutils, strformat, terminal]
import term import term
from zig import zigTargets
proc columns*(items: seq[string]): string = proc columns*(items: seq[string]): string =
## return a list of items as equally spaced columns ## return a list of items as equally spaced columns
@ -21,24 +22,6 @@ template parseArgs*(args: seq[string]): seq[string] =
else: else:
args args
# based on https://github.com/enthus1ast/zigcc
template callZig*(zigCmd: string) =
# Set the zig compiler to call and append args
var args = @[zigCmd]
args &= commandLineParams()
# Start process
let process = startProcess(
"zig",
args = args,
options = {poStdErrToStdOut, poUsePath, poParentStreams}
)
# Get the code so we can carry across the exit code
let exitCode = process.waitForExit()
# Clean up
close process
quit exitCode
type type
Triplet* = object Triplet* = object
cpu: string cpu: string
@ -47,29 +30,25 @@ type
proc `$`*(t: Triplet): string = &"{t.cpu}-{t.os}-{t.libc}" proc `$`*(t: Triplet): string = &"{t.cpu}-{t.os}-{t.libc}"
proc parseTriplet*(s: string, targets: seq[string]): Triplet = proc checkTargets*(targets: seq[string]) =
if s notin targets: let knownTargets = zigTargets()
termErr &"unknown target: {s}", "", "must be one of:" var unknownTargets: seq[string]
stderr.writeLine targets.columns for target in targets:
if target notin knowntargets:
unknownTargets.add target
if unknownTargets.len != 0:
termErr &"unknown target(s): " & unknownTargets.join(", ")
termEcho "must be one of:"
stderr.writeLine knownTargets.columns
quit 1 quit 1
proc parseTriplet*(s: string): Triplet =
let parts = s.split("-") let parts = s.split("-")
result.cpu = parts[0] result.cpu = parts[0]
result.os = parts[1] result.os = parts[1]
result.libc = parts[2] result.libc = parts[2]
proc zigExists*() =
if (findExe "zig") == "":
termErr "zig not found"
termErr " forge requires a working installation of zig"
termErr " see: https://ziglang.org/download/"
quit 1
proc zigTargets*(): seq[string] =
let (output, _) = execCmdEx "zig targets"
parseJson(output)["libc"].to(seq[string])
macro addFlag*(arg: untyped): untyped = macro addFlag*(arg: untyped): untyped =
let let
flag = "--" & arg.strVal & ":" flag = "--" & arg.strVal & ":"

34
src/forge/zig.nim Normal file
View file

@ -0,0 +1,34 @@
import std/[json, os, osproc, terminal]
import term
proc zigTargets*(): seq[string] =
let (output, _) = execCmdEx "zig targets"
parseJson(output)["libc"].to(seq[string])
# based on https://github.com/enthus1ast/zigcc
template callZig*(zigCmd: string) =
# Set the zig compiler to call and append args
var args = @[zigCmd]
args &= commandLineParams()
# Start process
let process = startProcess(
"zig",
args = args,
options = {poStdErrToStdOut, poUsePath, poParentStreams}
)
# Get the code so we can carry across the exit code
let exitCode = process.waitForExit()
# Clean up
close process
quit exitCode
proc zigExists*() =
if (findExe "zig") == "":
termErr "zig not found"
termErr " forge requires a working installation of zig"
termErr " see: https://ziglang.org/download/"
quit 1

View file

@ -1,3 +1,3 @@
import forge/utils import forge/zig
callZig("cc") callZig("cc")