Guest User

nimr.nim

a guest
Jun 11th, 2017
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 3.77 KB | None | 0 0
  1. import
  2.   os, osproc, times, tables, strutils, strtabs,
  3.   al_term, al_str, birthcert
  4.  
  5. let
  6.   srcDir = getEnv("NIMR_SRC")
  7.   binDir = getEnv("NIMR_BIN")
  8.  
  9. var
  10.   startTime = epochTime()
  11.   nimOpt = initTable[string,string]()
  12.  
  13. proc test() =
  14.   if paramCount() < 1: die "At least one argument required!"
  15.   if srcDir == "": die "NIMR_SRC environmental variable blank!"
  16.   if binDir == "": die "NIMR_BIN environmental variable blank!"
  17.   if not dirExists(srcDir): die "NIMR_SRC directory doesn't exist!"
  18.   if not dirExists(binDir): die "NIMR_BIN directory doesn't exist!"
  19.   if findExe("nim") == "": die "Required executable `nim` not found!"
  20.  
  21. proc findSourceFile(cmd: string): string =
  22.   var baseFilename = cmd.changeFileExt("nim")
  23.   var fullPath = joinPath(getCurrentDir(), baseFilename)
  24.   if fileExists(fullPath): return fullPath
  25.   for eachFile in walkDirRec(srcDir):
  26.     let eachBaseFn = eachFile.splitPath().tail
  27.     if baseFilename == eachBaseFn: return eachFile.expandFilename
  28.   die "Cannot find source code for command '$1'" % [cmd]
  29.  
  30. proc procSetOptions(srcFile: string) =
  31.   # Set options based on executable (or link name) used:
  32.   when nimCompilerOutFile == "nimf":
  33.     nimOpt["d"] = "release"
  34.     nimOpt["opt"] = "speed"
  35.     nimOpt["verbosity"] = "1"
  36.   elif nimCompilerOutFile == "nims":
  37.     nimOpt["d"] = "release"
  38.     nimOpt["opt"] = "size"
  39.     nimOpt["verbosity"] = "1"
  40.   else:
  41.     nimOpt["d"] = "debug"
  42.     nimOpt["opt"] = "none"
  43.   # Set options from directives inside the source file:
  44.   let srcHandle = open(srcFile)
  45.   defer: srcHandle.close
  46.   while not srcHandle.endOfFile:
  47.     let srcLine = srcHandle.readLine.strip
  48.     if not srcLine.startsWith("##:"): continue
  49.     let splitKeyVal = srcLine[3..^1].split("=")
  50.     if splitKeyVal.len < 2: continue
  51.     let key = splitKeyVal[0].strip
  52.     let val = splitKeyVal[1..^1].join("=").strip
  53.     nimOpt[key] = val
  54.  
  55. proc compile() =
  56.   let srcFile = findSourceFile(paramStr(1))
  57.   let srcPathSplit = splitPath(srcFile)
  58.   # if srcPathSplit.head != "":
  59.   #   log "found source in: " & srcPathSplit.head
  60.   #   setCurrentDir(srcPathSplit.head)
  61.   procSetOptions(srcFile)
  62.   nimOpt["out"] = binDir / srcPathSplit.tail.changeFileExt("")
  63.   var nimCmd = "nim c"
  64.   for k, v in nimOpt:
  65.     nimCmd &= " --" & k & ":" & v
  66.   nimCmd &= " " & srcFile
  67.   log "nimr running `$1`" % [nimCmd]
  68.   echo "{"
  69.   var exitCode = execCmd(nimCmd)
  70.   echo "}"
  71.   let runSec = epochTime() - startTime
  72.   let infoStr = "`nim c` completed in $1 sec, exit status $2." % [$runSec, $exitCode]
  73.   if exitCode != 0: die infoStr
  74.   else: grn infoStr
  75.  
  76. proc run() =
  77.   var runCmd = " " & nimOpt["out"]
  78.   for argN in 2..paramCount():
  79.     runCmd &= " " & paramStr(argN)
  80.   #log "nimr running `$1`" % [runCmd]
  81.   #echo "{"
  82.   startTime = epochTime()
  83.   let exitCode = execCmd(runCmd)
  84.   let runSec = epochTime() - startTime
  85.   #echo "}"
  86.   let infoStr = "execution completed in $1 sec, exit status $2." % [$runSec, $exitCode]
  87.   if exitCode != 0: err infoStr
  88.   #else: red infoStr
  89.  
  90. proc optimizeSize() =
  91.   startTime = epochTime()
  92.   if findExe("upx") == "":
  93.     yel "Cannot find `upx` command for further size optimization!"
  94.     return
  95.   let optCmd = "upx -9qqq --ultra-brute " & nimOpt["out"]
  96.   log "binary size: $1 bytes" % getFileSize(nimOpt["out"]).thousandsSep
  97.   grn "nimr running `$1`" % [optCmd]
  98.   #echo "{"
  99.   startTime = epochTime()
  100.   let exitCode = execCmd(optCmd)
  101.   let runSec = epochTime() - startTime
  102.   #echo "}"
  103.   let infoStr = "upx completed in $1 sec, exit status $2." % [$runSec, $exitCode]
  104.   if exitCode == 0:
  105.     grn infoStr
  106.     log "binary size: $1 bytes" % getFileSize(nimOpt["out"]).thousandsSep
  107.   else: die infoStr
  108.  
  109.  
  110. when isMainModule:
  111.   test()
  112.   compile()
  113.   when nimCompilerOutFile == "nims": optimizeSize()
  114.   elif nimCompilerOutFile == "nimr": run()
Advertisement
Add Comment
Please, Sign In to add comment