txt2deps.nim: new utility

This commit is contained in:
Anna “CyberTailor” 2023-04-25 18:04:52 +05:00
parent 325bfa4fea
commit ea0456c81b
Signed by: CyberTaIlor
GPG Key ID: E7B76EDC50864BB1
6 changed files with 137 additions and 3 deletions

View File

@ -1,3 +1,9 @@
2023-04-25 Anna <cyber@sysrq.in>
* nimbus.nim: drop support for patched Nim compilers
* txt2deps: new utility
2022-07-13 Anna <cyber@sysrq.in>
* 0.3.0, 0.3.1 tagged

View File

@ -135,6 +135,7 @@ ninja
sudo ninja install
.Ed
.Sh SEE ALSO
.Xr txt2deps 1
.Bl -bullet -width 1n
.It
.Lk https://nim-lang.org/docs/nimc.html "Nim compiler user guide"

47
man/txt2man.1 Normal file
View File

@ -0,0 +1,47 @@
.\" SPDX-FileType: DOCUMENTATION
.\" SPDX-FileCopyrightText: 2023 Anna <cyber@sysrq.in>
.\" SPDX-License-Identifier: BSD-3-Clause
.Dd April 25, 2023
.Dt TXT2DEPS 1
.Os
.Sh NAME
.Nm txt2deps
.Nd convert plain list of files to GCC-style depfiles
.Sh SYNOPSIS
.Nm
.Fl T : Ns Ar path
.Op Fl i : Ns Ar path
.Op Fl o : Ns Ar path
.Sh DESCRIPTION
The
.Nm
utility takes a file with newline-separated list of filenames as input and outputs one
.Xr make 1
rule containing the target file name for that source file, a colon, and the names of all the included files.
.Pp
The arguments are as follows:
.Bl -tag -width Ds
.It Fl T : Ns Ar path
Target file name.
.
.It Fl i : Ns Ar path
Input file name.
The default is the standard input.
.
.It Fl o : Ns Ar path
Output file name.
The default is the standard output.
.El
.
.Sh EXIT STATUS
.Ex -std
.Sh EXAMPLES
Generate a depfile:
.Dl txt2deps -O test < ~/.cache/nim/test/test.deps > test.d
.Sh SEE ALSO
.Xr make 1 ,
.Xr nimbus 1
.Sh AUTHORS
.An -split
.An Anna
.Aq Mt cyber@sysrq.in

View File

@ -8,7 +8,7 @@ author = "Anna"
description = "A Nim build system"
license = "BSD"
bin = @["nimbus"]
bin = @["nimbus", "txt2deps"]
srcDir = "src"
installExt = @["nim"]

View File

@ -1,5 +1,5 @@
# SPDX-FileCopyrightText: Copyright (C) Dominik Picheta. All rights reserved.
# SPDX-FileCopyrightText: 2022 Anna <cyber@sysrq.in>
# SPDX-FileCopyrightText: 2023 Anna <cyber@sysrq.in>
# SPDX-License-Identifier: BSD-3-Clause
import std/[logging, os, parseopt, strutils]
@ -13,7 +13,7 @@ type
nimbleDir*: string
binDir*: string
nim*: string # Nim compiler location
url*: string
url*: string
sourceDir*: string
buildDir*: string
logger*: ConsoleLogger

80
src/txt2deps.nim Normal file
View File

@ -0,0 +1,80 @@
# SPDX-FileCopyrightText: 2023 Anna <cyber@sysrq.in>
# SPDX-License-Identifier: BSD-3-Clause
import std/[os, parseopt, strutils]
type
Options = object
invalidArguments: bool
showHelp: bool
target: string
inFilePath: string
outFilePath: string
const help* = """
Usage: txt2deps -T:path [-h] [-i:path] [-o:path]
Converts plain list of files to GCC-style depfile.
arguments:
-h, --help Show this help message and exit.
-T:path Build target.
-i:path Input deps file (default: stdin).
-o:path Output deps file (default: stdout).
""".strip()
proc writeHelp(status: int = QuitSuccess) =
echo(help)
quit(status)
proc parseCmdline(): Options =
# Init the defaults
result.invalidArguments = false
result.showHelp = false
for kind, key, val in getOpt():
case kind
of cmdLongOption, cmdShortOption:
case key.normalize()
of "help", "h": result.showHelp = true
of "t": result.target = val
of "i": result.inFilePath = val
of "o": result.outFilePath = val
else: result.invalidArguments = true
of cmdArgument:
result.invalidArguments = true
of cmdEnd:
discard
if result.target.len == 0:
result.invalidArguments = true
func quoteMake(s: string): string =
return s.multiReplace(
(" ", "\\ "),
("#", "\\#")
)
proc main(options: Options) =
var inFile = stdin
var outFile = stdout
if options.inFilePath.len != 0:
inFile = open(options.inFilePath)
if options.outFilePath.len != 0:
outFile = open(options.outFilePath, fmWrite)
outFile.write(options.target.quoteMake & ": \\" & '\n')
for line in inFile.lines:
if line.len == 0 or not fileExists(line):
continue
outFile.write('\t' & line.quoteMake & " \\" & '\n')
outFile.close()
when isMainModule:
let opt = parseCmdLine()
if opt.showHelp:
writeHelp()
if opt.invalidArguments:
writeHelp(QuitFailure)
main(opt)