mirror of
https://github.com/daylinmorgan/forge.git
synced 2024-12-25 11:50:44 -06:00
refactor: improve abstractions
This commit is contained in:
parent
2ad23ad783
commit
98a1c74c04
4 changed files with 61 additions and 44 deletions
|
@ -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 "os"
|
||||
|
||||
|
@ -27,10 +29,11 @@ proc cc(target: string, dryrun: bool = false, nimble: bool = false, args: seq[st
|
|||
if args.len == 0:
|
||||
termErrQuit "expected additional arguments i.e. -- -d:release src/main.nim"
|
||||
|
||||
checkTargets(@[target])
|
||||
|
||||
let
|
||||
targets = zigTargets()
|
||||
ccArgs = genFlags(parseTriplet(target, targets), args)
|
||||
rest = parseArgs(args)
|
||||
ccArgs = genFlags(target, rest)
|
||||
baseCmd = if nimble: "nimble" else: "nim"
|
||||
cmd = (@[baseCmd] & @["c"] & ccArgs & rest).join(" ")
|
||||
|
||||
|
@ -79,14 +82,15 @@ proc release(
|
|||
if cfg.bins.len == 0:
|
||||
termErrQuit "expected at least 1 bin"
|
||||
|
||||
checkTargets(cfg.targets.keys.toSeq())
|
||||
|
||||
if verbose:
|
||||
termEcho $cfg
|
||||
|
||||
if dryrun:
|
||||
termEcho styleBright, fgBlue, "dry run...see below for commands"
|
||||
|
||||
|
||||
let
|
||||
targets = zigTargets()
|
||||
baseCmd = if nimble or cfg.nimble: "nimble" else: "nim"
|
||||
rest = parseArgs(args)
|
||||
|
||||
|
@ -101,7 +105,7 @@ proc release(
|
|||
) & "'"
|
||||
|
||||
cmdParts &= @[baseCmd, "c"]
|
||||
cmdParts.add genFlags(parseTriplet(t, targets), rest)
|
||||
cmdParts.add genFlags(t, rest)
|
||||
cmdParts.add "-d:release"
|
||||
cmdParts.add rest
|
||||
cmdParts.add outFlag
|
||||
|
|
|
@ -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
|
||||
from zig import zigTargets
|
||||
|
||||
proc columns*(items: seq[string]): string =
|
||||
## return a list of items as equally spaced columns
|
||||
|
@ -21,24 +22,6 @@ template parseArgs*(args: seq[string]): seq[string] =
|
|||
else:
|
||||
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
|
||||
Triplet* = object
|
||||
cpu: string
|
||||
|
@ -47,29 +30,25 @@ type
|
|||
|
||||
proc `$`*(t: Triplet): string = &"{t.cpu}-{t.os}-{t.libc}"
|
||||
|
||||
proc parseTriplet*(s: string, targets: seq[string]): Triplet =
|
||||
if s notin targets:
|
||||
termErr &"unknown target: {s}", "", "must be one of:"
|
||||
stderr.writeLine targets.columns
|
||||
proc checkTargets*(targets: seq[string]) =
|
||||
let knownTargets = zigTargets()
|
||||
var unknownTargets: seq[string]
|
||||
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
|
||||
|
||||
|
||||
proc parseTriplet*(s: string): Triplet =
|
||||
let parts = s.split("-")
|
||||
result.cpu = parts[0]
|
||||
result.os = parts[1]
|
||||
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 =
|
||||
let
|
||||
flag = "--" & arg.strVal & ":"
|
||||
|
|
34
src/forge/zig.nim
Normal file
34
src/forge/zig.nim
Normal 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
|
||||
|
||||
|
|
@ -1,3 +1,3 @@
|
|||
import forge/utils
|
||||
import forge/zig
|
||||
|
||||
callZig("cc")
|
||||
|
|
Loading…
Reference in a new issue