Add support for romfs, improve verbose logging, and sanitize file paths

This commit is contained in:
Joey Yakimowich-Payne 2018-06-25 18:29:06 +09:00
commit 2b48cd774c

View file

@ -12,6 +12,7 @@ type
compilerPath: string compilerPath: string
toolsPath: string toolsPath: string
romfsPath: string
outDir: string outDir: string
libs: string libs: string
@ -27,11 +28,17 @@ type
proc execProc(cmd: string, verbose: bool=false): string {.discardable.}= proc execProc(cmd: string, verbose: bool=false): string {.discardable.}=
result = "" result = ""
var var
p = startProcess(cmd, options = {poStdErrToStdOut, poUsePath, poEvalCommand}) p = startProcess(
cmd,
options={poStdErrToStdOut, poUsePath, poEvalCommand}
)
outp = outputStream(p) outp = outputStream(p)
line = newStringOfCap(120).TaintedString line = newStringOfCap(120).TaintedString
if verbose:
echo "Executing command: " & cmd
while true: while true:
if outp.readLine(line): if outp.readLine(line):
if verbose: if verbose:
@ -82,6 +89,7 @@ Options:
-a, --author:STR sets the author name for the generate NRO and NACP file -a, --author:STR sets the author name for the generate NRO and NACP file
-v, --version:STR sets the version information for the generated NRO and NACP -v, --version:STR sets the version information for the generated NRO and NACP
file file
-q, --romfsPath:PATH Path to use to build in a romfs image
-p, --icon:PATH sets the icon to use for the generated NRO and NACP -p, --icon:PATH sets the icon to use for the generated NRO and NACP
(defaults to "$DKP/libnx/default_icon.jpg) (defaults to "$DKP/libnx/default_icon.jpg)
-h, --help show this help -h, --help show this help
@ -92,7 +100,7 @@ Note, single letter options that take an argument require a colon. E.g. -p:PATH.
proc buildElf(buildInfo: BuildInfo): string = proc buildElf(buildInfo: BuildInfo): string =
echo "Building elf file..." echo "Building elf file..."
var cmd = "nim $args c " & var cmd = "nim $args c " &
"--os:nintendoswitch " & buildInfo.filename "--os:nintendoswitch " & buildInfo.filename.quoteShell
var args = "" var args = ""
if buildInfo.release: if buildInfo.release:
@ -173,6 +181,9 @@ proc buildNro(buildInfo: BuildInfo): string =
var cmd = toolsPath / "elf2nro " & elfLocation & " " & result var cmd = toolsPath / "elf2nro " & elfLocation & " " & result
cmd &= " --icon=" & icon & " --nacp=" & nacpPath cmd &= " --icon=" & icon & " --nacp=" & nacpPath
if buildInfo.romfsPath != "":
cmd &= " --romfsdir=" & buildInfo.romfsPath
execProc cmd, buildInfo.verbose execProc cmd, buildInfo.verbose
proc buildAll(buildInfo: BuildInfo): seq[string] = proc buildAll(buildInfo: BuildInfo): seq[string] =
@ -200,6 +211,9 @@ proc build(buildType: string, buildInfo: BuildInfo): string =
of "lst": of "lst":
result = buildLst(buildInfo) result = buildLst(buildInfo)
proc sanitizePath(path: string): string =
path.expandFilename().quoteShell()
proc processArgs() = proc processArgs() =
let dkpEnv = "DEVKITPRO" let dkpEnv = "DEVKITPRO"
@ -215,10 +229,17 @@ proc processArgs() =
compilerPath: "", compilerPath: "",
toolsPath: "", toolsPath: "",
romfsPath: "",
outDir: "", outDir: "",
libs: "", libs: "",
includes: "" includes: "",
force: false,
verbose: false,
release: false,
elfLocation: ""
) )
var buildTypes: seq[string] = @[] var buildTypes: seq[string] = @[]
@ -227,7 +248,7 @@ proc processArgs() =
for kind, key, val in getopt(): for kind, key, val in getopt():
case kind case kind
of cmdArgument: of cmdArgument:
buildInfo.filename = key buildInfo.filename = key.sanitizePath()
of cmdLongOption, cmdShortOption: of cmdLongOption, cmdShortOption:
case key case key
of "devkitProPath", "d": of "devkitProPath", "d":
@ -236,9 +257,9 @@ proc processArgs() =
of "devkitCompilerPath", "c": of "devkitCompilerPath", "c":
buildInfo.compilerPath = val buildInfo.compilerPath = val
of "output", "o": of "output", "o":
buildInfo.outDir = val buildInfo.outDir = val.sanitizePath()
of "tools", "t": of "tools", "t":
buildInfo.toolsPath = val buildInfo.toolsPath = val.sanitizePath()
of "build", "b": of "build", "b":
buildTypes.add(val) buildTypes.add(val)
of "libs", "l": of "libs", "l":
@ -248,18 +269,24 @@ proc processArgs() =
of "name", "n": of "name", "n":
buildInfo.name = val buildInfo.name = val
of "icon", "p": of "icon", "p":
buildInfo.icon = val buildInfo.icon = val.sanitizePath()
of "version", "v": of "version", "v":
buildInfo.version = val buildInfo.version = val
of "forceBuild", "f": of "forceBuild", "f":
buildInfo.force = true buildInfo.force = true
of "release", "r": of "release", "r":
buildInfo.release = true buildInfo.release = true
of "romfsPath", "romfsDir", "q":
buildInfo.romfsPath = val.sanitizePath()
of "verbose": of "verbose":
buildInfo.verbose = true buildInfo.verbose = true
of "help", "h": of "help", "h":
writeHelp() writeHelp()
quit(0) quit(0)
else:
writeHelp()
echo "Error invalid argument: \"" & key & "\""
quit(1)
of cmdEnd: assert(false) # cannot happen of cmdEnd: assert(false) # cannot happen
if buildInfo.filename == "": if buildInfo.filename == "":
@ -274,13 +301,13 @@ proc processArgs() =
buildInfo.name = buildInfo.filename.splitFile().name buildInfo.name = buildInfo.filename.splitFile().name
if buildInfo.outDir == "": if buildInfo.outDir == "":
buildInfo.outDir = "build" buildInfo.outDir = expandFilename("build")
if not dirExists buildInfo.outDir: if not dirExists buildInfo.outDir:
createDir buildInfo.outDir createDir buildInfo.outDir
if buildInfo.dkpPath == "": if buildInfo.dkpPath == "":
buildInfo.dkpPath = getEnv(dkpEnv, "") buildInfo.dkpPath = getEnv(dkpEnv, "").sanitizePath()
if buildInfo.dkpPath == "" and not dkpEnv.existsEnv(): if buildInfo.dkpPath == "" and not dkpEnv.existsEnv():
writeHelp() writeHelp()
raise newException(Exception, dkpEnv & " path must be set!") raise newException(Exception, dkpEnv & " path must be set!")
@ -298,6 +325,8 @@ proc processArgs() =
putEnv("SWITCHLIBS", getEnv("SWITCHLIBS") & " " & buildInfo.libs) putEnv("SWITCHLIBS", getEnv("SWITCHLIBS") & " " & buildInfo.libs)
putEnv("SWITCHINCLUDES", getEnv("SWITCHINCLUDES") & " " & buildInfo.includes) putEnv("SWITCHINCLUDES", getEnv("SWITCHINCLUDES") & " " & buildInfo.includes)
echo "Building: $#..." % buildInfo.filename
buildInfo.elfLocation = buildElf(buildInfo) buildInfo.elfLocation = buildElf(buildInfo)
# Build the files # Build the files